blob: a9c4e5d8a99c50612f3dcc0e9ffc697bd9366012 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01002/*
3 * net/core/devlink.c - Network physical/parent device Netlink interface
4 *
5 * Heavily inspired by net/wireless/
6 * Copyright (c) 2016 Mellanox Technologies. All rights reserved.
7 * Copyright (c) 2016 Jiri Pirko <jiri@mellanox.com>
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01008 */
9
10#include <linux/kernel.h>
11#include <linux/module.h>
12#include <linux/types.h>
13#include <linux/slab.h>
14#include <linux/gfp.h>
15#include <linux/device.h>
16#include <linux/list.h>
17#include <linux/netdevice.h>
Jiri Pirkob8f97552019-03-24 11:14:37 +010018#include <linux/spinlock.h>
Moshe Shemeshb587bda2019-04-29 12:41:45 +030019#include <linux/refcount.h>
Jiri Pirko136bf272019-05-23 10:43:35 +020020#include <linux/workqueue.h>
Jiri Pirkobfcd3a42016-02-26 17:32:23 +010021#include <rdma/ib_verbs.h>
22#include <net/netlink.h>
23#include <net/genetlink.h>
24#include <net/rtnetlink.h>
25#include <net/net_namespace.h>
26#include <net/sock.h>
27#include <net/devlink.h>
Jiri Pirkoe5224f02016-07-12 18:05:03 +020028#define CREATE_TRACE_POINTS
29#include <trace/events/devlink.h>
30
Arkadi Sharshevsky11770092017-08-24 08:39:59 +020031static struct devlink_dpipe_field devlink_dpipe_fields_ethernet[] = {
32 {
David Ahern12bdc5e2017-08-30 17:07:30 -070033 .name = "destination mac",
Arkadi Sharshevsky11770092017-08-24 08:39:59 +020034 .id = DEVLINK_DPIPE_FIELD_ETHERNET_DST_MAC,
35 .bitwidth = 48,
36 },
37};
38
39struct devlink_dpipe_header devlink_dpipe_header_ethernet = {
40 .name = "ethernet",
41 .id = DEVLINK_DPIPE_HEADER_ETHERNET,
42 .fields = devlink_dpipe_fields_ethernet,
43 .fields_count = ARRAY_SIZE(devlink_dpipe_fields_ethernet),
44 .global = true,
45};
46EXPORT_SYMBOL(devlink_dpipe_header_ethernet);
47
Arkadi Sharshevsky3fb886e2017-08-24 08:40:00 +020048static struct devlink_dpipe_field devlink_dpipe_fields_ipv4[] = {
49 {
50 .name = "destination ip",
51 .id = DEVLINK_DPIPE_FIELD_IPV4_DST_IP,
52 .bitwidth = 32,
53 },
54};
55
56struct devlink_dpipe_header devlink_dpipe_header_ipv4 = {
57 .name = "ipv4",
58 .id = DEVLINK_DPIPE_HEADER_IPV4,
59 .fields = devlink_dpipe_fields_ipv4,
60 .fields_count = ARRAY_SIZE(devlink_dpipe_fields_ipv4),
61 .global = true,
62};
63EXPORT_SYMBOL(devlink_dpipe_header_ipv4);
64
Arkadi Sharshevsky1797f5b2017-08-31 17:59:12 +020065static struct devlink_dpipe_field devlink_dpipe_fields_ipv6[] = {
66 {
67 .name = "destination ip",
68 .id = DEVLINK_DPIPE_FIELD_IPV6_DST_IP,
69 .bitwidth = 128,
70 },
71};
72
73struct devlink_dpipe_header devlink_dpipe_header_ipv6 = {
74 .name = "ipv6",
75 .id = DEVLINK_DPIPE_HEADER_IPV6,
76 .fields = devlink_dpipe_fields_ipv6,
77 .fields_count = ARRAY_SIZE(devlink_dpipe_fields_ipv6),
78 .global = true,
79};
80EXPORT_SYMBOL(devlink_dpipe_header_ipv6);
81
Jiri Pirkoe5224f02016-07-12 18:05:03 +020082EXPORT_TRACEPOINT_SYMBOL_GPL(devlink_hwmsg);
Nir Dotan57186a52019-02-04 18:47:45 +000083EXPORT_TRACEPOINT_SYMBOL_GPL(devlink_hwerr);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +010084
85static LIST_HEAD(devlink_list);
86
87/* devlink_mutex
88 *
89 * An overall lock guarding every operation coming from userspace.
90 * It also guards devlink devices list and it is taken when
91 * driver registers/unregisters it.
92 */
93static DEFINE_MUTEX(devlink_mutex);
94
Jiri Pirkobfcd3a42016-02-26 17:32:23 +010095static struct net *devlink_net(const struct devlink *devlink)
96{
97 return read_pnet(&devlink->_net);
98}
99
100static void devlink_net_set(struct devlink *devlink, struct net *net)
101{
102 write_pnet(&devlink->_net, net);
103}
104
105static struct devlink *devlink_get_from_attrs(struct net *net,
106 struct nlattr **attrs)
107{
108 struct devlink *devlink;
109 char *busname;
110 char *devname;
111
112 if (!attrs[DEVLINK_ATTR_BUS_NAME] || !attrs[DEVLINK_ATTR_DEV_NAME])
113 return ERR_PTR(-EINVAL);
114
115 busname = nla_data(attrs[DEVLINK_ATTR_BUS_NAME]);
116 devname = nla_data(attrs[DEVLINK_ATTR_DEV_NAME]);
117
Parav Panditdac7c082019-02-12 14:24:08 -0600118 lockdep_assert_held(&devlink_mutex);
119
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100120 list_for_each_entry(devlink, &devlink_list, list) {
121 if (strcmp(devlink->dev->bus->name, busname) == 0 &&
122 strcmp(dev_name(devlink->dev), devname) == 0 &&
123 net_eq(devlink_net(devlink), net))
124 return devlink;
125 }
126
127 return ERR_PTR(-ENODEV);
128}
129
130static struct devlink *devlink_get_from_info(struct genl_info *info)
131{
132 return devlink_get_from_attrs(genl_info_net(info), info->attrs);
133}
134
135static struct devlink_port *devlink_port_get_by_index(struct devlink *devlink,
136 int port_index)
137{
138 struct devlink_port *devlink_port;
139
140 list_for_each_entry(devlink_port, &devlink->port_list, list) {
141 if (devlink_port->index == port_index)
142 return devlink_port;
143 }
144 return NULL;
145}
146
147static bool devlink_port_index_exists(struct devlink *devlink, int port_index)
148{
149 return devlink_port_get_by_index(devlink, port_index);
150}
151
152static struct devlink_port *devlink_port_get_from_attrs(struct devlink *devlink,
153 struct nlattr **attrs)
154{
155 if (attrs[DEVLINK_ATTR_PORT_INDEX]) {
156 u32 port_index = nla_get_u32(attrs[DEVLINK_ATTR_PORT_INDEX]);
157 struct devlink_port *devlink_port;
158
159 devlink_port = devlink_port_get_by_index(devlink, port_index);
160 if (!devlink_port)
161 return ERR_PTR(-ENODEV);
162 return devlink_port;
163 }
164 return ERR_PTR(-EINVAL);
165}
166
167static struct devlink_port *devlink_port_get_from_info(struct devlink *devlink,
168 struct genl_info *info)
169{
170 return devlink_port_get_from_attrs(devlink, info->attrs);
171}
172
Jiri Pirkobf797472016-04-14 18:19:13 +0200173struct devlink_sb {
174 struct list_head list;
175 unsigned int index;
176 u32 size;
177 u16 ingress_pools_count;
178 u16 egress_pools_count;
179 u16 ingress_tc_count;
180 u16 egress_tc_count;
181};
182
183static u16 devlink_sb_pool_count(struct devlink_sb *devlink_sb)
184{
185 return devlink_sb->ingress_pools_count + devlink_sb->egress_pools_count;
186}
187
188static struct devlink_sb *devlink_sb_get_by_index(struct devlink *devlink,
189 unsigned int sb_index)
190{
191 struct devlink_sb *devlink_sb;
192
193 list_for_each_entry(devlink_sb, &devlink->sb_list, list) {
194 if (devlink_sb->index == sb_index)
195 return devlink_sb;
196 }
197 return NULL;
198}
199
200static bool devlink_sb_index_exists(struct devlink *devlink,
201 unsigned int sb_index)
202{
203 return devlink_sb_get_by_index(devlink, sb_index);
204}
205
206static struct devlink_sb *devlink_sb_get_from_attrs(struct devlink *devlink,
207 struct nlattr **attrs)
208{
209 if (attrs[DEVLINK_ATTR_SB_INDEX]) {
210 u32 sb_index = nla_get_u32(attrs[DEVLINK_ATTR_SB_INDEX]);
211 struct devlink_sb *devlink_sb;
212
213 devlink_sb = devlink_sb_get_by_index(devlink, sb_index);
214 if (!devlink_sb)
215 return ERR_PTR(-ENODEV);
216 return devlink_sb;
217 }
218 return ERR_PTR(-EINVAL);
219}
220
221static struct devlink_sb *devlink_sb_get_from_info(struct devlink *devlink,
222 struct genl_info *info)
223{
224 return devlink_sb_get_from_attrs(devlink, info->attrs);
225}
226
227static int devlink_sb_pool_index_get_from_attrs(struct devlink_sb *devlink_sb,
228 struct nlattr **attrs,
229 u16 *p_pool_index)
230{
231 u16 val;
232
233 if (!attrs[DEVLINK_ATTR_SB_POOL_INDEX])
234 return -EINVAL;
235
236 val = nla_get_u16(attrs[DEVLINK_ATTR_SB_POOL_INDEX]);
237 if (val >= devlink_sb_pool_count(devlink_sb))
238 return -EINVAL;
239 *p_pool_index = val;
240 return 0;
241}
242
243static int devlink_sb_pool_index_get_from_info(struct devlink_sb *devlink_sb,
244 struct genl_info *info,
245 u16 *p_pool_index)
246{
247 return devlink_sb_pool_index_get_from_attrs(devlink_sb, info->attrs,
248 p_pool_index);
249}
250
251static int
252devlink_sb_pool_type_get_from_attrs(struct nlattr **attrs,
253 enum devlink_sb_pool_type *p_pool_type)
254{
255 u8 val;
256
257 if (!attrs[DEVLINK_ATTR_SB_POOL_TYPE])
258 return -EINVAL;
259
260 val = nla_get_u8(attrs[DEVLINK_ATTR_SB_POOL_TYPE]);
261 if (val != DEVLINK_SB_POOL_TYPE_INGRESS &&
262 val != DEVLINK_SB_POOL_TYPE_EGRESS)
263 return -EINVAL;
264 *p_pool_type = val;
265 return 0;
266}
267
268static int
269devlink_sb_pool_type_get_from_info(struct genl_info *info,
270 enum devlink_sb_pool_type *p_pool_type)
271{
272 return devlink_sb_pool_type_get_from_attrs(info->attrs, p_pool_type);
273}
274
275static int
276devlink_sb_th_type_get_from_attrs(struct nlattr **attrs,
277 enum devlink_sb_threshold_type *p_th_type)
278{
279 u8 val;
280
281 if (!attrs[DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE])
282 return -EINVAL;
283
284 val = nla_get_u8(attrs[DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE]);
285 if (val != DEVLINK_SB_THRESHOLD_TYPE_STATIC &&
286 val != DEVLINK_SB_THRESHOLD_TYPE_DYNAMIC)
287 return -EINVAL;
288 *p_th_type = val;
289 return 0;
290}
291
292static int
293devlink_sb_th_type_get_from_info(struct genl_info *info,
294 enum devlink_sb_threshold_type *p_th_type)
295{
296 return devlink_sb_th_type_get_from_attrs(info->attrs, p_th_type);
297}
298
299static int
300devlink_sb_tc_index_get_from_attrs(struct devlink_sb *devlink_sb,
301 struct nlattr **attrs,
302 enum devlink_sb_pool_type pool_type,
303 u16 *p_tc_index)
304{
305 u16 val;
306
307 if (!attrs[DEVLINK_ATTR_SB_TC_INDEX])
308 return -EINVAL;
309
310 val = nla_get_u16(attrs[DEVLINK_ATTR_SB_TC_INDEX]);
311 if (pool_type == DEVLINK_SB_POOL_TYPE_INGRESS &&
312 val >= devlink_sb->ingress_tc_count)
313 return -EINVAL;
314 if (pool_type == DEVLINK_SB_POOL_TYPE_EGRESS &&
315 val >= devlink_sb->egress_tc_count)
316 return -EINVAL;
317 *p_tc_index = val;
318 return 0;
319}
320
321static int
322devlink_sb_tc_index_get_from_info(struct devlink_sb *devlink_sb,
323 struct genl_info *info,
324 enum devlink_sb_pool_type pool_type,
325 u16 *p_tc_index)
326{
327 return devlink_sb_tc_index_get_from_attrs(devlink_sb, info->attrs,
328 pool_type, p_tc_index);
329}
330
Alex Veskerb16ebe92018-07-12 15:13:08 +0300331struct devlink_region {
332 struct devlink *devlink;
333 struct list_head list;
334 const char *name;
335 struct list_head snapshot_list;
336 u32 max_snapshots;
337 u32 cur_snapshots;
338 u64 size;
339};
340
Alex Veskerd7e52722018-07-12 15:13:10 +0300341struct devlink_snapshot {
342 struct list_head list;
343 struct devlink_region *region;
344 devlink_snapshot_data_dest_t *data_destructor;
345 u64 data_len;
346 u8 *data;
347 u32 id;
348};
349
Alex Veskerb16ebe92018-07-12 15:13:08 +0300350static struct devlink_region *
351devlink_region_get_by_name(struct devlink *devlink, const char *region_name)
352{
353 struct devlink_region *region;
354
355 list_for_each_entry(region, &devlink->region_list, list)
356 if (!strcmp(region->name, region_name))
357 return region;
358
359 return NULL;
360}
361
Alex Veskerd7e52722018-07-12 15:13:10 +0300362static struct devlink_snapshot *
363devlink_region_snapshot_get_by_id(struct devlink_region *region, u32 id)
364{
365 struct devlink_snapshot *snapshot;
366
367 list_for_each_entry(snapshot, &region->snapshot_list, list)
368 if (snapshot->id == id)
369 return snapshot;
370
371 return NULL;
372}
373
374static void devlink_region_snapshot_del(struct devlink_snapshot *snapshot)
375{
376 snapshot->region->cur_snapshots--;
377 list_del(&snapshot->list);
378 (*snapshot->data_destructor)(snapshot->data);
379 kfree(snapshot);
380}
381
Jiri Pirko1fc22572016-04-08 19:12:48 +0200382#define DEVLINK_NL_FLAG_NEED_DEVLINK BIT(0)
383#define DEVLINK_NL_FLAG_NEED_PORT BIT(1)
Jiri Pirkobf797472016-04-14 18:19:13 +0200384#define DEVLINK_NL_FLAG_NEED_SB BIT(2)
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +0100385
386/* The per devlink instance lock is taken by default in the pre-doit
387 * operation, yet several commands do not require this. The global
388 * devlink lock is taken and protects from disruption by user-calls.
389 */
390#define DEVLINK_NL_FLAG_NO_LOCK BIT(3)
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100391
392static int devlink_nl_pre_doit(const struct genl_ops *ops,
393 struct sk_buff *skb, struct genl_info *info)
394{
395 struct devlink *devlink;
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +0100396 int err;
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100397
398 mutex_lock(&devlink_mutex);
399 devlink = devlink_get_from_info(info);
400 if (IS_ERR(devlink)) {
401 mutex_unlock(&devlink_mutex);
402 return PTR_ERR(devlink);
403 }
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +0100404 if (~ops->internal_flags & DEVLINK_NL_FLAG_NO_LOCK)
405 mutex_lock(&devlink->lock);
Jiri Pirko1fc22572016-04-08 19:12:48 +0200406 if (ops->internal_flags & DEVLINK_NL_FLAG_NEED_DEVLINK) {
407 info->user_ptr[0] = devlink;
408 } else if (ops->internal_flags & DEVLINK_NL_FLAG_NEED_PORT) {
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100409 struct devlink_port *devlink_port;
410
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100411 devlink_port = devlink_port_get_from_info(devlink, info);
412 if (IS_ERR(devlink_port)) {
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +0100413 err = PTR_ERR(devlink_port);
414 goto unlock;
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100415 }
Jiri Pirko1fc22572016-04-08 19:12:48 +0200416 info->user_ptr[0] = devlink_port;
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100417 }
Jiri Pirkobf797472016-04-14 18:19:13 +0200418 if (ops->internal_flags & DEVLINK_NL_FLAG_NEED_SB) {
419 struct devlink_sb *devlink_sb;
420
421 devlink_sb = devlink_sb_get_from_info(devlink, info);
422 if (IS_ERR(devlink_sb)) {
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +0100423 err = PTR_ERR(devlink_sb);
424 goto unlock;
Jiri Pirkobf797472016-04-14 18:19:13 +0200425 }
426 info->user_ptr[1] = devlink_sb;
427 }
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100428 return 0;
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +0100429
430unlock:
431 if (~ops->internal_flags & DEVLINK_NL_FLAG_NO_LOCK)
432 mutex_unlock(&devlink->lock);
433 mutex_unlock(&devlink_mutex);
434 return err;
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100435}
436
437static void devlink_nl_post_doit(const struct genl_ops *ops,
438 struct sk_buff *skb, struct genl_info *info)
439{
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +0100440 struct devlink *devlink;
441
442 devlink = devlink_get_from_info(info);
443 if (~ops->internal_flags & DEVLINK_NL_FLAG_NO_LOCK)
444 mutex_unlock(&devlink->lock);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100445 mutex_unlock(&devlink_mutex);
446}
447
Johannes Berg489111e2016-10-24 14:40:03 +0200448static struct genl_family devlink_nl_family;
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100449
450enum devlink_multicast_groups {
451 DEVLINK_MCGRP_CONFIG,
452};
453
454static const struct genl_multicast_group devlink_nl_mcgrps[] = {
455 [DEVLINK_MCGRP_CONFIG] = { .name = DEVLINK_GENL_MCGRP_CONFIG_NAME },
456};
457
458static int devlink_nl_put_handle(struct sk_buff *msg, struct devlink *devlink)
459{
460 if (nla_put_string(msg, DEVLINK_ATTR_BUS_NAME, devlink->dev->bus->name))
461 return -EMSGSIZE;
462 if (nla_put_string(msg, DEVLINK_ATTR_DEV_NAME, dev_name(devlink->dev)))
463 return -EMSGSIZE;
464 return 0;
465}
466
467static int devlink_nl_fill(struct sk_buff *msg, struct devlink *devlink,
468 enum devlink_command cmd, u32 portid,
469 u32 seq, int flags)
470{
471 void *hdr;
472
473 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
474 if (!hdr)
475 return -EMSGSIZE;
476
477 if (devlink_nl_put_handle(msg, devlink))
478 goto nla_put_failure;
479
480 genlmsg_end(msg, hdr);
481 return 0;
482
483nla_put_failure:
484 genlmsg_cancel(msg, hdr);
485 return -EMSGSIZE;
486}
487
488static void devlink_notify(struct devlink *devlink, enum devlink_command cmd)
489{
490 struct sk_buff *msg;
491 int err;
492
493 WARN_ON(cmd != DEVLINK_CMD_NEW && cmd != DEVLINK_CMD_DEL);
494
495 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
496 if (!msg)
497 return;
498
499 err = devlink_nl_fill(msg, devlink, cmd, 0, 0, 0);
500 if (err) {
501 nlmsg_free(msg);
502 return;
503 }
504
505 genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
506 msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
507}
508
Jiri Pirkob9ffcba2018-05-18 09:29:00 +0200509static int devlink_nl_port_attrs_put(struct sk_buff *msg,
510 struct devlink_port *devlink_port)
511{
512 struct devlink_port_attrs *attrs = &devlink_port->attrs;
513
514 if (!attrs->set)
515 return 0;
Jiri Pirko5ec13802018-05-18 09:29:01 +0200516 if (nla_put_u16(msg, DEVLINK_ATTR_PORT_FLAVOUR, attrs->flavour))
517 return -EMSGSIZE;
Parav Pandita2c6b872019-07-08 23:17:36 -0500518 if (devlink_port->attrs.flavour != DEVLINK_PORT_FLAVOUR_PHYSICAL &&
519 devlink_port->attrs.flavour != DEVLINK_PORT_FLAVOUR_CPU &&
520 devlink_port->attrs.flavour != DEVLINK_PORT_FLAVOUR_DSA)
521 return 0;
Parav Pandit378ef012019-07-08 23:17:35 -0500522 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_NUMBER,
523 attrs->phys.port_number))
Jiri Pirkob9ffcba2018-05-18 09:29:00 +0200524 return -EMSGSIZE;
525 if (!attrs->split)
526 return 0;
Parav Pandit378ef012019-07-08 23:17:35 -0500527 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_SPLIT_GROUP,
528 attrs->phys.port_number))
Jiri Pirkob9ffcba2018-05-18 09:29:00 +0200529 return -EMSGSIZE;
530 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_SPLIT_SUBPORT_NUMBER,
Parav Pandit378ef012019-07-08 23:17:35 -0500531 attrs->phys.split_subport_number))
Jiri Pirkob9ffcba2018-05-18 09:29:00 +0200532 return -EMSGSIZE;
533 return 0;
534}
535
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100536static int devlink_nl_port_fill(struct sk_buff *msg, struct devlink *devlink,
537 struct devlink_port *devlink_port,
538 enum devlink_command cmd, u32 portid,
539 u32 seq, int flags)
540{
541 void *hdr;
542
543 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
544 if (!hdr)
545 return -EMSGSIZE;
546
547 if (devlink_nl_put_handle(msg, devlink))
548 goto nla_put_failure;
549 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, devlink_port->index))
550 goto nla_put_failure;
Jiri Pirkob8f97552019-03-24 11:14:37 +0100551
552 spin_lock(&devlink_port->type_lock);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100553 if (nla_put_u16(msg, DEVLINK_ATTR_PORT_TYPE, devlink_port->type))
Jiri Pirkob8f97552019-03-24 11:14:37 +0100554 goto nla_put_failure_type_locked;
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100555 if (devlink_port->desired_type != DEVLINK_PORT_TYPE_NOTSET &&
556 nla_put_u16(msg, DEVLINK_ATTR_PORT_DESIRED_TYPE,
557 devlink_port->desired_type))
Jiri Pirkob8f97552019-03-24 11:14:37 +0100558 goto nla_put_failure_type_locked;
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100559 if (devlink_port->type == DEVLINK_PORT_TYPE_ETH) {
560 struct net_device *netdev = devlink_port->type_dev;
561
562 if (netdev &&
563 (nla_put_u32(msg, DEVLINK_ATTR_PORT_NETDEV_IFINDEX,
564 netdev->ifindex) ||
565 nla_put_string(msg, DEVLINK_ATTR_PORT_NETDEV_NAME,
566 netdev->name)))
Jiri Pirkob8f97552019-03-24 11:14:37 +0100567 goto nla_put_failure_type_locked;
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100568 }
569 if (devlink_port->type == DEVLINK_PORT_TYPE_IB) {
570 struct ib_device *ibdev = devlink_port->type_dev;
571
572 if (ibdev &&
573 nla_put_string(msg, DEVLINK_ATTR_PORT_IBDEV_NAME,
574 ibdev->name))
Jiri Pirkob8f97552019-03-24 11:14:37 +0100575 goto nla_put_failure_type_locked;
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100576 }
Jiri Pirkob8f97552019-03-24 11:14:37 +0100577 spin_unlock(&devlink_port->type_lock);
Jiri Pirkob9ffcba2018-05-18 09:29:00 +0200578 if (devlink_nl_port_attrs_put(msg, devlink_port))
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100579 goto nla_put_failure;
580
581 genlmsg_end(msg, hdr);
582 return 0;
583
Jiri Pirkob8f97552019-03-24 11:14:37 +0100584nla_put_failure_type_locked:
585 spin_unlock(&devlink_port->type_lock);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100586nla_put_failure:
587 genlmsg_cancel(msg, hdr);
588 return -EMSGSIZE;
589}
590
591static void devlink_port_notify(struct devlink_port *devlink_port,
592 enum devlink_command cmd)
593{
594 struct devlink *devlink = devlink_port->devlink;
595 struct sk_buff *msg;
596 int err;
597
598 if (!devlink_port->registered)
599 return;
600
601 WARN_ON(cmd != DEVLINK_CMD_PORT_NEW && cmd != DEVLINK_CMD_PORT_DEL);
602
603 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
604 if (!msg)
605 return;
606
607 err = devlink_nl_port_fill(msg, devlink, devlink_port, cmd, 0, 0, 0);
608 if (err) {
609 nlmsg_free(msg);
610 return;
611 }
612
613 genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
614 msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
615}
616
617static int devlink_nl_cmd_get_doit(struct sk_buff *skb, struct genl_info *info)
618{
619 struct devlink *devlink = info->user_ptr[0];
620 struct sk_buff *msg;
621 int err;
622
623 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
624 if (!msg)
625 return -ENOMEM;
626
627 err = devlink_nl_fill(msg, devlink, DEVLINK_CMD_NEW,
628 info->snd_portid, info->snd_seq, 0);
629 if (err) {
630 nlmsg_free(msg);
631 return err;
632 }
633
634 return genlmsg_reply(msg, info);
635}
636
637static int devlink_nl_cmd_get_dumpit(struct sk_buff *msg,
638 struct netlink_callback *cb)
639{
640 struct devlink *devlink;
641 int start = cb->args[0];
642 int idx = 0;
643 int err;
644
645 mutex_lock(&devlink_mutex);
646 list_for_each_entry(devlink, &devlink_list, list) {
647 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
648 continue;
649 if (idx < start) {
650 idx++;
651 continue;
652 }
653 err = devlink_nl_fill(msg, devlink, DEVLINK_CMD_NEW,
654 NETLINK_CB(cb->skb).portid,
655 cb->nlh->nlmsg_seq, NLM_F_MULTI);
656 if (err)
657 goto out;
658 idx++;
659 }
660out:
661 mutex_unlock(&devlink_mutex);
662
663 cb->args[0] = idx;
664 return msg->len;
665}
666
667static int devlink_nl_cmd_port_get_doit(struct sk_buff *skb,
668 struct genl_info *info)
669{
Jiri Pirko1fc22572016-04-08 19:12:48 +0200670 struct devlink_port *devlink_port = info->user_ptr[0];
671 struct devlink *devlink = devlink_port->devlink;
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100672 struct sk_buff *msg;
673 int err;
674
675 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
676 if (!msg)
677 return -ENOMEM;
678
679 err = devlink_nl_port_fill(msg, devlink, devlink_port,
680 DEVLINK_CMD_PORT_NEW,
681 info->snd_portid, info->snd_seq, 0);
682 if (err) {
683 nlmsg_free(msg);
684 return err;
685 }
686
687 return genlmsg_reply(msg, info);
688}
689
690static int devlink_nl_cmd_port_get_dumpit(struct sk_buff *msg,
691 struct netlink_callback *cb)
692{
693 struct devlink *devlink;
694 struct devlink_port *devlink_port;
695 int start = cb->args[0];
696 int idx = 0;
697 int err;
698
699 mutex_lock(&devlink_mutex);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100700 list_for_each_entry(devlink, &devlink_list, list) {
701 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
702 continue;
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +0100703 mutex_lock(&devlink->lock);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100704 list_for_each_entry(devlink_port, &devlink->port_list, list) {
705 if (idx < start) {
706 idx++;
707 continue;
708 }
709 err = devlink_nl_port_fill(msg, devlink, devlink_port,
710 DEVLINK_CMD_NEW,
711 NETLINK_CB(cb->skb).portid,
712 cb->nlh->nlmsg_seq,
713 NLM_F_MULTI);
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +0100714 if (err) {
715 mutex_unlock(&devlink->lock);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100716 goto out;
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +0100717 }
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100718 idx++;
719 }
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +0100720 mutex_unlock(&devlink->lock);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100721 }
722out:
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100723 mutex_unlock(&devlink_mutex);
724
725 cb->args[0] = idx;
726 return msg->len;
727}
728
729static int devlink_port_type_set(struct devlink *devlink,
730 struct devlink_port *devlink_port,
731 enum devlink_port_type port_type)
732
733{
734 int err;
735
Jakub Kicinskibe6fe1d2019-02-25 19:34:07 -0800736 if (devlink->ops->port_type_set) {
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100737 if (port_type == DEVLINK_PORT_TYPE_NOTSET)
738 return -EINVAL;
Elad Raz6edf1012016-10-23 17:43:05 +0200739 if (port_type == devlink_port->type)
740 return 0;
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100741 err = devlink->ops->port_type_set(devlink_port, port_type);
742 if (err)
743 return err;
744 devlink_port->desired_type = port_type;
745 devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW);
746 return 0;
747 }
748 return -EOPNOTSUPP;
749}
750
751static int devlink_nl_cmd_port_set_doit(struct sk_buff *skb,
752 struct genl_info *info)
753{
Jiri Pirko1fc22572016-04-08 19:12:48 +0200754 struct devlink_port *devlink_port = info->user_ptr[0];
755 struct devlink *devlink = devlink_port->devlink;
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100756 int err;
757
758 if (info->attrs[DEVLINK_ATTR_PORT_TYPE]) {
759 enum devlink_port_type port_type;
760
761 port_type = nla_get_u16(info->attrs[DEVLINK_ATTR_PORT_TYPE]);
762 err = devlink_port_type_set(devlink, devlink_port, port_type);
763 if (err)
764 return err;
765 }
766 return 0;
767}
768
David Ahernac0fc8a2018-06-05 08:14:09 -0700769static int devlink_port_split(struct devlink *devlink, u32 port_index,
770 u32 count, struct netlink_ext_ack *extack)
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100771
772{
Jakub Kicinskibe6fe1d2019-02-25 19:34:07 -0800773 if (devlink->ops->port_split)
David Ahernac0fc8a2018-06-05 08:14:09 -0700774 return devlink->ops->port_split(devlink, port_index, count,
775 extack);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100776 return -EOPNOTSUPP;
777}
778
779static int devlink_nl_cmd_port_split_doit(struct sk_buff *skb,
780 struct genl_info *info)
781{
782 struct devlink *devlink = info->user_ptr[0];
783 u32 port_index;
784 u32 count;
785
786 if (!info->attrs[DEVLINK_ATTR_PORT_INDEX] ||
787 !info->attrs[DEVLINK_ATTR_PORT_SPLIT_COUNT])
788 return -EINVAL;
789
790 port_index = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_INDEX]);
791 count = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_SPLIT_COUNT]);
David Ahernac0fc8a2018-06-05 08:14:09 -0700792 return devlink_port_split(devlink, port_index, count, info->extack);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100793}
794
David Ahernac0fc8a2018-06-05 08:14:09 -0700795static int devlink_port_unsplit(struct devlink *devlink, u32 port_index,
796 struct netlink_ext_ack *extack)
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100797
798{
Jakub Kicinskibe6fe1d2019-02-25 19:34:07 -0800799 if (devlink->ops->port_unsplit)
David Ahernac0fc8a2018-06-05 08:14:09 -0700800 return devlink->ops->port_unsplit(devlink, port_index, extack);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100801 return -EOPNOTSUPP;
802}
803
804static int devlink_nl_cmd_port_unsplit_doit(struct sk_buff *skb,
805 struct genl_info *info)
806{
807 struct devlink *devlink = info->user_ptr[0];
808 u32 port_index;
809
810 if (!info->attrs[DEVLINK_ATTR_PORT_INDEX])
811 return -EINVAL;
812
813 port_index = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_INDEX]);
David Ahernac0fc8a2018-06-05 08:14:09 -0700814 return devlink_port_unsplit(devlink, port_index, info->extack);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +0100815}
816
Jiri Pirkobf797472016-04-14 18:19:13 +0200817static int devlink_nl_sb_fill(struct sk_buff *msg, struct devlink *devlink,
818 struct devlink_sb *devlink_sb,
819 enum devlink_command cmd, u32 portid,
820 u32 seq, int flags)
821{
822 void *hdr;
823
824 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
825 if (!hdr)
826 return -EMSGSIZE;
827
828 if (devlink_nl_put_handle(msg, devlink))
829 goto nla_put_failure;
830 if (nla_put_u32(msg, DEVLINK_ATTR_SB_INDEX, devlink_sb->index))
831 goto nla_put_failure;
832 if (nla_put_u32(msg, DEVLINK_ATTR_SB_SIZE, devlink_sb->size))
833 goto nla_put_failure;
834 if (nla_put_u16(msg, DEVLINK_ATTR_SB_INGRESS_POOL_COUNT,
835 devlink_sb->ingress_pools_count))
836 goto nla_put_failure;
837 if (nla_put_u16(msg, DEVLINK_ATTR_SB_EGRESS_POOL_COUNT,
838 devlink_sb->egress_pools_count))
839 goto nla_put_failure;
840 if (nla_put_u16(msg, DEVLINK_ATTR_SB_INGRESS_TC_COUNT,
841 devlink_sb->ingress_tc_count))
842 goto nla_put_failure;
843 if (nla_put_u16(msg, DEVLINK_ATTR_SB_EGRESS_TC_COUNT,
844 devlink_sb->egress_tc_count))
845 goto nla_put_failure;
846
847 genlmsg_end(msg, hdr);
848 return 0;
849
850nla_put_failure:
851 genlmsg_cancel(msg, hdr);
852 return -EMSGSIZE;
853}
854
855static int devlink_nl_cmd_sb_get_doit(struct sk_buff *skb,
856 struct genl_info *info)
857{
858 struct devlink *devlink = info->user_ptr[0];
859 struct devlink_sb *devlink_sb = info->user_ptr[1];
860 struct sk_buff *msg;
861 int err;
862
863 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
864 if (!msg)
865 return -ENOMEM;
866
867 err = devlink_nl_sb_fill(msg, devlink, devlink_sb,
868 DEVLINK_CMD_SB_NEW,
869 info->snd_portid, info->snd_seq, 0);
870 if (err) {
871 nlmsg_free(msg);
872 return err;
873 }
874
875 return genlmsg_reply(msg, info);
876}
877
878static int devlink_nl_cmd_sb_get_dumpit(struct sk_buff *msg,
879 struct netlink_callback *cb)
880{
881 struct devlink *devlink;
882 struct devlink_sb *devlink_sb;
883 int start = cb->args[0];
884 int idx = 0;
885 int err;
886
887 mutex_lock(&devlink_mutex);
888 list_for_each_entry(devlink, &devlink_list, list) {
889 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
890 continue;
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +0100891 mutex_lock(&devlink->lock);
Jiri Pirkobf797472016-04-14 18:19:13 +0200892 list_for_each_entry(devlink_sb, &devlink->sb_list, list) {
893 if (idx < start) {
894 idx++;
895 continue;
896 }
897 err = devlink_nl_sb_fill(msg, devlink, devlink_sb,
898 DEVLINK_CMD_SB_NEW,
899 NETLINK_CB(cb->skb).portid,
900 cb->nlh->nlmsg_seq,
901 NLM_F_MULTI);
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +0100902 if (err) {
903 mutex_unlock(&devlink->lock);
Jiri Pirkobf797472016-04-14 18:19:13 +0200904 goto out;
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +0100905 }
Jiri Pirkobf797472016-04-14 18:19:13 +0200906 idx++;
907 }
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +0100908 mutex_unlock(&devlink->lock);
Jiri Pirkobf797472016-04-14 18:19:13 +0200909 }
910out:
911 mutex_unlock(&devlink_mutex);
912
913 cb->args[0] = idx;
914 return msg->len;
915}
916
917static int devlink_nl_sb_pool_fill(struct sk_buff *msg, struct devlink *devlink,
918 struct devlink_sb *devlink_sb,
919 u16 pool_index, enum devlink_command cmd,
920 u32 portid, u32 seq, int flags)
921{
922 struct devlink_sb_pool_info pool_info;
923 void *hdr;
924 int err;
925
926 err = devlink->ops->sb_pool_get(devlink, devlink_sb->index,
927 pool_index, &pool_info);
928 if (err)
929 return err;
930
931 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
932 if (!hdr)
933 return -EMSGSIZE;
934
935 if (devlink_nl_put_handle(msg, devlink))
936 goto nla_put_failure;
937 if (nla_put_u32(msg, DEVLINK_ATTR_SB_INDEX, devlink_sb->index))
938 goto nla_put_failure;
939 if (nla_put_u16(msg, DEVLINK_ATTR_SB_POOL_INDEX, pool_index))
940 goto nla_put_failure;
941 if (nla_put_u8(msg, DEVLINK_ATTR_SB_POOL_TYPE, pool_info.pool_type))
942 goto nla_put_failure;
943 if (nla_put_u32(msg, DEVLINK_ATTR_SB_POOL_SIZE, pool_info.size))
944 goto nla_put_failure;
945 if (nla_put_u8(msg, DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE,
946 pool_info.threshold_type))
947 goto nla_put_failure;
Jakub Kicinskibff57312019-02-01 17:56:28 -0800948 if (nla_put_u32(msg, DEVLINK_ATTR_SB_POOL_CELL_SIZE,
949 pool_info.cell_size))
950 goto nla_put_failure;
Jiri Pirkobf797472016-04-14 18:19:13 +0200951
952 genlmsg_end(msg, hdr);
953 return 0;
954
955nla_put_failure:
956 genlmsg_cancel(msg, hdr);
957 return -EMSGSIZE;
958}
959
960static int devlink_nl_cmd_sb_pool_get_doit(struct sk_buff *skb,
961 struct genl_info *info)
962{
963 struct devlink *devlink = info->user_ptr[0];
964 struct devlink_sb *devlink_sb = info->user_ptr[1];
965 struct sk_buff *msg;
966 u16 pool_index;
967 int err;
968
969 err = devlink_sb_pool_index_get_from_info(devlink_sb, info,
970 &pool_index);
971 if (err)
972 return err;
973
Jakub Kicinskibe6fe1d2019-02-25 19:34:07 -0800974 if (!devlink->ops->sb_pool_get)
Jiri Pirkobf797472016-04-14 18:19:13 +0200975 return -EOPNOTSUPP;
976
977 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
978 if (!msg)
979 return -ENOMEM;
980
981 err = devlink_nl_sb_pool_fill(msg, devlink, devlink_sb, pool_index,
982 DEVLINK_CMD_SB_POOL_NEW,
983 info->snd_portid, info->snd_seq, 0);
984 if (err) {
985 nlmsg_free(msg);
986 return err;
987 }
988
989 return genlmsg_reply(msg, info);
990}
991
992static int __sb_pool_get_dumpit(struct sk_buff *msg, int start, int *p_idx,
993 struct devlink *devlink,
994 struct devlink_sb *devlink_sb,
995 u32 portid, u32 seq)
996{
997 u16 pool_count = devlink_sb_pool_count(devlink_sb);
998 u16 pool_index;
999 int err;
1000
1001 for (pool_index = 0; pool_index < pool_count; pool_index++) {
1002 if (*p_idx < start) {
1003 (*p_idx)++;
1004 continue;
1005 }
1006 err = devlink_nl_sb_pool_fill(msg, devlink,
1007 devlink_sb,
1008 pool_index,
1009 DEVLINK_CMD_SB_POOL_NEW,
1010 portid, seq, NLM_F_MULTI);
1011 if (err)
1012 return err;
1013 (*p_idx)++;
1014 }
1015 return 0;
1016}
1017
1018static int devlink_nl_cmd_sb_pool_get_dumpit(struct sk_buff *msg,
1019 struct netlink_callback *cb)
1020{
1021 struct devlink *devlink;
1022 struct devlink_sb *devlink_sb;
1023 int start = cb->args[0];
1024 int idx = 0;
1025 int err;
1026
1027 mutex_lock(&devlink_mutex);
1028 list_for_each_entry(devlink, &devlink_list, list) {
1029 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)) ||
Jakub Kicinskibe6fe1d2019-02-25 19:34:07 -08001030 !devlink->ops->sb_pool_get)
Jiri Pirkobf797472016-04-14 18:19:13 +02001031 continue;
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01001032 mutex_lock(&devlink->lock);
Jiri Pirkobf797472016-04-14 18:19:13 +02001033 list_for_each_entry(devlink_sb, &devlink->sb_list, list) {
1034 err = __sb_pool_get_dumpit(msg, start, &idx, devlink,
1035 devlink_sb,
1036 NETLINK_CB(cb->skb).portid,
1037 cb->nlh->nlmsg_seq);
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01001038 if (err && err != -EOPNOTSUPP) {
1039 mutex_unlock(&devlink->lock);
Jiri Pirkobf797472016-04-14 18:19:13 +02001040 goto out;
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01001041 }
Jiri Pirkobf797472016-04-14 18:19:13 +02001042 }
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01001043 mutex_unlock(&devlink->lock);
Jiri Pirkobf797472016-04-14 18:19:13 +02001044 }
1045out:
1046 mutex_unlock(&devlink_mutex);
1047
1048 cb->args[0] = idx;
1049 return msg->len;
1050}
1051
1052static int devlink_sb_pool_set(struct devlink *devlink, unsigned int sb_index,
1053 u16 pool_index, u32 size,
Ido Schimmelf2ad1a52019-04-22 12:08:39 +00001054 enum devlink_sb_threshold_type threshold_type,
1055 struct netlink_ext_ack *extack)
Jiri Pirkobf797472016-04-14 18:19:13 +02001056
1057{
1058 const struct devlink_ops *ops = devlink->ops;
1059
Jakub Kicinskibe6fe1d2019-02-25 19:34:07 -08001060 if (ops->sb_pool_set)
Jiri Pirkobf797472016-04-14 18:19:13 +02001061 return ops->sb_pool_set(devlink, sb_index, pool_index,
Ido Schimmelf2ad1a52019-04-22 12:08:39 +00001062 size, threshold_type, extack);
Jiri Pirkobf797472016-04-14 18:19:13 +02001063 return -EOPNOTSUPP;
1064}
1065
1066static int devlink_nl_cmd_sb_pool_set_doit(struct sk_buff *skb,
1067 struct genl_info *info)
1068{
1069 struct devlink *devlink = info->user_ptr[0];
1070 struct devlink_sb *devlink_sb = info->user_ptr[1];
1071 enum devlink_sb_threshold_type threshold_type;
1072 u16 pool_index;
1073 u32 size;
1074 int err;
1075
1076 err = devlink_sb_pool_index_get_from_info(devlink_sb, info,
1077 &pool_index);
1078 if (err)
1079 return err;
1080
1081 err = devlink_sb_th_type_get_from_info(info, &threshold_type);
1082 if (err)
1083 return err;
1084
1085 if (!info->attrs[DEVLINK_ATTR_SB_POOL_SIZE])
1086 return -EINVAL;
1087
1088 size = nla_get_u32(info->attrs[DEVLINK_ATTR_SB_POOL_SIZE]);
1089 return devlink_sb_pool_set(devlink, devlink_sb->index,
Ido Schimmelf2ad1a52019-04-22 12:08:39 +00001090 pool_index, size, threshold_type,
1091 info->extack);
Jiri Pirkobf797472016-04-14 18:19:13 +02001092}
1093
1094static int devlink_nl_sb_port_pool_fill(struct sk_buff *msg,
1095 struct devlink *devlink,
1096 struct devlink_port *devlink_port,
1097 struct devlink_sb *devlink_sb,
1098 u16 pool_index,
1099 enum devlink_command cmd,
1100 u32 portid, u32 seq, int flags)
1101{
Jiri Pirkodf38daf2016-04-14 18:19:14 +02001102 const struct devlink_ops *ops = devlink->ops;
Jiri Pirkobf797472016-04-14 18:19:13 +02001103 u32 threshold;
1104 void *hdr;
1105 int err;
1106
Jiri Pirkodf38daf2016-04-14 18:19:14 +02001107 err = ops->sb_port_pool_get(devlink_port, devlink_sb->index,
1108 pool_index, &threshold);
Jiri Pirkobf797472016-04-14 18:19:13 +02001109 if (err)
1110 return err;
1111
1112 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
1113 if (!hdr)
1114 return -EMSGSIZE;
1115
1116 if (devlink_nl_put_handle(msg, devlink))
1117 goto nla_put_failure;
1118 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, devlink_port->index))
1119 goto nla_put_failure;
1120 if (nla_put_u32(msg, DEVLINK_ATTR_SB_INDEX, devlink_sb->index))
1121 goto nla_put_failure;
1122 if (nla_put_u16(msg, DEVLINK_ATTR_SB_POOL_INDEX, pool_index))
1123 goto nla_put_failure;
1124 if (nla_put_u32(msg, DEVLINK_ATTR_SB_THRESHOLD, threshold))
1125 goto nla_put_failure;
1126
Jiri Pirkodf38daf2016-04-14 18:19:14 +02001127 if (ops->sb_occ_port_pool_get) {
1128 u32 cur;
1129 u32 max;
1130
1131 err = ops->sb_occ_port_pool_get(devlink_port, devlink_sb->index,
1132 pool_index, &cur, &max);
1133 if (err && err != -EOPNOTSUPP)
1134 return err;
1135 if (!err) {
1136 if (nla_put_u32(msg, DEVLINK_ATTR_SB_OCC_CUR, cur))
1137 goto nla_put_failure;
1138 if (nla_put_u32(msg, DEVLINK_ATTR_SB_OCC_MAX, max))
1139 goto nla_put_failure;
1140 }
1141 }
1142
Jiri Pirkobf797472016-04-14 18:19:13 +02001143 genlmsg_end(msg, hdr);
1144 return 0;
1145
1146nla_put_failure:
1147 genlmsg_cancel(msg, hdr);
1148 return -EMSGSIZE;
1149}
1150
1151static int devlink_nl_cmd_sb_port_pool_get_doit(struct sk_buff *skb,
1152 struct genl_info *info)
1153{
1154 struct devlink_port *devlink_port = info->user_ptr[0];
1155 struct devlink *devlink = devlink_port->devlink;
1156 struct devlink_sb *devlink_sb = info->user_ptr[1];
1157 struct sk_buff *msg;
1158 u16 pool_index;
1159 int err;
1160
1161 err = devlink_sb_pool_index_get_from_info(devlink_sb, info,
1162 &pool_index);
1163 if (err)
1164 return err;
1165
Jakub Kicinskibe6fe1d2019-02-25 19:34:07 -08001166 if (!devlink->ops->sb_port_pool_get)
Jiri Pirkobf797472016-04-14 18:19:13 +02001167 return -EOPNOTSUPP;
1168
1169 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1170 if (!msg)
1171 return -ENOMEM;
1172
1173 err = devlink_nl_sb_port_pool_fill(msg, devlink, devlink_port,
1174 devlink_sb, pool_index,
1175 DEVLINK_CMD_SB_PORT_POOL_NEW,
1176 info->snd_portid, info->snd_seq, 0);
1177 if (err) {
1178 nlmsg_free(msg);
1179 return err;
1180 }
1181
1182 return genlmsg_reply(msg, info);
1183}
1184
1185static int __sb_port_pool_get_dumpit(struct sk_buff *msg, int start, int *p_idx,
1186 struct devlink *devlink,
1187 struct devlink_sb *devlink_sb,
1188 u32 portid, u32 seq)
1189{
1190 struct devlink_port *devlink_port;
1191 u16 pool_count = devlink_sb_pool_count(devlink_sb);
1192 u16 pool_index;
1193 int err;
1194
1195 list_for_each_entry(devlink_port, &devlink->port_list, list) {
1196 for (pool_index = 0; pool_index < pool_count; pool_index++) {
1197 if (*p_idx < start) {
1198 (*p_idx)++;
1199 continue;
1200 }
1201 err = devlink_nl_sb_port_pool_fill(msg, devlink,
1202 devlink_port,
1203 devlink_sb,
1204 pool_index,
1205 DEVLINK_CMD_SB_PORT_POOL_NEW,
1206 portid, seq,
1207 NLM_F_MULTI);
1208 if (err)
1209 return err;
1210 (*p_idx)++;
1211 }
1212 }
1213 return 0;
1214}
1215
1216static int devlink_nl_cmd_sb_port_pool_get_dumpit(struct sk_buff *msg,
1217 struct netlink_callback *cb)
1218{
1219 struct devlink *devlink;
1220 struct devlink_sb *devlink_sb;
1221 int start = cb->args[0];
1222 int idx = 0;
1223 int err;
1224
1225 mutex_lock(&devlink_mutex);
Jiri Pirkobf797472016-04-14 18:19:13 +02001226 list_for_each_entry(devlink, &devlink_list, list) {
1227 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)) ||
Jakub Kicinskibe6fe1d2019-02-25 19:34:07 -08001228 !devlink->ops->sb_port_pool_get)
Jiri Pirkobf797472016-04-14 18:19:13 +02001229 continue;
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01001230 mutex_lock(&devlink->lock);
Jiri Pirkobf797472016-04-14 18:19:13 +02001231 list_for_each_entry(devlink_sb, &devlink->sb_list, list) {
1232 err = __sb_port_pool_get_dumpit(msg, start, &idx,
1233 devlink, devlink_sb,
1234 NETLINK_CB(cb->skb).portid,
1235 cb->nlh->nlmsg_seq);
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01001236 if (err && err != -EOPNOTSUPP) {
1237 mutex_unlock(&devlink->lock);
Jiri Pirkobf797472016-04-14 18:19:13 +02001238 goto out;
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01001239 }
Jiri Pirkobf797472016-04-14 18:19:13 +02001240 }
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01001241 mutex_unlock(&devlink->lock);
Jiri Pirkobf797472016-04-14 18:19:13 +02001242 }
1243out:
Jiri Pirkobf797472016-04-14 18:19:13 +02001244 mutex_unlock(&devlink_mutex);
1245
1246 cb->args[0] = idx;
1247 return msg->len;
1248}
1249
1250static int devlink_sb_port_pool_set(struct devlink_port *devlink_port,
1251 unsigned int sb_index, u16 pool_index,
Ido Schimmelf2ad1a52019-04-22 12:08:39 +00001252 u32 threshold,
1253 struct netlink_ext_ack *extack)
Jiri Pirkobf797472016-04-14 18:19:13 +02001254
1255{
1256 const struct devlink_ops *ops = devlink_port->devlink->ops;
1257
Jakub Kicinskibe6fe1d2019-02-25 19:34:07 -08001258 if (ops->sb_port_pool_set)
Jiri Pirkobf797472016-04-14 18:19:13 +02001259 return ops->sb_port_pool_set(devlink_port, sb_index,
Ido Schimmelf2ad1a52019-04-22 12:08:39 +00001260 pool_index, threshold, extack);
Jiri Pirkobf797472016-04-14 18:19:13 +02001261 return -EOPNOTSUPP;
1262}
1263
1264static int devlink_nl_cmd_sb_port_pool_set_doit(struct sk_buff *skb,
1265 struct genl_info *info)
1266{
1267 struct devlink_port *devlink_port = info->user_ptr[0];
1268 struct devlink_sb *devlink_sb = info->user_ptr[1];
1269 u16 pool_index;
1270 u32 threshold;
1271 int err;
1272
1273 err = devlink_sb_pool_index_get_from_info(devlink_sb, info,
1274 &pool_index);
1275 if (err)
1276 return err;
1277
1278 if (!info->attrs[DEVLINK_ATTR_SB_THRESHOLD])
1279 return -EINVAL;
1280
1281 threshold = nla_get_u32(info->attrs[DEVLINK_ATTR_SB_THRESHOLD]);
1282 return devlink_sb_port_pool_set(devlink_port, devlink_sb->index,
Ido Schimmelf2ad1a52019-04-22 12:08:39 +00001283 pool_index, threshold, info->extack);
Jiri Pirkobf797472016-04-14 18:19:13 +02001284}
1285
1286static int
1287devlink_nl_sb_tc_pool_bind_fill(struct sk_buff *msg, struct devlink *devlink,
1288 struct devlink_port *devlink_port,
1289 struct devlink_sb *devlink_sb, u16 tc_index,
1290 enum devlink_sb_pool_type pool_type,
1291 enum devlink_command cmd,
1292 u32 portid, u32 seq, int flags)
1293{
Jiri Pirkodf38daf2016-04-14 18:19:14 +02001294 const struct devlink_ops *ops = devlink->ops;
Jiri Pirkobf797472016-04-14 18:19:13 +02001295 u16 pool_index;
1296 u32 threshold;
1297 void *hdr;
1298 int err;
1299
Jiri Pirkodf38daf2016-04-14 18:19:14 +02001300 err = ops->sb_tc_pool_bind_get(devlink_port, devlink_sb->index,
1301 tc_index, pool_type,
1302 &pool_index, &threshold);
Jiri Pirkobf797472016-04-14 18:19:13 +02001303 if (err)
1304 return err;
1305
1306 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
1307 if (!hdr)
1308 return -EMSGSIZE;
1309
1310 if (devlink_nl_put_handle(msg, devlink))
1311 goto nla_put_failure;
1312 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, devlink_port->index))
1313 goto nla_put_failure;
1314 if (nla_put_u32(msg, DEVLINK_ATTR_SB_INDEX, devlink_sb->index))
1315 goto nla_put_failure;
1316 if (nla_put_u16(msg, DEVLINK_ATTR_SB_TC_INDEX, tc_index))
1317 goto nla_put_failure;
1318 if (nla_put_u8(msg, DEVLINK_ATTR_SB_POOL_TYPE, pool_type))
1319 goto nla_put_failure;
1320 if (nla_put_u16(msg, DEVLINK_ATTR_SB_POOL_INDEX, pool_index))
1321 goto nla_put_failure;
1322 if (nla_put_u32(msg, DEVLINK_ATTR_SB_THRESHOLD, threshold))
1323 goto nla_put_failure;
1324
Jiri Pirkodf38daf2016-04-14 18:19:14 +02001325 if (ops->sb_occ_tc_port_bind_get) {
1326 u32 cur;
1327 u32 max;
1328
1329 err = ops->sb_occ_tc_port_bind_get(devlink_port,
1330 devlink_sb->index,
1331 tc_index, pool_type,
1332 &cur, &max);
1333 if (err && err != -EOPNOTSUPP)
1334 return err;
1335 if (!err) {
1336 if (nla_put_u32(msg, DEVLINK_ATTR_SB_OCC_CUR, cur))
1337 goto nla_put_failure;
1338 if (nla_put_u32(msg, DEVLINK_ATTR_SB_OCC_MAX, max))
1339 goto nla_put_failure;
1340 }
1341 }
1342
Jiri Pirkobf797472016-04-14 18:19:13 +02001343 genlmsg_end(msg, hdr);
1344 return 0;
1345
1346nla_put_failure:
1347 genlmsg_cancel(msg, hdr);
1348 return -EMSGSIZE;
1349}
1350
1351static int devlink_nl_cmd_sb_tc_pool_bind_get_doit(struct sk_buff *skb,
1352 struct genl_info *info)
1353{
1354 struct devlink_port *devlink_port = info->user_ptr[0];
1355 struct devlink *devlink = devlink_port->devlink;
1356 struct devlink_sb *devlink_sb = info->user_ptr[1];
1357 struct sk_buff *msg;
1358 enum devlink_sb_pool_type pool_type;
1359 u16 tc_index;
1360 int err;
1361
1362 err = devlink_sb_pool_type_get_from_info(info, &pool_type);
1363 if (err)
1364 return err;
1365
1366 err = devlink_sb_tc_index_get_from_info(devlink_sb, info,
1367 pool_type, &tc_index);
1368 if (err)
1369 return err;
1370
Jakub Kicinskibe6fe1d2019-02-25 19:34:07 -08001371 if (!devlink->ops->sb_tc_pool_bind_get)
Jiri Pirkobf797472016-04-14 18:19:13 +02001372 return -EOPNOTSUPP;
1373
1374 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1375 if (!msg)
1376 return -ENOMEM;
1377
1378 err = devlink_nl_sb_tc_pool_bind_fill(msg, devlink, devlink_port,
1379 devlink_sb, tc_index, pool_type,
1380 DEVLINK_CMD_SB_TC_POOL_BIND_NEW,
1381 info->snd_portid,
1382 info->snd_seq, 0);
1383 if (err) {
1384 nlmsg_free(msg);
1385 return err;
1386 }
1387
1388 return genlmsg_reply(msg, info);
1389}
1390
1391static int __sb_tc_pool_bind_get_dumpit(struct sk_buff *msg,
1392 int start, int *p_idx,
1393 struct devlink *devlink,
1394 struct devlink_sb *devlink_sb,
1395 u32 portid, u32 seq)
1396{
1397 struct devlink_port *devlink_port;
1398 u16 tc_index;
1399 int err;
1400
1401 list_for_each_entry(devlink_port, &devlink->port_list, list) {
1402 for (tc_index = 0;
1403 tc_index < devlink_sb->ingress_tc_count; tc_index++) {
1404 if (*p_idx < start) {
1405 (*p_idx)++;
1406 continue;
1407 }
1408 err = devlink_nl_sb_tc_pool_bind_fill(msg, devlink,
1409 devlink_port,
1410 devlink_sb,
1411 tc_index,
1412 DEVLINK_SB_POOL_TYPE_INGRESS,
1413 DEVLINK_CMD_SB_TC_POOL_BIND_NEW,
1414 portid, seq,
1415 NLM_F_MULTI);
1416 if (err)
1417 return err;
1418 (*p_idx)++;
1419 }
1420 for (tc_index = 0;
1421 tc_index < devlink_sb->egress_tc_count; tc_index++) {
1422 if (*p_idx < start) {
1423 (*p_idx)++;
1424 continue;
1425 }
1426 err = devlink_nl_sb_tc_pool_bind_fill(msg, devlink,
1427 devlink_port,
1428 devlink_sb,
1429 tc_index,
1430 DEVLINK_SB_POOL_TYPE_EGRESS,
1431 DEVLINK_CMD_SB_TC_POOL_BIND_NEW,
1432 portid, seq,
1433 NLM_F_MULTI);
1434 if (err)
1435 return err;
1436 (*p_idx)++;
1437 }
1438 }
1439 return 0;
1440}
1441
1442static int
1443devlink_nl_cmd_sb_tc_pool_bind_get_dumpit(struct sk_buff *msg,
1444 struct netlink_callback *cb)
1445{
1446 struct devlink *devlink;
1447 struct devlink_sb *devlink_sb;
1448 int start = cb->args[0];
1449 int idx = 0;
1450 int err;
1451
1452 mutex_lock(&devlink_mutex);
Jiri Pirkobf797472016-04-14 18:19:13 +02001453 list_for_each_entry(devlink, &devlink_list, list) {
1454 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)) ||
Jakub Kicinskibe6fe1d2019-02-25 19:34:07 -08001455 !devlink->ops->sb_tc_pool_bind_get)
Jiri Pirkobf797472016-04-14 18:19:13 +02001456 continue;
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01001457
1458 mutex_lock(&devlink->lock);
Jiri Pirkobf797472016-04-14 18:19:13 +02001459 list_for_each_entry(devlink_sb, &devlink->sb_list, list) {
1460 err = __sb_tc_pool_bind_get_dumpit(msg, start, &idx,
1461 devlink,
1462 devlink_sb,
1463 NETLINK_CB(cb->skb).portid,
1464 cb->nlh->nlmsg_seq);
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01001465 if (err && err != -EOPNOTSUPP) {
1466 mutex_unlock(&devlink->lock);
Jiri Pirkobf797472016-04-14 18:19:13 +02001467 goto out;
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01001468 }
Jiri Pirkobf797472016-04-14 18:19:13 +02001469 }
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01001470 mutex_unlock(&devlink->lock);
Jiri Pirkobf797472016-04-14 18:19:13 +02001471 }
1472out:
Jiri Pirkobf797472016-04-14 18:19:13 +02001473 mutex_unlock(&devlink_mutex);
1474
1475 cb->args[0] = idx;
1476 return msg->len;
1477}
1478
1479static int devlink_sb_tc_pool_bind_set(struct devlink_port *devlink_port,
1480 unsigned int sb_index, u16 tc_index,
1481 enum devlink_sb_pool_type pool_type,
Ido Schimmelf2ad1a52019-04-22 12:08:39 +00001482 u16 pool_index, u32 threshold,
1483 struct netlink_ext_ack *extack)
Jiri Pirkobf797472016-04-14 18:19:13 +02001484
1485{
1486 const struct devlink_ops *ops = devlink_port->devlink->ops;
1487
Jakub Kicinskibe6fe1d2019-02-25 19:34:07 -08001488 if (ops->sb_tc_pool_bind_set)
Jiri Pirkobf797472016-04-14 18:19:13 +02001489 return ops->sb_tc_pool_bind_set(devlink_port, sb_index,
1490 tc_index, pool_type,
Ido Schimmelf2ad1a52019-04-22 12:08:39 +00001491 pool_index, threshold, extack);
Jiri Pirkobf797472016-04-14 18:19:13 +02001492 return -EOPNOTSUPP;
1493}
1494
1495static int devlink_nl_cmd_sb_tc_pool_bind_set_doit(struct sk_buff *skb,
1496 struct genl_info *info)
1497{
1498 struct devlink_port *devlink_port = info->user_ptr[0];
1499 struct devlink_sb *devlink_sb = info->user_ptr[1];
1500 enum devlink_sb_pool_type pool_type;
1501 u16 tc_index;
1502 u16 pool_index;
1503 u32 threshold;
1504 int err;
1505
1506 err = devlink_sb_pool_type_get_from_info(info, &pool_type);
1507 if (err)
1508 return err;
1509
1510 err = devlink_sb_tc_index_get_from_info(devlink_sb, info,
1511 pool_type, &tc_index);
1512 if (err)
1513 return err;
1514
1515 err = devlink_sb_pool_index_get_from_info(devlink_sb, info,
1516 &pool_index);
1517 if (err)
1518 return err;
1519
1520 if (!info->attrs[DEVLINK_ATTR_SB_THRESHOLD])
1521 return -EINVAL;
1522
1523 threshold = nla_get_u32(info->attrs[DEVLINK_ATTR_SB_THRESHOLD]);
1524 return devlink_sb_tc_pool_bind_set(devlink_port, devlink_sb->index,
1525 tc_index, pool_type,
Ido Schimmelf2ad1a52019-04-22 12:08:39 +00001526 pool_index, threshold, info->extack);
Jiri Pirkobf797472016-04-14 18:19:13 +02001527}
1528
Jiri Pirkodf38daf2016-04-14 18:19:14 +02001529static int devlink_nl_cmd_sb_occ_snapshot_doit(struct sk_buff *skb,
1530 struct genl_info *info)
1531{
1532 struct devlink *devlink = info->user_ptr[0];
1533 struct devlink_sb *devlink_sb = info->user_ptr[1];
1534 const struct devlink_ops *ops = devlink->ops;
1535
Jakub Kicinskibe6fe1d2019-02-25 19:34:07 -08001536 if (ops->sb_occ_snapshot)
Jiri Pirkodf38daf2016-04-14 18:19:14 +02001537 return ops->sb_occ_snapshot(devlink, devlink_sb->index);
1538 return -EOPNOTSUPP;
1539}
1540
1541static int devlink_nl_cmd_sb_occ_max_clear_doit(struct sk_buff *skb,
1542 struct genl_info *info)
1543{
1544 struct devlink *devlink = info->user_ptr[0];
1545 struct devlink_sb *devlink_sb = info->user_ptr[1];
1546 const struct devlink_ops *ops = devlink->ops;
1547
Jakub Kicinskibe6fe1d2019-02-25 19:34:07 -08001548 if (ops->sb_occ_max_clear)
Jiri Pirkodf38daf2016-04-14 18:19:14 +02001549 return ops->sb_occ_max_clear(devlink, devlink_sb->index);
1550 return -EOPNOTSUPP;
1551}
1552
Jiri Pirko21e3d2d2017-02-09 15:54:34 +01001553static int devlink_nl_eswitch_fill(struct sk_buff *msg, struct devlink *devlink,
1554 enum devlink_command cmd, u32 portid,
1555 u32 seq, int flags)
Or Gerlitz08f4b592016-07-01 14:51:01 +03001556{
Roi Dayan59bfde02016-11-22 23:09:57 +02001557 const struct devlink_ops *ops = devlink->ops;
Leon Romanovsky98fdbea2019-06-12 15:20:11 +03001558 enum devlink_eswitch_encap_mode encap_mode;
1559 u8 inline_mode;
Or Gerlitz08f4b592016-07-01 14:51:01 +03001560 void *hdr;
Roi Dayan59bfde02016-11-22 23:09:57 +02001561 int err = 0;
1562 u16 mode;
Or Gerlitz08f4b592016-07-01 14:51:01 +03001563
1564 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
1565 if (!hdr)
1566 return -EMSGSIZE;
1567
Roi Dayan59bfde02016-11-22 23:09:57 +02001568 err = devlink_nl_put_handle(msg, devlink);
1569 if (err)
Jiri Pirko1a6aa362017-02-09 15:54:35 +01001570 goto nla_put_failure;
Or Gerlitz08f4b592016-07-01 14:51:01 +03001571
Jiri Pirko4456f612017-02-09 15:54:36 +01001572 if (ops->eswitch_mode_get) {
1573 err = ops->eswitch_mode_get(devlink, &mode);
1574 if (err)
1575 goto nla_put_failure;
1576 err = nla_put_u16(msg, DEVLINK_ATTR_ESWITCH_MODE, mode);
1577 if (err)
1578 goto nla_put_failure;
1579 }
Roi Dayan59bfde02016-11-22 23:09:57 +02001580
1581 if (ops->eswitch_inline_mode_get) {
1582 err = ops->eswitch_inline_mode_get(devlink, &inline_mode);
1583 if (err)
Jiri Pirko1a6aa362017-02-09 15:54:35 +01001584 goto nla_put_failure;
Roi Dayan59bfde02016-11-22 23:09:57 +02001585 err = nla_put_u8(msg, DEVLINK_ATTR_ESWITCH_INLINE_MODE,
1586 inline_mode);
1587 if (err)
Jiri Pirko1a6aa362017-02-09 15:54:35 +01001588 goto nla_put_failure;
Roi Dayan59bfde02016-11-22 23:09:57 +02001589 }
Or Gerlitz08f4b592016-07-01 14:51:01 +03001590
Roi Dayanf43e9b02016-09-25 13:52:44 +03001591 if (ops->eswitch_encap_mode_get) {
1592 err = ops->eswitch_encap_mode_get(devlink, &encap_mode);
1593 if (err)
1594 goto nla_put_failure;
1595 err = nla_put_u8(msg, DEVLINK_ATTR_ESWITCH_ENCAP_MODE, encap_mode);
1596 if (err)
1597 goto nla_put_failure;
1598 }
1599
Or Gerlitz08f4b592016-07-01 14:51:01 +03001600 genlmsg_end(msg, hdr);
1601 return 0;
1602
Jiri Pirko1a6aa362017-02-09 15:54:35 +01001603nla_put_failure:
Or Gerlitz08f4b592016-07-01 14:51:01 +03001604 genlmsg_cancel(msg, hdr);
Roi Dayan59bfde02016-11-22 23:09:57 +02001605 return err;
Or Gerlitz08f4b592016-07-01 14:51:01 +03001606}
1607
Jiri Pirkoadf200f2017-02-09 15:54:33 +01001608static int devlink_nl_cmd_eswitch_get_doit(struct sk_buff *skb,
1609 struct genl_info *info)
Or Gerlitz08f4b592016-07-01 14:51:01 +03001610{
1611 struct devlink *devlink = info->user_ptr[0];
Or Gerlitz08f4b592016-07-01 14:51:01 +03001612 struct sk_buff *msg;
Or Gerlitz08f4b592016-07-01 14:51:01 +03001613 int err;
1614
Or Gerlitz08f4b592016-07-01 14:51:01 +03001615 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1616 if (!msg)
1617 return -ENOMEM;
1618
Jiri Pirko21e3d2d2017-02-09 15:54:34 +01001619 err = devlink_nl_eswitch_fill(msg, devlink, DEVLINK_CMD_ESWITCH_GET,
1620 info->snd_portid, info->snd_seq, 0);
Or Gerlitz08f4b592016-07-01 14:51:01 +03001621
1622 if (err) {
1623 nlmsg_free(msg);
1624 return err;
1625 }
1626
1627 return genlmsg_reply(msg, info);
1628}
1629
Jiri Pirkoadf200f2017-02-09 15:54:33 +01001630static int devlink_nl_cmd_eswitch_set_doit(struct sk_buff *skb,
1631 struct genl_info *info)
Or Gerlitz08f4b592016-07-01 14:51:01 +03001632{
1633 struct devlink *devlink = info->user_ptr[0];
1634 const struct devlink_ops *ops = devlink->ops;
Leon Romanovsky98fdbea2019-06-12 15:20:11 +03001635 enum devlink_eswitch_encap_mode encap_mode;
1636 u8 inline_mode;
Roi Dayan59bfde02016-11-22 23:09:57 +02001637 int err = 0;
Roi Dayanf43e9b02016-09-25 13:52:44 +03001638 u16 mode;
Or Gerlitz08f4b592016-07-01 14:51:01 +03001639
Roi Dayan59bfde02016-11-22 23:09:57 +02001640 if (info->attrs[DEVLINK_ATTR_ESWITCH_MODE]) {
1641 if (!ops->eswitch_mode_set)
1642 return -EOPNOTSUPP;
1643 mode = nla_get_u16(info->attrs[DEVLINK_ATTR_ESWITCH_MODE]);
Eli Britsteindb7ff192018-08-15 16:02:18 +03001644 err = ops->eswitch_mode_set(devlink, mode, info->extack);
Roi Dayan59bfde02016-11-22 23:09:57 +02001645 if (err)
1646 return err;
1647 }
Or Gerlitz08f4b592016-07-01 14:51:01 +03001648
Roi Dayan59bfde02016-11-22 23:09:57 +02001649 if (info->attrs[DEVLINK_ATTR_ESWITCH_INLINE_MODE]) {
1650 if (!ops->eswitch_inline_mode_set)
1651 return -EOPNOTSUPP;
1652 inline_mode = nla_get_u8(
1653 info->attrs[DEVLINK_ATTR_ESWITCH_INLINE_MODE]);
Eli Britsteindb7ff192018-08-15 16:02:18 +03001654 err = ops->eswitch_inline_mode_set(devlink, inline_mode,
1655 info->extack);
Roi Dayan59bfde02016-11-22 23:09:57 +02001656 if (err)
1657 return err;
1658 }
Roi Dayanf43e9b02016-09-25 13:52:44 +03001659
1660 if (info->attrs[DEVLINK_ATTR_ESWITCH_ENCAP_MODE]) {
1661 if (!ops->eswitch_encap_mode_set)
1662 return -EOPNOTSUPP;
1663 encap_mode = nla_get_u8(info->attrs[DEVLINK_ATTR_ESWITCH_ENCAP_MODE]);
Eli Britsteindb7ff192018-08-15 16:02:18 +03001664 err = ops->eswitch_encap_mode_set(devlink, encap_mode,
1665 info->extack);
Roi Dayanf43e9b02016-09-25 13:52:44 +03001666 if (err)
1667 return err;
1668 }
1669
Roi Dayan59bfde02016-11-22 23:09:57 +02001670 return 0;
Or Gerlitz08f4b592016-07-01 14:51:01 +03001671}
1672
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02001673int devlink_dpipe_match_put(struct sk_buff *skb,
1674 struct devlink_dpipe_match *match)
1675{
1676 struct devlink_dpipe_header *header = match->header;
1677 struct devlink_dpipe_field *field = &header->fields[match->field_id];
1678 struct nlattr *match_attr;
1679
Michal Kubecekae0be8d2019-04-26 11:13:06 +02001680 match_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_DPIPE_MATCH);
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02001681 if (!match_attr)
1682 return -EMSGSIZE;
1683
1684 if (nla_put_u32(skb, DEVLINK_ATTR_DPIPE_MATCH_TYPE, match->type) ||
1685 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_INDEX, match->header_index) ||
1686 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_ID, header->id) ||
1687 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_ID, field->id) ||
1688 nla_put_u8(skb, DEVLINK_ATTR_DPIPE_HEADER_GLOBAL, header->global))
1689 goto nla_put_failure;
1690
1691 nla_nest_end(skb, match_attr);
1692 return 0;
1693
1694nla_put_failure:
1695 nla_nest_cancel(skb, match_attr);
1696 return -EMSGSIZE;
1697}
1698EXPORT_SYMBOL_GPL(devlink_dpipe_match_put);
1699
1700static int devlink_dpipe_matches_put(struct devlink_dpipe_table *table,
1701 struct sk_buff *skb)
1702{
1703 struct nlattr *matches_attr;
1704
Michal Kubecekae0be8d2019-04-26 11:13:06 +02001705 matches_attr = nla_nest_start_noflag(skb,
1706 DEVLINK_ATTR_DPIPE_TABLE_MATCHES);
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02001707 if (!matches_attr)
1708 return -EMSGSIZE;
1709
1710 if (table->table_ops->matches_dump(table->priv, skb))
1711 goto nla_put_failure;
1712
1713 nla_nest_end(skb, matches_attr);
1714 return 0;
1715
1716nla_put_failure:
1717 nla_nest_cancel(skb, matches_attr);
1718 return -EMSGSIZE;
1719}
1720
1721int devlink_dpipe_action_put(struct sk_buff *skb,
1722 struct devlink_dpipe_action *action)
1723{
1724 struct devlink_dpipe_header *header = action->header;
1725 struct devlink_dpipe_field *field = &header->fields[action->field_id];
1726 struct nlattr *action_attr;
1727
Michal Kubecekae0be8d2019-04-26 11:13:06 +02001728 action_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_DPIPE_ACTION);
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02001729 if (!action_attr)
1730 return -EMSGSIZE;
1731
1732 if (nla_put_u32(skb, DEVLINK_ATTR_DPIPE_ACTION_TYPE, action->type) ||
1733 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_INDEX, action->header_index) ||
1734 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_ID, header->id) ||
1735 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_ID, field->id) ||
1736 nla_put_u8(skb, DEVLINK_ATTR_DPIPE_HEADER_GLOBAL, header->global))
1737 goto nla_put_failure;
1738
1739 nla_nest_end(skb, action_attr);
1740 return 0;
1741
1742nla_put_failure:
1743 nla_nest_cancel(skb, action_attr);
1744 return -EMSGSIZE;
1745}
1746EXPORT_SYMBOL_GPL(devlink_dpipe_action_put);
1747
1748static int devlink_dpipe_actions_put(struct devlink_dpipe_table *table,
1749 struct sk_buff *skb)
1750{
1751 struct nlattr *actions_attr;
1752
Michal Kubecekae0be8d2019-04-26 11:13:06 +02001753 actions_attr = nla_nest_start_noflag(skb,
1754 DEVLINK_ATTR_DPIPE_TABLE_ACTIONS);
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02001755 if (!actions_attr)
1756 return -EMSGSIZE;
1757
1758 if (table->table_ops->actions_dump(table->priv, skb))
1759 goto nla_put_failure;
1760
1761 nla_nest_end(skb, actions_attr);
1762 return 0;
1763
1764nla_put_failure:
1765 nla_nest_cancel(skb, actions_attr);
1766 return -EMSGSIZE;
1767}
1768
1769static int devlink_dpipe_table_put(struct sk_buff *skb,
1770 struct devlink_dpipe_table *table)
1771{
1772 struct nlattr *table_attr;
Arkadi Sharshevskyffd3cdc2017-08-24 08:40:02 +02001773 u64 table_size;
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02001774
Arkadi Sharshevskyffd3cdc2017-08-24 08:40:02 +02001775 table_size = table->table_ops->size_get(table->priv);
Michal Kubecekae0be8d2019-04-26 11:13:06 +02001776 table_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_DPIPE_TABLE);
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02001777 if (!table_attr)
1778 return -EMSGSIZE;
1779
1780 if (nla_put_string(skb, DEVLINK_ATTR_DPIPE_TABLE_NAME, table->name) ||
Arkadi Sharshevskyffd3cdc2017-08-24 08:40:02 +02001781 nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_TABLE_SIZE, table_size,
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02001782 DEVLINK_ATTR_PAD))
1783 goto nla_put_failure;
1784 if (nla_put_u8(skb, DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED,
1785 table->counters_enabled))
1786 goto nla_put_failure;
1787
Arkadi Sharshevsky56dc7cd2018-01-15 08:59:05 +01001788 if (table->resource_valid) {
Arkadi Sharshevsky3d18e4f2018-02-26 18:25:42 +02001789 if (nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_TABLE_RESOURCE_ID,
1790 table->resource_id, DEVLINK_ATTR_PAD) ||
1791 nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_TABLE_RESOURCE_UNITS,
1792 table->resource_units, DEVLINK_ATTR_PAD))
1793 goto nla_put_failure;
Arkadi Sharshevsky56dc7cd2018-01-15 08:59:05 +01001794 }
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02001795 if (devlink_dpipe_matches_put(table, skb))
1796 goto nla_put_failure;
1797
1798 if (devlink_dpipe_actions_put(table, skb))
1799 goto nla_put_failure;
1800
1801 nla_nest_end(skb, table_attr);
1802 return 0;
1803
1804nla_put_failure:
1805 nla_nest_cancel(skb, table_attr);
1806 return -EMSGSIZE;
1807}
1808
1809static int devlink_dpipe_send_and_alloc_skb(struct sk_buff **pskb,
1810 struct genl_info *info)
1811{
1812 int err;
1813
1814 if (*pskb) {
1815 err = genlmsg_reply(*pskb, info);
1816 if (err)
1817 return err;
1818 }
1819 *pskb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
1820 if (!*pskb)
1821 return -ENOMEM;
1822 return 0;
1823}
1824
1825static int devlink_dpipe_tables_fill(struct genl_info *info,
1826 enum devlink_command cmd, int flags,
1827 struct list_head *dpipe_tables,
1828 const char *table_name)
1829{
1830 struct devlink *devlink = info->user_ptr[0];
1831 struct devlink_dpipe_table *table;
1832 struct nlattr *tables_attr;
1833 struct sk_buff *skb = NULL;
1834 struct nlmsghdr *nlh;
1835 bool incomplete;
1836 void *hdr;
1837 int i;
1838 int err;
1839
1840 table = list_first_entry(dpipe_tables,
1841 struct devlink_dpipe_table, list);
1842start_again:
1843 err = devlink_dpipe_send_and_alloc_skb(&skb, info);
1844 if (err)
1845 return err;
1846
1847 hdr = genlmsg_put(skb, info->snd_portid, info->snd_seq,
1848 &devlink_nl_family, NLM_F_MULTI, cmd);
Haishuang Yan6044bd42017-06-05 08:57:21 +08001849 if (!hdr) {
1850 nlmsg_free(skb);
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02001851 return -EMSGSIZE;
Haishuang Yan6044bd42017-06-05 08:57:21 +08001852 }
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02001853
1854 if (devlink_nl_put_handle(skb, devlink))
1855 goto nla_put_failure;
Michal Kubecekae0be8d2019-04-26 11:13:06 +02001856 tables_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_DPIPE_TABLES);
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02001857 if (!tables_attr)
1858 goto nla_put_failure;
1859
1860 i = 0;
1861 incomplete = false;
1862 list_for_each_entry_from(table, dpipe_tables, list) {
1863 if (!table_name) {
1864 err = devlink_dpipe_table_put(skb, table);
1865 if (err) {
1866 if (!i)
1867 goto err_table_put;
1868 incomplete = true;
1869 break;
1870 }
1871 } else {
1872 if (!strcmp(table->name, table_name)) {
1873 err = devlink_dpipe_table_put(skb, table);
1874 if (err)
1875 break;
1876 }
1877 }
1878 i++;
1879 }
1880
1881 nla_nest_end(skb, tables_attr);
1882 genlmsg_end(skb, hdr);
1883 if (incomplete)
1884 goto start_again;
1885
1886send_done:
1887 nlh = nlmsg_put(skb, info->snd_portid, info->snd_seq,
1888 NLMSG_DONE, 0, flags | NLM_F_MULTI);
1889 if (!nlh) {
1890 err = devlink_dpipe_send_and_alloc_skb(&skb, info);
1891 if (err)
Arkadi Sharshevsky7fe4d6d2018-03-18 17:37:22 +02001892 return err;
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02001893 goto send_done;
1894 }
1895
1896 return genlmsg_reply(skb, info);
1897
1898nla_put_failure:
1899 err = -EMSGSIZE;
1900err_table_put:
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02001901 nlmsg_free(skb);
1902 return err;
1903}
1904
1905static int devlink_nl_cmd_dpipe_table_get(struct sk_buff *skb,
1906 struct genl_info *info)
1907{
1908 struct devlink *devlink = info->user_ptr[0];
1909 const char *table_name = NULL;
1910
1911 if (info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME])
1912 table_name = nla_data(info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME]);
1913
1914 return devlink_dpipe_tables_fill(info, DEVLINK_CMD_DPIPE_TABLE_GET, 0,
1915 &devlink->dpipe_table_list,
1916 table_name);
1917}
1918
1919static int devlink_dpipe_value_put(struct sk_buff *skb,
1920 struct devlink_dpipe_value *value)
1921{
1922 if (nla_put(skb, DEVLINK_ATTR_DPIPE_VALUE,
1923 value->value_size, value->value))
1924 return -EMSGSIZE;
1925 if (value->mask)
1926 if (nla_put(skb, DEVLINK_ATTR_DPIPE_VALUE_MASK,
1927 value->value_size, value->mask))
1928 return -EMSGSIZE;
1929 if (value->mapping_valid)
1930 if (nla_put_u32(skb, DEVLINK_ATTR_DPIPE_VALUE_MAPPING,
1931 value->mapping_value))
1932 return -EMSGSIZE;
1933 return 0;
1934}
1935
1936static int devlink_dpipe_action_value_put(struct sk_buff *skb,
1937 struct devlink_dpipe_value *value)
1938{
1939 if (!value->action)
1940 return -EINVAL;
1941 if (devlink_dpipe_action_put(skb, value->action))
1942 return -EMSGSIZE;
1943 if (devlink_dpipe_value_put(skb, value))
1944 return -EMSGSIZE;
1945 return 0;
1946}
1947
1948static int devlink_dpipe_action_values_put(struct sk_buff *skb,
1949 struct devlink_dpipe_value *values,
1950 unsigned int values_count)
1951{
1952 struct nlattr *action_attr;
1953 int i;
1954 int err;
1955
1956 for (i = 0; i < values_count; i++) {
Michal Kubecekae0be8d2019-04-26 11:13:06 +02001957 action_attr = nla_nest_start_noflag(skb,
1958 DEVLINK_ATTR_DPIPE_ACTION_VALUE);
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02001959 if (!action_attr)
1960 return -EMSGSIZE;
1961 err = devlink_dpipe_action_value_put(skb, &values[i]);
1962 if (err)
1963 goto err_action_value_put;
1964 nla_nest_end(skb, action_attr);
1965 }
1966 return 0;
1967
1968err_action_value_put:
1969 nla_nest_cancel(skb, action_attr);
1970 return err;
1971}
1972
1973static int devlink_dpipe_match_value_put(struct sk_buff *skb,
1974 struct devlink_dpipe_value *value)
1975{
1976 if (!value->match)
1977 return -EINVAL;
1978 if (devlink_dpipe_match_put(skb, value->match))
1979 return -EMSGSIZE;
1980 if (devlink_dpipe_value_put(skb, value))
1981 return -EMSGSIZE;
1982 return 0;
1983}
1984
1985static int devlink_dpipe_match_values_put(struct sk_buff *skb,
1986 struct devlink_dpipe_value *values,
1987 unsigned int values_count)
1988{
1989 struct nlattr *match_attr;
1990 int i;
1991 int err;
1992
1993 for (i = 0; i < values_count; i++) {
Michal Kubecekae0be8d2019-04-26 11:13:06 +02001994 match_attr = nla_nest_start_noflag(skb,
1995 DEVLINK_ATTR_DPIPE_MATCH_VALUE);
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02001996 if (!match_attr)
1997 return -EMSGSIZE;
1998 err = devlink_dpipe_match_value_put(skb, &values[i]);
1999 if (err)
2000 goto err_match_value_put;
2001 nla_nest_end(skb, match_attr);
2002 }
2003 return 0;
2004
2005err_match_value_put:
2006 nla_nest_cancel(skb, match_attr);
2007 return err;
2008}
2009
2010static int devlink_dpipe_entry_put(struct sk_buff *skb,
2011 struct devlink_dpipe_entry *entry)
2012{
2013 struct nlattr *entry_attr, *matches_attr, *actions_attr;
2014 int err;
2015
Michal Kubecekae0be8d2019-04-26 11:13:06 +02002016 entry_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_DPIPE_ENTRY);
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02002017 if (!entry_attr)
2018 return -EMSGSIZE;
2019
2020 if (nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_ENTRY_INDEX, entry->index,
2021 DEVLINK_ATTR_PAD))
2022 goto nla_put_failure;
2023 if (entry->counter_valid)
2024 if (nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_ENTRY_COUNTER,
2025 entry->counter, DEVLINK_ATTR_PAD))
2026 goto nla_put_failure;
2027
Michal Kubecekae0be8d2019-04-26 11:13:06 +02002028 matches_attr = nla_nest_start_noflag(skb,
2029 DEVLINK_ATTR_DPIPE_ENTRY_MATCH_VALUES);
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02002030 if (!matches_attr)
2031 goto nla_put_failure;
2032
2033 err = devlink_dpipe_match_values_put(skb, entry->match_values,
2034 entry->match_values_count);
2035 if (err) {
2036 nla_nest_cancel(skb, matches_attr);
2037 goto err_match_values_put;
2038 }
2039 nla_nest_end(skb, matches_attr);
2040
Michal Kubecekae0be8d2019-04-26 11:13:06 +02002041 actions_attr = nla_nest_start_noflag(skb,
2042 DEVLINK_ATTR_DPIPE_ENTRY_ACTION_VALUES);
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02002043 if (!actions_attr)
2044 goto nla_put_failure;
2045
2046 err = devlink_dpipe_action_values_put(skb, entry->action_values,
2047 entry->action_values_count);
2048 if (err) {
2049 nla_nest_cancel(skb, actions_attr);
2050 goto err_action_values_put;
2051 }
2052 nla_nest_end(skb, actions_attr);
2053
2054 nla_nest_end(skb, entry_attr);
2055 return 0;
2056
2057nla_put_failure:
2058 err = -EMSGSIZE;
2059err_match_values_put:
2060err_action_values_put:
2061 nla_nest_cancel(skb, entry_attr);
2062 return err;
2063}
2064
2065static struct devlink_dpipe_table *
2066devlink_dpipe_table_find(struct list_head *dpipe_tables,
2067 const char *table_name)
2068{
2069 struct devlink_dpipe_table *table;
2070
2071 list_for_each_entry_rcu(table, dpipe_tables, list) {
2072 if (!strcmp(table->name, table_name))
2073 return table;
2074 }
2075 return NULL;
2076}
2077
2078int devlink_dpipe_entry_ctx_prepare(struct devlink_dpipe_dump_ctx *dump_ctx)
2079{
2080 struct devlink *devlink;
2081 int err;
2082
2083 err = devlink_dpipe_send_and_alloc_skb(&dump_ctx->skb,
2084 dump_ctx->info);
2085 if (err)
2086 return err;
2087
2088 dump_ctx->hdr = genlmsg_put(dump_ctx->skb,
2089 dump_ctx->info->snd_portid,
2090 dump_ctx->info->snd_seq,
2091 &devlink_nl_family, NLM_F_MULTI,
2092 dump_ctx->cmd);
2093 if (!dump_ctx->hdr)
2094 goto nla_put_failure;
2095
2096 devlink = dump_ctx->info->user_ptr[0];
2097 if (devlink_nl_put_handle(dump_ctx->skb, devlink))
2098 goto nla_put_failure;
Michal Kubecekae0be8d2019-04-26 11:13:06 +02002099 dump_ctx->nest = nla_nest_start_noflag(dump_ctx->skb,
2100 DEVLINK_ATTR_DPIPE_ENTRIES);
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02002101 if (!dump_ctx->nest)
2102 goto nla_put_failure;
2103 return 0;
2104
2105nla_put_failure:
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02002106 nlmsg_free(dump_ctx->skb);
2107 return -EMSGSIZE;
2108}
2109EXPORT_SYMBOL_GPL(devlink_dpipe_entry_ctx_prepare);
2110
2111int devlink_dpipe_entry_ctx_append(struct devlink_dpipe_dump_ctx *dump_ctx,
2112 struct devlink_dpipe_entry *entry)
2113{
2114 return devlink_dpipe_entry_put(dump_ctx->skb, entry);
2115}
2116EXPORT_SYMBOL_GPL(devlink_dpipe_entry_ctx_append);
2117
2118int devlink_dpipe_entry_ctx_close(struct devlink_dpipe_dump_ctx *dump_ctx)
2119{
2120 nla_nest_end(dump_ctx->skb, dump_ctx->nest);
2121 genlmsg_end(dump_ctx->skb, dump_ctx->hdr);
2122 return 0;
2123}
2124EXPORT_SYMBOL_GPL(devlink_dpipe_entry_ctx_close);
2125
Arkadi Sharshevsky35807322017-08-24 08:40:03 +02002126void devlink_dpipe_entry_clear(struct devlink_dpipe_entry *entry)
2127
2128{
2129 unsigned int value_count, value_index;
2130 struct devlink_dpipe_value *value;
2131
2132 value = entry->action_values;
2133 value_count = entry->action_values_count;
2134 for (value_index = 0; value_index < value_count; value_index++) {
2135 kfree(value[value_index].value);
2136 kfree(value[value_index].mask);
2137 }
2138
2139 value = entry->match_values;
2140 value_count = entry->match_values_count;
2141 for (value_index = 0; value_index < value_count; value_index++) {
2142 kfree(value[value_index].value);
2143 kfree(value[value_index].mask);
2144 }
2145}
2146EXPORT_SYMBOL(devlink_dpipe_entry_clear);
2147
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02002148static int devlink_dpipe_entries_fill(struct genl_info *info,
2149 enum devlink_command cmd, int flags,
2150 struct devlink_dpipe_table *table)
2151{
2152 struct devlink_dpipe_dump_ctx dump_ctx;
2153 struct nlmsghdr *nlh;
2154 int err;
2155
2156 dump_ctx.skb = NULL;
2157 dump_ctx.cmd = cmd;
2158 dump_ctx.info = info;
2159
2160 err = table->table_ops->entries_dump(table->priv,
2161 table->counters_enabled,
2162 &dump_ctx);
2163 if (err)
Arkadi Sharshevsky7fe4d6d2018-03-18 17:37:22 +02002164 return err;
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02002165
2166send_done:
2167 nlh = nlmsg_put(dump_ctx.skb, info->snd_portid, info->snd_seq,
2168 NLMSG_DONE, 0, flags | NLM_F_MULTI);
2169 if (!nlh) {
2170 err = devlink_dpipe_send_and_alloc_skb(&dump_ctx.skb, info);
2171 if (err)
Arkadi Sharshevsky7fe4d6d2018-03-18 17:37:22 +02002172 return err;
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02002173 goto send_done;
2174 }
2175 return genlmsg_reply(dump_ctx.skb, info);
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02002176}
2177
2178static int devlink_nl_cmd_dpipe_entries_get(struct sk_buff *skb,
2179 struct genl_info *info)
2180{
2181 struct devlink *devlink = info->user_ptr[0];
2182 struct devlink_dpipe_table *table;
2183 const char *table_name;
2184
2185 if (!info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME])
2186 return -EINVAL;
2187
2188 table_name = nla_data(info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME]);
2189 table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
2190 table_name);
2191 if (!table)
2192 return -EINVAL;
2193
2194 if (!table->table_ops->entries_dump)
2195 return -EINVAL;
2196
2197 return devlink_dpipe_entries_fill(info, DEVLINK_CMD_DPIPE_ENTRIES_GET,
2198 0, table);
2199}
2200
2201static int devlink_dpipe_fields_put(struct sk_buff *skb,
2202 const struct devlink_dpipe_header *header)
2203{
2204 struct devlink_dpipe_field *field;
2205 struct nlattr *field_attr;
2206 int i;
2207
2208 for (i = 0; i < header->fields_count; i++) {
2209 field = &header->fields[i];
Michal Kubecekae0be8d2019-04-26 11:13:06 +02002210 field_attr = nla_nest_start_noflag(skb,
2211 DEVLINK_ATTR_DPIPE_FIELD);
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02002212 if (!field_attr)
2213 return -EMSGSIZE;
2214 if (nla_put_string(skb, DEVLINK_ATTR_DPIPE_FIELD_NAME, field->name) ||
2215 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_ID, field->id) ||
2216 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_BITWIDTH, field->bitwidth) ||
2217 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_MAPPING_TYPE, field->mapping_type))
2218 goto nla_put_failure;
2219 nla_nest_end(skb, field_attr);
2220 }
2221 return 0;
2222
2223nla_put_failure:
2224 nla_nest_cancel(skb, field_attr);
2225 return -EMSGSIZE;
2226}
2227
2228static int devlink_dpipe_header_put(struct sk_buff *skb,
2229 struct devlink_dpipe_header *header)
2230{
2231 struct nlattr *fields_attr, *header_attr;
2232 int err;
2233
Michal Kubecekae0be8d2019-04-26 11:13:06 +02002234 header_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_DPIPE_HEADER);
Wei Yongjuncb6bf9c2017-04-11 16:02:02 +00002235 if (!header_attr)
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02002236 return -EMSGSIZE;
2237
2238 if (nla_put_string(skb, DEVLINK_ATTR_DPIPE_HEADER_NAME, header->name) ||
2239 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_ID, header->id) ||
2240 nla_put_u8(skb, DEVLINK_ATTR_DPIPE_HEADER_GLOBAL, header->global))
2241 goto nla_put_failure;
2242
Michal Kubecekae0be8d2019-04-26 11:13:06 +02002243 fields_attr = nla_nest_start_noflag(skb,
2244 DEVLINK_ATTR_DPIPE_HEADER_FIELDS);
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02002245 if (!fields_attr)
2246 goto nla_put_failure;
2247
2248 err = devlink_dpipe_fields_put(skb, header);
2249 if (err) {
2250 nla_nest_cancel(skb, fields_attr);
2251 goto nla_put_failure;
2252 }
2253 nla_nest_end(skb, fields_attr);
2254 nla_nest_end(skb, header_attr);
2255 return 0;
2256
2257nla_put_failure:
2258 err = -EMSGSIZE;
2259 nla_nest_cancel(skb, header_attr);
2260 return err;
2261}
2262
2263static int devlink_dpipe_headers_fill(struct genl_info *info,
2264 enum devlink_command cmd, int flags,
2265 struct devlink_dpipe_headers *
2266 dpipe_headers)
2267{
2268 struct devlink *devlink = info->user_ptr[0];
2269 struct nlattr *headers_attr;
2270 struct sk_buff *skb = NULL;
2271 struct nlmsghdr *nlh;
2272 void *hdr;
2273 int i, j;
2274 int err;
2275
2276 i = 0;
2277start_again:
2278 err = devlink_dpipe_send_and_alloc_skb(&skb, info);
2279 if (err)
2280 return err;
2281
2282 hdr = genlmsg_put(skb, info->snd_portid, info->snd_seq,
2283 &devlink_nl_family, NLM_F_MULTI, cmd);
Haishuang Yan6044bd42017-06-05 08:57:21 +08002284 if (!hdr) {
2285 nlmsg_free(skb);
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02002286 return -EMSGSIZE;
Haishuang Yan6044bd42017-06-05 08:57:21 +08002287 }
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02002288
2289 if (devlink_nl_put_handle(skb, devlink))
2290 goto nla_put_failure;
Michal Kubecekae0be8d2019-04-26 11:13:06 +02002291 headers_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_DPIPE_HEADERS);
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02002292 if (!headers_attr)
2293 goto nla_put_failure;
2294
2295 j = 0;
2296 for (; i < dpipe_headers->headers_count; i++) {
2297 err = devlink_dpipe_header_put(skb, dpipe_headers->headers[i]);
2298 if (err) {
2299 if (!j)
2300 goto err_table_put;
2301 break;
2302 }
2303 j++;
2304 }
2305 nla_nest_end(skb, headers_attr);
2306 genlmsg_end(skb, hdr);
2307 if (i != dpipe_headers->headers_count)
2308 goto start_again;
2309
2310send_done:
2311 nlh = nlmsg_put(skb, info->snd_portid, info->snd_seq,
2312 NLMSG_DONE, 0, flags | NLM_F_MULTI);
2313 if (!nlh) {
2314 err = devlink_dpipe_send_and_alloc_skb(&skb, info);
2315 if (err)
Arkadi Sharshevsky7fe4d6d2018-03-18 17:37:22 +02002316 return err;
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02002317 goto send_done;
2318 }
2319 return genlmsg_reply(skb, info);
2320
2321nla_put_failure:
2322 err = -EMSGSIZE;
2323err_table_put:
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02002324 nlmsg_free(skb);
2325 return err;
2326}
2327
2328static int devlink_nl_cmd_dpipe_headers_get(struct sk_buff *skb,
2329 struct genl_info *info)
2330{
2331 struct devlink *devlink = info->user_ptr[0];
2332
2333 if (!devlink->dpipe_headers)
2334 return -EOPNOTSUPP;
2335 return devlink_dpipe_headers_fill(info, DEVLINK_CMD_DPIPE_HEADERS_GET,
2336 0, devlink->dpipe_headers);
2337}
2338
2339static int devlink_dpipe_table_counters_set(struct devlink *devlink,
2340 const char *table_name,
2341 bool enable)
2342{
2343 struct devlink_dpipe_table *table;
2344
2345 table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
2346 table_name);
2347 if (!table)
2348 return -EINVAL;
2349
2350 if (table->counter_control_extern)
2351 return -EOPNOTSUPP;
2352
2353 if (!(table->counters_enabled ^ enable))
2354 return 0;
2355
2356 table->counters_enabled = enable;
2357 if (table->table_ops->counters_set_update)
2358 table->table_ops->counters_set_update(table->priv, enable);
2359 return 0;
2360}
2361
2362static int devlink_nl_cmd_dpipe_table_counters_set(struct sk_buff *skb,
2363 struct genl_info *info)
2364{
2365 struct devlink *devlink = info->user_ptr[0];
2366 const char *table_name;
2367 bool counters_enable;
2368
2369 if (!info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME] ||
2370 !info->attrs[DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED])
2371 return -EINVAL;
2372
2373 table_name = nla_data(info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME]);
2374 counters_enable = !!nla_get_u8(info->attrs[DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED]);
2375
2376 return devlink_dpipe_table_counters_set(devlink, table_name,
2377 counters_enable);
2378}
2379
Wei Yongjun43dd7512018-01-17 03:27:42 +00002380static struct devlink_resource *
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01002381devlink_resource_find(struct devlink *devlink,
2382 struct devlink_resource *resource, u64 resource_id)
2383{
2384 struct list_head *resource_list;
2385
2386 if (resource)
2387 resource_list = &resource->resource_list;
2388 else
2389 resource_list = &devlink->resource_list;
2390
2391 list_for_each_entry(resource, resource_list, list) {
2392 struct devlink_resource *child_resource;
2393
2394 if (resource->id == resource_id)
2395 return resource;
2396
2397 child_resource = devlink_resource_find(devlink, resource,
2398 resource_id);
2399 if (child_resource)
2400 return child_resource;
2401 }
2402 return NULL;
2403}
2404
Wei Yongjun43dd7512018-01-17 03:27:42 +00002405static void
2406devlink_resource_validate_children(struct devlink_resource *resource)
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01002407{
2408 struct devlink_resource *child_resource;
2409 bool size_valid = true;
2410 u64 parts_size = 0;
2411
2412 if (list_empty(&resource->resource_list))
2413 goto out;
2414
2415 list_for_each_entry(child_resource, &resource->resource_list, list)
2416 parts_size += child_resource->size_new;
2417
Arkadi Sharshevskyb9d17172018-02-26 10:59:53 +01002418 if (parts_size > resource->size_new)
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01002419 size_valid = false;
2420out:
2421 resource->size_valid = size_valid;
2422}
2423
Arkadi Sharshevskycc944ea2018-02-20 08:44:20 +01002424static int
2425devlink_resource_validate_size(struct devlink_resource *resource, u64 size,
2426 struct netlink_ext_ack *extack)
2427{
2428 u64 reminder;
2429 int err = 0;
2430
David S. Miller0f3e9c92018-03-06 00:53:44 -05002431 if (size > resource->size_params.size_max) {
Arkadi Sharshevskycc944ea2018-02-20 08:44:20 +01002432 NL_SET_ERR_MSG_MOD(extack, "Size larger than maximum");
2433 err = -EINVAL;
2434 }
2435
David S. Miller0f3e9c92018-03-06 00:53:44 -05002436 if (size < resource->size_params.size_min) {
Arkadi Sharshevskycc944ea2018-02-20 08:44:20 +01002437 NL_SET_ERR_MSG_MOD(extack, "Size smaller than minimum");
2438 err = -EINVAL;
2439 }
2440
David S. Miller0f3e9c92018-03-06 00:53:44 -05002441 div64_u64_rem(size, resource->size_params.size_granularity, &reminder);
Arkadi Sharshevskycc944ea2018-02-20 08:44:20 +01002442 if (reminder) {
2443 NL_SET_ERR_MSG_MOD(extack, "Wrong granularity");
2444 err = -EINVAL;
2445 }
2446
2447 return err;
2448}
2449
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01002450static int devlink_nl_cmd_resource_set(struct sk_buff *skb,
2451 struct genl_info *info)
2452{
2453 struct devlink *devlink = info->user_ptr[0];
2454 struct devlink_resource *resource;
2455 u64 resource_id;
2456 u64 size;
2457 int err;
2458
2459 if (!info->attrs[DEVLINK_ATTR_RESOURCE_ID] ||
2460 !info->attrs[DEVLINK_ATTR_RESOURCE_SIZE])
2461 return -EINVAL;
2462 resource_id = nla_get_u64(info->attrs[DEVLINK_ATTR_RESOURCE_ID]);
2463
2464 resource = devlink_resource_find(devlink, NULL, resource_id);
2465 if (!resource)
2466 return -EINVAL;
2467
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01002468 size = nla_get_u64(info->attrs[DEVLINK_ATTR_RESOURCE_SIZE]);
Arkadi Sharshevskycc944ea2018-02-20 08:44:20 +01002469 err = devlink_resource_validate_size(resource, size, info->extack);
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01002470 if (err)
2471 return err;
2472
2473 resource->size_new = size;
2474 devlink_resource_validate_children(resource);
2475 if (resource->parent)
2476 devlink_resource_validate_children(resource->parent);
2477 return 0;
2478}
2479
Arkadi Sharshevsky3d18e4f2018-02-26 18:25:42 +02002480static int
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01002481devlink_resource_size_params_put(struct devlink_resource *resource,
2482 struct sk_buff *skb)
2483{
2484 struct devlink_resource_size_params *size_params;
2485
Jiri Pirko77d27092018-02-28 13:12:09 +01002486 size_params = &resource->size_params;
Arkadi Sharshevsky3d18e4f2018-02-26 18:25:42 +02002487 if (nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_SIZE_GRAN,
2488 size_params->size_granularity, DEVLINK_ATTR_PAD) ||
2489 nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_SIZE_MAX,
2490 size_params->size_max, DEVLINK_ATTR_PAD) ||
2491 nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_SIZE_MIN,
2492 size_params->size_min, DEVLINK_ATTR_PAD) ||
2493 nla_put_u8(skb, DEVLINK_ATTR_RESOURCE_UNIT, size_params->unit))
2494 return -EMSGSIZE;
2495 return 0;
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01002496}
2497
Jiri Pirkofc56be42018-04-05 22:13:21 +02002498static int devlink_resource_occ_put(struct devlink_resource *resource,
2499 struct sk_buff *skb)
2500{
2501 if (!resource->occ_get)
2502 return 0;
2503 return nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_OCC,
2504 resource->occ_get(resource->occ_get_priv),
2505 DEVLINK_ATTR_PAD);
2506}
2507
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01002508static int devlink_resource_put(struct devlink *devlink, struct sk_buff *skb,
2509 struct devlink_resource *resource)
2510{
2511 struct devlink_resource *child_resource;
2512 struct nlattr *child_resource_attr;
2513 struct nlattr *resource_attr;
2514
Michal Kubecekae0be8d2019-04-26 11:13:06 +02002515 resource_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_RESOURCE);
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01002516 if (!resource_attr)
2517 return -EMSGSIZE;
2518
2519 if (nla_put_string(skb, DEVLINK_ATTR_RESOURCE_NAME, resource->name) ||
2520 nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_SIZE, resource->size,
2521 DEVLINK_ATTR_PAD) ||
2522 nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_ID, resource->id,
2523 DEVLINK_ATTR_PAD))
2524 goto nla_put_failure;
2525 if (resource->size != resource->size_new)
2526 nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_SIZE_NEW,
2527 resource->size_new, DEVLINK_ATTR_PAD);
Jiri Pirkofc56be42018-04-05 22:13:21 +02002528 if (devlink_resource_occ_put(resource, skb))
2529 goto nla_put_failure;
Arkadi Sharshevsky3d18e4f2018-02-26 18:25:42 +02002530 if (devlink_resource_size_params_put(resource, skb))
2531 goto nla_put_failure;
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01002532 if (list_empty(&resource->resource_list))
2533 goto out;
2534
2535 if (nla_put_u8(skb, DEVLINK_ATTR_RESOURCE_SIZE_VALID,
2536 resource->size_valid))
2537 goto nla_put_failure;
2538
Michal Kubecekae0be8d2019-04-26 11:13:06 +02002539 child_resource_attr = nla_nest_start_noflag(skb,
2540 DEVLINK_ATTR_RESOURCE_LIST);
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01002541 if (!child_resource_attr)
2542 goto nla_put_failure;
2543
2544 list_for_each_entry(child_resource, &resource->resource_list, list) {
2545 if (devlink_resource_put(devlink, skb, child_resource))
2546 goto resource_put_failure;
2547 }
2548
2549 nla_nest_end(skb, child_resource_attr);
2550out:
2551 nla_nest_end(skb, resource_attr);
2552 return 0;
2553
2554resource_put_failure:
2555 nla_nest_cancel(skb, child_resource_attr);
2556nla_put_failure:
2557 nla_nest_cancel(skb, resource_attr);
2558 return -EMSGSIZE;
2559}
2560
2561static int devlink_resource_fill(struct genl_info *info,
2562 enum devlink_command cmd, int flags)
2563{
2564 struct devlink *devlink = info->user_ptr[0];
2565 struct devlink_resource *resource;
2566 struct nlattr *resources_attr;
2567 struct sk_buff *skb = NULL;
2568 struct nlmsghdr *nlh;
2569 bool incomplete;
2570 void *hdr;
2571 int i;
2572 int err;
2573
2574 resource = list_first_entry(&devlink->resource_list,
2575 struct devlink_resource, list);
2576start_again:
2577 err = devlink_dpipe_send_and_alloc_skb(&skb, info);
2578 if (err)
2579 return err;
2580
2581 hdr = genlmsg_put(skb, info->snd_portid, info->snd_seq,
2582 &devlink_nl_family, NLM_F_MULTI, cmd);
2583 if (!hdr) {
2584 nlmsg_free(skb);
2585 return -EMSGSIZE;
2586 }
2587
2588 if (devlink_nl_put_handle(skb, devlink))
2589 goto nla_put_failure;
2590
Michal Kubecekae0be8d2019-04-26 11:13:06 +02002591 resources_attr = nla_nest_start_noflag(skb,
2592 DEVLINK_ATTR_RESOURCE_LIST);
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01002593 if (!resources_attr)
2594 goto nla_put_failure;
2595
2596 incomplete = false;
2597 i = 0;
2598 list_for_each_entry_from(resource, &devlink->resource_list, list) {
2599 err = devlink_resource_put(devlink, skb, resource);
2600 if (err) {
2601 if (!i)
2602 goto err_resource_put;
2603 incomplete = true;
2604 break;
2605 }
2606 i++;
2607 }
2608 nla_nest_end(skb, resources_attr);
2609 genlmsg_end(skb, hdr);
2610 if (incomplete)
2611 goto start_again;
2612send_done:
2613 nlh = nlmsg_put(skb, info->snd_portid, info->snd_seq,
2614 NLMSG_DONE, 0, flags | NLM_F_MULTI);
2615 if (!nlh) {
2616 err = devlink_dpipe_send_and_alloc_skb(&skb, info);
2617 if (err)
Dan Carpenter83fe9a92018-09-21 11:07:55 +03002618 return err;
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01002619 goto send_done;
2620 }
2621 return genlmsg_reply(skb, info);
2622
2623nla_put_failure:
2624 err = -EMSGSIZE;
2625err_resource_put:
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01002626 nlmsg_free(skb);
2627 return err;
2628}
2629
2630static int devlink_nl_cmd_resource_dump(struct sk_buff *skb,
2631 struct genl_info *info)
2632{
2633 struct devlink *devlink = info->user_ptr[0];
2634
2635 if (list_empty(&devlink->resource_list))
2636 return -EOPNOTSUPP;
2637
2638 return devlink_resource_fill(info, DEVLINK_CMD_RESOURCE_DUMP, 0);
2639}
2640
Arkadi Sharshevsky2d8dc5b2018-01-15 08:59:04 +01002641static int
2642devlink_resources_validate(struct devlink *devlink,
2643 struct devlink_resource *resource,
2644 struct genl_info *info)
2645{
2646 struct list_head *resource_list;
2647 int err = 0;
2648
2649 if (resource)
2650 resource_list = &resource->resource_list;
2651 else
2652 resource_list = &devlink->resource_list;
2653
2654 list_for_each_entry(resource, resource_list, list) {
2655 if (!resource->size_valid)
2656 return -EINVAL;
2657 err = devlink_resources_validate(devlink, resource, info);
2658 if (err)
2659 return err;
2660 }
2661 return err;
2662}
2663
2664static int devlink_nl_cmd_reload(struct sk_buff *skb, struct genl_info *info)
2665{
2666 struct devlink *devlink = info->user_ptr[0];
2667 int err;
2668
2669 if (!devlink->ops->reload)
2670 return -EOPNOTSUPP;
2671
2672 err = devlink_resources_validate(devlink, NULL, info);
2673 if (err) {
2674 NL_SET_ERR_MSG_MOD(info->extack, "resources size validation failed");
2675 return err;
2676 }
David Ahernac0fc8a2018-06-05 08:14:09 -07002677 return devlink->ops->reload(devlink, info->extack);
Arkadi Sharshevsky2d8dc5b2018-01-15 08:59:04 +01002678}
2679
Jiri Pirko191ed202019-06-04 15:40:40 +02002680static int devlink_nl_flash_update_fill(struct sk_buff *msg,
2681 struct devlink *devlink,
2682 enum devlink_command cmd,
2683 const char *status_msg,
2684 const char *component,
2685 unsigned long done, unsigned long total)
2686{
2687 void *hdr;
2688
2689 hdr = genlmsg_put(msg, 0, 0, &devlink_nl_family, 0, cmd);
2690 if (!hdr)
2691 return -EMSGSIZE;
2692
2693 if (devlink_nl_put_handle(msg, devlink))
2694 goto nla_put_failure;
2695
2696 if (cmd != DEVLINK_CMD_FLASH_UPDATE_STATUS)
2697 goto out;
2698
2699 if (status_msg &&
2700 nla_put_string(msg, DEVLINK_ATTR_FLASH_UPDATE_STATUS_MSG,
2701 status_msg))
2702 goto nla_put_failure;
2703 if (component &&
2704 nla_put_string(msg, DEVLINK_ATTR_FLASH_UPDATE_COMPONENT,
2705 component))
2706 goto nla_put_failure;
2707 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_FLASH_UPDATE_STATUS_DONE,
2708 done, DEVLINK_ATTR_PAD))
2709 goto nla_put_failure;
2710 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_FLASH_UPDATE_STATUS_TOTAL,
2711 total, DEVLINK_ATTR_PAD))
2712 goto nla_put_failure;
2713
2714out:
2715 genlmsg_end(msg, hdr);
2716 return 0;
2717
2718nla_put_failure:
2719 genlmsg_cancel(msg, hdr);
2720 return -EMSGSIZE;
2721}
2722
2723static void __devlink_flash_update_notify(struct devlink *devlink,
2724 enum devlink_command cmd,
2725 const char *status_msg,
2726 const char *component,
2727 unsigned long done,
2728 unsigned long total)
2729{
2730 struct sk_buff *msg;
2731 int err;
2732
2733 WARN_ON(cmd != DEVLINK_CMD_FLASH_UPDATE &&
2734 cmd != DEVLINK_CMD_FLASH_UPDATE_END &&
2735 cmd != DEVLINK_CMD_FLASH_UPDATE_STATUS);
2736
2737 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2738 if (!msg)
2739 return;
2740
2741 err = devlink_nl_flash_update_fill(msg, devlink, cmd, status_msg,
2742 component, done, total);
2743 if (err)
2744 goto out_free_msg;
2745
2746 genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
2747 msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
2748 return;
2749
2750out_free_msg:
2751 nlmsg_free(msg);
2752}
2753
2754void devlink_flash_update_begin_notify(struct devlink *devlink)
2755{
2756 __devlink_flash_update_notify(devlink,
2757 DEVLINK_CMD_FLASH_UPDATE,
2758 NULL, NULL, 0, 0);
2759}
2760EXPORT_SYMBOL_GPL(devlink_flash_update_begin_notify);
2761
2762void devlink_flash_update_end_notify(struct devlink *devlink)
2763{
2764 __devlink_flash_update_notify(devlink,
2765 DEVLINK_CMD_FLASH_UPDATE_END,
2766 NULL, NULL, 0, 0);
2767}
2768EXPORT_SYMBOL_GPL(devlink_flash_update_end_notify);
2769
2770void devlink_flash_update_status_notify(struct devlink *devlink,
2771 const char *status_msg,
2772 const char *component,
2773 unsigned long done,
2774 unsigned long total)
2775{
2776 __devlink_flash_update_notify(devlink,
2777 DEVLINK_CMD_FLASH_UPDATE_STATUS,
2778 status_msg, component, done, total);
2779}
2780EXPORT_SYMBOL_GPL(devlink_flash_update_status_notify);
2781
Jakub Kicinski76726cc2019-02-14 13:40:44 -08002782static int devlink_nl_cmd_flash_update(struct sk_buff *skb,
2783 struct genl_info *info)
2784{
2785 struct devlink *devlink = info->user_ptr[0];
2786 const char *file_name, *component;
2787 struct nlattr *nla_component;
2788
2789 if (!devlink->ops->flash_update)
2790 return -EOPNOTSUPP;
2791
2792 if (!info->attrs[DEVLINK_ATTR_FLASH_UPDATE_FILE_NAME])
2793 return -EINVAL;
2794 file_name = nla_data(info->attrs[DEVLINK_ATTR_FLASH_UPDATE_FILE_NAME]);
2795
2796 nla_component = info->attrs[DEVLINK_ATTR_FLASH_UPDATE_COMPONENT];
2797 component = nla_component ? nla_data(nla_component) : NULL;
2798
2799 return devlink->ops->flash_update(devlink, file_name, component,
2800 info->extack);
2801}
2802
Moshe Shemesh036467c2018-07-04 14:30:33 +03002803static const struct devlink_param devlink_param_generic[] = {
2804 {
2805 .id = DEVLINK_PARAM_GENERIC_ID_INT_ERR_RESET,
2806 .name = DEVLINK_PARAM_GENERIC_INT_ERR_RESET_NAME,
2807 .type = DEVLINK_PARAM_GENERIC_INT_ERR_RESET_TYPE,
2808 },
2809 {
2810 .id = DEVLINK_PARAM_GENERIC_ID_MAX_MACS,
2811 .name = DEVLINK_PARAM_GENERIC_MAX_MACS_NAME,
2812 .type = DEVLINK_PARAM_GENERIC_MAX_MACS_TYPE,
2813 },
Vasundhara Volamf567bcd2018-07-04 14:30:36 +03002814 {
2815 .id = DEVLINK_PARAM_GENERIC_ID_ENABLE_SRIOV,
2816 .name = DEVLINK_PARAM_GENERIC_ENABLE_SRIOV_NAME,
2817 .type = DEVLINK_PARAM_GENERIC_ENABLE_SRIOV_TYPE,
2818 },
Alex Veskerf6a698852018-07-12 15:13:17 +03002819 {
2820 .id = DEVLINK_PARAM_GENERIC_ID_REGION_SNAPSHOT,
2821 .name = DEVLINK_PARAM_GENERIC_REGION_SNAPSHOT_NAME,
2822 .type = DEVLINK_PARAM_GENERIC_REGION_SNAPSHOT_TYPE,
2823 },
Vasundhara Volame3b51062018-10-04 11:13:44 +05302824 {
2825 .id = DEVLINK_PARAM_GENERIC_ID_IGNORE_ARI,
2826 .name = DEVLINK_PARAM_GENERIC_IGNORE_ARI_NAME,
2827 .type = DEVLINK_PARAM_GENERIC_IGNORE_ARI_TYPE,
2828 },
Vasundhara Volamf61cba42018-10-04 11:13:45 +05302829 {
2830 .id = DEVLINK_PARAM_GENERIC_ID_MSIX_VEC_PER_PF_MAX,
2831 .name = DEVLINK_PARAM_GENERIC_MSIX_VEC_PER_PF_MAX_NAME,
2832 .type = DEVLINK_PARAM_GENERIC_MSIX_VEC_PER_PF_MAX_TYPE,
2833 },
Vasundhara Volam16511782018-10-04 11:13:46 +05302834 {
2835 .id = DEVLINK_PARAM_GENERIC_ID_MSIX_VEC_PER_PF_MIN,
2836 .name = DEVLINK_PARAM_GENERIC_MSIX_VEC_PER_PF_MIN_NAME,
2837 .type = DEVLINK_PARAM_GENERIC_MSIX_VEC_PER_PF_MIN_TYPE,
2838 },
Shalom Toledo846e9802018-12-03 07:58:59 +00002839 {
2840 .id = DEVLINK_PARAM_GENERIC_ID_FW_LOAD_POLICY,
2841 .name = DEVLINK_PARAM_GENERIC_FW_LOAD_POLICY_NAME,
2842 .type = DEVLINK_PARAM_GENERIC_FW_LOAD_POLICY_TYPE,
2843 },
Moshe Shemesh036467c2018-07-04 14:30:33 +03002844};
Moshe Shemesheabaef12018-07-04 14:30:28 +03002845
2846static int devlink_param_generic_verify(const struct devlink_param *param)
2847{
2848 /* verify it match generic parameter by id and name */
2849 if (param->id > DEVLINK_PARAM_GENERIC_ID_MAX)
2850 return -EINVAL;
2851 if (strcmp(param->name, devlink_param_generic[param->id].name))
2852 return -ENOENT;
2853
2854 WARN_ON(param->type != devlink_param_generic[param->id].type);
2855
2856 return 0;
2857}
2858
2859static int devlink_param_driver_verify(const struct devlink_param *param)
2860{
2861 int i;
2862
2863 if (param->id <= DEVLINK_PARAM_GENERIC_ID_MAX)
2864 return -EINVAL;
2865 /* verify no such name in generic params */
2866 for (i = 0; i <= DEVLINK_PARAM_GENERIC_ID_MAX; i++)
2867 if (!strcmp(param->name, devlink_param_generic[i].name))
2868 return -EEXIST;
2869
2870 return 0;
2871}
2872
2873static struct devlink_param_item *
2874devlink_param_find_by_name(struct list_head *param_list,
2875 const char *param_name)
2876{
2877 struct devlink_param_item *param_item;
2878
2879 list_for_each_entry(param_item, param_list, list)
2880 if (!strcmp(param_item->param->name, param_name))
2881 return param_item;
2882 return NULL;
2883}
2884
Moshe Shemeshec01aeb2018-07-04 14:30:31 +03002885static struct devlink_param_item *
2886devlink_param_find_by_id(struct list_head *param_list, u32 param_id)
2887{
2888 struct devlink_param_item *param_item;
2889
2890 list_for_each_entry(param_item, param_list, list)
2891 if (param_item->param->id == param_id)
2892 return param_item;
2893 return NULL;
2894}
2895
Moshe Shemesh45f05de2018-07-04 14:30:29 +03002896static bool
2897devlink_param_cmode_is_supported(const struct devlink_param *param,
2898 enum devlink_param_cmode cmode)
2899{
2900 return test_bit(cmode, &param->supported_cmodes);
2901}
2902
2903static int devlink_param_get(struct devlink *devlink,
2904 const struct devlink_param *param,
2905 struct devlink_param_gset_ctx *ctx)
2906{
2907 if (!param->get)
2908 return -EOPNOTSUPP;
2909 return param->get(devlink, param->id, ctx);
2910}
2911
Moshe Shemeshe3b7ca12018-07-04 14:30:30 +03002912static int devlink_param_set(struct devlink *devlink,
2913 const struct devlink_param *param,
2914 struct devlink_param_gset_ctx *ctx)
2915{
2916 if (!param->set)
2917 return -EOPNOTSUPP;
2918 return param->set(devlink, param->id, ctx);
2919}
2920
Moshe Shemesh45f05de2018-07-04 14:30:29 +03002921static int
2922devlink_param_type_to_nla_type(enum devlink_param_type param_type)
2923{
2924 switch (param_type) {
2925 case DEVLINK_PARAM_TYPE_U8:
2926 return NLA_U8;
2927 case DEVLINK_PARAM_TYPE_U16:
2928 return NLA_U16;
2929 case DEVLINK_PARAM_TYPE_U32:
2930 return NLA_U32;
2931 case DEVLINK_PARAM_TYPE_STRING:
2932 return NLA_STRING;
2933 case DEVLINK_PARAM_TYPE_BOOL:
2934 return NLA_FLAG;
2935 default:
2936 return -EINVAL;
2937 }
2938}
2939
2940static int
2941devlink_nl_param_value_fill_one(struct sk_buff *msg,
2942 enum devlink_param_type type,
2943 enum devlink_param_cmode cmode,
2944 union devlink_param_value val)
2945{
2946 struct nlattr *param_value_attr;
2947
Michal Kubecekae0be8d2019-04-26 11:13:06 +02002948 param_value_attr = nla_nest_start_noflag(msg,
2949 DEVLINK_ATTR_PARAM_VALUE);
Moshe Shemesh45f05de2018-07-04 14:30:29 +03002950 if (!param_value_attr)
2951 goto nla_put_failure;
2952
2953 if (nla_put_u8(msg, DEVLINK_ATTR_PARAM_VALUE_CMODE, cmode))
2954 goto value_nest_cancel;
2955
2956 switch (type) {
2957 case DEVLINK_PARAM_TYPE_U8:
2958 if (nla_put_u8(msg, DEVLINK_ATTR_PARAM_VALUE_DATA, val.vu8))
2959 goto value_nest_cancel;
2960 break;
2961 case DEVLINK_PARAM_TYPE_U16:
2962 if (nla_put_u16(msg, DEVLINK_ATTR_PARAM_VALUE_DATA, val.vu16))
2963 goto value_nest_cancel;
2964 break;
2965 case DEVLINK_PARAM_TYPE_U32:
2966 if (nla_put_u32(msg, DEVLINK_ATTR_PARAM_VALUE_DATA, val.vu32))
2967 goto value_nest_cancel;
2968 break;
2969 case DEVLINK_PARAM_TYPE_STRING:
2970 if (nla_put_string(msg, DEVLINK_ATTR_PARAM_VALUE_DATA,
2971 val.vstr))
2972 goto value_nest_cancel;
2973 break;
2974 case DEVLINK_PARAM_TYPE_BOOL:
2975 if (val.vbool &&
2976 nla_put_flag(msg, DEVLINK_ATTR_PARAM_VALUE_DATA))
2977 goto value_nest_cancel;
2978 break;
2979 }
2980
2981 nla_nest_end(msg, param_value_attr);
2982 return 0;
2983
2984value_nest_cancel:
2985 nla_nest_cancel(msg, param_value_attr);
2986nla_put_failure:
2987 return -EMSGSIZE;
2988}
2989
2990static int devlink_nl_param_fill(struct sk_buff *msg, struct devlink *devlink,
Vasundhara Volamf4601de2019-01-28 18:00:21 +05302991 unsigned int port_index,
Moshe Shemesh45f05de2018-07-04 14:30:29 +03002992 struct devlink_param_item *param_item,
2993 enum devlink_command cmd,
2994 u32 portid, u32 seq, int flags)
2995{
2996 union devlink_param_value param_value[DEVLINK_PARAM_CMODE_MAX + 1];
Jiri Pirko7c62cfb2019-02-07 11:22:45 +00002997 bool param_value_set[DEVLINK_PARAM_CMODE_MAX + 1] = {};
Moshe Shemesh45f05de2018-07-04 14:30:29 +03002998 const struct devlink_param *param = param_item->param;
2999 struct devlink_param_gset_ctx ctx;
3000 struct nlattr *param_values_list;
3001 struct nlattr *param_attr;
3002 int nla_type;
3003 void *hdr;
3004 int err;
3005 int i;
3006
3007 /* Get value from driver part to driverinit configuration mode */
3008 for (i = 0; i <= DEVLINK_PARAM_CMODE_MAX; i++) {
3009 if (!devlink_param_cmode_is_supported(param, i))
3010 continue;
3011 if (i == DEVLINK_PARAM_CMODE_DRIVERINIT) {
3012 if (!param_item->driverinit_value_valid)
3013 return -EOPNOTSUPP;
3014 param_value[i] = param_item->driverinit_value;
3015 } else {
Jiri Pirko7c62cfb2019-02-07 11:22:45 +00003016 if (!param_item->published)
3017 continue;
Moshe Shemesh45f05de2018-07-04 14:30:29 +03003018 ctx.cmode = i;
3019 err = devlink_param_get(devlink, param, &ctx);
3020 if (err)
3021 return err;
3022 param_value[i] = ctx.val;
3023 }
Jiri Pirko7c62cfb2019-02-07 11:22:45 +00003024 param_value_set[i] = true;
Moshe Shemesh45f05de2018-07-04 14:30:29 +03003025 }
3026
3027 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
3028 if (!hdr)
3029 return -EMSGSIZE;
3030
3031 if (devlink_nl_put_handle(msg, devlink))
3032 goto genlmsg_cancel;
Vasundhara Volamf4601de2019-01-28 18:00:21 +05303033
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05303034 if (cmd == DEVLINK_CMD_PORT_PARAM_GET ||
3035 cmd == DEVLINK_CMD_PORT_PARAM_NEW ||
3036 cmd == DEVLINK_CMD_PORT_PARAM_DEL)
Vasundhara Volamf4601de2019-01-28 18:00:21 +05303037 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, port_index))
3038 goto genlmsg_cancel;
3039
Michal Kubecekae0be8d2019-04-26 11:13:06 +02003040 param_attr = nla_nest_start_noflag(msg, DEVLINK_ATTR_PARAM);
Moshe Shemesh45f05de2018-07-04 14:30:29 +03003041 if (!param_attr)
3042 goto genlmsg_cancel;
3043 if (nla_put_string(msg, DEVLINK_ATTR_PARAM_NAME, param->name))
3044 goto param_nest_cancel;
3045 if (param->generic && nla_put_flag(msg, DEVLINK_ATTR_PARAM_GENERIC))
3046 goto param_nest_cancel;
3047
3048 nla_type = devlink_param_type_to_nla_type(param->type);
3049 if (nla_type < 0)
3050 goto param_nest_cancel;
3051 if (nla_put_u8(msg, DEVLINK_ATTR_PARAM_TYPE, nla_type))
3052 goto param_nest_cancel;
3053
Michal Kubecekae0be8d2019-04-26 11:13:06 +02003054 param_values_list = nla_nest_start_noflag(msg,
3055 DEVLINK_ATTR_PARAM_VALUES_LIST);
Moshe Shemesh45f05de2018-07-04 14:30:29 +03003056 if (!param_values_list)
3057 goto param_nest_cancel;
3058
3059 for (i = 0; i <= DEVLINK_PARAM_CMODE_MAX; i++) {
Jiri Pirko7c62cfb2019-02-07 11:22:45 +00003060 if (!param_value_set[i])
Moshe Shemesh45f05de2018-07-04 14:30:29 +03003061 continue;
3062 err = devlink_nl_param_value_fill_one(msg, param->type,
3063 i, param_value[i]);
3064 if (err)
3065 goto values_list_nest_cancel;
3066 }
3067
3068 nla_nest_end(msg, param_values_list);
3069 nla_nest_end(msg, param_attr);
3070 genlmsg_end(msg, hdr);
3071 return 0;
3072
3073values_list_nest_cancel:
3074 nla_nest_end(msg, param_values_list);
3075param_nest_cancel:
3076 nla_nest_cancel(msg, param_attr);
3077genlmsg_cancel:
3078 genlmsg_cancel(msg, hdr);
3079 return -EMSGSIZE;
3080}
3081
Moshe Shemeshea601e12018-07-04 14:30:32 +03003082static void devlink_param_notify(struct devlink *devlink,
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05303083 unsigned int port_index,
Moshe Shemeshea601e12018-07-04 14:30:32 +03003084 struct devlink_param_item *param_item,
3085 enum devlink_command cmd)
3086{
3087 struct sk_buff *msg;
3088 int err;
3089
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05303090 WARN_ON(cmd != DEVLINK_CMD_PARAM_NEW && cmd != DEVLINK_CMD_PARAM_DEL &&
3091 cmd != DEVLINK_CMD_PORT_PARAM_NEW &&
3092 cmd != DEVLINK_CMD_PORT_PARAM_DEL);
Moshe Shemeshea601e12018-07-04 14:30:32 +03003093
3094 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
3095 if (!msg)
3096 return;
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05303097 err = devlink_nl_param_fill(msg, devlink, port_index, param_item, cmd,
3098 0, 0, 0);
Moshe Shemeshea601e12018-07-04 14:30:32 +03003099 if (err) {
3100 nlmsg_free(msg);
3101 return;
3102 }
3103
3104 genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
3105 msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
3106}
3107
Moshe Shemesh45f05de2018-07-04 14:30:29 +03003108static int devlink_nl_cmd_param_get_dumpit(struct sk_buff *msg,
3109 struct netlink_callback *cb)
3110{
3111 struct devlink_param_item *param_item;
3112 struct devlink *devlink;
3113 int start = cb->args[0];
3114 int idx = 0;
3115 int err;
3116
3117 mutex_lock(&devlink_mutex);
3118 list_for_each_entry(devlink, &devlink_list, list) {
3119 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
3120 continue;
3121 mutex_lock(&devlink->lock);
3122 list_for_each_entry(param_item, &devlink->param_list, list) {
3123 if (idx < start) {
3124 idx++;
3125 continue;
3126 }
Vasundhara Volamf4601de2019-01-28 18:00:21 +05303127 err = devlink_nl_param_fill(msg, devlink, 0, param_item,
Moshe Shemesh45f05de2018-07-04 14:30:29 +03003128 DEVLINK_CMD_PARAM_GET,
3129 NETLINK_CB(cb->skb).portid,
3130 cb->nlh->nlmsg_seq,
3131 NLM_F_MULTI);
3132 if (err) {
3133 mutex_unlock(&devlink->lock);
3134 goto out;
3135 }
3136 idx++;
3137 }
3138 mutex_unlock(&devlink->lock);
3139 }
3140out:
3141 mutex_unlock(&devlink_mutex);
3142
3143 cb->args[0] = idx;
3144 return msg->len;
3145}
3146
Moshe Shemeshe3b7ca12018-07-04 14:30:30 +03003147static int
3148devlink_param_type_get_from_info(struct genl_info *info,
3149 enum devlink_param_type *param_type)
3150{
3151 if (!info->attrs[DEVLINK_ATTR_PARAM_TYPE])
3152 return -EINVAL;
3153
3154 switch (nla_get_u8(info->attrs[DEVLINK_ATTR_PARAM_TYPE])) {
3155 case NLA_U8:
3156 *param_type = DEVLINK_PARAM_TYPE_U8;
3157 break;
3158 case NLA_U16:
3159 *param_type = DEVLINK_PARAM_TYPE_U16;
3160 break;
3161 case NLA_U32:
3162 *param_type = DEVLINK_PARAM_TYPE_U32;
3163 break;
3164 case NLA_STRING:
3165 *param_type = DEVLINK_PARAM_TYPE_STRING;
3166 break;
3167 case NLA_FLAG:
3168 *param_type = DEVLINK_PARAM_TYPE_BOOL;
3169 break;
3170 default:
3171 return -EINVAL;
3172 }
3173
3174 return 0;
3175}
3176
3177static int
3178devlink_param_value_get_from_info(const struct devlink_param *param,
3179 struct genl_info *info,
3180 union devlink_param_value *value)
3181{
Moshe Shemeshf355cfc2018-10-10 16:09:25 +03003182 int len;
3183
Moshe Shemeshe3b7ca12018-07-04 14:30:30 +03003184 if (param->type != DEVLINK_PARAM_TYPE_BOOL &&
3185 !info->attrs[DEVLINK_ATTR_PARAM_VALUE_DATA])
3186 return -EINVAL;
3187
3188 switch (param->type) {
3189 case DEVLINK_PARAM_TYPE_U8:
3190 value->vu8 = nla_get_u8(info->attrs[DEVLINK_ATTR_PARAM_VALUE_DATA]);
3191 break;
3192 case DEVLINK_PARAM_TYPE_U16:
3193 value->vu16 = nla_get_u16(info->attrs[DEVLINK_ATTR_PARAM_VALUE_DATA]);
3194 break;
3195 case DEVLINK_PARAM_TYPE_U32:
3196 value->vu32 = nla_get_u32(info->attrs[DEVLINK_ATTR_PARAM_VALUE_DATA]);
3197 break;
3198 case DEVLINK_PARAM_TYPE_STRING:
Moshe Shemeshf355cfc2018-10-10 16:09:25 +03003199 len = strnlen(nla_data(info->attrs[DEVLINK_ATTR_PARAM_VALUE_DATA]),
3200 nla_len(info->attrs[DEVLINK_ATTR_PARAM_VALUE_DATA]));
3201 if (len == nla_len(info->attrs[DEVLINK_ATTR_PARAM_VALUE_DATA]) ||
Moshe Shemeshbde74ad12018-10-10 16:09:27 +03003202 len >= __DEVLINK_PARAM_MAX_STRING_VALUE)
Moshe Shemeshe3b7ca12018-07-04 14:30:30 +03003203 return -EINVAL;
Moshe Shemeshf355cfc2018-10-10 16:09:25 +03003204 strcpy(value->vstr,
3205 nla_data(info->attrs[DEVLINK_ATTR_PARAM_VALUE_DATA]));
Moshe Shemeshe3b7ca12018-07-04 14:30:30 +03003206 break;
3207 case DEVLINK_PARAM_TYPE_BOOL:
3208 value->vbool = info->attrs[DEVLINK_ATTR_PARAM_VALUE_DATA] ?
3209 true : false;
3210 break;
3211 }
3212 return 0;
3213}
3214
Moshe Shemesh45f05de2018-07-04 14:30:29 +03003215static struct devlink_param_item *
Vasundhara Volamf4601de2019-01-28 18:00:21 +05303216devlink_param_get_from_info(struct list_head *param_list,
Moshe Shemesh45f05de2018-07-04 14:30:29 +03003217 struct genl_info *info)
3218{
3219 char *param_name;
3220
3221 if (!info->attrs[DEVLINK_ATTR_PARAM_NAME])
3222 return NULL;
3223
3224 param_name = nla_data(info->attrs[DEVLINK_ATTR_PARAM_NAME]);
Vasundhara Volamf4601de2019-01-28 18:00:21 +05303225 return devlink_param_find_by_name(param_list, param_name);
Moshe Shemesh45f05de2018-07-04 14:30:29 +03003226}
3227
3228static int devlink_nl_cmd_param_get_doit(struct sk_buff *skb,
3229 struct genl_info *info)
3230{
3231 struct devlink *devlink = info->user_ptr[0];
3232 struct devlink_param_item *param_item;
3233 struct sk_buff *msg;
3234 int err;
3235
Vasundhara Volamf4601de2019-01-28 18:00:21 +05303236 param_item = devlink_param_get_from_info(&devlink->param_list, info);
Moshe Shemesh45f05de2018-07-04 14:30:29 +03003237 if (!param_item)
3238 return -EINVAL;
3239
3240 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
3241 if (!msg)
3242 return -ENOMEM;
3243
Vasundhara Volamf4601de2019-01-28 18:00:21 +05303244 err = devlink_nl_param_fill(msg, devlink, 0, param_item,
Moshe Shemesh45f05de2018-07-04 14:30:29 +03003245 DEVLINK_CMD_PARAM_GET,
3246 info->snd_portid, info->snd_seq, 0);
3247 if (err) {
3248 nlmsg_free(msg);
3249 return err;
3250 }
3251
3252 return genlmsg_reply(msg, info);
3253}
3254
Vasundhara Volam9c548732019-01-28 18:00:22 +05303255static int __devlink_nl_cmd_param_set_doit(struct devlink *devlink,
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05303256 unsigned int port_index,
Vasundhara Volam9c548732019-01-28 18:00:22 +05303257 struct list_head *param_list,
3258 struct genl_info *info,
3259 enum devlink_command cmd)
Moshe Shemeshe3b7ca12018-07-04 14:30:30 +03003260{
Moshe Shemeshe3b7ca12018-07-04 14:30:30 +03003261 enum devlink_param_type param_type;
3262 struct devlink_param_gset_ctx ctx;
3263 enum devlink_param_cmode cmode;
3264 struct devlink_param_item *param_item;
3265 const struct devlink_param *param;
3266 union devlink_param_value value;
3267 int err = 0;
3268
Vasundhara Volam9c548732019-01-28 18:00:22 +05303269 param_item = devlink_param_get_from_info(param_list, info);
Moshe Shemeshe3b7ca12018-07-04 14:30:30 +03003270 if (!param_item)
3271 return -EINVAL;
3272 param = param_item->param;
3273 err = devlink_param_type_get_from_info(info, &param_type);
3274 if (err)
3275 return err;
3276 if (param_type != param->type)
3277 return -EINVAL;
3278 err = devlink_param_value_get_from_info(param, info, &value);
3279 if (err)
3280 return err;
3281 if (param->validate) {
3282 err = param->validate(devlink, param->id, value, info->extack);
3283 if (err)
3284 return err;
3285 }
3286
3287 if (!info->attrs[DEVLINK_ATTR_PARAM_VALUE_CMODE])
3288 return -EINVAL;
3289 cmode = nla_get_u8(info->attrs[DEVLINK_ATTR_PARAM_VALUE_CMODE]);
3290 if (!devlink_param_cmode_is_supported(param, cmode))
3291 return -EOPNOTSUPP;
3292
3293 if (cmode == DEVLINK_PARAM_CMODE_DRIVERINIT) {
Moshe Shemesh12765342018-10-10 16:09:26 +03003294 if (param->type == DEVLINK_PARAM_TYPE_STRING)
3295 strcpy(param_item->driverinit_value.vstr, value.vstr);
3296 else
3297 param_item->driverinit_value = value;
Moshe Shemeshe3b7ca12018-07-04 14:30:30 +03003298 param_item->driverinit_value_valid = true;
3299 } else {
3300 if (!param->set)
3301 return -EOPNOTSUPP;
3302 ctx.val = value;
3303 ctx.cmode = cmode;
3304 err = devlink_param_set(devlink, param, &ctx);
3305 if (err)
3306 return err;
3307 }
3308
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05303309 devlink_param_notify(devlink, port_index, param_item, cmd);
Moshe Shemeshe3b7ca12018-07-04 14:30:30 +03003310 return 0;
3311}
3312
Vasundhara Volam9c548732019-01-28 18:00:22 +05303313static int devlink_nl_cmd_param_set_doit(struct sk_buff *skb,
3314 struct genl_info *info)
3315{
3316 struct devlink *devlink = info->user_ptr[0];
3317
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05303318 return __devlink_nl_cmd_param_set_doit(devlink, 0, &devlink->param_list,
Vasundhara Volam9c548732019-01-28 18:00:22 +05303319 info, DEVLINK_CMD_PARAM_NEW);
3320}
3321
Moshe Shemesheabaef12018-07-04 14:30:28 +03003322static int devlink_param_register_one(struct devlink *devlink,
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05303323 unsigned int port_index,
Vasundhara Volam39e61602019-01-28 18:00:20 +05303324 struct list_head *param_list,
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05303325 const struct devlink_param *param,
3326 enum devlink_command cmd)
Moshe Shemesheabaef12018-07-04 14:30:28 +03003327{
3328 struct devlink_param_item *param_item;
3329
Vasundhara Volam39e61602019-01-28 18:00:20 +05303330 if (devlink_param_find_by_name(param_list, param->name))
Moshe Shemesheabaef12018-07-04 14:30:28 +03003331 return -EEXIST;
3332
3333 if (param->supported_cmodes == BIT(DEVLINK_PARAM_CMODE_DRIVERINIT))
3334 WARN_ON(param->get || param->set);
3335 else
3336 WARN_ON(!param->get || !param->set);
3337
3338 param_item = kzalloc(sizeof(*param_item), GFP_KERNEL);
3339 if (!param_item)
3340 return -ENOMEM;
3341 param_item->param = param;
3342
Vasundhara Volam39e61602019-01-28 18:00:20 +05303343 list_add_tail(&param_item->list, param_list);
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05303344 devlink_param_notify(devlink, port_index, param_item, cmd);
Moshe Shemesheabaef12018-07-04 14:30:28 +03003345 return 0;
3346}
3347
3348static void devlink_param_unregister_one(struct devlink *devlink,
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05303349 unsigned int port_index,
Vasundhara Volam39e61602019-01-28 18:00:20 +05303350 struct list_head *param_list,
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05303351 const struct devlink_param *param,
3352 enum devlink_command cmd)
Moshe Shemesheabaef12018-07-04 14:30:28 +03003353{
3354 struct devlink_param_item *param_item;
3355
Vasundhara Volam39e61602019-01-28 18:00:20 +05303356 param_item = devlink_param_find_by_name(param_list, param->name);
Moshe Shemesheabaef12018-07-04 14:30:28 +03003357 WARN_ON(!param_item);
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05303358 devlink_param_notify(devlink, port_index, param_item, cmd);
Moshe Shemesheabaef12018-07-04 14:30:28 +03003359 list_del(&param_item->list);
3360 kfree(param_item);
3361}
3362
Vasundhara Volamf4601de2019-01-28 18:00:21 +05303363static int devlink_nl_cmd_port_param_get_dumpit(struct sk_buff *msg,
3364 struct netlink_callback *cb)
3365{
3366 struct devlink_param_item *param_item;
3367 struct devlink_port *devlink_port;
3368 struct devlink *devlink;
3369 int start = cb->args[0];
3370 int idx = 0;
3371 int err;
3372
3373 mutex_lock(&devlink_mutex);
3374 list_for_each_entry(devlink, &devlink_list, list) {
3375 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
3376 continue;
3377 mutex_lock(&devlink->lock);
3378 list_for_each_entry(devlink_port, &devlink->port_list, list) {
3379 list_for_each_entry(param_item,
3380 &devlink_port->param_list, list) {
3381 if (idx < start) {
3382 idx++;
3383 continue;
3384 }
3385 err = devlink_nl_param_fill(msg,
3386 devlink_port->devlink,
3387 devlink_port->index, param_item,
3388 DEVLINK_CMD_PORT_PARAM_GET,
3389 NETLINK_CB(cb->skb).portid,
3390 cb->nlh->nlmsg_seq,
3391 NLM_F_MULTI);
3392 if (err) {
3393 mutex_unlock(&devlink->lock);
3394 goto out;
3395 }
3396 idx++;
3397 }
3398 }
3399 mutex_unlock(&devlink->lock);
3400 }
3401out:
3402 mutex_unlock(&devlink_mutex);
3403
3404 cb->args[0] = idx;
3405 return msg->len;
3406}
3407
3408static int devlink_nl_cmd_port_param_get_doit(struct sk_buff *skb,
3409 struct genl_info *info)
3410{
3411 struct devlink_port *devlink_port = info->user_ptr[0];
3412 struct devlink_param_item *param_item;
3413 struct sk_buff *msg;
3414 int err;
3415
3416 param_item = devlink_param_get_from_info(&devlink_port->param_list,
3417 info);
3418 if (!param_item)
3419 return -EINVAL;
3420
3421 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
3422 if (!msg)
3423 return -ENOMEM;
3424
3425 err = devlink_nl_param_fill(msg, devlink_port->devlink,
3426 devlink_port->index, param_item,
3427 DEVLINK_CMD_PORT_PARAM_GET,
3428 info->snd_portid, info->snd_seq, 0);
3429 if (err) {
3430 nlmsg_free(msg);
3431 return err;
3432 }
3433
3434 return genlmsg_reply(msg, info);
3435}
3436
Vasundhara Volam9c548732019-01-28 18:00:22 +05303437static int devlink_nl_cmd_port_param_set_doit(struct sk_buff *skb,
3438 struct genl_info *info)
3439{
3440 struct devlink_port *devlink_port = info->user_ptr[0];
3441
3442 return __devlink_nl_cmd_param_set_doit(devlink_port->devlink,
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05303443 devlink_port->index,
3444 &devlink_port->param_list, info,
3445 DEVLINK_CMD_PORT_PARAM_NEW);
Vasundhara Volam9c548732019-01-28 18:00:22 +05303446}
3447
Alex Veskera006d462018-07-12 15:13:12 +03003448static int devlink_nl_region_snapshot_id_put(struct sk_buff *msg,
3449 struct devlink *devlink,
3450 struct devlink_snapshot *snapshot)
3451{
3452 struct nlattr *snap_attr;
3453 int err;
3454
Michal Kubecekae0be8d2019-04-26 11:13:06 +02003455 snap_attr = nla_nest_start_noflag(msg, DEVLINK_ATTR_REGION_SNAPSHOT);
Alex Veskera006d462018-07-12 15:13:12 +03003456 if (!snap_attr)
3457 return -EINVAL;
3458
3459 err = nla_put_u32(msg, DEVLINK_ATTR_REGION_SNAPSHOT_ID, snapshot->id);
3460 if (err)
3461 goto nla_put_failure;
3462
3463 nla_nest_end(msg, snap_attr);
3464 return 0;
3465
3466nla_put_failure:
3467 nla_nest_cancel(msg, snap_attr);
3468 return err;
3469}
3470
3471static int devlink_nl_region_snapshots_id_put(struct sk_buff *msg,
3472 struct devlink *devlink,
3473 struct devlink_region *region)
3474{
3475 struct devlink_snapshot *snapshot;
3476 struct nlattr *snapshots_attr;
3477 int err;
3478
Michal Kubecekae0be8d2019-04-26 11:13:06 +02003479 snapshots_attr = nla_nest_start_noflag(msg,
3480 DEVLINK_ATTR_REGION_SNAPSHOTS);
Alex Veskera006d462018-07-12 15:13:12 +03003481 if (!snapshots_attr)
3482 return -EINVAL;
3483
3484 list_for_each_entry(snapshot, &region->snapshot_list, list) {
3485 err = devlink_nl_region_snapshot_id_put(msg, devlink, snapshot);
3486 if (err)
3487 goto nla_put_failure;
3488 }
3489
3490 nla_nest_end(msg, snapshots_attr);
3491 return 0;
3492
3493nla_put_failure:
3494 nla_nest_cancel(msg, snapshots_attr);
3495 return err;
3496}
3497
Alex Veskerd8db7ea52018-07-12 15:13:11 +03003498static int devlink_nl_region_fill(struct sk_buff *msg, struct devlink *devlink,
3499 enum devlink_command cmd, u32 portid,
3500 u32 seq, int flags,
3501 struct devlink_region *region)
3502{
3503 void *hdr;
3504 int err;
3505
3506 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
3507 if (!hdr)
3508 return -EMSGSIZE;
3509
3510 err = devlink_nl_put_handle(msg, devlink);
3511 if (err)
3512 goto nla_put_failure;
3513
3514 err = nla_put_string(msg, DEVLINK_ATTR_REGION_NAME, region->name);
3515 if (err)
3516 goto nla_put_failure;
3517
3518 err = nla_put_u64_64bit(msg, DEVLINK_ATTR_REGION_SIZE,
3519 region->size,
3520 DEVLINK_ATTR_PAD);
3521 if (err)
3522 goto nla_put_failure;
3523
Alex Veskera006d462018-07-12 15:13:12 +03003524 err = devlink_nl_region_snapshots_id_put(msg, devlink, region);
3525 if (err)
3526 goto nla_put_failure;
3527
Alex Veskerd8db7ea52018-07-12 15:13:11 +03003528 genlmsg_end(msg, hdr);
3529 return 0;
3530
3531nla_put_failure:
3532 genlmsg_cancel(msg, hdr);
3533 return err;
3534}
3535
Alex Vesker866319b2018-07-12 15:13:13 +03003536static void devlink_nl_region_notify(struct devlink_region *region,
3537 struct devlink_snapshot *snapshot,
3538 enum devlink_command cmd)
3539{
3540 struct devlink *devlink = region->devlink;
3541 struct sk_buff *msg;
3542 void *hdr;
3543 int err;
3544
3545 WARN_ON(cmd != DEVLINK_CMD_REGION_NEW && cmd != DEVLINK_CMD_REGION_DEL);
3546
3547 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
3548 if (!msg)
3549 return;
3550
3551 hdr = genlmsg_put(msg, 0, 0, &devlink_nl_family, 0, cmd);
3552 if (!hdr)
3553 goto out_free_msg;
3554
3555 err = devlink_nl_put_handle(msg, devlink);
3556 if (err)
3557 goto out_cancel_msg;
3558
3559 err = nla_put_string(msg, DEVLINK_ATTR_REGION_NAME,
3560 region->name);
3561 if (err)
3562 goto out_cancel_msg;
3563
3564 if (snapshot) {
3565 err = nla_put_u32(msg, DEVLINK_ATTR_REGION_SNAPSHOT_ID,
3566 snapshot->id);
3567 if (err)
3568 goto out_cancel_msg;
3569 } else {
3570 err = nla_put_u64_64bit(msg, DEVLINK_ATTR_REGION_SIZE,
3571 region->size, DEVLINK_ATTR_PAD);
3572 if (err)
3573 goto out_cancel_msg;
3574 }
3575 genlmsg_end(msg, hdr);
3576
3577 genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
3578 msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
3579
3580 return;
3581
3582out_cancel_msg:
3583 genlmsg_cancel(msg, hdr);
3584out_free_msg:
3585 nlmsg_free(msg);
3586}
3587
Alex Veskerd8db7ea52018-07-12 15:13:11 +03003588static int devlink_nl_cmd_region_get_doit(struct sk_buff *skb,
3589 struct genl_info *info)
3590{
3591 struct devlink *devlink = info->user_ptr[0];
3592 struct devlink_region *region;
3593 const char *region_name;
3594 struct sk_buff *msg;
3595 int err;
3596
3597 if (!info->attrs[DEVLINK_ATTR_REGION_NAME])
3598 return -EINVAL;
3599
3600 region_name = nla_data(info->attrs[DEVLINK_ATTR_REGION_NAME]);
3601 region = devlink_region_get_by_name(devlink, region_name);
3602 if (!region)
3603 return -EINVAL;
3604
3605 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
3606 if (!msg)
3607 return -ENOMEM;
3608
3609 err = devlink_nl_region_fill(msg, devlink, DEVLINK_CMD_REGION_GET,
3610 info->snd_portid, info->snd_seq, 0,
3611 region);
3612 if (err) {
3613 nlmsg_free(msg);
3614 return err;
3615 }
3616
3617 return genlmsg_reply(msg, info);
3618}
3619
3620static int devlink_nl_cmd_region_get_dumpit(struct sk_buff *msg,
3621 struct netlink_callback *cb)
3622{
3623 struct devlink_region *region;
3624 struct devlink *devlink;
3625 int start = cb->args[0];
3626 int idx = 0;
3627 int err;
3628
3629 mutex_lock(&devlink_mutex);
3630 list_for_each_entry(devlink, &devlink_list, list) {
3631 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
3632 continue;
3633
3634 mutex_lock(&devlink->lock);
3635 list_for_each_entry(region, &devlink->region_list, list) {
3636 if (idx < start) {
3637 idx++;
3638 continue;
3639 }
3640 err = devlink_nl_region_fill(msg, devlink,
3641 DEVLINK_CMD_REGION_GET,
3642 NETLINK_CB(cb->skb).portid,
3643 cb->nlh->nlmsg_seq,
3644 NLM_F_MULTI, region);
3645 if (err) {
3646 mutex_unlock(&devlink->lock);
3647 goto out;
3648 }
3649 idx++;
3650 }
3651 mutex_unlock(&devlink->lock);
3652 }
3653out:
3654 mutex_unlock(&devlink_mutex);
3655 cb->args[0] = idx;
3656 return msg->len;
3657}
3658
Alex Vesker866319b2018-07-12 15:13:13 +03003659static int devlink_nl_cmd_region_del(struct sk_buff *skb,
3660 struct genl_info *info)
3661{
3662 struct devlink *devlink = info->user_ptr[0];
3663 struct devlink_snapshot *snapshot;
3664 struct devlink_region *region;
3665 const char *region_name;
3666 u32 snapshot_id;
3667
3668 if (!info->attrs[DEVLINK_ATTR_REGION_NAME] ||
3669 !info->attrs[DEVLINK_ATTR_REGION_SNAPSHOT_ID])
3670 return -EINVAL;
3671
3672 region_name = nla_data(info->attrs[DEVLINK_ATTR_REGION_NAME]);
3673 snapshot_id = nla_get_u32(info->attrs[DEVLINK_ATTR_REGION_SNAPSHOT_ID]);
3674
3675 region = devlink_region_get_by_name(devlink, region_name);
3676 if (!region)
3677 return -EINVAL;
3678
3679 snapshot = devlink_region_snapshot_get_by_id(region, snapshot_id);
3680 if (!snapshot)
3681 return -EINVAL;
3682
3683 devlink_nl_region_notify(region, snapshot, DEVLINK_CMD_REGION_DEL);
3684 devlink_region_snapshot_del(snapshot);
3685 return 0;
3686}
3687
Alex Vesker4e547952018-07-12 15:13:14 +03003688static int devlink_nl_cmd_region_read_chunk_fill(struct sk_buff *msg,
3689 struct devlink *devlink,
3690 u8 *chunk, u32 chunk_size,
3691 u64 addr)
3692{
3693 struct nlattr *chunk_attr;
3694 int err;
3695
Michal Kubecekae0be8d2019-04-26 11:13:06 +02003696 chunk_attr = nla_nest_start_noflag(msg, DEVLINK_ATTR_REGION_CHUNK);
Alex Vesker4e547952018-07-12 15:13:14 +03003697 if (!chunk_attr)
3698 return -EINVAL;
3699
3700 err = nla_put(msg, DEVLINK_ATTR_REGION_CHUNK_DATA, chunk_size, chunk);
3701 if (err)
3702 goto nla_put_failure;
3703
3704 err = nla_put_u64_64bit(msg, DEVLINK_ATTR_REGION_CHUNK_ADDR, addr,
3705 DEVLINK_ATTR_PAD);
3706 if (err)
3707 goto nla_put_failure;
3708
3709 nla_nest_end(msg, chunk_attr);
3710 return 0;
3711
3712nla_put_failure:
3713 nla_nest_cancel(msg, chunk_attr);
3714 return err;
3715}
3716
3717#define DEVLINK_REGION_READ_CHUNK_SIZE 256
3718
3719static int devlink_nl_region_read_snapshot_fill(struct sk_buff *skb,
3720 struct devlink *devlink,
3721 struct devlink_region *region,
3722 struct nlattr **attrs,
3723 u64 start_offset,
3724 u64 end_offset,
3725 bool dump,
3726 u64 *new_offset)
3727{
3728 struct devlink_snapshot *snapshot;
3729 u64 curr_offset = start_offset;
3730 u32 snapshot_id;
3731 int err = 0;
3732
3733 *new_offset = start_offset;
3734
3735 snapshot_id = nla_get_u32(attrs[DEVLINK_ATTR_REGION_SNAPSHOT_ID]);
3736 snapshot = devlink_region_snapshot_get_by_id(region, snapshot_id);
3737 if (!snapshot)
3738 return -EINVAL;
3739
3740 if (end_offset > snapshot->data_len || dump)
3741 end_offset = snapshot->data_len;
3742
3743 while (curr_offset < end_offset) {
3744 u32 data_size;
3745 u8 *data;
3746
3747 if (end_offset - curr_offset < DEVLINK_REGION_READ_CHUNK_SIZE)
3748 data_size = end_offset - curr_offset;
3749 else
3750 data_size = DEVLINK_REGION_READ_CHUNK_SIZE;
3751
3752 data = &snapshot->data[curr_offset];
3753 err = devlink_nl_cmd_region_read_chunk_fill(skb, devlink,
3754 data, data_size,
3755 curr_offset);
3756 if (err)
3757 break;
3758
3759 curr_offset += data_size;
3760 }
3761 *new_offset = curr_offset;
3762
3763 return err;
3764}
3765
3766static int devlink_nl_cmd_region_read_dumpit(struct sk_buff *skb,
3767 struct netlink_callback *cb)
3768{
3769 u64 ret_offset, start_offset, end_offset = 0;
Alex Vesker4e547952018-07-12 15:13:14 +03003770 struct devlink_region *region;
3771 struct nlattr *chunks_attr;
3772 const char *region_name;
3773 struct devlink *devlink;
Jakub Kicinski68750562019-02-10 19:35:28 -08003774 struct nlattr **attrs;
Alex Vesker4e547952018-07-12 15:13:14 +03003775 bool dump = true;
3776 void *hdr;
3777 int err;
3778
3779 start_offset = *((u64 *)&cb->args[0]);
3780
Jakub Kicinski68750562019-02-10 19:35:28 -08003781 attrs = kmalloc_array(DEVLINK_ATTR_MAX + 1, sizeof(*attrs), GFP_KERNEL);
3782 if (!attrs)
3783 return -ENOMEM;
3784
Johannes Berg8cb08172019-04-26 14:07:28 +02003785 err = nlmsg_parse_deprecated(cb->nlh,
3786 GENL_HDRLEN + devlink_nl_family.hdrsize,
3787 attrs, DEVLINK_ATTR_MAX,
3788 devlink_nl_family.policy, cb->extack);
Alex Vesker4e547952018-07-12 15:13:14 +03003789 if (err)
Jakub Kicinski68750562019-02-10 19:35:28 -08003790 goto out_free;
Alex Vesker4e547952018-07-12 15:13:14 +03003791
Parav Panditdac7c082019-02-12 14:24:08 -06003792 mutex_lock(&devlink_mutex);
Alex Vesker4e547952018-07-12 15:13:14 +03003793 devlink = devlink_get_from_attrs(sock_net(cb->skb->sk), attrs);
Parav Panditfdd41ec2019-02-12 14:23:58 -06003794 if (IS_ERR(devlink)) {
3795 err = PTR_ERR(devlink);
Parav Panditdac7c082019-02-12 14:24:08 -06003796 goto out_dev;
Parav Panditfdd41ec2019-02-12 14:23:58 -06003797 }
Alex Vesker4e547952018-07-12 15:13:14 +03003798
Alex Vesker4e547952018-07-12 15:13:14 +03003799 mutex_lock(&devlink->lock);
3800
3801 if (!attrs[DEVLINK_ATTR_REGION_NAME] ||
Parav Panditfdd41ec2019-02-12 14:23:58 -06003802 !attrs[DEVLINK_ATTR_REGION_SNAPSHOT_ID]) {
3803 err = -EINVAL;
Alex Vesker4e547952018-07-12 15:13:14 +03003804 goto out_unlock;
Parav Panditfdd41ec2019-02-12 14:23:58 -06003805 }
Alex Vesker4e547952018-07-12 15:13:14 +03003806
3807 region_name = nla_data(attrs[DEVLINK_ATTR_REGION_NAME]);
3808 region = devlink_region_get_by_name(devlink, region_name);
Parav Panditfdd41ec2019-02-12 14:23:58 -06003809 if (!region) {
3810 err = -EINVAL;
Alex Vesker4e547952018-07-12 15:13:14 +03003811 goto out_unlock;
Parav Panditfdd41ec2019-02-12 14:23:58 -06003812 }
Alex Vesker4e547952018-07-12 15:13:14 +03003813
3814 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
3815 &devlink_nl_family, NLM_F_ACK | NLM_F_MULTI,
3816 DEVLINK_CMD_REGION_READ);
Parav Panditfdd41ec2019-02-12 14:23:58 -06003817 if (!hdr) {
3818 err = -EMSGSIZE;
Alex Vesker4e547952018-07-12 15:13:14 +03003819 goto out_unlock;
Parav Panditfdd41ec2019-02-12 14:23:58 -06003820 }
Alex Vesker4e547952018-07-12 15:13:14 +03003821
3822 err = devlink_nl_put_handle(skb, devlink);
3823 if (err)
3824 goto nla_put_failure;
3825
3826 err = nla_put_string(skb, DEVLINK_ATTR_REGION_NAME, region_name);
3827 if (err)
3828 goto nla_put_failure;
3829
Michal Kubecekae0be8d2019-04-26 11:13:06 +02003830 chunks_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_REGION_CHUNKS);
Parav Panditfdd41ec2019-02-12 14:23:58 -06003831 if (!chunks_attr) {
3832 err = -EMSGSIZE;
Alex Vesker4e547952018-07-12 15:13:14 +03003833 goto nla_put_failure;
Parav Panditfdd41ec2019-02-12 14:23:58 -06003834 }
Alex Vesker4e547952018-07-12 15:13:14 +03003835
3836 if (attrs[DEVLINK_ATTR_REGION_CHUNK_ADDR] &&
3837 attrs[DEVLINK_ATTR_REGION_CHUNK_LEN]) {
3838 if (!start_offset)
3839 start_offset =
3840 nla_get_u64(attrs[DEVLINK_ATTR_REGION_CHUNK_ADDR]);
3841
3842 end_offset = nla_get_u64(attrs[DEVLINK_ATTR_REGION_CHUNK_ADDR]);
3843 end_offset += nla_get_u64(attrs[DEVLINK_ATTR_REGION_CHUNK_LEN]);
3844 dump = false;
3845 }
3846
3847 err = devlink_nl_region_read_snapshot_fill(skb, devlink,
3848 region, attrs,
3849 start_offset,
3850 end_offset, dump,
3851 &ret_offset);
3852
3853 if (err && err != -EMSGSIZE)
3854 goto nla_put_failure;
3855
3856 /* Check if there was any progress done to prevent infinite loop */
Parav Panditfdd41ec2019-02-12 14:23:58 -06003857 if (ret_offset == start_offset) {
3858 err = -EINVAL;
Alex Vesker4e547952018-07-12 15:13:14 +03003859 goto nla_put_failure;
Parav Panditfdd41ec2019-02-12 14:23:58 -06003860 }
Alex Vesker4e547952018-07-12 15:13:14 +03003861
3862 *((u64 *)&cb->args[0]) = ret_offset;
3863
3864 nla_nest_end(skb, chunks_attr);
3865 genlmsg_end(skb, hdr);
3866 mutex_unlock(&devlink->lock);
3867 mutex_unlock(&devlink_mutex);
Jakub Kicinski68750562019-02-10 19:35:28 -08003868 kfree(attrs);
Alex Vesker4e547952018-07-12 15:13:14 +03003869
3870 return skb->len;
3871
3872nla_put_failure:
3873 genlmsg_cancel(skb, hdr);
3874out_unlock:
3875 mutex_unlock(&devlink->lock);
Parav Panditdac7c082019-02-12 14:24:08 -06003876out_dev:
Alex Vesker4e547952018-07-12 15:13:14 +03003877 mutex_unlock(&devlink_mutex);
Jakub Kicinski68750562019-02-10 19:35:28 -08003878out_free:
3879 kfree(attrs);
Parav Panditfdd41ec2019-02-12 14:23:58 -06003880 return err;
Alex Vesker4e547952018-07-12 15:13:14 +03003881}
3882
Jakub Kicinskif9cf2282019-01-31 10:50:40 -08003883struct devlink_info_req {
3884 struct sk_buff *msg;
3885};
3886
3887int devlink_info_driver_name_put(struct devlink_info_req *req, const char *name)
3888{
3889 return nla_put_string(req->msg, DEVLINK_ATTR_INFO_DRIVER_NAME, name);
3890}
3891EXPORT_SYMBOL_GPL(devlink_info_driver_name_put);
3892
3893int devlink_info_serial_number_put(struct devlink_info_req *req, const char *sn)
3894{
3895 return nla_put_string(req->msg, DEVLINK_ATTR_INFO_SERIAL_NUMBER, sn);
3896}
3897EXPORT_SYMBOL_GPL(devlink_info_serial_number_put);
3898
Jakub Kicinskifc6fae72019-01-31 10:50:41 -08003899static int devlink_info_version_put(struct devlink_info_req *req, int attr,
3900 const char *version_name,
3901 const char *version_value)
3902{
3903 struct nlattr *nest;
3904 int err;
3905
Michal Kubecekae0be8d2019-04-26 11:13:06 +02003906 nest = nla_nest_start_noflag(req->msg, attr);
Jakub Kicinskifc6fae72019-01-31 10:50:41 -08003907 if (!nest)
3908 return -EMSGSIZE;
3909
3910 err = nla_put_string(req->msg, DEVLINK_ATTR_INFO_VERSION_NAME,
3911 version_name);
3912 if (err)
3913 goto nla_put_failure;
3914
3915 err = nla_put_string(req->msg, DEVLINK_ATTR_INFO_VERSION_VALUE,
3916 version_value);
3917 if (err)
3918 goto nla_put_failure;
3919
3920 nla_nest_end(req->msg, nest);
3921
3922 return 0;
3923
3924nla_put_failure:
3925 nla_nest_cancel(req->msg, nest);
3926 return err;
3927}
3928
3929int devlink_info_version_fixed_put(struct devlink_info_req *req,
3930 const char *version_name,
3931 const char *version_value)
3932{
3933 return devlink_info_version_put(req, DEVLINK_ATTR_INFO_VERSION_FIXED,
3934 version_name, version_value);
3935}
3936EXPORT_SYMBOL_GPL(devlink_info_version_fixed_put);
3937
3938int devlink_info_version_stored_put(struct devlink_info_req *req,
3939 const char *version_name,
3940 const char *version_value)
3941{
3942 return devlink_info_version_put(req, DEVLINK_ATTR_INFO_VERSION_STORED,
3943 version_name, version_value);
3944}
3945EXPORT_SYMBOL_GPL(devlink_info_version_stored_put);
3946
3947int devlink_info_version_running_put(struct devlink_info_req *req,
3948 const char *version_name,
3949 const char *version_value)
3950{
3951 return devlink_info_version_put(req, DEVLINK_ATTR_INFO_VERSION_RUNNING,
3952 version_name, version_value);
3953}
3954EXPORT_SYMBOL_GPL(devlink_info_version_running_put);
3955
Jakub Kicinskif9cf2282019-01-31 10:50:40 -08003956static int
3957devlink_nl_info_fill(struct sk_buff *msg, struct devlink *devlink,
3958 enum devlink_command cmd, u32 portid,
3959 u32 seq, int flags, struct netlink_ext_ack *extack)
3960{
3961 struct devlink_info_req req;
3962 void *hdr;
3963 int err;
3964
3965 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
3966 if (!hdr)
3967 return -EMSGSIZE;
3968
3969 err = -EMSGSIZE;
3970 if (devlink_nl_put_handle(msg, devlink))
3971 goto err_cancel_msg;
3972
3973 req.msg = msg;
3974 err = devlink->ops->info_get(devlink, &req, extack);
3975 if (err)
3976 goto err_cancel_msg;
3977
3978 genlmsg_end(msg, hdr);
3979 return 0;
3980
3981err_cancel_msg:
3982 genlmsg_cancel(msg, hdr);
3983 return err;
3984}
3985
3986static int devlink_nl_cmd_info_get_doit(struct sk_buff *skb,
3987 struct genl_info *info)
3988{
3989 struct devlink *devlink = info->user_ptr[0];
3990 struct sk_buff *msg;
3991 int err;
3992
Jakub Kicinskibe6fe1d2019-02-25 19:34:07 -08003993 if (!devlink->ops->info_get)
Jakub Kicinskif9cf2282019-01-31 10:50:40 -08003994 return -EOPNOTSUPP;
3995
3996 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
3997 if (!msg)
3998 return -ENOMEM;
3999
4000 err = devlink_nl_info_fill(msg, devlink, DEVLINK_CMD_INFO_GET,
4001 info->snd_portid, info->snd_seq, 0,
4002 info->extack);
4003 if (err) {
4004 nlmsg_free(msg);
4005 return err;
4006 }
4007
4008 return genlmsg_reply(msg, info);
4009}
4010
4011static int devlink_nl_cmd_info_get_dumpit(struct sk_buff *msg,
4012 struct netlink_callback *cb)
4013{
4014 struct devlink *devlink;
4015 int start = cb->args[0];
4016 int idx = 0;
4017 int err;
4018
4019 mutex_lock(&devlink_mutex);
4020 list_for_each_entry(devlink, &devlink_list, list) {
4021 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
4022 continue;
4023 if (idx < start) {
4024 idx++;
4025 continue;
4026 }
4027
Jiri Pirkoc493b09b2019-03-24 00:21:03 +01004028 if (!devlink->ops->info_get) {
4029 idx++;
4030 continue;
4031 }
4032
Jakub Kicinskif9cf2282019-01-31 10:50:40 -08004033 mutex_lock(&devlink->lock);
4034 err = devlink_nl_info_fill(msg, devlink, DEVLINK_CMD_INFO_GET,
4035 NETLINK_CB(cb->skb).portid,
4036 cb->nlh->nlmsg_seq, NLM_F_MULTI,
4037 cb->extack);
4038 mutex_unlock(&devlink->lock);
4039 if (err)
4040 break;
4041 idx++;
4042 }
4043 mutex_unlock(&devlink_mutex);
4044
4045 cb->args[0] = idx;
4046 return msg->len;
4047}
4048
Eran Ben Elisha1db64e82019-02-07 11:36:32 +02004049struct devlink_fmsg_item {
4050 struct list_head list;
4051 int attrtype;
4052 u8 nla_type;
4053 u16 len;
4054 int value[0];
4055};
4056
4057struct devlink_fmsg {
4058 struct list_head item_list;
4059};
4060
4061static struct devlink_fmsg *devlink_fmsg_alloc(void)
4062{
4063 struct devlink_fmsg *fmsg;
4064
4065 fmsg = kzalloc(sizeof(*fmsg), GFP_KERNEL);
4066 if (!fmsg)
4067 return NULL;
4068
4069 INIT_LIST_HEAD(&fmsg->item_list);
4070
4071 return fmsg;
4072}
4073
4074static void devlink_fmsg_free(struct devlink_fmsg *fmsg)
4075{
4076 struct devlink_fmsg_item *item, *tmp;
4077
4078 list_for_each_entry_safe(item, tmp, &fmsg->item_list, list) {
4079 list_del(&item->list);
4080 kfree(item);
4081 }
4082 kfree(fmsg);
4083}
4084
4085static int devlink_fmsg_nest_common(struct devlink_fmsg *fmsg,
4086 int attrtype)
4087{
4088 struct devlink_fmsg_item *item;
4089
4090 item = kzalloc(sizeof(*item), GFP_KERNEL);
4091 if (!item)
4092 return -ENOMEM;
4093
4094 item->attrtype = attrtype;
4095 list_add_tail(&item->list, &fmsg->item_list);
4096
4097 return 0;
4098}
4099
4100int devlink_fmsg_obj_nest_start(struct devlink_fmsg *fmsg)
4101{
4102 return devlink_fmsg_nest_common(fmsg, DEVLINK_ATTR_FMSG_OBJ_NEST_START);
4103}
4104EXPORT_SYMBOL_GPL(devlink_fmsg_obj_nest_start);
4105
4106static int devlink_fmsg_nest_end(struct devlink_fmsg *fmsg)
4107{
4108 return devlink_fmsg_nest_common(fmsg, DEVLINK_ATTR_FMSG_NEST_END);
4109}
4110
4111int devlink_fmsg_obj_nest_end(struct devlink_fmsg *fmsg)
4112{
4113 return devlink_fmsg_nest_end(fmsg);
4114}
4115EXPORT_SYMBOL_GPL(devlink_fmsg_obj_nest_end);
4116
4117#define DEVLINK_FMSG_MAX_SIZE (GENLMSG_DEFAULT_SIZE - GENL_HDRLEN - NLA_HDRLEN)
4118
4119static int devlink_fmsg_put_name(struct devlink_fmsg *fmsg, const char *name)
4120{
4121 struct devlink_fmsg_item *item;
4122
4123 if (strlen(name) + 1 > DEVLINK_FMSG_MAX_SIZE)
4124 return -EMSGSIZE;
4125
4126 item = kzalloc(sizeof(*item) + strlen(name) + 1, GFP_KERNEL);
4127 if (!item)
4128 return -ENOMEM;
4129
4130 item->nla_type = NLA_NUL_STRING;
4131 item->len = strlen(name) + 1;
4132 item->attrtype = DEVLINK_ATTR_FMSG_OBJ_NAME;
4133 memcpy(&item->value, name, item->len);
4134 list_add_tail(&item->list, &fmsg->item_list);
4135
4136 return 0;
4137}
4138
4139int devlink_fmsg_pair_nest_start(struct devlink_fmsg *fmsg, const char *name)
4140{
4141 int err;
4142
4143 err = devlink_fmsg_nest_common(fmsg, DEVLINK_ATTR_FMSG_PAIR_NEST_START);
4144 if (err)
4145 return err;
4146
4147 err = devlink_fmsg_put_name(fmsg, name);
4148 if (err)
4149 return err;
4150
4151 return 0;
4152}
4153EXPORT_SYMBOL_GPL(devlink_fmsg_pair_nest_start);
4154
4155int devlink_fmsg_pair_nest_end(struct devlink_fmsg *fmsg)
4156{
4157 return devlink_fmsg_nest_end(fmsg);
4158}
4159EXPORT_SYMBOL_GPL(devlink_fmsg_pair_nest_end);
4160
4161int devlink_fmsg_arr_pair_nest_start(struct devlink_fmsg *fmsg,
4162 const char *name)
4163{
4164 int err;
4165
4166 err = devlink_fmsg_pair_nest_start(fmsg, name);
4167 if (err)
4168 return err;
4169
4170 err = devlink_fmsg_nest_common(fmsg, DEVLINK_ATTR_FMSG_ARR_NEST_START);
4171 if (err)
4172 return err;
4173
4174 return 0;
4175}
4176EXPORT_SYMBOL_GPL(devlink_fmsg_arr_pair_nest_start);
4177
4178int devlink_fmsg_arr_pair_nest_end(struct devlink_fmsg *fmsg)
4179{
4180 int err;
4181
4182 err = devlink_fmsg_nest_end(fmsg);
4183 if (err)
4184 return err;
4185
4186 err = devlink_fmsg_nest_end(fmsg);
4187 if (err)
4188 return err;
4189
4190 return 0;
4191}
4192EXPORT_SYMBOL_GPL(devlink_fmsg_arr_pair_nest_end);
4193
4194static int devlink_fmsg_put_value(struct devlink_fmsg *fmsg,
4195 const void *value, u16 value_len,
4196 u8 value_nla_type)
4197{
4198 struct devlink_fmsg_item *item;
4199
4200 if (value_len > DEVLINK_FMSG_MAX_SIZE)
4201 return -EMSGSIZE;
4202
4203 item = kzalloc(sizeof(*item) + value_len, GFP_KERNEL);
4204 if (!item)
4205 return -ENOMEM;
4206
4207 item->nla_type = value_nla_type;
4208 item->len = value_len;
4209 item->attrtype = DEVLINK_ATTR_FMSG_OBJ_VALUE_DATA;
4210 memcpy(&item->value, value, item->len);
4211 list_add_tail(&item->list, &fmsg->item_list);
4212
4213 return 0;
4214}
4215
4216int devlink_fmsg_bool_put(struct devlink_fmsg *fmsg, bool value)
4217{
4218 return devlink_fmsg_put_value(fmsg, &value, sizeof(value), NLA_FLAG);
4219}
4220EXPORT_SYMBOL_GPL(devlink_fmsg_bool_put);
4221
4222int devlink_fmsg_u8_put(struct devlink_fmsg *fmsg, u8 value)
4223{
4224 return devlink_fmsg_put_value(fmsg, &value, sizeof(value), NLA_U8);
4225}
4226EXPORT_SYMBOL_GPL(devlink_fmsg_u8_put);
4227
4228int devlink_fmsg_u32_put(struct devlink_fmsg *fmsg, u32 value)
4229{
4230 return devlink_fmsg_put_value(fmsg, &value, sizeof(value), NLA_U32);
4231}
4232EXPORT_SYMBOL_GPL(devlink_fmsg_u32_put);
4233
4234int devlink_fmsg_u64_put(struct devlink_fmsg *fmsg, u64 value)
4235{
4236 return devlink_fmsg_put_value(fmsg, &value, sizeof(value), NLA_U64);
4237}
4238EXPORT_SYMBOL_GPL(devlink_fmsg_u64_put);
4239
4240int devlink_fmsg_string_put(struct devlink_fmsg *fmsg, const char *value)
4241{
4242 return devlink_fmsg_put_value(fmsg, value, strlen(value) + 1,
4243 NLA_NUL_STRING);
4244}
4245EXPORT_SYMBOL_GPL(devlink_fmsg_string_put);
4246
4247int devlink_fmsg_binary_put(struct devlink_fmsg *fmsg, const void *value,
4248 u16 value_len)
4249{
4250 return devlink_fmsg_put_value(fmsg, value, value_len, NLA_BINARY);
4251}
4252EXPORT_SYMBOL_GPL(devlink_fmsg_binary_put);
4253
4254int devlink_fmsg_bool_pair_put(struct devlink_fmsg *fmsg, const char *name,
4255 bool value)
4256{
4257 int err;
4258
4259 err = devlink_fmsg_pair_nest_start(fmsg, name);
4260 if (err)
4261 return err;
4262
4263 err = devlink_fmsg_bool_put(fmsg, value);
4264 if (err)
4265 return err;
4266
4267 err = devlink_fmsg_pair_nest_end(fmsg);
4268 if (err)
4269 return err;
4270
4271 return 0;
4272}
4273EXPORT_SYMBOL_GPL(devlink_fmsg_bool_pair_put);
4274
4275int devlink_fmsg_u8_pair_put(struct devlink_fmsg *fmsg, const char *name,
4276 u8 value)
4277{
4278 int err;
4279
4280 err = devlink_fmsg_pair_nest_start(fmsg, name);
4281 if (err)
4282 return err;
4283
4284 err = devlink_fmsg_u8_put(fmsg, value);
4285 if (err)
4286 return err;
4287
4288 err = devlink_fmsg_pair_nest_end(fmsg);
4289 if (err)
4290 return err;
4291
4292 return 0;
4293}
4294EXPORT_SYMBOL_GPL(devlink_fmsg_u8_pair_put);
4295
4296int devlink_fmsg_u32_pair_put(struct devlink_fmsg *fmsg, const char *name,
4297 u32 value)
4298{
4299 int err;
4300
4301 err = devlink_fmsg_pair_nest_start(fmsg, name);
4302 if (err)
4303 return err;
4304
4305 err = devlink_fmsg_u32_put(fmsg, value);
4306 if (err)
4307 return err;
4308
4309 err = devlink_fmsg_pair_nest_end(fmsg);
4310 if (err)
4311 return err;
4312
4313 return 0;
4314}
4315EXPORT_SYMBOL_GPL(devlink_fmsg_u32_pair_put);
4316
4317int devlink_fmsg_u64_pair_put(struct devlink_fmsg *fmsg, const char *name,
4318 u64 value)
4319{
4320 int err;
4321
4322 err = devlink_fmsg_pair_nest_start(fmsg, name);
4323 if (err)
4324 return err;
4325
4326 err = devlink_fmsg_u64_put(fmsg, value);
4327 if (err)
4328 return err;
4329
4330 err = devlink_fmsg_pair_nest_end(fmsg);
4331 if (err)
4332 return err;
4333
4334 return 0;
4335}
4336EXPORT_SYMBOL_GPL(devlink_fmsg_u64_pair_put);
4337
4338int devlink_fmsg_string_pair_put(struct devlink_fmsg *fmsg, const char *name,
4339 const char *value)
4340{
4341 int err;
4342
4343 err = devlink_fmsg_pair_nest_start(fmsg, name);
4344 if (err)
4345 return err;
4346
4347 err = devlink_fmsg_string_put(fmsg, value);
4348 if (err)
4349 return err;
4350
4351 err = devlink_fmsg_pair_nest_end(fmsg);
4352 if (err)
4353 return err;
4354
4355 return 0;
4356}
4357EXPORT_SYMBOL_GPL(devlink_fmsg_string_pair_put);
4358
4359int devlink_fmsg_binary_pair_put(struct devlink_fmsg *fmsg, const char *name,
4360 const void *value, u16 value_len)
4361{
4362 int err;
4363
4364 err = devlink_fmsg_pair_nest_start(fmsg, name);
4365 if (err)
4366 return err;
4367
4368 err = devlink_fmsg_binary_put(fmsg, value, value_len);
4369 if (err)
4370 return err;
4371
4372 err = devlink_fmsg_pair_nest_end(fmsg);
4373 if (err)
4374 return err;
4375
4376 return 0;
4377}
4378EXPORT_SYMBOL_GPL(devlink_fmsg_binary_pair_put);
4379
4380static int
4381devlink_fmsg_item_fill_type(struct devlink_fmsg_item *msg, struct sk_buff *skb)
4382{
4383 switch (msg->nla_type) {
4384 case NLA_FLAG:
4385 case NLA_U8:
4386 case NLA_U32:
4387 case NLA_U64:
4388 case NLA_NUL_STRING:
4389 case NLA_BINARY:
4390 return nla_put_u8(skb, DEVLINK_ATTR_FMSG_OBJ_VALUE_TYPE,
4391 msg->nla_type);
4392 default:
4393 return -EINVAL;
4394 }
4395}
4396
4397static int
4398devlink_fmsg_item_fill_data(struct devlink_fmsg_item *msg, struct sk_buff *skb)
4399{
4400 int attrtype = DEVLINK_ATTR_FMSG_OBJ_VALUE_DATA;
4401 u8 tmp;
4402
4403 switch (msg->nla_type) {
4404 case NLA_FLAG:
4405 /* Always provide flag data, regardless of its value */
4406 tmp = *(bool *) msg->value;
4407
4408 return nla_put_u8(skb, attrtype, tmp);
4409 case NLA_U8:
4410 return nla_put_u8(skb, attrtype, *(u8 *) msg->value);
4411 case NLA_U32:
4412 return nla_put_u32(skb, attrtype, *(u32 *) msg->value);
4413 case NLA_U64:
4414 return nla_put_u64_64bit(skb, attrtype, *(u64 *) msg->value,
4415 DEVLINK_ATTR_PAD);
4416 case NLA_NUL_STRING:
4417 return nla_put_string(skb, attrtype, (char *) &msg->value);
4418 case NLA_BINARY:
4419 return nla_put(skb, attrtype, msg->len, (void *) &msg->value);
4420 default:
4421 return -EINVAL;
4422 }
4423}
4424
4425static int
4426devlink_fmsg_prepare_skb(struct devlink_fmsg *fmsg, struct sk_buff *skb,
4427 int *start)
4428{
4429 struct devlink_fmsg_item *item;
4430 struct nlattr *fmsg_nlattr;
4431 int i = 0;
4432 int err;
4433
Michal Kubecekae0be8d2019-04-26 11:13:06 +02004434 fmsg_nlattr = nla_nest_start_noflag(skb, DEVLINK_ATTR_FMSG);
Eran Ben Elisha1db64e82019-02-07 11:36:32 +02004435 if (!fmsg_nlattr)
4436 return -EMSGSIZE;
4437
4438 list_for_each_entry(item, &fmsg->item_list, list) {
4439 if (i < *start) {
4440 i++;
4441 continue;
4442 }
4443
4444 switch (item->attrtype) {
4445 case DEVLINK_ATTR_FMSG_OBJ_NEST_START:
4446 case DEVLINK_ATTR_FMSG_PAIR_NEST_START:
4447 case DEVLINK_ATTR_FMSG_ARR_NEST_START:
4448 case DEVLINK_ATTR_FMSG_NEST_END:
4449 err = nla_put_flag(skb, item->attrtype);
4450 break;
4451 case DEVLINK_ATTR_FMSG_OBJ_VALUE_DATA:
4452 err = devlink_fmsg_item_fill_type(item, skb);
4453 if (err)
4454 break;
4455 err = devlink_fmsg_item_fill_data(item, skb);
4456 break;
4457 case DEVLINK_ATTR_FMSG_OBJ_NAME:
4458 err = nla_put_string(skb, item->attrtype,
4459 (char *) &item->value);
4460 break;
4461 default:
4462 err = -EINVAL;
4463 break;
4464 }
4465 if (!err)
4466 *start = ++i;
4467 else
4468 break;
4469 }
4470
4471 nla_nest_end(skb, fmsg_nlattr);
4472 return err;
4473}
4474
4475static int devlink_fmsg_snd(struct devlink_fmsg *fmsg,
4476 struct genl_info *info,
4477 enum devlink_command cmd, int flags)
4478{
4479 struct nlmsghdr *nlh;
4480 struct sk_buff *skb;
4481 bool last = false;
4482 int index = 0;
4483 void *hdr;
4484 int err;
4485
4486 while (!last) {
4487 int tmp_index = index;
4488
4489 skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
4490 if (!skb)
4491 return -ENOMEM;
4492
4493 hdr = genlmsg_put(skb, info->snd_portid, info->snd_seq,
4494 &devlink_nl_family, flags | NLM_F_MULTI, cmd);
4495 if (!hdr) {
4496 err = -EMSGSIZE;
4497 goto nla_put_failure;
4498 }
4499
4500 err = devlink_fmsg_prepare_skb(fmsg, skb, &index);
4501 if (!err)
4502 last = true;
4503 else if (err != -EMSGSIZE || tmp_index == index)
4504 goto nla_put_failure;
4505
4506 genlmsg_end(skb, hdr);
4507 err = genlmsg_reply(skb, info);
4508 if (err)
4509 return err;
4510 }
4511
4512 skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
4513 if (!skb)
4514 return -ENOMEM;
4515 nlh = nlmsg_put(skb, info->snd_portid, info->snd_seq,
4516 NLMSG_DONE, 0, flags | NLM_F_MULTI);
4517 if (!nlh) {
4518 err = -EMSGSIZE;
4519 goto nla_put_failure;
4520 }
Eran Ben Elisha1db64e82019-02-07 11:36:32 +02004521
Li RongQingfde55ea2019-02-11 19:09:07 +08004522 return genlmsg_reply(skb, info);
Eran Ben Elisha1db64e82019-02-07 11:36:32 +02004523
4524nla_put_failure:
4525 nlmsg_free(skb);
4526 return err;
4527}
4528
Aya Levine44ef4e2019-05-16 09:49:20 +03004529static int devlink_fmsg_dumpit(struct devlink_fmsg *fmsg, struct sk_buff *skb,
4530 struct netlink_callback *cb,
4531 enum devlink_command cmd)
4532{
4533 int index = cb->args[0];
4534 int tmp_index = index;
4535 void *hdr;
4536 int err;
4537
4538 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
4539 &devlink_nl_family, NLM_F_ACK | NLM_F_MULTI, cmd);
4540 if (!hdr) {
4541 err = -EMSGSIZE;
4542 goto nla_put_failure;
4543 }
4544
4545 err = devlink_fmsg_prepare_skb(fmsg, skb, &index);
4546 if ((err && err != -EMSGSIZE) || tmp_index == index)
4547 goto nla_put_failure;
4548
4549 cb->args[0] = index;
4550 genlmsg_end(skb, hdr);
4551 return skb->len;
4552
4553nla_put_failure:
4554 genlmsg_cancel(skb, hdr);
4555 return err;
4556}
4557
Eran Ben Elishaa0bdcc52019-02-07 11:36:33 +02004558struct devlink_health_reporter {
4559 struct list_head list;
4560 void *priv;
4561 const struct devlink_health_reporter_ops *ops;
4562 struct devlink *devlink;
Eran Ben Elishac8e1da02019-02-07 11:36:34 +02004563 struct devlink_fmsg *dump_fmsg;
4564 struct mutex dump_lock; /* lock parallel read/write from dump buffers */
Eran Ben Elishaa0bdcc52019-02-07 11:36:33 +02004565 u64 graceful_period;
4566 bool auto_recover;
4567 u8 health_state;
Eran Ben Elishac8e1da02019-02-07 11:36:34 +02004568 u64 dump_ts;
4569 u64 error_count;
4570 u64 recovery_count;
4571 u64 last_recovery_ts;
Moshe Shemeshb587bda2019-04-29 12:41:45 +03004572 refcount_t refcount;
Eran Ben Elishac8e1da02019-02-07 11:36:34 +02004573};
4574
Eran Ben Elishaa0bdcc52019-02-07 11:36:33 +02004575void *
4576devlink_health_reporter_priv(struct devlink_health_reporter *reporter)
4577{
4578 return reporter->priv;
4579}
4580EXPORT_SYMBOL_GPL(devlink_health_reporter_priv);
4581
4582static struct devlink_health_reporter *
4583devlink_health_reporter_find_by_name(struct devlink *devlink,
4584 const char *reporter_name)
4585{
4586 struct devlink_health_reporter *reporter;
4587
Moshe Shemeshb587bda2019-04-29 12:41:45 +03004588 lockdep_assert_held(&devlink->reporters_lock);
Eran Ben Elishaa0bdcc52019-02-07 11:36:33 +02004589 list_for_each_entry(reporter, &devlink->reporter_list, list)
4590 if (!strcmp(reporter->ops->name, reporter_name))
4591 return reporter;
4592 return NULL;
4593}
4594
4595/**
4596 * devlink_health_reporter_create - create devlink health reporter
4597 *
4598 * @devlink: devlink
4599 * @ops: ops
4600 * @graceful_period: to avoid recovery loops, in msecs
4601 * @auto_recover: auto recover when error occurs
4602 * @priv: priv
4603 */
4604struct devlink_health_reporter *
4605devlink_health_reporter_create(struct devlink *devlink,
4606 const struct devlink_health_reporter_ops *ops,
4607 u64 graceful_period, bool auto_recover,
4608 void *priv)
4609{
4610 struct devlink_health_reporter *reporter;
4611
Moshe Shemeshb587bda2019-04-29 12:41:45 +03004612 mutex_lock(&devlink->reporters_lock);
Eran Ben Elishaa0bdcc52019-02-07 11:36:33 +02004613 if (devlink_health_reporter_find_by_name(devlink, ops->name)) {
4614 reporter = ERR_PTR(-EEXIST);
4615 goto unlock;
4616 }
4617
4618 if (WARN_ON(auto_recover && !ops->recover) ||
4619 WARN_ON(graceful_period && !ops->recover)) {
4620 reporter = ERR_PTR(-EINVAL);
4621 goto unlock;
4622 }
4623
4624 reporter = kzalloc(sizeof(*reporter), GFP_KERNEL);
4625 if (!reporter) {
4626 reporter = ERR_PTR(-ENOMEM);
4627 goto unlock;
4628 }
4629
4630 reporter->priv = priv;
4631 reporter->ops = ops;
4632 reporter->devlink = devlink;
4633 reporter->graceful_period = graceful_period;
4634 reporter->auto_recover = auto_recover;
Eran Ben Elishac8e1da02019-02-07 11:36:34 +02004635 mutex_init(&reporter->dump_lock);
Moshe Shemeshb587bda2019-04-29 12:41:45 +03004636 refcount_set(&reporter->refcount, 1);
Eran Ben Elishaa0bdcc52019-02-07 11:36:33 +02004637 list_add_tail(&reporter->list, &devlink->reporter_list);
4638unlock:
Moshe Shemeshb587bda2019-04-29 12:41:45 +03004639 mutex_unlock(&devlink->reporters_lock);
Eran Ben Elishaa0bdcc52019-02-07 11:36:33 +02004640 return reporter;
4641}
4642EXPORT_SYMBOL_GPL(devlink_health_reporter_create);
4643
4644/**
4645 * devlink_health_reporter_destroy - destroy devlink health reporter
4646 *
4647 * @reporter: devlink health reporter to destroy
4648 */
4649void
4650devlink_health_reporter_destroy(struct devlink_health_reporter *reporter)
4651{
Moshe Shemeshb587bda2019-04-29 12:41:45 +03004652 mutex_lock(&reporter->devlink->reporters_lock);
Eran Ben Elishaa0bdcc52019-02-07 11:36:33 +02004653 list_del(&reporter->list);
Moshe Shemeshb587bda2019-04-29 12:41:45 +03004654 mutex_unlock(&reporter->devlink->reporters_lock);
4655 while (refcount_read(&reporter->refcount) > 1)
4656 msleep(100);
Jiri Pirko375cf8c2019-03-24 11:14:24 +01004657 mutex_destroy(&reporter->dump_lock);
Eran Ben Elishac8e1da02019-02-07 11:36:34 +02004658 if (reporter->dump_fmsg)
4659 devlink_fmsg_free(reporter->dump_fmsg);
Eran Ben Elishaa0bdcc52019-02-07 11:36:33 +02004660 kfree(reporter);
4661}
4662EXPORT_SYMBOL_GPL(devlink_health_reporter_destroy);
4663
Eran Ben Elisha3167b272019-03-03 10:57:30 +02004664void
4665devlink_health_reporter_state_update(struct devlink_health_reporter *reporter,
4666 enum devlink_health_reporter_state state)
4667{
4668 if (WARN_ON(state != DEVLINK_HEALTH_REPORTER_STATE_HEALTHY &&
4669 state != DEVLINK_HEALTH_REPORTER_STATE_ERROR))
4670 return;
4671
4672 if (reporter->health_state == state)
4673 return;
4674
4675 reporter->health_state = state;
4676 trace_devlink_health_reporter_state_update(reporter->devlink,
4677 reporter->ops->name, state);
4678}
4679EXPORT_SYMBOL_GPL(devlink_health_reporter_state_update);
4680
Eran Ben Elishac8e1da02019-02-07 11:36:34 +02004681static int
4682devlink_health_reporter_recover(struct devlink_health_reporter *reporter,
4683 void *priv_ctx)
4684{
4685 int err;
4686
4687 if (!reporter->ops->recover)
4688 return -EOPNOTSUPP;
4689
4690 err = reporter->ops->recover(reporter, priv_ctx);
4691 if (err)
4692 return err;
4693
4694 reporter->recovery_count++;
4695 reporter->health_state = DEVLINK_HEALTH_REPORTER_STATE_HEALTHY;
4696 reporter->last_recovery_ts = jiffies;
4697
4698 return 0;
4699}
4700
4701static void
4702devlink_health_dump_clear(struct devlink_health_reporter *reporter)
4703{
4704 if (!reporter->dump_fmsg)
4705 return;
4706 devlink_fmsg_free(reporter->dump_fmsg);
4707 reporter->dump_fmsg = NULL;
4708}
4709
4710static int devlink_health_do_dump(struct devlink_health_reporter *reporter,
4711 void *priv_ctx)
4712{
4713 int err;
4714
4715 if (!reporter->ops->dump)
4716 return 0;
4717
4718 if (reporter->dump_fmsg)
4719 return 0;
4720
4721 reporter->dump_fmsg = devlink_fmsg_alloc();
4722 if (!reporter->dump_fmsg) {
4723 err = -ENOMEM;
4724 return err;
4725 }
4726
4727 err = devlink_fmsg_obj_nest_start(reporter->dump_fmsg);
4728 if (err)
4729 goto dump_err;
4730
4731 err = reporter->ops->dump(reporter, reporter->dump_fmsg,
4732 priv_ctx);
4733 if (err)
4734 goto dump_err;
4735
4736 err = devlink_fmsg_obj_nest_end(reporter->dump_fmsg);
4737 if (err)
4738 goto dump_err;
4739
4740 reporter->dump_ts = jiffies;
4741
4742 return 0;
4743
4744dump_err:
4745 devlink_health_dump_clear(reporter);
4746 return err;
4747}
4748
4749int devlink_health_report(struct devlink_health_reporter *reporter,
4750 const char *msg, void *priv_ctx)
4751{
Eran Ben Elishaa0a21ad2019-03-03 10:57:29 +02004752 enum devlink_health_reporter_state prev_health_state;
Eran Ben Elishac8e1da02019-02-07 11:36:34 +02004753 struct devlink *devlink = reporter->devlink;
4754
4755 /* write a log message of the current error */
4756 WARN_ON(!msg);
4757 trace_devlink_health_report(devlink, reporter->ops->name, msg);
4758 reporter->error_count++;
Eran Ben Elishaa0a21ad2019-03-03 10:57:29 +02004759 prev_health_state = reporter->health_state;
4760 reporter->health_state = DEVLINK_HEALTH_REPORTER_STATE_ERROR;
Eran Ben Elishac8e1da02019-02-07 11:36:34 +02004761
4762 /* abort if the previous error wasn't recovered */
4763 if (reporter->auto_recover &&
Eran Ben Elishaa0a21ad2019-03-03 10:57:29 +02004764 (prev_health_state != DEVLINK_HEALTH_REPORTER_STATE_HEALTHY ||
Eran Ben Elishac8e1da02019-02-07 11:36:34 +02004765 jiffies - reporter->last_recovery_ts <
4766 msecs_to_jiffies(reporter->graceful_period))) {
4767 trace_devlink_health_recover_aborted(devlink,
4768 reporter->ops->name,
4769 reporter->health_state,
4770 jiffies -
4771 reporter->last_recovery_ts);
4772 return -ECANCELED;
4773 }
4774
4775 reporter->health_state = DEVLINK_HEALTH_REPORTER_STATE_ERROR;
4776
4777 mutex_lock(&reporter->dump_lock);
4778 /* store current dump of current error, for later analysis */
4779 devlink_health_do_dump(reporter, priv_ctx);
4780 mutex_unlock(&reporter->dump_lock);
4781
4782 if (reporter->auto_recover)
4783 return devlink_health_reporter_recover(reporter, priv_ctx);
4784
4785 return 0;
4786}
4787EXPORT_SYMBOL_GPL(devlink_health_report);
4788
Eran Ben Elisha7afe335a2019-02-07 11:36:35 +02004789static struct devlink_health_reporter *
Aya Levine44ef4e2019-05-16 09:49:20 +03004790devlink_health_reporter_get_from_attrs(struct devlink *devlink,
4791 struct nlattr **attrs)
Eran Ben Elisha7afe335a2019-02-07 11:36:35 +02004792{
Moshe Shemeshb587bda2019-04-29 12:41:45 +03004793 struct devlink_health_reporter *reporter;
Eran Ben Elisha7afe335a2019-02-07 11:36:35 +02004794 char *reporter_name;
4795
Aya Levine44ef4e2019-05-16 09:49:20 +03004796 if (!attrs[DEVLINK_ATTR_HEALTH_REPORTER_NAME])
Eran Ben Elisha7afe335a2019-02-07 11:36:35 +02004797 return NULL;
4798
Aya Levine44ef4e2019-05-16 09:49:20 +03004799 reporter_name = nla_data(attrs[DEVLINK_ATTR_HEALTH_REPORTER_NAME]);
Moshe Shemeshb587bda2019-04-29 12:41:45 +03004800 mutex_lock(&devlink->reporters_lock);
4801 reporter = devlink_health_reporter_find_by_name(devlink, reporter_name);
4802 if (reporter)
4803 refcount_inc(&reporter->refcount);
4804 mutex_unlock(&devlink->reporters_lock);
4805 return reporter;
4806}
4807
Aya Levine44ef4e2019-05-16 09:49:20 +03004808static struct devlink_health_reporter *
4809devlink_health_reporter_get_from_info(struct devlink *devlink,
4810 struct genl_info *info)
4811{
4812 return devlink_health_reporter_get_from_attrs(devlink, info->attrs);
4813}
4814
4815static struct devlink_health_reporter *
4816devlink_health_reporter_get_from_cb(struct netlink_callback *cb)
4817{
4818 struct devlink_health_reporter *reporter;
4819 struct devlink *devlink;
4820 struct nlattr **attrs;
4821 int err;
4822
4823 attrs = kmalloc_array(DEVLINK_ATTR_MAX + 1, sizeof(*attrs), GFP_KERNEL);
4824 if (!attrs)
4825 return NULL;
4826
4827 err = nlmsg_parse_deprecated(cb->nlh,
4828 GENL_HDRLEN + devlink_nl_family.hdrsize,
4829 attrs, DEVLINK_ATTR_MAX,
4830 devlink_nl_family.policy, cb->extack);
4831 if (err)
4832 goto free;
4833
4834 mutex_lock(&devlink_mutex);
4835 devlink = devlink_get_from_attrs(sock_net(cb->skb->sk), attrs);
4836 if (IS_ERR(devlink))
4837 goto unlock;
4838
4839 reporter = devlink_health_reporter_get_from_attrs(devlink, attrs);
4840 mutex_unlock(&devlink_mutex);
4841 kfree(attrs);
4842 return reporter;
4843unlock:
4844 mutex_unlock(&devlink_mutex);
4845free:
4846 kfree(attrs);
4847 return NULL;
4848}
4849
Moshe Shemeshb587bda2019-04-29 12:41:45 +03004850static void
4851devlink_health_reporter_put(struct devlink_health_reporter *reporter)
4852{
4853 refcount_dec(&reporter->refcount);
Eran Ben Elisha7afe335a2019-02-07 11:36:35 +02004854}
4855
4856static int
4857devlink_nl_health_reporter_fill(struct sk_buff *msg,
4858 struct devlink *devlink,
4859 struct devlink_health_reporter *reporter,
4860 enum devlink_command cmd, u32 portid,
4861 u32 seq, int flags)
4862{
4863 struct nlattr *reporter_attr;
4864 void *hdr;
4865
4866 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
4867 if (!hdr)
4868 return -EMSGSIZE;
4869
4870 if (devlink_nl_put_handle(msg, devlink))
4871 goto genlmsg_cancel;
4872
Michal Kubecekae0be8d2019-04-26 11:13:06 +02004873 reporter_attr = nla_nest_start_noflag(msg,
4874 DEVLINK_ATTR_HEALTH_REPORTER);
Eran Ben Elisha7afe335a2019-02-07 11:36:35 +02004875 if (!reporter_attr)
4876 goto genlmsg_cancel;
4877 if (nla_put_string(msg, DEVLINK_ATTR_HEALTH_REPORTER_NAME,
4878 reporter->ops->name))
4879 goto reporter_nest_cancel;
4880 if (nla_put_u8(msg, DEVLINK_ATTR_HEALTH_REPORTER_STATE,
4881 reporter->health_state))
4882 goto reporter_nest_cancel;
Aya Levin54719522019-02-21 14:12:01 +02004883 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_HEALTH_REPORTER_ERR_COUNT,
Eran Ben Elisha7afe335a2019-02-07 11:36:35 +02004884 reporter->error_count, DEVLINK_ATTR_PAD))
4885 goto reporter_nest_cancel;
Aya Levin54719522019-02-21 14:12:01 +02004886 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_HEALTH_REPORTER_RECOVER_COUNT,
Eran Ben Elisha7afe335a2019-02-07 11:36:35 +02004887 reporter->recovery_count, DEVLINK_ATTR_PAD))
4888 goto reporter_nest_cancel;
Aya Levin574b1e12019-02-21 14:12:02 +02004889 if (reporter->ops->recover &&
4890 nla_put_u64_64bit(msg, DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD,
Eran Ben Elisha7afe335a2019-02-07 11:36:35 +02004891 reporter->graceful_period,
4892 DEVLINK_ATTR_PAD))
4893 goto reporter_nest_cancel;
Aya Levin574b1e12019-02-21 14:12:02 +02004894 if (reporter->ops->recover &&
4895 nla_put_u8(msg, DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER,
Eran Ben Elisha7afe335a2019-02-07 11:36:35 +02004896 reporter->auto_recover))
4897 goto reporter_nest_cancel;
4898 if (reporter->dump_fmsg &&
4899 nla_put_u64_64bit(msg, DEVLINK_ATTR_HEALTH_REPORTER_DUMP_TS,
4900 jiffies_to_msecs(reporter->dump_ts),
4901 DEVLINK_ATTR_PAD))
4902 goto reporter_nest_cancel;
4903
4904 nla_nest_end(msg, reporter_attr);
4905 genlmsg_end(msg, hdr);
4906 return 0;
4907
4908reporter_nest_cancel:
4909 nla_nest_end(msg, reporter_attr);
4910genlmsg_cancel:
4911 genlmsg_cancel(msg, hdr);
4912 return -EMSGSIZE;
4913}
4914
4915static int devlink_nl_cmd_health_reporter_get_doit(struct sk_buff *skb,
4916 struct genl_info *info)
4917{
4918 struct devlink *devlink = info->user_ptr[0];
4919 struct devlink_health_reporter *reporter;
4920 struct sk_buff *msg;
4921 int err;
4922
4923 reporter = devlink_health_reporter_get_from_info(devlink, info);
4924 if (!reporter)
4925 return -EINVAL;
4926
4927 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Moshe Shemeshb587bda2019-04-29 12:41:45 +03004928 if (!msg) {
4929 err = -ENOMEM;
4930 goto out;
4931 }
Eran Ben Elisha7afe335a2019-02-07 11:36:35 +02004932
4933 err = devlink_nl_health_reporter_fill(msg, devlink, reporter,
4934 DEVLINK_CMD_HEALTH_REPORTER_GET,
4935 info->snd_portid, info->snd_seq,
4936 0);
4937 if (err) {
4938 nlmsg_free(msg);
Moshe Shemeshb587bda2019-04-29 12:41:45 +03004939 goto out;
Eran Ben Elisha7afe335a2019-02-07 11:36:35 +02004940 }
4941
Moshe Shemeshb587bda2019-04-29 12:41:45 +03004942 err = genlmsg_reply(msg, info);
4943out:
4944 devlink_health_reporter_put(reporter);
4945 return err;
Eran Ben Elisha7afe335a2019-02-07 11:36:35 +02004946}
4947
4948static int
4949devlink_nl_cmd_health_reporter_get_dumpit(struct sk_buff *msg,
4950 struct netlink_callback *cb)
4951{
4952 struct devlink_health_reporter *reporter;
4953 struct devlink *devlink;
4954 int start = cb->args[0];
4955 int idx = 0;
4956 int err;
4957
4958 mutex_lock(&devlink_mutex);
4959 list_for_each_entry(devlink, &devlink_list, list) {
4960 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
4961 continue;
Moshe Shemeshb587bda2019-04-29 12:41:45 +03004962 mutex_lock(&devlink->reporters_lock);
Eran Ben Elisha7afe335a2019-02-07 11:36:35 +02004963 list_for_each_entry(reporter, &devlink->reporter_list,
4964 list) {
4965 if (idx < start) {
4966 idx++;
4967 continue;
4968 }
4969 err = devlink_nl_health_reporter_fill(msg, devlink,
4970 reporter,
4971 DEVLINK_CMD_HEALTH_REPORTER_GET,
4972 NETLINK_CB(cb->skb).portid,
4973 cb->nlh->nlmsg_seq,
4974 NLM_F_MULTI);
4975 if (err) {
Moshe Shemeshb587bda2019-04-29 12:41:45 +03004976 mutex_unlock(&devlink->reporters_lock);
Eran Ben Elisha7afe335a2019-02-07 11:36:35 +02004977 goto out;
4978 }
4979 idx++;
4980 }
Moshe Shemeshb587bda2019-04-29 12:41:45 +03004981 mutex_unlock(&devlink->reporters_lock);
Eran Ben Elisha7afe335a2019-02-07 11:36:35 +02004982 }
4983out:
4984 mutex_unlock(&devlink_mutex);
4985
4986 cb->args[0] = idx;
4987 return msg->len;
4988}
4989
Eran Ben Elishaa1e55ec2019-02-07 11:36:36 +02004990static int
4991devlink_nl_cmd_health_reporter_set_doit(struct sk_buff *skb,
4992 struct genl_info *info)
4993{
4994 struct devlink *devlink = info->user_ptr[0];
4995 struct devlink_health_reporter *reporter;
Moshe Shemeshb587bda2019-04-29 12:41:45 +03004996 int err;
Eran Ben Elishaa1e55ec2019-02-07 11:36:36 +02004997
4998 reporter = devlink_health_reporter_get_from_info(devlink, info);
4999 if (!reporter)
5000 return -EINVAL;
5001
5002 if (!reporter->ops->recover &&
5003 (info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD] ||
Moshe Shemeshb587bda2019-04-29 12:41:45 +03005004 info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER])) {
5005 err = -EOPNOTSUPP;
5006 goto out;
5007 }
Eran Ben Elishaa1e55ec2019-02-07 11:36:36 +02005008
5009 if (info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD])
5010 reporter->graceful_period =
5011 nla_get_u64(info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD]);
5012
5013 if (info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER])
5014 reporter->auto_recover =
5015 nla_get_u8(info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER]);
5016
Moshe Shemeshb587bda2019-04-29 12:41:45 +03005017 devlink_health_reporter_put(reporter);
Eran Ben Elishaa1e55ec2019-02-07 11:36:36 +02005018 return 0;
Moshe Shemeshb587bda2019-04-29 12:41:45 +03005019out:
5020 devlink_health_reporter_put(reporter);
5021 return err;
Eran Ben Elishaa1e55ec2019-02-07 11:36:36 +02005022}
5023
Eran Ben Elisha20a09432019-02-07 11:36:37 +02005024static int devlink_nl_cmd_health_reporter_recover_doit(struct sk_buff *skb,
5025 struct genl_info *info)
5026{
5027 struct devlink *devlink = info->user_ptr[0];
5028 struct devlink_health_reporter *reporter;
Moshe Shemeshb587bda2019-04-29 12:41:45 +03005029 int err;
Eran Ben Elisha20a09432019-02-07 11:36:37 +02005030
5031 reporter = devlink_health_reporter_get_from_info(devlink, info);
5032 if (!reporter)
5033 return -EINVAL;
5034
Moshe Shemeshb587bda2019-04-29 12:41:45 +03005035 err = devlink_health_reporter_recover(reporter, NULL);
5036
5037 devlink_health_reporter_put(reporter);
5038 return err;
Eran Ben Elisha20a09432019-02-07 11:36:37 +02005039}
5040
Eran Ben Elishafca42a22019-02-07 11:36:38 +02005041static int devlink_nl_cmd_health_reporter_diagnose_doit(struct sk_buff *skb,
5042 struct genl_info *info)
5043{
5044 struct devlink *devlink = info->user_ptr[0];
5045 struct devlink_health_reporter *reporter;
5046 struct devlink_fmsg *fmsg;
5047 int err;
5048
5049 reporter = devlink_health_reporter_get_from_info(devlink, info);
5050 if (!reporter)
5051 return -EINVAL;
5052
Moshe Shemeshb587bda2019-04-29 12:41:45 +03005053 if (!reporter->ops->diagnose) {
5054 devlink_health_reporter_put(reporter);
Eran Ben Elishafca42a22019-02-07 11:36:38 +02005055 return -EOPNOTSUPP;
Moshe Shemeshb587bda2019-04-29 12:41:45 +03005056 }
Eran Ben Elishafca42a22019-02-07 11:36:38 +02005057
5058 fmsg = devlink_fmsg_alloc();
Moshe Shemeshb587bda2019-04-29 12:41:45 +03005059 if (!fmsg) {
5060 devlink_health_reporter_put(reporter);
Eran Ben Elishafca42a22019-02-07 11:36:38 +02005061 return -ENOMEM;
Moshe Shemeshb587bda2019-04-29 12:41:45 +03005062 }
Eran Ben Elishafca42a22019-02-07 11:36:38 +02005063
5064 err = devlink_fmsg_obj_nest_start(fmsg);
5065 if (err)
5066 goto out;
5067
5068 err = reporter->ops->diagnose(reporter, fmsg);
5069 if (err)
5070 goto out;
5071
5072 err = devlink_fmsg_obj_nest_end(fmsg);
5073 if (err)
5074 goto out;
5075
5076 err = devlink_fmsg_snd(fmsg, info,
5077 DEVLINK_CMD_HEALTH_REPORTER_DIAGNOSE, 0);
5078
5079out:
5080 devlink_fmsg_free(fmsg);
Moshe Shemeshb587bda2019-04-29 12:41:45 +03005081 devlink_health_reporter_put(reporter);
Eran Ben Elishafca42a22019-02-07 11:36:38 +02005082 return err;
5083}
5084
Aya Levine44ef4e2019-05-16 09:49:20 +03005085static int
5086devlink_nl_cmd_health_reporter_dump_get_dumpit(struct sk_buff *skb,
5087 struct netlink_callback *cb)
Eran Ben Elisha35455e22019-02-07 11:36:39 +02005088{
Eran Ben Elisha35455e22019-02-07 11:36:39 +02005089 struct devlink_health_reporter *reporter;
Aya Levine44ef4e2019-05-16 09:49:20 +03005090 u64 start = cb->args[0];
Eran Ben Elisha35455e22019-02-07 11:36:39 +02005091 int err;
5092
Aya Levine44ef4e2019-05-16 09:49:20 +03005093 reporter = devlink_health_reporter_get_from_cb(cb);
Eran Ben Elisha35455e22019-02-07 11:36:39 +02005094 if (!reporter)
5095 return -EINVAL;
5096
Moshe Shemeshb587bda2019-04-29 12:41:45 +03005097 if (!reporter->ops->dump) {
Aya Levine44ef4e2019-05-16 09:49:20 +03005098 err = -EOPNOTSUPP;
5099 goto out;
5100 }
5101 mutex_lock(&reporter->dump_lock);
5102 if (!start) {
5103 err = devlink_health_do_dump(reporter, NULL);
5104 if (err)
5105 goto unlock;
5106 cb->args[1] = reporter->dump_ts;
5107 }
5108 if (!reporter->dump_fmsg || cb->args[1] != reporter->dump_ts) {
5109 NL_SET_ERR_MSG_MOD(cb->extack, "Dump trampled, please retry");
5110 err = -EAGAIN;
5111 goto unlock;
Moshe Shemeshb587bda2019-04-29 12:41:45 +03005112 }
Eran Ben Elisha35455e22019-02-07 11:36:39 +02005113
Aya Levine44ef4e2019-05-16 09:49:20 +03005114 err = devlink_fmsg_dumpit(reporter->dump_fmsg, skb, cb,
5115 DEVLINK_CMD_HEALTH_REPORTER_DUMP_GET);
5116unlock:
Eran Ben Elisha35455e22019-02-07 11:36:39 +02005117 mutex_unlock(&reporter->dump_lock);
Aya Levine44ef4e2019-05-16 09:49:20 +03005118out:
Moshe Shemeshb587bda2019-04-29 12:41:45 +03005119 devlink_health_reporter_put(reporter);
Eran Ben Elisha35455e22019-02-07 11:36:39 +02005120 return err;
5121}
5122
5123static int
5124devlink_nl_cmd_health_reporter_dump_clear_doit(struct sk_buff *skb,
5125 struct genl_info *info)
5126{
5127 struct devlink *devlink = info->user_ptr[0];
5128 struct devlink_health_reporter *reporter;
5129
5130 reporter = devlink_health_reporter_get_from_info(devlink, info);
5131 if (!reporter)
5132 return -EINVAL;
5133
Moshe Shemeshb587bda2019-04-29 12:41:45 +03005134 if (!reporter->ops->dump) {
5135 devlink_health_reporter_put(reporter);
Eran Ben Elisha35455e22019-02-07 11:36:39 +02005136 return -EOPNOTSUPP;
Moshe Shemeshb587bda2019-04-29 12:41:45 +03005137 }
Eran Ben Elisha35455e22019-02-07 11:36:39 +02005138
5139 mutex_lock(&reporter->dump_lock);
5140 devlink_health_dump_clear(reporter);
5141 mutex_unlock(&reporter->dump_lock);
Moshe Shemeshb587bda2019-04-29 12:41:45 +03005142 devlink_health_reporter_put(reporter);
Eran Ben Elisha35455e22019-02-07 11:36:39 +02005143 return 0;
5144}
5145
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01005146static const struct nla_policy devlink_nl_policy[DEVLINK_ATTR_MAX + 1] = {
5147 [DEVLINK_ATTR_BUS_NAME] = { .type = NLA_NUL_STRING },
5148 [DEVLINK_ATTR_DEV_NAME] = { .type = NLA_NUL_STRING },
5149 [DEVLINK_ATTR_PORT_INDEX] = { .type = NLA_U32 },
5150 [DEVLINK_ATTR_PORT_TYPE] = { .type = NLA_U16 },
5151 [DEVLINK_ATTR_PORT_SPLIT_COUNT] = { .type = NLA_U32 },
Jiri Pirkobf797472016-04-14 18:19:13 +02005152 [DEVLINK_ATTR_SB_INDEX] = { .type = NLA_U32 },
5153 [DEVLINK_ATTR_SB_POOL_INDEX] = { .type = NLA_U16 },
5154 [DEVLINK_ATTR_SB_POOL_TYPE] = { .type = NLA_U8 },
5155 [DEVLINK_ATTR_SB_POOL_SIZE] = { .type = NLA_U32 },
5156 [DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE] = { .type = NLA_U8 },
5157 [DEVLINK_ATTR_SB_THRESHOLD] = { .type = NLA_U32 },
5158 [DEVLINK_ATTR_SB_TC_INDEX] = { .type = NLA_U16 },
Or Gerlitz08f4b592016-07-01 14:51:01 +03005159 [DEVLINK_ATTR_ESWITCH_MODE] = { .type = NLA_U16 },
Roi Dayan59bfde02016-11-22 23:09:57 +02005160 [DEVLINK_ATTR_ESWITCH_INLINE_MODE] = { .type = NLA_U8 },
Roi Dayanf43e9b02016-09-25 13:52:44 +03005161 [DEVLINK_ATTR_ESWITCH_ENCAP_MODE] = { .type = NLA_U8 },
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02005162 [DEVLINK_ATTR_DPIPE_TABLE_NAME] = { .type = NLA_NUL_STRING },
5163 [DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED] = { .type = NLA_U8 },
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01005164 [DEVLINK_ATTR_RESOURCE_ID] = { .type = NLA_U64},
5165 [DEVLINK_ATTR_RESOURCE_SIZE] = { .type = NLA_U64},
Moshe Shemeshe3b7ca12018-07-04 14:30:30 +03005166 [DEVLINK_ATTR_PARAM_NAME] = { .type = NLA_NUL_STRING },
5167 [DEVLINK_ATTR_PARAM_TYPE] = { .type = NLA_U8 },
5168 [DEVLINK_ATTR_PARAM_VALUE_CMODE] = { .type = NLA_U8 },
Alex Veskerd8db7ea52018-07-12 15:13:11 +03005169 [DEVLINK_ATTR_REGION_NAME] = { .type = NLA_NUL_STRING },
Alex Vesker866319b2018-07-12 15:13:13 +03005170 [DEVLINK_ATTR_REGION_SNAPSHOT_ID] = { .type = NLA_U32 },
Eran Ben Elisha7afe335a2019-02-07 11:36:35 +02005171 [DEVLINK_ATTR_HEALTH_REPORTER_NAME] = { .type = NLA_NUL_STRING },
Eran Ben Elishaa1e55ec2019-02-07 11:36:36 +02005172 [DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD] = { .type = NLA_U64 },
5173 [DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER] = { .type = NLA_U8 },
Jakub Kicinski76726cc2019-02-14 13:40:44 -08005174 [DEVLINK_ATTR_FLASH_UPDATE_FILE_NAME] = { .type = NLA_NUL_STRING },
5175 [DEVLINK_ATTR_FLASH_UPDATE_COMPONENT] = { .type = NLA_NUL_STRING },
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01005176};
5177
5178static const struct genl_ops devlink_nl_ops[] = {
5179 {
5180 .cmd = DEVLINK_CMD_GET,
Johannes Bergef6243a2019-04-26 14:07:31 +02005181 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01005182 .doit = devlink_nl_cmd_get_doit,
5183 .dumpit = devlink_nl_cmd_get_dumpit,
Jiri Pirko1fc22572016-04-08 19:12:48 +02005184 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01005185 /* can be retrieved by unprivileged users */
5186 },
5187 {
5188 .cmd = DEVLINK_CMD_PORT_GET,
Johannes Bergef6243a2019-04-26 14:07:31 +02005189 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01005190 .doit = devlink_nl_cmd_port_get_doit,
5191 .dumpit = devlink_nl_cmd_port_get_dumpit,
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01005192 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
5193 /* can be retrieved by unprivileged users */
5194 },
5195 {
5196 .cmd = DEVLINK_CMD_PORT_SET,
Johannes Bergef6243a2019-04-26 14:07:31 +02005197 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01005198 .doit = devlink_nl_cmd_port_set_doit,
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01005199 .flags = GENL_ADMIN_PERM,
5200 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
5201 },
5202 {
5203 .cmd = DEVLINK_CMD_PORT_SPLIT,
Johannes Bergef6243a2019-04-26 14:07:31 +02005204 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01005205 .doit = devlink_nl_cmd_port_split_doit,
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01005206 .flags = GENL_ADMIN_PERM,
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01005207 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
5208 DEVLINK_NL_FLAG_NO_LOCK,
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01005209 },
5210 {
5211 .cmd = DEVLINK_CMD_PORT_UNSPLIT,
Johannes Bergef6243a2019-04-26 14:07:31 +02005212 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01005213 .doit = devlink_nl_cmd_port_unsplit_doit,
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01005214 .flags = GENL_ADMIN_PERM,
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01005215 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
5216 DEVLINK_NL_FLAG_NO_LOCK,
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01005217 },
Jiri Pirkobf797472016-04-14 18:19:13 +02005218 {
5219 .cmd = DEVLINK_CMD_SB_GET,
Johannes Bergef6243a2019-04-26 14:07:31 +02005220 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Jiri Pirkobf797472016-04-14 18:19:13 +02005221 .doit = devlink_nl_cmd_sb_get_doit,
5222 .dumpit = devlink_nl_cmd_sb_get_dumpit,
Jiri Pirkobf797472016-04-14 18:19:13 +02005223 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
5224 DEVLINK_NL_FLAG_NEED_SB,
5225 /* can be retrieved by unprivileged users */
5226 },
5227 {
5228 .cmd = DEVLINK_CMD_SB_POOL_GET,
Johannes Bergef6243a2019-04-26 14:07:31 +02005229 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Jiri Pirkobf797472016-04-14 18:19:13 +02005230 .doit = devlink_nl_cmd_sb_pool_get_doit,
5231 .dumpit = devlink_nl_cmd_sb_pool_get_dumpit,
Jiri Pirkobf797472016-04-14 18:19:13 +02005232 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
5233 DEVLINK_NL_FLAG_NEED_SB,
5234 /* can be retrieved by unprivileged users */
5235 },
5236 {
5237 .cmd = DEVLINK_CMD_SB_POOL_SET,
Johannes Bergef6243a2019-04-26 14:07:31 +02005238 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Jiri Pirkobf797472016-04-14 18:19:13 +02005239 .doit = devlink_nl_cmd_sb_pool_set_doit,
Jiri Pirkobf797472016-04-14 18:19:13 +02005240 .flags = GENL_ADMIN_PERM,
5241 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
5242 DEVLINK_NL_FLAG_NEED_SB,
5243 },
5244 {
5245 .cmd = DEVLINK_CMD_SB_PORT_POOL_GET,
Johannes Bergef6243a2019-04-26 14:07:31 +02005246 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Jiri Pirkobf797472016-04-14 18:19:13 +02005247 .doit = devlink_nl_cmd_sb_port_pool_get_doit,
5248 .dumpit = devlink_nl_cmd_sb_port_pool_get_dumpit,
Jiri Pirkobf797472016-04-14 18:19:13 +02005249 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT |
5250 DEVLINK_NL_FLAG_NEED_SB,
5251 /* can be retrieved by unprivileged users */
5252 },
5253 {
5254 .cmd = DEVLINK_CMD_SB_PORT_POOL_SET,
Johannes Bergef6243a2019-04-26 14:07:31 +02005255 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Jiri Pirkobf797472016-04-14 18:19:13 +02005256 .doit = devlink_nl_cmd_sb_port_pool_set_doit,
Jiri Pirkobf797472016-04-14 18:19:13 +02005257 .flags = GENL_ADMIN_PERM,
5258 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT |
5259 DEVLINK_NL_FLAG_NEED_SB,
5260 },
5261 {
5262 .cmd = DEVLINK_CMD_SB_TC_POOL_BIND_GET,
Johannes Bergef6243a2019-04-26 14:07:31 +02005263 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Jiri Pirkobf797472016-04-14 18:19:13 +02005264 .doit = devlink_nl_cmd_sb_tc_pool_bind_get_doit,
5265 .dumpit = devlink_nl_cmd_sb_tc_pool_bind_get_dumpit,
Jiri Pirkobf797472016-04-14 18:19:13 +02005266 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT |
5267 DEVLINK_NL_FLAG_NEED_SB,
5268 /* can be retrieved by unprivileged users */
5269 },
5270 {
5271 .cmd = DEVLINK_CMD_SB_TC_POOL_BIND_SET,
Johannes Bergef6243a2019-04-26 14:07:31 +02005272 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Jiri Pirkobf797472016-04-14 18:19:13 +02005273 .doit = devlink_nl_cmd_sb_tc_pool_bind_set_doit,
Jiri Pirkobf797472016-04-14 18:19:13 +02005274 .flags = GENL_ADMIN_PERM,
5275 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT |
5276 DEVLINK_NL_FLAG_NEED_SB,
5277 },
Jiri Pirkodf38daf2016-04-14 18:19:14 +02005278 {
5279 .cmd = DEVLINK_CMD_SB_OCC_SNAPSHOT,
Johannes Bergef6243a2019-04-26 14:07:31 +02005280 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Jiri Pirkodf38daf2016-04-14 18:19:14 +02005281 .doit = devlink_nl_cmd_sb_occ_snapshot_doit,
Jiri Pirkodf38daf2016-04-14 18:19:14 +02005282 .flags = GENL_ADMIN_PERM,
5283 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01005284 DEVLINK_NL_FLAG_NEED_SB,
Jiri Pirkodf38daf2016-04-14 18:19:14 +02005285 },
5286 {
5287 .cmd = DEVLINK_CMD_SB_OCC_MAX_CLEAR,
Johannes Bergef6243a2019-04-26 14:07:31 +02005288 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Jiri Pirkodf38daf2016-04-14 18:19:14 +02005289 .doit = devlink_nl_cmd_sb_occ_max_clear_doit,
Jiri Pirkodf38daf2016-04-14 18:19:14 +02005290 .flags = GENL_ADMIN_PERM,
5291 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01005292 DEVLINK_NL_FLAG_NEED_SB,
Jiri Pirkodf38daf2016-04-14 18:19:14 +02005293 },
Or Gerlitz08f4b592016-07-01 14:51:01 +03005294 {
Jiri Pirkoadf200f2017-02-09 15:54:33 +01005295 .cmd = DEVLINK_CMD_ESWITCH_GET,
Johannes Bergef6243a2019-04-26 14:07:31 +02005296 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Jiri Pirkoadf200f2017-02-09 15:54:33 +01005297 .doit = devlink_nl_cmd_eswitch_get_doit,
Or Gerlitz08f4b592016-07-01 14:51:01 +03005298 .flags = GENL_ADMIN_PERM,
5299 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
5300 },
5301 {
Jiri Pirkoadf200f2017-02-09 15:54:33 +01005302 .cmd = DEVLINK_CMD_ESWITCH_SET,
Johannes Bergef6243a2019-04-26 14:07:31 +02005303 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Jiri Pirkoadf200f2017-02-09 15:54:33 +01005304 .doit = devlink_nl_cmd_eswitch_set_doit,
Or Gerlitz08f4b592016-07-01 14:51:01 +03005305 .flags = GENL_ADMIN_PERM,
Jakub Kicinski7ac1cc92018-05-21 22:12:50 -07005306 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
5307 DEVLINK_NL_FLAG_NO_LOCK,
Or Gerlitz08f4b592016-07-01 14:51:01 +03005308 },
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02005309 {
5310 .cmd = DEVLINK_CMD_DPIPE_TABLE_GET,
Johannes Bergef6243a2019-04-26 14:07:31 +02005311 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02005312 .doit = devlink_nl_cmd_dpipe_table_get,
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02005313 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
Arkadi Sharshevsky67ae6862018-03-08 12:52:25 +02005314 /* can be retrieved by unprivileged users */
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02005315 },
5316 {
5317 .cmd = DEVLINK_CMD_DPIPE_ENTRIES_GET,
Johannes Bergef6243a2019-04-26 14:07:31 +02005318 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02005319 .doit = devlink_nl_cmd_dpipe_entries_get,
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02005320 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
Arkadi Sharshevsky67ae6862018-03-08 12:52:25 +02005321 /* can be retrieved by unprivileged users */
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02005322 },
5323 {
5324 .cmd = DEVLINK_CMD_DPIPE_HEADERS_GET,
Johannes Bergef6243a2019-04-26 14:07:31 +02005325 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02005326 .doit = devlink_nl_cmd_dpipe_headers_get,
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02005327 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
Arkadi Sharshevsky67ae6862018-03-08 12:52:25 +02005328 /* can be retrieved by unprivileged users */
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02005329 },
5330 {
5331 .cmd = DEVLINK_CMD_DPIPE_TABLE_COUNTERS_SET,
Johannes Bergef6243a2019-04-26 14:07:31 +02005332 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02005333 .doit = devlink_nl_cmd_dpipe_table_counters_set,
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02005334 .flags = GENL_ADMIN_PERM,
5335 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
5336 },
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01005337 {
5338 .cmd = DEVLINK_CMD_RESOURCE_SET,
Johannes Bergef6243a2019-04-26 14:07:31 +02005339 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01005340 .doit = devlink_nl_cmd_resource_set,
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01005341 .flags = GENL_ADMIN_PERM,
5342 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
5343 },
5344 {
5345 .cmd = DEVLINK_CMD_RESOURCE_DUMP,
Johannes Bergef6243a2019-04-26 14:07:31 +02005346 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01005347 .doit = devlink_nl_cmd_resource_dump,
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01005348 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
Arkadi Sharshevsky67ae6862018-03-08 12:52:25 +02005349 /* can be retrieved by unprivileged users */
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01005350 },
Arkadi Sharshevsky2d8dc5b2018-01-15 08:59:04 +01005351 {
5352 .cmd = DEVLINK_CMD_RELOAD,
Johannes Bergef6243a2019-04-26 14:07:31 +02005353 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Arkadi Sharshevsky2d8dc5b2018-01-15 08:59:04 +01005354 .doit = devlink_nl_cmd_reload,
Arkadi Sharshevsky2d8dc5b2018-01-15 08:59:04 +01005355 .flags = GENL_ADMIN_PERM,
5356 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
5357 DEVLINK_NL_FLAG_NO_LOCK,
5358 },
Moshe Shemesh45f05de2018-07-04 14:30:29 +03005359 {
5360 .cmd = DEVLINK_CMD_PARAM_GET,
Johannes Bergef6243a2019-04-26 14:07:31 +02005361 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Moshe Shemesh45f05de2018-07-04 14:30:29 +03005362 .doit = devlink_nl_cmd_param_get_doit,
5363 .dumpit = devlink_nl_cmd_param_get_dumpit,
Moshe Shemesh45f05de2018-07-04 14:30:29 +03005364 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
5365 /* can be retrieved by unprivileged users */
5366 },
Moshe Shemeshe3b7ca12018-07-04 14:30:30 +03005367 {
5368 .cmd = DEVLINK_CMD_PARAM_SET,
Johannes Bergef6243a2019-04-26 14:07:31 +02005369 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Moshe Shemeshe3b7ca12018-07-04 14:30:30 +03005370 .doit = devlink_nl_cmd_param_set_doit,
Moshe Shemeshe3b7ca12018-07-04 14:30:30 +03005371 .flags = GENL_ADMIN_PERM,
5372 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
5373 },
Alex Veskerd8db7ea52018-07-12 15:13:11 +03005374 {
Vasundhara Volamf4601de2019-01-28 18:00:21 +05305375 .cmd = DEVLINK_CMD_PORT_PARAM_GET,
Johannes Bergef6243a2019-04-26 14:07:31 +02005376 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Vasundhara Volamf4601de2019-01-28 18:00:21 +05305377 .doit = devlink_nl_cmd_port_param_get_doit,
5378 .dumpit = devlink_nl_cmd_port_param_get_dumpit,
Vasundhara Volamf4601de2019-01-28 18:00:21 +05305379 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
5380 /* can be retrieved by unprivileged users */
5381 },
5382 {
Vasundhara Volam9c548732019-01-28 18:00:22 +05305383 .cmd = DEVLINK_CMD_PORT_PARAM_SET,
Johannes Bergef6243a2019-04-26 14:07:31 +02005384 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Vasundhara Volam9c548732019-01-28 18:00:22 +05305385 .doit = devlink_nl_cmd_port_param_set_doit,
Vasundhara Volam9c548732019-01-28 18:00:22 +05305386 .flags = GENL_ADMIN_PERM,
5387 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
5388 },
5389 {
Alex Veskerd8db7ea52018-07-12 15:13:11 +03005390 .cmd = DEVLINK_CMD_REGION_GET,
Johannes Bergef6243a2019-04-26 14:07:31 +02005391 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Alex Veskerd8db7ea52018-07-12 15:13:11 +03005392 .doit = devlink_nl_cmd_region_get_doit,
5393 .dumpit = devlink_nl_cmd_region_get_dumpit,
Alex Veskerd8db7ea52018-07-12 15:13:11 +03005394 .flags = GENL_ADMIN_PERM,
5395 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
5396 },
Alex Vesker866319b2018-07-12 15:13:13 +03005397 {
5398 .cmd = DEVLINK_CMD_REGION_DEL,
Johannes Bergef6243a2019-04-26 14:07:31 +02005399 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Alex Vesker866319b2018-07-12 15:13:13 +03005400 .doit = devlink_nl_cmd_region_del,
Alex Vesker866319b2018-07-12 15:13:13 +03005401 .flags = GENL_ADMIN_PERM,
5402 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
5403 },
Alex Vesker4e547952018-07-12 15:13:14 +03005404 {
5405 .cmd = DEVLINK_CMD_REGION_READ,
Johannes Bergef6243a2019-04-26 14:07:31 +02005406 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Alex Vesker4e547952018-07-12 15:13:14 +03005407 .dumpit = devlink_nl_cmd_region_read_dumpit,
Alex Vesker4e547952018-07-12 15:13:14 +03005408 .flags = GENL_ADMIN_PERM,
5409 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
5410 },
Jakub Kicinskif9cf2282019-01-31 10:50:40 -08005411 {
5412 .cmd = DEVLINK_CMD_INFO_GET,
Johannes Bergef6243a2019-04-26 14:07:31 +02005413 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Jakub Kicinskif9cf2282019-01-31 10:50:40 -08005414 .doit = devlink_nl_cmd_info_get_doit,
5415 .dumpit = devlink_nl_cmd_info_get_dumpit,
Jakub Kicinskif9cf2282019-01-31 10:50:40 -08005416 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
5417 /* can be retrieved by unprivileged users */
5418 },
Eran Ben Elisha7afe335a2019-02-07 11:36:35 +02005419 {
5420 .cmd = DEVLINK_CMD_HEALTH_REPORTER_GET,
Johannes Bergef6243a2019-04-26 14:07:31 +02005421 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Eran Ben Elisha7afe335a2019-02-07 11:36:35 +02005422 .doit = devlink_nl_cmd_health_reporter_get_doit,
5423 .dumpit = devlink_nl_cmd_health_reporter_get_dumpit,
Moshe Shemeshb587bda2019-04-29 12:41:45 +03005424 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
5425 DEVLINK_NL_FLAG_NO_LOCK,
Eran Ben Elisha7afe335a2019-02-07 11:36:35 +02005426 /* can be retrieved by unprivileged users */
5427 },
Eran Ben Elishaa1e55ec2019-02-07 11:36:36 +02005428 {
5429 .cmd = DEVLINK_CMD_HEALTH_REPORTER_SET,
Johannes Bergef6243a2019-04-26 14:07:31 +02005430 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Eran Ben Elishaa1e55ec2019-02-07 11:36:36 +02005431 .doit = devlink_nl_cmd_health_reporter_set_doit,
Eran Ben Elishaa1e55ec2019-02-07 11:36:36 +02005432 .flags = GENL_ADMIN_PERM,
Moshe Shemeshb587bda2019-04-29 12:41:45 +03005433 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
5434 DEVLINK_NL_FLAG_NO_LOCK,
Eran Ben Elishaa1e55ec2019-02-07 11:36:36 +02005435 },
Eran Ben Elisha20a09432019-02-07 11:36:37 +02005436 {
5437 .cmd = DEVLINK_CMD_HEALTH_REPORTER_RECOVER,
Johannes Bergef6243a2019-04-26 14:07:31 +02005438 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Eran Ben Elisha20a09432019-02-07 11:36:37 +02005439 .doit = devlink_nl_cmd_health_reporter_recover_doit,
Eran Ben Elisha20a09432019-02-07 11:36:37 +02005440 .flags = GENL_ADMIN_PERM,
Moshe Shemeshb587bda2019-04-29 12:41:45 +03005441 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
5442 DEVLINK_NL_FLAG_NO_LOCK,
Eran Ben Elisha20a09432019-02-07 11:36:37 +02005443 },
Eran Ben Elishafca42a22019-02-07 11:36:38 +02005444 {
5445 .cmd = DEVLINK_CMD_HEALTH_REPORTER_DIAGNOSE,
Johannes Bergef6243a2019-04-26 14:07:31 +02005446 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Eran Ben Elishafca42a22019-02-07 11:36:38 +02005447 .doit = devlink_nl_cmd_health_reporter_diagnose_doit,
Eran Ben Elishafca42a22019-02-07 11:36:38 +02005448 .flags = GENL_ADMIN_PERM,
Moshe Shemeshb587bda2019-04-29 12:41:45 +03005449 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
5450 DEVLINK_NL_FLAG_NO_LOCK,
Eran Ben Elishafca42a22019-02-07 11:36:38 +02005451 },
Eran Ben Elisha35455e22019-02-07 11:36:39 +02005452 {
5453 .cmd = DEVLINK_CMD_HEALTH_REPORTER_DUMP_GET,
Johannes Bergef6243a2019-04-26 14:07:31 +02005454 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Aya Levine44ef4e2019-05-16 09:49:20 +03005455 .dumpit = devlink_nl_cmd_health_reporter_dump_get_dumpit,
Eran Ben Elisha35455e22019-02-07 11:36:39 +02005456 .flags = GENL_ADMIN_PERM,
5457 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
5458 DEVLINK_NL_FLAG_NO_LOCK,
5459 },
5460 {
5461 .cmd = DEVLINK_CMD_HEALTH_REPORTER_DUMP_CLEAR,
Johannes Bergef6243a2019-04-26 14:07:31 +02005462 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Eran Ben Elisha35455e22019-02-07 11:36:39 +02005463 .doit = devlink_nl_cmd_health_reporter_dump_clear_doit,
Eran Ben Elisha35455e22019-02-07 11:36:39 +02005464 .flags = GENL_ADMIN_PERM,
5465 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
5466 DEVLINK_NL_FLAG_NO_LOCK,
5467 },
Jakub Kicinski76726cc2019-02-14 13:40:44 -08005468 {
5469 .cmd = DEVLINK_CMD_FLASH_UPDATE,
Johannes Bergef6243a2019-04-26 14:07:31 +02005470 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Jakub Kicinski76726cc2019-02-14 13:40:44 -08005471 .doit = devlink_nl_cmd_flash_update,
Jakub Kicinski76726cc2019-02-14 13:40:44 -08005472 .flags = GENL_ADMIN_PERM,
5473 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
5474 },
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01005475};
5476
Johannes Berg56989f62016-10-24 14:40:05 +02005477static struct genl_family devlink_nl_family __ro_after_init = {
Johannes Berg489111e2016-10-24 14:40:03 +02005478 .name = DEVLINK_GENL_NAME,
5479 .version = DEVLINK_GENL_VERSION,
5480 .maxattr = DEVLINK_ATTR_MAX,
Johannes Berg3b0f31f2019-03-21 22:51:02 +01005481 .policy = devlink_nl_policy,
Johannes Berg489111e2016-10-24 14:40:03 +02005482 .netnsok = true,
5483 .pre_doit = devlink_nl_pre_doit,
5484 .post_doit = devlink_nl_post_doit,
5485 .module = THIS_MODULE,
5486 .ops = devlink_nl_ops,
5487 .n_ops = ARRAY_SIZE(devlink_nl_ops),
5488 .mcgrps = devlink_nl_mcgrps,
5489 .n_mcgrps = ARRAY_SIZE(devlink_nl_mcgrps),
5490};
5491
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01005492/**
5493 * devlink_alloc - Allocate new devlink instance resources
5494 *
5495 * @ops: ops
5496 * @priv_size: size of user private data
5497 *
5498 * Allocate new devlink instance resources, including devlink index
5499 * and name.
5500 */
5501struct devlink *devlink_alloc(const struct devlink_ops *ops, size_t priv_size)
5502{
5503 struct devlink *devlink;
5504
Jakub Kicinskibe6fe1d2019-02-25 19:34:07 -08005505 if (WARN_ON(!ops))
5506 return NULL;
5507
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01005508 devlink = kzalloc(sizeof(*devlink) + priv_size, GFP_KERNEL);
5509 if (!devlink)
5510 return NULL;
5511 devlink->ops = ops;
5512 devlink_net_set(devlink, &init_net);
5513 INIT_LIST_HEAD(&devlink->port_list);
Jiri Pirkobf797472016-04-14 18:19:13 +02005514 INIT_LIST_HEAD(&devlink->sb_list);
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02005515 INIT_LIST_HEAD_RCU(&devlink->dpipe_table_list);
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01005516 INIT_LIST_HEAD(&devlink->resource_list);
Moshe Shemesheabaef12018-07-04 14:30:28 +03005517 INIT_LIST_HEAD(&devlink->param_list);
Alex Veskerb16ebe92018-07-12 15:13:08 +03005518 INIT_LIST_HEAD(&devlink->region_list);
Eran Ben Elishaa0bdcc52019-02-07 11:36:33 +02005519 INIT_LIST_HEAD(&devlink->reporter_list);
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01005520 mutex_init(&devlink->lock);
Moshe Shemeshb587bda2019-04-29 12:41:45 +03005521 mutex_init(&devlink->reporters_lock);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01005522 return devlink;
5523}
5524EXPORT_SYMBOL_GPL(devlink_alloc);
5525
5526/**
5527 * devlink_register - Register devlink instance
5528 *
5529 * @devlink: devlink
Jakub Kicinskieeaadd82019-02-27 11:36:36 -08005530 * @dev: parent device
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01005531 */
5532int devlink_register(struct devlink *devlink, struct device *dev)
5533{
5534 mutex_lock(&devlink_mutex);
5535 devlink->dev = dev;
5536 list_add_tail(&devlink->list, &devlink_list);
5537 devlink_notify(devlink, DEVLINK_CMD_NEW);
5538 mutex_unlock(&devlink_mutex);
5539 return 0;
5540}
5541EXPORT_SYMBOL_GPL(devlink_register);
5542
5543/**
5544 * devlink_unregister - Unregister devlink instance
5545 *
5546 * @devlink: devlink
5547 */
5548void devlink_unregister(struct devlink *devlink)
5549{
5550 mutex_lock(&devlink_mutex);
5551 devlink_notify(devlink, DEVLINK_CMD_DEL);
5552 list_del(&devlink->list);
5553 mutex_unlock(&devlink_mutex);
5554}
5555EXPORT_SYMBOL_GPL(devlink_unregister);
5556
5557/**
5558 * devlink_free - Free devlink instance resources
5559 *
5560 * @devlink: devlink
5561 */
5562void devlink_free(struct devlink *devlink)
5563{
Moshe Shemeshb587bda2019-04-29 12:41:45 +03005564 mutex_destroy(&devlink->reporters_lock);
Jiri Pirko375cf8c2019-03-24 11:14:24 +01005565 mutex_destroy(&devlink->lock);
Parav Panditb904aad2019-02-08 15:15:00 -06005566 WARN_ON(!list_empty(&devlink->reporter_list));
5567 WARN_ON(!list_empty(&devlink->region_list));
5568 WARN_ON(!list_empty(&devlink->param_list));
5569 WARN_ON(!list_empty(&devlink->resource_list));
5570 WARN_ON(!list_empty(&devlink->dpipe_table_list));
5571 WARN_ON(!list_empty(&devlink->sb_list));
5572 WARN_ON(!list_empty(&devlink->port_list));
5573
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01005574 kfree(devlink);
5575}
5576EXPORT_SYMBOL_GPL(devlink_free);
5577
Jiri Pirko136bf272019-05-23 10:43:35 +02005578static void devlink_port_type_warn(struct work_struct *work)
5579{
5580 WARN(true, "Type was not set for devlink port.");
5581}
5582
5583static bool devlink_port_type_should_warn(struct devlink_port *devlink_port)
5584{
5585 /* Ignore CPU and DSA flavours. */
5586 return devlink_port->attrs.flavour != DEVLINK_PORT_FLAVOUR_CPU &&
5587 devlink_port->attrs.flavour != DEVLINK_PORT_FLAVOUR_DSA;
5588}
5589
5590#define DEVLINK_PORT_TYPE_WARN_TIMEOUT (HZ * 30)
5591
5592static void devlink_port_type_warn_schedule(struct devlink_port *devlink_port)
5593{
5594 if (!devlink_port_type_should_warn(devlink_port))
5595 return;
5596 /* Schedule a work to WARN in case driver does not set port
5597 * type within timeout.
5598 */
5599 schedule_delayed_work(&devlink_port->type_warn_dw,
5600 DEVLINK_PORT_TYPE_WARN_TIMEOUT);
5601}
5602
5603static void devlink_port_type_warn_cancel(struct devlink_port *devlink_port)
5604{
5605 if (!devlink_port_type_should_warn(devlink_port))
5606 return;
5607 cancel_delayed_work_sync(&devlink_port->type_warn_dw);
5608}
5609
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01005610/**
5611 * devlink_port_register - Register devlink port
5612 *
5613 * @devlink: devlink
5614 * @devlink_port: devlink port
Jakub Kicinskieeaadd82019-02-27 11:36:36 -08005615 * @port_index: driver-specific numerical identifier of the port
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01005616 *
5617 * Register devlink port with provided port index. User can use
5618 * any indexing, even hw-related one. devlink_port structure
5619 * is convenient to be embedded inside user driver private structure.
5620 * Note that the caller should take care of zeroing the devlink_port
5621 * structure.
5622 */
5623int devlink_port_register(struct devlink *devlink,
5624 struct devlink_port *devlink_port,
5625 unsigned int port_index)
5626{
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01005627 mutex_lock(&devlink->lock);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01005628 if (devlink_port_index_exists(devlink, port_index)) {
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01005629 mutex_unlock(&devlink->lock);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01005630 return -EEXIST;
5631 }
5632 devlink_port->devlink = devlink;
5633 devlink_port->index = port_index;
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01005634 devlink_port->registered = true;
Jiri Pirkob8f97552019-03-24 11:14:37 +01005635 spin_lock_init(&devlink_port->type_lock);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01005636 list_add_tail(&devlink_port->list, &devlink->port_list);
Vasundhara Volam39e61602019-01-28 18:00:20 +05305637 INIT_LIST_HEAD(&devlink_port->param_list);
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01005638 mutex_unlock(&devlink->lock);
Jiri Pirko136bf272019-05-23 10:43:35 +02005639 INIT_DELAYED_WORK(&devlink_port->type_warn_dw, &devlink_port_type_warn);
5640 devlink_port_type_warn_schedule(devlink_port);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01005641 devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW);
5642 return 0;
5643}
5644EXPORT_SYMBOL_GPL(devlink_port_register);
5645
5646/**
5647 * devlink_port_unregister - Unregister devlink port
5648 *
5649 * @devlink_port: devlink port
5650 */
5651void devlink_port_unregister(struct devlink_port *devlink_port)
5652{
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01005653 struct devlink *devlink = devlink_port->devlink;
5654
Jiri Pirko136bf272019-05-23 10:43:35 +02005655 devlink_port_type_warn_cancel(devlink_port);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01005656 devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_DEL);
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01005657 mutex_lock(&devlink->lock);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01005658 list_del(&devlink_port->list);
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01005659 mutex_unlock(&devlink->lock);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01005660}
5661EXPORT_SYMBOL_GPL(devlink_port_unregister);
5662
5663static void __devlink_port_type_set(struct devlink_port *devlink_port,
5664 enum devlink_port_type type,
5665 void *type_dev)
5666{
Jiri Pirko2b239e72019-03-24 11:14:36 +01005667 if (WARN_ON(!devlink_port->registered))
5668 return;
Jiri Pirko136bf272019-05-23 10:43:35 +02005669 devlink_port_type_warn_cancel(devlink_port);
Jiri Pirkob8f97552019-03-24 11:14:37 +01005670 spin_lock(&devlink_port->type_lock);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01005671 devlink_port->type = type;
5672 devlink_port->type_dev = type_dev;
Jiri Pirkob8f97552019-03-24 11:14:37 +01005673 spin_unlock(&devlink_port->type_lock);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01005674 devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW);
5675}
5676
5677/**
5678 * devlink_port_type_eth_set - Set port type to Ethernet
5679 *
5680 * @devlink_port: devlink port
5681 * @netdev: related netdevice
5682 */
5683void devlink_port_type_eth_set(struct devlink_port *devlink_port,
5684 struct net_device *netdev)
5685{
Jiri Pirko119c0b52019-04-03 14:24:27 +02005686 const struct net_device_ops *ops = netdev->netdev_ops;
5687
Jiri Pirko746364f2019-03-28 13:56:46 +01005688 /* If driver registers devlink port, it should set devlink port
5689 * attributes accordingly so the compat functions are called
5690 * and the original ops are not used.
5691 */
Jiri Pirko119c0b52019-04-03 14:24:27 +02005692 if (ops->ndo_get_phys_port_name) {
Jiri Pirko746364f2019-03-28 13:56:46 +01005693 /* Some drivers use the same set of ndos for netdevs
5694 * that have devlink_port registered and also for
5695 * those who don't. Make sure that ndo_get_phys_port_name
5696 * returns -EOPNOTSUPP here in case it is defined.
5697 * Warn if not.
5698 */
Jiri Pirko746364f2019-03-28 13:56:46 +01005699 char name[IFNAMSIZ];
5700 int err;
5701
5702 err = ops->ndo_get_phys_port_name(netdev, name, sizeof(name));
5703 WARN_ON(err != -EOPNOTSUPP);
5704 }
Jiri Pirko119c0b52019-04-03 14:24:27 +02005705 if (ops->ndo_get_port_parent_id) {
5706 /* Some drivers use the same set of ndos for netdevs
5707 * that have devlink_port registered and also for
5708 * those who don't. Make sure that ndo_get_port_parent_id
5709 * returns -EOPNOTSUPP here in case it is defined.
5710 * Warn if not.
5711 */
5712 struct netdev_phys_item_id ppid;
5713 int err;
5714
5715 err = ops->ndo_get_port_parent_id(netdev, &ppid);
5716 WARN_ON(err != -EOPNOTSUPP);
5717 }
Jiri Pirko773b1f32019-03-24 11:14:30 +01005718 __devlink_port_type_set(devlink_port, DEVLINK_PORT_TYPE_ETH, netdev);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01005719}
5720EXPORT_SYMBOL_GPL(devlink_port_type_eth_set);
5721
5722/**
5723 * devlink_port_type_ib_set - Set port type to InfiniBand
5724 *
5725 * @devlink_port: devlink port
5726 * @ibdev: related IB device
5727 */
5728void devlink_port_type_ib_set(struct devlink_port *devlink_port,
5729 struct ib_device *ibdev)
5730{
Jiri Pirko773b1f32019-03-24 11:14:30 +01005731 __devlink_port_type_set(devlink_port, DEVLINK_PORT_TYPE_IB, ibdev);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01005732}
5733EXPORT_SYMBOL_GPL(devlink_port_type_ib_set);
5734
5735/**
5736 * devlink_port_type_clear - Clear port type
5737 *
5738 * @devlink_port: devlink port
5739 */
5740void devlink_port_type_clear(struct devlink_port *devlink_port)
5741{
Jiri Pirko773b1f32019-03-24 11:14:30 +01005742 __devlink_port_type_set(devlink_port, DEVLINK_PORT_TYPE_NOTSET, NULL);
Jiri Pirko136bf272019-05-23 10:43:35 +02005743 devlink_port_type_warn_schedule(devlink_port);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01005744}
5745EXPORT_SYMBOL_GPL(devlink_port_type_clear);
5746
Parav Pandit378ef012019-07-08 23:17:35 -05005747static int __devlink_port_attrs_set(struct devlink_port *devlink_port,
5748 enum devlink_port_flavour flavour,
5749 const unsigned char *switch_id,
5750 unsigned char switch_id_len)
5751{
5752 struct devlink_port_attrs *attrs = &devlink_port->attrs;
5753
5754 if (WARN_ON(devlink_port->registered))
5755 return -EEXIST;
5756 attrs->set = true;
5757 attrs->flavour = flavour;
5758 if (switch_id) {
5759 attrs->switch_port = true;
5760 if (WARN_ON(switch_id_len > MAX_PHYS_ITEM_ID_LEN))
5761 switch_id_len = MAX_PHYS_ITEM_ID_LEN;
5762 memcpy(attrs->switch_id.id, switch_id, switch_id_len);
5763 attrs->switch_id.id_len = switch_id_len;
5764 } else {
5765 attrs->switch_port = false;
5766 }
5767 return 0;
5768}
5769
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01005770/**
Jiri Pirkob9ffcba2018-05-18 09:29:00 +02005771 * devlink_port_attrs_set - Set port attributes
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01005772 *
5773 * @devlink_port: devlink port
Jiri Pirko5ec13802018-05-18 09:29:01 +02005774 * @flavour: flavour of the port
Jiri Pirkob9ffcba2018-05-18 09:29:00 +02005775 * @port_number: number of the port that is facing user, for example
5776 * the front panel port number
5777 * @split: indicates if this is split port
5778 * @split_subport_number: if the port is split, this is the number
5779 * of subport.
Jiri Pirkobec52672019-04-03 14:24:16 +02005780 * @switch_id: if the port is part of switch, this is buffer with ID,
5781 * otwerwise this is NULL
5782 * @switch_id_len: length of the switch_id buffer
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01005783 */
Jiri Pirkob9ffcba2018-05-18 09:29:00 +02005784void devlink_port_attrs_set(struct devlink_port *devlink_port,
Jiri Pirko5ec13802018-05-18 09:29:01 +02005785 enum devlink_port_flavour flavour,
Jiri Pirkob9ffcba2018-05-18 09:29:00 +02005786 u32 port_number, bool split,
Jiri Pirkobec52672019-04-03 14:24:16 +02005787 u32 split_subport_number,
5788 const unsigned char *switch_id,
5789 unsigned char switch_id_len)
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01005790{
Jiri Pirkob9ffcba2018-05-18 09:29:00 +02005791 struct devlink_port_attrs *attrs = &devlink_port->attrs;
Parav Pandit378ef012019-07-08 23:17:35 -05005792 int ret;
Jiri Pirkob9ffcba2018-05-18 09:29:00 +02005793
Parav Pandit378ef012019-07-08 23:17:35 -05005794 ret = __devlink_port_attrs_set(devlink_port, flavour,
5795 switch_id, switch_id_len);
5796 if (ret)
Jiri Pirko45b86112019-03-24 11:14:33 +01005797 return;
Jiri Pirkob9ffcba2018-05-18 09:29:00 +02005798 attrs->split = split;
Parav Pandit378ef012019-07-08 23:17:35 -05005799 attrs->phys.port_number = port_number;
5800 attrs->phys.split_subport_number = split_subport_number;
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01005801}
Jiri Pirkob9ffcba2018-05-18 09:29:00 +02005802EXPORT_SYMBOL_GPL(devlink_port_attrs_set);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01005803
Jiri Pirkoaf3836d2019-03-28 13:56:37 +01005804static int __devlink_port_phys_port_name_get(struct devlink_port *devlink_port,
5805 char *name, size_t len)
Jiri Pirko08474c12018-05-18 09:29:02 +02005806{
5807 struct devlink_port_attrs *attrs = &devlink_port->attrs;
5808 int n = 0;
5809
5810 if (!attrs->set)
5811 return -EOPNOTSUPP;
5812
5813 switch (attrs->flavour) {
5814 case DEVLINK_PORT_FLAVOUR_PHYSICAL:
5815 if (!attrs->split)
Parav Pandit378ef012019-07-08 23:17:35 -05005816 n = snprintf(name, len, "p%u", attrs->phys.port_number);
Jiri Pirko08474c12018-05-18 09:29:02 +02005817 else
Parav Pandit378ef012019-07-08 23:17:35 -05005818 n = snprintf(name, len, "p%us%u",
5819 attrs->phys.port_number,
5820 attrs->phys.split_subport_number);
Jiri Pirko08474c12018-05-18 09:29:02 +02005821 break;
5822 case DEVLINK_PORT_FLAVOUR_CPU:
5823 case DEVLINK_PORT_FLAVOUR_DSA:
5824 /* As CPU and DSA ports do not have a netdevice associated
5825 * case should not ever happen.
5826 */
5827 WARN_ON(1);
5828 return -EINVAL;
5829 }
5830
5831 if (n >= len)
5832 return -EINVAL;
5833
5834 return 0;
5835}
Jiri Pirkoaf3836d2019-03-28 13:56:37 +01005836
Jiri Pirkobf797472016-04-14 18:19:13 +02005837int devlink_sb_register(struct devlink *devlink, unsigned int sb_index,
5838 u32 size, u16 ingress_pools_count,
5839 u16 egress_pools_count, u16 ingress_tc_count,
5840 u16 egress_tc_count)
5841{
5842 struct devlink_sb *devlink_sb;
5843 int err = 0;
5844
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01005845 mutex_lock(&devlink->lock);
Jiri Pirkobf797472016-04-14 18:19:13 +02005846 if (devlink_sb_index_exists(devlink, sb_index)) {
5847 err = -EEXIST;
5848 goto unlock;
5849 }
5850
5851 devlink_sb = kzalloc(sizeof(*devlink_sb), GFP_KERNEL);
5852 if (!devlink_sb) {
5853 err = -ENOMEM;
5854 goto unlock;
5855 }
5856 devlink_sb->index = sb_index;
5857 devlink_sb->size = size;
5858 devlink_sb->ingress_pools_count = ingress_pools_count;
5859 devlink_sb->egress_pools_count = egress_pools_count;
5860 devlink_sb->ingress_tc_count = ingress_tc_count;
5861 devlink_sb->egress_tc_count = egress_tc_count;
5862 list_add_tail(&devlink_sb->list, &devlink->sb_list);
5863unlock:
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01005864 mutex_unlock(&devlink->lock);
Jiri Pirkobf797472016-04-14 18:19:13 +02005865 return err;
5866}
5867EXPORT_SYMBOL_GPL(devlink_sb_register);
5868
5869void devlink_sb_unregister(struct devlink *devlink, unsigned int sb_index)
5870{
5871 struct devlink_sb *devlink_sb;
5872
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01005873 mutex_lock(&devlink->lock);
Jiri Pirkobf797472016-04-14 18:19:13 +02005874 devlink_sb = devlink_sb_get_by_index(devlink, sb_index);
5875 WARN_ON(!devlink_sb);
5876 list_del(&devlink_sb->list);
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01005877 mutex_unlock(&devlink->lock);
Jiri Pirkobf797472016-04-14 18:19:13 +02005878 kfree(devlink_sb);
5879}
5880EXPORT_SYMBOL_GPL(devlink_sb_unregister);
5881
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02005882/**
5883 * devlink_dpipe_headers_register - register dpipe headers
5884 *
5885 * @devlink: devlink
5886 * @dpipe_headers: dpipe header array
5887 *
5888 * Register the headers supported by hardware.
5889 */
5890int devlink_dpipe_headers_register(struct devlink *devlink,
5891 struct devlink_dpipe_headers *dpipe_headers)
5892{
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01005893 mutex_lock(&devlink->lock);
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02005894 devlink->dpipe_headers = dpipe_headers;
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01005895 mutex_unlock(&devlink->lock);
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02005896 return 0;
5897}
5898EXPORT_SYMBOL_GPL(devlink_dpipe_headers_register);
5899
5900/**
5901 * devlink_dpipe_headers_unregister - unregister dpipe headers
5902 *
5903 * @devlink: devlink
5904 *
5905 * Unregister the headers supported by hardware.
5906 */
5907void devlink_dpipe_headers_unregister(struct devlink *devlink)
5908{
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01005909 mutex_lock(&devlink->lock);
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02005910 devlink->dpipe_headers = NULL;
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01005911 mutex_unlock(&devlink->lock);
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02005912}
5913EXPORT_SYMBOL_GPL(devlink_dpipe_headers_unregister);
5914
5915/**
5916 * devlink_dpipe_table_counter_enabled - check if counter allocation
5917 * required
5918 * @devlink: devlink
5919 * @table_name: tables name
5920 *
5921 * Used by driver to check if counter allocation is required.
5922 * After counter allocation is turned on the table entries
5923 * are updated to include counter statistics.
5924 *
5925 * After that point on the driver must respect the counter
5926 * state so that each entry added to the table is added
5927 * with a counter.
5928 */
5929bool devlink_dpipe_table_counter_enabled(struct devlink *devlink,
5930 const char *table_name)
5931{
5932 struct devlink_dpipe_table *table;
5933 bool enabled;
5934
5935 rcu_read_lock();
5936 table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
5937 table_name);
5938 enabled = false;
5939 if (table)
5940 enabled = table->counters_enabled;
5941 rcu_read_unlock();
5942 return enabled;
5943}
5944EXPORT_SYMBOL_GPL(devlink_dpipe_table_counter_enabled);
5945
5946/**
5947 * devlink_dpipe_table_register - register dpipe table
5948 *
5949 * @devlink: devlink
5950 * @table_name: table name
5951 * @table_ops: table ops
5952 * @priv: priv
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02005953 * @counter_control_extern: external control for counters
5954 */
5955int devlink_dpipe_table_register(struct devlink *devlink,
5956 const char *table_name,
5957 struct devlink_dpipe_table_ops *table_ops,
Arkadi Sharshevskyffd3cdc2017-08-24 08:40:02 +02005958 void *priv, bool counter_control_extern)
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02005959{
5960 struct devlink_dpipe_table *table;
5961
5962 if (devlink_dpipe_table_find(&devlink->dpipe_table_list, table_name))
5963 return -EEXIST;
5964
Arkadi Sharshevskyffd3cdc2017-08-24 08:40:02 +02005965 if (WARN_ON(!table_ops->size_get))
5966 return -EINVAL;
5967
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02005968 table = kzalloc(sizeof(*table), GFP_KERNEL);
5969 if (!table)
5970 return -ENOMEM;
5971
5972 table->name = table_name;
5973 table->table_ops = table_ops;
5974 table->priv = priv;
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02005975 table->counter_control_extern = counter_control_extern;
5976
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01005977 mutex_lock(&devlink->lock);
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02005978 list_add_tail_rcu(&table->list, &devlink->dpipe_table_list);
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01005979 mutex_unlock(&devlink->lock);
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02005980 return 0;
5981}
5982EXPORT_SYMBOL_GPL(devlink_dpipe_table_register);
5983
5984/**
5985 * devlink_dpipe_table_unregister - unregister dpipe table
5986 *
5987 * @devlink: devlink
5988 * @table_name: table name
5989 */
5990void devlink_dpipe_table_unregister(struct devlink *devlink,
5991 const char *table_name)
5992{
5993 struct devlink_dpipe_table *table;
5994
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01005995 mutex_lock(&devlink->lock);
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02005996 table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
5997 table_name);
5998 if (!table)
5999 goto unlock;
6000 list_del_rcu(&table->list);
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01006001 mutex_unlock(&devlink->lock);
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02006002 kfree_rcu(table, rcu);
6003 return;
6004unlock:
Arkadi Sharshevsky2406e7e2018-01-15 08:59:02 +01006005 mutex_unlock(&devlink->lock);
Arkadi Sharshevsky1555d202017-03-28 17:24:10 +02006006}
6007EXPORT_SYMBOL_GPL(devlink_dpipe_table_unregister);
6008
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01006009/**
6010 * devlink_resource_register - devlink resource register
6011 *
6012 * @devlink: devlink
6013 * @resource_name: resource's name
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01006014 * @resource_size: resource's size
6015 * @resource_id: resource's id
Jakub Kicinskieeaadd82019-02-27 11:36:36 -08006016 * @parent_resource_id: resource's parent id
6017 * @size_params: size parameters
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01006018 */
6019int devlink_resource_register(struct devlink *devlink,
6020 const char *resource_name,
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01006021 u64 resource_size,
6022 u64 resource_id,
6023 u64 parent_resource_id,
Jiri Pirkofc56be42018-04-05 22:13:21 +02006024 const struct devlink_resource_size_params *size_params)
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01006025{
6026 struct devlink_resource *resource;
6027 struct list_head *resource_list;
David Ahern14530742018-03-20 19:31:14 -07006028 bool top_hierarchy;
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01006029 int err = 0;
6030
David Ahern14530742018-03-20 19:31:14 -07006031 top_hierarchy = parent_resource_id == DEVLINK_RESOURCE_ID_PARENT_TOP;
6032
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01006033 mutex_lock(&devlink->lock);
6034 resource = devlink_resource_find(devlink, NULL, resource_id);
6035 if (resource) {
6036 err = -EINVAL;
6037 goto out;
6038 }
6039
6040 resource = kzalloc(sizeof(*resource), GFP_KERNEL);
6041 if (!resource) {
6042 err = -ENOMEM;
6043 goto out;
6044 }
6045
6046 if (top_hierarchy) {
6047 resource_list = &devlink->resource_list;
6048 } else {
6049 struct devlink_resource *parent_resource;
6050
6051 parent_resource = devlink_resource_find(devlink, NULL,
6052 parent_resource_id);
6053 if (parent_resource) {
6054 resource_list = &parent_resource->resource_list;
6055 resource->parent = parent_resource;
6056 } else {
Colin Ian Kingb75703d2018-01-22 10:31:19 +00006057 kfree(resource);
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01006058 err = -EINVAL;
6059 goto out;
6060 }
6061 }
6062
6063 resource->name = resource_name;
6064 resource->size = resource_size;
6065 resource->size_new = resource_size;
6066 resource->id = resource_id;
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01006067 resource->size_valid = true;
Jiri Pirko77d27092018-02-28 13:12:09 +01006068 memcpy(&resource->size_params, size_params,
6069 sizeof(resource->size_params));
Arkadi Sharshevskyd9f9b9a2018-01-15 08:59:03 +01006070 INIT_LIST_HEAD(&resource->resource_list);
6071 list_add_tail(&resource->list, resource_list);
6072out:
6073 mutex_unlock(&devlink->lock);
6074 return err;
6075}
6076EXPORT_SYMBOL_GPL(devlink_resource_register);
6077
6078/**
6079 * devlink_resources_unregister - free all resources
6080 *
6081 * @devlink: devlink
6082 * @resource: resource
6083 */
6084void devlink_resources_unregister(struct devlink *devlink,
6085 struct devlink_resource *resource)
6086{
6087 struct devlink_resource *tmp, *child_resource;
6088 struct list_head *resource_list;
6089
6090 if (resource)
6091 resource_list = &resource->resource_list;
6092 else
6093 resource_list = &devlink->resource_list;
6094
6095 if (!resource)
6096 mutex_lock(&devlink->lock);
6097
6098 list_for_each_entry_safe(child_resource, tmp, resource_list, list) {
6099 devlink_resources_unregister(devlink, child_resource);
6100 list_del(&child_resource->list);
6101 kfree(child_resource);
6102 }
6103
6104 if (!resource)
6105 mutex_unlock(&devlink->lock);
6106}
6107EXPORT_SYMBOL_GPL(devlink_resources_unregister);
6108
6109/**
6110 * devlink_resource_size_get - get and update size
6111 *
6112 * @devlink: devlink
6113 * @resource_id: the requested resource id
6114 * @p_resource_size: ptr to update
6115 */
6116int devlink_resource_size_get(struct devlink *devlink,
6117 u64 resource_id,
6118 u64 *p_resource_size)
6119{
6120 struct devlink_resource *resource;
6121 int err = 0;
6122
6123 mutex_lock(&devlink->lock);
6124 resource = devlink_resource_find(devlink, NULL, resource_id);
6125 if (!resource) {
6126 err = -EINVAL;
6127 goto out;
6128 }
6129 *p_resource_size = resource->size_new;
6130 resource->size = resource->size_new;
6131out:
6132 mutex_unlock(&devlink->lock);
6133 return err;
6134}
6135EXPORT_SYMBOL_GPL(devlink_resource_size_get);
6136
Arkadi Sharshevsky56dc7cd2018-01-15 08:59:05 +01006137/**
6138 * devlink_dpipe_table_resource_set - set the resource id
6139 *
6140 * @devlink: devlink
6141 * @table_name: table name
6142 * @resource_id: resource id
6143 * @resource_units: number of resource's units consumed per table's entry
6144 */
6145int devlink_dpipe_table_resource_set(struct devlink *devlink,
6146 const char *table_name, u64 resource_id,
6147 u64 resource_units)
6148{
6149 struct devlink_dpipe_table *table;
6150 int err = 0;
6151
6152 mutex_lock(&devlink->lock);
6153 table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
6154 table_name);
6155 if (!table) {
6156 err = -EINVAL;
6157 goto out;
6158 }
6159 table->resource_id = resource_id;
6160 table->resource_units = resource_units;
6161 table->resource_valid = true;
6162out:
6163 mutex_unlock(&devlink->lock);
6164 return err;
6165}
6166EXPORT_SYMBOL_GPL(devlink_dpipe_table_resource_set);
6167
Jiri Pirkofc56be42018-04-05 22:13:21 +02006168/**
6169 * devlink_resource_occ_get_register - register occupancy getter
6170 *
6171 * @devlink: devlink
6172 * @resource_id: resource id
6173 * @occ_get: occupancy getter callback
6174 * @occ_get_priv: occupancy getter callback priv
6175 */
6176void devlink_resource_occ_get_register(struct devlink *devlink,
6177 u64 resource_id,
6178 devlink_resource_occ_get_t *occ_get,
6179 void *occ_get_priv)
6180{
6181 struct devlink_resource *resource;
6182
6183 mutex_lock(&devlink->lock);
6184 resource = devlink_resource_find(devlink, NULL, resource_id);
6185 if (WARN_ON(!resource))
6186 goto out;
6187 WARN_ON(resource->occ_get);
6188
6189 resource->occ_get = occ_get;
6190 resource->occ_get_priv = occ_get_priv;
6191out:
6192 mutex_unlock(&devlink->lock);
6193}
6194EXPORT_SYMBOL_GPL(devlink_resource_occ_get_register);
6195
6196/**
6197 * devlink_resource_occ_get_unregister - unregister occupancy getter
6198 *
6199 * @devlink: devlink
6200 * @resource_id: resource id
6201 */
6202void devlink_resource_occ_get_unregister(struct devlink *devlink,
6203 u64 resource_id)
6204{
6205 struct devlink_resource *resource;
6206
6207 mutex_lock(&devlink->lock);
6208 resource = devlink_resource_find(devlink, NULL, resource_id);
6209 if (WARN_ON(!resource))
6210 goto out;
6211 WARN_ON(!resource->occ_get);
6212
6213 resource->occ_get = NULL;
6214 resource->occ_get_priv = NULL;
6215out:
6216 mutex_unlock(&devlink->lock);
6217}
6218EXPORT_SYMBOL_GPL(devlink_resource_occ_get_unregister);
6219
Vasundhara Volam39e61602019-01-28 18:00:20 +05306220static int devlink_param_verify(const struct devlink_param *param)
6221{
6222 if (!param || !param->name || !param->supported_cmodes)
6223 return -EINVAL;
6224 if (param->generic)
6225 return devlink_param_generic_verify(param);
6226 else
6227 return devlink_param_driver_verify(param);
6228}
6229
6230static int __devlink_params_register(struct devlink *devlink,
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05306231 unsigned int port_index,
Vasundhara Volam39e61602019-01-28 18:00:20 +05306232 struct list_head *param_list,
6233 const struct devlink_param *params,
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05306234 size_t params_count,
6235 enum devlink_command reg_cmd,
6236 enum devlink_command unreg_cmd)
Vasundhara Volam39e61602019-01-28 18:00:20 +05306237{
6238 const struct devlink_param *param = params;
6239 int i;
6240 int err;
6241
6242 mutex_lock(&devlink->lock);
6243 for (i = 0; i < params_count; i++, param++) {
6244 err = devlink_param_verify(param);
6245 if (err)
6246 goto rollback;
6247
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05306248 err = devlink_param_register_one(devlink, port_index,
6249 param_list, param, reg_cmd);
Vasundhara Volam39e61602019-01-28 18:00:20 +05306250 if (err)
6251 goto rollback;
6252 }
6253
6254 mutex_unlock(&devlink->lock);
6255 return 0;
6256
6257rollback:
6258 if (!i)
6259 goto unlock;
6260 for (param--; i > 0; i--, param--)
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05306261 devlink_param_unregister_one(devlink, port_index, param_list,
6262 param, unreg_cmd);
Vasundhara Volam39e61602019-01-28 18:00:20 +05306263unlock:
6264 mutex_unlock(&devlink->lock);
6265 return err;
6266}
6267
6268static void __devlink_params_unregister(struct devlink *devlink,
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05306269 unsigned int port_index,
Vasundhara Volam39e61602019-01-28 18:00:20 +05306270 struct list_head *param_list,
6271 const struct devlink_param *params,
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05306272 size_t params_count,
6273 enum devlink_command cmd)
Vasundhara Volam39e61602019-01-28 18:00:20 +05306274{
6275 const struct devlink_param *param = params;
6276 int i;
6277
6278 mutex_lock(&devlink->lock);
6279 for (i = 0; i < params_count; i++, param++)
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05306280 devlink_param_unregister_one(devlink, 0, param_list, param,
6281 cmd);
Vasundhara Volam39e61602019-01-28 18:00:20 +05306282 mutex_unlock(&devlink->lock);
6283}
6284
Moshe Shemesheabaef12018-07-04 14:30:28 +03006285/**
6286 * devlink_params_register - register configuration parameters
6287 *
6288 * @devlink: devlink
6289 * @params: configuration parameters array
6290 * @params_count: number of parameters provided
6291 *
6292 * Register the configuration parameters supported by the driver.
6293 */
6294int devlink_params_register(struct devlink *devlink,
6295 const struct devlink_param *params,
6296 size_t params_count)
6297{
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05306298 return __devlink_params_register(devlink, 0, &devlink->param_list,
6299 params, params_count,
6300 DEVLINK_CMD_PARAM_NEW,
6301 DEVLINK_CMD_PARAM_DEL);
Moshe Shemesheabaef12018-07-04 14:30:28 +03006302}
6303EXPORT_SYMBOL_GPL(devlink_params_register);
6304
6305/**
6306 * devlink_params_unregister - unregister configuration parameters
6307 * @devlink: devlink
6308 * @params: configuration parameters to unregister
6309 * @params_count: number of parameters provided
6310 */
6311void devlink_params_unregister(struct devlink *devlink,
6312 const struct devlink_param *params,
6313 size_t params_count)
6314{
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05306315 return __devlink_params_unregister(devlink, 0, &devlink->param_list,
6316 params, params_count,
6317 DEVLINK_CMD_PARAM_DEL);
Moshe Shemesheabaef12018-07-04 14:30:28 +03006318}
6319EXPORT_SYMBOL_GPL(devlink_params_unregister);
6320
Moshe Shemeshec01aeb2018-07-04 14:30:31 +03006321/**
Jiri Pirko7c62cfb2019-02-07 11:22:45 +00006322 * devlink_params_publish - publish configuration parameters
6323 *
6324 * @devlink: devlink
6325 *
6326 * Publish previously registered configuration parameters.
6327 */
6328void devlink_params_publish(struct devlink *devlink)
6329{
6330 struct devlink_param_item *param_item;
6331
6332 list_for_each_entry(param_item, &devlink->param_list, list) {
6333 if (param_item->published)
6334 continue;
6335 param_item->published = true;
6336 devlink_param_notify(devlink, 0, param_item,
6337 DEVLINK_CMD_PARAM_NEW);
6338 }
6339}
6340EXPORT_SYMBOL_GPL(devlink_params_publish);
6341
6342/**
6343 * devlink_params_unpublish - unpublish configuration parameters
6344 *
6345 * @devlink: devlink
6346 *
6347 * Unpublish previously registered configuration parameters.
6348 */
6349void devlink_params_unpublish(struct devlink *devlink)
6350{
6351 struct devlink_param_item *param_item;
6352
6353 list_for_each_entry(param_item, &devlink->param_list, list) {
6354 if (!param_item->published)
6355 continue;
6356 param_item->published = false;
6357 devlink_param_notify(devlink, 0, param_item,
6358 DEVLINK_CMD_PARAM_DEL);
6359 }
6360}
6361EXPORT_SYMBOL_GPL(devlink_params_unpublish);
6362
6363/**
Vasundhara Volam39e61602019-01-28 18:00:20 +05306364 * devlink_port_params_register - register port configuration parameters
6365 *
6366 * @devlink_port: devlink port
6367 * @params: configuration parameters array
6368 * @params_count: number of parameters provided
6369 *
6370 * Register the configuration parameters supported by the port.
6371 */
6372int devlink_port_params_register(struct devlink_port *devlink_port,
6373 const struct devlink_param *params,
6374 size_t params_count)
6375{
6376 return __devlink_params_register(devlink_port->devlink,
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05306377 devlink_port->index,
Vasundhara Volam39e61602019-01-28 18:00:20 +05306378 &devlink_port->param_list, params,
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05306379 params_count,
6380 DEVLINK_CMD_PORT_PARAM_NEW,
6381 DEVLINK_CMD_PORT_PARAM_DEL);
Vasundhara Volam39e61602019-01-28 18:00:20 +05306382}
6383EXPORT_SYMBOL_GPL(devlink_port_params_register);
6384
6385/**
6386 * devlink_port_params_unregister - unregister port configuration
6387 * parameters
6388 *
6389 * @devlink_port: devlink port
6390 * @params: configuration parameters array
6391 * @params_count: number of parameters provided
6392 */
6393void devlink_port_params_unregister(struct devlink_port *devlink_port,
6394 const struct devlink_param *params,
6395 size_t params_count)
6396{
6397 return __devlink_params_unregister(devlink_port->devlink,
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05306398 devlink_port->index,
Vasundhara Volam39e61602019-01-28 18:00:20 +05306399 &devlink_port->param_list,
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05306400 params, params_count,
6401 DEVLINK_CMD_PORT_PARAM_DEL);
Vasundhara Volam39e61602019-01-28 18:00:20 +05306402}
6403EXPORT_SYMBOL_GPL(devlink_port_params_unregister);
6404
Vasundhara Volamffd19b92019-01-28 18:00:23 +05306405static int
6406__devlink_param_driverinit_value_get(struct list_head *param_list, u32 param_id,
6407 union devlink_param_value *init_val)
Moshe Shemeshec01aeb2018-07-04 14:30:31 +03006408{
6409 struct devlink_param_item *param_item;
6410
Vasundhara Volamffd19b92019-01-28 18:00:23 +05306411 param_item = devlink_param_find_by_id(param_list, param_id);
Moshe Shemeshec01aeb2018-07-04 14:30:31 +03006412 if (!param_item)
6413 return -EINVAL;
6414
6415 if (!param_item->driverinit_value_valid ||
6416 !devlink_param_cmode_is_supported(param_item->param,
6417 DEVLINK_PARAM_CMODE_DRIVERINIT))
6418 return -EOPNOTSUPP;
6419
Moshe Shemesh12765342018-10-10 16:09:26 +03006420 if (param_item->param->type == DEVLINK_PARAM_TYPE_STRING)
6421 strcpy(init_val->vstr, param_item->driverinit_value.vstr);
6422 else
6423 *init_val = param_item->driverinit_value;
Moshe Shemeshec01aeb2018-07-04 14:30:31 +03006424
6425 return 0;
6426}
Vasundhara Volamffd19b92019-01-28 18:00:23 +05306427
Vasundhara Volam5473a7b2019-01-28 18:00:24 +05306428static int
6429__devlink_param_driverinit_value_set(struct devlink *devlink,
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05306430 unsigned int port_index,
Vasundhara Volam5473a7b2019-01-28 18:00:24 +05306431 struct list_head *param_list, u32 param_id,
6432 union devlink_param_value init_val,
6433 enum devlink_command cmd)
6434{
6435 struct devlink_param_item *param_item;
6436
6437 param_item = devlink_param_find_by_id(param_list, param_id);
6438 if (!param_item)
6439 return -EINVAL;
6440
6441 if (!devlink_param_cmode_is_supported(param_item->param,
6442 DEVLINK_PARAM_CMODE_DRIVERINIT))
6443 return -EOPNOTSUPP;
6444
6445 if (param_item->param->type == DEVLINK_PARAM_TYPE_STRING)
6446 strcpy(param_item->driverinit_value.vstr, init_val.vstr);
6447 else
6448 param_item->driverinit_value = init_val;
6449 param_item->driverinit_value_valid = true;
6450
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05306451 devlink_param_notify(devlink, port_index, param_item, cmd);
Vasundhara Volam5473a7b2019-01-28 18:00:24 +05306452 return 0;
6453}
6454
Vasundhara Volamffd19b92019-01-28 18:00:23 +05306455/**
6456 * devlink_param_driverinit_value_get - get configuration parameter
6457 * value for driver initializing
6458 *
6459 * @devlink: devlink
6460 * @param_id: parameter ID
6461 * @init_val: value of parameter in driverinit configuration mode
6462 *
6463 * This function should be used by the driver to get driverinit
6464 * configuration for initialization after reload command.
6465 */
6466int devlink_param_driverinit_value_get(struct devlink *devlink, u32 param_id,
6467 union devlink_param_value *init_val)
6468{
Jakub Kicinskibe6fe1d2019-02-25 19:34:07 -08006469 if (!devlink->ops->reload)
Vasundhara Volamffd19b92019-01-28 18:00:23 +05306470 return -EOPNOTSUPP;
6471
6472 return __devlink_param_driverinit_value_get(&devlink->param_list,
6473 param_id, init_val);
6474}
Moshe Shemeshec01aeb2018-07-04 14:30:31 +03006475EXPORT_SYMBOL_GPL(devlink_param_driverinit_value_get);
6476
6477/**
6478 * devlink_param_driverinit_value_set - set value of configuration
6479 * parameter for driverinit
6480 * configuration mode
6481 *
6482 * @devlink: devlink
6483 * @param_id: parameter ID
6484 * @init_val: value of parameter to set for driverinit configuration mode
6485 *
6486 * This function should be used by the driver to set driverinit
6487 * configuration mode default value.
6488 */
6489int devlink_param_driverinit_value_set(struct devlink *devlink, u32 param_id,
6490 union devlink_param_value init_val)
6491{
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05306492 return __devlink_param_driverinit_value_set(devlink, 0,
Vasundhara Volam5473a7b2019-01-28 18:00:24 +05306493 &devlink->param_list,
6494 param_id, init_val,
6495 DEVLINK_CMD_PARAM_NEW);
Moshe Shemeshec01aeb2018-07-04 14:30:31 +03006496}
6497EXPORT_SYMBOL_GPL(devlink_param_driverinit_value_set);
6498
Moshe Shemeshea601e12018-07-04 14:30:32 +03006499/**
Vasundhara Volamffd19b92019-01-28 18:00:23 +05306500 * devlink_port_param_driverinit_value_get - get configuration parameter
6501 * value for driver initializing
6502 *
6503 * @devlink_port: devlink_port
6504 * @param_id: parameter ID
6505 * @init_val: value of parameter in driverinit configuration mode
6506 *
6507 * This function should be used by the driver to get driverinit
6508 * configuration for initialization after reload command.
6509 */
6510int devlink_port_param_driverinit_value_get(struct devlink_port *devlink_port,
6511 u32 param_id,
6512 union devlink_param_value *init_val)
6513{
6514 struct devlink *devlink = devlink_port->devlink;
6515
Jakub Kicinskibe6fe1d2019-02-25 19:34:07 -08006516 if (!devlink->ops->reload)
Vasundhara Volamffd19b92019-01-28 18:00:23 +05306517 return -EOPNOTSUPP;
6518
6519 return __devlink_param_driverinit_value_get(&devlink_port->param_list,
6520 param_id, init_val);
6521}
6522EXPORT_SYMBOL_GPL(devlink_port_param_driverinit_value_get);
6523
6524/**
Vasundhara Volam5473a7b2019-01-28 18:00:24 +05306525 * devlink_port_param_driverinit_value_set - set value of configuration
6526 * parameter for driverinit
6527 * configuration mode
6528 *
6529 * @devlink_port: devlink_port
6530 * @param_id: parameter ID
6531 * @init_val: value of parameter to set for driverinit configuration mode
6532 *
6533 * This function should be used by the driver to set driverinit
6534 * configuration mode default value.
6535 */
6536int devlink_port_param_driverinit_value_set(struct devlink_port *devlink_port,
6537 u32 param_id,
6538 union devlink_param_value init_val)
6539{
6540 return __devlink_param_driverinit_value_set(devlink_port->devlink,
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05306541 devlink_port->index,
Vasundhara Volam5473a7b2019-01-28 18:00:24 +05306542 &devlink_port->param_list,
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05306543 param_id, init_val,
6544 DEVLINK_CMD_PORT_PARAM_NEW);
Vasundhara Volam5473a7b2019-01-28 18:00:24 +05306545}
6546EXPORT_SYMBOL_GPL(devlink_port_param_driverinit_value_set);
6547
6548/**
Moshe Shemeshea601e12018-07-04 14:30:32 +03006549 * devlink_param_value_changed - notify devlink on a parameter's value
6550 * change. Should be called by the driver
6551 * right after the change.
6552 *
6553 * @devlink: devlink
6554 * @param_id: parameter ID
6555 *
6556 * This function should be used by the driver to notify devlink on value
6557 * change, excluding driverinit configuration mode.
6558 * For driverinit configuration mode driver should use the function
Moshe Shemeshea601e12018-07-04 14:30:32 +03006559 */
6560void devlink_param_value_changed(struct devlink *devlink, u32 param_id)
6561{
6562 struct devlink_param_item *param_item;
6563
6564 param_item = devlink_param_find_by_id(&devlink->param_list, param_id);
6565 WARN_ON(!param_item);
6566
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05306567 devlink_param_notify(devlink, 0, param_item, DEVLINK_CMD_PARAM_NEW);
Moshe Shemeshea601e12018-07-04 14:30:32 +03006568}
6569EXPORT_SYMBOL_GPL(devlink_param_value_changed);
6570
Alex Veskerb16ebe92018-07-12 15:13:08 +03006571/**
Vasundhara Volamc1e5786d2019-01-28 18:00:25 +05306572 * devlink_port_param_value_changed - notify devlink on a parameter's value
6573 * change. Should be called by the driver
6574 * right after the change.
6575 *
6576 * @devlink_port: devlink_port
6577 * @param_id: parameter ID
6578 *
6579 * This function should be used by the driver to notify devlink on value
6580 * change, excluding driverinit configuration mode.
6581 * For driverinit configuration mode driver should use the function
6582 * devlink_port_param_driverinit_value_set() instead.
6583 */
6584void devlink_port_param_value_changed(struct devlink_port *devlink_port,
6585 u32 param_id)
6586{
6587 struct devlink_param_item *param_item;
6588
6589 param_item = devlink_param_find_by_id(&devlink_port->param_list,
6590 param_id);
6591 WARN_ON(!param_item);
6592
6593 devlink_param_notify(devlink_port->devlink, devlink_port->index,
6594 param_item, DEVLINK_CMD_PORT_PARAM_NEW);
6595}
6596EXPORT_SYMBOL_GPL(devlink_port_param_value_changed);
6597
6598/**
Moshe Shemeshbde74ad12018-10-10 16:09:27 +03006599 * devlink_param_value_str_fill - Safely fill-up the string preventing
6600 * from overflow of the preallocated buffer
6601 *
6602 * @dst_val: destination devlink_param_value
6603 * @src: source buffer
6604 */
6605void devlink_param_value_str_fill(union devlink_param_value *dst_val,
6606 const char *src)
6607{
6608 size_t len;
6609
6610 len = strlcpy(dst_val->vstr, src, __DEVLINK_PARAM_MAX_STRING_VALUE);
6611 WARN_ON(len >= __DEVLINK_PARAM_MAX_STRING_VALUE);
6612}
6613EXPORT_SYMBOL_GPL(devlink_param_value_str_fill);
6614
6615/**
Alex Veskerb16ebe92018-07-12 15:13:08 +03006616 * devlink_region_create - create a new address region
6617 *
6618 * @devlink: devlink
6619 * @region_name: region name
6620 * @region_max_snapshots: Maximum supported number of snapshots for region
6621 * @region_size: size of region
6622 */
6623struct devlink_region *devlink_region_create(struct devlink *devlink,
6624 const char *region_name,
6625 u32 region_max_snapshots,
6626 u64 region_size)
6627{
6628 struct devlink_region *region;
6629 int err = 0;
6630
6631 mutex_lock(&devlink->lock);
6632
6633 if (devlink_region_get_by_name(devlink, region_name)) {
6634 err = -EEXIST;
6635 goto unlock;
6636 }
6637
6638 region = kzalloc(sizeof(*region), GFP_KERNEL);
6639 if (!region) {
6640 err = -ENOMEM;
6641 goto unlock;
6642 }
6643
6644 region->devlink = devlink;
6645 region->max_snapshots = region_max_snapshots;
6646 region->name = region_name;
6647 region->size = region_size;
6648 INIT_LIST_HEAD(&region->snapshot_list);
6649 list_add_tail(&region->list, &devlink->region_list);
Alex Vesker866319b2018-07-12 15:13:13 +03006650 devlink_nl_region_notify(region, NULL, DEVLINK_CMD_REGION_NEW);
Alex Veskerb16ebe92018-07-12 15:13:08 +03006651
6652 mutex_unlock(&devlink->lock);
6653 return region;
6654
6655unlock:
6656 mutex_unlock(&devlink->lock);
6657 return ERR_PTR(err);
6658}
6659EXPORT_SYMBOL_GPL(devlink_region_create);
6660
6661/**
6662 * devlink_region_destroy - destroy address region
6663 *
6664 * @region: devlink region to destroy
6665 */
6666void devlink_region_destroy(struct devlink_region *region)
6667{
6668 struct devlink *devlink = region->devlink;
Alex Veskerd7e52722018-07-12 15:13:10 +03006669 struct devlink_snapshot *snapshot, *ts;
Alex Veskerb16ebe92018-07-12 15:13:08 +03006670
6671 mutex_lock(&devlink->lock);
Alex Veskerd7e52722018-07-12 15:13:10 +03006672
6673 /* Free all snapshots of region */
6674 list_for_each_entry_safe(snapshot, ts, &region->snapshot_list, list)
6675 devlink_region_snapshot_del(snapshot);
6676
Alex Veskerb16ebe92018-07-12 15:13:08 +03006677 list_del(&region->list);
Alex Vesker866319b2018-07-12 15:13:13 +03006678
6679 devlink_nl_region_notify(region, NULL, DEVLINK_CMD_REGION_DEL);
Alex Veskerb16ebe92018-07-12 15:13:08 +03006680 mutex_unlock(&devlink->lock);
6681 kfree(region);
6682}
6683EXPORT_SYMBOL_GPL(devlink_region_destroy);
6684
Alex Veskerccadfa42018-07-12 15:13:09 +03006685/**
6686 * devlink_region_shapshot_id_get - get snapshot ID
6687 *
6688 * This callback should be called when adding a new snapshot,
6689 * Driver should use the same id for multiple snapshots taken
6690 * on multiple regions at the same time/by the same trigger.
6691 *
6692 * @devlink: devlink
6693 */
6694u32 devlink_region_shapshot_id_get(struct devlink *devlink)
6695{
6696 u32 id;
6697
6698 mutex_lock(&devlink->lock);
6699 id = ++devlink->snapshot_id;
6700 mutex_unlock(&devlink->lock);
6701
6702 return id;
6703}
6704EXPORT_SYMBOL_GPL(devlink_region_shapshot_id_get);
6705
Alex Veskerd7e52722018-07-12 15:13:10 +03006706/**
6707 * devlink_region_snapshot_create - create a new snapshot
6708 * This will add a new snapshot of a region. The snapshot
6709 * will be stored on the region struct and can be accessed
6710 * from devlink. This is useful for future analyses of snapshots.
6711 * Multiple snapshots can be created on a region.
6712 * The @snapshot_id should be obtained using the getter function.
6713 *
Jakub Kicinskieeaadd82019-02-27 11:36:36 -08006714 * @region: devlink region of the snapshot
Alex Veskerd7e52722018-07-12 15:13:10 +03006715 * @data_len: size of snapshot data
6716 * @data: snapshot data
6717 * @snapshot_id: snapshot id to be created
6718 * @data_destructor: pointer to destructor function to free data
6719 */
6720int devlink_region_snapshot_create(struct devlink_region *region, u64 data_len,
6721 u8 *data, u32 snapshot_id,
6722 devlink_snapshot_data_dest_t *data_destructor)
6723{
6724 struct devlink *devlink = region->devlink;
6725 struct devlink_snapshot *snapshot;
6726 int err;
6727
6728 mutex_lock(&devlink->lock);
6729
6730 /* check if region can hold one more snapshot */
6731 if (region->cur_snapshots == region->max_snapshots) {
6732 err = -ENOMEM;
6733 goto unlock;
6734 }
6735
6736 if (devlink_region_snapshot_get_by_id(region, snapshot_id)) {
6737 err = -EEXIST;
6738 goto unlock;
6739 }
6740
6741 snapshot = kzalloc(sizeof(*snapshot), GFP_KERNEL);
6742 if (!snapshot) {
6743 err = -ENOMEM;
6744 goto unlock;
6745 }
6746
6747 snapshot->id = snapshot_id;
6748 snapshot->region = region;
6749 snapshot->data = data;
6750 snapshot->data_len = data_len;
6751 snapshot->data_destructor = data_destructor;
6752
6753 list_add_tail(&snapshot->list, &region->snapshot_list);
6754
6755 region->cur_snapshots++;
6756
Alex Vesker866319b2018-07-12 15:13:13 +03006757 devlink_nl_region_notify(region, snapshot, DEVLINK_CMD_REGION_NEW);
Alex Veskerd7e52722018-07-12 15:13:10 +03006758 mutex_unlock(&devlink->lock);
6759 return 0;
6760
6761unlock:
6762 mutex_unlock(&devlink->lock);
6763 return err;
6764}
6765EXPORT_SYMBOL_GPL(devlink_region_snapshot_create);
6766
Jakub Kicinskiddb6e992019-01-31 10:50:47 -08006767static void __devlink_compat_running_version(struct devlink *devlink,
6768 char *buf, size_t len)
6769{
6770 const struct nlattr *nlattr;
6771 struct devlink_info_req req;
6772 struct sk_buff *msg;
6773 int rem, err;
6774
Jakub Kicinskiddb6e992019-01-31 10:50:47 -08006775 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6776 if (!msg)
6777 return;
6778
6779 req.msg = msg;
6780 err = devlink->ops->info_get(devlink, &req, NULL);
6781 if (err)
6782 goto free_msg;
6783
6784 nla_for_each_attr(nlattr, (void *)msg->data, msg->len, rem) {
6785 const struct nlattr *kv;
6786 int rem_kv;
6787
6788 if (nla_type(nlattr) != DEVLINK_ATTR_INFO_VERSION_RUNNING)
6789 continue;
6790
6791 nla_for_each_nested(kv, nlattr, rem_kv) {
6792 if (nla_type(kv) != DEVLINK_ATTR_INFO_VERSION_VALUE)
6793 continue;
6794
6795 strlcat(buf, nla_data(kv), len);
6796 strlcat(buf, " ", len);
6797 }
6798 }
6799free_msg:
6800 nlmsg_free(msg);
6801}
6802
6803void devlink_compat_running_version(struct net_device *dev,
6804 char *buf, size_t len)
6805{
Jakub Kicinskiddb6e992019-01-31 10:50:47 -08006806 struct devlink *devlink;
6807
Jakub Kicinski1b45ff62019-02-25 19:34:06 -08006808 dev_hold(dev);
6809 rtnl_unlock();
6810
Jakub Kicinskib473b0d2019-02-25 19:34:03 -08006811 devlink = netdev_to_devlink(dev);
Jakub Kicinskibe6fe1d2019-02-25 19:34:07 -08006812 if (!devlink || !devlink->ops->info_get)
Jiri Pirkoe0dcd382019-03-24 11:14:29 +01006813 goto out;
Jakub Kicinskib473b0d2019-02-25 19:34:03 -08006814
6815 mutex_lock(&devlink->lock);
6816 __devlink_compat_running_version(devlink, buf, len);
6817 mutex_unlock(&devlink->lock);
Jakub Kicinski1b45ff62019-02-25 19:34:06 -08006818
Jiri Pirkoe0dcd382019-03-24 11:14:29 +01006819out:
Jakub Kicinski1b45ff62019-02-25 19:34:06 -08006820 rtnl_lock();
6821 dev_put(dev);
Jakub Kicinskiddb6e992019-01-31 10:50:47 -08006822}
6823
Jakub Kicinski4eceba12019-02-14 13:40:45 -08006824int devlink_compat_flash_update(struct net_device *dev, const char *file_name)
6825{
Jakub Kicinski4eceba12019-02-14 13:40:45 -08006826 struct devlink *devlink;
Jiri Pirkoe0dcd382019-03-24 11:14:29 +01006827 int ret;
Jakub Kicinski4eceba12019-02-14 13:40:45 -08006828
Jakub Kicinski1b45ff62019-02-25 19:34:06 -08006829 dev_hold(dev);
6830 rtnl_unlock();
6831
Jakub Kicinskib473b0d2019-02-25 19:34:03 -08006832 devlink = netdev_to_devlink(dev);
Jiri Pirkoe0dcd382019-03-24 11:14:29 +01006833 if (!devlink || !devlink->ops->flash_update) {
6834 ret = -EOPNOTSUPP;
6835 goto out;
6836 }
Jakub Kicinski4eceba12019-02-14 13:40:45 -08006837
Jakub Kicinskib473b0d2019-02-25 19:34:03 -08006838 mutex_lock(&devlink->lock);
6839 ret = devlink->ops->flash_update(devlink, file_name, NULL, NULL);
6840 mutex_unlock(&devlink->lock);
Jakub Kicinski1b45ff62019-02-25 19:34:06 -08006841
Jiri Pirkoe0dcd382019-03-24 11:14:29 +01006842out:
Jakub Kicinski1b45ff62019-02-25 19:34:06 -08006843 rtnl_lock();
6844 dev_put(dev);
6845
Jakub Kicinskib473b0d2019-02-25 19:34:03 -08006846 return ret;
Jakub Kicinski4eceba12019-02-14 13:40:45 -08006847}
6848
Jiri Pirkoaf3836d2019-03-28 13:56:37 +01006849int devlink_compat_phys_port_name_get(struct net_device *dev,
6850 char *name, size_t len)
6851{
6852 struct devlink_port *devlink_port;
6853
6854 /* RTNL mutex is held here which ensures that devlink_port
6855 * instance cannot disappear in the middle. No need to take
6856 * any devlink lock as only permanent values are accessed.
6857 */
6858 ASSERT_RTNL();
6859
6860 devlink_port = netdev_to_devlink_port(dev);
6861 if (!devlink_port)
6862 return -EOPNOTSUPP;
6863
6864 return __devlink_port_phys_port_name_get(devlink_port, name, len);
6865}
6866
Jiri Pirko7e1146e2019-04-03 14:24:17 +02006867int devlink_compat_switch_id_get(struct net_device *dev,
6868 struct netdev_phys_item_id *ppid)
6869{
6870 struct devlink_port *devlink_port;
6871
6872 /* RTNL mutex is held here which ensures that devlink_port
6873 * instance cannot disappear in the middle. No need to take
6874 * any devlink lock as only permanent values are accessed.
6875 */
6876 ASSERT_RTNL();
6877 devlink_port = netdev_to_devlink_port(dev);
6878 if (!devlink_port || !devlink_port->attrs.switch_port)
6879 return -EOPNOTSUPP;
6880
6881 memcpy(ppid, &devlink_port->attrs.switch_id, sizeof(*ppid));
6882
6883 return 0;
6884}
6885
Jakub Kicinskif4b6bcc2019-02-25 19:34:02 -08006886static int __init devlink_init(void)
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01006887{
Johannes Berg489111e2016-10-24 14:40:03 +02006888 return genl_register_family(&devlink_nl_family);
Jiri Pirkobfcd3a42016-02-26 17:32:23 +01006889}
6890
Jakub Kicinskif4b6bcc2019-02-25 19:34:02 -08006891subsys_initcall(devlink_init);