56ff63b41e96f384e29a53a69f945e226df8cd89
[platform/kernel/linux-starfive.git] / net / devlink / leftover.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
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>
8  */
9
10 #include <linux/etherdevice.h>
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/types.h>
14 #include <linux/slab.h>
15 #include <linux/gfp.h>
16 #include <linux/device.h>
17 #include <linux/list.h>
18 #include <linux/netdevice.h>
19 #include <linux/spinlock.h>
20 #include <linux/refcount.h>
21 #include <linux/workqueue.h>
22 #include <linux/u64_stats_sync.h>
23 #include <linux/timekeeping.h>
24 #include <rdma/ib_verbs.h>
25 #include <net/netlink.h>
26 #include <net/genetlink.h>
27 #include <net/rtnetlink.h>
28 #include <net/net_namespace.h>
29 #include <net/sock.h>
30 #include <net/devlink.h>
31 #define CREATE_TRACE_POINTS
32 #include <trace/events/devlink.h>
33
34 #include "devl_internal.h"
35
36 struct devlink_linecard {
37         struct list_head list;
38         struct devlink *devlink;
39         unsigned int index;
40         refcount_t refcount;
41         const struct devlink_linecard_ops *ops;
42         void *priv;
43         enum devlink_linecard_state state;
44         struct mutex state_lock; /* Protects state */
45         const char *type;
46         struct devlink_linecard_type *types;
47         unsigned int types_count;
48         struct devlink *nested_devlink;
49 };
50
51 /**
52  * struct devlink_resource - devlink resource
53  * @name: name of the resource
54  * @id: id, per devlink instance
55  * @size: size of the resource
56  * @size_new: updated size of the resource, reload is needed
57  * @size_valid: valid in case the total size of the resource is valid
58  *              including its children
59  * @parent: parent resource
60  * @size_params: size parameters
61  * @list: parent list
62  * @resource_list: list of child resources
63  * @occ_get: occupancy getter callback
64  * @occ_get_priv: occupancy getter callback priv
65  */
66 struct devlink_resource {
67         const char *name;
68         u64 id;
69         u64 size;
70         u64 size_new;
71         bool size_valid;
72         struct devlink_resource *parent;
73         struct devlink_resource_size_params size_params;
74         struct list_head list;
75         struct list_head resource_list;
76         devlink_resource_occ_get_t *occ_get;
77         void *occ_get_priv;
78 };
79
80 static struct devlink_dpipe_field devlink_dpipe_fields_ethernet[] = {
81         {
82                 .name = "destination mac",
83                 .id = DEVLINK_DPIPE_FIELD_ETHERNET_DST_MAC,
84                 .bitwidth = 48,
85         },
86 };
87
88 struct devlink_dpipe_header devlink_dpipe_header_ethernet = {
89         .name = "ethernet",
90         .id = DEVLINK_DPIPE_HEADER_ETHERNET,
91         .fields = devlink_dpipe_fields_ethernet,
92         .fields_count = ARRAY_SIZE(devlink_dpipe_fields_ethernet),
93         .global = true,
94 };
95 EXPORT_SYMBOL_GPL(devlink_dpipe_header_ethernet);
96
97 static struct devlink_dpipe_field devlink_dpipe_fields_ipv4[] = {
98         {
99                 .name = "destination ip",
100                 .id = DEVLINK_DPIPE_FIELD_IPV4_DST_IP,
101                 .bitwidth = 32,
102         },
103 };
104
105 struct devlink_dpipe_header devlink_dpipe_header_ipv4 = {
106         .name = "ipv4",
107         .id = DEVLINK_DPIPE_HEADER_IPV4,
108         .fields = devlink_dpipe_fields_ipv4,
109         .fields_count = ARRAY_SIZE(devlink_dpipe_fields_ipv4),
110         .global = true,
111 };
112 EXPORT_SYMBOL_GPL(devlink_dpipe_header_ipv4);
113
114 static struct devlink_dpipe_field devlink_dpipe_fields_ipv6[] = {
115         {
116                 .name = "destination ip",
117                 .id = DEVLINK_DPIPE_FIELD_IPV6_DST_IP,
118                 .bitwidth = 128,
119         },
120 };
121
122 struct devlink_dpipe_header devlink_dpipe_header_ipv6 = {
123         .name = "ipv6",
124         .id = DEVLINK_DPIPE_HEADER_IPV6,
125         .fields = devlink_dpipe_fields_ipv6,
126         .fields_count = ARRAY_SIZE(devlink_dpipe_fields_ipv6),
127         .global = true,
128 };
129 EXPORT_SYMBOL_GPL(devlink_dpipe_header_ipv6);
130
131 EXPORT_TRACEPOINT_SYMBOL_GPL(devlink_hwmsg);
132 EXPORT_TRACEPOINT_SYMBOL_GPL(devlink_hwerr);
133 EXPORT_TRACEPOINT_SYMBOL_GPL(devlink_trap_report);
134
135 #define DEVLINK_PORT_FN_CAPS_VALID_MASK \
136         (_BITUL(__DEVLINK_PORT_FN_ATTR_CAPS_MAX) - 1)
137
138 static const struct nla_policy devlink_function_nl_policy[DEVLINK_PORT_FUNCTION_ATTR_MAX + 1] = {
139         [DEVLINK_PORT_FUNCTION_ATTR_HW_ADDR] = { .type = NLA_BINARY },
140         [DEVLINK_PORT_FN_ATTR_STATE] =
141                 NLA_POLICY_RANGE(NLA_U8, DEVLINK_PORT_FN_STATE_INACTIVE,
142                                  DEVLINK_PORT_FN_STATE_ACTIVE),
143         [DEVLINK_PORT_FN_ATTR_CAPS] =
144                 NLA_POLICY_BITFIELD32(DEVLINK_PORT_FN_CAPS_VALID_MASK),
145 };
146
147 static const struct nla_policy devlink_selftest_nl_policy[DEVLINK_ATTR_SELFTEST_ID_MAX + 1] = {
148         [DEVLINK_ATTR_SELFTEST_ID_FLASH] = { .type = NLA_FLAG },
149 };
150
151 #define ASSERT_DEVLINK_PORT_REGISTERED(devlink_port)                            \
152         WARN_ON_ONCE(!(devlink_port)->registered)
153 #define ASSERT_DEVLINK_PORT_NOT_REGISTERED(devlink_port)                        \
154         WARN_ON_ONCE((devlink_port)->registered)
155 #define ASSERT_DEVLINK_PORT_INITIALIZED(devlink_port)                           \
156         WARN_ON_ONCE(!(devlink_port)->initialized)
157
158 static struct devlink_port *devlink_port_get_by_index(struct devlink *devlink,
159                                                       unsigned int port_index)
160 {
161         return xa_load(&devlink->ports, port_index);
162 }
163
164 static struct devlink_port *devlink_port_get_from_attrs(struct devlink *devlink,
165                                                         struct nlattr **attrs)
166 {
167         if (attrs[DEVLINK_ATTR_PORT_INDEX]) {
168                 u32 port_index = nla_get_u32(attrs[DEVLINK_ATTR_PORT_INDEX]);
169                 struct devlink_port *devlink_port;
170
171                 devlink_port = devlink_port_get_by_index(devlink, port_index);
172                 if (!devlink_port)
173                         return ERR_PTR(-ENODEV);
174                 return devlink_port;
175         }
176         return ERR_PTR(-EINVAL);
177 }
178
179 struct devlink_port *devlink_port_get_from_info(struct devlink *devlink,
180                                                 struct genl_info *info)
181 {
182         return devlink_port_get_from_attrs(devlink, info->attrs);
183 }
184
185 static inline bool
186 devlink_rate_is_leaf(struct devlink_rate *devlink_rate)
187 {
188         return devlink_rate->type == DEVLINK_RATE_TYPE_LEAF;
189 }
190
191 static inline bool
192 devlink_rate_is_node(struct devlink_rate *devlink_rate)
193 {
194         return devlink_rate->type == DEVLINK_RATE_TYPE_NODE;
195 }
196
197 static struct devlink_rate *
198 devlink_rate_leaf_get_from_info(struct devlink *devlink, struct genl_info *info)
199 {
200         struct devlink_rate *devlink_rate;
201         struct devlink_port *devlink_port;
202
203         devlink_port = devlink_port_get_from_attrs(devlink, info->attrs);
204         if (IS_ERR(devlink_port))
205                 return ERR_CAST(devlink_port);
206         devlink_rate = devlink_port->devlink_rate;
207         return devlink_rate ?: ERR_PTR(-ENODEV);
208 }
209
210 static struct devlink_rate *
211 devlink_rate_node_get_by_name(struct devlink *devlink, const char *node_name)
212 {
213         static struct devlink_rate *devlink_rate;
214
215         list_for_each_entry(devlink_rate, &devlink->rate_list, list) {
216                 if (devlink_rate_is_node(devlink_rate) &&
217                     !strcmp(node_name, devlink_rate->name))
218                         return devlink_rate;
219         }
220         return ERR_PTR(-ENODEV);
221 }
222
223 static struct devlink_rate *
224 devlink_rate_node_get_from_attrs(struct devlink *devlink, struct nlattr **attrs)
225 {
226         const char *rate_node_name;
227         size_t len;
228
229         if (!attrs[DEVLINK_ATTR_RATE_NODE_NAME])
230                 return ERR_PTR(-EINVAL);
231         rate_node_name = nla_data(attrs[DEVLINK_ATTR_RATE_NODE_NAME]);
232         len = strlen(rate_node_name);
233         /* Name cannot be empty or decimal number */
234         if (!len || strspn(rate_node_name, "0123456789") == len)
235                 return ERR_PTR(-EINVAL);
236
237         return devlink_rate_node_get_by_name(devlink, rate_node_name);
238 }
239
240 struct devlink_rate *
241 devlink_rate_node_get_from_info(struct devlink *devlink, struct genl_info *info)
242 {
243         return devlink_rate_node_get_from_attrs(devlink, info->attrs);
244 }
245
246 struct devlink_rate *
247 devlink_rate_get_from_info(struct devlink *devlink, struct genl_info *info)
248 {
249         struct nlattr **attrs = info->attrs;
250
251         if (attrs[DEVLINK_ATTR_PORT_INDEX])
252                 return devlink_rate_leaf_get_from_info(devlink, info);
253         else if (attrs[DEVLINK_ATTR_RATE_NODE_NAME])
254                 return devlink_rate_node_get_from_info(devlink, info);
255         else
256                 return ERR_PTR(-EINVAL);
257 }
258
259 static struct devlink_linecard *
260 devlink_linecard_get_by_index(struct devlink *devlink,
261                               unsigned int linecard_index)
262 {
263         struct devlink_linecard *devlink_linecard;
264
265         list_for_each_entry(devlink_linecard, &devlink->linecard_list, list) {
266                 if (devlink_linecard->index == linecard_index)
267                         return devlink_linecard;
268         }
269         return NULL;
270 }
271
272 static bool devlink_linecard_index_exists(struct devlink *devlink,
273                                           unsigned int linecard_index)
274 {
275         return devlink_linecard_get_by_index(devlink, linecard_index);
276 }
277
278 static struct devlink_linecard *
279 devlink_linecard_get_from_attrs(struct devlink *devlink, struct nlattr **attrs)
280 {
281         if (attrs[DEVLINK_ATTR_LINECARD_INDEX]) {
282                 u32 linecard_index = nla_get_u32(attrs[DEVLINK_ATTR_LINECARD_INDEX]);
283                 struct devlink_linecard *linecard;
284
285                 mutex_lock(&devlink->linecards_lock);
286                 linecard = devlink_linecard_get_by_index(devlink, linecard_index);
287                 if (linecard)
288                         refcount_inc(&linecard->refcount);
289                 mutex_unlock(&devlink->linecards_lock);
290                 if (!linecard)
291                         return ERR_PTR(-ENODEV);
292                 return linecard;
293         }
294         return ERR_PTR(-EINVAL);
295 }
296
297 struct devlink_linecard *
298 devlink_linecard_get_from_info(struct devlink *devlink, struct genl_info *info)
299 {
300         return devlink_linecard_get_from_attrs(devlink, info->attrs);
301 }
302
303 void devlink_linecard_put(struct devlink_linecard *linecard)
304 {
305         if (refcount_dec_and_test(&linecard->refcount)) {
306                 mutex_destroy(&linecard->state_lock);
307                 kfree(linecard);
308         }
309 }
310
311 struct devlink_sb {
312         struct list_head list;
313         unsigned int index;
314         u32 size;
315         u16 ingress_pools_count;
316         u16 egress_pools_count;
317         u16 ingress_tc_count;
318         u16 egress_tc_count;
319 };
320
321 static u16 devlink_sb_pool_count(struct devlink_sb *devlink_sb)
322 {
323         return devlink_sb->ingress_pools_count + devlink_sb->egress_pools_count;
324 }
325
326 static struct devlink_sb *devlink_sb_get_by_index(struct devlink *devlink,
327                                                   unsigned int sb_index)
328 {
329         struct devlink_sb *devlink_sb;
330
331         list_for_each_entry(devlink_sb, &devlink->sb_list, list) {
332                 if (devlink_sb->index == sb_index)
333                         return devlink_sb;
334         }
335         return NULL;
336 }
337
338 static bool devlink_sb_index_exists(struct devlink *devlink,
339                                     unsigned int sb_index)
340 {
341         return devlink_sb_get_by_index(devlink, sb_index);
342 }
343
344 static struct devlink_sb *devlink_sb_get_from_attrs(struct devlink *devlink,
345                                                     struct nlattr **attrs)
346 {
347         if (attrs[DEVLINK_ATTR_SB_INDEX]) {
348                 u32 sb_index = nla_get_u32(attrs[DEVLINK_ATTR_SB_INDEX]);
349                 struct devlink_sb *devlink_sb;
350
351                 devlink_sb = devlink_sb_get_by_index(devlink, sb_index);
352                 if (!devlink_sb)
353                         return ERR_PTR(-ENODEV);
354                 return devlink_sb;
355         }
356         return ERR_PTR(-EINVAL);
357 }
358
359 static struct devlink_sb *devlink_sb_get_from_info(struct devlink *devlink,
360                                                    struct genl_info *info)
361 {
362         return devlink_sb_get_from_attrs(devlink, info->attrs);
363 }
364
365 static int devlink_sb_pool_index_get_from_attrs(struct devlink_sb *devlink_sb,
366                                                 struct nlattr **attrs,
367                                                 u16 *p_pool_index)
368 {
369         u16 val;
370
371         if (!attrs[DEVLINK_ATTR_SB_POOL_INDEX])
372                 return -EINVAL;
373
374         val = nla_get_u16(attrs[DEVLINK_ATTR_SB_POOL_INDEX]);
375         if (val >= devlink_sb_pool_count(devlink_sb))
376                 return -EINVAL;
377         *p_pool_index = val;
378         return 0;
379 }
380
381 static int devlink_sb_pool_index_get_from_info(struct devlink_sb *devlink_sb,
382                                                struct genl_info *info,
383                                                u16 *p_pool_index)
384 {
385         return devlink_sb_pool_index_get_from_attrs(devlink_sb, info->attrs,
386                                                     p_pool_index);
387 }
388
389 static int
390 devlink_sb_pool_type_get_from_attrs(struct nlattr **attrs,
391                                     enum devlink_sb_pool_type *p_pool_type)
392 {
393         u8 val;
394
395         if (!attrs[DEVLINK_ATTR_SB_POOL_TYPE])
396                 return -EINVAL;
397
398         val = nla_get_u8(attrs[DEVLINK_ATTR_SB_POOL_TYPE]);
399         if (val != DEVLINK_SB_POOL_TYPE_INGRESS &&
400             val != DEVLINK_SB_POOL_TYPE_EGRESS)
401                 return -EINVAL;
402         *p_pool_type = val;
403         return 0;
404 }
405
406 static int
407 devlink_sb_pool_type_get_from_info(struct genl_info *info,
408                                    enum devlink_sb_pool_type *p_pool_type)
409 {
410         return devlink_sb_pool_type_get_from_attrs(info->attrs, p_pool_type);
411 }
412
413 static int
414 devlink_sb_th_type_get_from_attrs(struct nlattr **attrs,
415                                   enum devlink_sb_threshold_type *p_th_type)
416 {
417         u8 val;
418
419         if (!attrs[DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE])
420                 return -EINVAL;
421
422         val = nla_get_u8(attrs[DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE]);
423         if (val != DEVLINK_SB_THRESHOLD_TYPE_STATIC &&
424             val != DEVLINK_SB_THRESHOLD_TYPE_DYNAMIC)
425                 return -EINVAL;
426         *p_th_type = val;
427         return 0;
428 }
429
430 static int
431 devlink_sb_th_type_get_from_info(struct genl_info *info,
432                                  enum devlink_sb_threshold_type *p_th_type)
433 {
434         return devlink_sb_th_type_get_from_attrs(info->attrs, p_th_type);
435 }
436
437 static int
438 devlink_sb_tc_index_get_from_attrs(struct devlink_sb *devlink_sb,
439                                    struct nlattr **attrs,
440                                    enum devlink_sb_pool_type pool_type,
441                                    u16 *p_tc_index)
442 {
443         u16 val;
444
445         if (!attrs[DEVLINK_ATTR_SB_TC_INDEX])
446                 return -EINVAL;
447
448         val = nla_get_u16(attrs[DEVLINK_ATTR_SB_TC_INDEX]);
449         if (pool_type == DEVLINK_SB_POOL_TYPE_INGRESS &&
450             val >= devlink_sb->ingress_tc_count)
451                 return -EINVAL;
452         if (pool_type == DEVLINK_SB_POOL_TYPE_EGRESS &&
453             val >= devlink_sb->egress_tc_count)
454                 return -EINVAL;
455         *p_tc_index = val;
456         return 0;
457 }
458
459 static void devlink_port_fn_cap_fill(struct nla_bitfield32 *caps,
460                                      u32 cap, bool is_enable)
461 {
462         caps->selector |= cap;
463         if (is_enable)
464                 caps->value |= cap;
465 }
466
467 static int devlink_port_fn_roce_fill(const struct devlink_ops *ops,
468                                      struct devlink_port *devlink_port,
469                                      struct nla_bitfield32 *caps,
470                                      struct netlink_ext_ack *extack)
471 {
472         bool is_enable;
473         int err;
474
475         if (!ops->port_fn_roce_get)
476                 return 0;
477
478         err = ops->port_fn_roce_get(devlink_port, &is_enable, extack);
479         if (err) {
480                 if (err == -EOPNOTSUPP)
481                         return 0;
482                 return err;
483         }
484
485         devlink_port_fn_cap_fill(caps, DEVLINK_PORT_FN_CAP_ROCE, is_enable);
486         return 0;
487 }
488
489 static int devlink_port_fn_migratable_fill(const struct devlink_ops *ops,
490                                            struct devlink_port *devlink_port,
491                                            struct nla_bitfield32 *caps,
492                                            struct netlink_ext_ack *extack)
493 {
494         bool is_enable;
495         int err;
496
497         if (!ops->port_fn_migratable_get ||
498             devlink_port->attrs.flavour != DEVLINK_PORT_FLAVOUR_PCI_VF)
499                 return 0;
500
501         err = ops->port_fn_migratable_get(devlink_port, &is_enable, extack);
502         if (err) {
503                 if (err == -EOPNOTSUPP)
504                         return 0;
505                 return err;
506         }
507
508         devlink_port_fn_cap_fill(caps, DEVLINK_PORT_FN_CAP_MIGRATABLE, is_enable);
509         return 0;
510 }
511
512 static int devlink_port_fn_caps_fill(const struct devlink_ops *ops,
513                                      struct devlink_port *devlink_port,
514                                      struct sk_buff *msg,
515                                      struct netlink_ext_ack *extack,
516                                      bool *msg_updated)
517 {
518         struct nla_bitfield32 caps = {};
519         int err;
520
521         err = devlink_port_fn_roce_fill(ops, devlink_port, &caps, extack);
522         if (err)
523                 return err;
524
525         err = devlink_port_fn_migratable_fill(ops, devlink_port, &caps, extack);
526         if (err)
527                 return err;
528
529         if (!caps.selector)
530                 return 0;
531         err = nla_put_bitfield32(msg, DEVLINK_PORT_FN_ATTR_CAPS, caps.value,
532                                  caps.selector);
533         if (err)
534                 return err;
535
536         *msg_updated = true;
537         return 0;
538 }
539
540 static int
541 devlink_sb_tc_index_get_from_info(struct devlink_sb *devlink_sb,
542                                   struct genl_info *info,
543                                   enum devlink_sb_pool_type pool_type,
544                                   u16 *p_tc_index)
545 {
546         return devlink_sb_tc_index_get_from_attrs(devlink_sb, info->attrs,
547                                                   pool_type, p_tc_index);
548 }
549
550 struct devlink_region {
551         struct devlink *devlink;
552         struct devlink_port *port;
553         struct list_head list;
554         union {
555                 const struct devlink_region_ops *ops;
556                 const struct devlink_port_region_ops *port_ops;
557         };
558         struct mutex snapshot_lock; /* protects snapshot_list,
559                                      * max_snapshots and cur_snapshots
560                                      * consistency.
561                                      */
562         struct list_head snapshot_list;
563         u32 max_snapshots;
564         u32 cur_snapshots;
565         u64 size;
566 };
567
568 struct devlink_snapshot {
569         struct list_head list;
570         struct devlink_region *region;
571         u8 *data;
572         u32 id;
573 };
574
575 static struct devlink_region *
576 devlink_region_get_by_name(struct devlink *devlink, const char *region_name)
577 {
578         struct devlink_region *region;
579
580         list_for_each_entry(region, &devlink->region_list, list)
581                 if (!strcmp(region->ops->name, region_name))
582                         return region;
583
584         return NULL;
585 }
586
587 static struct devlink_region *
588 devlink_port_region_get_by_name(struct devlink_port *port,
589                                 const char *region_name)
590 {
591         struct devlink_region *region;
592
593         list_for_each_entry(region, &port->region_list, list)
594                 if (!strcmp(region->ops->name, region_name))
595                         return region;
596
597         return NULL;
598 }
599
600 static struct devlink_snapshot *
601 devlink_region_snapshot_get_by_id(struct devlink_region *region, u32 id)
602 {
603         struct devlink_snapshot *snapshot;
604
605         list_for_each_entry(snapshot, &region->snapshot_list, list)
606                 if (snapshot->id == id)
607                         return snapshot;
608
609         return NULL;
610 }
611
612 static int devlink_nl_put_handle(struct sk_buff *msg, struct devlink *devlink)
613 {
614         if (nla_put_string(msg, DEVLINK_ATTR_BUS_NAME, devlink->dev->bus->name))
615                 return -EMSGSIZE;
616         if (nla_put_string(msg, DEVLINK_ATTR_DEV_NAME, dev_name(devlink->dev)))
617                 return -EMSGSIZE;
618         return 0;
619 }
620
621 static int devlink_nl_put_nested_handle(struct sk_buff *msg, struct devlink *devlink)
622 {
623         struct nlattr *nested_attr;
624
625         nested_attr = nla_nest_start(msg, DEVLINK_ATTR_NESTED_DEVLINK);
626         if (!nested_attr)
627                 return -EMSGSIZE;
628         if (devlink_nl_put_handle(msg, devlink))
629                 goto nla_put_failure;
630
631         nla_nest_end(msg, nested_attr);
632         return 0;
633
634 nla_put_failure:
635         nla_nest_cancel(msg, nested_attr);
636         return -EMSGSIZE;
637 }
638
639 int devlink_nl_port_handle_fill(struct sk_buff *msg, struct devlink_port *devlink_port)
640 {
641         if (devlink_nl_put_handle(msg, devlink_port->devlink))
642                 return -EMSGSIZE;
643         if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, devlink_port->index))
644                 return -EMSGSIZE;
645         return 0;
646 }
647
648 size_t devlink_nl_port_handle_size(struct devlink_port *devlink_port)
649 {
650         struct devlink *devlink = devlink_port->devlink;
651
652         return nla_total_size(strlen(devlink->dev->bus->name) + 1) /* DEVLINK_ATTR_BUS_NAME */
653              + nla_total_size(strlen(dev_name(devlink->dev)) + 1) /* DEVLINK_ATTR_DEV_NAME */
654              + nla_total_size(4); /* DEVLINK_ATTR_PORT_INDEX */
655 }
656
657 struct devlink_reload_combination {
658         enum devlink_reload_action action;
659         enum devlink_reload_limit limit;
660 };
661
662 static const struct devlink_reload_combination devlink_reload_invalid_combinations[] = {
663         {
664                 /* can't reinitialize driver with no down time */
665                 .action = DEVLINK_RELOAD_ACTION_DRIVER_REINIT,
666                 .limit = DEVLINK_RELOAD_LIMIT_NO_RESET,
667         },
668 };
669
670 static bool
671 devlink_reload_combination_is_invalid(enum devlink_reload_action action,
672                                       enum devlink_reload_limit limit)
673 {
674         int i;
675
676         for (i = 0; i < ARRAY_SIZE(devlink_reload_invalid_combinations); i++)
677                 if (devlink_reload_invalid_combinations[i].action == action &&
678                     devlink_reload_invalid_combinations[i].limit == limit)
679                         return true;
680         return false;
681 }
682
683 static bool
684 devlink_reload_action_is_supported(struct devlink *devlink, enum devlink_reload_action action)
685 {
686         return test_bit(action, &devlink->ops->reload_actions);
687 }
688
689 static bool
690 devlink_reload_limit_is_supported(struct devlink *devlink, enum devlink_reload_limit limit)
691 {
692         return test_bit(limit, &devlink->ops->reload_limits);
693 }
694
695 static int devlink_reload_stat_put(struct sk_buff *msg,
696                                    enum devlink_reload_limit limit, u32 value)
697 {
698         struct nlattr *reload_stats_entry;
699
700         reload_stats_entry = nla_nest_start(msg, DEVLINK_ATTR_RELOAD_STATS_ENTRY);
701         if (!reload_stats_entry)
702                 return -EMSGSIZE;
703
704         if (nla_put_u8(msg, DEVLINK_ATTR_RELOAD_STATS_LIMIT, limit) ||
705             nla_put_u32(msg, DEVLINK_ATTR_RELOAD_STATS_VALUE, value))
706                 goto nla_put_failure;
707         nla_nest_end(msg, reload_stats_entry);
708         return 0;
709
710 nla_put_failure:
711         nla_nest_cancel(msg, reload_stats_entry);
712         return -EMSGSIZE;
713 }
714
715 static int devlink_reload_stats_put(struct sk_buff *msg, struct devlink *devlink, bool is_remote)
716 {
717         struct nlattr *reload_stats_attr, *act_info, *act_stats;
718         int i, j, stat_idx;
719         u32 value;
720
721         if (!is_remote)
722                 reload_stats_attr = nla_nest_start(msg, DEVLINK_ATTR_RELOAD_STATS);
723         else
724                 reload_stats_attr = nla_nest_start(msg, DEVLINK_ATTR_REMOTE_RELOAD_STATS);
725
726         if (!reload_stats_attr)
727                 return -EMSGSIZE;
728
729         for (i = 0; i <= DEVLINK_RELOAD_ACTION_MAX; i++) {
730                 if ((!is_remote &&
731                      !devlink_reload_action_is_supported(devlink, i)) ||
732                     i == DEVLINK_RELOAD_ACTION_UNSPEC)
733                         continue;
734                 act_info = nla_nest_start(msg, DEVLINK_ATTR_RELOAD_ACTION_INFO);
735                 if (!act_info)
736                         goto nla_put_failure;
737
738                 if (nla_put_u8(msg, DEVLINK_ATTR_RELOAD_ACTION, i))
739                         goto action_info_nest_cancel;
740                 act_stats = nla_nest_start(msg, DEVLINK_ATTR_RELOAD_ACTION_STATS);
741                 if (!act_stats)
742                         goto action_info_nest_cancel;
743
744                 for (j = 0; j <= DEVLINK_RELOAD_LIMIT_MAX; j++) {
745                         /* Remote stats are shown even if not locally supported.
746                          * Stats of actions with unspecified limit are shown
747                          * though drivers don't need to register unspecified
748                          * limit.
749                          */
750                         if ((!is_remote && j != DEVLINK_RELOAD_LIMIT_UNSPEC &&
751                              !devlink_reload_limit_is_supported(devlink, j)) ||
752                             devlink_reload_combination_is_invalid(i, j))
753                                 continue;
754
755                         stat_idx = j * __DEVLINK_RELOAD_ACTION_MAX + i;
756                         if (!is_remote)
757                                 value = devlink->stats.reload_stats[stat_idx];
758                         else
759                                 value = devlink->stats.remote_reload_stats[stat_idx];
760                         if (devlink_reload_stat_put(msg, j, value))
761                                 goto action_stats_nest_cancel;
762                 }
763                 nla_nest_end(msg, act_stats);
764                 nla_nest_end(msg, act_info);
765         }
766         nla_nest_end(msg, reload_stats_attr);
767         return 0;
768
769 action_stats_nest_cancel:
770         nla_nest_cancel(msg, act_stats);
771 action_info_nest_cancel:
772         nla_nest_cancel(msg, act_info);
773 nla_put_failure:
774         nla_nest_cancel(msg, reload_stats_attr);
775         return -EMSGSIZE;
776 }
777
778 static int devlink_nl_fill(struct sk_buff *msg, struct devlink *devlink,
779                            enum devlink_command cmd, u32 portid,
780                            u32 seq, int flags)
781 {
782         struct nlattr *dev_stats;
783         void *hdr;
784
785         hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
786         if (!hdr)
787                 return -EMSGSIZE;
788
789         if (devlink_nl_put_handle(msg, devlink))
790                 goto nla_put_failure;
791         if (nla_put_u8(msg, DEVLINK_ATTR_RELOAD_FAILED, devlink->reload_failed))
792                 goto nla_put_failure;
793
794         dev_stats = nla_nest_start(msg, DEVLINK_ATTR_DEV_STATS);
795         if (!dev_stats)
796                 goto nla_put_failure;
797
798         if (devlink_reload_stats_put(msg, devlink, false))
799                 goto dev_stats_nest_cancel;
800         if (devlink_reload_stats_put(msg, devlink, true))
801                 goto dev_stats_nest_cancel;
802
803         nla_nest_end(msg, dev_stats);
804         genlmsg_end(msg, hdr);
805         return 0;
806
807 dev_stats_nest_cancel:
808         nla_nest_cancel(msg, dev_stats);
809 nla_put_failure:
810         genlmsg_cancel(msg, hdr);
811         return -EMSGSIZE;
812 }
813
814 static void devlink_notify(struct devlink *devlink, enum devlink_command cmd)
815 {
816         struct sk_buff *msg;
817         int err;
818
819         WARN_ON(cmd != DEVLINK_CMD_NEW && cmd != DEVLINK_CMD_DEL);
820         WARN_ON(!xa_get_mark(&devlinks, devlink->index, DEVLINK_REGISTERED));
821
822         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
823         if (!msg)
824                 return;
825
826         err = devlink_nl_fill(msg, devlink, cmd, 0, 0, 0);
827         if (err) {
828                 nlmsg_free(msg);
829                 return;
830         }
831
832         genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
833                                 msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
834 }
835
836 static int devlink_nl_port_attrs_put(struct sk_buff *msg,
837                                      struct devlink_port *devlink_port)
838 {
839         struct devlink_port_attrs *attrs = &devlink_port->attrs;
840
841         if (!devlink_port->attrs_set)
842                 return 0;
843         if (attrs->lanes) {
844                 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_LANES, attrs->lanes))
845                         return -EMSGSIZE;
846         }
847         if (nla_put_u8(msg, DEVLINK_ATTR_PORT_SPLITTABLE, attrs->splittable))
848                 return -EMSGSIZE;
849         if (nla_put_u16(msg, DEVLINK_ATTR_PORT_FLAVOUR, attrs->flavour))
850                 return -EMSGSIZE;
851         switch (devlink_port->attrs.flavour) {
852         case DEVLINK_PORT_FLAVOUR_PCI_PF:
853                 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_CONTROLLER_NUMBER,
854                                 attrs->pci_pf.controller) ||
855                     nla_put_u16(msg, DEVLINK_ATTR_PORT_PCI_PF_NUMBER, attrs->pci_pf.pf))
856                         return -EMSGSIZE;
857                 if (nla_put_u8(msg, DEVLINK_ATTR_PORT_EXTERNAL, attrs->pci_pf.external))
858                         return -EMSGSIZE;
859                 break;
860         case DEVLINK_PORT_FLAVOUR_PCI_VF:
861                 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_CONTROLLER_NUMBER,
862                                 attrs->pci_vf.controller) ||
863                     nla_put_u16(msg, DEVLINK_ATTR_PORT_PCI_PF_NUMBER, attrs->pci_vf.pf) ||
864                     nla_put_u16(msg, DEVLINK_ATTR_PORT_PCI_VF_NUMBER, attrs->pci_vf.vf))
865                         return -EMSGSIZE;
866                 if (nla_put_u8(msg, DEVLINK_ATTR_PORT_EXTERNAL, attrs->pci_vf.external))
867                         return -EMSGSIZE;
868                 break;
869         case DEVLINK_PORT_FLAVOUR_PCI_SF:
870                 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_CONTROLLER_NUMBER,
871                                 attrs->pci_sf.controller) ||
872                     nla_put_u16(msg, DEVLINK_ATTR_PORT_PCI_PF_NUMBER,
873                                 attrs->pci_sf.pf) ||
874                     nla_put_u32(msg, DEVLINK_ATTR_PORT_PCI_SF_NUMBER,
875                                 attrs->pci_sf.sf))
876                         return -EMSGSIZE;
877                 break;
878         case DEVLINK_PORT_FLAVOUR_PHYSICAL:
879         case DEVLINK_PORT_FLAVOUR_CPU:
880         case DEVLINK_PORT_FLAVOUR_DSA:
881                 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_NUMBER,
882                                 attrs->phys.port_number))
883                         return -EMSGSIZE;
884                 if (!attrs->split)
885                         return 0;
886                 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_SPLIT_GROUP,
887                                 attrs->phys.port_number))
888                         return -EMSGSIZE;
889                 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_SPLIT_SUBPORT_NUMBER,
890                                 attrs->phys.split_subport_number))
891                         return -EMSGSIZE;
892                 break;
893         default:
894                 break;
895         }
896         return 0;
897 }
898
899 static int devlink_port_fn_hw_addr_fill(const struct devlink_ops *ops,
900                                         struct devlink_port *port,
901                                         struct sk_buff *msg,
902                                         struct netlink_ext_ack *extack,
903                                         bool *msg_updated)
904 {
905         u8 hw_addr[MAX_ADDR_LEN];
906         int hw_addr_len;
907         int err;
908
909         if (!ops->port_function_hw_addr_get)
910                 return 0;
911
912         err = ops->port_function_hw_addr_get(port, hw_addr, &hw_addr_len,
913                                              extack);
914         if (err) {
915                 if (err == -EOPNOTSUPP)
916                         return 0;
917                 return err;
918         }
919         err = nla_put(msg, DEVLINK_PORT_FUNCTION_ATTR_HW_ADDR, hw_addr_len, hw_addr);
920         if (err)
921                 return err;
922         *msg_updated = true;
923         return 0;
924 }
925
926 static int devlink_nl_rate_fill(struct sk_buff *msg,
927                                 struct devlink_rate *devlink_rate,
928                                 enum devlink_command cmd, u32 portid, u32 seq,
929                                 int flags, struct netlink_ext_ack *extack)
930 {
931         struct devlink *devlink = devlink_rate->devlink;
932         void *hdr;
933
934         hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
935         if (!hdr)
936                 return -EMSGSIZE;
937
938         if (devlink_nl_put_handle(msg, devlink))
939                 goto nla_put_failure;
940
941         if (nla_put_u16(msg, DEVLINK_ATTR_RATE_TYPE, devlink_rate->type))
942                 goto nla_put_failure;
943
944         if (devlink_rate_is_leaf(devlink_rate)) {
945                 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX,
946                                 devlink_rate->devlink_port->index))
947                         goto nla_put_failure;
948         } else if (devlink_rate_is_node(devlink_rate)) {
949                 if (nla_put_string(msg, DEVLINK_ATTR_RATE_NODE_NAME,
950                                    devlink_rate->name))
951                         goto nla_put_failure;
952         }
953
954         if (nla_put_u64_64bit(msg, DEVLINK_ATTR_RATE_TX_SHARE,
955                               devlink_rate->tx_share, DEVLINK_ATTR_PAD))
956                 goto nla_put_failure;
957
958         if (nla_put_u64_64bit(msg, DEVLINK_ATTR_RATE_TX_MAX,
959                               devlink_rate->tx_max, DEVLINK_ATTR_PAD))
960                 goto nla_put_failure;
961
962         if (nla_put_u32(msg, DEVLINK_ATTR_RATE_TX_PRIORITY,
963                         devlink_rate->tx_priority))
964                 goto nla_put_failure;
965
966         if (nla_put_u32(msg, DEVLINK_ATTR_RATE_TX_WEIGHT,
967                         devlink_rate->tx_weight))
968                 goto nla_put_failure;
969
970         if (devlink_rate->parent)
971                 if (nla_put_string(msg, DEVLINK_ATTR_RATE_PARENT_NODE_NAME,
972                                    devlink_rate->parent->name))
973                         goto nla_put_failure;
974
975         genlmsg_end(msg, hdr);
976         return 0;
977
978 nla_put_failure:
979         genlmsg_cancel(msg, hdr);
980         return -EMSGSIZE;
981 }
982
983 static bool
984 devlink_port_fn_state_valid(enum devlink_port_fn_state state)
985 {
986         return state == DEVLINK_PORT_FN_STATE_INACTIVE ||
987                state == DEVLINK_PORT_FN_STATE_ACTIVE;
988 }
989
990 static bool
991 devlink_port_fn_opstate_valid(enum devlink_port_fn_opstate opstate)
992 {
993         return opstate == DEVLINK_PORT_FN_OPSTATE_DETACHED ||
994                opstate == DEVLINK_PORT_FN_OPSTATE_ATTACHED;
995 }
996
997 static int devlink_port_fn_state_fill(const struct devlink_ops *ops,
998                                       struct devlink_port *port,
999                                       struct sk_buff *msg,
1000                                       struct netlink_ext_ack *extack,
1001                                       bool *msg_updated)
1002 {
1003         enum devlink_port_fn_opstate opstate;
1004         enum devlink_port_fn_state state;
1005         int err;
1006
1007         if (!ops->port_fn_state_get)
1008                 return 0;
1009
1010         err = ops->port_fn_state_get(port, &state, &opstate, extack);
1011         if (err) {
1012                 if (err == -EOPNOTSUPP)
1013                         return 0;
1014                 return err;
1015         }
1016         if (!devlink_port_fn_state_valid(state)) {
1017                 WARN_ON_ONCE(1);
1018                 NL_SET_ERR_MSG_MOD(extack, "Invalid state read from driver");
1019                 return -EINVAL;
1020         }
1021         if (!devlink_port_fn_opstate_valid(opstate)) {
1022                 WARN_ON_ONCE(1);
1023                 NL_SET_ERR_MSG_MOD(extack,
1024                                    "Invalid operational state read from driver");
1025                 return -EINVAL;
1026         }
1027         if (nla_put_u8(msg, DEVLINK_PORT_FN_ATTR_STATE, state) ||
1028             nla_put_u8(msg, DEVLINK_PORT_FN_ATTR_OPSTATE, opstate))
1029                 return -EMSGSIZE;
1030         *msg_updated = true;
1031         return 0;
1032 }
1033
1034 static int
1035 devlink_port_fn_mig_set(struct devlink_port *devlink_port, bool enable,
1036                         struct netlink_ext_ack *extack)
1037 {
1038         const struct devlink_ops *ops = devlink_port->devlink->ops;
1039
1040         return ops->port_fn_migratable_set(devlink_port, enable, extack);
1041 }
1042
1043 static int
1044 devlink_port_fn_roce_set(struct devlink_port *devlink_port, bool enable,
1045                          struct netlink_ext_ack *extack)
1046 {
1047         const struct devlink_ops *ops = devlink_port->devlink->ops;
1048
1049         return ops->port_fn_roce_set(devlink_port, enable, extack);
1050 }
1051
1052 static int devlink_port_fn_caps_set(struct devlink_port *devlink_port,
1053                                     const struct nlattr *attr,
1054                                     struct netlink_ext_ack *extack)
1055 {
1056         struct nla_bitfield32 caps;
1057         u32 caps_value;
1058         int err;
1059
1060         caps = nla_get_bitfield32(attr);
1061         caps_value = caps.value & caps.selector;
1062         if (caps.selector & DEVLINK_PORT_FN_CAP_ROCE) {
1063                 err = devlink_port_fn_roce_set(devlink_port,
1064                                                caps_value & DEVLINK_PORT_FN_CAP_ROCE,
1065                                                extack);
1066                 if (err)
1067                         return err;
1068         }
1069         if (caps.selector & DEVLINK_PORT_FN_CAP_MIGRATABLE) {
1070                 err = devlink_port_fn_mig_set(devlink_port, caps_value &
1071                                               DEVLINK_PORT_FN_CAP_MIGRATABLE,
1072                                               extack);
1073                 if (err)
1074                         return err;
1075         }
1076         return 0;
1077 }
1078
1079 static int
1080 devlink_nl_port_function_attrs_put(struct sk_buff *msg, struct devlink_port *port,
1081                                    struct netlink_ext_ack *extack)
1082 {
1083         const struct devlink_ops *ops;
1084         struct nlattr *function_attr;
1085         bool msg_updated = false;
1086         int err;
1087
1088         function_attr = nla_nest_start_noflag(msg, DEVLINK_ATTR_PORT_FUNCTION);
1089         if (!function_attr)
1090                 return -EMSGSIZE;
1091
1092         ops = port->devlink->ops;
1093         err = devlink_port_fn_hw_addr_fill(ops, port, msg, extack,
1094                                            &msg_updated);
1095         if (err)
1096                 goto out;
1097         err = devlink_port_fn_caps_fill(ops, port, msg, extack,
1098                                         &msg_updated);
1099         if (err)
1100                 goto out;
1101         err = devlink_port_fn_state_fill(ops, port, msg, extack, &msg_updated);
1102 out:
1103         if (err || !msg_updated)
1104                 nla_nest_cancel(msg, function_attr);
1105         else
1106                 nla_nest_end(msg, function_attr);
1107         return err;
1108 }
1109
1110 static int devlink_nl_port_fill(struct sk_buff *msg,
1111                                 struct devlink_port *devlink_port,
1112                                 enum devlink_command cmd, u32 portid, u32 seq,
1113                                 int flags, struct netlink_ext_ack *extack)
1114 {
1115         struct devlink *devlink = devlink_port->devlink;
1116         void *hdr;
1117
1118         hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
1119         if (!hdr)
1120                 return -EMSGSIZE;
1121
1122         if (devlink_nl_put_handle(msg, devlink))
1123                 goto nla_put_failure;
1124         if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, devlink_port->index))
1125                 goto nla_put_failure;
1126
1127         spin_lock_bh(&devlink_port->type_lock);
1128         if (nla_put_u16(msg, DEVLINK_ATTR_PORT_TYPE, devlink_port->type))
1129                 goto nla_put_failure_type_locked;
1130         if (devlink_port->desired_type != DEVLINK_PORT_TYPE_NOTSET &&
1131             nla_put_u16(msg, DEVLINK_ATTR_PORT_DESIRED_TYPE,
1132                         devlink_port->desired_type))
1133                 goto nla_put_failure_type_locked;
1134         if (devlink_port->type == DEVLINK_PORT_TYPE_ETH) {
1135                 if (devlink_port->type_eth.netdev &&
1136                     (nla_put_u32(msg, DEVLINK_ATTR_PORT_NETDEV_IFINDEX,
1137                                  devlink_port->type_eth.ifindex) ||
1138                      nla_put_string(msg, DEVLINK_ATTR_PORT_NETDEV_NAME,
1139                                     devlink_port->type_eth.ifname)))
1140                         goto nla_put_failure_type_locked;
1141         }
1142         if (devlink_port->type == DEVLINK_PORT_TYPE_IB) {
1143                 struct ib_device *ibdev = devlink_port->type_ib.ibdev;
1144
1145                 if (ibdev &&
1146                     nla_put_string(msg, DEVLINK_ATTR_PORT_IBDEV_NAME,
1147                                    ibdev->name))
1148                         goto nla_put_failure_type_locked;
1149         }
1150         spin_unlock_bh(&devlink_port->type_lock);
1151         if (devlink_nl_port_attrs_put(msg, devlink_port))
1152                 goto nla_put_failure;
1153         if (devlink_nl_port_function_attrs_put(msg, devlink_port, extack))
1154                 goto nla_put_failure;
1155         if (devlink_port->linecard &&
1156             nla_put_u32(msg, DEVLINK_ATTR_LINECARD_INDEX,
1157                         devlink_port->linecard->index))
1158                 goto nla_put_failure;
1159
1160         genlmsg_end(msg, hdr);
1161         return 0;
1162
1163 nla_put_failure_type_locked:
1164         spin_unlock_bh(&devlink_port->type_lock);
1165 nla_put_failure:
1166         genlmsg_cancel(msg, hdr);
1167         return -EMSGSIZE;
1168 }
1169
1170 static void devlink_port_notify(struct devlink_port *devlink_port,
1171                                 enum devlink_command cmd)
1172 {
1173         struct devlink *devlink = devlink_port->devlink;
1174         struct sk_buff *msg;
1175         int err;
1176
1177         WARN_ON(cmd != DEVLINK_CMD_PORT_NEW && cmd != DEVLINK_CMD_PORT_DEL);
1178
1179         if (!xa_get_mark(&devlinks, devlink->index, DEVLINK_REGISTERED))
1180                 return;
1181
1182         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1183         if (!msg)
1184                 return;
1185
1186         err = devlink_nl_port_fill(msg, devlink_port, cmd, 0, 0, 0, NULL);
1187         if (err) {
1188                 nlmsg_free(msg);
1189                 return;
1190         }
1191
1192         genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink), msg,
1193                                 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
1194 }
1195
1196 static void devlink_rate_notify(struct devlink_rate *devlink_rate,
1197                                 enum devlink_command cmd)
1198 {
1199         struct devlink *devlink = devlink_rate->devlink;
1200         struct sk_buff *msg;
1201         int err;
1202
1203         WARN_ON(cmd != DEVLINK_CMD_RATE_NEW && cmd != DEVLINK_CMD_RATE_DEL);
1204
1205         if (!xa_get_mark(&devlinks, devlink->index, DEVLINK_REGISTERED))
1206                 return;
1207
1208         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1209         if (!msg)
1210                 return;
1211
1212         err = devlink_nl_rate_fill(msg, devlink_rate, cmd, 0, 0, 0, NULL);
1213         if (err) {
1214                 nlmsg_free(msg);
1215                 return;
1216         }
1217
1218         genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink), msg,
1219                                 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
1220 }
1221
1222 static int devlink_nl_cmd_rate_get_dumpit(struct sk_buff *msg,
1223                                           struct netlink_callback *cb)
1224 {
1225         struct devlink_nl_dump_state *state = devlink_dump_state(cb);
1226         struct devlink_rate *devlink_rate;
1227         struct devlink *devlink;
1228         int start = state->idx;
1229         unsigned long index;
1230         int idx = 0;
1231         int err = 0;
1232
1233         devlinks_xa_for_each_registered_get(sock_net(msg->sk), index, devlink) {
1234                 devl_lock(devlink);
1235                 list_for_each_entry(devlink_rate, &devlink->rate_list, list) {
1236                         enum devlink_command cmd = DEVLINK_CMD_RATE_NEW;
1237                         u32 id = NETLINK_CB(cb->skb).portid;
1238
1239                         if (idx < start) {
1240                                 idx++;
1241                                 continue;
1242                         }
1243                         err = devlink_nl_rate_fill(msg, devlink_rate, cmd, id,
1244                                                    cb->nlh->nlmsg_seq,
1245                                                    NLM_F_MULTI, NULL);
1246                         if (err) {
1247                                 devl_unlock(devlink);
1248                                 devlink_put(devlink);
1249                                 goto out;
1250                         }
1251                         idx++;
1252                 }
1253                 devl_unlock(devlink);
1254                 devlink_put(devlink);
1255         }
1256 out:
1257         if (err != -EMSGSIZE)
1258                 return err;
1259
1260         state->idx = idx;
1261         return msg->len;
1262 }
1263
1264 static int devlink_nl_cmd_rate_get_doit(struct sk_buff *skb,
1265                                         struct genl_info *info)
1266 {
1267         struct devlink_rate *devlink_rate = info->user_ptr[1];
1268         struct sk_buff *msg;
1269         int err;
1270
1271         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1272         if (!msg)
1273                 return -ENOMEM;
1274
1275         err = devlink_nl_rate_fill(msg, devlink_rate, DEVLINK_CMD_RATE_NEW,
1276                                    info->snd_portid, info->snd_seq, 0,
1277                                    info->extack);
1278         if (err) {
1279                 nlmsg_free(msg);
1280                 return err;
1281         }
1282
1283         return genlmsg_reply(msg, info);
1284 }
1285
1286 static bool
1287 devlink_rate_is_parent_node(struct devlink_rate *devlink_rate,
1288                             struct devlink_rate *parent)
1289 {
1290         while (parent) {
1291                 if (parent == devlink_rate)
1292                         return true;
1293                 parent = parent->parent;
1294         }
1295         return false;
1296 }
1297
1298 static int devlink_nl_cmd_get_doit(struct sk_buff *skb, struct genl_info *info)
1299 {
1300         struct devlink *devlink = info->user_ptr[0];
1301         struct sk_buff *msg;
1302         int err;
1303
1304         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1305         if (!msg)
1306                 return -ENOMEM;
1307
1308         err = devlink_nl_fill(msg, devlink, DEVLINK_CMD_NEW,
1309                               info->snd_portid, info->snd_seq, 0);
1310         if (err) {
1311                 nlmsg_free(msg);
1312                 return err;
1313         }
1314
1315         return genlmsg_reply(msg, info);
1316 }
1317
1318 static int devlink_nl_cmd_get_dumpit(struct sk_buff *msg,
1319                                      struct netlink_callback *cb)
1320 {
1321         struct devlink_nl_dump_state *state = devlink_dump_state(cb);
1322         struct devlink *devlink;
1323         int start = state->idx;
1324         unsigned long index;
1325         int idx = 0;
1326         int err;
1327
1328         devlinks_xa_for_each_registered_get(sock_net(msg->sk), index, devlink) {
1329                 if (idx < start) {
1330                         idx++;
1331                         devlink_put(devlink);
1332                         continue;
1333                 }
1334
1335                 devl_lock(devlink);
1336                 err = devlink_nl_fill(msg, devlink, DEVLINK_CMD_NEW,
1337                                       NETLINK_CB(cb->skb).portid,
1338                                       cb->nlh->nlmsg_seq, NLM_F_MULTI);
1339                 devl_unlock(devlink);
1340                 devlink_put(devlink);
1341
1342                 if (err)
1343                         goto out;
1344                 idx++;
1345         }
1346 out:
1347         state->idx = idx;
1348         return msg->len;
1349 }
1350
1351 static int devlink_nl_cmd_port_get_doit(struct sk_buff *skb,
1352                                         struct genl_info *info)
1353 {
1354         struct devlink_port *devlink_port = info->user_ptr[1];
1355         struct sk_buff *msg;
1356         int err;
1357
1358         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1359         if (!msg)
1360                 return -ENOMEM;
1361
1362         err = devlink_nl_port_fill(msg, devlink_port, DEVLINK_CMD_PORT_NEW,
1363                                    info->snd_portid, info->snd_seq, 0,
1364                                    info->extack);
1365         if (err) {
1366                 nlmsg_free(msg);
1367                 return err;
1368         }
1369
1370         return genlmsg_reply(msg, info);
1371 }
1372
1373 static int devlink_nl_cmd_port_get_dumpit(struct sk_buff *msg,
1374                                           struct netlink_callback *cb)
1375 {
1376         struct devlink_nl_dump_state *state = devlink_dump_state(cb);
1377         struct devlink *devlink;
1378         struct devlink_port *devlink_port;
1379         unsigned long index, port_index;
1380         int start = state->idx;
1381         int idx = 0;
1382         int err;
1383
1384         devlinks_xa_for_each_registered_get(sock_net(msg->sk), index, devlink) {
1385                 devl_lock(devlink);
1386                 xa_for_each(&devlink->ports, port_index, devlink_port) {
1387                         if (idx < start) {
1388                                 idx++;
1389                                 continue;
1390                         }
1391                         err = devlink_nl_port_fill(msg, devlink_port,
1392                                                    DEVLINK_CMD_NEW,
1393                                                    NETLINK_CB(cb->skb).portid,
1394                                                    cb->nlh->nlmsg_seq,
1395                                                    NLM_F_MULTI, cb->extack);
1396                         if (err) {
1397                                 devl_unlock(devlink);
1398                                 devlink_put(devlink);
1399                                 goto out;
1400                         }
1401                         idx++;
1402                 }
1403                 devl_unlock(devlink);
1404                 devlink_put(devlink);
1405         }
1406 out:
1407         state->idx = idx;
1408         return msg->len;
1409 }
1410
1411 static int devlink_port_type_set(struct devlink_port *devlink_port,
1412                                  enum devlink_port_type port_type)
1413
1414 {
1415         int err;
1416
1417         if (!devlink_port->devlink->ops->port_type_set)
1418                 return -EOPNOTSUPP;
1419
1420         if (port_type == devlink_port->type)
1421                 return 0;
1422
1423         err = devlink_port->devlink->ops->port_type_set(devlink_port,
1424                                                         port_type);
1425         if (err)
1426                 return err;
1427
1428         devlink_port->desired_type = port_type;
1429         devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW);
1430         return 0;
1431 }
1432
1433 static int devlink_port_function_hw_addr_set(struct devlink_port *port,
1434                                              const struct nlattr *attr,
1435                                              struct netlink_ext_ack *extack)
1436 {
1437         const struct devlink_ops *ops = port->devlink->ops;
1438         const u8 *hw_addr;
1439         int hw_addr_len;
1440
1441         hw_addr = nla_data(attr);
1442         hw_addr_len = nla_len(attr);
1443         if (hw_addr_len > MAX_ADDR_LEN) {
1444                 NL_SET_ERR_MSG_MOD(extack, "Port function hardware address too long");
1445                 return -EINVAL;
1446         }
1447         if (port->type == DEVLINK_PORT_TYPE_ETH) {
1448                 if (hw_addr_len != ETH_ALEN) {
1449                         NL_SET_ERR_MSG_MOD(extack, "Address must be 6 bytes for Ethernet device");
1450                         return -EINVAL;
1451                 }
1452                 if (!is_unicast_ether_addr(hw_addr)) {
1453                         NL_SET_ERR_MSG_MOD(extack, "Non-unicast hardware address unsupported");
1454                         return -EINVAL;
1455                 }
1456         }
1457
1458         return ops->port_function_hw_addr_set(port, hw_addr, hw_addr_len,
1459                                               extack);
1460 }
1461
1462 static int devlink_port_fn_state_set(struct devlink_port *port,
1463                                      const struct nlattr *attr,
1464                                      struct netlink_ext_ack *extack)
1465 {
1466         enum devlink_port_fn_state state;
1467         const struct devlink_ops *ops;
1468
1469         state = nla_get_u8(attr);
1470         ops = port->devlink->ops;
1471         return ops->port_fn_state_set(port, state, extack);
1472 }
1473
1474 static int devlink_port_function_validate(struct devlink_port *devlink_port,
1475                                           struct nlattr **tb,
1476                                           struct netlink_ext_ack *extack)
1477 {
1478         const struct devlink_ops *ops = devlink_port->devlink->ops;
1479         struct nlattr *attr;
1480
1481         if (tb[DEVLINK_PORT_FUNCTION_ATTR_HW_ADDR] &&
1482             !ops->port_function_hw_addr_set) {
1483                 NL_SET_ERR_MSG_ATTR(extack, tb[DEVLINK_PORT_FUNCTION_ATTR_HW_ADDR],
1484                                     "Port doesn't support function attributes");
1485                 return -EOPNOTSUPP;
1486         }
1487         if (tb[DEVLINK_PORT_FN_ATTR_STATE] && !ops->port_fn_state_set) {
1488                 NL_SET_ERR_MSG_ATTR(extack, tb[DEVLINK_PORT_FUNCTION_ATTR_HW_ADDR],
1489                                     "Function does not support state setting");
1490                 return -EOPNOTSUPP;
1491         }
1492         attr = tb[DEVLINK_PORT_FN_ATTR_CAPS];
1493         if (attr) {
1494                 struct nla_bitfield32 caps;
1495
1496                 caps = nla_get_bitfield32(attr);
1497                 if (caps.selector & DEVLINK_PORT_FN_CAP_ROCE &&
1498                     !ops->port_fn_roce_set) {
1499                         NL_SET_ERR_MSG_ATTR(extack, attr,
1500                                             "Port doesn't support RoCE function attribute");
1501                         return -EOPNOTSUPP;
1502                 }
1503                 if (caps.selector & DEVLINK_PORT_FN_CAP_MIGRATABLE) {
1504                         if (!ops->port_fn_migratable_set) {
1505                                 NL_SET_ERR_MSG_ATTR(extack, attr,
1506                                                     "Port doesn't support migratable function attribute");
1507                                 return -EOPNOTSUPP;
1508                         }
1509                         if (devlink_port->attrs.flavour != DEVLINK_PORT_FLAVOUR_PCI_VF) {
1510                                 NL_SET_ERR_MSG_ATTR(extack, attr,
1511                                                     "migratable function attribute supported for VFs only");
1512                                 return -EOPNOTSUPP;
1513                         }
1514                 }
1515         }
1516         return 0;
1517 }
1518
1519 static int devlink_port_function_set(struct devlink_port *port,
1520                                      const struct nlattr *attr,
1521                                      struct netlink_ext_ack *extack)
1522 {
1523         struct nlattr *tb[DEVLINK_PORT_FUNCTION_ATTR_MAX + 1];
1524         int err;
1525
1526         err = nla_parse_nested(tb, DEVLINK_PORT_FUNCTION_ATTR_MAX, attr,
1527                                devlink_function_nl_policy, extack);
1528         if (err < 0) {
1529                 NL_SET_ERR_MSG_MOD(extack, "Fail to parse port function attributes");
1530                 return err;
1531         }
1532
1533         err = devlink_port_function_validate(port, tb, extack);
1534         if (err)
1535                 return err;
1536
1537         attr = tb[DEVLINK_PORT_FUNCTION_ATTR_HW_ADDR];
1538         if (attr) {
1539                 err = devlink_port_function_hw_addr_set(port, attr, extack);
1540                 if (err)
1541                         return err;
1542         }
1543
1544         attr = tb[DEVLINK_PORT_FN_ATTR_CAPS];
1545         if (attr) {
1546                 err = devlink_port_fn_caps_set(port, attr, extack);
1547                 if (err)
1548                         return err;
1549         }
1550
1551         /* Keep this as the last function attribute set, so that when
1552          * multiple port function attributes are set along with state,
1553          * Those can be applied first before activating the state.
1554          */
1555         attr = tb[DEVLINK_PORT_FN_ATTR_STATE];
1556         if (attr)
1557                 err = devlink_port_fn_state_set(port, attr, extack);
1558
1559         if (!err)
1560                 devlink_port_notify(port, DEVLINK_CMD_PORT_NEW);
1561         return err;
1562 }
1563
1564 static int devlink_nl_cmd_port_set_doit(struct sk_buff *skb,
1565                                         struct genl_info *info)
1566 {
1567         struct devlink_port *devlink_port = info->user_ptr[1];
1568         int err;
1569
1570         if (info->attrs[DEVLINK_ATTR_PORT_TYPE]) {
1571                 enum devlink_port_type port_type;
1572
1573                 port_type = nla_get_u16(info->attrs[DEVLINK_ATTR_PORT_TYPE]);
1574                 err = devlink_port_type_set(devlink_port, port_type);
1575                 if (err)
1576                         return err;
1577         }
1578
1579         if (info->attrs[DEVLINK_ATTR_PORT_FUNCTION]) {
1580                 struct nlattr *attr = info->attrs[DEVLINK_ATTR_PORT_FUNCTION];
1581                 struct netlink_ext_ack *extack = info->extack;
1582
1583                 err = devlink_port_function_set(devlink_port, attr, extack);
1584                 if (err)
1585                         return err;
1586         }
1587
1588         return 0;
1589 }
1590
1591 static int devlink_nl_cmd_port_split_doit(struct sk_buff *skb,
1592                                           struct genl_info *info)
1593 {
1594         struct devlink_port *devlink_port = info->user_ptr[1];
1595         struct devlink *devlink = info->user_ptr[0];
1596         u32 count;
1597
1598         if (GENL_REQ_ATTR_CHECK(info, DEVLINK_ATTR_PORT_SPLIT_COUNT))
1599                 return -EINVAL;
1600         if (!devlink->ops->port_split)
1601                 return -EOPNOTSUPP;
1602
1603         count = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_SPLIT_COUNT]);
1604
1605         if (!devlink_port->attrs.splittable) {
1606                 /* Split ports cannot be split. */
1607                 if (devlink_port->attrs.split)
1608                         NL_SET_ERR_MSG_MOD(info->extack, "Port cannot be split further");
1609                 else
1610                         NL_SET_ERR_MSG_MOD(info->extack, "Port cannot be split");
1611                 return -EINVAL;
1612         }
1613
1614         if (count < 2 || !is_power_of_2(count) || count > devlink_port->attrs.lanes) {
1615                 NL_SET_ERR_MSG_MOD(info->extack, "Invalid split count");
1616                 return -EINVAL;
1617         }
1618
1619         return devlink->ops->port_split(devlink, devlink_port, count,
1620                                         info->extack);
1621 }
1622
1623 static int devlink_nl_cmd_port_unsplit_doit(struct sk_buff *skb,
1624                                             struct genl_info *info)
1625 {
1626         struct devlink_port *devlink_port = info->user_ptr[1];
1627         struct devlink *devlink = info->user_ptr[0];
1628
1629         if (!devlink->ops->port_unsplit)
1630                 return -EOPNOTSUPP;
1631         return devlink->ops->port_unsplit(devlink, devlink_port, info->extack);
1632 }
1633
1634 static int devlink_port_new_notify(struct devlink *devlink,
1635                                    unsigned int port_index,
1636                                    struct genl_info *info)
1637 {
1638         struct devlink_port *devlink_port;
1639         struct sk_buff *msg;
1640         int err;
1641
1642         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1643         if (!msg)
1644                 return -ENOMEM;
1645
1646         lockdep_assert_held(&devlink->lock);
1647         devlink_port = devlink_port_get_by_index(devlink, port_index);
1648         if (!devlink_port) {
1649                 err = -ENODEV;
1650                 goto out;
1651         }
1652
1653         err = devlink_nl_port_fill(msg, devlink_port, DEVLINK_CMD_NEW,
1654                                    info->snd_portid, info->snd_seq, 0, NULL);
1655         if (err)
1656                 goto out;
1657
1658         return genlmsg_reply(msg, info);
1659
1660 out:
1661         nlmsg_free(msg);
1662         return err;
1663 }
1664
1665 static int devlink_nl_cmd_port_new_doit(struct sk_buff *skb,
1666                                         struct genl_info *info)
1667 {
1668         struct netlink_ext_ack *extack = info->extack;
1669         struct devlink_port_new_attrs new_attrs = {};
1670         struct devlink *devlink = info->user_ptr[0];
1671         unsigned int new_port_index;
1672         int err;
1673
1674         if (!devlink->ops->port_new || !devlink->ops->port_del)
1675                 return -EOPNOTSUPP;
1676
1677         if (!info->attrs[DEVLINK_ATTR_PORT_FLAVOUR] ||
1678             !info->attrs[DEVLINK_ATTR_PORT_PCI_PF_NUMBER]) {
1679                 NL_SET_ERR_MSG_MOD(extack, "Port flavour or PCI PF are not specified");
1680                 return -EINVAL;
1681         }
1682         new_attrs.flavour = nla_get_u16(info->attrs[DEVLINK_ATTR_PORT_FLAVOUR]);
1683         new_attrs.pfnum =
1684                 nla_get_u16(info->attrs[DEVLINK_ATTR_PORT_PCI_PF_NUMBER]);
1685
1686         if (info->attrs[DEVLINK_ATTR_PORT_INDEX]) {
1687                 /* Port index of the new port being created by driver. */
1688                 new_attrs.port_index =
1689                         nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_INDEX]);
1690                 new_attrs.port_index_valid = true;
1691         }
1692         if (info->attrs[DEVLINK_ATTR_PORT_CONTROLLER_NUMBER]) {
1693                 new_attrs.controller =
1694                         nla_get_u16(info->attrs[DEVLINK_ATTR_PORT_CONTROLLER_NUMBER]);
1695                 new_attrs.controller_valid = true;
1696         }
1697         if (new_attrs.flavour == DEVLINK_PORT_FLAVOUR_PCI_SF &&
1698             info->attrs[DEVLINK_ATTR_PORT_PCI_SF_NUMBER]) {
1699                 new_attrs.sfnum = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_PCI_SF_NUMBER]);
1700                 new_attrs.sfnum_valid = true;
1701         }
1702
1703         err = devlink->ops->port_new(devlink, &new_attrs, extack,
1704                                      &new_port_index);
1705         if (err)
1706                 return err;
1707
1708         err = devlink_port_new_notify(devlink, new_port_index, info);
1709         if (err && err != -ENODEV) {
1710                 /* Fail to send the response; destroy newly created port. */
1711                 devlink->ops->port_del(devlink, new_port_index, extack);
1712         }
1713         return err;
1714 }
1715
1716 static int devlink_nl_cmd_port_del_doit(struct sk_buff *skb,
1717                                         struct genl_info *info)
1718 {
1719         struct netlink_ext_ack *extack = info->extack;
1720         struct devlink *devlink = info->user_ptr[0];
1721         unsigned int port_index;
1722
1723         if (!devlink->ops->port_del)
1724                 return -EOPNOTSUPP;
1725
1726         if (GENL_REQ_ATTR_CHECK(info, DEVLINK_ATTR_PORT_INDEX)) {
1727                 NL_SET_ERR_MSG_MOD(extack, "Port index is not specified");
1728                 return -EINVAL;
1729         }
1730         port_index = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_INDEX]);
1731
1732         return devlink->ops->port_del(devlink, port_index, extack);
1733 }
1734
1735 static int
1736 devlink_nl_rate_parent_node_set(struct devlink_rate *devlink_rate,
1737                                 struct genl_info *info,
1738                                 struct nlattr *nla_parent)
1739 {
1740         struct devlink *devlink = devlink_rate->devlink;
1741         const char *parent_name = nla_data(nla_parent);
1742         const struct devlink_ops *ops = devlink->ops;
1743         size_t len = strlen(parent_name);
1744         struct devlink_rate *parent;
1745         int err = -EOPNOTSUPP;
1746
1747         parent = devlink_rate->parent;
1748
1749         if (parent && !len) {
1750                 if (devlink_rate_is_leaf(devlink_rate))
1751                         err = ops->rate_leaf_parent_set(devlink_rate, NULL,
1752                                                         devlink_rate->priv, NULL,
1753                                                         info->extack);
1754                 else if (devlink_rate_is_node(devlink_rate))
1755                         err = ops->rate_node_parent_set(devlink_rate, NULL,
1756                                                         devlink_rate->priv, NULL,
1757                                                         info->extack);
1758                 if (err)
1759                         return err;
1760
1761                 refcount_dec(&parent->refcnt);
1762                 devlink_rate->parent = NULL;
1763         } else if (len) {
1764                 parent = devlink_rate_node_get_by_name(devlink, parent_name);
1765                 if (IS_ERR(parent))
1766                         return -ENODEV;
1767
1768                 if (parent == devlink_rate) {
1769                         NL_SET_ERR_MSG_MOD(info->extack, "Parent to self is not allowed");
1770                         return -EINVAL;
1771                 }
1772
1773                 if (devlink_rate_is_node(devlink_rate) &&
1774                     devlink_rate_is_parent_node(devlink_rate, parent->parent)) {
1775                         NL_SET_ERR_MSG_MOD(info->extack, "Node is already a parent of parent node.");
1776                         return -EEXIST;
1777                 }
1778
1779                 if (devlink_rate_is_leaf(devlink_rate))
1780                         err = ops->rate_leaf_parent_set(devlink_rate, parent,
1781                                                         devlink_rate->priv, parent->priv,
1782                                                         info->extack);
1783                 else if (devlink_rate_is_node(devlink_rate))
1784                         err = ops->rate_node_parent_set(devlink_rate, parent,
1785                                                         devlink_rate->priv, parent->priv,
1786                                                         info->extack);
1787                 if (err)
1788                         return err;
1789
1790                 if (devlink_rate->parent)
1791                         /* we're reassigning to other parent in this case */
1792                         refcount_dec(&devlink_rate->parent->refcnt);
1793
1794                 refcount_inc(&parent->refcnt);
1795                 devlink_rate->parent = parent;
1796         }
1797
1798         return 0;
1799 }
1800
1801 static int devlink_nl_rate_set(struct devlink_rate *devlink_rate,
1802                                const struct devlink_ops *ops,
1803                                struct genl_info *info)
1804 {
1805         struct nlattr *nla_parent, **attrs = info->attrs;
1806         int err = -EOPNOTSUPP;
1807         u32 priority;
1808         u32 weight;
1809         u64 rate;
1810
1811         if (attrs[DEVLINK_ATTR_RATE_TX_SHARE]) {
1812                 rate = nla_get_u64(attrs[DEVLINK_ATTR_RATE_TX_SHARE]);
1813                 if (devlink_rate_is_leaf(devlink_rate))
1814                         err = ops->rate_leaf_tx_share_set(devlink_rate, devlink_rate->priv,
1815                                                           rate, info->extack);
1816                 else if (devlink_rate_is_node(devlink_rate))
1817                         err = ops->rate_node_tx_share_set(devlink_rate, devlink_rate->priv,
1818                                                           rate, info->extack);
1819                 if (err)
1820                         return err;
1821                 devlink_rate->tx_share = rate;
1822         }
1823
1824         if (attrs[DEVLINK_ATTR_RATE_TX_MAX]) {
1825                 rate = nla_get_u64(attrs[DEVLINK_ATTR_RATE_TX_MAX]);
1826                 if (devlink_rate_is_leaf(devlink_rate))
1827                         err = ops->rate_leaf_tx_max_set(devlink_rate, devlink_rate->priv,
1828                                                         rate, info->extack);
1829                 else if (devlink_rate_is_node(devlink_rate))
1830                         err = ops->rate_node_tx_max_set(devlink_rate, devlink_rate->priv,
1831                                                         rate, info->extack);
1832                 if (err)
1833                         return err;
1834                 devlink_rate->tx_max = rate;
1835         }
1836
1837         if (attrs[DEVLINK_ATTR_RATE_TX_PRIORITY]) {
1838                 priority = nla_get_u32(attrs[DEVLINK_ATTR_RATE_TX_PRIORITY]);
1839                 if (devlink_rate_is_leaf(devlink_rate))
1840                         err = ops->rate_leaf_tx_priority_set(devlink_rate, devlink_rate->priv,
1841                                                              priority, info->extack);
1842                 else if (devlink_rate_is_node(devlink_rate))
1843                         err = ops->rate_node_tx_priority_set(devlink_rate, devlink_rate->priv,
1844                                                              priority, info->extack);
1845
1846                 if (err)
1847                         return err;
1848                 devlink_rate->tx_priority = priority;
1849         }
1850
1851         if (attrs[DEVLINK_ATTR_RATE_TX_WEIGHT]) {
1852                 weight = nla_get_u32(attrs[DEVLINK_ATTR_RATE_TX_WEIGHT]);
1853                 if (devlink_rate_is_leaf(devlink_rate))
1854                         err = ops->rate_leaf_tx_weight_set(devlink_rate, devlink_rate->priv,
1855                                                            weight, info->extack);
1856                 else if (devlink_rate_is_node(devlink_rate))
1857                         err = ops->rate_node_tx_weight_set(devlink_rate, devlink_rate->priv,
1858                                                            weight, info->extack);
1859
1860                 if (err)
1861                         return err;
1862                 devlink_rate->tx_weight = weight;
1863         }
1864
1865         nla_parent = attrs[DEVLINK_ATTR_RATE_PARENT_NODE_NAME];
1866         if (nla_parent) {
1867                 err = devlink_nl_rate_parent_node_set(devlink_rate, info,
1868                                                       nla_parent);
1869                 if (err)
1870                         return err;
1871         }
1872
1873         return 0;
1874 }
1875
1876 static bool devlink_rate_set_ops_supported(const struct devlink_ops *ops,
1877                                            struct genl_info *info,
1878                                            enum devlink_rate_type type)
1879 {
1880         struct nlattr **attrs = info->attrs;
1881
1882         if (type == DEVLINK_RATE_TYPE_LEAF) {
1883                 if (attrs[DEVLINK_ATTR_RATE_TX_SHARE] && !ops->rate_leaf_tx_share_set) {
1884                         NL_SET_ERR_MSG_MOD(info->extack, "TX share set isn't supported for the leafs");
1885                         return false;
1886                 }
1887                 if (attrs[DEVLINK_ATTR_RATE_TX_MAX] && !ops->rate_leaf_tx_max_set) {
1888                         NL_SET_ERR_MSG_MOD(info->extack, "TX max set isn't supported for the leafs");
1889                         return false;
1890                 }
1891                 if (attrs[DEVLINK_ATTR_RATE_PARENT_NODE_NAME] &&
1892                     !ops->rate_leaf_parent_set) {
1893                         NL_SET_ERR_MSG_MOD(info->extack, "Parent set isn't supported for the leafs");
1894                         return false;
1895                 }
1896                 if (attrs[DEVLINK_ATTR_RATE_TX_PRIORITY] && !ops->rate_leaf_tx_priority_set) {
1897                         NL_SET_ERR_MSG_ATTR(info->extack,
1898                                             attrs[DEVLINK_ATTR_RATE_TX_PRIORITY],
1899                                             "TX priority set isn't supported for the leafs");
1900                         return false;
1901                 }
1902                 if (attrs[DEVLINK_ATTR_RATE_TX_WEIGHT] && !ops->rate_leaf_tx_weight_set) {
1903                         NL_SET_ERR_MSG_ATTR(info->extack,
1904                                             attrs[DEVLINK_ATTR_RATE_TX_WEIGHT],
1905                                             "TX weight set isn't supported for the leafs");
1906                         return false;
1907                 }
1908         } else if (type == DEVLINK_RATE_TYPE_NODE) {
1909                 if (attrs[DEVLINK_ATTR_RATE_TX_SHARE] && !ops->rate_node_tx_share_set) {
1910                         NL_SET_ERR_MSG_MOD(info->extack, "TX share set isn't supported for the nodes");
1911                         return false;
1912                 }
1913                 if (attrs[DEVLINK_ATTR_RATE_TX_MAX] && !ops->rate_node_tx_max_set) {
1914                         NL_SET_ERR_MSG_MOD(info->extack, "TX max set isn't supported for the nodes");
1915                         return false;
1916                 }
1917                 if (attrs[DEVLINK_ATTR_RATE_PARENT_NODE_NAME] &&
1918                     !ops->rate_node_parent_set) {
1919                         NL_SET_ERR_MSG_MOD(info->extack, "Parent set isn't supported for the nodes");
1920                         return false;
1921                 }
1922                 if (attrs[DEVLINK_ATTR_RATE_TX_PRIORITY] && !ops->rate_node_tx_priority_set) {
1923                         NL_SET_ERR_MSG_ATTR(info->extack,
1924                                             attrs[DEVLINK_ATTR_RATE_TX_PRIORITY],
1925                                             "TX priority set isn't supported for the nodes");
1926                         return false;
1927                 }
1928                 if (attrs[DEVLINK_ATTR_RATE_TX_WEIGHT] && !ops->rate_node_tx_weight_set) {
1929                         NL_SET_ERR_MSG_ATTR(info->extack,
1930                                             attrs[DEVLINK_ATTR_RATE_TX_WEIGHT],
1931                                             "TX weight set isn't supported for the nodes");
1932                         return false;
1933                 }
1934         } else {
1935                 WARN(1, "Unknown type of rate object");
1936                 return false;
1937         }
1938
1939         return true;
1940 }
1941
1942 static int devlink_nl_cmd_rate_set_doit(struct sk_buff *skb,
1943                                         struct genl_info *info)
1944 {
1945         struct devlink_rate *devlink_rate = info->user_ptr[1];
1946         struct devlink *devlink = devlink_rate->devlink;
1947         const struct devlink_ops *ops = devlink->ops;
1948         int err;
1949
1950         if (!ops || !devlink_rate_set_ops_supported(ops, info, devlink_rate->type))
1951                 return -EOPNOTSUPP;
1952
1953         err = devlink_nl_rate_set(devlink_rate, ops, info);
1954
1955         if (!err)
1956                 devlink_rate_notify(devlink_rate, DEVLINK_CMD_RATE_NEW);
1957         return err;
1958 }
1959
1960 static int devlink_nl_cmd_rate_new_doit(struct sk_buff *skb,
1961                                         struct genl_info *info)
1962 {
1963         struct devlink *devlink = info->user_ptr[0];
1964         struct devlink_rate *rate_node;
1965         const struct devlink_ops *ops;
1966         int err;
1967
1968         ops = devlink->ops;
1969         if (!ops || !ops->rate_node_new || !ops->rate_node_del) {
1970                 NL_SET_ERR_MSG_MOD(info->extack, "Rate nodes aren't supported");
1971                 return -EOPNOTSUPP;
1972         }
1973
1974         if (!devlink_rate_set_ops_supported(ops, info, DEVLINK_RATE_TYPE_NODE))
1975                 return -EOPNOTSUPP;
1976
1977         rate_node = devlink_rate_node_get_from_attrs(devlink, info->attrs);
1978         if (!IS_ERR(rate_node))
1979                 return -EEXIST;
1980         else if (rate_node == ERR_PTR(-EINVAL))
1981                 return -EINVAL;
1982
1983         rate_node = kzalloc(sizeof(*rate_node), GFP_KERNEL);
1984         if (!rate_node)
1985                 return -ENOMEM;
1986
1987         rate_node->devlink = devlink;
1988         rate_node->type = DEVLINK_RATE_TYPE_NODE;
1989         rate_node->name = nla_strdup(info->attrs[DEVLINK_ATTR_RATE_NODE_NAME], GFP_KERNEL);
1990         if (!rate_node->name) {
1991                 err = -ENOMEM;
1992                 goto err_strdup;
1993         }
1994
1995         err = ops->rate_node_new(rate_node, &rate_node->priv, info->extack);
1996         if (err)
1997                 goto err_node_new;
1998
1999         err = devlink_nl_rate_set(rate_node, ops, info);
2000         if (err)
2001                 goto err_rate_set;
2002
2003         refcount_set(&rate_node->refcnt, 1);
2004         list_add(&rate_node->list, &devlink->rate_list);
2005         devlink_rate_notify(rate_node, DEVLINK_CMD_RATE_NEW);
2006         return 0;
2007
2008 err_rate_set:
2009         ops->rate_node_del(rate_node, rate_node->priv, info->extack);
2010 err_node_new:
2011         kfree(rate_node->name);
2012 err_strdup:
2013         kfree(rate_node);
2014         return err;
2015 }
2016
2017 static int devlink_nl_cmd_rate_del_doit(struct sk_buff *skb,
2018                                         struct genl_info *info)
2019 {
2020         struct devlink_rate *rate_node = info->user_ptr[1];
2021         struct devlink *devlink = rate_node->devlink;
2022         const struct devlink_ops *ops = devlink->ops;
2023         int err;
2024
2025         if (refcount_read(&rate_node->refcnt) > 1) {
2026                 NL_SET_ERR_MSG_MOD(info->extack, "Node has children. Cannot delete node.");
2027                 return -EBUSY;
2028         }
2029
2030         devlink_rate_notify(rate_node, DEVLINK_CMD_RATE_DEL);
2031         err = ops->rate_node_del(rate_node, rate_node->priv, info->extack);
2032         if (rate_node->parent)
2033                 refcount_dec(&rate_node->parent->refcnt);
2034         list_del(&rate_node->list);
2035         kfree(rate_node->name);
2036         kfree(rate_node);
2037         return err;
2038 }
2039
2040 struct devlink_linecard_type {
2041         const char *type;
2042         const void *priv;
2043 };
2044
2045 static int devlink_nl_linecard_fill(struct sk_buff *msg,
2046                                     struct devlink *devlink,
2047                                     struct devlink_linecard *linecard,
2048                                     enum devlink_command cmd, u32 portid,
2049                                     u32 seq, int flags,
2050                                     struct netlink_ext_ack *extack)
2051 {
2052         struct devlink_linecard_type *linecard_type;
2053         struct nlattr *attr;
2054         void *hdr;
2055         int i;
2056
2057         hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
2058         if (!hdr)
2059                 return -EMSGSIZE;
2060
2061         if (devlink_nl_put_handle(msg, devlink))
2062                 goto nla_put_failure;
2063         if (nla_put_u32(msg, DEVLINK_ATTR_LINECARD_INDEX, linecard->index))
2064                 goto nla_put_failure;
2065         if (nla_put_u8(msg, DEVLINK_ATTR_LINECARD_STATE, linecard->state))
2066                 goto nla_put_failure;
2067         if (linecard->type &&
2068             nla_put_string(msg, DEVLINK_ATTR_LINECARD_TYPE, linecard->type))
2069                 goto nla_put_failure;
2070
2071         if (linecard->types_count) {
2072                 attr = nla_nest_start(msg,
2073                                       DEVLINK_ATTR_LINECARD_SUPPORTED_TYPES);
2074                 if (!attr)
2075                         goto nla_put_failure;
2076                 for (i = 0; i < linecard->types_count; i++) {
2077                         linecard_type = &linecard->types[i];
2078                         if (nla_put_string(msg, DEVLINK_ATTR_LINECARD_TYPE,
2079                                            linecard_type->type)) {
2080                                 nla_nest_cancel(msg, attr);
2081                                 goto nla_put_failure;
2082                         }
2083                 }
2084                 nla_nest_end(msg, attr);
2085         }
2086
2087         if (linecard->nested_devlink &&
2088             devlink_nl_put_nested_handle(msg, linecard->nested_devlink))
2089                 goto nla_put_failure;
2090
2091         genlmsg_end(msg, hdr);
2092         return 0;
2093
2094 nla_put_failure:
2095         genlmsg_cancel(msg, hdr);
2096         return -EMSGSIZE;
2097 }
2098
2099 static void devlink_linecard_notify(struct devlink_linecard *linecard,
2100                                     enum devlink_command cmd)
2101 {
2102         struct devlink *devlink = linecard->devlink;
2103         struct sk_buff *msg;
2104         int err;
2105
2106         WARN_ON(cmd != DEVLINK_CMD_LINECARD_NEW &&
2107                 cmd != DEVLINK_CMD_LINECARD_DEL);
2108
2109         if (!xa_get_mark(&devlinks, devlink->index, DEVLINK_REGISTERED))
2110                 return;
2111
2112         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2113         if (!msg)
2114                 return;
2115
2116         err = devlink_nl_linecard_fill(msg, devlink, linecard, cmd, 0, 0, 0,
2117                                        NULL);
2118         if (err) {
2119                 nlmsg_free(msg);
2120                 return;
2121         }
2122
2123         genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
2124                                 msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
2125 }
2126
2127 static int devlink_nl_cmd_linecard_get_doit(struct sk_buff *skb,
2128                                             struct genl_info *info)
2129 {
2130         struct devlink_linecard *linecard = info->user_ptr[1];
2131         struct devlink *devlink = linecard->devlink;
2132         struct sk_buff *msg;
2133         int err;
2134
2135         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2136         if (!msg)
2137                 return -ENOMEM;
2138
2139         mutex_lock(&linecard->state_lock);
2140         err = devlink_nl_linecard_fill(msg, devlink, linecard,
2141                                        DEVLINK_CMD_LINECARD_NEW,
2142                                        info->snd_portid, info->snd_seq, 0,
2143                                        info->extack);
2144         mutex_unlock(&linecard->state_lock);
2145         if (err) {
2146                 nlmsg_free(msg);
2147                 return err;
2148         }
2149
2150         return genlmsg_reply(msg, info);
2151 }
2152
2153 static int devlink_nl_cmd_linecard_get_dumpit(struct sk_buff *msg,
2154                                               struct netlink_callback *cb)
2155 {
2156         struct devlink_nl_dump_state *state = devlink_dump_state(cb);
2157         struct devlink_linecard *linecard;
2158         struct devlink *devlink;
2159         int start = state->idx;
2160         unsigned long index;
2161         int idx = 0;
2162         int err;
2163
2164         devlinks_xa_for_each_registered_get(sock_net(msg->sk), index, devlink) {
2165                 mutex_lock(&devlink->linecards_lock);
2166                 list_for_each_entry(linecard, &devlink->linecard_list, list) {
2167                         if (idx < start) {
2168                                 idx++;
2169                                 continue;
2170                         }
2171                         mutex_lock(&linecard->state_lock);
2172                         err = devlink_nl_linecard_fill(msg, devlink, linecard,
2173                                                        DEVLINK_CMD_LINECARD_NEW,
2174                                                        NETLINK_CB(cb->skb).portid,
2175                                                        cb->nlh->nlmsg_seq,
2176                                                        NLM_F_MULTI,
2177                                                        cb->extack);
2178                         mutex_unlock(&linecard->state_lock);
2179                         if (err) {
2180                                 mutex_unlock(&devlink->linecards_lock);
2181                                 devlink_put(devlink);
2182                                 goto out;
2183                         }
2184                         idx++;
2185                 }
2186                 mutex_unlock(&devlink->linecards_lock);
2187                 devlink_put(devlink);
2188         }
2189 out:
2190         state->idx = idx;
2191         return msg->len;
2192 }
2193
2194 static struct devlink_linecard_type *
2195 devlink_linecard_type_lookup(struct devlink_linecard *linecard,
2196                              const char *type)
2197 {
2198         struct devlink_linecard_type *linecard_type;
2199         int i;
2200
2201         for (i = 0; i < linecard->types_count; i++) {
2202                 linecard_type = &linecard->types[i];
2203                 if (!strcmp(type, linecard_type->type))
2204                         return linecard_type;
2205         }
2206         return NULL;
2207 }
2208
2209 static int devlink_linecard_type_set(struct devlink_linecard *linecard,
2210                                      const char *type,
2211                                      struct netlink_ext_ack *extack)
2212 {
2213         const struct devlink_linecard_ops *ops = linecard->ops;
2214         struct devlink_linecard_type *linecard_type;
2215         int err;
2216
2217         mutex_lock(&linecard->state_lock);
2218         if (linecard->state == DEVLINK_LINECARD_STATE_PROVISIONING) {
2219                 NL_SET_ERR_MSG_MOD(extack, "Line card is currently being provisioned");
2220                 err = -EBUSY;
2221                 goto out;
2222         }
2223         if (linecard->state == DEVLINK_LINECARD_STATE_UNPROVISIONING) {
2224                 NL_SET_ERR_MSG_MOD(extack, "Line card is currently being unprovisioned");
2225                 err = -EBUSY;
2226                 goto out;
2227         }
2228
2229         linecard_type = devlink_linecard_type_lookup(linecard, type);
2230         if (!linecard_type) {
2231                 NL_SET_ERR_MSG_MOD(extack, "Unsupported line card type provided");
2232                 err = -EINVAL;
2233                 goto out;
2234         }
2235
2236         if (linecard->state != DEVLINK_LINECARD_STATE_UNPROVISIONED &&
2237             linecard->state != DEVLINK_LINECARD_STATE_PROVISIONING_FAILED) {
2238                 NL_SET_ERR_MSG_MOD(extack, "Line card already provisioned");
2239                 err = -EBUSY;
2240                 /* Check if the line card is provisioned in the same
2241                  * way the user asks. In case it is, make the operation
2242                  * to return success.
2243                  */
2244                 if (ops->same_provision &&
2245                     ops->same_provision(linecard, linecard->priv,
2246                                         linecard_type->type,
2247                                         linecard_type->priv))
2248                         err = 0;
2249                 goto out;
2250         }
2251
2252         linecard->state = DEVLINK_LINECARD_STATE_PROVISIONING;
2253         linecard->type = linecard_type->type;
2254         devlink_linecard_notify(linecard, DEVLINK_CMD_LINECARD_NEW);
2255         mutex_unlock(&linecard->state_lock);
2256         err = ops->provision(linecard, linecard->priv, linecard_type->type,
2257                              linecard_type->priv, extack);
2258         if (err) {
2259                 /* Provisioning failed. Assume the linecard is unprovisioned
2260                  * for future operations.
2261                  */
2262                 mutex_lock(&linecard->state_lock);
2263                 linecard->state = DEVLINK_LINECARD_STATE_UNPROVISIONED;
2264                 linecard->type = NULL;
2265                 devlink_linecard_notify(linecard, DEVLINK_CMD_LINECARD_NEW);
2266                 mutex_unlock(&linecard->state_lock);
2267         }
2268         return err;
2269
2270 out:
2271         mutex_unlock(&linecard->state_lock);
2272         return err;
2273 }
2274
2275 static int devlink_linecard_type_unset(struct devlink_linecard *linecard,
2276                                        struct netlink_ext_ack *extack)
2277 {
2278         int err;
2279
2280         mutex_lock(&linecard->state_lock);
2281         if (linecard->state == DEVLINK_LINECARD_STATE_PROVISIONING) {
2282                 NL_SET_ERR_MSG_MOD(extack, "Line card is currently being provisioned");
2283                 err = -EBUSY;
2284                 goto out;
2285         }
2286         if (linecard->state == DEVLINK_LINECARD_STATE_UNPROVISIONING) {
2287                 NL_SET_ERR_MSG_MOD(extack, "Line card is currently being unprovisioned");
2288                 err = -EBUSY;
2289                 goto out;
2290         }
2291         if (linecard->state == DEVLINK_LINECARD_STATE_PROVISIONING_FAILED) {
2292                 linecard->state = DEVLINK_LINECARD_STATE_UNPROVISIONED;
2293                 linecard->type = NULL;
2294                 devlink_linecard_notify(linecard, DEVLINK_CMD_LINECARD_NEW);
2295                 err = 0;
2296                 goto out;
2297         }
2298
2299         if (linecard->state == DEVLINK_LINECARD_STATE_UNPROVISIONED) {
2300                 NL_SET_ERR_MSG_MOD(extack, "Line card is not provisioned");
2301                 err = 0;
2302                 goto out;
2303         }
2304         linecard->state = DEVLINK_LINECARD_STATE_UNPROVISIONING;
2305         devlink_linecard_notify(linecard, DEVLINK_CMD_LINECARD_NEW);
2306         mutex_unlock(&linecard->state_lock);
2307         err = linecard->ops->unprovision(linecard, linecard->priv,
2308                                          extack);
2309         if (err) {
2310                 /* Unprovisioning failed. Assume the linecard is unprovisioned
2311                  * for future operations.
2312                  */
2313                 mutex_lock(&linecard->state_lock);
2314                 linecard->state = DEVLINK_LINECARD_STATE_UNPROVISIONED;
2315                 linecard->type = NULL;
2316                 devlink_linecard_notify(linecard, DEVLINK_CMD_LINECARD_NEW);
2317                 mutex_unlock(&linecard->state_lock);
2318         }
2319         return err;
2320
2321 out:
2322         mutex_unlock(&linecard->state_lock);
2323         return err;
2324 }
2325
2326 static int devlink_nl_cmd_linecard_set_doit(struct sk_buff *skb,
2327                                             struct genl_info *info)
2328 {
2329         struct devlink_linecard *linecard = info->user_ptr[1];
2330         struct netlink_ext_ack *extack = info->extack;
2331         int err;
2332
2333         if (info->attrs[DEVLINK_ATTR_LINECARD_TYPE]) {
2334                 const char *type;
2335
2336                 type = nla_data(info->attrs[DEVLINK_ATTR_LINECARD_TYPE]);
2337                 if (strcmp(type, "")) {
2338                         err = devlink_linecard_type_set(linecard, type, extack);
2339                         if (err)
2340                                 return err;
2341                 } else {
2342                         err = devlink_linecard_type_unset(linecard, extack);
2343                         if (err)
2344                                 return err;
2345                 }
2346         }
2347
2348         return 0;
2349 }
2350
2351 static int devlink_nl_sb_fill(struct sk_buff *msg, struct devlink *devlink,
2352                               struct devlink_sb *devlink_sb,
2353                               enum devlink_command cmd, u32 portid,
2354                               u32 seq, int flags)
2355 {
2356         void *hdr;
2357
2358         hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
2359         if (!hdr)
2360                 return -EMSGSIZE;
2361
2362         if (devlink_nl_put_handle(msg, devlink))
2363                 goto nla_put_failure;
2364         if (nla_put_u32(msg, DEVLINK_ATTR_SB_INDEX, devlink_sb->index))
2365                 goto nla_put_failure;
2366         if (nla_put_u32(msg, DEVLINK_ATTR_SB_SIZE, devlink_sb->size))
2367                 goto nla_put_failure;
2368         if (nla_put_u16(msg, DEVLINK_ATTR_SB_INGRESS_POOL_COUNT,
2369                         devlink_sb->ingress_pools_count))
2370                 goto nla_put_failure;
2371         if (nla_put_u16(msg, DEVLINK_ATTR_SB_EGRESS_POOL_COUNT,
2372                         devlink_sb->egress_pools_count))
2373                 goto nla_put_failure;
2374         if (nla_put_u16(msg, DEVLINK_ATTR_SB_INGRESS_TC_COUNT,
2375                         devlink_sb->ingress_tc_count))
2376                 goto nla_put_failure;
2377         if (nla_put_u16(msg, DEVLINK_ATTR_SB_EGRESS_TC_COUNT,
2378                         devlink_sb->egress_tc_count))
2379                 goto nla_put_failure;
2380
2381         genlmsg_end(msg, hdr);
2382         return 0;
2383
2384 nla_put_failure:
2385         genlmsg_cancel(msg, hdr);
2386         return -EMSGSIZE;
2387 }
2388
2389 static int devlink_nl_cmd_sb_get_doit(struct sk_buff *skb,
2390                                       struct genl_info *info)
2391 {
2392         struct devlink *devlink = info->user_ptr[0];
2393         struct devlink_sb *devlink_sb;
2394         struct sk_buff *msg;
2395         int err;
2396
2397         devlink_sb = devlink_sb_get_from_info(devlink, info);
2398         if (IS_ERR(devlink_sb))
2399                 return PTR_ERR(devlink_sb);
2400
2401         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2402         if (!msg)
2403                 return -ENOMEM;
2404
2405         err = devlink_nl_sb_fill(msg, devlink, devlink_sb,
2406                                  DEVLINK_CMD_SB_NEW,
2407                                  info->snd_portid, info->snd_seq, 0);
2408         if (err) {
2409                 nlmsg_free(msg);
2410                 return err;
2411         }
2412
2413         return genlmsg_reply(msg, info);
2414 }
2415
2416 static int devlink_nl_cmd_sb_get_dumpit(struct sk_buff *msg,
2417                                         struct netlink_callback *cb)
2418 {
2419         struct devlink_nl_dump_state *state = devlink_dump_state(cb);
2420         struct devlink *devlink;
2421         struct devlink_sb *devlink_sb;
2422         int start = state->idx;
2423         unsigned long index;
2424         int idx = 0;
2425         int err;
2426
2427         devlinks_xa_for_each_registered_get(sock_net(msg->sk), index, devlink) {
2428                 devl_lock(devlink);
2429                 list_for_each_entry(devlink_sb, &devlink->sb_list, list) {
2430                         if (idx < start) {
2431                                 idx++;
2432                                 continue;
2433                         }
2434                         err = devlink_nl_sb_fill(msg, devlink, devlink_sb,
2435                                                  DEVLINK_CMD_SB_NEW,
2436                                                  NETLINK_CB(cb->skb).portid,
2437                                                  cb->nlh->nlmsg_seq,
2438                                                  NLM_F_MULTI);
2439                         if (err) {
2440                                 devl_unlock(devlink);
2441                                 devlink_put(devlink);
2442                                 goto out;
2443                         }
2444                         idx++;
2445                 }
2446                 devl_unlock(devlink);
2447                 devlink_put(devlink);
2448         }
2449 out:
2450         state->idx = idx;
2451         return msg->len;
2452 }
2453
2454 static int devlink_nl_sb_pool_fill(struct sk_buff *msg, struct devlink *devlink,
2455                                    struct devlink_sb *devlink_sb,
2456                                    u16 pool_index, enum devlink_command cmd,
2457                                    u32 portid, u32 seq, int flags)
2458 {
2459         struct devlink_sb_pool_info pool_info;
2460         void *hdr;
2461         int err;
2462
2463         err = devlink->ops->sb_pool_get(devlink, devlink_sb->index,
2464                                         pool_index, &pool_info);
2465         if (err)
2466                 return err;
2467
2468         hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
2469         if (!hdr)
2470                 return -EMSGSIZE;
2471
2472         if (devlink_nl_put_handle(msg, devlink))
2473                 goto nla_put_failure;
2474         if (nla_put_u32(msg, DEVLINK_ATTR_SB_INDEX, devlink_sb->index))
2475                 goto nla_put_failure;
2476         if (nla_put_u16(msg, DEVLINK_ATTR_SB_POOL_INDEX, pool_index))
2477                 goto nla_put_failure;
2478         if (nla_put_u8(msg, DEVLINK_ATTR_SB_POOL_TYPE, pool_info.pool_type))
2479                 goto nla_put_failure;
2480         if (nla_put_u32(msg, DEVLINK_ATTR_SB_POOL_SIZE, pool_info.size))
2481                 goto nla_put_failure;
2482         if (nla_put_u8(msg, DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE,
2483                        pool_info.threshold_type))
2484                 goto nla_put_failure;
2485         if (nla_put_u32(msg, DEVLINK_ATTR_SB_POOL_CELL_SIZE,
2486                         pool_info.cell_size))
2487                 goto nla_put_failure;
2488
2489         genlmsg_end(msg, hdr);
2490         return 0;
2491
2492 nla_put_failure:
2493         genlmsg_cancel(msg, hdr);
2494         return -EMSGSIZE;
2495 }
2496
2497 static int devlink_nl_cmd_sb_pool_get_doit(struct sk_buff *skb,
2498                                            struct genl_info *info)
2499 {
2500         struct devlink *devlink = info->user_ptr[0];
2501         struct devlink_sb *devlink_sb;
2502         struct sk_buff *msg;
2503         u16 pool_index;
2504         int err;
2505
2506         devlink_sb = devlink_sb_get_from_info(devlink, info);
2507         if (IS_ERR(devlink_sb))
2508                 return PTR_ERR(devlink_sb);
2509
2510         err = devlink_sb_pool_index_get_from_info(devlink_sb, info,
2511                                                   &pool_index);
2512         if (err)
2513                 return err;
2514
2515         if (!devlink->ops->sb_pool_get)
2516                 return -EOPNOTSUPP;
2517
2518         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2519         if (!msg)
2520                 return -ENOMEM;
2521
2522         err = devlink_nl_sb_pool_fill(msg, devlink, devlink_sb, pool_index,
2523                                       DEVLINK_CMD_SB_POOL_NEW,
2524                                       info->snd_portid, info->snd_seq, 0);
2525         if (err) {
2526                 nlmsg_free(msg);
2527                 return err;
2528         }
2529
2530         return genlmsg_reply(msg, info);
2531 }
2532
2533 static int __sb_pool_get_dumpit(struct sk_buff *msg, int start, int *p_idx,
2534                                 struct devlink *devlink,
2535                                 struct devlink_sb *devlink_sb,
2536                                 u32 portid, u32 seq)
2537 {
2538         u16 pool_count = devlink_sb_pool_count(devlink_sb);
2539         u16 pool_index;
2540         int err;
2541
2542         for (pool_index = 0; pool_index < pool_count; pool_index++) {
2543                 if (*p_idx < start) {
2544                         (*p_idx)++;
2545                         continue;
2546                 }
2547                 err = devlink_nl_sb_pool_fill(msg, devlink,
2548                                               devlink_sb,
2549                                               pool_index,
2550                                               DEVLINK_CMD_SB_POOL_NEW,
2551                                               portid, seq, NLM_F_MULTI);
2552                 if (err)
2553                         return err;
2554                 (*p_idx)++;
2555         }
2556         return 0;
2557 }
2558
2559 static int devlink_nl_cmd_sb_pool_get_dumpit(struct sk_buff *msg,
2560                                              struct netlink_callback *cb)
2561 {
2562         struct devlink_nl_dump_state *state = devlink_dump_state(cb);
2563         struct devlink *devlink;
2564         struct devlink_sb *devlink_sb;
2565         int start = state->idx;
2566         unsigned long index;
2567         int idx = 0;
2568         int err = 0;
2569
2570         devlinks_xa_for_each_registered_get(sock_net(msg->sk), index, devlink) {
2571                 if (!devlink->ops->sb_pool_get)
2572                         goto retry;
2573
2574                 devl_lock(devlink);
2575                 list_for_each_entry(devlink_sb, &devlink->sb_list, list) {
2576                         err = __sb_pool_get_dumpit(msg, start, &idx, devlink,
2577                                                    devlink_sb,
2578                                                    NETLINK_CB(cb->skb).portid,
2579                                                    cb->nlh->nlmsg_seq);
2580                         if (err == -EOPNOTSUPP) {
2581                                 err = 0;
2582                         } else if (err) {
2583                                 devl_unlock(devlink);
2584                                 devlink_put(devlink);
2585                                 goto out;
2586                         }
2587                 }
2588                 devl_unlock(devlink);
2589 retry:
2590                 devlink_put(devlink);
2591         }
2592 out:
2593         if (err != -EMSGSIZE)
2594                 return err;
2595
2596         state->idx = idx;
2597         return msg->len;
2598 }
2599
2600 static int devlink_sb_pool_set(struct devlink *devlink, unsigned int sb_index,
2601                                u16 pool_index, u32 size,
2602                                enum devlink_sb_threshold_type threshold_type,
2603                                struct netlink_ext_ack *extack)
2604
2605 {
2606         const struct devlink_ops *ops = devlink->ops;
2607
2608         if (ops->sb_pool_set)
2609                 return ops->sb_pool_set(devlink, sb_index, pool_index,
2610                                         size, threshold_type, extack);
2611         return -EOPNOTSUPP;
2612 }
2613
2614 static int devlink_nl_cmd_sb_pool_set_doit(struct sk_buff *skb,
2615                                            struct genl_info *info)
2616 {
2617         struct devlink *devlink = info->user_ptr[0];
2618         enum devlink_sb_threshold_type threshold_type;
2619         struct devlink_sb *devlink_sb;
2620         u16 pool_index;
2621         u32 size;
2622         int err;
2623
2624         devlink_sb = devlink_sb_get_from_info(devlink, info);
2625         if (IS_ERR(devlink_sb))
2626                 return PTR_ERR(devlink_sb);
2627
2628         err = devlink_sb_pool_index_get_from_info(devlink_sb, info,
2629                                                   &pool_index);
2630         if (err)
2631                 return err;
2632
2633         err = devlink_sb_th_type_get_from_info(info, &threshold_type);
2634         if (err)
2635                 return err;
2636
2637         if (GENL_REQ_ATTR_CHECK(info, DEVLINK_ATTR_SB_POOL_SIZE))
2638                 return -EINVAL;
2639
2640         size = nla_get_u32(info->attrs[DEVLINK_ATTR_SB_POOL_SIZE]);
2641         return devlink_sb_pool_set(devlink, devlink_sb->index,
2642                                    pool_index, size, threshold_type,
2643                                    info->extack);
2644 }
2645
2646 static int devlink_nl_sb_port_pool_fill(struct sk_buff *msg,
2647                                         struct devlink *devlink,
2648                                         struct devlink_port *devlink_port,
2649                                         struct devlink_sb *devlink_sb,
2650                                         u16 pool_index,
2651                                         enum devlink_command cmd,
2652                                         u32 portid, u32 seq, int flags)
2653 {
2654         const struct devlink_ops *ops = devlink->ops;
2655         u32 threshold;
2656         void *hdr;
2657         int err;
2658
2659         err = ops->sb_port_pool_get(devlink_port, devlink_sb->index,
2660                                     pool_index, &threshold);
2661         if (err)
2662                 return err;
2663
2664         hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
2665         if (!hdr)
2666                 return -EMSGSIZE;
2667
2668         if (devlink_nl_put_handle(msg, devlink))
2669                 goto nla_put_failure;
2670         if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, devlink_port->index))
2671                 goto nla_put_failure;
2672         if (nla_put_u32(msg, DEVLINK_ATTR_SB_INDEX, devlink_sb->index))
2673                 goto nla_put_failure;
2674         if (nla_put_u16(msg, DEVLINK_ATTR_SB_POOL_INDEX, pool_index))
2675                 goto nla_put_failure;
2676         if (nla_put_u32(msg, DEVLINK_ATTR_SB_THRESHOLD, threshold))
2677                 goto nla_put_failure;
2678
2679         if (ops->sb_occ_port_pool_get) {
2680                 u32 cur;
2681                 u32 max;
2682
2683                 err = ops->sb_occ_port_pool_get(devlink_port, devlink_sb->index,
2684                                                 pool_index, &cur, &max);
2685                 if (err && err != -EOPNOTSUPP)
2686                         goto sb_occ_get_failure;
2687                 if (!err) {
2688                         if (nla_put_u32(msg, DEVLINK_ATTR_SB_OCC_CUR, cur))
2689                                 goto nla_put_failure;
2690                         if (nla_put_u32(msg, DEVLINK_ATTR_SB_OCC_MAX, max))
2691                                 goto nla_put_failure;
2692                 }
2693         }
2694
2695         genlmsg_end(msg, hdr);
2696         return 0;
2697
2698 nla_put_failure:
2699         err = -EMSGSIZE;
2700 sb_occ_get_failure:
2701         genlmsg_cancel(msg, hdr);
2702         return err;
2703 }
2704
2705 static int devlink_nl_cmd_sb_port_pool_get_doit(struct sk_buff *skb,
2706                                                 struct genl_info *info)
2707 {
2708         struct devlink_port *devlink_port = info->user_ptr[1];
2709         struct devlink *devlink = devlink_port->devlink;
2710         struct devlink_sb *devlink_sb;
2711         struct sk_buff *msg;
2712         u16 pool_index;
2713         int err;
2714
2715         devlink_sb = devlink_sb_get_from_info(devlink, info);
2716         if (IS_ERR(devlink_sb))
2717                 return PTR_ERR(devlink_sb);
2718
2719         err = devlink_sb_pool_index_get_from_info(devlink_sb, info,
2720                                                   &pool_index);
2721         if (err)
2722                 return err;
2723
2724         if (!devlink->ops->sb_port_pool_get)
2725                 return -EOPNOTSUPP;
2726
2727         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2728         if (!msg)
2729                 return -ENOMEM;
2730
2731         err = devlink_nl_sb_port_pool_fill(msg, devlink, devlink_port,
2732                                            devlink_sb, pool_index,
2733                                            DEVLINK_CMD_SB_PORT_POOL_NEW,
2734                                            info->snd_portid, info->snd_seq, 0);
2735         if (err) {
2736                 nlmsg_free(msg);
2737                 return err;
2738         }
2739
2740         return genlmsg_reply(msg, info);
2741 }
2742
2743 static int __sb_port_pool_get_dumpit(struct sk_buff *msg, int start, int *p_idx,
2744                                      struct devlink *devlink,
2745                                      struct devlink_sb *devlink_sb,
2746                                      u32 portid, u32 seq)
2747 {
2748         struct devlink_port *devlink_port;
2749         u16 pool_count = devlink_sb_pool_count(devlink_sb);
2750         unsigned long port_index;
2751         u16 pool_index;
2752         int err;
2753
2754         xa_for_each(&devlink->ports, port_index, devlink_port) {
2755                 for (pool_index = 0; pool_index < pool_count; pool_index++) {
2756                         if (*p_idx < start) {
2757                                 (*p_idx)++;
2758                                 continue;
2759                         }
2760                         err = devlink_nl_sb_port_pool_fill(msg, devlink,
2761                                                            devlink_port,
2762                                                            devlink_sb,
2763                                                            pool_index,
2764                                                            DEVLINK_CMD_SB_PORT_POOL_NEW,
2765                                                            portid, seq,
2766                                                            NLM_F_MULTI);
2767                         if (err)
2768                                 return err;
2769                         (*p_idx)++;
2770                 }
2771         }
2772         return 0;
2773 }
2774
2775 static int devlink_nl_cmd_sb_port_pool_get_dumpit(struct sk_buff *msg,
2776                                                   struct netlink_callback *cb)
2777 {
2778         struct devlink_nl_dump_state *state = devlink_dump_state(cb);
2779         struct devlink *devlink;
2780         struct devlink_sb *devlink_sb;
2781         int start = state->idx;
2782         unsigned long index;
2783         int idx = 0;
2784         int err = 0;
2785
2786         devlinks_xa_for_each_registered_get(sock_net(msg->sk), index, devlink) {
2787                 if (!devlink->ops->sb_port_pool_get)
2788                         goto retry;
2789
2790                 devl_lock(devlink);
2791                 list_for_each_entry(devlink_sb, &devlink->sb_list, list) {
2792                         err = __sb_port_pool_get_dumpit(msg, start, &idx,
2793                                                         devlink, devlink_sb,
2794                                                         NETLINK_CB(cb->skb).portid,
2795                                                         cb->nlh->nlmsg_seq);
2796                         if (err == -EOPNOTSUPP) {
2797                                 err = 0;
2798                         } else if (err) {
2799                                 devl_unlock(devlink);
2800                                 devlink_put(devlink);
2801                                 goto out;
2802                         }
2803                 }
2804                 devl_unlock(devlink);
2805 retry:
2806                 devlink_put(devlink);
2807         }
2808 out:
2809         if (err != -EMSGSIZE)
2810                 return err;
2811
2812         state->idx = idx;
2813         return msg->len;
2814 }
2815
2816 static int devlink_sb_port_pool_set(struct devlink_port *devlink_port,
2817                                     unsigned int sb_index, u16 pool_index,
2818                                     u32 threshold,
2819                                     struct netlink_ext_ack *extack)
2820
2821 {
2822         const struct devlink_ops *ops = devlink_port->devlink->ops;
2823
2824         if (ops->sb_port_pool_set)
2825                 return ops->sb_port_pool_set(devlink_port, sb_index,
2826                                              pool_index, threshold, extack);
2827         return -EOPNOTSUPP;
2828 }
2829
2830 static int devlink_nl_cmd_sb_port_pool_set_doit(struct sk_buff *skb,
2831                                                 struct genl_info *info)
2832 {
2833         struct devlink_port *devlink_port = info->user_ptr[1];
2834         struct devlink *devlink = info->user_ptr[0];
2835         struct devlink_sb *devlink_sb;
2836         u16 pool_index;
2837         u32 threshold;
2838         int err;
2839
2840         devlink_sb = devlink_sb_get_from_info(devlink, info);
2841         if (IS_ERR(devlink_sb))
2842                 return PTR_ERR(devlink_sb);
2843
2844         err = devlink_sb_pool_index_get_from_info(devlink_sb, info,
2845                                                   &pool_index);
2846         if (err)
2847                 return err;
2848
2849         if (GENL_REQ_ATTR_CHECK(info, DEVLINK_ATTR_SB_THRESHOLD))
2850                 return -EINVAL;
2851
2852         threshold = nla_get_u32(info->attrs[DEVLINK_ATTR_SB_THRESHOLD]);
2853         return devlink_sb_port_pool_set(devlink_port, devlink_sb->index,
2854                                         pool_index, threshold, info->extack);
2855 }
2856
2857 static int
2858 devlink_nl_sb_tc_pool_bind_fill(struct sk_buff *msg, struct devlink *devlink,
2859                                 struct devlink_port *devlink_port,
2860                                 struct devlink_sb *devlink_sb, u16 tc_index,
2861                                 enum devlink_sb_pool_type pool_type,
2862                                 enum devlink_command cmd,
2863                                 u32 portid, u32 seq, int flags)
2864 {
2865         const struct devlink_ops *ops = devlink->ops;
2866         u16 pool_index;
2867         u32 threshold;
2868         void *hdr;
2869         int err;
2870
2871         err = ops->sb_tc_pool_bind_get(devlink_port, devlink_sb->index,
2872                                        tc_index, pool_type,
2873                                        &pool_index, &threshold);
2874         if (err)
2875                 return err;
2876
2877         hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
2878         if (!hdr)
2879                 return -EMSGSIZE;
2880
2881         if (devlink_nl_put_handle(msg, devlink))
2882                 goto nla_put_failure;
2883         if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, devlink_port->index))
2884                 goto nla_put_failure;
2885         if (nla_put_u32(msg, DEVLINK_ATTR_SB_INDEX, devlink_sb->index))
2886                 goto nla_put_failure;
2887         if (nla_put_u16(msg, DEVLINK_ATTR_SB_TC_INDEX, tc_index))
2888                 goto nla_put_failure;
2889         if (nla_put_u8(msg, DEVLINK_ATTR_SB_POOL_TYPE, pool_type))
2890                 goto nla_put_failure;
2891         if (nla_put_u16(msg, DEVLINK_ATTR_SB_POOL_INDEX, pool_index))
2892                 goto nla_put_failure;
2893         if (nla_put_u32(msg, DEVLINK_ATTR_SB_THRESHOLD, threshold))
2894                 goto nla_put_failure;
2895
2896         if (ops->sb_occ_tc_port_bind_get) {
2897                 u32 cur;
2898                 u32 max;
2899
2900                 err = ops->sb_occ_tc_port_bind_get(devlink_port,
2901                                                    devlink_sb->index,
2902                                                    tc_index, pool_type,
2903                                                    &cur, &max);
2904                 if (err && err != -EOPNOTSUPP)
2905                         return err;
2906                 if (!err) {
2907                         if (nla_put_u32(msg, DEVLINK_ATTR_SB_OCC_CUR, cur))
2908                                 goto nla_put_failure;
2909                         if (nla_put_u32(msg, DEVLINK_ATTR_SB_OCC_MAX, max))
2910                                 goto nla_put_failure;
2911                 }
2912         }
2913
2914         genlmsg_end(msg, hdr);
2915         return 0;
2916
2917 nla_put_failure:
2918         genlmsg_cancel(msg, hdr);
2919         return -EMSGSIZE;
2920 }
2921
2922 static int devlink_nl_cmd_sb_tc_pool_bind_get_doit(struct sk_buff *skb,
2923                                                    struct genl_info *info)
2924 {
2925         struct devlink_port *devlink_port = info->user_ptr[1];
2926         struct devlink *devlink = devlink_port->devlink;
2927         struct devlink_sb *devlink_sb;
2928         struct sk_buff *msg;
2929         enum devlink_sb_pool_type pool_type;
2930         u16 tc_index;
2931         int err;
2932
2933         devlink_sb = devlink_sb_get_from_info(devlink, info);
2934         if (IS_ERR(devlink_sb))
2935                 return PTR_ERR(devlink_sb);
2936
2937         err = devlink_sb_pool_type_get_from_info(info, &pool_type);
2938         if (err)
2939                 return err;
2940
2941         err = devlink_sb_tc_index_get_from_info(devlink_sb, info,
2942                                                 pool_type, &tc_index);
2943         if (err)
2944                 return err;
2945
2946         if (!devlink->ops->sb_tc_pool_bind_get)
2947                 return -EOPNOTSUPP;
2948
2949         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2950         if (!msg)
2951                 return -ENOMEM;
2952
2953         err = devlink_nl_sb_tc_pool_bind_fill(msg, devlink, devlink_port,
2954                                               devlink_sb, tc_index, pool_type,
2955                                               DEVLINK_CMD_SB_TC_POOL_BIND_NEW,
2956                                               info->snd_portid,
2957                                               info->snd_seq, 0);
2958         if (err) {
2959                 nlmsg_free(msg);
2960                 return err;
2961         }
2962
2963         return genlmsg_reply(msg, info);
2964 }
2965
2966 static int __sb_tc_pool_bind_get_dumpit(struct sk_buff *msg,
2967                                         int start, int *p_idx,
2968                                         struct devlink *devlink,
2969                                         struct devlink_sb *devlink_sb,
2970                                         u32 portid, u32 seq)
2971 {
2972         struct devlink_port *devlink_port;
2973         unsigned long port_index;
2974         u16 tc_index;
2975         int err;
2976
2977         xa_for_each(&devlink->ports, port_index, devlink_port) {
2978                 for (tc_index = 0;
2979                      tc_index < devlink_sb->ingress_tc_count; tc_index++) {
2980                         if (*p_idx < start) {
2981                                 (*p_idx)++;
2982                                 continue;
2983                         }
2984                         err = devlink_nl_sb_tc_pool_bind_fill(msg, devlink,
2985                                                               devlink_port,
2986                                                               devlink_sb,
2987                                                               tc_index,
2988                                                               DEVLINK_SB_POOL_TYPE_INGRESS,
2989                                                               DEVLINK_CMD_SB_TC_POOL_BIND_NEW,
2990                                                               portid, seq,
2991                                                               NLM_F_MULTI);
2992                         if (err)
2993                                 return err;
2994                         (*p_idx)++;
2995                 }
2996                 for (tc_index = 0;
2997                      tc_index < devlink_sb->egress_tc_count; tc_index++) {
2998                         if (*p_idx < start) {
2999                                 (*p_idx)++;
3000                                 continue;
3001                         }
3002                         err = devlink_nl_sb_tc_pool_bind_fill(msg, devlink,
3003                                                               devlink_port,
3004                                                               devlink_sb,
3005                                                               tc_index,
3006                                                               DEVLINK_SB_POOL_TYPE_EGRESS,
3007                                                               DEVLINK_CMD_SB_TC_POOL_BIND_NEW,
3008                                                               portid, seq,
3009                                                               NLM_F_MULTI);
3010                         if (err)
3011                                 return err;
3012                         (*p_idx)++;
3013                 }
3014         }
3015         return 0;
3016 }
3017
3018 static int
3019 devlink_nl_cmd_sb_tc_pool_bind_get_dumpit(struct sk_buff *msg,
3020                                           struct netlink_callback *cb)
3021 {
3022         struct devlink_nl_dump_state *state = devlink_dump_state(cb);
3023         struct devlink *devlink;
3024         struct devlink_sb *devlink_sb;
3025         int start = state->idx;
3026         unsigned long index;
3027         int idx = 0;
3028         int err = 0;
3029
3030         devlinks_xa_for_each_registered_get(sock_net(msg->sk), index, devlink) {
3031                 if (!devlink->ops->sb_tc_pool_bind_get)
3032                         goto retry;
3033
3034                 devl_lock(devlink);
3035                 list_for_each_entry(devlink_sb, &devlink->sb_list, list) {
3036                         err = __sb_tc_pool_bind_get_dumpit(msg, start, &idx,
3037                                                            devlink,
3038                                                            devlink_sb,
3039                                                            NETLINK_CB(cb->skb).portid,
3040                                                            cb->nlh->nlmsg_seq);
3041                         if (err == -EOPNOTSUPP) {
3042                                 err = 0;
3043                         } else if (err) {
3044                                 devl_unlock(devlink);
3045                                 devlink_put(devlink);
3046                                 goto out;
3047                         }
3048                 }
3049                 devl_unlock(devlink);
3050 retry:
3051                 devlink_put(devlink);
3052         }
3053 out:
3054         if (err != -EMSGSIZE)
3055                 return err;
3056
3057         state->idx = idx;
3058         return msg->len;
3059 }
3060
3061 static int devlink_sb_tc_pool_bind_set(struct devlink_port *devlink_port,
3062                                        unsigned int sb_index, u16 tc_index,
3063                                        enum devlink_sb_pool_type pool_type,
3064                                        u16 pool_index, u32 threshold,
3065                                        struct netlink_ext_ack *extack)
3066
3067 {
3068         const struct devlink_ops *ops = devlink_port->devlink->ops;
3069
3070         if (ops->sb_tc_pool_bind_set)
3071                 return ops->sb_tc_pool_bind_set(devlink_port, sb_index,
3072                                                 tc_index, pool_type,
3073                                                 pool_index, threshold, extack);
3074         return -EOPNOTSUPP;
3075 }
3076
3077 static int devlink_nl_cmd_sb_tc_pool_bind_set_doit(struct sk_buff *skb,
3078                                                    struct genl_info *info)
3079 {
3080         struct devlink_port *devlink_port = info->user_ptr[1];
3081         struct devlink *devlink = info->user_ptr[0];
3082         enum devlink_sb_pool_type pool_type;
3083         struct devlink_sb *devlink_sb;
3084         u16 tc_index;
3085         u16 pool_index;
3086         u32 threshold;
3087         int err;
3088
3089         devlink_sb = devlink_sb_get_from_info(devlink, info);
3090         if (IS_ERR(devlink_sb))
3091                 return PTR_ERR(devlink_sb);
3092
3093         err = devlink_sb_pool_type_get_from_info(info, &pool_type);
3094         if (err)
3095                 return err;
3096
3097         err = devlink_sb_tc_index_get_from_info(devlink_sb, info,
3098                                                 pool_type, &tc_index);
3099         if (err)
3100                 return err;
3101
3102         err = devlink_sb_pool_index_get_from_info(devlink_sb, info,
3103                                                   &pool_index);
3104         if (err)
3105                 return err;
3106
3107         if (GENL_REQ_ATTR_CHECK(info, DEVLINK_ATTR_SB_THRESHOLD))
3108                 return -EINVAL;
3109
3110         threshold = nla_get_u32(info->attrs[DEVLINK_ATTR_SB_THRESHOLD]);
3111         return devlink_sb_tc_pool_bind_set(devlink_port, devlink_sb->index,
3112                                            tc_index, pool_type,
3113                                            pool_index, threshold, info->extack);
3114 }
3115
3116 static int devlink_nl_cmd_sb_occ_snapshot_doit(struct sk_buff *skb,
3117                                                struct genl_info *info)
3118 {
3119         struct devlink *devlink = info->user_ptr[0];
3120         const struct devlink_ops *ops = devlink->ops;
3121         struct devlink_sb *devlink_sb;
3122
3123         devlink_sb = devlink_sb_get_from_info(devlink, info);
3124         if (IS_ERR(devlink_sb))
3125                 return PTR_ERR(devlink_sb);
3126
3127         if (ops->sb_occ_snapshot)
3128                 return ops->sb_occ_snapshot(devlink, devlink_sb->index);
3129         return -EOPNOTSUPP;
3130 }
3131
3132 static int devlink_nl_cmd_sb_occ_max_clear_doit(struct sk_buff *skb,
3133                                                 struct genl_info *info)
3134 {
3135         struct devlink *devlink = info->user_ptr[0];
3136         const struct devlink_ops *ops = devlink->ops;
3137         struct devlink_sb *devlink_sb;
3138
3139         devlink_sb = devlink_sb_get_from_info(devlink, info);
3140         if (IS_ERR(devlink_sb))
3141                 return PTR_ERR(devlink_sb);
3142
3143         if (ops->sb_occ_max_clear)
3144                 return ops->sb_occ_max_clear(devlink, devlink_sb->index);
3145         return -EOPNOTSUPP;
3146 }
3147
3148 static int devlink_nl_eswitch_fill(struct sk_buff *msg, struct devlink *devlink,
3149                                    enum devlink_command cmd, u32 portid,
3150                                    u32 seq, int flags)
3151 {
3152         const struct devlink_ops *ops = devlink->ops;
3153         enum devlink_eswitch_encap_mode encap_mode;
3154         u8 inline_mode;
3155         void *hdr;
3156         int err = 0;
3157         u16 mode;
3158
3159         hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
3160         if (!hdr)
3161                 return -EMSGSIZE;
3162
3163         err = devlink_nl_put_handle(msg, devlink);
3164         if (err)
3165                 goto nla_put_failure;
3166
3167         if (ops->eswitch_mode_get) {
3168                 err = ops->eswitch_mode_get(devlink, &mode);
3169                 if (err)
3170                         goto nla_put_failure;
3171                 err = nla_put_u16(msg, DEVLINK_ATTR_ESWITCH_MODE, mode);
3172                 if (err)
3173                         goto nla_put_failure;
3174         }
3175
3176         if (ops->eswitch_inline_mode_get) {
3177                 err = ops->eswitch_inline_mode_get(devlink, &inline_mode);
3178                 if (err)
3179                         goto nla_put_failure;
3180                 err = nla_put_u8(msg, DEVLINK_ATTR_ESWITCH_INLINE_MODE,
3181                                  inline_mode);
3182                 if (err)
3183                         goto nla_put_failure;
3184         }
3185
3186         if (ops->eswitch_encap_mode_get) {
3187                 err = ops->eswitch_encap_mode_get(devlink, &encap_mode);
3188                 if (err)
3189                         goto nla_put_failure;
3190                 err = nla_put_u8(msg, DEVLINK_ATTR_ESWITCH_ENCAP_MODE, encap_mode);
3191                 if (err)
3192                         goto nla_put_failure;
3193         }
3194
3195         genlmsg_end(msg, hdr);
3196         return 0;
3197
3198 nla_put_failure:
3199         genlmsg_cancel(msg, hdr);
3200         return err;
3201 }
3202
3203 static int devlink_nl_cmd_eswitch_get_doit(struct sk_buff *skb,
3204                                            struct genl_info *info)
3205 {
3206         struct devlink *devlink = info->user_ptr[0];
3207         struct sk_buff *msg;
3208         int err;
3209
3210         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
3211         if (!msg)
3212                 return -ENOMEM;
3213
3214         err = devlink_nl_eswitch_fill(msg, devlink, DEVLINK_CMD_ESWITCH_GET,
3215                                       info->snd_portid, info->snd_seq, 0);
3216
3217         if (err) {
3218                 nlmsg_free(msg);
3219                 return err;
3220         }
3221
3222         return genlmsg_reply(msg, info);
3223 }
3224
3225 static int devlink_rate_nodes_check(struct devlink *devlink, u16 mode,
3226                                     struct netlink_ext_ack *extack)
3227 {
3228         struct devlink_rate *devlink_rate;
3229
3230         list_for_each_entry(devlink_rate, &devlink->rate_list, list)
3231                 if (devlink_rate_is_node(devlink_rate)) {
3232                         NL_SET_ERR_MSG_MOD(extack, "Rate node(s) exists.");
3233                         return -EBUSY;
3234                 }
3235         return 0;
3236 }
3237
3238 static int devlink_nl_cmd_eswitch_set_doit(struct sk_buff *skb,
3239                                            struct genl_info *info)
3240 {
3241         struct devlink *devlink = info->user_ptr[0];
3242         const struct devlink_ops *ops = devlink->ops;
3243         enum devlink_eswitch_encap_mode encap_mode;
3244         u8 inline_mode;
3245         int err = 0;
3246         u16 mode;
3247
3248         if (info->attrs[DEVLINK_ATTR_ESWITCH_MODE]) {
3249                 if (!ops->eswitch_mode_set)
3250                         return -EOPNOTSUPP;
3251                 mode = nla_get_u16(info->attrs[DEVLINK_ATTR_ESWITCH_MODE]);
3252                 err = devlink_rate_nodes_check(devlink, mode, info->extack);
3253                 if (err)
3254                         return err;
3255                 err = ops->eswitch_mode_set(devlink, mode, info->extack);
3256                 if (err)
3257                         return err;
3258         }
3259
3260         if (info->attrs[DEVLINK_ATTR_ESWITCH_INLINE_MODE]) {
3261                 if (!ops->eswitch_inline_mode_set)
3262                         return -EOPNOTSUPP;
3263                 inline_mode = nla_get_u8(
3264                                 info->attrs[DEVLINK_ATTR_ESWITCH_INLINE_MODE]);
3265                 err = ops->eswitch_inline_mode_set(devlink, inline_mode,
3266                                                    info->extack);
3267                 if (err)
3268                         return err;
3269         }
3270
3271         if (info->attrs[DEVLINK_ATTR_ESWITCH_ENCAP_MODE]) {
3272                 if (!ops->eswitch_encap_mode_set)
3273                         return -EOPNOTSUPP;
3274                 encap_mode = nla_get_u8(info->attrs[DEVLINK_ATTR_ESWITCH_ENCAP_MODE]);
3275                 err = ops->eswitch_encap_mode_set(devlink, encap_mode,
3276                                                   info->extack);
3277                 if (err)
3278                         return err;
3279         }
3280
3281         return 0;
3282 }
3283
3284 int devlink_dpipe_match_put(struct sk_buff *skb,
3285                             struct devlink_dpipe_match *match)
3286 {
3287         struct devlink_dpipe_header *header = match->header;
3288         struct devlink_dpipe_field *field = &header->fields[match->field_id];
3289         struct nlattr *match_attr;
3290
3291         match_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_DPIPE_MATCH);
3292         if (!match_attr)
3293                 return -EMSGSIZE;
3294
3295         if (nla_put_u32(skb, DEVLINK_ATTR_DPIPE_MATCH_TYPE, match->type) ||
3296             nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_INDEX, match->header_index) ||
3297             nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_ID, header->id) ||
3298             nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_ID, field->id) ||
3299             nla_put_u8(skb, DEVLINK_ATTR_DPIPE_HEADER_GLOBAL, header->global))
3300                 goto nla_put_failure;
3301
3302         nla_nest_end(skb, match_attr);
3303         return 0;
3304
3305 nla_put_failure:
3306         nla_nest_cancel(skb, match_attr);
3307         return -EMSGSIZE;
3308 }
3309 EXPORT_SYMBOL_GPL(devlink_dpipe_match_put);
3310
3311 static int devlink_dpipe_matches_put(struct devlink_dpipe_table *table,
3312                                      struct sk_buff *skb)
3313 {
3314         struct nlattr *matches_attr;
3315
3316         matches_attr = nla_nest_start_noflag(skb,
3317                                              DEVLINK_ATTR_DPIPE_TABLE_MATCHES);
3318         if (!matches_attr)
3319                 return -EMSGSIZE;
3320
3321         if (table->table_ops->matches_dump(table->priv, skb))
3322                 goto nla_put_failure;
3323
3324         nla_nest_end(skb, matches_attr);
3325         return 0;
3326
3327 nla_put_failure:
3328         nla_nest_cancel(skb, matches_attr);
3329         return -EMSGSIZE;
3330 }
3331
3332 int devlink_dpipe_action_put(struct sk_buff *skb,
3333                              struct devlink_dpipe_action *action)
3334 {
3335         struct devlink_dpipe_header *header = action->header;
3336         struct devlink_dpipe_field *field = &header->fields[action->field_id];
3337         struct nlattr *action_attr;
3338
3339         action_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_DPIPE_ACTION);
3340         if (!action_attr)
3341                 return -EMSGSIZE;
3342
3343         if (nla_put_u32(skb, DEVLINK_ATTR_DPIPE_ACTION_TYPE, action->type) ||
3344             nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_INDEX, action->header_index) ||
3345             nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_ID, header->id) ||
3346             nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_ID, field->id) ||
3347             nla_put_u8(skb, DEVLINK_ATTR_DPIPE_HEADER_GLOBAL, header->global))
3348                 goto nla_put_failure;
3349
3350         nla_nest_end(skb, action_attr);
3351         return 0;
3352
3353 nla_put_failure:
3354         nla_nest_cancel(skb, action_attr);
3355         return -EMSGSIZE;
3356 }
3357 EXPORT_SYMBOL_GPL(devlink_dpipe_action_put);
3358
3359 static int devlink_dpipe_actions_put(struct devlink_dpipe_table *table,
3360                                      struct sk_buff *skb)
3361 {
3362         struct nlattr *actions_attr;
3363
3364         actions_attr = nla_nest_start_noflag(skb,
3365                                              DEVLINK_ATTR_DPIPE_TABLE_ACTIONS);
3366         if (!actions_attr)
3367                 return -EMSGSIZE;
3368
3369         if (table->table_ops->actions_dump(table->priv, skb))
3370                 goto nla_put_failure;
3371
3372         nla_nest_end(skb, actions_attr);
3373         return 0;
3374
3375 nla_put_failure:
3376         nla_nest_cancel(skb, actions_attr);
3377         return -EMSGSIZE;
3378 }
3379
3380 static int devlink_dpipe_table_put(struct sk_buff *skb,
3381                                    struct devlink_dpipe_table *table)
3382 {
3383         struct nlattr *table_attr;
3384         u64 table_size;
3385
3386         table_size = table->table_ops->size_get(table->priv);
3387         table_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_DPIPE_TABLE);
3388         if (!table_attr)
3389                 return -EMSGSIZE;
3390
3391         if (nla_put_string(skb, DEVLINK_ATTR_DPIPE_TABLE_NAME, table->name) ||
3392             nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_TABLE_SIZE, table_size,
3393                               DEVLINK_ATTR_PAD))
3394                 goto nla_put_failure;
3395         if (nla_put_u8(skb, DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED,
3396                        table->counters_enabled))
3397                 goto nla_put_failure;
3398
3399         if (table->resource_valid) {
3400                 if (nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_TABLE_RESOURCE_ID,
3401                                       table->resource_id, DEVLINK_ATTR_PAD) ||
3402                     nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_TABLE_RESOURCE_UNITS,
3403                                       table->resource_units, DEVLINK_ATTR_PAD))
3404                         goto nla_put_failure;
3405         }
3406         if (devlink_dpipe_matches_put(table, skb))
3407                 goto nla_put_failure;
3408
3409         if (devlink_dpipe_actions_put(table, skb))
3410                 goto nla_put_failure;
3411
3412         nla_nest_end(skb, table_attr);
3413         return 0;
3414
3415 nla_put_failure:
3416         nla_nest_cancel(skb, table_attr);
3417         return -EMSGSIZE;
3418 }
3419
3420 static int devlink_dpipe_send_and_alloc_skb(struct sk_buff **pskb,
3421                                             struct genl_info *info)
3422 {
3423         int err;
3424
3425         if (*pskb) {
3426                 err = genlmsg_reply(*pskb, info);
3427                 if (err)
3428                         return err;
3429         }
3430         *pskb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
3431         if (!*pskb)
3432                 return -ENOMEM;
3433         return 0;
3434 }
3435
3436 static int devlink_dpipe_tables_fill(struct genl_info *info,
3437                                      enum devlink_command cmd, int flags,
3438                                      struct list_head *dpipe_tables,
3439                                      const char *table_name)
3440 {
3441         struct devlink *devlink = info->user_ptr[0];
3442         struct devlink_dpipe_table *table;
3443         struct nlattr *tables_attr;
3444         struct sk_buff *skb = NULL;
3445         struct nlmsghdr *nlh;
3446         bool incomplete;
3447         void *hdr;
3448         int i;
3449         int err;
3450
3451         table = list_first_entry(dpipe_tables,
3452                                  struct devlink_dpipe_table, list);
3453 start_again:
3454         err = devlink_dpipe_send_and_alloc_skb(&skb, info);
3455         if (err)
3456                 return err;
3457
3458         hdr = genlmsg_put(skb, info->snd_portid, info->snd_seq,
3459                           &devlink_nl_family, NLM_F_MULTI, cmd);
3460         if (!hdr) {
3461                 nlmsg_free(skb);
3462                 return -EMSGSIZE;
3463         }
3464
3465         if (devlink_nl_put_handle(skb, devlink))
3466                 goto nla_put_failure;
3467         tables_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_DPIPE_TABLES);
3468         if (!tables_attr)
3469                 goto nla_put_failure;
3470
3471         i = 0;
3472         incomplete = false;
3473         list_for_each_entry_from(table, dpipe_tables, list) {
3474                 if (!table_name) {
3475                         err = devlink_dpipe_table_put(skb, table);
3476                         if (err) {
3477                                 if (!i)
3478                                         goto err_table_put;
3479                                 incomplete = true;
3480                                 break;
3481                         }
3482                 } else {
3483                         if (!strcmp(table->name, table_name)) {
3484                                 err = devlink_dpipe_table_put(skb, table);
3485                                 if (err)
3486                                         break;
3487                         }
3488                 }
3489                 i++;
3490         }
3491
3492         nla_nest_end(skb, tables_attr);
3493         genlmsg_end(skb, hdr);
3494         if (incomplete)
3495                 goto start_again;
3496
3497 send_done:
3498         nlh = nlmsg_put(skb, info->snd_portid, info->snd_seq,
3499                         NLMSG_DONE, 0, flags | NLM_F_MULTI);
3500         if (!nlh) {
3501                 err = devlink_dpipe_send_and_alloc_skb(&skb, info);
3502                 if (err)
3503                         return err;
3504                 goto send_done;
3505         }
3506
3507         return genlmsg_reply(skb, info);
3508
3509 nla_put_failure:
3510         err = -EMSGSIZE;
3511 err_table_put:
3512         nlmsg_free(skb);
3513         return err;
3514 }
3515
3516 static int devlink_nl_cmd_dpipe_table_get(struct sk_buff *skb,
3517                                           struct genl_info *info)
3518 {
3519         struct devlink *devlink = info->user_ptr[0];
3520         const char *table_name =  NULL;
3521
3522         if (info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME])
3523                 table_name = nla_data(info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME]);
3524
3525         return devlink_dpipe_tables_fill(info, DEVLINK_CMD_DPIPE_TABLE_GET, 0,
3526                                          &devlink->dpipe_table_list,
3527                                          table_name);
3528 }
3529
3530 static int devlink_dpipe_value_put(struct sk_buff *skb,
3531                                    struct devlink_dpipe_value *value)
3532 {
3533         if (nla_put(skb, DEVLINK_ATTR_DPIPE_VALUE,
3534                     value->value_size, value->value))
3535                 return -EMSGSIZE;
3536         if (value->mask)
3537                 if (nla_put(skb, DEVLINK_ATTR_DPIPE_VALUE_MASK,
3538                             value->value_size, value->mask))
3539                         return -EMSGSIZE;
3540         if (value->mapping_valid)
3541                 if (nla_put_u32(skb, DEVLINK_ATTR_DPIPE_VALUE_MAPPING,
3542                                 value->mapping_value))
3543                         return -EMSGSIZE;
3544         return 0;
3545 }
3546
3547 static int devlink_dpipe_action_value_put(struct sk_buff *skb,
3548                                           struct devlink_dpipe_value *value)
3549 {
3550         if (!value->action)
3551                 return -EINVAL;
3552         if (devlink_dpipe_action_put(skb, value->action))
3553                 return -EMSGSIZE;
3554         if (devlink_dpipe_value_put(skb, value))
3555                 return -EMSGSIZE;
3556         return 0;
3557 }
3558
3559 static int devlink_dpipe_action_values_put(struct sk_buff *skb,
3560                                            struct devlink_dpipe_value *values,
3561                                            unsigned int values_count)
3562 {
3563         struct nlattr *action_attr;
3564         int i;
3565         int err;
3566
3567         for (i = 0; i < values_count; i++) {
3568                 action_attr = nla_nest_start_noflag(skb,
3569                                                     DEVLINK_ATTR_DPIPE_ACTION_VALUE);
3570                 if (!action_attr)
3571                         return -EMSGSIZE;
3572                 err = devlink_dpipe_action_value_put(skb, &values[i]);
3573                 if (err)
3574                         goto err_action_value_put;
3575                 nla_nest_end(skb, action_attr);
3576         }
3577         return 0;
3578
3579 err_action_value_put:
3580         nla_nest_cancel(skb, action_attr);
3581         return err;
3582 }
3583
3584 static int devlink_dpipe_match_value_put(struct sk_buff *skb,
3585                                          struct devlink_dpipe_value *value)
3586 {
3587         if (!value->match)
3588                 return -EINVAL;
3589         if (devlink_dpipe_match_put(skb, value->match))
3590                 return -EMSGSIZE;
3591         if (devlink_dpipe_value_put(skb, value))
3592                 return -EMSGSIZE;
3593         return 0;
3594 }
3595
3596 static int devlink_dpipe_match_values_put(struct sk_buff *skb,
3597                                           struct devlink_dpipe_value *values,
3598                                           unsigned int values_count)
3599 {
3600         struct nlattr *match_attr;
3601         int i;
3602         int err;
3603
3604         for (i = 0; i < values_count; i++) {
3605                 match_attr = nla_nest_start_noflag(skb,
3606                                                    DEVLINK_ATTR_DPIPE_MATCH_VALUE);
3607                 if (!match_attr)
3608                         return -EMSGSIZE;
3609                 err = devlink_dpipe_match_value_put(skb, &values[i]);
3610                 if (err)
3611                         goto err_match_value_put;
3612                 nla_nest_end(skb, match_attr);
3613         }
3614         return 0;
3615
3616 err_match_value_put:
3617         nla_nest_cancel(skb, match_attr);
3618         return err;
3619 }
3620
3621 static int devlink_dpipe_entry_put(struct sk_buff *skb,
3622                                    struct devlink_dpipe_entry *entry)
3623 {
3624         struct nlattr *entry_attr, *matches_attr, *actions_attr;
3625         int err;
3626
3627         entry_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_DPIPE_ENTRY);
3628         if (!entry_attr)
3629                 return  -EMSGSIZE;
3630
3631         if (nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_ENTRY_INDEX, entry->index,
3632                               DEVLINK_ATTR_PAD))
3633                 goto nla_put_failure;
3634         if (entry->counter_valid)
3635                 if (nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_ENTRY_COUNTER,
3636                                       entry->counter, DEVLINK_ATTR_PAD))
3637                         goto nla_put_failure;
3638
3639         matches_attr = nla_nest_start_noflag(skb,
3640                                              DEVLINK_ATTR_DPIPE_ENTRY_MATCH_VALUES);
3641         if (!matches_attr)
3642                 goto nla_put_failure;
3643
3644         err = devlink_dpipe_match_values_put(skb, entry->match_values,
3645                                              entry->match_values_count);
3646         if (err) {
3647                 nla_nest_cancel(skb, matches_attr);
3648                 goto err_match_values_put;
3649         }
3650         nla_nest_end(skb, matches_attr);
3651
3652         actions_attr = nla_nest_start_noflag(skb,
3653                                              DEVLINK_ATTR_DPIPE_ENTRY_ACTION_VALUES);
3654         if (!actions_attr)
3655                 goto nla_put_failure;
3656
3657         err = devlink_dpipe_action_values_put(skb, entry->action_values,
3658                                               entry->action_values_count);
3659         if (err) {
3660                 nla_nest_cancel(skb, actions_attr);
3661                 goto err_action_values_put;
3662         }
3663         nla_nest_end(skb, actions_attr);
3664
3665         nla_nest_end(skb, entry_attr);
3666         return 0;
3667
3668 nla_put_failure:
3669         err = -EMSGSIZE;
3670 err_match_values_put:
3671 err_action_values_put:
3672         nla_nest_cancel(skb, entry_attr);
3673         return err;
3674 }
3675
3676 static struct devlink_dpipe_table *
3677 devlink_dpipe_table_find(struct list_head *dpipe_tables,
3678                          const char *table_name, struct devlink *devlink)
3679 {
3680         struct devlink_dpipe_table *table;
3681         list_for_each_entry_rcu(table, dpipe_tables, list,
3682                                 lockdep_is_held(&devlink->lock)) {
3683                 if (!strcmp(table->name, table_name))
3684                         return table;
3685         }
3686         return NULL;
3687 }
3688
3689 int devlink_dpipe_entry_ctx_prepare(struct devlink_dpipe_dump_ctx *dump_ctx)
3690 {
3691         struct devlink *devlink;
3692         int err;
3693
3694         err = devlink_dpipe_send_and_alloc_skb(&dump_ctx->skb,
3695                                                dump_ctx->info);
3696         if (err)
3697                 return err;
3698
3699         dump_ctx->hdr = genlmsg_put(dump_ctx->skb,
3700                                     dump_ctx->info->snd_portid,
3701                                     dump_ctx->info->snd_seq,
3702                                     &devlink_nl_family, NLM_F_MULTI,
3703                                     dump_ctx->cmd);
3704         if (!dump_ctx->hdr)
3705                 goto nla_put_failure;
3706
3707         devlink = dump_ctx->info->user_ptr[0];
3708         if (devlink_nl_put_handle(dump_ctx->skb, devlink))
3709                 goto nla_put_failure;
3710         dump_ctx->nest = nla_nest_start_noflag(dump_ctx->skb,
3711                                                DEVLINK_ATTR_DPIPE_ENTRIES);
3712         if (!dump_ctx->nest)
3713                 goto nla_put_failure;
3714         return 0;
3715
3716 nla_put_failure:
3717         nlmsg_free(dump_ctx->skb);
3718         return -EMSGSIZE;
3719 }
3720 EXPORT_SYMBOL_GPL(devlink_dpipe_entry_ctx_prepare);
3721
3722 int devlink_dpipe_entry_ctx_append(struct devlink_dpipe_dump_ctx *dump_ctx,
3723                                    struct devlink_dpipe_entry *entry)
3724 {
3725         return devlink_dpipe_entry_put(dump_ctx->skb, entry);
3726 }
3727 EXPORT_SYMBOL_GPL(devlink_dpipe_entry_ctx_append);
3728
3729 int devlink_dpipe_entry_ctx_close(struct devlink_dpipe_dump_ctx *dump_ctx)
3730 {
3731         nla_nest_end(dump_ctx->skb, dump_ctx->nest);
3732         genlmsg_end(dump_ctx->skb, dump_ctx->hdr);
3733         return 0;
3734 }
3735 EXPORT_SYMBOL_GPL(devlink_dpipe_entry_ctx_close);
3736
3737 void devlink_dpipe_entry_clear(struct devlink_dpipe_entry *entry)
3738
3739 {
3740         unsigned int value_count, value_index;
3741         struct devlink_dpipe_value *value;
3742
3743         value = entry->action_values;
3744         value_count = entry->action_values_count;
3745         for (value_index = 0; value_index < value_count; value_index++) {
3746                 kfree(value[value_index].value);
3747                 kfree(value[value_index].mask);
3748         }
3749
3750         value = entry->match_values;
3751         value_count = entry->match_values_count;
3752         for (value_index = 0; value_index < value_count; value_index++) {
3753                 kfree(value[value_index].value);
3754                 kfree(value[value_index].mask);
3755         }
3756 }
3757 EXPORT_SYMBOL_GPL(devlink_dpipe_entry_clear);
3758
3759 static int devlink_dpipe_entries_fill(struct genl_info *info,
3760                                       enum devlink_command cmd, int flags,
3761                                       struct devlink_dpipe_table *table)
3762 {
3763         struct devlink_dpipe_dump_ctx dump_ctx;
3764         struct nlmsghdr *nlh;
3765         int err;
3766
3767         dump_ctx.skb = NULL;
3768         dump_ctx.cmd = cmd;
3769         dump_ctx.info = info;
3770
3771         err = table->table_ops->entries_dump(table->priv,
3772                                              table->counters_enabled,
3773                                              &dump_ctx);
3774         if (err)
3775                 return err;
3776
3777 send_done:
3778         nlh = nlmsg_put(dump_ctx.skb, info->snd_portid, info->snd_seq,
3779                         NLMSG_DONE, 0, flags | NLM_F_MULTI);
3780         if (!nlh) {
3781                 err = devlink_dpipe_send_and_alloc_skb(&dump_ctx.skb, info);
3782                 if (err)
3783                         return err;
3784                 goto send_done;
3785         }
3786         return genlmsg_reply(dump_ctx.skb, info);
3787 }
3788
3789 static int devlink_nl_cmd_dpipe_entries_get(struct sk_buff *skb,
3790                                             struct genl_info *info)
3791 {
3792         struct devlink *devlink = info->user_ptr[0];
3793         struct devlink_dpipe_table *table;
3794         const char *table_name;
3795
3796         if (GENL_REQ_ATTR_CHECK(info, DEVLINK_ATTR_DPIPE_TABLE_NAME))
3797                 return -EINVAL;
3798
3799         table_name = nla_data(info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME]);
3800         table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
3801                                          table_name, devlink);
3802         if (!table)
3803                 return -EINVAL;
3804
3805         if (!table->table_ops->entries_dump)
3806                 return -EINVAL;
3807
3808         return devlink_dpipe_entries_fill(info, DEVLINK_CMD_DPIPE_ENTRIES_GET,
3809                                           0, table);
3810 }
3811
3812 static int devlink_dpipe_fields_put(struct sk_buff *skb,
3813                                     const struct devlink_dpipe_header *header)
3814 {
3815         struct devlink_dpipe_field *field;
3816         struct nlattr *field_attr;
3817         int i;
3818
3819         for (i = 0; i < header->fields_count; i++) {
3820                 field = &header->fields[i];
3821                 field_attr = nla_nest_start_noflag(skb,
3822                                                    DEVLINK_ATTR_DPIPE_FIELD);
3823                 if (!field_attr)
3824                         return -EMSGSIZE;
3825                 if (nla_put_string(skb, DEVLINK_ATTR_DPIPE_FIELD_NAME, field->name) ||
3826                     nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_ID, field->id) ||
3827                     nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_BITWIDTH, field->bitwidth) ||
3828                     nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_MAPPING_TYPE, field->mapping_type))
3829                         goto nla_put_failure;
3830                 nla_nest_end(skb, field_attr);
3831         }
3832         return 0;
3833
3834 nla_put_failure:
3835         nla_nest_cancel(skb, field_attr);
3836         return -EMSGSIZE;
3837 }
3838
3839 static int devlink_dpipe_header_put(struct sk_buff *skb,
3840                                     struct devlink_dpipe_header *header)
3841 {
3842         struct nlattr *fields_attr, *header_attr;
3843         int err;
3844
3845         header_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_DPIPE_HEADER);
3846         if (!header_attr)
3847                 return -EMSGSIZE;
3848
3849         if (nla_put_string(skb, DEVLINK_ATTR_DPIPE_HEADER_NAME, header->name) ||
3850             nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_ID, header->id) ||
3851             nla_put_u8(skb, DEVLINK_ATTR_DPIPE_HEADER_GLOBAL, header->global))
3852                 goto nla_put_failure;
3853
3854         fields_attr = nla_nest_start_noflag(skb,
3855                                             DEVLINK_ATTR_DPIPE_HEADER_FIELDS);
3856         if (!fields_attr)
3857                 goto nla_put_failure;
3858
3859         err = devlink_dpipe_fields_put(skb, header);
3860         if (err) {
3861                 nla_nest_cancel(skb, fields_attr);
3862                 goto nla_put_failure;
3863         }
3864         nla_nest_end(skb, fields_attr);
3865         nla_nest_end(skb, header_attr);
3866         return 0;
3867
3868 nla_put_failure:
3869         err = -EMSGSIZE;
3870         nla_nest_cancel(skb, header_attr);
3871         return err;
3872 }
3873
3874 static int devlink_dpipe_headers_fill(struct genl_info *info,
3875                                       enum devlink_command cmd, int flags,
3876                                       struct devlink_dpipe_headers *
3877                                       dpipe_headers)
3878 {
3879         struct devlink *devlink = info->user_ptr[0];
3880         struct nlattr *headers_attr;
3881         struct sk_buff *skb = NULL;
3882         struct nlmsghdr *nlh;
3883         void *hdr;
3884         int i, j;
3885         int err;
3886
3887         i = 0;
3888 start_again:
3889         err = devlink_dpipe_send_and_alloc_skb(&skb, info);
3890         if (err)
3891                 return err;
3892
3893         hdr = genlmsg_put(skb, info->snd_portid, info->snd_seq,
3894                           &devlink_nl_family, NLM_F_MULTI, cmd);
3895         if (!hdr) {
3896                 nlmsg_free(skb);
3897                 return -EMSGSIZE;
3898         }
3899
3900         if (devlink_nl_put_handle(skb, devlink))
3901                 goto nla_put_failure;
3902         headers_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_DPIPE_HEADERS);
3903         if (!headers_attr)
3904                 goto nla_put_failure;
3905
3906         j = 0;
3907         for (; i < dpipe_headers->headers_count; i++) {
3908                 err = devlink_dpipe_header_put(skb, dpipe_headers->headers[i]);
3909                 if (err) {
3910                         if (!j)
3911                                 goto err_table_put;
3912                         break;
3913                 }
3914                 j++;
3915         }
3916         nla_nest_end(skb, headers_attr);
3917         genlmsg_end(skb, hdr);
3918         if (i != dpipe_headers->headers_count)
3919                 goto start_again;
3920
3921 send_done:
3922         nlh = nlmsg_put(skb, info->snd_portid, info->snd_seq,
3923                         NLMSG_DONE, 0, flags | NLM_F_MULTI);
3924         if (!nlh) {
3925                 err = devlink_dpipe_send_and_alloc_skb(&skb, info);
3926                 if (err)
3927                         return err;
3928                 goto send_done;
3929         }
3930         return genlmsg_reply(skb, info);
3931
3932 nla_put_failure:
3933         err = -EMSGSIZE;
3934 err_table_put:
3935         nlmsg_free(skb);
3936         return err;
3937 }
3938
3939 static int devlink_nl_cmd_dpipe_headers_get(struct sk_buff *skb,
3940                                             struct genl_info *info)
3941 {
3942         struct devlink *devlink = info->user_ptr[0];
3943
3944         if (!devlink->dpipe_headers)
3945                 return -EOPNOTSUPP;
3946         return devlink_dpipe_headers_fill(info, DEVLINK_CMD_DPIPE_HEADERS_GET,
3947                                           0, devlink->dpipe_headers);
3948 }
3949
3950 static int devlink_dpipe_table_counters_set(struct devlink *devlink,
3951                                             const char *table_name,
3952                                             bool enable)
3953 {
3954         struct devlink_dpipe_table *table;
3955
3956         table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
3957                                          table_name, devlink);
3958         if (!table)
3959                 return -EINVAL;
3960
3961         if (table->counter_control_extern)
3962                 return -EOPNOTSUPP;
3963
3964         if (!(table->counters_enabled ^ enable))
3965                 return 0;
3966
3967         table->counters_enabled = enable;
3968         if (table->table_ops->counters_set_update)
3969                 table->table_ops->counters_set_update(table->priv, enable);
3970         return 0;
3971 }
3972
3973 static int devlink_nl_cmd_dpipe_table_counters_set(struct sk_buff *skb,
3974                                                    struct genl_info *info)
3975 {
3976         struct devlink *devlink = info->user_ptr[0];
3977         const char *table_name;
3978         bool counters_enable;
3979
3980         if (GENL_REQ_ATTR_CHECK(info, DEVLINK_ATTR_DPIPE_TABLE_NAME) ||
3981             GENL_REQ_ATTR_CHECK(info,
3982                                 DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED))
3983                 return -EINVAL;
3984
3985         table_name = nla_data(info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME]);
3986         counters_enable = !!nla_get_u8(info->attrs[DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED]);
3987
3988         return devlink_dpipe_table_counters_set(devlink, table_name,
3989                                                 counters_enable);
3990 }
3991
3992 static struct devlink_resource *
3993 devlink_resource_find(struct devlink *devlink,
3994                       struct devlink_resource *resource, u64 resource_id)
3995 {
3996         struct list_head *resource_list;
3997
3998         if (resource)
3999                 resource_list = &resource->resource_list;
4000         else
4001                 resource_list = &devlink->resource_list;
4002
4003         list_for_each_entry(resource, resource_list, list) {
4004                 struct devlink_resource *child_resource;
4005
4006                 if (resource->id == resource_id)
4007                         return resource;
4008
4009                 child_resource = devlink_resource_find(devlink, resource,
4010                                                        resource_id);
4011                 if (child_resource)
4012                         return child_resource;
4013         }
4014         return NULL;
4015 }
4016
4017 static void
4018 devlink_resource_validate_children(struct devlink_resource *resource)
4019 {
4020         struct devlink_resource *child_resource;
4021         bool size_valid = true;
4022         u64 parts_size = 0;
4023
4024         if (list_empty(&resource->resource_list))
4025                 goto out;
4026
4027         list_for_each_entry(child_resource, &resource->resource_list, list)
4028                 parts_size += child_resource->size_new;
4029
4030         if (parts_size > resource->size_new)
4031                 size_valid = false;
4032 out:
4033         resource->size_valid = size_valid;
4034 }
4035
4036 static int
4037 devlink_resource_validate_size(struct devlink_resource *resource, u64 size,
4038                                struct netlink_ext_ack *extack)
4039 {
4040         u64 reminder;
4041         int err = 0;
4042
4043         if (size > resource->size_params.size_max) {
4044                 NL_SET_ERR_MSG_MOD(extack, "Size larger than maximum");
4045                 err = -EINVAL;
4046         }
4047
4048         if (size < resource->size_params.size_min) {
4049                 NL_SET_ERR_MSG_MOD(extack, "Size smaller than minimum");
4050                 err = -EINVAL;
4051         }
4052
4053         div64_u64_rem(size, resource->size_params.size_granularity, &reminder);
4054         if (reminder) {
4055                 NL_SET_ERR_MSG_MOD(extack, "Wrong granularity");
4056                 err = -EINVAL;
4057         }
4058
4059         return err;
4060 }
4061
4062 static int devlink_nl_cmd_resource_set(struct sk_buff *skb,
4063                                        struct genl_info *info)
4064 {
4065         struct devlink *devlink = info->user_ptr[0];
4066         struct devlink_resource *resource;
4067         u64 resource_id;
4068         u64 size;
4069         int err;
4070
4071         if (GENL_REQ_ATTR_CHECK(info, DEVLINK_ATTR_RESOURCE_ID) ||
4072             GENL_REQ_ATTR_CHECK(info, DEVLINK_ATTR_RESOURCE_SIZE))
4073                 return -EINVAL;
4074         resource_id = nla_get_u64(info->attrs[DEVLINK_ATTR_RESOURCE_ID]);
4075
4076         resource = devlink_resource_find(devlink, NULL, resource_id);
4077         if (!resource)
4078                 return -EINVAL;
4079
4080         size = nla_get_u64(info->attrs[DEVLINK_ATTR_RESOURCE_SIZE]);
4081         err = devlink_resource_validate_size(resource, size, info->extack);
4082         if (err)
4083                 return err;
4084
4085         resource->size_new = size;
4086         devlink_resource_validate_children(resource);
4087         if (resource->parent)
4088                 devlink_resource_validate_children(resource->parent);
4089         return 0;
4090 }
4091
4092 static int
4093 devlink_resource_size_params_put(struct devlink_resource *resource,
4094                                  struct sk_buff *skb)
4095 {
4096         struct devlink_resource_size_params *size_params;
4097
4098         size_params = &resource->size_params;
4099         if (nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_SIZE_GRAN,
4100                               size_params->size_granularity, DEVLINK_ATTR_PAD) ||
4101             nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_SIZE_MAX,
4102                               size_params->size_max, DEVLINK_ATTR_PAD) ||
4103             nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_SIZE_MIN,
4104                               size_params->size_min, DEVLINK_ATTR_PAD) ||
4105             nla_put_u8(skb, DEVLINK_ATTR_RESOURCE_UNIT, size_params->unit))
4106                 return -EMSGSIZE;
4107         return 0;
4108 }
4109
4110 static int devlink_resource_occ_put(struct devlink_resource *resource,
4111                                     struct sk_buff *skb)
4112 {
4113         if (!resource->occ_get)
4114                 return 0;
4115         return nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_OCC,
4116                                  resource->occ_get(resource->occ_get_priv),
4117                                  DEVLINK_ATTR_PAD);
4118 }
4119
4120 static int devlink_resource_put(struct devlink *devlink, struct sk_buff *skb,
4121                                 struct devlink_resource *resource)
4122 {
4123         struct devlink_resource *child_resource;
4124         struct nlattr *child_resource_attr;
4125         struct nlattr *resource_attr;
4126
4127         resource_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_RESOURCE);
4128         if (!resource_attr)
4129                 return -EMSGSIZE;
4130
4131         if (nla_put_string(skb, DEVLINK_ATTR_RESOURCE_NAME, resource->name) ||
4132             nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_SIZE, resource->size,
4133                               DEVLINK_ATTR_PAD) ||
4134             nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_ID, resource->id,
4135                               DEVLINK_ATTR_PAD))
4136                 goto nla_put_failure;
4137         if (resource->size != resource->size_new &&
4138             nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_SIZE_NEW,
4139                               resource->size_new, DEVLINK_ATTR_PAD))
4140                 goto nla_put_failure;
4141         if (devlink_resource_occ_put(resource, skb))
4142                 goto nla_put_failure;
4143         if (devlink_resource_size_params_put(resource, skb))
4144                 goto nla_put_failure;
4145         if (list_empty(&resource->resource_list))
4146                 goto out;
4147
4148         if (nla_put_u8(skb, DEVLINK_ATTR_RESOURCE_SIZE_VALID,
4149                        resource->size_valid))
4150                 goto nla_put_failure;
4151
4152         child_resource_attr = nla_nest_start_noflag(skb,
4153                                                     DEVLINK_ATTR_RESOURCE_LIST);
4154         if (!child_resource_attr)
4155                 goto nla_put_failure;
4156
4157         list_for_each_entry(child_resource, &resource->resource_list, list) {
4158                 if (devlink_resource_put(devlink, skb, child_resource))
4159                         goto resource_put_failure;
4160         }
4161
4162         nla_nest_end(skb, child_resource_attr);
4163 out:
4164         nla_nest_end(skb, resource_attr);
4165         return 0;
4166
4167 resource_put_failure:
4168         nla_nest_cancel(skb, child_resource_attr);
4169 nla_put_failure:
4170         nla_nest_cancel(skb, resource_attr);
4171         return -EMSGSIZE;
4172 }
4173
4174 static int devlink_resource_fill(struct genl_info *info,
4175                                  enum devlink_command cmd, int flags)
4176 {
4177         struct devlink *devlink = info->user_ptr[0];
4178         struct devlink_resource *resource;
4179         struct nlattr *resources_attr;
4180         struct sk_buff *skb = NULL;
4181         struct nlmsghdr *nlh;
4182         bool incomplete;
4183         void *hdr;
4184         int i;
4185         int err;
4186
4187         resource = list_first_entry(&devlink->resource_list,
4188                                     struct devlink_resource, list);
4189 start_again:
4190         err = devlink_dpipe_send_and_alloc_skb(&skb, info);
4191         if (err)
4192                 return err;
4193
4194         hdr = genlmsg_put(skb, info->snd_portid, info->snd_seq,
4195                           &devlink_nl_family, NLM_F_MULTI, cmd);
4196         if (!hdr) {
4197                 nlmsg_free(skb);
4198                 return -EMSGSIZE;
4199         }
4200
4201         if (devlink_nl_put_handle(skb, devlink))
4202                 goto nla_put_failure;
4203
4204         resources_attr = nla_nest_start_noflag(skb,
4205                                                DEVLINK_ATTR_RESOURCE_LIST);
4206         if (!resources_attr)
4207                 goto nla_put_failure;
4208
4209         incomplete = false;
4210         i = 0;
4211         list_for_each_entry_from(resource, &devlink->resource_list, list) {
4212                 err = devlink_resource_put(devlink, skb, resource);
4213                 if (err) {
4214                         if (!i)
4215                                 goto err_resource_put;
4216                         incomplete = true;
4217                         break;
4218                 }
4219                 i++;
4220         }
4221         nla_nest_end(skb, resources_attr);
4222         genlmsg_end(skb, hdr);
4223         if (incomplete)
4224                 goto start_again;
4225 send_done:
4226         nlh = nlmsg_put(skb, info->snd_portid, info->snd_seq,
4227                         NLMSG_DONE, 0, flags | NLM_F_MULTI);
4228         if (!nlh) {
4229                 err = devlink_dpipe_send_and_alloc_skb(&skb, info);
4230                 if (err)
4231                         return err;
4232                 goto send_done;
4233         }
4234         return genlmsg_reply(skb, info);
4235
4236 nla_put_failure:
4237         err = -EMSGSIZE;
4238 err_resource_put:
4239         nlmsg_free(skb);
4240         return err;
4241 }
4242
4243 static int devlink_nl_cmd_resource_dump(struct sk_buff *skb,
4244                                         struct genl_info *info)
4245 {
4246         struct devlink *devlink = info->user_ptr[0];
4247
4248         if (list_empty(&devlink->resource_list))
4249                 return -EOPNOTSUPP;
4250
4251         return devlink_resource_fill(info, DEVLINK_CMD_RESOURCE_DUMP, 0);
4252 }
4253
4254 static int
4255 devlink_resources_validate(struct devlink *devlink,
4256                            struct devlink_resource *resource,
4257                            struct genl_info *info)
4258 {
4259         struct list_head *resource_list;
4260         int err = 0;
4261
4262         if (resource)
4263                 resource_list = &resource->resource_list;
4264         else
4265                 resource_list = &devlink->resource_list;
4266
4267         list_for_each_entry(resource, resource_list, list) {
4268                 if (!resource->size_valid)
4269                         return -EINVAL;
4270                 err = devlink_resources_validate(devlink, resource, info);
4271                 if (err)
4272                         return err;
4273         }
4274         return err;
4275 }
4276
4277 static struct net *devlink_netns_get(struct sk_buff *skb,
4278                                      struct genl_info *info)
4279 {
4280         struct nlattr *netns_pid_attr = info->attrs[DEVLINK_ATTR_NETNS_PID];
4281         struct nlattr *netns_fd_attr = info->attrs[DEVLINK_ATTR_NETNS_FD];
4282         struct nlattr *netns_id_attr = info->attrs[DEVLINK_ATTR_NETNS_ID];
4283         struct net *net;
4284
4285         if (!!netns_pid_attr + !!netns_fd_attr + !!netns_id_attr > 1) {
4286                 NL_SET_ERR_MSG_MOD(info->extack, "multiple netns identifying attributes specified");
4287                 return ERR_PTR(-EINVAL);
4288         }
4289
4290         if (netns_pid_attr) {
4291                 net = get_net_ns_by_pid(nla_get_u32(netns_pid_attr));
4292         } else if (netns_fd_attr) {
4293                 net = get_net_ns_by_fd(nla_get_u32(netns_fd_attr));
4294         } else if (netns_id_attr) {
4295                 net = get_net_ns_by_id(sock_net(skb->sk),
4296                                        nla_get_u32(netns_id_attr));
4297                 if (!net)
4298                         net = ERR_PTR(-EINVAL);
4299         } else {
4300                 WARN_ON(1);
4301                 net = ERR_PTR(-EINVAL);
4302         }
4303         if (IS_ERR(net)) {
4304                 NL_SET_ERR_MSG_MOD(info->extack, "Unknown network namespace");
4305                 return ERR_PTR(-EINVAL);
4306         }
4307         if (!netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN)) {
4308                 put_net(net);
4309                 return ERR_PTR(-EPERM);
4310         }
4311         return net;
4312 }
4313
4314 static void devlink_param_notify(struct devlink *devlink,
4315                                  unsigned int port_index,
4316                                  struct devlink_param_item *param_item,
4317                                  enum devlink_command cmd);
4318
4319 static void devlink_ns_change_notify(struct devlink *devlink,
4320                                      struct net *dest_net, struct net *curr_net,
4321                                      bool new)
4322 {
4323         struct devlink_param_item *param_item;
4324         enum devlink_command cmd;
4325
4326         /* Userspace needs to be notified about devlink objects
4327          * removed from original and entering new network namespace.
4328          * The rest of the devlink objects are re-created during
4329          * reload process so the notifications are generated separatelly.
4330          */
4331
4332         if (!dest_net || net_eq(dest_net, curr_net))
4333                 return;
4334
4335         if (new)
4336                 devlink_notify(devlink, DEVLINK_CMD_NEW);
4337
4338         cmd = new ? DEVLINK_CMD_PARAM_NEW : DEVLINK_CMD_PARAM_DEL;
4339         list_for_each_entry(param_item, &devlink->param_list, list)
4340                 devlink_param_notify(devlink, 0, param_item, cmd);
4341
4342         if (!new)
4343                 devlink_notify(devlink, DEVLINK_CMD_DEL);
4344 }
4345
4346 static void devlink_reload_failed_set(struct devlink *devlink,
4347                                       bool reload_failed)
4348 {
4349         if (devlink->reload_failed == reload_failed)
4350                 return;
4351         devlink->reload_failed = reload_failed;
4352         devlink_notify(devlink, DEVLINK_CMD_NEW);
4353 }
4354
4355 bool devlink_is_reload_failed(const struct devlink *devlink)
4356 {
4357         return devlink->reload_failed;
4358 }
4359 EXPORT_SYMBOL_GPL(devlink_is_reload_failed);
4360
4361 static void
4362 __devlink_reload_stats_update(struct devlink *devlink, u32 *reload_stats,
4363                               enum devlink_reload_limit limit, u32 actions_performed)
4364 {
4365         unsigned long actions = actions_performed;
4366         int stat_idx;
4367         int action;
4368
4369         for_each_set_bit(action, &actions, __DEVLINK_RELOAD_ACTION_MAX) {
4370                 stat_idx = limit * __DEVLINK_RELOAD_ACTION_MAX + action;
4371                 reload_stats[stat_idx]++;
4372         }
4373         devlink_notify(devlink, DEVLINK_CMD_NEW);
4374 }
4375
4376 static void
4377 devlink_reload_stats_update(struct devlink *devlink, enum devlink_reload_limit limit,
4378                             u32 actions_performed)
4379 {
4380         __devlink_reload_stats_update(devlink, devlink->stats.reload_stats, limit,
4381                                       actions_performed);
4382 }
4383
4384 /**
4385  *      devlink_remote_reload_actions_performed - Update devlink on reload actions
4386  *        performed which are not a direct result of devlink reload call.
4387  *
4388  *      This should be called by a driver after performing reload actions in case it was not
4389  *      a result of devlink reload call. For example fw_activate was performed as a result
4390  *      of devlink reload triggered fw_activate on another host.
4391  *      The motivation for this function is to keep data on reload actions performed on this
4392  *      function whether it was done due to direct devlink reload call or not.
4393  *
4394  *      @devlink: devlink
4395  *      @limit: reload limit
4396  *      @actions_performed: bitmask of actions performed
4397  */
4398 void devlink_remote_reload_actions_performed(struct devlink *devlink,
4399                                              enum devlink_reload_limit limit,
4400                                              u32 actions_performed)
4401 {
4402         if (WARN_ON(!actions_performed ||
4403                     actions_performed & BIT(DEVLINK_RELOAD_ACTION_UNSPEC) ||
4404                     actions_performed >= BIT(__DEVLINK_RELOAD_ACTION_MAX) ||
4405                     limit > DEVLINK_RELOAD_LIMIT_MAX))
4406                 return;
4407
4408         __devlink_reload_stats_update(devlink, devlink->stats.remote_reload_stats, limit,
4409                                       actions_performed);
4410 }
4411 EXPORT_SYMBOL_GPL(devlink_remote_reload_actions_performed);
4412
4413 int devlink_reload(struct devlink *devlink, struct net *dest_net,
4414                    enum devlink_reload_action action,
4415                    enum devlink_reload_limit limit,
4416                    u32 *actions_performed, struct netlink_ext_ack *extack)
4417 {
4418         u32 remote_reload_stats[DEVLINK_RELOAD_STATS_ARRAY_SIZE];
4419         struct net *curr_net;
4420         int err;
4421
4422         memcpy(remote_reload_stats, devlink->stats.remote_reload_stats,
4423                sizeof(remote_reload_stats));
4424
4425         curr_net = devlink_net(devlink);
4426         devlink_ns_change_notify(devlink, dest_net, curr_net, false);
4427         err = devlink->ops->reload_down(devlink, !!dest_net, action, limit, extack);
4428         if (err)
4429                 return err;
4430
4431         if (dest_net && !net_eq(dest_net, curr_net)) {
4432                 move_netdevice_notifier_net(curr_net, dest_net,
4433                                             &devlink->netdevice_nb);
4434                 write_pnet(&devlink->_net, dest_net);
4435         }
4436
4437         err = devlink->ops->reload_up(devlink, action, limit, actions_performed, extack);
4438         devlink_reload_failed_set(devlink, !!err);
4439         if (err)
4440                 return err;
4441
4442         devlink_ns_change_notify(devlink, dest_net, curr_net, true);
4443         WARN_ON(!(*actions_performed & BIT(action)));
4444         /* Catch driver on updating the remote action within devlink reload */
4445         WARN_ON(memcmp(remote_reload_stats, devlink->stats.remote_reload_stats,
4446                        sizeof(remote_reload_stats)));
4447         devlink_reload_stats_update(devlink, limit, *actions_performed);
4448         return 0;
4449 }
4450
4451 static int
4452 devlink_nl_reload_actions_performed_snd(struct devlink *devlink, u32 actions_performed,
4453                                         enum devlink_command cmd, struct genl_info *info)
4454 {
4455         struct sk_buff *msg;
4456         void *hdr;
4457
4458         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
4459         if (!msg)
4460                 return -ENOMEM;
4461
4462         hdr = genlmsg_put(msg, info->snd_portid, info->snd_seq, &devlink_nl_family, 0, cmd);
4463         if (!hdr)
4464                 goto free_msg;
4465
4466         if (devlink_nl_put_handle(msg, devlink))
4467                 goto nla_put_failure;
4468
4469         if (nla_put_bitfield32(msg, DEVLINK_ATTR_RELOAD_ACTIONS_PERFORMED, actions_performed,
4470                                actions_performed))
4471                 goto nla_put_failure;
4472         genlmsg_end(msg, hdr);
4473
4474         return genlmsg_reply(msg, info);
4475
4476 nla_put_failure:
4477         genlmsg_cancel(msg, hdr);
4478 free_msg:
4479         nlmsg_free(msg);
4480         return -EMSGSIZE;
4481 }
4482
4483 static int devlink_nl_cmd_reload(struct sk_buff *skb, struct genl_info *info)
4484 {
4485         struct devlink *devlink = info->user_ptr[0];
4486         enum devlink_reload_action action;
4487         enum devlink_reload_limit limit;
4488         struct net *dest_net = NULL;
4489         u32 actions_performed;
4490         int err;
4491
4492         if (!(devlink->features & DEVLINK_F_RELOAD))
4493                 return -EOPNOTSUPP;
4494
4495         err = devlink_resources_validate(devlink, NULL, info);
4496         if (err) {
4497                 NL_SET_ERR_MSG_MOD(info->extack, "resources size validation failed");
4498                 return err;
4499         }
4500
4501         if (info->attrs[DEVLINK_ATTR_RELOAD_ACTION])
4502                 action = nla_get_u8(info->attrs[DEVLINK_ATTR_RELOAD_ACTION]);
4503         else
4504                 action = DEVLINK_RELOAD_ACTION_DRIVER_REINIT;
4505
4506         if (!devlink_reload_action_is_supported(devlink, action)) {
4507                 NL_SET_ERR_MSG_MOD(info->extack,
4508                                    "Requested reload action is not supported by the driver");
4509                 return -EOPNOTSUPP;
4510         }
4511
4512         limit = DEVLINK_RELOAD_LIMIT_UNSPEC;
4513         if (info->attrs[DEVLINK_ATTR_RELOAD_LIMITS]) {
4514                 struct nla_bitfield32 limits;
4515                 u32 limits_selected;
4516
4517                 limits = nla_get_bitfield32(info->attrs[DEVLINK_ATTR_RELOAD_LIMITS]);
4518                 limits_selected = limits.value & limits.selector;
4519                 if (!limits_selected) {
4520                         NL_SET_ERR_MSG_MOD(info->extack, "Invalid limit selected");
4521                         return -EINVAL;
4522                 }
4523                 for (limit = 0 ; limit <= DEVLINK_RELOAD_LIMIT_MAX ; limit++)
4524                         if (limits_selected & BIT(limit))
4525                                 break;
4526                 /* UAPI enables multiselection, but currently it is not used */
4527                 if (limits_selected != BIT(limit)) {
4528                         NL_SET_ERR_MSG_MOD(info->extack,
4529                                            "Multiselection of limit is not supported");
4530                         return -EOPNOTSUPP;
4531                 }
4532                 if (!devlink_reload_limit_is_supported(devlink, limit)) {
4533                         NL_SET_ERR_MSG_MOD(info->extack,
4534                                            "Requested limit is not supported by the driver");
4535                         return -EOPNOTSUPP;
4536                 }
4537                 if (devlink_reload_combination_is_invalid(action, limit)) {
4538                         NL_SET_ERR_MSG_MOD(info->extack,
4539                                            "Requested limit is invalid for this action");
4540                         return -EINVAL;
4541                 }
4542         }
4543         if (info->attrs[DEVLINK_ATTR_NETNS_PID] ||
4544             info->attrs[DEVLINK_ATTR_NETNS_FD] ||
4545             info->attrs[DEVLINK_ATTR_NETNS_ID]) {
4546                 dest_net = devlink_netns_get(skb, info);
4547                 if (IS_ERR(dest_net))
4548                         return PTR_ERR(dest_net);
4549         }
4550
4551         err = devlink_reload(devlink, dest_net, action, limit, &actions_performed, info->extack);
4552
4553         if (dest_net)
4554                 put_net(dest_net);
4555
4556         if (err)
4557                 return err;
4558         /* For backward compatibility generate reply only if attributes used by user */
4559         if (!info->attrs[DEVLINK_ATTR_RELOAD_ACTION] && !info->attrs[DEVLINK_ATTR_RELOAD_LIMITS])
4560                 return 0;
4561
4562         return devlink_nl_reload_actions_performed_snd(devlink, actions_performed,
4563                                                        DEVLINK_CMD_RELOAD, info);
4564 }
4565
4566 static int devlink_nl_flash_update_fill(struct sk_buff *msg,
4567                                         struct devlink *devlink,
4568                                         enum devlink_command cmd,
4569                                         struct devlink_flash_notify *params)
4570 {
4571         void *hdr;
4572
4573         hdr = genlmsg_put(msg, 0, 0, &devlink_nl_family, 0, cmd);
4574         if (!hdr)
4575                 return -EMSGSIZE;
4576
4577         if (devlink_nl_put_handle(msg, devlink))
4578                 goto nla_put_failure;
4579
4580         if (cmd != DEVLINK_CMD_FLASH_UPDATE_STATUS)
4581                 goto out;
4582
4583         if (params->status_msg &&
4584             nla_put_string(msg, DEVLINK_ATTR_FLASH_UPDATE_STATUS_MSG,
4585                            params->status_msg))
4586                 goto nla_put_failure;
4587         if (params->component &&
4588             nla_put_string(msg, DEVLINK_ATTR_FLASH_UPDATE_COMPONENT,
4589                            params->component))
4590                 goto nla_put_failure;
4591         if (nla_put_u64_64bit(msg, DEVLINK_ATTR_FLASH_UPDATE_STATUS_DONE,
4592                               params->done, DEVLINK_ATTR_PAD))
4593                 goto nla_put_failure;
4594         if (nla_put_u64_64bit(msg, DEVLINK_ATTR_FLASH_UPDATE_STATUS_TOTAL,
4595                               params->total, DEVLINK_ATTR_PAD))
4596                 goto nla_put_failure;
4597         if (nla_put_u64_64bit(msg, DEVLINK_ATTR_FLASH_UPDATE_STATUS_TIMEOUT,
4598                               params->timeout, DEVLINK_ATTR_PAD))
4599                 goto nla_put_failure;
4600
4601 out:
4602         genlmsg_end(msg, hdr);
4603         return 0;
4604
4605 nla_put_failure:
4606         genlmsg_cancel(msg, hdr);
4607         return -EMSGSIZE;
4608 }
4609
4610 static void __devlink_flash_update_notify(struct devlink *devlink,
4611                                           enum devlink_command cmd,
4612                                           struct devlink_flash_notify *params)
4613 {
4614         struct sk_buff *msg;
4615         int err;
4616
4617         WARN_ON(cmd != DEVLINK_CMD_FLASH_UPDATE &&
4618                 cmd != DEVLINK_CMD_FLASH_UPDATE_END &&
4619                 cmd != DEVLINK_CMD_FLASH_UPDATE_STATUS);
4620
4621         if (!xa_get_mark(&devlinks, devlink->index, DEVLINK_REGISTERED))
4622                 return;
4623
4624         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
4625         if (!msg)
4626                 return;
4627
4628         err = devlink_nl_flash_update_fill(msg, devlink, cmd, params);
4629         if (err)
4630                 goto out_free_msg;
4631
4632         genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
4633                                 msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
4634         return;
4635
4636 out_free_msg:
4637         nlmsg_free(msg);
4638 }
4639
4640 static void devlink_flash_update_begin_notify(struct devlink *devlink)
4641 {
4642         struct devlink_flash_notify params = {};
4643
4644         __devlink_flash_update_notify(devlink,
4645                                       DEVLINK_CMD_FLASH_UPDATE,
4646                                       &params);
4647 }
4648
4649 static void devlink_flash_update_end_notify(struct devlink *devlink)
4650 {
4651         struct devlink_flash_notify params = {};
4652
4653         __devlink_flash_update_notify(devlink,
4654                                       DEVLINK_CMD_FLASH_UPDATE_END,
4655                                       &params);
4656 }
4657
4658 void devlink_flash_update_status_notify(struct devlink *devlink,
4659                                         const char *status_msg,
4660                                         const char *component,
4661                                         unsigned long done,
4662                                         unsigned long total)
4663 {
4664         struct devlink_flash_notify params = {
4665                 .status_msg = status_msg,
4666                 .component = component,
4667                 .done = done,
4668                 .total = total,
4669         };
4670
4671         __devlink_flash_update_notify(devlink,
4672                                       DEVLINK_CMD_FLASH_UPDATE_STATUS,
4673                                       &params);
4674 }
4675 EXPORT_SYMBOL_GPL(devlink_flash_update_status_notify);
4676
4677 void devlink_flash_update_timeout_notify(struct devlink *devlink,
4678                                          const char *status_msg,
4679                                          const char *component,
4680                                          unsigned long timeout)
4681 {
4682         struct devlink_flash_notify params = {
4683                 .status_msg = status_msg,
4684                 .component = component,
4685                 .timeout = timeout,
4686         };
4687
4688         __devlink_flash_update_notify(devlink,
4689                                       DEVLINK_CMD_FLASH_UPDATE_STATUS,
4690                                       &params);
4691 }
4692 EXPORT_SYMBOL_GPL(devlink_flash_update_timeout_notify);
4693
4694 struct devlink_info_req {
4695         struct sk_buff *msg;
4696         void (*version_cb)(const char *version_name,
4697                            enum devlink_info_version_type version_type,
4698                            void *version_cb_priv);
4699         void *version_cb_priv;
4700 };
4701
4702 struct devlink_flash_component_lookup_ctx {
4703         const char *lookup_name;
4704         bool lookup_name_found;
4705 };
4706
4707 static void
4708 devlink_flash_component_lookup_cb(const char *version_name,
4709                                   enum devlink_info_version_type version_type,
4710                                   void *version_cb_priv)
4711 {
4712         struct devlink_flash_component_lookup_ctx *lookup_ctx = version_cb_priv;
4713
4714         if (version_type != DEVLINK_INFO_VERSION_TYPE_COMPONENT ||
4715             lookup_ctx->lookup_name_found)
4716                 return;
4717
4718         lookup_ctx->lookup_name_found =
4719                 !strcmp(lookup_ctx->lookup_name, version_name);
4720 }
4721
4722 static int devlink_flash_component_get(struct devlink *devlink,
4723                                        struct nlattr *nla_component,
4724                                        const char **p_component,
4725                                        struct netlink_ext_ack *extack)
4726 {
4727         struct devlink_flash_component_lookup_ctx lookup_ctx = {};
4728         struct devlink_info_req req = {};
4729         const char *component;
4730         int ret;
4731
4732         if (!nla_component)
4733                 return 0;
4734
4735         component = nla_data(nla_component);
4736
4737         if (!devlink->ops->info_get) {
4738                 NL_SET_ERR_MSG_ATTR(extack, nla_component,
4739                                     "component update is not supported by this device");
4740                 return -EOPNOTSUPP;
4741         }
4742
4743         lookup_ctx.lookup_name = component;
4744         req.version_cb = devlink_flash_component_lookup_cb;
4745         req.version_cb_priv = &lookup_ctx;
4746
4747         ret = devlink->ops->info_get(devlink, &req, NULL);
4748         if (ret)
4749                 return ret;
4750
4751         if (!lookup_ctx.lookup_name_found) {
4752                 NL_SET_ERR_MSG_ATTR(extack, nla_component,
4753                                     "selected component is not supported by this device");
4754                 return -EINVAL;
4755         }
4756         *p_component = component;
4757         return 0;
4758 }
4759
4760 static int devlink_nl_cmd_flash_update(struct sk_buff *skb,
4761                                        struct genl_info *info)
4762 {
4763         struct nlattr *nla_overwrite_mask, *nla_file_name;
4764         struct devlink_flash_update_params params = {};
4765         struct devlink *devlink = info->user_ptr[0];
4766         const char *file_name;
4767         u32 supported_params;
4768         int ret;
4769
4770         if (!devlink->ops->flash_update)
4771                 return -EOPNOTSUPP;
4772
4773         if (GENL_REQ_ATTR_CHECK(info, DEVLINK_ATTR_FLASH_UPDATE_FILE_NAME))
4774                 return -EINVAL;
4775
4776         ret = devlink_flash_component_get(devlink,
4777                                           info->attrs[DEVLINK_ATTR_FLASH_UPDATE_COMPONENT],
4778                                           &params.component, info->extack);
4779         if (ret)
4780                 return ret;
4781
4782         supported_params = devlink->ops->supported_flash_update_params;
4783
4784         nla_overwrite_mask = info->attrs[DEVLINK_ATTR_FLASH_UPDATE_OVERWRITE_MASK];
4785         if (nla_overwrite_mask) {
4786                 struct nla_bitfield32 sections;
4787
4788                 if (!(supported_params & DEVLINK_SUPPORT_FLASH_UPDATE_OVERWRITE_MASK)) {
4789                         NL_SET_ERR_MSG_ATTR(info->extack, nla_overwrite_mask,
4790                                             "overwrite settings are not supported by this device");
4791                         return -EOPNOTSUPP;
4792                 }
4793                 sections = nla_get_bitfield32(nla_overwrite_mask);
4794                 params.overwrite_mask = sections.value & sections.selector;
4795         }
4796
4797         nla_file_name = info->attrs[DEVLINK_ATTR_FLASH_UPDATE_FILE_NAME];
4798         file_name = nla_data(nla_file_name);
4799         ret = request_firmware(&params.fw, file_name, devlink->dev);
4800         if (ret) {
4801                 NL_SET_ERR_MSG_ATTR(info->extack, nla_file_name, "failed to locate the requested firmware file");
4802                 return ret;
4803         }
4804
4805         devlink_flash_update_begin_notify(devlink);
4806         ret = devlink->ops->flash_update(devlink, &params, info->extack);
4807         devlink_flash_update_end_notify(devlink);
4808
4809         release_firmware(params.fw);
4810
4811         return ret;
4812 }
4813
4814 static int
4815 devlink_nl_selftests_fill(struct sk_buff *msg, struct devlink *devlink,
4816                           u32 portid, u32 seq, int flags,
4817                           struct netlink_ext_ack *extack)
4818 {
4819         struct nlattr *selftests;
4820         void *hdr;
4821         int err;
4822         int i;
4823
4824         hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags,
4825                           DEVLINK_CMD_SELFTESTS_GET);
4826         if (!hdr)
4827                 return -EMSGSIZE;
4828
4829         err = -EMSGSIZE;
4830         if (devlink_nl_put_handle(msg, devlink))
4831                 goto err_cancel_msg;
4832
4833         selftests = nla_nest_start(msg, DEVLINK_ATTR_SELFTESTS);
4834         if (!selftests)
4835                 goto err_cancel_msg;
4836
4837         for (i = DEVLINK_ATTR_SELFTEST_ID_UNSPEC + 1;
4838              i <= DEVLINK_ATTR_SELFTEST_ID_MAX; i++) {
4839                 if (devlink->ops->selftest_check(devlink, i, extack)) {
4840                         err = nla_put_flag(msg, i);
4841                         if (err)
4842                                 goto err_cancel_msg;
4843                 }
4844         }
4845
4846         nla_nest_end(msg, selftests);
4847         genlmsg_end(msg, hdr);
4848         return 0;
4849
4850 err_cancel_msg:
4851         genlmsg_cancel(msg, hdr);
4852         return err;
4853 }
4854
4855 static int devlink_nl_cmd_selftests_get_doit(struct sk_buff *skb,
4856                                              struct genl_info *info)
4857 {
4858         struct devlink *devlink = info->user_ptr[0];
4859         struct sk_buff *msg;
4860         int err;
4861
4862         if (!devlink->ops->selftest_check)
4863                 return -EOPNOTSUPP;
4864
4865         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
4866         if (!msg)
4867                 return -ENOMEM;
4868
4869         err = devlink_nl_selftests_fill(msg, devlink, info->snd_portid,
4870                                         info->snd_seq, 0, info->extack);
4871         if (err) {
4872                 nlmsg_free(msg);
4873                 return err;
4874         }
4875
4876         return genlmsg_reply(msg, info);
4877 }
4878
4879 static int devlink_nl_cmd_selftests_get_dumpit(struct sk_buff *msg,
4880                                                struct netlink_callback *cb)
4881 {
4882         struct devlink_nl_dump_state *state = devlink_dump_state(cb);
4883         struct devlink *devlink;
4884         int start = state->idx;
4885         unsigned long index;
4886         int idx = 0;
4887         int err = 0;
4888
4889         devlinks_xa_for_each_registered_get(sock_net(msg->sk), index, devlink) {
4890                 if (idx < start || !devlink->ops->selftest_check)
4891                         goto inc;
4892
4893                 devl_lock(devlink);
4894                 err = devlink_nl_selftests_fill(msg, devlink,
4895                                                 NETLINK_CB(cb->skb).portid,
4896                                                 cb->nlh->nlmsg_seq, NLM_F_MULTI,
4897                                                 cb->extack);
4898                 devl_unlock(devlink);
4899                 if (err) {
4900                         devlink_put(devlink);
4901                         break;
4902                 }
4903 inc:
4904                 idx++;
4905                 devlink_put(devlink);
4906         }
4907
4908         if (err != -EMSGSIZE)
4909                 return err;
4910
4911         state->idx = idx;
4912         return msg->len;
4913 }
4914
4915 static int devlink_selftest_result_put(struct sk_buff *skb, unsigned int id,
4916                                        enum devlink_selftest_status test_status)
4917 {
4918         struct nlattr *result_attr;
4919
4920         result_attr = nla_nest_start(skb, DEVLINK_ATTR_SELFTEST_RESULT);
4921         if (!result_attr)
4922                 return -EMSGSIZE;
4923
4924         if (nla_put_u32(skb, DEVLINK_ATTR_SELFTEST_RESULT_ID, id) ||
4925             nla_put_u8(skb, DEVLINK_ATTR_SELFTEST_RESULT_STATUS,
4926                        test_status))
4927                 goto nla_put_failure;
4928
4929         nla_nest_end(skb, result_attr);
4930         return 0;
4931
4932 nla_put_failure:
4933         nla_nest_cancel(skb, result_attr);
4934         return -EMSGSIZE;
4935 }
4936
4937 static int devlink_nl_cmd_selftests_run(struct sk_buff *skb,
4938                                         struct genl_info *info)
4939 {
4940         struct nlattr *tb[DEVLINK_ATTR_SELFTEST_ID_MAX + 1];
4941         struct devlink *devlink = info->user_ptr[0];
4942         struct nlattr *attrs, *selftests;
4943         struct sk_buff *msg;
4944         void *hdr;
4945         int err;
4946         int i;
4947
4948         if (!devlink->ops->selftest_run || !devlink->ops->selftest_check)
4949                 return -EOPNOTSUPP;
4950
4951         if (GENL_REQ_ATTR_CHECK(info, DEVLINK_ATTR_SELFTESTS))
4952                 return -EINVAL;
4953
4954         attrs = info->attrs[DEVLINK_ATTR_SELFTESTS];
4955
4956         err = nla_parse_nested(tb, DEVLINK_ATTR_SELFTEST_ID_MAX, attrs,
4957                                devlink_selftest_nl_policy, info->extack);
4958         if (err < 0)
4959                 return err;
4960
4961         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
4962         if (!msg)
4963                 return -ENOMEM;
4964
4965         err = -EMSGSIZE;
4966         hdr = genlmsg_put(msg, info->snd_portid, info->snd_seq,
4967                           &devlink_nl_family, 0, DEVLINK_CMD_SELFTESTS_RUN);
4968         if (!hdr)
4969                 goto free_msg;
4970
4971         if (devlink_nl_put_handle(msg, devlink))
4972                 goto genlmsg_cancel;
4973
4974         selftests = nla_nest_start(msg, DEVLINK_ATTR_SELFTESTS);
4975         if (!selftests)
4976                 goto genlmsg_cancel;
4977
4978         for (i = DEVLINK_ATTR_SELFTEST_ID_UNSPEC + 1;
4979              i <= DEVLINK_ATTR_SELFTEST_ID_MAX; i++) {
4980                 enum devlink_selftest_status test_status;
4981
4982                 if (nla_get_flag(tb[i])) {
4983                         if (!devlink->ops->selftest_check(devlink, i,
4984                                                           info->extack)) {
4985                                 if (devlink_selftest_result_put(msg, i,
4986                                                                 DEVLINK_SELFTEST_STATUS_SKIP))
4987                                         goto selftests_nest_cancel;
4988                                 continue;
4989                         }
4990
4991                         test_status = devlink->ops->selftest_run(devlink, i,
4992                                                                  info->extack);
4993                         if (devlink_selftest_result_put(msg, i, test_status))
4994                                 goto selftests_nest_cancel;
4995                 }
4996         }
4997
4998         nla_nest_end(msg, selftests);
4999         genlmsg_end(msg, hdr);
5000         return genlmsg_reply(msg, info);
5001
5002 selftests_nest_cancel:
5003         nla_nest_cancel(msg, selftests);
5004 genlmsg_cancel:
5005         genlmsg_cancel(msg, hdr);
5006 free_msg:
5007         nlmsg_free(msg);
5008         return err;
5009 }
5010
5011 static const struct devlink_param devlink_param_generic[] = {
5012         {
5013                 .id = DEVLINK_PARAM_GENERIC_ID_INT_ERR_RESET,
5014                 .name = DEVLINK_PARAM_GENERIC_INT_ERR_RESET_NAME,
5015                 .type = DEVLINK_PARAM_GENERIC_INT_ERR_RESET_TYPE,
5016         },
5017         {
5018                 .id = DEVLINK_PARAM_GENERIC_ID_MAX_MACS,
5019                 .name = DEVLINK_PARAM_GENERIC_MAX_MACS_NAME,
5020                 .type = DEVLINK_PARAM_GENERIC_MAX_MACS_TYPE,
5021         },
5022         {
5023                 .id = DEVLINK_PARAM_GENERIC_ID_ENABLE_SRIOV,
5024                 .name = DEVLINK_PARAM_GENERIC_ENABLE_SRIOV_NAME,
5025                 .type = DEVLINK_PARAM_GENERIC_ENABLE_SRIOV_TYPE,
5026         },
5027         {
5028                 .id = DEVLINK_PARAM_GENERIC_ID_REGION_SNAPSHOT,
5029                 .name = DEVLINK_PARAM_GENERIC_REGION_SNAPSHOT_NAME,
5030                 .type = DEVLINK_PARAM_GENERIC_REGION_SNAPSHOT_TYPE,
5031         },
5032         {
5033                 .id = DEVLINK_PARAM_GENERIC_ID_IGNORE_ARI,
5034                 .name = DEVLINK_PARAM_GENERIC_IGNORE_ARI_NAME,
5035                 .type = DEVLINK_PARAM_GENERIC_IGNORE_ARI_TYPE,
5036         },
5037         {
5038                 .id = DEVLINK_PARAM_GENERIC_ID_MSIX_VEC_PER_PF_MAX,
5039                 .name = DEVLINK_PARAM_GENERIC_MSIX_VEC_PER_PF_MAX_NAME,
5040                 .type = DEVLINK_PARAM_GENERIC_MSIX_VEC_PER_PF_MAX_TYPE,
5041         },
5042         {
5043                 .id = DEVLINK_PARAM_GENERIC_ID_MSIX_VEC_PER_PF_MIN,
5044                 .name = DEVLINK_PARAM_GENERIC_MSIX_VEC_PER_PF_MIN_NAME,
5045                 .type = DEVLINK_PARAM_GENERIC_MSIX_VEC_PER_PF_MIN_TYPE,
5046         },
5047         {
5048                 .id = DEVLINK_PARAM_GENERIC_ID_FW_LOAD_POLICY,
5049                 .name = DEVLINK_PARAM_GENERIC_FW_LOAD_POLICY_NAME,
5050                 .type = DEVLINK_PARAM_GENERIC_FW_LOAD_POLICY_TYPE,
5051         },
5052         {
5053                 .id = DEVLINK_PARAM_GENERIC_ID_RESET_DEV_ON_DRV_PROBE,
5054                 .name = DEVLINK_PARAM_GENERIC_RESET_DEV_ON_DRV_PROBE_NAME,
5055                 .type = DEVLINK_PARAM_GENERIC_RESET_DEV_ON_DRV_PROBE_TYPE,
5056         },
5057         {
5058                 .id = DEVLINK_PARAM_GENERIC_ID_ENABLE_ROCE,
5059                 .name = DEVLINK_PARAM_GENERIC_ENABLE_ROCE_NAME,
5060                 .type = DEVLINK_PARAM_GENERIC_ENABLE_ROCE_TYPE,
5061         },
5062         {
5063                 .id = DEVLINK_PARAM_GENERIC_ID_ENABLE_REMOTE_DEV_RESET,
5064                 .name = DEVLINK_PARAM_GENERIC_ENABLE_REMOTE_DEV_RESET_NAME,
5065                 .type = DEVLINK_PARAM_GENERIC_ENABLE_REMOTE_DEV_RESET_TYPE,
5066         },
5067         {
5068                 .id = DEVLINK_PARAM_GENERIC_ID_ENABLE_ETH,
5069                 .name = DEVLINK_PARAM_GENERIC_ENABLE_ETH_NAME,
5070                 .type = DEVLINK_PARAM_GENERIC_ENABLE_ETH_TYPE,
5071         },
5072         {
5073                 .id = DEVLINK_PARAM_GENERIC_ID_ENABLE_RDMA,
5074                 .name = DEVLINK_PARAM_GENERIC_ENABLE_RDMA_NAME,
5075                 .type = DEVLINK_PARAM_GENERIC_ENABLE_RDMA_TYPE,
5076         },
5077         {
5078                 .id = DEVLINK_PARAM_GENERIC_ID_ENABLE_VNET,
5079                 .name = DEVLINK_PARAM_GENERIC_ENABLE_VNET_NAME,
5080                 .type = DEVLINK_PARAM_GENERIC_ENABLE_VNET_TYPE,
5081         },
5082         {
5083                 .id = DEVLINK_PARAM_GENERIC_ID_ENABLE_IWARP,
5084                 .name = DEVLINK_PARAM_GENERIC_ENABLE_IWARP_NAME,
5085                 .type = DEVLINK_PARAM_GENERIC_ENABLE_IWARP_TYPE,
5086         },
5087         {
5088                 .id = DEVLINK_PARAM_GENERIC_ID_IO_EQ_SIZE,
5089                 .name = DEVLINK_PARAM_GENERIC_IO_EQ_SIZE_NAME,
5090                 .type = DEVLINK_PARAM_GENERIC_IO_EQ_SIZE_TYPE,
5091         },
5092         {
5093                 .id = DEVLINK_PARAM_GENERIC_ID_EVENT_EQ_SIZE,
5094                 .name = DEVLINK_PARAM_GENERIC_EVENT_EQ_SIZE_NAME,
5095                 .type = DEVLINK_PARAM_GENERIC_EVENT_EQ_SIZE_TYPE,
5096         },
5097 };
5098
5099 static int devlink_param_generic_verify(const struct devlink_param *param)
5100 {
5101         /* verify it match generic parameter by id and name */
5102         if (param->id > DEVLINK_PARAM_GENERIC_ID_MAX)
5103                 return -EINVAL;
5104         if (strcmp(param->name, devlink_param_generic[param->id].name))
5105                 return -ENOENT;
5106
5107         WARN_ON(param->type != devlink_param_generic[param->id].type);
5108
5109         return 0;
5110 }
5111
5112 static int devlink_param_driver_verify(const struct devlink_param *param)
5113 {
5114         int i;
5115
5116         if (param->id <= DEVLINK_PARAM_GENERIC_ID_MAX)
5117                 return -EINVAL;
5118         /* verify no such name in generic params */
5119         for (i = 0; i <= DEVLINK_PARAM_GENERIC_ID_MAX; i++)
5120                 if (!strcmp(param->name, devlink_param_generic[i].name))
5121                         return -EEXIST;
5122
5123         return 0;
5124 }
5125
5126 static struct devlink_param_item *
5127 devlink_param_find_by_name(struct list_head *param_list,
5128                            const char *param_name)
5129 {
5130         struct devlink_param_item *param_item;
5131
5132         list_for_each_entry(param_item, param_list, list)
5133                 if (!strcmp(param_item->param->name, param_name))
5134                         return param_item;
5135         return NULL;
5136 }
5137
5138 static struct devlink_param_item *
5139 devlink_param_find_by_id(struct list_head *param_list, u32 param_id)
5140 {
5141         struct devlink_param_item *param_item;
5142
5143         list_for_each_entry(param_item, param_list, list)
5144                 if (param_item->param->id == param_id)
5145                         return param_item;
5146         return NULL;
5147 }
5148
5149 static bool
5150 devlink_param_cmode_is_supported(const struct devlink_param *param,
5151                                  enum devlink_param_cmode cmode)
5152 {
5153         return test_bit(cmode, &param->supported_cmodes);
5154 }
5155
5156 static int devlink_param_get(struct devlink *devlink,
5157                              const struct devlink_param *param,
5158                              struct devlink_param_gset_ctx *ctx)
5159 {
5160         if (!param->get || devlink->reload_failed)
5161                 return -EOPNOTSUPP;
5162         return param->get(devlink, param->id, ctx);
5163 }
5164
5165 static int devlink_param_set(struct devlink *devlink,
5166                              const struct devlink_param *param,
5167                              struct devlink_param_gset_ctx *ctx)
5168 {
5169         if (!param->set || devlink->reload_failed)
5170                 return -EOPNOTSUPP;
5171         return param->set(devlink, param->id, ctx);
5172 }
5173
5174 static int
5175 devlink_param_type_to_nla_type(enum devlink_param_type param_type)
5176 {
5177         switch (param_type) {
5178         case DEVLINK_PARAM_TYPE_U8:
5179                 return NLA_U8;
5180         case DEVLINK_PARAM_TYPE_U16:
5181                 return NLA_U16;
5182         case DEVLINK_PARAM_TYPE_U32:
5183                 return NLA_U32;
5184         case DEVLINK_PARAM_TYPE_STRING:
5185                 return NLA_STRING;
5186         case DEVLINK_PARAM_TYPE_BOOL:
5187                 return NLA_FLAG;
5188         default:
5189                 return -EINVAL;
5190         }
5191 }
5192
5193 static int
5194 devlink_nl_param_value_fill_one(struct sk_buff *msg,
5195                                 enum devlink_param_type type,
5196                                 enum devlink_param_cmode cmode,
5197                                 union devlink_param_value val)
5198 {
5199         struct nlattr *param_value_attr;
5200
5201         param_value_attr = nla_nest_start_noflag(msg,
5202                                                  DEVLINK_ATTR_PARAM_VALUE);
5203         if (!param_value_attr)
5204                 goto nla_put_failure;
5205
5206         if (nla_put_u8(msg, DEVLINK_ATTR_PARAM_VALUE_CMODE, cmode))
5207                 goto value_nest_cancel;
5208
5209         switch (type) {
5210         case DEVLINK_PARAM_TYPE_U8:
5211                 if (nla_put_u8(msg, DEVLINK_ATTR_PARAM_VALUE_DATA, val.vu8))
5212                         goto value_nest_cancel;
5213                 break;
5214         case DEVLINK_PARAM_TYPE_U16:
5215                 if (nla_put_u16(msg, DEVLINK_ATTR_PARAM_VALUE_DATA, val.vu16))
5216                         goto value_nest_cancel;
5217                 break;
5218         case DEVLINK_PARAM_TYPE_U32:
5219                 if (nla_put_u32(msg, DEVLINK_ATTR_PARAM_VALUE_DATA, val.vu32))
5220                         goto value_nest_cancel;
5221                 break;
5222         case DEVLINK_PARAM_TYPE_STRING:
5223                 if (nla_put_string(msg, DEVLINK_ATTR_PARAM_VALUE_DATA,
5224                                    val.vstr))
5225                         goto value_nest_cancel;
5226                 break;
5227         case DEVLINK_PARAM_TYPE_BOOL:
5228                 if (val.vbool &&
5229                     nla_put_flag(msg, DEVLINK_ATTR_PARAM_VALUE_DATA))
5230                         goto value_nest_cancel;
5231                 break;
5232         }
5233
5234         nla_nest_end(msg, param_value_attr);
5235         return 0;
5236
5237 value_nest_cancel:
5238         nla_nest_cancel(msg, param_value_attr);
5239 nla_put_failure:
5240         return -EMSGSIZE;
5241 }
5242
5243 static int devlink_nl_param_fill(struct sk_buff *msg, struct devlink *devlink,
5244                                  unsigned int port_index,
5245                                  struct devlink_param_item *param_item,
5246                                  enum devlink_command cmd,
5247                                  u32 portid, u32 seq, int flags)
5248 {
5249         union devlink_param_value param_value[DEVLINK_PARAM_CMODE_MAX + 1];
5250         bool param_value_set[DEVLINK_PARAM_CMODE_MAX + 1] = {};
5251         const struct devlink_param *param = param_item->param;
5252         struct devlink_param_gset_ctx ctx;
5253         struct nlattr *param_values_list;
5254         struct nlattr *param_attr;
5255         int nla_type;
5256         void *hdr;
5257         int err;
5258         int i;
5259
5260         /* Get value from driver part to driverinit configuration mode */
5261         for (i = 0; i <= DEVLINK_PARAM_CMODE_MAX; i++) {
5262                 if (!devlink_param_cmode_is_supported(param, i))
5263                         continue;
5264                 if (i == DEVLINK_PARAM_CMODE_DRIVERINIT) {
5265                         if (!param_item->driverinit_value_valid)
5266                                 return -EOPNOTSUPP;
5267                         param_value[i] = param_item->driverinit_value;
5268                 } else {
5269                         ctx.cmode = i;
5270                         err = devlink_param_get(devlink, param, &ctx);
5271                         if (err)
5272                                 return err;
5273                         param_value[i] = ctx.val;
5274                 }
5275                 param_value_set[i] = true;
5276         }
5277
5278         hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
5279         if (!hdr)
5280                 return -EMSGSIZE;
5281
5282         if (devlink_nl_put_handle(msg, devlink))
5283                 goto genlmsg_cancel;
5284
5285         if (cmd == DEVLINK_CMD_PORT_PARAM_GET ||
5286             cmd == DEVLINK_CMD_PORT_PARAM_NEW ||
5287             cmd == DEVLINK_CMD_PORT_PARAM_DEL)
5288                 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, port_index))
5289                         goto genlmsg_cancel;
5290
5291         param_attr = nla_nest_start_noflag(msg, DEVLINK_ATTR_PARAM);
5292         if (!param_attr)
5293                 goto genlmsg_cancel;
5294         if (nla_put_string(msg, DEVLINK_ATTR_PARAM_NAME, param->name))
5295                 goto param_nest_cancel;
5296         if (param->generic && nla_put_flag(msg, DEVLINK_ATTR_PARAM_GENERIC))
5297                 goto param_nest_cancel;
5298
5299         nla_type = devlink_param_type_to_nla_type(param->type);
5300         if (nla_type < 0)
5301                 goto param_nest_cancel;
5302         if (nla_put_u8(msg, DEVLINK_ATTR_PARAM_TYPE, nla_type))
5303                 goto param_nest_cancel;
5304
5305         param_values_list = nla_nest_start_noflag(msg,
5306                                                   DEVLINK_ATTR_PARAM_VALUES_LIST);
5307         if (!param_values_list)
5308                 goto param_nest_cancel;
5309
5310         for (i = 0; i <= DEVLINK_PARAM_CMODE_MAX; i++) {
5311                 if (!param_value_set[i])
5312                         continue;
5313                 err = devlink_nl_param_value_fill_one(msg, param->type,
5314                                                       i, param_value[i]);
5315                 if (err)
5316                         goto values_list_nest_cancel;
5317         }
5318
5319         nla_nest_end(msg, param_values_list);
5320         nla_nest_end(msg, param_attr);
5321         genlmsg_end(msg, hdr);
5322         return 0;
5323
5324 values_list_nest_cancel:
5325         nla_nest_end(msg, param_values_list);
5326 param_nest_cancel:
5327         nla_nest_cancel(msg, param_attr);
5328 genlmsg_cancel:
5329         genlmsg_cancel(msg, hdr);
5330         return -EMSGSIZE;
5331 }
5332
5333 static void devlink_param_notify(struct devlink *devlink,
5334                                  unsigned int port_index,
5335                                  struct devlink_param_item *param_item,
5336                                  enum devlink_command cmd)
5337 {
5338         struct sk_buff *msg;
5339         int err;
5340
5341         WARN_ON(cmd != DEVLINK_CMD_PARAM_NEW && cmd != DEVLINK_CMD_PARAM_DEL &&
5342                 cmd != DEVLINK_CMD_PORT_PARAM_NEW &&
5343                 cmd != DEVLINK_CMD_PORT_PARAM_DEL);
5344         ASSERT_DEVLINK_REGISTERED(devlink);
5345
5346         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
5347         if (!msg)
5348                 return;
5349         err = devlink_nl_param_fill(msg, devlink, port_index, param_item, cmd,
5350                                     0, 0, 0);
5351         if (err) {
5352                 nlmsg_free(msg);
5353                 return;
5354         }
5355
5356         genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
5357                                 msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
5358 }
5359
5360 static int devlink_nl_cmd_param_get_dumpit(struct sk_buff *msg,
5361                                            struct netlink_callback *cb)
5362 {
5363         struct devlink_nl_dump_state *state = devlink_dump_state(cb);
5364         struct devlink_param_item *param_item;
5365         struct devlink *devlink;
5366         int start = state->idx;
5367         unsigned long index;
5368         int idx = 0;
5369         int err = 0;
5370
5371         devlinks_xa_for_each_registered_get(sock_net(msg->sk), index, devlink) {
5372                 devl_lock(devlink);
5373                 list_for_each_entry(param_item, &devlink->param_list, list) {
5374                         if (idx < start) {
5375                                 idx++;
5376                                 continue;
5377                         }
5378                         err = devlink_nl_param_fill(msg, devlink, 0, param_item,
5379                                                     DEVLINK_CMD_PARAM_GET,
5380                                                     NETLINK_CB(cb->skb).portid,
5381                                                     cb->nlh->nlmsg_seq,
5382                                                     NLM_F_MULTI);
5383                         if (err == -EOPNOTSUPP) {
5384                                 err = 0;
5385                         } else if (err) {
5386                                 devl_unlock(devlink);
5387                                 devlink_put(devlink);
5388                                 goto out;
5389                         }
5390                         idx++;
5391                 }
5392                 devl_unlock(devlink);
5393                 devlink_put(devlink);
5394         }
5395 out:
5396         if (err != -EMSGSIZE)
5397                 return err;
5398
5399         state->idx = idx;
5400         return msg->len;
5401 }
5402
5403 static int
5404 devlink_param_type_get_from_info(struct genl_info *info,
5405                                  enum devlink_param_type *param_type)
5406 {
5407         if (GENL_REQ_ATTR_CHECK(info, DEVLINK_ATTR_PARAM_TYPE))
5408                 return -EINVAL;
5409
5410         switch (nla_get_u8(info->attrs[DEVLINK_ATTR_PARAM_TYPE])) {
5411         case NLA_U8:
5412                 *param_type = DEVLINK_PARAM_TYPE_U8;
5413                 break;
5414         case NLA_U16:
5415                 *param_type = DEVLINK_PARAM_TYPE_U16;
5416                 break;
5417         case NLA_U32:
5418                 *param_type = DEVLINK_PARAM_TYPE_U32;
5419                 break;
5420         case NLA_STRING:
5421                 *param_type = DEVLINK_PARAM_TYPE_STRING;
5422                 break;
5423         case NLA_FLAG:
5424                 *param_type = DEVLINK_PARAM_TYPE_BOOL;
5425                 break;
5426         default:
5427                 return -EINVAL;
5428         }
5429
5430         return 0;
5431 }
5432
5433 static int
5434 devlink_param_value_get_from_info(const struct devlink_param *param,
5435                                   struct genl_info *info,
5436                                   union devlink_param_value *value)
5437 {
5438         struct nlattr *param_data;
5439         int len;
5440
5441         param_data = info->attrs[DEVLINK_ATTR_PARAM_VALUE_DATA];
5442
5443         if (param->type != DEVLINK_PARAM_TYPE_BOOL && !param_data)
5444                 return -EINVAL;
5445
5446         switch (param->type) {
5447         case DEVLINK_PARAM_TYPE_U8:
5448                 if (nla_len(param_data) != sizeof(u8))
5449                         return -EINVAL;
5450                 value->vu8 = nla_get_u8(param_data);
5451                 break;
5452         case DEVLINK_PARAM_TYPE_U16:
5453                 if (nla_len(param_data) != sizeof(u16))
5454                         return -EINVAL;
5455                 value->vu16 = nla_get_u16(param_data);
5456                 break;
5457         case DEVLINK_PARAM_TYPE_U32:
5458                 if (nla_len(param_data) != sizeof(u32))
5459                         return -EINVAL;
5460                 value->vu32 = nla_get_u32(param_data);
5461                 break;
5462         case DEVLINK_PARAM_TYPE_STRING:
5463                 len = strnlen(nla_data(param_data), nla_len(param_data));
5464                 if (len == nla_len(param_data) ||
5465                     len >= __DEVLINK_PARAM_MAX_STRING_VALUE)
5466                         return -EINVAL;
5467                 strcpy(value->vstr, nla_data(param_data));
5468                 break;
5469         case DEVLINK_PARAM_TYPE_BOOL:
5470                 if (param_data && nla_len(param_data))
5471                         return -EINVAL;
5472                 value->vbool = nla_get_flag(param_data);
5473                 break;
5474         }
5475         return 0;
5476 }
5477
5478 static struct devlink_param_item *
5479 devlink_param_get_from_info(struct list_head *param_list,
5480                             struct genl_info *info)
5481 {
5482         char *param_name;
5483
5484         if (GENL_REQ_ATTR_CHECK(info, DEVLINK_ATTR_PARAM_NAME))
5485                 return NULL;
5486
5487         param_name = nla_data(info->attrs[DEVLINK_ATTR_PARAM_NAME]);
5488         return devlink_param_find_by_name(param_list, param_name);
5489 }
5490
5491 static int devlink_nl_cmd_param_get_doit(struct sk_buff *skb,
5492                                          struct genl_info *info)
5493 {
5494         struct devlink *devlink = info->user_ptr[0];
5495         struct devlink_param_item *param_item;
5496         struct sk_buff *msg;
5497         int err;
5498
5499         param_item = devlink_param_get_from_info(&devlink->param_list, info);
5500         if (!param_item)
5501                 return -EINVAL;
5502
5503         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
5504         if (!msg)
5505                 return -ENOMEM;
5506
5507         err = devlink_nl_param_fill(msg, devlink, 0, param_item,
5508                                     DEVLINK_CMD_PARAM_GET,
5509                                     info->snd_portid, info->snd_seq, 0);
5510         if (err) {
5511                 nlmsg_free(msg);
5512                 return err;
5513         }
5514
5515         return genlmsg_reply(msg, info);
5516 }
5517
5518 static int __devlink_nl_cmd_param_set_doit(struct devlink *devlink,
5519                                            unsigned int port_index,
5520                                            struct list_head *param_list,
5521                                            struct genl_info *info,
5522                                            enum devlink_command cmd)
5523 {
5524         enum devlink_param_type param_type;
5525         struct devlink_param_gset_ctx ctx;
5526         enum devlink_param_cmode cmode;
5527         struct devlink_param_item *param_item;
5528         const struct devlink_param *param;
5529         union devlink_param_value value;
5530         int err = 0;
5531
5532         param_item = devlink_param_get_from_info(param_list, info);
5533         if (!param_item)
5534                 return -EINVAL;
5535         param = param_item->param;
5536         err = devlink_param_type_get_from_info(info, &param_type);
5537         if (err)
5538                 return err;
5539         if (param_type != param->type)
5540                 return -EINVAL;
5541         err = devlink_param_value_get_from_info(param, info, &value);
5542         if (err)
5543                 return err;
5544         if (param->validate) {
5545                 err = param->validate(devlink, param->id, value, info->extack);
5546                 if (err)
5547                         return err;
5548         }
5549
5550         if (GENL_REQ_ATTR_CHECK(info, DEVLINK_ATTR_PARAM_VALUE_CMODE))
5551                 return -EINVAL;
5552         cmode = nla_get_u8(info->attrs[DEVLINK_ATTR_PARAM_VALUE_CMODE]);
5553         if (!devlink_param_cmode_is_supported(param, cmode))
5554                 return -EOPNOTSUPP;
5555
5556         if (cmode == DEVLINK_PARAM_CMODE_DRIVERINIT) {
5557                 if (param->type == DEVLINK_PARAM_TYPE_STRING)
5558                         strcpy(param_item->driverinit_value.vstr, value.vstr);
5559                 else
5560                         param_item->driverinit_value = value;
5561                 param_item->driverinit_value_valid = true;
5562         } else {
5563                 if (!param->set)
5564                         return -EOPNOTSUPP;
5565                 ctx.val = value;
5566                 ctx.cmode = cmode;
5567                 err = devlink_param_set(devlink, param, &ctx);
5568                 if (err)
5569                         return err;
5570         }
5571
5572         devlink_param_notify(devlink, port_index, param_item, cmd);
5573         return 0;
5574 }
5575
5576 static int devlink_nl_cmd_param_set_doit(struct sk_buff *skb,
5577                                          struct genl_info *info)
5578 {
5579         struct devlink *devlink = info->user_ptr[0];
5580
5581         return __devlink_nl_cmd_param_set_doit(devlink, 0, &devlink->param_list,
5582                                                info, DEVLINK_CMD_PARAM_NEW);
5583 }
5584
5585 static int devlink_nl_cmd_port_param_get_dumpit(struct sk_buff *msg,
5586                                                 struct netlink_callback *cb)
5587 {
5588         NL_SET_ERR_MSG_MOD(cb->extack, "Port params are not supported");
5589         return msg->len;
5590 }
5591
5592 static int devlink_nl_cmd_port_param_get_doit(struct sk_buff *skb,
5593                                               struct genl_info *info)
5594 {
5595         NL_SET_ERR_MSG_MOD(info->extack, "Port params are not supported");
5596         return -EINVAL;
5597 }
5598
5599 static int devlink_nl_cmd_port_param_set_doit(struct sk_buff *skb,
5600                                               struct genl_info *info)
5601 {
5602         NL_SET_ERR_MSG_MOD(info->extack, "Port params are not supported");
5603         return -EINVAL;
5604 }
5605
5606 static int devlink_nl_region_snapshot_id_put(struct sk_buff *msg,
5607                                              struct devlink *devlink,
5608                                              struct devlink_snapshot *snapshot)
5609 {
5610         struct nlattr *snap_attr;
5611         int err;
5612
5613         snap_attr = nla_nest_start_noflag(msg, DEVLINK_ATTR_REGION_SNAPSHOT);
5614         if (!snap_attr)
5615                 return -EINVAL;
5616
5617         err = nla_put_u32(msg, DEVLINK_ATTR_REGION_SNAPSHOT_ID, snapshot->id);
5618         if (err)
5619                 goto nla_put_failure;
5620
5621         nla_nest_end(msg, snap_attr);
5622         return 0;
5623
5624 nla_put_failure:
5625         nla_nest_cancel(msg, snap_attr);
5626         return err;
5627 }
5628
5629 static int devlink_nl_region_snapshots_id_put(struct sk_buff *msg,
5630                                               struct devlink *devlink,
5631                                               struct devlink_region *region)
5632 {
5633         struct devlink_snapshot *snapshot;
5634         struct nlattr *snapshots_attr;
5635         int err;
5636
5637         snapshots_attr = nla_nest_start_noflag(msg,
5638                                                DEVLINK_ATTR_REGION_SNAPSHOTS);
5639         if (!snapshots_attr)
5640                 return -EINVAL;
5641
5642         list_for_each_entry(snapshot, &region->snapshot_list, list) {
5643                 err = devlink_nl_region_snapshot_id_put(msg, devlink, snapshot);
5644                 if (err)
5645                         goto nla_put_failure;
5646         }
5647
5648         nla_nest_end(msg, snapshots_attr);
5649         return 0;
5650
5651 nla_put_failure:
5652         nla_nest_cancel(msg, snapshots_attr);
5653         return err;
5654 }
5655
5656 static int devlink_nl_region_fill(struct sk_buff *msg, struct devlink *devlink,
5657                                   enum devlink_command cmd, u32 portid,
5658                                   u32 seq, int flags,
5659                                   struct devlink_region *region)
5660 {
5661         void *hdr;
5662         int err;
5663
5664         hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
5665         if (!hdr)
5666                 return -EMSGSIZE;
5667
5668         err = devlink_nl_put_handle(msg, devlink);
5669         if (err)
5670                 goto nla_put_failure;
5671
5672         if (region->port) {
5673                 err = nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX,
5674                                   region->port->index);
5675                 if (err)
5676                         goto nla_put_failure;
5677         }
5678
5679         err = nla_put_string(msg, DEVLINK_ATTR_REGION_NAME, region->ops->name);
5680         if (err)
5681                 goto nla_put_failure;
5682
5683         err = nla_put_u64_64bit(msg, DEVLINK_ATTR_REGION_SIZE,
5684                                 region->size,
5685                                 DEVLINK_ATTR_PAD);
5686         if (err)
5687                 goto nla_put_failure;
5688
5689         err = nla_put_u32(msg, DEVLINK_ATTR_REGION_MAX_SNAPSHOTS,
5690                           region->max_snapshots);
5691         if (err)
5692                 goto nla_put_failure;
5693
5694         err = devlink_nl_region_snapshots_id_put(msg, devlink, region);
5695         if (err)
5696                 goto nla_put_failure;
5697
5698         genlmsg_end(msg, hdr);
5699         return 0;
5700
5701 nla_put_failure:
5702         genlmsg_cancel(msg, hdr);
5703         return err;
5704 }
5705
5706 static struct sk_buff *
5707 devlink_nl_region_notify_build(struct devlink_region *region,
5708                                struct devlink_snapshot *snapshot,
5709                                enum devlink_command cmd, u32 portid, u32 seq)
5710 {
5711         struct devlink *devlink = region->devlink;
5712         struct sk_buff *msg;
5713         void *hdr;
5714         int err;
5715
5716
5717         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
5718         if (!msg)
5719                 return ERR_PTR(-ENOMEM);
5720
5721         hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, 0, cmd);
5722         if (!hdr) {
5723                 err = -EMSGSIZE;
5724                 goto out_free_msg;
5725         }
5726
5727         err = devlink_nl_put_handle(msg, devlink);
5728         if (err)
5729                 goto out_cancel_msg;
5730
5731         if (region->port) {
5732                 err = nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX,
5733                                   region->port->index);
5734                 if (err)
5735                         goto out_cancel_msg;
5736         }
5737
5738         err = nla_put_string(msg, DEVLINK_ATTR_REGION_NAME,
5739                              region->ops->name);
5740         if (err)
5741                 goto out_cancel_msg;
5742
5743         if (snapshot) {
5744                 err = nla_put_u32(msg, DEVLINK_ATTR_REGION_SNAPSHOT_ID,
5745                                   snapshot->id);
5746                 if (err)
5747                         goto out_cancel_msg;
5748         } else {
5749                 err = nla_put_u64_64bit(msg, DEVLINK_ATTR_REGION_SIZE,
5750                                         region->size, DEVLINK_ATTR_PAD);
5751                 if (err)
5752                         goto out_cancel_msg;
5753         }
5754         genlmsg_end(msg, hdr);
5755
5756         return msg;
5757
5758 out_cancel_msg:
5759         genlmsg_cancel(msg, hdr);
5760 out_free_msg:
5761         nlmsg_free(msg);
5762         return ERR_PTR(err);
5763 }
5764
5765 static void devlink_nl_region_notify(struct devlink_region *region,
5766                                      struct devlink_snapshot *snapshot,
5767                                      enum devlink_command cmd)
5768 {
5769         struct devlink *devlink = region->devlink;
5770         struct sk_buff *msg;
5771
5772         WARN_ON(cmd != DEVLINK_CMD_REGION_NEW && cmd != DEVLINK_CMD_REGION_DEL);
5773         if (!xa_get_mark(&devlinks, devlink->index, DEVLINK_REGISTERED))
5774                 return;
5775
5776         msg = devlink_nl_region_notify_build(region, snapshot, cmd, 0, 0);
5777         if (IS_ERR(msg))
5778                 return;
5779
5780         genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink), msg,
5781                                 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
5782 }
5783
5784 /**
5785  * __devlink_snapshot_id_increment - Increment number of snapshots using an id
5786  *      @devlink: devlink instance
5787  *      @id: the snapshot id
5788  *
5789  *      Track when a new snapshot begins using an id. Load the count for the
5790  *      given id from the snapshot xarray, increment it, and store it back.
5791  *
5792  *      Called when a new snapshot is created with the given id.
5793  *
5794  *      The id *must* have been previously allocated by
5795  *      devlink_region_snapshot_id_get().
5796  *
5797  *      Returns 0 on success, or an error on failure.
5798  */
5799 static int __devlink_snapshot_id_increment(struct devlink *devlink, u32 id)
5800 {
5801         unsigned long count;
5802         void *p;
5803         int err;
5804
5805         xa_lock(&devlink->snapshot_ids);
5806         p = xa_load(&devlink->snapshot_ids, id);
5807         if (WARN_ON(!p)) {
5808                 err = -EINVAL;
5809                 goto unlock;
5810         }
5811
5812         if (WARN_ON(!xa_is_value(p))) {
5813                 err = -EINVAL;
5814                 goto unlock;
5815         }
5816
5817         count = xa_to_value(p);
5818         count++;
5819
5820         err = xa_err(__xa_store(&devlink->snapshot_ids, id, xa_mk_value(count),
5821                                 GFP_ATOMIC));
5822 unlock:
5823         xa_unlock(&devlink->snapshot_ids);
5824         return err;
5825 }
5826
5827 /**
5828  * __devlink_snapshot_id_decrement - Decrease number of snapshots using an id
5829  *      @devlink: devlink instance
5830  *      @id: the snapshot id
5831  *
5832  *      Track when a snapshot is deleted and stops using an id. Load the count
5833  *      for the given id from the snapshot xarray, decrement it, and store it
5834  *      back.
5835  *
5836  *      If the count reaches zero, erase this id from the xarray, freeing it
5837  *      up for future re-use by devlink_region_snapshot_id_get().
5838  *
5839  *      Called when a snapshot using the given id is deleted, and when the
5840  *      initial allocator of the id is finished using it.
5841  */
5842 static void __devlink_snapshot_id_decrement(struct devlink *devlink, u32 id)
5843 {
5844         unsigned long count;
5845         void *p;
5846
5847         xa_lock(&devlink->snapshot_ids);
5848         p = xa_load(&devlink->snapshot_ids, id);
5849         if (WARN_ON(!p))
5850                 goto unlock;
5851
5852         if (WARN_ON(!xa_is_value(p)))
5853                 goto unlock;
5854
5855         count = xa_to_value(p);
5856
5857         if (count > 1) {
5858                 count--;
5859                 __xa_store(&devlink->snapshot_ids, id, xa_mk_value(count),
5860                            GFP_ATOMIC);
5861         } else {
5862                 /* If this was the last user, we can erase this id */
5863                 __xa_erase(&devlink->snapshot_ids, id);
5864         }
5865 unlock:
5866         xa_unlock(&devlink->snapshot_ids);
5867 }
5868
5869 /**
5870  *      __devlink_snapshot_id_insert - Insert a specific snapshot ID
5871  *      @devlink: devlink instance
5872  *      @id: the snapshot id
5873  *
5874  *      Mark the given snapshot id as used by inserting a zero value into the
5875  *      snapshot xarray.
5876  *
5877  *      This must be called while holding the devlink instance lock. Unlike
5878  *      devlink_snapshot_id_get, the initial reference count is zero, not one.
5879  *      It is expected that the id will immediately be used before
5880  *      releasing the devlink instance lock.
5881  *
5882  *      Returns zero on success, or an error code if the snapshot id could not
5883  *      be inserted.
5884  */
5885 static int __devlink_snapshot_id_insert(struct devlink *devlink, u32 id)
5886 {
5887         int err;
5888
5889         xa_lock(&devlink->snapshot_ids);
5890         if (xa_load(&devlink->snapshot_ids, id)) {
5891                 xa_unlock(&devlink->snapshot_ids);
5892                 return -EEXIST;
5893         }
5894         err = xa_err(__xa_store(&devlink->snapshot_ids, id, xa_mk_value(0),
5895                                 GFP_ATOMIC));
5896         xa_unlock(&devlink->snapshot_ids);
5897         return err;
5898 }
5899
5900 /**
5901  *      __devlink_region_snapshot_id_get - get snapshot ID
5902  *      @devlink: devlink instance
5903  *      @id: storage to return snapshot id
5904  *
5905  *      Allocates a new snapshot id. Returns zero on success, or a negative
5906  *      error on failure. Must be called while holding the devlink instance
5907  *      lock.
5908  *
5909  *      Snapshot IDs are tracked using an xarray which stores the number of
5910  *      users of the snapshot id.
5911  *
5912  *      Note that the caller of this function counts as a 'user', in order to
5913  *      avoid race conditions. The caller must release its hold on the
5914  *      snapshot by using devlink_region_snapshot_id_put.
5915  */
5916 static int __devlink_region_snapshot_id_get(struct devlink *devlink, u32 *id)
5917 {
5918         return xa_alloc(&devlink->snapshot_ids, id, xa_mk_value(1),
5919                         xa_limit_32b, GFP_KERNEL);
5920 }
5921
5922 /**
5923  *      __devlink_region_snapshot_create - create a new snapshot
5924  *      This will add a new snapshot of a region. The snapshot
5925  *      will be stored on the region struct and can be accessed
5926  *      from devlink. This is useful for future analyses of snapshots.
5927  *      Multiple snapshots can be created on a region.
5928  *      The @snapshot_id should be obtained using the getter function.
5929  *
5930  *      Must be called only while holding the region snapshot lock.
5931  *
5932  *      @region: devlink region of the snapshot
5933  *      @data: snapshot data
5934  *      @snapshot_id: snapshot id to be created
5935  */
5936 static int
5937 __devlink_region_snapshot_create(struct devlink_region *region,
5938                                  u8 *data, u32 snapshot_id)
5939 {
5940         struct devlink *devlink = region->devlink;
5941         struct devlink_snapshot *snapshot;
5942         int err;
5943
5944         lockdep_assert_held(&region->snapshot_lock);
5945
5946         /* check if region can hold one more snapshot */
5947         if (region->cur_snapshots == region->max_snapshots)
5948                 return -ENOSPC;
5949
5950         if (devlink_region_snapshot_get_by_id(region, snapshot_id))
5951                 return -EEXIST;
5952
5953         snapshot = kzalloc(sizeof(*snapshot), GFP_KERNEL);
5954         if (!snapshot)
5955                 return -ENOMEM;
5956
5957         err = __devlink_snapshot_id_increment(devlink, snapshot_id);
5958         if (err)
5959                 goto err_snapshot_id_increment;
5960
5961         snapshot->id = snapshot_id;
5962         snapshot->region = region;
5963         snapshot->data = data;
5964
5965         list_add_tail(&snapshot->list, &region->snapshot_list);
5966
5967         region->cur_snapshots++;
5968
5969         devlink_nl_region_notify(region, snapshot, DEVLINK_CMD_REGION_NEW);
5970         return 0;
5971
5972 err_snapshot_id_increment:
5973         kfree(snapshot);
5974         return err;
5975 }
5976
5977 static void devlink_region_snapshot_del(struct devlink_region *region,
5978                                         struct devlink_snapshot *snapshot)
5979 {
5980         struct devlink *devlink = region->devlink;
5981
5982         lockdep_assert_held(&region->snapshot_lock);
5983
5984         devlink_nl_region_notify(region, snapshot, DEVLINK_CMD_REGION_DEL);
5985         region->cur_snapshots--;
5986         list_del(&snapshot->list);
5987         region->ops->destructor(snapshot->data);
5988         __devlink_snapshot_id_decrement(devlink, snapshot->id);
5989         kfree(snapshot);
5990 }
5991
5992 static int devlink_nl_cmd_region_get_doit(struct sk_buff *skb,
5993                                           struct genl_info *info)
5994 {
5995         struct devlink *devlink = info->user_ptr[0];
5996         struct devlink_port *port = NULL;
5997         struct devlink_region *region;
5998         const char *region_name;
5999         struct sk_buff *msg;
6000         unsigned int index;
6001         int err;
6002
6003         if (GENL_REQ_ATTR_CHECK(info, DEVLINK_ATTR_REGION_NAME))
6004                 return -EINVAL;
6005
6006         if (info->attrs[DEVLINK_ATTR_PORT_INDEX]) {
6007                 index = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_INDEX]);
6008
6009                 port = devlink_port_get_by_index(devlink, index);
6010                 if (!port)
6011                         return -ENODEV;
6012         }
6013
6014         region_name = nla_data(info->attrs[DEVLINK_ATTR_REGION_NAME]);
6015         if (port)
6016                 region = devlink_port_region_get_by_name(port, region_name);
6017         else
6018                 region = devlink_region_get_by_name(devlink, region_name);
6019
6020         if (!region)
6021                 return -EINVAL;
6022
6023         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6024         if (!msg)
6025                 return -ENOMEM;
6026
6027         err = devlink_nl_region_fill(msg, devlink, DEVLINK_CMD_REGION_GET,
6028                                      info->snd_portid, info->snd_seq, 0,
6029                                      region);
6030         if (err) {
6031                 nlmsg_free(msg);
6032                 return err;
6033         }
6034
6035         return genlmsg_reply(msg, info);
6036 }
6037
6038 static int devlink_nl_cmd_region_get_port_dumpit(struct sk_buff *msg,
6039                                                  struct netlink_callback *cb,
6040                                                  struct devlink_port *port,
6041                                                  int *idx,
6042                                                  int start)
6043 {
6044         struct devlink_region *region;
6045         int err = 0;
6046
6047         list_for_each_entry(region, &port->region_list, list) {
6048                 if (*idx < start) {
6049                         (*idx)++;
6050                         continue;
6051                 }
6052                 err = devlink_nl_region_fill(msg, port->devlink,
6053                                              DEVLINK_CMD_REGION_GET,
6054                                              NETLINK_CB(cb->skb).portid,
6055                                              cb->nlh->nlmsg_seq,
6056                                              NLM_F_MULTI, region);
6057                 if (err)
6058                         goto out;
6059                 (*idx)++;
6060         }
6061
6062 out:
6063         return err;
6064 }
6065
6066 static int devlink_nl_cmd_region_get_devlink_dumpit(struct sk_buff *msg,
6067                                                     struct netlink_callback *cb,
6068                                                     struct devlink *devlink,
6069                                                     int *idx,
6070                                                     int start)
6071 {
6072         struct devlink_region *region;
6073         struct devlink_port *port;
6074         unsigned long port_index;
6075         int err = 0;
6076
6077         devl_lock(devlink);
6078         list_for_each_entry(region, &devlink->region_list, list) {
6079                 if (*idx < start) {
6080                         (*idx)++;
6081                         continue;
6082                 }
6083                 err = devlink_nl_region_fill(msg, devlink,
6084                                              DEVLINK_CMD_REGION_GET,
6085                                              NETLINK_CB(cb->skb).portid,
6086                                              cb->nlh->nlmsg_seq,
6087                                              NLM_F_MULTI, region);
6088                 if (err)
6089                         goto out;
6090                 (*idx)++;
6091         }
6092
6093         xa_for_each(&devlink->ports, port_index, port) {
6094                 err = devlink_nl_cmd_region_get_port_dumpit(msg, cb, port, idx,
6095                                                             start);
6096                 if (err)
6097                         goto out;
6098         }
6099
6100 out:
6101         devl_unlock(devlink);
6102         return err;
6103 }
6104
6105 static int devlink_nl_cmd_region_get_dumpit(struct sk_buff *msg,
6106                                             struct netlink_callback *cb)
6107 {
6108         struct devlink_nl_dump_state *state = devlink_dump_state(cb);
6109         struct devlink *devlink;
6110         int start = state->idx;
6111         unsigned long index;
6112         int idx = 0;
6113         int err = 0;
6114
6115         devlinks_xa_for_each_registered_get(sock_net(msg->sk), index, devlink) {
6116                 err = devlink_nl_cmd_region_get_devlink_dumpit(msg, cb, devlink,
6117                                                                &idx, start);
6118                 devlink_put(devlink);
6119                 if (err)
6120                         goto out;
6121         }
6122 out:
6123         state->idx = idx;
6124         return msg->len;
6125 }
6126
6127 static int devlink_nl_cmd_region_del(struct sk_buff *skb,
6128                                      struct genl_info *info)
6129 {
6130         struct devlink *devlink = info->user_ptr[0];
6131         struct devlink_snapshot *snapshot;
6132         struct devlink_port *port = NULL;
6133         struct devlink_region *region;
6134         const char *region_name;
6135         unsigned int index;
6136         u32 snapshot_id;
6137
6138         if (GENL_REQ_ATTR_CHECK(info, DEVLINK_ATTR_REGION_NAME) ||
6139             GENL_REQ_ATTR_CHECK(info, DEVLINK_ATTR_REGION_SNAPSHOT_ID))
6140                 return -EINVAL;
6141
6142         region_name = nla_data(info->attrs[DEVLINK_ATTR_REGION_NAME]);
6143         snapshot_id = nla_get_u32(info->attrs[DEVLINK_ATTR_REGION_SNAPSHOT_ID]);
6144
6145         if (info->attrs[DEVLINK_ATTR_PORT_INDEX]) {
6146                 index = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_INDEX]);
6147
6148                 port = devlink_port_get_by_index(devlink, index);
6149                 if (!port)
6150                         return -ENODEV;
6151         }
6152
6153         if (port)
6154                 region = devlink_port_region_get_by_name(port, region_name);
6155         else
6156                 region = devlink_region_get_by_name(devlink, region_name);
6157
6158         if (!region)
6159                 return -EINVAL;
6160
6161         mutex_lock(&region->snapshot_lock);
6162         snapshot = devlink_region_snapshot_get_by_id(region, snapshot_id);
6163         if (!snapshot) {
6164                 mutex_unlock(&region->snapshot_lock);
6165                 return -EINVAL;
6166         }
6167
6168         devlink_region_snapshot_del(region, snapshot);
6169         mutex_unlock(&region->snapshot_lock);
6170         return 0;
6171 }
6172
6173 static int
6174 devlink_nl_cmd_region_new(struct sk_buff *skb, struct genl_info *info)
6175 {
6176         struct devlink *devlink = info->user_ptr[0];
6177         struct devlink_snapshot *snapshot;
6178         struct devlink_port *port = NULL;
6179         struct nlattr *snapshot_id_attr;
6180         struct devlink_region *region;
6181         const char *region_name;
6182         unsigned int index;
6183         u32 snapshot_id;
6184         u8 *data;
6185         int err;
6186
6187         if (GENL_REQ_ATTR_CHECK(info, DEVLINK_ATTR_REGION_NAME)) {
6188                 NL_SET_ERR_MSG_MOD(info->extack, "No region name provided");
6189                 return -EINVAL;
6190         }
6191
6192         region_name = nla_data(info->attrs[DEVLINK_ATTR_REGION_NAME]);
6193
6194         if (info->attrs[DEVLINK_ATTR_PORT_INDEX]) {
6195                 index = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_INDEX]);
6196
6197                 port = devlink_port_get_by_index(devlink, index);
6198                 if (!port)
6199                         return -ENODEV;
6200         }
6201
6202         if (port)
6203                 region = devlink_port_region_get_by_name(port, region_name);
6204         else
6205                 region = devlink_region_get_by_name(devlink, region_name);
6206
6207         if (!region) {
6208                 NL_SET_ERR_MSG_MOD(info->extack, "The requested region does not exist");
6209                 return -EINVAL;
6210         }
6211
6212         if (!region->ops->snapshot) {
6213                 NL_SET_ERR_MSG_MOD(info->extack, "The requested region does not support taking an immediate snapshot");
6214                 return -EOPNOTSUPP;
6215         }
6216
6217         mutex_lock(&region->snapshot_lock);
6218
6219         if (region->cur_snapshots == region->max_snapshots) {
6220                 NL_SET_ERR_MSG_MOD(info->extack, "The region has reached the maximum number of stored snapshots");
6221                 err = -ENOSPC;
6222                 goto unlock;
6223         }
6224
6225         snapshot_id_attr = info->attrs[DEVLINK_ATTR_REGION_SNAPSHOT_ID];
6226         if (snapshot_id_attr) {
6227                 snapshot_id = nla_get_u32(snapshot_id_attr);
6228
6229                 if (devlink_region_snapshot_get_by_id(region, snapshot_id)) {
6230                         NL_SET_ERR_MSG_MOD(info->extack, "The requested snapshot id is already in use");
6231                         err = -EEXIST;
6232                         goto unlock;
6233                 }
6234
6235                 err = __devlink_snapshot_id_insert(devlink, snapshot_id);
6236                 if (err)
6237                         goto unlock;
6238         } else {
6239                 err = __devlink_region_snapshot_id_get(devlink, &snapshot_id);
6240                 if (err) {
6241                         NL_SET_ERR_MSG_MOD(info->extack, "Failed to allocate a new snapshot id");
6242                         goto unlock;
6243                 }
6244         }
6245
6246         if (port)
6247                 err = region->port_ops->snapshot(port, region->port_ops,
6248                                                  info->extack, &data);
6249         else
6250                 err = region->ops->snapshot(devlink, region->ops,
6251                                             info->extack, &data);
6252         if (err)
6253                 goto err_snapshot_capture;
6254
6255         err = __devlink_region_snapshot_create(region, data, snapshot_id);
6256         if (err)
6257                 goto err_snapshot_create;
6258
6259         if (!snapshot_id_attr) {
6260                 struct sk_buff *msg;
6261
6262                 snapshot = devlink_region_snapshot_get_by_id(region,
6263                                                              snapshot_id);
6264                 if (WARN_ON(!snapshot)) {
6265                         err = -EINVAL;
6266                         goto unlock;
6267                 }
6268
6269                 msg = devlink_nl_region_notify_build(region, snapshot,
6270                                                      DEVLINK_CMD_REGION_NEW,
6271                                                      info->snd_portid,
6272                                                      info->snd_seq);
6273                 err = PTR_ERR_OR_ZERO(msg);
6274                 if (err)
6275                         goto err_notify;
6276
6277                 err = genlmsg_reply(msg, info);
6278                 if (err)
6279                         goto err_notify;
6280         }
6281
6282         mutex_unlock(&region->snapshot_lock);
6283         return 0;
6284
6285 err_snapshot_create:
6286         region->ops->destructor(data);
6287 err_snapshot_capture:
6288         __devlink_snapshot_id_decrement(devlink, snapshot_id);
6289         mutex_unlock(&region->snapshot_lock);
6290         return err;
6291
6292 err_notify:
6293         devlink_region_snapshot_del(region, snapshot);
6294 unlock:
6295         mutex_unlock(&region->snapshot_lock);
6296         return err;
6297 }
6298
6299 static int devlink_nl_cmd_region_read_chunk_fill(struct sk_buff *msg,
6300                                                  u8 *chunk, u32 chunk_size,
6301                                                  u64 addr)
6302 {
6303         struct nlattr *chunk_attr;
6304         int err;
6305
6306         chunk_attr = nla_nest_start_noflag(msg, DEVLINK_ATTR_REGION_CHUNK);
6307         if (!chunk_attr)
6308                 return -EINVAL;
6309
6310         err = nla_put(msg, DEVLINK_ATTR_REGION_CHUNK_DATA, chunk_size, chunk);
6311         if (err)
6312                 goto nla_put_failure;
6313
6314         err = nla_put_u64_64bit(msg, DEVLINK_ATTR_REGION_CHUNK_ADDR, addr,
6315                                 DEVLINK_ATTR_PAD);
6316         if (err)
6317                 goto nla_put_failure;
6318
6319         nla_nest_end(msg, chunk_attr);
6320         return 0;
6321
6322 nla_put_failure:
6323         nla_nest_cancel(msg, chunk_attr);
6324         return err;
6325 }
6326
6327 #define DEVLINK_REGION_READ_CHUNK_SIZE 256
6328
6329 typedef int devlink_chunk_fill_t(void *cb_priv, u8 *chunk, u32 chunk_size,
6330                                  u64 curr_offset,
6331                                  struct netlink_ext_ack *extack);
6332
6333 static int
6334 devlink_nl_region_read_fill(struct sk_buff *skb, devlink_chunk_fill_t *cb,
6335                             void *cb_priv, u64 start_offset, u64 end_offset,
6336                             u64 *new_offset, struct netlink_ext_ack *extack)
6337 {
6338         u64 curr_offset = start_offset;
6339         int err = 0;
6340         u8 *data;
6341
6342         /* Allocate and re-use a single buffer */
6343         data = kmalloc(DEVLINK_REGION_READ_CHUNK_SIZE, GFP_KERNEL);
6344         if (!data)
6345                 return -ENOMEM;
6346
6347         *new_offset = start_offset;
6348
6349         while (curr_offset < end_offset) {
6350                 u32 data_size;
6351
6352                 data_size = min_t(u32, end_offset - curr_offset,
6353                                   DEVLINK_REGION_READ_CHUNK_SIZE);
6354
6355                 err = cb(cb_priv, data, data_size, curr_offset, extack);
6356                 if (err)
6357                         break;
6358
6359                 err = devlink_nl_cmd_region_read_chunk_fill(skb, data, data_size, curr_offset);
6360                 if (err)
6361                         break;
6362
6363                 curr_offset += data_size;
6364         }
6365         *new_offset = curr_offset;
6366
6367         kfree(data);
6368
6369         return err;
6370 }
6371
6372 static int
6373 devlink_region_snapshot_fill(void *cb_priv, u8 *chunk, u32 chunk_size,
6374                              u64 curr_offset,
6375                              struct netlink_ext_ack __always_unused *extack)
6376 {
6377         struct devlink_snapshot *snapshot = cb_priv;
6378
6379         memcpy(chunk, &snapshot->data[curr_offset], chunk_size);
6380
6381         return 0;
6382 }
6383
6384 static int
6385 devlink_region_port_direct_fill(void *cb_priv, u8 *chunk, u32 chunk_size,
6386                                 u64 curr_offset, struct netlink_ext_ack *extack)
6387 {
6388         struct devlink_region *region = cb_priv;
6389
6390         return region->port_ops->read(region->port, region->port_ops, extack,
6391                                       curr_offset, chunk_size, chunk);
6392 }
6393
6394 static int
6395 devlink_region_direct_fill(void *cb_priv, u8 *chunk, u32 chunk_size,
6396                            u64 curr_offset, struct netlink_ext_ack *extack)
6397 {
6398         struct devlink_region *region = cb_priv;
6399
6400         return region->ops->read(region->devlink, region->ops, extack,
6401                                  curr_offset, chunk_size, chunk);
6402 }
6403
6404 static int devlink_nl_cmd_region_read_dumpit(struct sk_buff *skb,
6405                                              struct netlink_callback *cb)
6406 {
6407         const struct genl_dumpit_info *info = genl_dumpit_info(cb);
6408         struct devlink_nl_dump_state *state = devlink_dump_state(cb);
6409         struct nlattr *chunks_attr, *region_attr, *snapshot_attr;
6410         u64 ret_offset, start_offset, end_offset = U64_MAX;
6411         struct nlattr **attrs = info->attrs;
6412         struct devlink_port *port = NULL;
6413         devlink_chunk_fill_t *region_cb;
6414         struct devlink_region *region;
6415         const char *region_name;
6416         struct devlink *devlink;
6417         unsigned int index;
6418         void *region_cb_priv;
6419         void *hdr;
6420         int err;
6421
6422         start_offset = state->start_offset;
6423
6424         devlink = devlink_get_from_attrs(sock_net(cb->skb->sk), attrs);
6425         if (IS_ERR(devlink))
6426                 return PTR_ERR(devlink);
6427
6428         devl_lock(devlink);
6429
6430         if (!attrs[DEVLINK_ATTR_REGION_NAME]) {
6431                 NL_SET_ERR_MSG(cb->extack, "No region name provided");
6432                 err = -EINVAL;
6433                 goto out_unlock;
6434         }
6435
6436         if (info->attrs[DEVLINK_ATTR_PORT_INDEX]) {
6437                 index = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_INDEX]);
6438
6439                 port = devlink_port_get_by_index(devlink, index);
6440                 if (!port) {
6441                         err = -ENODEV;
6442                         goto out_unlock;
6443                 }
6444         }
6445
6446         region_attr = attrs[DEVLINK_ATTR_REGION_NAME];
6447         region_name = nla_data(region_attr);
6448
6449         if (port)
6450                 region = devlink_port_region_get_by_name(port, region_name);
6451         else
6452                 region = devlink_region_get_by_name(devlink, region_name);
6453
6454         if (!region) {
6455                 NL_SET_ERR_MSG_ATTR(cb->extack, region_attr, "Requested region does not exist");
6456                 err = -EINVAL;
6457                 goto out_unlock;
6458         }
6459
6460         snapshot_attr = attrs[DEVLINK_ATTR_REGION_SNAPSHOT_ID];
6461         if (!snapshot_attr) {
6462                 if (!nla_get_flag(attrs[DEVLINK_ATTR_REGION_DIRECT])) {
6463                         NL_SET_ERR_MSG(cb->extack, "No snapshot id provided");
6464                         err = -EINVAL;
6465                         goto out_unlock;
6466                 }
6467
6468                 if (!region->ops->read) {
6469                         NL_SET_ERR_MSG(cb->extack, "Requested region does not support direct read");
6470                         err = -EOPNOTSUPP;
6471                         goto out_unlock;
6472                 }
6473
6474                 if (port)
6475                         region_cb = &devlink_region_port_direct_fill;
6476                 else
6477                         region_cb = &devlink_region_direct_fill;
6478                 region_cb_priv = region;
6479         } else {
6480                 struct devlink_snapshot *snapshot;
6481                 u32 snapshot_id;
6482
6483                 if (nla_get_flag(attrs[DEVLINK_ATTR_REGION_DIRECT])) {
6484                         NL_SET_ERR_MSG_ATTR(cb->extack, snapshot_attr, "Direct region read does not use snapshot");
6485                         err = -EINVAL;
6486                         goto out_unlock;
6487                 }
6488
6489                 snapshot_id = nla_get_u32(snapshot_attr);
6490                 snapshot = devlink_region_snapshot_get_by_id(region, snapshot_id);
6491                 if (!snapshot) {
6492                         NL_SET_ERR_MSG_ATTR(cb->extack, snapshot_attr, "Requested snapshot does not exist");
6493                         err = -EINVAL;
6494                         goto out_unlock;
6495                 }
6496                 region_cb = &devlink_region_snapshot_fill;
6497                 region_cb_priv = snapshot;
6498         }
6499
6500         if (attrs[DEVLINK_ATTR_REGION_CHUNK_ADDR] &&
6501             attrs[DEVLINK_ATTR_REGION_CHUNK_LEN]) {
6502                 if (!start_offset)
6503                         start_offset =
6504                                 nla_get_u64(attrs[DEVLINK_ATTR_REGION_CHUNK_ADDR]);
6505
6506                 end_offset = nla_get_u64(attrs[DEVLINK_ATTR_REGION_CHUNK_ADDR]);
6507                 end_offset += nla_get_u64(attrs[DEVLINK_ATTR_REGION_CHUNK_LEN]);
6508         }
6509
6510         if (end_offset > region->size)
6511                 end_offset = region->size;
6512
6513         /* return 0 if there is no further data to read */
6514         if (start_offset == end_offset) {
6515                 err = 0;
6516                 goto out_unlock;
6517         }
6518
6519         hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
6520                           &devlink_nl_family, NLM_F_ACK | NLM_F_MULTI,
6521                           DEVLINK_CMD_REGION_READ);
6522         if (!hdr) {
6523                 err = -EMSGSIZE;
6524                 goto out_unlock;
6525         }
6526
6527         err = devlink_nl_put_handle(skb, devlink);
6528         if (err)
6529                 goto nla_put_failure;
6530
6531         if (region->port) {
6532                 err = nla_put_u32(skb, DEVLINK_ATTR_PORT_INDEX,
6533                                   region->port->index);
6534                 if (err)
6535                         goto nla_put_failure;
6536         }
6537
6538         err = nla_put_string(skb, DEVLINK_ATTR_REGION_NAME, region_name);
6539         if (err)
6540                 goto nla_put_failure;
6541
6542         chunks_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_REGION_CHUNKS);
6543         if (!chunks_attr) {
6544                 err = -EMSGSIZE;
6545                 goto nla_put_failure;
6546         }
6547
6548         err = devlink_nl_region_read_fill(skb, region_cb, region_cb_priv,
6549                                           start_offset, end_offset, &ret_offset,
6550                                           cb->extack);
6551
6552         if (err && err != -EMSGSIZE)
6553                 goto nla_put_failure;
6554
6555         /* Check if there was any progress done to prevent infinite loop */
6556         if (ret_offset == start_offset) {
6557                 err = -EINVAL;
6558                 goto nla_put_failure;
6559         }
6560
6561         state->start_offset = ret_offset;
6562
6563         nla_nest_end(skb, chunks_attr);
6564         genlmsg_end(skb, hdr);
6565         devl_unlock(devlink);
6566         devlink_put(devlink);
6567         return skb->len;
6568
6569 nla_put_failure:
6570         genlmsg_cancel(skb, hdr);
6571 out_unlock:
6572         devl_unlock(devlink);
6573         devlink_put(devlink);
6574         return err;
6575 }
6576
6577 int devlink_info_serial_number_put(struct devlink_info_req *req, const char *sn)
6578 {
6579         if (!req->msg)
6580                 return 0;
6581         return nla_put_string(req->msg, DEVLINK_ATTR_INFO_SERIAL_NUMBER, sn);
6582 }
6583 EXPORT_SYMBOL_GPL(devlink_info_serial_number_put);
6584
6585 int devlink_info_board_serial_number_put(struct devlink_info_req *req,
6586                                          const char *bsn)
6587 {
6588         if (!req->msg)
6589                 return 0;
6590         return nla_put_string(req->msg, DEVLINK_ATTR_INFO_BOARD_SERIAL_NUMBER,
6591                               bsn);
6592 }
6593 EXPORT_SYMBOL_GPL(devlink_info_board_serial_number_put);
6594
6595 static int devlink_info_version_put(struct devlink_info_req *req, int attr,
6596                                     const char *version_name,
6597                                     const char *version_value,
6598                                     enum devlink_info_version_type version_type)
6599 {
6600         struct nlattr *nest;
6601         int err;
6602
6603         if (req->version_cb)
6604                 req->version_cb(version_name, version_type,
6605                                 req->version_cb_priv);
6606
6607         if (!req->msg)
6608                 return 0;
6609
6610         nest = nla_nest_start_noflag(req->msg, attr);
6611         if (!nest)
6612                 return -EMSGSIZE;
6613
6614         err = nla_put_string(req->msg, DEVLINK_ATTR_INFO_VERSION_NAME,
6615                              version_name);
6616         if (err)
6617                 goto nla_put_failure;
6618
6619         err = nla_put_string(req->msg, DEVLINK_ATTR_INFO_VERSION_VALUE,
6620                              version_value);
6621         if (err)
6622                 goto nla_put_failure;
6623
6624         nla_nest_end(req->msg, nest);
6625
6626         return 0;
6627
6628 nla_put_failure:
6629         nla_nest_cancel(req->msg, nest);
6630         return err;
6631 }
6632
6633 int devlink_info_version_fixed_put(struct devlink_info_req *req,
6634                                    const char *version_name,
6635                                    const char *version_value)
6636 {
6637         return devlink_info_version_put(req, DEVLINK_ATTR_INFO_VERSION_FIXED,
6638                                         version_name, version_value,
6639                                         DEVLINK_INFO_VERSION_TYPE_NONE);
6640 }
6641 EXPORT_SYMBOL_GPL(devlink_info_version_fixed_put);
6642
6643 int devlink_info_version_stored_put(struct devlink_info_req *req,
6644                                     const char *version_name,
6645                                     const char *version_value)
6646 {
6647         return devlink_info_version_put(req, DEVLINK_ATTR_INFO_VERSION_STORED,
6648                                         version_name, version_value,
6649                                         DEVLINK_INFO_VERSION_TYPE_NONE);
6650 }
6651 EXPORT_SYMBOL_GPL(devlink_info_version_stored_put);
6652
6653 int devlink_info_version_stored_put_ext(struct devlink_info_req *req,
6654                                         const char *version_name,
6655                                         const char *version_value,
6656                                         enum devlink_info_version_type version_type)
6657 {
6658         return devlink_info_version_put(req, DEVLINK_ATTR_INFO_VERSION_STORED,
6659                                         version_name, version_value,
6660                                         version_type);
6661 }
6662 EXPORT_SYMBOL_GPL(devlink_info_version_stored_put_ext);
6663
6664 int devlink_info_version_running_put(struct devlink_info_req *req,
6665                                      const char *version_name,
6666                                      const char *version_value)
6667 {
6668         return devlink_info_version_put(req, DEVLINK_ATTR_INFO_VERSION_RUNNING,
6669                                         version_name, version_value,
6670                                         DEVLINK_INFO_VERSION_TYPE_NONE);
6671 }
6672 EXPORT_SYMBOL_GPL(devlink_info_version_running_put);
6673
6674 int devlink_info_version_running_put_ext(struct devlink_info_req *req,
6675                                          const char *version_name,
6676                                          const char *version_value,
6677                                          enum devlink_info_version_type version_type)
6678 {
6679         return devlink_info_version_put(req, DEVLINK_ATTR_INFO_VERSION_RUNNING,
6680                                         version_name, version_value,
6681                                         version_type);
6682 }
6683 EXPORT_SYMBOL_GPL(devlink_info_version_running_put_ext);
6684
6685 static int devlink_nl_driver_info_get(struct device_driver *drv,
6686                                       struct devlink_info_req *req)
6687 {
6688         if (!drv)
6689                 return 0;
6690
6691         if (drv->name[0])
6692                 return nla_put_string(req->msg, DEVLINK_ATTR_INFO_DRIVER_NAME,
6693                                       drv->name);
6694
6695         return 0;
6696 }
6697
6698 static int
6699 devlink_nl_info_fill(struct sk_buff *msg, struct devlink *devlink,
6700                      enum devlink_command cmd, u32 portid,
6701                      u32 seq, int flags, struct netlink_ext_ack *extack)
6702 {
6703         struct device *dev = devlink_to_dev(devlink);
6704         struct devlink_info_req req = {};
6705         void *hdr;
6706         int err;
6707
6708         hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
6709         if (!hdr)
6710                 return -EMSGSIZE;
6711
6712         err = -EMSGSIZE;
6713         if (devlink_nl_put_handle(msg, devlink))
6714                 goto err_cancel_msg;
6715
6716         req.msg = msg;
6717         if (devlink->ops->info_get) {
6718                 err = devlink->ops->info_get(devlink, &req, extack);
6719                 if (err)
6720                         goto err_cancel_msg;
6721         }
6722
6723         err = devlink_nl_driver_info_get(dev->driver, &req);
6724         if (err)
6725                 goto err_cancel_msg;
6726
6727         genlmsg_end(msg, hdr);
6728         return 0;
6729
6730 err_cancel_msg:
6731         genlmsg_cancel(msg, hdr);
6732         return err;
6733 }
6734
6735 static int devlink_nl_cmd_info_get_doit(struct sk_buff *skb,
6736                                         struct genl_info *info)
6737 {
6738         struct devlink *devlink = info->user_ptr[0];
6739         struct sk_buff *msg;
6740         int err;
6741
6742         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6743         if (!msg)
6744                 return -ENOMEM;
6745
6746         err = devlink_nl_info_fill(msg, devlink, DEVLINK_CMD_INFO_GET,
6747                                    info->snd_portid, info->snd_seq, 0,
6748                                    info->extack);
6749         if (err) {
6750                 nlmsg_free(msg);
6751                 return err;
6752         }
6753
6754         return genlmsg_reply(msg, info);
6755 }
6756
6757 static int devlink_nl_cmd_info_get_dumpit(struct sk_buff *msg,
6758                                           struct netlink_callback *cb)
6759 {
6760         struct devlink_nl_dump_state *state = devlink_dump_state(cb);
6761         struct devlink *devlink;
6762         int start = state->idx;
6763         unsigned long index;
6764         int idx = 0;
6765         int err = 0;
6766
6767         devlinks_xa_for_each_registered_get(sock_net(msg->sk), index, devlink) {
6768                 if (idx < start)
6769                         goto inc;
6770
6771                 devl_lock(devlink);
6772                 err = devlink_nl_info_fill(msg, devlink, DEVLINK_CMD_INFO_GET,
6773                                            NETLINK_CB(cb->skb).portid,
6774                                            cb->nlh->nlmsg_seq, NLM_F_MULTI,
6775                                            cb->extack);
6776                 devl_unlock(devlink);
6777                 if (err == -EOPNOTSUPP)
6778                         err = 0;
6779                 else if (err) {
6780                         devlink_put(devlink);
6781                         break;
6782                 }
6783 inc:
6784                 idx++;
6785                 devlink_put(devlink);
6786         }
6787
6788         if (err != -EMSGSIZE)
6789                 return err;
6790
6791         state->idx = idx;
6792         return msg->len;
6793 }
6794
6795 struct devlink_fmsg_item {
6796         struct list_head list;
6797         int attrtype;
6798         u8 nla_type;
6799         u16 len;
6800         int value[];
6801 };
6802
6803 struct devlink_fmsg {
6804         struct list_head item_list;
6805         bool putting_binary; /* This flag forces enclosing of binary data
6806                               * in an array brackets. It forces using
6807                               * of designated API:
6808                               * devlink_fmsg_binary_pair_nest_start()
6809                               * devlink_fmsg_binary_pair_nest_end()
6810                               */
6811 };
6812
6813 static struct devlink_fmsg *devlink_fmsg_alloc(void)
6814 {
6815         struct devlink_fmsg *fmsg;
6816
6817         fmsg = kzalloc(sizeof(*fmsg), GFP_KERNEL);
6818         if (!fmsg)
6819                 return NULL;
6820
6821         INIT_LIST_HEAD(&fmsg->item_list);
6822
6823         return fmsg;
6824 }
6825
6826 static void devlink_fmsg_free(struct devlink_fmsg *fmsg)
6827 {
6828         struct devlink_fmsg_item *item, *tmp;
6829
6830         list_for_each_entry_safe(item, tmp, &fmsg->item_list, list) {
6831                 list_del(&item->list);
6832                 kfree(item);
6833         }
6834         kfree(fmsg);
6835 }
6836
6837 static int devlink_fmsg_nest_common(struct devlink_fmsg *fmsg,
6838                                     int attrtype)
6839 {
6840         struct devlink_fmsg_item *item;
6841
6842         item = kzalloc(sizeof(*item), GFP_KERNEL);
6843         if (!item)
6844                 return -ENOMEM;
6845
6846         item->attrtype = attrtype;
6847         list_add_tail(&item->list, &fmsg->item_list);
6848
6849         return 0;
6850 }
6851
6852 int devlink_fmsg_obj_nest_start(struct devlink_fmsg *fmsg)
6853 {
6854         if (fmsg->putting_binary)
6855                 return -EINVAL;
6856
6857         return devlink_fmsg_nest_common(fmsg, DEVLINK_ATTR_FMSG_OBJ_NEST_START);
6858 }
6859 EXPORT_SYMBOL_GPL(devlink_fmsg_obj_nest_start);
6860
6861 static int devlink_fmsg_nest_end(struct devlink_fmsg *fmsg)
6862 {
6863         if (fmsg->putting_binary)
6864                 return -EINVAL;
6865
6866         return devlink_fmsg_nest_common(fmsg, DEVLINK_ATTR_FMSG_NEST_END);
6867 }
6868
6869 int devlink_fmsg_obj_nest_end(struct devlink_fmsg *fmsg)
6870 {
6871         if (fmsg->putting_binary)
6872                 return -EINVAL;
6873
6874         return devlink_fmsg_nest_end(fmsg);
6875 }
6876 EXPORT_SYMBOL_GPL(devlink_fmsg_obj_nest_end);
6877
6878 #define DEVLINK_FMSG_MAX_SIZE (GENLMSG_DEFAULT_SIZE - GENL_HDRLEN - NLA_HDRLEN)
6879
6880 static int devlink_fmsg_put_name(struct devlink_fmsg *fmsg, const char *name)
6881 {
6882         struct devlink_fmsg_item *item;
6883
6884         if (fmsg->putting_binary)
6885                 return -EINVAL;
6886
6887         if (strlen(name) + 1 > DEVLINK_FMSG_MAX_SIZE)
6888                 return -EMSGSIZE;
6889
6890         item = kzalloc(sizeof(*item) + strlen(name) + 1, GFP_KERNEL);
6891         if (!item)
6892                 return -ENOMEM;
6893
6894         item->nla_type = NLA_NUL_STRING;
6895         item->len = strlen(name) + 1;
6896         item->attrtype = DEVLINK_ATTR_FMSG_OBJ_NAME;
6897         memcpy(&item->value, name, item->len);
6898         list_add_tail(&item->list, &fmsg->item_list);
6899
6900         return 0;
6901 }
6902
6903 int devlink_fmsg_pair_nest_start(struct devlink_fmsg *fmsg, const char *name)
6904 {
6905         int err;
6906
6907         if (fmsg->putting_binary)
6908                 return -EINVAL;
6909
6910         err = devlink_fmsg_nest_common(fmsg, DEVLINK_ATTR_FMSG_PAIR_NEST_START);
6911         if (err)
6912                 return err;
6913
6914         err = devlink_fmsg_put_name(fmsg, name);
6915         if (err)
6916                 return err;
6917
6918         return 0;
6919 }
6920 EXPORT_SYMBOL_GPL(devlink_fmsg_pair_nest_start);
6921
6922 int devlink_fmsg_pair_nest_end(struct devlink_fmsg *fmsg)
6923 {
6924         if (fmsg->putting_binary)
6925                 return -EINVAL;
6926
6927         return devlink_fmsg_nest_end(fmsg);
6928 }
6929 EXPORT_SYMBOL_GPL(devlink_fmsg_pair_nest_end);
6930
6931 int devlink_fmsg_arr_pair_nest_start(struct devlink_fmsg *fmsg,
6932                                      const char *name)
6933 {
6934         int err;
6935
6936         if (fmsg->putting_binary)
6937                 return -EINVAL;
6938
6939         err = devlink_fmsg_pair_nest_start(fmsg, name);
6940         if (err)
6941                 return err;
6942
6943         err = devlink_fmsg_nest_common(fmsg, DEVLINK_ATTR_FMSG_ARR_NEST_START);
6944         if (err)
6945                 return err;
6946
6947         return 0;
6948 }
6949 EXPORT_SYMBOL_GPL(devlink_fmsg_arr_pair_nest_start);
6950
6951 int devlink_fmsg_arr_pair_nest_end(struct devlink_fmsg *fmsg)
6952 {
6953         int err;
6954
6955         if (fmsg->putting_binary)
6956                 return -EINVAL;
6957
6958         err = devlink_fmsg_nest_end(fmsg);
6959         if (err)
6960                 return err;
6961
6962         err = devlink_fmsg_nest_end(fmsg);
6963         if (err)
6964                 return err;
6965
6966         return 0;
6967 }
6968 EXPORT_SYMBOL_GPL(devlink_fmsg_arr_pair_nest_end);
6969
6970 int devlink_fmsg_binary_pair_nest_start(struct devlink_fmsg *fmsg,
6971                                         const char *name)
6972 {
6973         int err;
6974
6975         err = devlink_fmsg_arr_pair_nest_start(fmsg, name);
6976         if (err)
6977                 return err;
6978
6979         fmsg->putting_binary = true;
6980         return err;
6981 }
6982 EXPORT_SYMBOL_GPL(devlink_fmsg_binary_pair_nest_start);
6983
6984 int devlink_fmsg_binary_pair_nest_end(struct devlink_fmsg *fmsg)
6985 {
6986         if (!fmsg->putting_binary)
6987                 return -EINVAL;
6988
6989         fmsg->putting_binary = false;
6990         return devlink_fmsg_arr_pair_nest_end(fmsg);
6991 }
6992 EXPORT_SYMBOL_GPL(devlink_fmsg_binary_pair_nest_end);
6993
6994 static int devlink_fmsg_put_value(struct devlink_fmsg *fmsg,
6995                                   const void *value, u16 value_len,
6996                                   u8 value_nla_type)
6997 {
6998         struct devlink_fmsg_item *item;
6999
7000         if (value_len > DEVLINK_FMSG_MAX_SIZE)
7001                 return -EMSGSIZE;
7002
7003         item = kzalloc(sizeof(*item) + value_len, GFP_KERNEL);
7004         if (!item)
7005                 return -ENOMEM;
7006
7007         item->nla_type = value_nla_type;
7008         item->len = value_len;
7009         item->attrtype = DEVLINK_ATTR_FMSG_OBJ_VALUE_DATA;
7010         memcpy(&item->value, value, item->len);
7011         list_add_tail(&item->list, &fmsg->item_list);
7012
7013         return 0;
7014 }
7015
7016 static int devlink_fmsg_bool_put(struct devlink_fmsg *fmsg, bool value)
7017 {
7018         if (fmsg->putting_binary)
7019                 return -EINVAL;
7020
7021         return devlink_fmsg_put_value(fmsg, &value, sizeof(value), NLA_FLAG);
7022 }
7023
7024 static int devlink_fmsg_u8_put(struct devlink_fmsg *fmsg, u8 value)
7025 {
7026         if (fmsg->putting_binary)
7027                 return -EINVAL;
7028
7029         return devlink_fmsg_put_value(fmsg, &value, sizeof(value), NLA_U8);
7030 }
7031
7032 int devlink_fmsg_u32_put(struct devlink_fmsg *fmsg, u32 value)
7033 {
7034         if (fmsg->putting_binary)
7035                 return -EINVAL;
7036
7037         return devlink_fmsg_put_value(fmsg, &value, sizeof(value), NLA_U32);
7038 }
7039 EXPORT_SYMBOL_GPL(devlink_fmsg_u32_put);
7040
7041 static int devlink_fmsg_u64_put(struct devlink_fmsg *fmsg, u64 value)
7042 {
7043         if (fmsg->putting_binary)
7044                 return -EINVAL;
7045
7046         return devlink_fmsg_put_value(fmsg, &value, sizeof(value), NLA_U64);
7047 }
7048
7049 int devlink_fmsg_string_put(struct devlink_fmsg *fmsg, const char *value)
7050 {
7051         if (fmsg->putting_binary)
7052                 return -EINVAL;
7053
7054         return devlink_fmsg_put_value(fmsg, value, strlen(value) + 1,
7055                                       NLA_NUL_STRING);
7056 }
7057 EXPORT_SYMBOL_GPL(devlink_fmsg_string_put);
7058
7059 int devlink_fmsg_binary_put(struct devlink_fmsg *fmsg, const void *value,
7060                             u16 value_len)
7061 {
7062         if (!fmsg->putting_binary)
7063                 return -EINVAL;
7064
7065         return devlink_fmsg_put_value(fmsg, value, value_len, NLA_BINARY);
7066 }
7067 EXPORT_SYMBOL_GPL(devlink_fmsg_binary_put);
7068
7069 int devlink_fmsg_bool_pair_put(struct devlink_fmsg *fmsg, const char *name,
7070                                bool value)
7071 {
7072         int err;
7073
7074         err = devlink_fmsg_pair_nest_start(fmsg, name);
7075         if (err)
7076                 return err;
7077
7078         err = devlink_fmsg_bool_put(fmsg, value);
7079         if (err)
7080                 return err;
7081
7082         err = devlink_fmsg_pair_nest_end(fmsg);
7083         if (err)
7084                 return err;
7085
7086         return 0;
7087 }
7088 EXPORT_SYMBOL_GPL(devlink_fmsg_bool_pair_put);
7089
7090 int devlink_fmsg_u8_pair_put(struct devlink_fmsg *fmsg, const char *name,
7091                              u8 value)
7092 {
7093         int err;
7094
7095         err = devlink_fmsg_pair_nest_start(fmsg, name);
7096         if (err)
7097                 return err;
7098
7099         err = devlink_fmsg_u8_put(fmsg, value);
7100         if (err)
7101                 return err;
7102
7103         err = devlink_fmsg_pair_nest_end(fmsg);
7104         if (err)
7105                 return err;
7106
7107         return 0;
7108 }
7109 EXPORT_SYMBOL_GPL(devlink_fmsg_u8_pair_put);
7110
7111 int devlink_fmsg_u32_pair_put(struct devlink_fmsg *fmsg, const char *name,
7112                               u32 value)
7113 {
7114         int err;
7115
7116         err = devlink_fmsg_pair_nest_start(fmsg, name);
7117         if (err)
7118                 return err;
7119
7120         err = devlink_fmsg_u32_put(fmsg, value);
7121         if (err)
7122                 return err;
7123
7124         err = devlink_fmsg_pair_nest_end(fmsg);
7125         if (err)
7126                 return err;
7127
7128         return 0;
7129 }
7130 EXPORT_SYMBOL_GPL(devlink_fmsg_u32_pair_put);
7131
7132 int devlink_fmsg_u64_pair_put(struct devlink_fmsg *fmsg, const char *name,
7133                               u64 value)
7134 {
7135         int err;
7136
7137         err = devlink_fmsg_pair_nest_start(fmsg, name);
7138         if (err)
7139                 return err;
7140
7141         err = devlink_fmsg_u64_put(fmsg, value);
7142         if (err)
7143                 return err;
7144
7145         err = devlink_fmsg_pair_nest_end(fmsg);
7146         if (err)
7147                 return err;
7148
7149         return 0;
7150 }
7151 EXPORT_SYMBOL_GPL(devlink_fmsg_u64_pair_put);
7152
7153 int devlink_fmsg_string_pair_put(struct devlink_fmsg *fmsg, const char *name,
7154                                  const char *value)
7155 {
7156         int err;
7157
7158         err = devlink_fmsg_pair_nest_start(fmsg, name);
7159         if (err)
7160                 return err;
7161
7162         err = devlink_fmsg_string_put(fmsg, value);
7163         if (err)
7164                 return err;
7165
7166         err = devlink_fmsg_pair_nest_end(fmsg);
7167         if (err)
7168                 return err;
7169
7170         return 0;
7171 }
7172 EXPORT_SYMBOL_GPL(devlink_fmsg_string_pair_put);
7173
7174 int devlink_fmsg_binary_pair_put(struct devlink_fmsg *fmsg, const char *name,
7175                                  const void *value, u32 value_len)
7176 {
7177         u32 data_size;
7178         int end_err;
7179         u32 offset;
7180         int err;
7181
7182         err = devlink_fmsg_binary_pair_nest_start(fmsg, name);
7183         if (err)
7184                 return err;
7185
7186         for (offset = 0; offset < value_len; offset += data_size) {
7187                 data_size = value_len - offset;
7188                 if (data_size > DEVLINK_FMSG_MAX_SIZE)
7189                         data_size = DEVLINK_FMSG_MAX_SIZE;
7190                 err = devlink_fmsg_binary_put(fmsg, value + offset, data_size);
7191                 if (err)
7192                         break;
7193                 /* Exit from loop with a break (instead of
7194                  * return) to make sure putting_binary is turned off in
7195                  * devlink_fmsg_binary_pair_nest_end
7196                  */
7197         }
7198
7199         end_err = devlink_fmsg_binary_pair_nest_end(fmsg);
7200         if (end_err)
7201                 err = end_err;
7202
7203         return err;
7204 }
7205 EXPORT_SYMBOL_GPL(devlink_fmsg_binary_pair_put);
7206
7207 static int
7208 devlink_fmsg_item_fill_type(struct devlink_fmsg_item *msg, struct sk_buff *skb)
7209 {
7210         switch (msg->nla_type) {
7211         case NLA_FLAG:
7212         case NLA_U8:
7213         case NLA_U32:
7214         case NLA_U64:
7215         case NLA_NUL_STRING:
7216         case NLA_BINARY:
7217                 return nla_put_u8(skb, DEVLINK_ATTR_FMSG_OBJ_VALUE_TYPE,
7218                                   msg->nla_type);
7219         default:
7220                 return -EINVAL;
7221         }
7222 }
7223
7224 static int
7225 devlink_fmsg_item_fill_data(struct devlink_fmsg_item *msg, struct sk_buff *skb)
7226 {
7227         int attrtype = DEVLINK_ATTR_FMSG_OBJ_VALUE_DATA;
7228         u8 tmp;
7229
7230         switch (msg->nla_type) {
7231         case NLA_FLAG:
7232                 /* Always provide flag data, regardless of its value */
7233                 tmp = *(bool *) msg->value;
7234
7235                 return nla_put_u8(skb, attrtype, tmp);
7236         case NLA_U8:
7237                 return nla_put_u8(skb, attrtype, *(u8 *) msg->value);
7238         case NLA_U32:
7239                 return nla_put_u32(skb, attrtype, *(u32 *) msg->value);
7240         case NLA_U64:
7241                 return nla_put_u64_64bit(skb, attrtype, *(u64 *) msg->value,
7242                                          DEVLINK_ATTR_PAD);
7243         case NLA_NUL_STRING:
7244                 return nla_put_string(skb, attrtype, (char *) &msg->value);
7245         case NLA_BINARY:
7246                 return nla_put(skb, attrtype, msg->len, (void *) &msg->value);
7247         default:
7248                 return -EINVAL;
7249         }
7250 }
7251
7252 static int
7253 devlink_fmsg_prepare_skb(struct devlink_fmsg *fmsg, struct sk_buff *skb,
7254                          int *start)
7255 {
7256         struct devlink_fmsg_item *item;
7257         struct nlattr *fmsg_nlattr;
7258         int i = 0;
7259         int err;
7260
7261         fmsg_nlattr = nla_nest_start_noflag(skb, DEVLINK_ATTR_FMSG);
7262         if (!fmsg_nlattr)
7263                 return -EMSGSIZE;
7264
7265         list_for_each_entry(item, &fmsg->item_list, list) {
7266                 if (i < *start) {
7267                         i++;
7268                         continue;
7269                 }
7270
7271                 switch (item->attrtype) {
7272                 case DEVLINK_ATTR_FMSG_OBJ_NEST_START:
7273                 case DEVLINK_ATTR_FMSG_PAIR_NEST_START:
7274                 case DEVLINK_ATTR_FMSG_ARR_NEST_START:
7275                 case DEVLINK_ATTR_FMSG_NEST_END:
7276                         err = nla_put_flag(skb, item->attrtype);
7277                         break;
7278                 case DEVLINK_ATTR_FMSG_OBJ_VALUE_DATA:
7279                         err = devlink_fmsg_item_fill_type(item, skb);
7280                         if (err)
7281                                 break;
7282                         err = devlink_fmsg_item_fill_data(item, skb);
7283                         break;
7284                 case DEVLINK_ATTR_FMSG_OBJ_NAME:
7285                         err = nla_put_string(skb, item->attrtype,
7286                                              (char *) &item->value);
7287                         break;
7288                 default:
7289                         err = -EINVAL;
7290                         break;
7291                 }
7292                 if (!err)
7293                         *start = ++i;
7294                 else
7295                         break;
7296         }
7297
7298         nla_nest_end(skb, fmsg_nlattr);
7299         return err;
7300 }
7301
7302 static int devlink_fmsg_snd(struct devlink_fmsg *fmsg,
7303                             struct genl_info *info,
7304                             enum devlink_command cmd, int flags)
7305 {
7306         struct nlmsghdr *nlh;
7307         struct sk_buff *skb;
7308         bool last = false;
7309         int index = 0;
7310         void *hdr;
7311         int err;
7312
7313         while (!last) {
7314                 int tmp_index = index;
7315
7316                 skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
7317                 if (!skb)
7318                         return -ENOMEM;
7319
7320                 hdr = genlmsg_put(skb, info->snd_portid, info->snd_seq,
7321                                   &devlink_nl_family, flags | NLM_F_MULTI, cmd);
7322                 if (!hdr) {
7323                         err = -EMSGSIZE;
7324                         goto nla_put_failure;
7325                 }
7326
7327                 err = devlink_fmsg_prepare_skb(fmsg, skb, &index);
7328                 if (!err)
7329                         last = true;
7330                 else if (err != -EMSGSIZE || tmp_index == index)
7331                         goto nla_put_failure;
7332
7333                 genlmsg_end(skb, hdr);
7334                 err = genlmsg_reply(skb, info);
7335                 if (err)
7336                         return err;
7337         }
7338
7339         skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
7340         if (!skb)
7341                 return -ENOMEM;
7342         nlh = nlmsg_put(skb, info->snd_portid, info->snd_seq,
7343                         NLMSG_DONE, 0, flags | NLM_F_MULTI);
7344         if (!nlh) {
7345                 err = -EMSGSIZE;
7346                 goto nla_put_failure;
7347         }
7348
7349         return genlmsg_reply(skb, info);
7350
7351 nla_put_failure:
7352         nlmsg_free(skb);
7353         return err;
7354 }
7355
7356 static int devlink_fmsg_dumpit(struct devlink_fmsg *fmsg, struct sk_buff *skb,
7357                                struct netlink_callback *cb,
7358                                enum devlink_command cmd)
7359 {
7360         struct devlink_nl_dump_state *state = devlink_dump_state(cb);
7361         int index = state->idx;
7362         int tmp_index = index;
7363         void *hdr;
7364         int err;
7365
7366         hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
7367                           &devlink_nl_family, NLM_F_ACK | NLM_F_MULTI, cmd);
7368         if (!hdr) {
7369                 err = -EMSGSIZE;
7370                 goto nla_put_failure;
7371         }
7372
7373         err = devlink_fmsg_prepare_skb(fmsg, skb, &index);
7374         if ((err && err != -EMSGSIZE) || tmp_index == index)
7375                 goto nla_put_failure;
7376
7377         state->idx = index;
7378         genlmsg_end(skb, hdr);
7379         return skb->len;
7380
7381 nla_put_failure:
7382         genlmsg_cancel(skb, hdr);
7383         return err;
7384 }
7385
7386 struct devlink_health_reporter {
7387         struct list_head list;
7388         void *priv;
7389         const struct devlink_health_reporter_ops *ops;
7390         struct devlink *devlink;
7391         struct devlink_port *devlink_port;
7392         struct devlink_fmsg *dump_fmsg;
7393         struct mutex dump_lock; /* lock parallel read/write from dump buffers */
7394         u64 graceful_period;
7395         bool auto_recover;
7396         bool auto_dump;
7397         u8 health_state;
7398         u64 dump_ts;
7399         u64 dump_real_ts;
7400         u64 error_count;
7401         u64 recovery_count;
7402         u64 last_recovery_ts;
7403         refcount_t refcount;
7404 };
7405
7406 void *
7407 devlink_health_reporter_priv(struct devlink_health_reporter *reporter)
7408 {
7409         return reporter->priv;
7410 }
7411 EXPORT_SYMBOL_GPL(devlink_health_reporter_priv);
7412
7413 static struct devlink_health_reporter *
7414 __devlink_health_reporter_find_by_name(struct list_head *reporter_list,
7415                                        struct mutex *list_lock,
7416                                        const char *reporter_name)
7417 {
7418         struct devlink_health_reporter *reporter;
7419
7420         lockdep_assert_held(list_lock);
7421         list_for_each_entry(reporter, reporter_list, list)
7422                 if (!strcmp(reporter->ops->name, reporter_name))
7423                         return reporter;
7424         return NULL;
7425 }
7426
7427 static struct devlink_health_reporter *
7428 devlink_health_reporter_find_by_name(struct devlink *devlink,
7429                                      const char *reporter_name)
7430 {
7431         return __devlink_health_reporter_find_by_name(&devlink->reporter_list,
7432                                                       &devlink->reporters_lock,
7433                                                       reporter_name);
7434 }
7435
7436 static struct devlink_health_reporter *
7437 devlink_port_health_reporter_find_by_name(struct devlink_port *devlink_port,
7438                                           const char *reporter_name)
7439 {
7440         return __devlink_health_reporter_find_by_name(&devlink_port->reporter_list,
7441                                                       &devlink_port->reporters_lock,
7442                                                       reporter_name);
7443 }
7444
7445 static struct devlink_health_reporter *
7446 __devlink_health_reporter_create(struct devlink *devlink,
7447                                  const struct devlink_health_reporter_ops *ops,
7448                                  u64 graceful_period, void *priv)
7449 {
7450         struct devlink_health_reporter *reporter;
7451
7452         if (WARN_ON(graceful_period && !ops->recover))
7453                 return ERR_PTR(-EINVAL);
7454
7455         reporter = kzalloc(sizeof(*reporter), GFP_KERNEL);
7456         if (!reporter)
7457                 return ERR_PTR(-ENOMEM);
7458
7459         reporter->priv = priv;
7460         reporter->ops = ops;
7461         reporter->devlink = devlink;
7462         reporter->graceful_period = graceful_period;
7463         reporter->auto_recover = !!ops->recover;
7464         reporter->auto_dump = !!ops->dump;
7465         mutex_init(&reporter->dump_lock);
7466         refcount_set(&reporter->refcount, 1);
7467         return reporter;
7468 }
7469
7470 /**
7471  *      devlink_port_health_reporter_create - create devlink health reporter for
7472  *                                            specified port instance
7473  *
7474  *      @port: devlink_port which should contain the new reporter
7475  *      @ops: ops
7476  *      @graceful_period: to avoid recovery loops, in msecs
7477  *      @priv: priv
7478  */
7479 struct devlink_health_reporter *
7480 devlink_port_health_reporter_create(struct devlink_port *port,
7481                                     const struct devlink_health_reporter_ops *ops,
7482                                     u64 graceful_period, void *priv)
7483 {
7484         struct devlink_health_reporter *reporter;
7485
7486         mutex_lock(&port->reporters_lock);
7487         if (__devlink_health_reporter_find_by_name(&port->reporter_list,
7488                                                    &port->reporters_lock, ops->name)) {
7489                 reporter = ERR_PTR(-EEXIST);
7490                 goto unlock;
7491         }
7492
7493         reporter = __devlink_health_reporter_create(port->devlink, ops,
7494                                                     graceful_period, priv);
7495         if (IS_ERR(reporter))
7496                 goto unlock;
7497
7498         reporter->devlink_port = port;
7499         list_add_tail(&reporter->list, &port->reporter_list);
7500 unlock:
7501         mutex_unlock(&port->reporters_lock);
7502         return reporter;
7503 }
7504 EXPORT_SYMBOL_GPL(devlink_port_health_reporter_create);
7505
7506 /**
7507  *      devlink_health_reporter_create - create devlink health reporter
7508  *
7509  *      @devlink: devlink
7510  *      @ops: ops
7511  *      @graceful_period: to avoid recovery loops, in msecs
7512  *      @priv: priv
7513  */
7514 struct devlink_health_reporter *
7515 devlink_health_reporter_create(struct devlink *devlink,
7516                                const struct devlink_health_reporter_ops *ops,
7517                                u64 graceful_period, void *priv)
7518 {
7519         struct devlink_health_reporter *reporter;
7520
7521         mutex_lock(&devlink->reporters_lock);
7522         if (devlink_health_reporter_find_by_name(devlink, ops->name)) {
7523                 reporter = ERR_PTR(-EEXIST);
7524                 goto unlock;
7525         }
7526
7527         reporter = __devlink_health_reporter_create(devlink, ops,
7528                                                     graceful_period, priv);
7529         if (IS_ERR(reporter))
7530                 goto unlock;
7531
7532         list_add_tail(&reporter->list, &devlink->reporter_list);
7533 unlock:
7534         mutex_unlock(&devlink->reporters_lock);
7535         return reporter;
7536 }
7537 EXPORT_SYMBOL_GPL(devlink_health_reporter_create);
7538
7539 static void
7540 devlink_health_reporter_free(struct devlink_health_reporter *reporter)
7541 {
7542         mutex_destroy(&reporter->dump_lock);
7543         if (reporter->dump_fmsg)
7544                 devlink_fmsg_free(reporter->dump_fmsg);
7545         kfree(reporter);
7546 }
7547
7548 static void
7549 devlink_health_reporter_put(struct devlink_health_reporter *reporter)
7550 {
7551         if (refcount_dec_and_test(&reporter->refcount))
7552                 devlink_health_reporter_free(reporter);
7553 }
7554
7555 static void
7556 __devlink_health_reporter_destroy(struct devlink_health_reporter *reporter)
7557 {
7558         list_del(&reporter->list);
7559         devlink_health_reporter_put(reporter);
7560 }
7561
7562 /**
7563  *      devlink_health_reporter_destroy - destroy devlink health reporter
7564  *
7565  *      @reporter: devlink health reporter to destroy
7566  */
7567 void
7568 devlink_health_reporter_destroy(struct devlink_health_reporter *reporter)
7569 {
7570         struct mutex *lock = &reporter->devlink->reporters_lock;
7571
7572         mutex_lock(lock);
7573         __devlink_health_reporter_destroy(reporter);
7574         mutex_unlock(lock);
7575 }
7576 EXPORT_SYMBOL_GPL(devlink_health_reporter_destroy);
7577
7578 /**
7579  *      devlink_port_health_reporter_destroy - destroy devlink port health reporter
7580  *
7581  *      @reporter: devlink health reporter to destroy
7582  */
7583 void
7584 devlink_port_health_reporter_destroy(struct devlink_health_reporter *reporter)
7585 {
7586         struct mutex *lock = &reporter->devlink_port->reporters_lock;
7587
7588         mutex_lock(lock);
7589         __devlink_health_reporter_destroy(reporter);
7590         mutex_unlock(lock);
7591 }
7592 EXPORT_SYMBOL_GPL(devlink_port_health_reporter_destroy);
7593
7594 static int
7595 devlink_nl_health_reporter_fill(struct sk_buff *msg,
7596                                 struct devlink_health_reporter *reporter,
7597                                 enum devlink_command cmd, u32 portid,
7598                                 u32 seq, int flags)
7599 {
7600         struct devlink *devlink = reporter->devlink;
7601         struct nlattr *reporter_attr;
7602         void *hdr;
7603
7604         hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
7605         if (!hdr)
7606                 return -EMSGSIZE;
7607
7608         if (devlink_nl_put_handle(msg, devlink))
7609                 goto genlmsg_cancel;
7610
7611         if (reporter->devlink_port) {
7612                 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, reporter->devlink_port->index))
7613                         goto genlmsg_cancel;
7614         }
7615         reporter_attr = nla_nest_start_noflag(msg,
7616                                               DEVLINK_ATTR_HEALTH_REPORTER);
7617         if (!reporter_attr)
7618                 goto genlmsg_cancel;
7619         if (nla_put_string(msg, DEVLINK_ATTR_HEALTH_REPORTER_NAME,
7620                            reporter->ops->name))
7621                 goto reporter_nest_cancel;
7622         if (nla_put_u8(msg, DEVLINK_ATTR_HEALTH_REPORTER_STATE,
7623                        reporter->health_state))
7624                 goto reporter_nest_cancel;
7625         if (nla_put_u64_64bit(msg, DEVLINK_ATTR_HEALTH_REPORTER_ERR_COUNT,
7626                               reporter->error_count, DEVLINK_ATTR_PAD))
7627                 goto reporter_nest_cancel;
7628         if (nla_put_u64_64bit(msg, DEVLINK_ATTR_HEALTH_REPORTER_RECOVER_COUNT,
7629                               reporter->recovery_count, DEVLINK_ATTR_PAD))
7630                 goto reporter_nest_cancel;
7631         if (reporter->ops->recover &&
7632             nla_put_u64_64bit(msg, DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD,
7633                               reporter->graceful_period,
7634                               DEVLINK_ATTR_PAD))
7635                 goto reporter_nest_cancel;
7636         if (reporter->ops->recover &&
7637             nla_put_u8(msg, DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER,
7638                        reporter->auto_recover))
7639                 goto reporter_nest_cancel;
7640         if (reporter->dump_fmsg &&
7641             nla_put_u64_64bit(msg, DEVLINK_ATTR_HEALTH_REPORTER_DUMP_TS,
7642                               jiffies_to_msecs(reporter->dump_ts),
7643                               DEVLINK_ATTR_PAD))
7644                 goto reporter_nest_cancel;
7645         if (reporter->dump_fmsg &&
7646             nla_put_u64_64bit(msg, DEVLINK_ATTR_HEALTH_REPORTER_DUMP_TS_NS,
7647                               reporter->dump_real_ts, DEVLINK_ATTR_PAD))
7648                 goto reporter_nest_cancel;
7649         if (reporter->ops->dump &&
7650             nla_put_u8(msg, DEVLINK_ATTR_HEALTH_REPORTER_AUTO_DUMP,
7651                        reporter->auto_dump))
7652                 goto reporter_nest_cancel;
7653
7654         nla_nest_end(msg, reporter_attr);
7655         genlmsg_end(msg, hdr);
7656         return 0;
7657
7658 reporter_nest_cancel:
7659         nla_nest_end(msg, reporter_attr);
7660 genlmsg_cancel:
7661         genlmsg_cancel(msg, hdr);
7662         return -EMSGSIZE;
7663 }
7664
7665 static void devlink_recover_notify(struct devlink_health_reporter *reporter,
7666                                    enum devlink_command cmd)
7667 {
7668         struct devlink *devlink = reporter->devlink;
7669         struct sk_buff *msg;
7670         int err;
7671
7672         WARN_ON(cmd != DEVLINK_CMD_HEALTH_REPORTER_RECOVER);
7673         WARN_ON(!xa_get_mark(&devlinks, devlink->index, DEVLINK_REGISTERED));
7674
7675         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
7676         if (!msg)
7677                 return;
7678
7679         err = devlink_nl_health_reporter_fill(msg, reporter, cmd, 0, 0, 0);
7680         if (err) {
7681                 nlmsg_free(msg);
7682                 return;
7683         }
7684
7685         genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink), msg,
7686                                 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
7687 }
7688
7689 void
7690 devlink_health_reporter_recovery_done(struct devlink_health_reporter *reporter)
7691 {
7692         reporter->recovery_count++;
7693         reporter->last_recovery_ts = jiffies;
7694 }
7695 EXPORT_SYMBOL_GPL(devlink_health_reporter_recovery_done);
7696
7697 static int
7698 devlink_health_reporter_recover(struct devlink_health_reporter *reporter,
7699                                 void *priv_ctx, struct netlink_ext_ack *extack)
7700 {
7701         int err;
7702
7703         if (reporter->health_state == DEVLINK_HEALTH_REPORTER_STATE_HEALTHY)
7704                 return 0;
7705
7706         if (!reporter->ops->recover)
7707                 return -EOPNOTSUPP;
7708
7709         err = reporter->ops->recover(reporter, priv_ctx, extack);
7710         if (err)
7711                 return err;
7712
7713         devlink_health_reporter_recovery_done(reporter);
7714         reporter->health_state = DEVLINK_HEALTH_REPORTER_STATE_HEALTHY;
7715         devlink_recover_notify(reporter, DEVLINK_CMD_HEALTH_REPORTER_RECOVER);
7716
7717         return 0;
7718 }
7719
7720 static void
7721 devlink_health_dump_clear(struct devlink_health_reporter *reporter)
7722 {
7723         if (!reporter->dump_fmsg)
7724                 return;
7725         devlink_fmsg_free(reporter->dump_fmsg);
7726         reporter->dump_fmsg = NULL;
7727 }
7728
7729 static int devlink_health_do_dump(struct devlink_health_reporter *reporter,
7730                                   void *priv_ctx,
7731                                   struct netlink_ext_ack *extack)
7732 {
7733         int err;
7734
7735         if (!reporter->ops->dump)
7736                 return 0;
7737
7738         if (reporter->dump_fmsg)
7739                 return 0;
7740
7741         reporter->dump_fmsg = devlink_fmsg_alloc();
7742         if (!reporter->dump_fmsg) {
7743                 err = -ENOMEM;
7744                 return err;
7745         }
7746
7747         err = devlink_fmsg_obj_nest_start(reporter->dump_fmsg);
7748         if (err)
7749                 goto dump_err;
7750
7751         err = reporter->ops->dump(reporter, reporter->dump_fmsg,
7752                                   priv_ctx, extack);
7753         if (err)
7754                 goto dump_err;
7755
7756         err = devlink_fmsg_obj_nest_end(reporter->dump_fmsg);
7757         if (err)
7758                 goto dump_err;
7759
7760         reporter->dump_ts = jiffies;
7761         reporter->dump_real_ts = ktime_get_real_ns();
7762
7763         return 0;
7764
7765 dump_err:
7766         devlink_health_dump_clear(reporter);
7767         return err;
7768 }
7769
7770 int devlink_health_report(struct devlink_health_reporter *reporter,
7771                           const char *msg, void *priv_ctx)
7772 {
7773         enum devlink_health_reporter_state prev_health_state;
7774         struct devlink *devlink = reporter->devlink;
7775         unsigned long recover_ts_threshold;
7776         int ret;
7777
7778         /* write a log message of the current error */
7779         WARN_ON(!msg);
7780         trace_devlink_health_report(devlink, reporter->ops->name, msg);
7781         reporter->error_count++;
7782         prev_health_state = reporter->health_state;
7783         reporter->health_state = DEVLINK_HEALTH_REPORTER_STATE_ERROR;
7784         devlink_recover_notify(reporter, DEVLINK_CMD_HEALTH_REPORTER_RECOVER);
7785
7786         /* abort if the previous error wasn't recovered */
7787         recover_ts_threshold = reporter->last_recovery_ts +
7788                                msecs_to_jiffies(reporter->graceful_period);
7789         if (reporter->auto_recover &&
7790             (prev_health_state != DEVLINK_HEALTH_REPORTER_STATE_HEALTHY ||
7791              (reporter->last_recovery_ts && reporter->recovery_count &&
7792               time_is_after_jiffies(recover_ts_threshold)))) {
7793                 trace_devlink_health_recover_aborted(devlink,
7794                                                      reporter->ops->name,
7795                                                      reporter->health_state,
7796                                                      jiffies -
7797                                                      reporter->last_recovery_ts);
7798                 return -ECANCELED;
7799         }
7800
7801         if (reporter->auto_dump) {
7802                 mutex_lock(&reporter->dump_lock);
7803                 /* store current dump of current error, for later analysis */
7804                 devlink_health_do_dump(reporter, priv_ctx, NULL);
7805                 mutex_unlock(&reporter->dump_lock);
7806         }
7807
7808         if (!reporter->auto_recover)
7809                 return 0;
7810
7811         devl_lock(devlink);
7812         ret = devlink_health_reporter_recover(reporter, priv_ctx, NULL);
7813         devl_unlock(devlink);
7814
7815         return ret;
7816 }
7817 EXPORT_SYMBOL_GPL(devlink_health_report);
7818
7819 static struct devlink_health_reporter *
7820 devlink_health_reporter_get_from_attrs(struct devlink *devlink,
7821                                        struct nlattr **attrs)
7822 {
7823         struct devlink_health_reporter *reporter;
7824         struct devlink_port *devlink_port;
7825         char *reporter_name;
7826
7827         if (!attrs[DEVLINK_ATTR_HEALTH_REPORTER_NAME])
7828                 return NULL;
7829
7830         reporter_name = nla_data(attrs[DEVLINK_ATTR_HEALTH_REPORTER_NAME]);
7831         devlink_port = devlink_port_get_from_attrs(devlink, attrs);
7832         if (IS_ERR(devlink_port)) {
7833                 mutex_lock(&devlink->reporters_lock);
7834                 reporter = devlink_health_reporter_find_by_name(devlink, reporter_name);
7835                 if (reporter)
7836                         refcount_inc(&reporter->refcount);
7837                 mutex_unlock(&devlink->reporters_lock);
7838         } else {
7839                 mutex_lock(&devlink_port->reporters_lock);
7840                 reporter = devlink_port_health_reporter_find_by_name(devlink_port, reporter_name);
7841                 if (reporter)
7842                         refcount_inc(&reporter->refcount);
7843                 mutex_unlock(&devlink_port->reporters_lock);
7844         }
7845
7846         return reporter;
7847 }
7848
7849 static struct devlink_health_reporter *
7850 devlink_health_reporter_get_from_info(struct devlink *devlink,
7851                                       struct genl_info *info)
7852 {
7853         return devlink_health_reporter_get_from_attrs(devlink, info->attrs);
7854 }
7855
7856 static struct devlink_health_reporter *
7857 devlink_health_reporter_get_from_cb(struct netlink_callback *cb)
7858 {
7859         const struct genl_dumpit_info *info = genl_dumpit_info(cb);
7860         struct devlink_health_reporter *reporter;
7861         struct nlattr **attrs = info->attrs;
7862         struct devlink *devlink;
7863
7864         devlink = devlink_get_from_attrs(sock_net(cb->skb->sk), attrs);
7865         if (IS_ERR(devlink))
7866                 return NULL;
7867
7868         reporter = devlink_health_reporter_get_from_attrs(devlink, attrs);
7869         devlink_put(devlink);
7870         return reporter;
7871 }
7872
7873 void
7874 devlink_health_reporter_state_update(struct devlink_health_reporter *reporter,
7875                                      enum devlink_health_reporter_state state)
7876 {
7877         if (WARN_ON(state != DEVLINK_HEALTH_REPORTER_STATE_HEALTHY &&
7878                     state != DEVLINK_HEALTH_REPORTER_STATE_ERROR))
7879                 return;
7880
7881         if (reporter->health_state == state)
7882                 return;
7883
7884         reporter->health_state = state;
7885         trace_devlink_health_reporter_state_update(reporter->devlink,
7886                                                    reporter->ops->name, state);
7887         devlink_recover_notify(reporter, DEVLINK_CMD_HEALTH_REPORTER_RECOVER);
7888 }
7889 EXPORT_SYMBOL_GPL(devlink_health_reporter_state_update);
7890
7891 static int devlink_nl_cmd_health_reporter_get_doit(struct sk_buff *skb,
7892                                                    struct genl_info *info)
7893 {
7894         struct devlink *devlink = info->user_ptr[0];
7895         struct devlink_health_reporter *reporter;
7896         struct sk_buff *msg;
7897         int err;
7898
7899         reporter = devlink_health_reporter_get_from_info(devlink, info);
7900         if (!reporter)
7901                 return -EINVAL;
7902
7903         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
7904         if (!msg) {
7905                 err = -ENOMEM;
7906                 goto out;
7907         }
7908
7909         err = devlink_nl_health_reporter_fill(msg, reporter,
7910                                               DEVLINK_CMD_HEALTH_REPORTER_GET,
7911                                               info->snd_portid, info->snd_seq,
7912                                               0);
7913         if (err) {
7914                 nlmsg_free(msg);
7915                 goto out;
7916         }
7917
7918         err = genlmsg_reply(msg, info);
7919 out:
7920         devlink_health_reporter_put(reporter);
7921         return err;
7922 }
7923
7924 static int
7925 devlink_nl_cmd_health_reporter_get_dumpit(struct sk_buff *msg,
7926                                           struct netlink_callback *cb)
7927 {
7928         struct devlink_nl_dump_state *state = devlink_dump_state(cb);
7929         struct devlink_health_reporter *reporter;
7930         unsigned long index, port_index;
7931         struct devlink_port *port;
7932         struct devlink *devlink;
7933         int start = state->idx;
7934         int idx = 0;
7935         int err;
7936
7937         devlinks_xa_for_each_registered_get(sock_net(msg->sk), index, devlink) {
7938                 mutex_lock(&devlink->reporters_lock);
7939                 list_for_each_entry(reporter, &devlink->reporter_list,
7940                                     list) {
7941                         if (idx < start) {
7942                                 idx++;
7943                                 continue;
7944                         }
7945                         err = devlink_nl_health_reporter_fill(
7946                                 msg, reporter, DEVLINK_CMD_HEALTH_REPORTER_GET,
7947                                 NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
7948                                 NLM_F_MULTI);
7949                         if (err) {
7950                                 mutex_unlock(&devlink->reporters_lock);
7951                                 devlink_put(devlink);
7952                                 goto out;
7953                         }
7954                         idx++;
7955                 }
7956                 mutex_unlock(&devlink->reporters_lock);
7957                 devlink_put(devlink);
7958         }
7959
7960         devlinks_xa_for_each_registered_get(sock_net(msg->sk), index, devlink) {
7961                 devl_lock(devlink);
7962                 xa_for_each(&devlink->ports, port_index, port) {
7963                         mutex_lock(&port->reporters_lock);
7964                         list_for_each_entry(reporter, &port->reporter_list, list) {
7965                                 if (idx < start) {
7966                                         idx++;
7967                                         continue;
7968                                 }
7969                                 err = devlink_nl_health_reporter_fill(
7970                                         msg, reporter,
7971                                         DEVLINK_CMD_HEALTH_REPORTER_GET,
7972                                         NETLINK_CB(cb->skb).portid,
7973                                         cb->nlh->nlmsg_seq, NLM_F_MULTI);
7974                                 if (err) {
7975                                         mutex_unlock(&port->reporters_lock);
7976                                         devl_unlock(devlink);
7977                                         devlink_put(devlink);
7978                                         goto out;
7979                                 }
7980                                 idx++;
7981                         }
7982                         mutex_unlock(&port->reporters_lock);
7983                 }
7984                 devl_unlock(devlink);
7985                 devlink_put(devlink);
7986         }
7987 out:
7988         state->idx = idx;
7989         return msg->len;
7990 }
7991
7992 static int
7993 devlink_nl_cmd_health_reporter_set_doit(struct sk_buff *skb,
7994                                         struct genl_info *info)
7995 {
7996         struct devlink *devlink = info->user_ptr[0];
7997         struct devlink_health_reporter *reporter;
7998         int err;
7999
8000         reporter = devlink_health_reporter_get_from_info(devlink, info);
8001         if (!reporter)
8002                 return -EINVAL;
8003
8004         if (!reporter->ops->recover &&
8005             (info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD] ||
8006              info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER])) {
8007                 err = -EOPNOTSUPP;
8008                 goto out;
8009         }
8010         if (!reporter->ops->dump &&
8011             info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_DUMP]) {
8012                 err = -EOPNOTSUPP;
8013                 goto out;
8014         }
8015
8016         if (info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD])
8017                 reporter->graceful_period =
8018                         nla_get_u64(info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD]);
8019
8020         if (info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER])
8021                 reporter->auto_recover =
8022                         nla_get_u8(info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER]);
8023
8024         if (info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_DUMP])
8025                 reporter->auto_dump =
8026                 nla_get_u8(info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_DUMP]);
8027
8028         devlink_health_reporter_put(reporter);
8029         return 0;
8030 out:
8031         devlink_health_reporter_put(reporter);
8032         return err;
8033 }
8034
8035 static int devlink_nl_cmd_health_reporter_recover_doit(struct sk_buff *skb,
8036                                                        struct genl_info *info)
8037 {
8038         struct devlink *devlink = info->user_ptr[0];
8039         struct devlink_health_reporter *reporter;
8040         int err;
8041
8042         reporter = devlink_health_reporter_get_from_info(devlink, info);
8043         if (!reporter)
8044                 return -EINVAL;
8045
8046         err = devlink_health_reporter_recover(reporter, NULL, info->extack);
8047
8048         devlink_health_reporter_put(reporter);
8049         return err;
8050 }
8051
8052 static int devlink_nl_cmd_health_reporter_diagnose_doit(struct sk_buff *skb,
8053                                                         struct genl_info *info)
8054 {
8055         struct devlink *devlink = info->user_ptr[0];
8056         struct devlink_health_reporter *reporter;
8057         struct devlink_fmsg *fmsg;
8058         int err;
8059
8060         reporter = devlink_health_reporter_get_from_info(devlink, info);
8061         if (!reporter)
8062                 return -EINVAL;
8063
8064         if (!reporter->ops->diagnose) {
8065                 devlink_health_reporter_put(reporter);
8066                 return -EOPNOTSUPP;
8067         }
8068
8069         fmsg = devlink_fmsg_alloc();
8070         if (!fmsg) {
8071                 devlink_health_reporter_put(reporter);
8072                 return -ENOMEM;
8073         }
8074
8075         err = devlink_fmsg_obj_nest_start(fmsg);
8076         if (err)
8077                 goto out;
8078
8079         err = reporter->ops->diagnose(reporter, fmsg, info->extack);
8080         if (err)
8081                 goto out;
8082
8083         err = devlink_fmsg_obj_nest_end(fmsg);
8084         if (err)
8085                 goto out;
8086
8087         err = devlink_fmsg_snd(fmsg, info,
8088                                DEVLINK_CMD_HEALTH_REPORTER_DIAGNOSE, 0);
8089
8090 out:
8091         devlink_fmsg_free(fmsg);
8092         devlink_health_reporter_put(reporter);
8093         return err;
8094 }
8095
8096 static int
8097 devlink_nl_cmd_health_reporter_dump_get_dumpit(struct sk_buff *skb,
8098                                                struct netlink_callback *cb)
8099 {
8100         struct devlink_nl_dump_state *state = devlink_dump_state(cb);
8101         struct devlink_health_reporter *reporter;
8102         int err;
8103
8104         reporter = devlink_health_reporter_get_from_cb(cb);
8105         if (!reporter)
8106                 return -EINVAL;
8107
8108         if (!reporter->ops->dump) {
8109                 err = -EOPNOTSUPP;
8110                 goto out;
8111         }
8112         mutex_lock(&reporter->dump_lock);
8113         if (!state->idx) {
8114                 err = devlink_health_do_dump(reporter, NULL, cb->extack);
8115                 if (err)
8116                         goto unlock;
8117                 state->dump_ts = reporter->dump_ts;
8118         }
8119         if (!reporter->dump_fmsg || state->dump_ts != reporter->dump_ts) {
8120                 NL_SET_ERR_MSG_MOD(cb->extack, "Dump trampled, please retry");
8121                 err = -EAGAIN;
8122                 goto unlock;
8123         }
8124
8125         err = devlink_fmsg_dumpit(reporter->dump_fmsg, skb, cb,
8126                                   DEVLINK_CMD_HEALTH_REPORTER_DUMP_GET);
8127 unlock:
8128         mutex_unlock(&reporter->dump_lock);
8129 out:
8130         devlink_health_reporter_put(reporter);
8131         return err;
8132 }
8133
8134 static int
8135 devlink_nl_cmd_health_reporter_dump_clear_doit(struct sk_buff *skb,
8136                                                struct genl_info *info)
8137 {
8138         struct devlink *devlink = info->user_ptr[0];
8139         struct devlink_health_reporter *reporter;
8140
8141         reporter = devlink_health_reporter_get_from_info(devlink, info);
8142         if (!reporter)
8143                 return -EINVAL;
8144
8145         if (!reporter->ops->dump) {
8146                 devlink_health_reporter_put(reporter);
8147                 return -EOPNOTSUPP;
8148         }
8149
8150         mutex_lock(&reporter->dump_lock);
8151         devlink_health_dump_clear(reporter);
8152         mutex_unlock(&reporter->dump_lock);
8153         devlink_health_reporter_put(reporter);
8154         return 0;
8155 }
8156
8157 static int devlink_nl_cmd_health_reporter_test_doit(struct sk_buff *skb,
8158                                                     struct genl_info *info)
8159 {
8160         struct devlink *devlink = info->user_ptr[0];
8161         struct devlink_health_reporter *reporter;
8162         int err;
8163
8164         reporter = devlink_health_reporter_get_from_info(devlink, info);
8165         if (!reporter)
8166                 return -EINVAL;
8167
8168         if (!reporter->ops->test) {
8169                 devlink_health_reporter_put(reporter);
8170                 return -EOPNOTSUPP;
8171         }
8172
8173         err = reporter->ops->test(reporter, info->extack);
8174
8175         devlink_health_reporter_put(reporter);
8176         return err;
8177 }
8178
8179 struct devlink_stats {
8180         u64_stats_t rx_bytes;
8181         u64_stats_t rx_packets;
8182         struct u64_stats_sync syncp;
8183 };
8184
8185 /**
8186  * struct devlink_trap_policer_item - Packet trap policer attributes.
8187  * @policer: Immutable packet trap policer attributes.
8188  * @rate: Rate in packets / sec.
8189  * @burst: Burst size in packets.
8190  * @list: trap_policer_list member.
8191  *
8192  * Describes packet trap policer attributes. Created by devlink during trap
8193  * policer registration.
8194  */
8195 struct devlink_trap_policer_item {
8196         const struct devlink_trap_policer *policer;
8197         u64 rate;
8198         u64 burst;
8199         struct list_head list;
8200 };
8201
8202 /**
8203  * struct devlink_trap_group_item - Packet trap group attributes.
8204  * @group: Immutable packet trap group attributes.
8205  * @policer_item: Associated policer item. Can be NULL.
8206  * @list: trap_group_list member.
8207  * @stats: Trap group statistics.
8208  *
8209  * Describes packet trap group attributes. Created by devlink during trap
8210  * group registration.
8211  */
8212 struct devlink_trap_group_item {
8213         const struct devlink_trap_group *group;
8214         struct devlink_trap_policer_item *policer_item;
8215         struct list_head list;
8216         struct devlink_stats __percpu *stats;
8217 };
8218
8219 /**
8220  * struct devlink_trap_item - Packet trap attributes.
8221  * @trap: Immutable packet trap attributes.
8222  * @group_item: Associated group item.
8223  * @list: trap_list member.
8224  * @action: Trap action.
8225  * @stats: Trap statistics.
8226  * @priv: Driver private information.
8227  *
8228  * Describes both mutable and immutable packet trap attributes. Created by
8229  * devlink during trap registration and used for all trap related operations.
8230  */
8231 struct devlink_trap_item {
8232         const struct devlink_trap *trap;
8233         struct devlink_trap_group_item *group_item;
8234         struct list_head list;
8235         enum devlink_trap_action action;
8236         struct devlink_stats __percpu *stats;
8237         void *priv;
8238 };
8239
8240 static struct devlink_trap_policer_item *
8241 devlink_trap_policer_item_lookup(struct devlink *devlink, u32 id)
8242 {
8243         struct devlink_trap_policer_item *policer_item;
8244
8245         list_for_each_entry(policer_item, &devlink->trap_policer_list, list) {
8246                 if (policer_item->policer->id == id)
8247                         return policer_item;
8248         }
8249
8250         return NULL;
8251 }
8252
8253 static struct devlink_trap_item *
8254 devlink_trap_item_lookup(struct devlink *devlink, const char *name)
8255 {
8256         struct devlink_trap_item *trap_item;
8257
8258         list_for_each_entry(trap_item, &devlink->trap_list, list) {
8259                 if (!strcmp(trap_item->trap->name, name))
8260                         return trap_item;
8261         }
8262
8263         return NULL;
8264 }
8265
8266 static struct devlink_trap_item *
8267 devlink_trap_item_get_from_info(struct devlink *devlink,
8268                                 struct genl_info *info)
8269 {
8270         struct nlattr *attr;
8271
8272         if (!info->attrs[DEVLINK_ATTR_TRAP_NAME])
8273                 return NULL;
8274         attr = info->attrs[DEVLINK_ATTR_TRAP_NAME];
8275
8276         return devlink_trap_item_lookup(devlink, nla_data(attr));
8277 }
8278
8279 static int
8280 devlink_trap_action_get_from_info(struct genl_info *info,
8281                                   enum devlink_trap_action *p_trap_action)
8282 {
8283         u8 val;
8284
8285         val = nla_get_u8(info->attrs[DEVLINK_ATTR_TRAP_ACTION]);
8286         switch (val) {
8287         case DEVLINK_TRAP_ACTION_DROP:
8288         case DEVLINK_TRAP_ACTION_TRAP:
8289         case DEVLINK_TRAP_ACTION_MIRROR:
8290                 *p_trap_action = val;
8291                 break;
8292         default:
8293                 return -EINVAL;
8294         }
8295
8296         return 0;
8297 }
8298
8299 static int devlink_trap_metadata_put(struct sk_buff *msg,
8300                                      const struct devlink_trap *trap)
8301 {
8302         struct nlattr *attr;
8303
8304         attr = nla_nest_start(msg, DEVLINK_ATTR_TRAP_METADATA);
8305         if (!attr)
8306                 return -EMSGSIZE;
8307
8308         if ((trap->metadata_cap & DEVLINK_TRAP_METADATA_TYPE_F_IN_PORT) &&
8309             nla_put_flag(msg, DEVLINK_ATTR_TRAP_METADATA_TYPE_IN_PORT))
8310                 goto nla_put_failure;
8311         if ((trap->metadata_cap & DEVLINK_TRAP_METADATA_TYPE_F_FA_COOKIE) &&
8312             nla_put_flag(msg, DEVLINK_ATTR_TRAP_METADATA_TYPE_FA_COOKIE))
8313                 goto nla_put_failure;
8314
8315         nla_nest_end(msg, attr);
8316
8317         return 0;
8318
8319 nla_put_failure:
8320         nla_nest_cancel(msg, attr);
8321         return -EMSGSIZE;
8322 }
8323
8324 static void devlink_trap_stats_read(struct devlink_stats __percpu *trap_stats,
8325                                     struct devlink_stats *stats)
8326 {
8327         int i;
8328
8329         memset(stats, 0, sizeof(*stats));
8330         for_each_possible_cpu(i) {
8331                 struct devlink_stats *cpu_stats;
8332                 u64 rx_packets, rx_bytes;
8333                 unsigned int start;
8334
8335                 cpu_stats = per_cpu_ptr(trap_stats, i);
8336                 do {
8337                         start = u64_stats_fetch_begin(&cpu_stats->syncp);
8338                         rx_packets = u64_stats_read(&cpu_stats->rx_packets);
8339                         rx_bytes = u64_stats_read(&cpu_stats->rx_bytes);
8340                 } while (u64_stats_fetch_retry(&cpu_stats->syncp, start));
8341
8342                 u64_stats_add(&stats->rx_packets, rx_packets);
8343                 u64_stats_add(&stats->rx_bytes, rx_bytes);
8344         }
8345 }
8346
8347 static int
8348 devlink_trap_group_stats_put(struct sk_buff *msg,
8349                              struct devlink_stats __percpu *trap_stats)
8350 {
8351         struct devlink_stats stats;
8352         struct nlattr *attr;
8353
8354         devlink_trap_stats_read(trap_stats, &stats);
8355
8356         attr = nla_nest_start(msg, DEVLINK_ATTR_STATS);
8357         if (!attr)
8358                 return -EMSGSIZE;
8359
8360         if (nla_put_u64_64bit(msg, DEVLINK_ATTR_STATS_RX_PACKETS,
8361                               u64_stats_read(&stats.rx_packets),
8362                               DEVLINK_ATTR_PAD))
8363                 goto nla_put_failure;
8364
8365         if (nla_put_u64_64bit(msg, DEVLINK_ATTR_STATS_RX_BYTES,
8366                               u64_stats_read(&stats.rx_bytes),
8367                               DEVLINK_ATTR_PAD))
8368                 goto nla_put_failure;
8369
8370         nla_nest_end(msg, attr);
8371
8372         return 0;
8373
8374 nla_put_failure:
8375         nla_nest_cancel(msg, attr);
8376         return -EMSGSIZE;
8377 }
8378
8379 static int devlink_trap_stats_put(struct sk_buff *msg, struct devlink *devlink,
8380                                   const struct devlink_trap_item *trap_item)
8381 {
8382         struct devlink_stats stats;
8383         struct nlattr *attr;
8384         u64 drops = 0;
8385         int err;
8386
8387         if (devlink->ops->trap_drop_counter_get) {
8388                 err = devlink->ops->trap_drop_counter_get(devlink,
8389                                                           trap_item->trap,
8390                                                           &drops);
8391                 if (err)
8392                         return err;
8393         }
8394
8395         devlink_trap_stats_read(trap_item->stats, &stats);
8396
8397         attr = nla_nest_start(msg, DEVLINK_ATTR_STATS);
8398         if (!attr)
8399                 return -EMSGSIZE;
8400
8401         if (devlink->ops->trap_drop_counter_get &&
8402             nla_put_u64_64bit(msg, DEVLINK_ATTR_STATS_RX_DROPPED, drops,
8403                               DEVLINK_ATTR_PAD))
8404                 goto nla_put_failure;
8405
8406         if (nla_put_u64_64bit(msg, DEVLINK_ATTR_STATS_RX_PACKETS,
8407                               u64_stats_read(&stats.rx_packets),
8408                               DEVLINK_ATTR_PAD))
8409                 goto nla_put_failure;
8410
8411         if (nla_put_u64_64bit(msg, DEVLINK_ATTR_STATS_RX_BYTES,
8412                               u64_stats_read(&stats.rx_bytes),
8413                               DEVLINK_ATTR_PAD))
8414                 goto nla_put_failure;
8415
8416         nla_nest_end(msg, attr);
8417
8418         return 0;
8419
8420 nla_put_failure:
8421         nla_nest_cancel(msg, attr);
8422         return -EMSGSIZE;
8423 }
8424
8425 static int devlink_nl_trap_fill(struct sk_buff *msg, struct devlink *devlink,
8426                                 const struct devlink_trap_item *trap_item,
8427                                 enum devlink_command cmd, u32 portid, u32 seq,
8428                                 int flags)
8429 {
8430         struct devlink_trap_group_item *group_item = trap_item->group_item;
8431         void *hdr;
8432         int err;
8433
8434         hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
8435         if (!hdr)
8436                 return -EMSGSIZE;
8437
8438         if (devlink_nl_put_handle(msg, devlink))
8439                 goto nla_put_failure;
8440
8441         if (nla_put_string(msg, DEVLINK_ATTR_TRAP_GROUP_NAME,
8442                            group_item->group->name))
8443                 goto nla_put_failure;
8444
8445         if (nla_put_string(msg, DEVLINK_ATTR_TRAP_NAME, trap_item->trap->name))
8446                 goto nla_put_failure;
8447
8448         if (nla_put_u8(msg, DEVLINK_ATTR_TRAP_TYPE, trap_item->trap->type))
8449                 goto nla_put_failure;
8450
8451         if (trap_item->trap->generic &&
8452             nla_put_flag(msg, DEVLINK_ATTR_TRAP_GENERIC))
8453                 goto nla_put_failure;
8454
8455         if (nla_put_u8(msg, DEVLINK_ATTR_TRAP_ACTION, trap_item->action))
8456                 goto nla_put_failure;
8457
8458         err = devlink_trap_metadata_put(msg, trap_item->trap);
8459         if (err)
8460                 goto nla_put_failure;
8461
8462         err = devlink_trap_stats_put(msg, devlink, trap_item);
8463         if (err)
8464                 goto nla_put_failure;
8465
8466         genlmsg_end(msg, hdr);
8467
8468         return 0;
8469
8470 nla_put_failure:
8471         genlmsg_cancel(msg, hdr);
8472         return -EMSGSIZE;
8473 }
8474
8475 static int devlink_nl_cmd_trap_get_doit(struct sk_buff *skb,
8476                                         struct genl_info *info)
8477 {
8478         struct netlink_ext_ack *extack = info->extack;
8479         struct devlink *devlink = info->user_ptr[0];
8480         struct devlink_trap_item *trap_item;
8481         struct sk_buff *msg;
8482         int err;
8483
8484         if (list_empty(&devlink->trap_list))
8485                 return -EOPNOTSUPP;
8486
8487         trap_item = devlink_trap_item_get_from_info(devlink, info);
8488         if (!trap_item) {
8489                 NL_SET_ERR_MSG_MOD(extack, "Device did not register this trap");
8490                 return -ENOENT;
8491         }
8492
8493         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
8494         if (!msg)
8495                 return -ENOMEM;
8496
8497         err = devlink_nl_trap_fill(msg, devlink, trap_item,
8498                                    DEVLINK_CMD_TRAP_NEW, info->snd_portid,
8499                                    info->snd_seq, 0);
8500         if (err)
8501                 goto err_trap_fill;
8502
8503         return genlmsg_reply(msg, info);
8504
8505 err_trap_fill:
8506         nlmsg_free(msg);
8507         return err;
8508 }
8509
8510 static int devlink_nl_cmd_trap_get_dumpit(struct sk_buff *msg,
8511                                           struct netlink_callback *cb)
8512 {
8513         struct devlink_nl_dump_state *state = devlink_dump_state(cb);
8514         struct devlink_trap_item *trap_item;
8515         struct devlink *devlink;
8516         int start = state->idx;
8517         unsigned long index;
8518         int idx = 0;
8519         int err;
8520
8521         devlinks_xa_for_each_registered_get(sock_net(msg->sk), index, devlink) {
8522                 devl_lock(devlink);
8523                 list_for_each_entry(trap_item, &devlink->trap_list, list) {
8524                         if (idx < start) {
8525                                 idx++;
8526                                 continue;
8527                         }
8528                         err = devlink_nl_trap_fill(msg, devlink, trap_item,
8529                                                    DEVLINK_CMD_TRAP_NEW,
8530                                                    NETLINK_CB(cb->skb).portid,
8531                                                    cb->nlh->nlmsg_seq,
8532                                                    NLM_F_MULTI);
8533                         if (err) {
8534                                 devl_unlock(devlink);
8535                                 devlink_put(devlink);
8536                                 goto out;
8537                         }
8538                         idx++;
8539                 }
8540                 devl_unlock(devlink);
8541                 devlink_put(devlink);
8542         }
8543 out:
8544         state->idx = idx;
8545         return msg->len;
8546 }
8547
8548 static int __devlink_trap_action_set(struct devlink *devlink,
8549                                      struct devlink_trap_item *trap_item,
8550                                      enum devlink_trap_action trap_action,
8551                                      struct netlink_ext_ack *extack)
8552 {
8553         int err;
8554
8555         if (trap_item->action != trap_action &&
8556             trap_item->trap->type != DEVLINK_TRAP_TYPE_DROP) {
8557                 NL_SET_ERR_MSG_MOD(extack, "Cannot change action of non-drop traps. Skipping");
8558                 return 0;
8559         }
8560
8561         err = devlink->ops->trap_action_set(devlink, trap_item->trap,
8562                                             trap_action, extack);
8563         if (err)
8564                 return err;
8565
8566         trap_item->action = trap_action;
8567
8568         return 0;
8569 }
8570
8571 static int devlink_trap_action_set(struct devlink *devlink,
8572                                    struct devlink_trap_item *trap_item,
8573                                    struct genl_info *info)
8574 {
8575         enum devlink_trap_action trap_action;
8576         int err;
8577
8578         if (!info->attrs[DEVLINK_ATTR_TRAP_ACTION])
8579                 return 0;
8580
8581         err = devlink_trap_action_get_from_info(info, &trap_action);
8582         if (err) {
8583                 NL_SET_ERR_MSG_MOD(info->extack, "Invalid trap action");
8584                 return -EINVAL;
8585         }
8586
8587         return __devlink_trap_action_set(devlink, trap_item, trap_action,
8588                                          info->extack);
8589 }
8590
8591 static int devlink_nl_cmd_trap_set_doit(struct sk_buff *skb,
8592                                         struct genl_info *info)
8593 {
8594         struct netlink_ext_ack *extack = info->extack;
8595         struct devlink *devlink = info->user_ptr[0];
8596         struct devlink_trap_item *trap_item;
8597
8598         if (list_empty(&devlink->trap_list))
8599                 return -EOPNOTSUPP;
8600
8601         trap_item = devlink_trap_item_get_from_info(devlink, info);
8602         if (!trap_item) {
8603                 NL_SET_ERR_MSG_MOD(extack, "Device did not register this trap");
8604                 return -ENOENT;
8605         }
8606
8607         return devlink_trap_action_set(devlink, trap_item, info);
8608 }
8609
8610 static struct devlink_trap_group_item *
8611 devlink_trap_group_item_lookup(struct devlink *devlink, const char *name)
8612 {
8613         struct devlink_trap_group_item *group_item;
8614
8615         list_for_each_entry(group_item, &devlink->trap_group_list, list) {
8616                 if (!strcmp(group_item->group->name, name))
8617                         return group_item;
8618         }
8619
8620         return NULL;
8621 }
8622
8623 static struct devlink_trap_group_item *
8624 devlink_trap_group_item_lookup_by_id(struct devlink *devlink, u16 id)
8625 {
8626         struct devlink_trap_group_item *group_item;
8627
8628         list_for_each_entry(group_item, &devlink->trap_group_list, list) {
8629                 if (group_item->group->id == id)
8630                         return group_item;
8631         }
8632
8633         return NULL;
8634 }
8635
8636 static struct devlink_trap_group_item *
8637 devlink_trap_group_item_get_from_info(struct devlink *devlink,
8638                                       struct genl_info *info)
8639 {
8640         char *name;
8641
8642         if (!info->attrs[DEVLINK_ATTR_TRAP_GROUP_NAME])
8643                 return NULL;
8644         name = nla_data(info->attrs[DEVLINK_ATTR_TRAP_GROUP_NAME]);
8645
8646         return devlink_trap_group_item_lookup(devlink, name);
8647 }
8648
8649 static int
8650 devlink_nl_trap_group_fill(struct sk_buff *msg, struct devlink *devlink,
8651                            const struct devlink_trap_group_item *group_item,
8652                            enum devlink_command cmd, u32 portid, u32 seq,
8653                            int flags)
8654 {
8655         void *hdr;
8656         int err;
8657
8658         hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
8659         if (!hdr)
8660                 return -EMSGSIZE;
8661
8662         if (devlink_nl_put_handle(msg, devlink))
8663                 goto nla_put_failure;
8664
8665         if (nla_put_string(msg, DEVLINK_ATTR_TRAP_GROUP_NAME,
8666                            group_item->group->name))
8667                 goto nla_put_failure;
8668
8669         if (group_item->group->generic &&
8670             nla_put_flag(msg, DEVLINK_ATTR_TRAP_GENERIC))
8671                 goto nla_put_failure;
8672
8673         if (group_item->policer_item &&
8674             nla_put_u32(msg, DEVLINK_ATTR_TRAP_POLICER_ID,
8675                         group_item->policer_item->policer->id))
8676                 goto nla_put_failure;
8677
8678         err = devlink_trap_group_stats_put(msg, group_item->stats);
8679         if (err)
8680                 goto nla_put_failure;
8681
8682         genlmsg_end(msg, hdr);
8683
8684         return 0;
8685
8686 nla_put_failure:
8687         genlmsg_cancel(msg, hdr);
8688         return -EMSGSIZE;
8689 }
8690
8691 static int devlink_nl_cmd_trap_group_get_doit(struct sk_buff *skb,
8692                                               struct genl_info *info)
8693 {
8694         struct netlink_ext_ack *extack = info->extack;
8695         struct devlink *devlink = info->user_ptr[0];
8696         struct devlink_trap_group_item *group_item;
8697         struct sk_buff *msg;
8698         int err;
8699
8700         if (list_empty(&devlink->trap_group_list))
8701                 return -EOPNOTSUPP;
8702
8703         group_item = devlink_trap_group_item_get_from_info(devlink, info);
8704         if (!group_item) {
8705                 NL_SET_ERR_MSG_MOD(extack, "Device did not register this trap group");
8706                 return -ENOENT;
8707         }
8708
8709         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
8710         if (!msg)
8711                 return -ENOMEM;
8712
8713         err = devlink_nl_trap_group_fill(msg, devlink, group_item,
8714                                          DEVLINK_CMD_TRAP_GROUP_NEW,
8715                                          info->snd_portid, info->snd_seq, 0);
8716         if (err)
8717                 goto err_trap_group_fill;
8718
8719         return genlmsg_reply(msg, info);
8720
8721 err_trap_group_fill:
8722         nlmsg_free(msg);
8723         return err;
8724 }
8725
8726 static int devlink_nl_cmd_trap_group_get_dumpit(struct sk_buff *msg,
8727                                                 struct netlink_callback *cb)
8728 {
8729         struct devlink_nl_dump_state *state = devlink_dump_state(cb);
8730         enum devlink_command cmd = DEVLINK_CMD_TRAP_GROUP_NEW;
8731         struct devlink_trap_group_item *group_item;
8732         u32 portid = NETLINK_CB(cb->skb).portid;
8733         struct devlink *devlink;
8734         int start = state->idx;
8735         unsigned long index;
8736         int idx = 0;
8737         int err;
8738
8739         devlinks_xa_for_each_registered_get(sock_net(msg->sk), index, devlink) {
8740                 devl_lock(devlink);
8741                 list_for_each_entry(group_item, &devlink->trap_group_list,
8742                                     list) {
8743                         if (idx < start) {
8744                                 idx++;
8745                                 continue;
8746                         }
8747                         err = devlink_nl_trap_group_fill(msg, devlink,
8748                                                          group_item, cmd,
8749                                                          portid,
8750                                                          cb->nlh->nlmsg_seq,
8751                                                          NLM_F_MULTI);
8752                         if (err) {
8753                                 devl_unlock(devlink);
8754                                 devlink_put(devlink);
8755                                 goto out;
8756                         }
8757                         idx++;
8758                 }
8759                 devl_unlock(devlink);
8760                 devlink_put(devlink);
8761         }
8762 out:
8763         state->idx = idx;
8764         return msg->len;
8765 }
8766
8767 static int
8768 __devlink_trap_group_action_set(struct devlink *devlink,
8769                                 struct devlink_trap_group_item *group_item,
8770                                 enum devlink_trap_action trap_action,
8771                                 struct netlink_ext_ack *extack)
8772 {
8773         const char *group_name = group_item->group->name;
8774         struct devlink_trap_item *trap_item;
8775         int err;
8776
8777         if (devlink->ops->trap_group_action_set) {
8778                 err = devlink->ops->trap_group_action_set(devlink, group_item->group,
8779                                                           trap_action, extack);
8780                 if (err)
8781                         return err;
8782
8783                 list_for_each_entry(trap_item, &devlink->trap_list, list) {
8784                         if (strcmp(trap_item->group_item->group->name, group_name))
8785                                 continue;
8786                         if (trap_item->action != trap_action &&
8787                             trap_item->trap->type != DEVLINK_TRAP_TYPE_DROP)
8788                                 continue;
8789                         trap_item->action = trap_action;
8790                 }
8791
8792                 return 0;
8793         }
8794
8795         list_for_each_entry(trap_item, &devlink->trap_list, list) {
8796                 if (strcmp(trap_item->group_item->group->name, group_name))
8797                         continue;
8798                 err = __devlink_trap_action_set(devlink, trap_item,
8799                                                 trap_action, extack);
8800                 if (err)
8801                         return err;
8802         }
8803
8804         return 0;
8805 }
8806
8807 static int
8808 devlink_trap_group_action_set(struct devlink *devlink,
8809                               struct devlink_trap_group_item *group_item,
8810                               struct genl_info *info, bool *p_modified)
8811 {
8812         enum devlink_trap_action trap_action;
8813         int err;
8814
8815         if (!info->attrs[DEVLINK_ATTR_TRAP_ACTION])
8816                 return 0;
8817
8818         err = devlink_trap_action_get_from_info(info, &trap_action);
8819         if (err) {
8820                 NL_SET_ERR_MSG_MOD(info->extack, "Invalid trap action");
8821                 return -EINVAL;
8822         }
8823
8824         err = __devlink_trap_group_action_set(devlink, group_item, trap_action,
8825                                               info->extack);
8826         if (err)
8827                 return err;
8828
8829         *p_modified = true;
8830
8831         return 0;
8832 }
8833
8834 static int devlink_trap_group_set(struct devlink *devlink,
8835                                   struct devlink_trap_group_item *group_item,
8836                                   struct genl_info *info)
8837 {
8838         struct devlink_trap_policer_item *policer_item;
8839         struct netlink_ext_ack *extack = info->extack;
8840         const struct devlink_trap_policer *policer;
8841         struct nlattr **attrs = info->attrs;
8842         int err;
8843
8844         if (!attrs[DEVLINK_ATTR_TRAP_POLICER_ID])
8845                 return 0;
8846
8847         if (!devlink->ops->trap_group_set)
8848                 return -EOPNOTSUPP;
8849
8850         policer_item = group_item->policer_item;
8851         if (attrs[DEVLINK_ATTR_TRAP_POLICER_ID]) {
8852                 u32 policer_id;
8853
8854                 policer_id = nla_get_u32(attrs[DEVLINK_ATTR_TRAP_POLICER_ID]);
8855                 policer_item = devlink_trap_policer_item_lookup(devlink,
8856                                                                 policer_id);
8857                 if (policer_id && !policer_item) {
8858                         NL_SET_ERR_MSG_MOD(extack, "Device did not register this trap policer");
8859                         return -ENOENT;
8860                 }
8861         }
8862         policer = policer_item ? policer_item->policer : NULL;
8863
8864         err = devlink->ops->trap_group_set(devlink, group_item->group, policer,
8865                                            extack);
8866         if (err)
8867                 return err;
8868
8869         group_item->policer_item = policer_item;
8870
8871         return 0;
8872 }
8873
8874 static int devlink_nl_cmd_trap_group_set_doit(struct sk_buff *skb,
8875                                               struct genl_info *info)
8876 {
8877         struct netlink_ext_ack *extack = info->extack;
8878         struct devlink *devlink = info->user_ptr[0];
8879         struct devlink_trap_group_item *group_item;
8880         bool modified = false;
8881         int err;
8882
8883         if (list_empty(&devlink->trap_group_list))
8884                 return -EOPNOTSUPP;
8885
8886         group_item = devlink_trap_group_item_get_from_info(devlink, info);
8887         if (!group_item) {
8888                 NL_SET_ERR_MSG_MOD(extack, "Device did not register this trap group");
8889                 return -ENOENT;
8890         }
8891
8892         err = devlink_trap_group_action_set(devlink, group_item, info,
8893                                             &modified);
8894         if (err)
8895                 return err;
8896
8897         err = devlink_trap_group_set(devlink, group_item, info);
8898         if (err)
8899                 goto err_trap_group_set;
8900
8901         return 0;
8902
8903 err_trap_group_set:
8904         if (modified)
8905                 NL_SET_ERR_MSG_MOD(extack, "Trap group set failed, but some changes were committed already");
8906         return err;
8907 }
8908
8909 static struct devlink_trap_policer_item *
8910 devlink_trap_policer_item_get_from_info(struct devlink *devlink,
8911                                         struct genl_info *info)
8912 {
8913         u32 id;
8914
8915         if (!info->attrs[DEVLINK_ATTR_TRAP_POLICER_ID])
8916                 return NULL;
8917         id = nla_get_u32(info->attrs[DEVLINK_ATTR_TRAP_POLICER_ID]);
8918
8919         return devlink_trap_policer_item_lookup(devlink, id);
8920 }
8921
8922 static int
8923 devlink_trap_policer_stats_put(struct sk_buff *msg, struct devlink *devlink,
8924                                const struct devlink_trap_policer *policer)
8925 {
8926         struct nlattr *attr;
8927         u64 drops;
8928         int err;
8929
8930         if (!devlink->ops->trap_policer_counter_get)
8931                 return 0;
8932
8933         err = devlink->ops->trap_policer_counter_get(devlink, policer, &drops);
8934         if (err)
8935                 return err;
8936
8937         attr = nla_nest_start(msg, DEVLINK_ATTR_STATS);
8938         if (!attr)
8939                 return -EMSGSIZE;
8940
8941         if (nla_put_u64_64bit(msg, DEVLINK_ATTR_STATS_RX_DROPPED, drops,
8942                               DEVLINK_ATTR_PAD))
8943                 goto nla_put_failure;
8944
8945         nla_nest_end(msg, attr);
8946
8947         return 0;
8948
8949 nla_put_failure:
8950         nla_nest_cancel(msg, attr);
8951         return -EMSGSIZE;
8952 }
8953
8954 static int
8955 devlink_nl_trap_policer_fill(struct sk_buff *msg, struct devlink *devlink,
8956                              const struct devlink_trap_policer_item *policer_item,
8957                              enum devlink_command cmd, u32 portid, u32 seq,
8958                              int flags)
8959 {
8960         void *hdr;
8961         int err;
8962
8963         hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
8964         if (!hdr)
8965                 return -EMSGSIZE;
8966
8967         if (devlink_nl_put_handle(msg, devlink))
8968                 goto nla_put_failure;
8969
8970         if (nla_put_u32(msg, DEVLINK_ATTR_TRAP_POLICER_ID,
8971                         policer_item->policer->id))
8972                 goto nla_put_failure;
8973
8974         if (nla_put_u64_64bit(msg, DEVLINK_ATTR_TRAP_POLICER_RATE,
8975                               policer_item->rate, DEVLINK_ATTR_PAD))
8976                 goto nla_put_failure;
8977
8978         if (nla_put_u64_64bit(msg, DEVLINK_ATTR_TRAP_POLICER_BURST,
8979                               policer_item->burst, DEVLINK_ATTR_PAD))
8980                 goto nla_put_failure;
8981
8982         err = devlink_trap_policer_stats_put(msg, devlink,
8983                                              policer_item->policer);
8984         if (err)
8985                 goto nla_put_failure;
8986
8987         genlmsg_end(msg, hdr);
8988
8989         return 0;
8990
8991 nla_put_failure:
8992         genlmsg_cancel(msg, hdr);
8993         return -EMSGSIZE;
8994 }
8995
8996 static int devlink_nl_cmd_trap_policer_get_doit(struct sk_buff *skb,
8997                                                 struct genl_info *info)
8998 {
8999         struct devlink_trap_policer_item *policer_item;
9000         struct netlink_ext_ack *extack = info->extack;
9001         struct devlink *devlink = info->user_ptr[0];
9002         struct sk_buff *msg;
9003         int err;
9004
9005         if (list_empty(&devlink->trap_policer_list))
9006                 return -EOPNOTSUPP;
9007
9008         policer_item = devlink_trap_policer_item_get_from_info(devlink, info);
9009         if (!policer_item) {
9010                 NL_SET_ERR_MSG_MOD(extack, "Device did not register this trap policer");
9011                 return -ENOENT;
9012         }
9013
9014         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
9015         if (!msg)
9016                 return -ENOMEM;
9017
9018         err = devlink_nl_trap_policer_fill(msg, devlink, policer_item,
9019                                            DEVLINK_CMD_TRAP_POLICER_NEW,
9020                                            info->snd_portid, info->snd_seq, 0);
9021         if (err)
9022                 goto err_trap_policer_fill;
9023
9024         return genlmsg_reply(msg, info);
9025
9026 err_trap_policer_fill:
9027         nlmsg_free(msg);
9028         return err;
9029 }
9030
9031 static int devlink_nl_cmd_trap_policer_get_dumpit(struct sk_buff *msg,
9032                                                   struct netlink_callback *cb)
9033 {
9034         struct devlink_nl_dump_state *state = devlink_dump_state(cb);
9035         enum devlink_command cmd = DEVLINK_CMD_TRAP_POLICER_NEW;
9036         struct devlink_trap_policer_item *policer_item;
9037         u32 portid = NETLINK_CB(cb->skb).portid;
9038         struct devlink *devlink;
9039         int start = state->idx;
9040         unsigned long index;
9041         int idx = 0;
9042         int err;
9043
9044         devlinks_xa_for_each_registered_get(sock_net(msg->sk), index, devlink) {
9045                 devl_lock(devlink);
9046                 list_for_each_entry(policer_item, &devlink->trap_policer_list,
9047                                     list) {
9048                         if (idx < start) {
9049                                 idx++;
9050                                 continue;
9051                         }
9052                         err = devlink_nl_trap_policer_fill(msg, devlink,
9053                                                            policer_item, cmd,
9054                                                            portid,
9055                                                            cb->nlh->nlmsg_seq,
9056                                                            NLM_F_MULTI);
9057                         if (err) {
9058                                 devl_unlock(devlink);
9059                                 devlink_put(devlink);
9060                                 goto out;
9061                         }
9062                         idx++;
9063                 }
9064                 devl_unlock(devlink);
9065                 devlink_put(devlink);
9066         }
9067 out:
9068         state->idx = idx;
9069         return msg->len;
9070 }
9071
9072 static int
9073 devlink_trap_policer_set(struct devlink *devlink,
9074                          struct devlink_trap_policer_item *policer_item,
9075                          struct genl_info *info)
9076 {
9077         struct netlink_ext_ack *extack = info->extack;
9078         struct nlattr **attrs = info->attrs;
9079         u64 rate, burst;
9080         int err;
9081
9082         rate = policer_item->rate;
9083         burst = policer_item->burst;
9084
9085         if (attrs[DEVLINK_ATTR_TRAP_POLICER_RATE])
9086                 rate = nla_get_u64(attrs[DEVLINK_ATTR_TRAP_POLICER_RATE]);
9087
9088         if (attrs[DEVLINK_ATTR_TRAP_POLICER_BURST])
9089                 burst = nla_get_u64(attrs[DEVLINK_ATTR_TRAP_POLICER_BURST]);
9090
9091         if (rate < policer_item->policer->min_rate) {
9092                 NL_SET_ERR_MSG_MOD(extack, "Policer rate lower than limit");
9093                 return -EINVAL;
9094         }
9095
9096         if (rate > policer_item->policer->max_rate) {
9097                 NL_SET_ERR_MSG_MOD(extack, "Policer rate higher than limit");
9098                 return -EINVAL;
9099         }
9100
9101         if (burst < policer_item->policer->min_burst) {
9102                 NL_SET_ERR_MSG_MOD(extack, "Policer burst size lower than limit");
9103                 return -EINVAL;
9104         }
9105
9106         if (burst > policer_item->policer->max_burst) {
9107                 NL_SET_ERR_MSG_MOD(extack, "Policer burst size higher than limit");
9108                 return -EINVAL;
9109         }
9110
9111         err = devlink->ops->trap_policer_set(devlink, policer_item->policer,
9112                                              rate, burst, info->extack);
9113         if (err)
9114                 return err;
9115
9116         policer_item->rate = rate;
9117         policer_item->burst = burst;
9118
9119         return 0;
9120 }
9121
9122 static int devlink_nl_cmd_trap_policer_set_doit(struct sk_buff *skb,
9123                                                 struct genl_info *info)
9124 {
9125         struct devlink_trap_policer_item *policer_item;
9126         struct netlink_ext_ack *extack = info->extack;
9127         struct devlink *devlink = info->user_ptr[0];
9128
9129         if (list_empty(&devlink->trap_policer_list))
9130                 return -EOPNOTSUPP;
9131
9132         if (!devlink->ops->trap_policer_set)
9133                 return -EOPNOTSUPP;
9134
9135         policer_item = devlink_trap_policer_item_get_from_info(devlink, info);
9136         if (!policer_item) {
9137                 NL_SET_ERR_MSG_MOD(extack, "Device did not register this trap policer");
9138                 return -ENOENT;
9139         }
9140
9141         return devlink_trap_policer_set(devlink, policer_item, info);
9142 }
9143
9144 const struct genl_small_ops devlink_nl_ops[56] = {
9145         {
9146                 .cmd = DEVLINK_CMD_GET,
9147                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9148                 .doit = devlink_nl_cmd_get_doit,
9149                 .dumpit = devlink_nl_cmd_get_dumpit,
9150                 /* can be retrieved by unprivileged users */
9151         },
9152         {
9153                 .cmd = DEVLINK_CMD_PORT_GET,
9154                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9155                 .doit = devlink_nl_cmd_port_get_doit,
9156                 .dumpit = devlink_nl_cmd_port_get_dumpit,
9157                 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
9158                 /* can be retrieved by unprivileged users */
9159         },
9160         {
9161                 .cmd = DEVLINK_CMD_PORT_SET,
9162                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9163                 .doit = devlink_nl_cmd_port_set_doit,
9164                 .flags = GENL_ADMIN_PERM,
9165                 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
9166         },
9167         {
9168                 .cmd = DEVLINK_CMD_RATE_GET,
9169                 .doit = devlink_nl_cmd_rate_get_doit,
9170                 .dumpit = devlink_nl_cmd_rate_get_dumpit,
9171                 .internal_flags = DEVLINK_NL_FLAG_NEED_RATE,
9172                 /* can be retrieved by unprivileged users */
9173         },
9174         {
9175                 .cmd = DEVLINK_CMD_RATE_SET,
9176                 .doit = devlink_nl_cmd_rate_set_doit,
9177                 .flags = GENL_ADMIN_PERM,
9178                 .internal_flags = DEVLINK_NL_FLAG_NEED_RATE,
9179         },
9180         {
9181                 .cmd = DEVLINK_CMD_RATE_NEW,
9182                 .doit = devlink_nl_cmd_rate_new_doit,
9183                 .flags = GENL_ADMIN_PERM,
9184         },
9185         {
9186                 .cmd = DEVLINK_CMD_RATE_DEL,
9187                 .doit = devlink_nl_cmd_rate_del_doit,
9188                 .flags = GENL_ADMIN_PERM,
9189                 .internal_flags = DEVLINK_NL_FLAG_NEED_RATE_NODE,
9190         },
9191         {
9192                 .cmd = DEVLINK_CMD_PORT_SPLIT,
9193                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9194                 .doit = devlink_nl_cmd_port_split_doit,
9195                 .flags = GENL_ADMIN_PERM,
9196                 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
9197         },
9198         {
9199                 .cmd = DEVLINK_CMD_PORT_UNSPLIT,
9200                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9201                 .doit = devlink_nl_cmd_port_unsplit_doit,
9202                 .flags = GENL_ADMIN_PERM,
9203                 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
9204         },
9205         {
9206                 .cmd = DEVLINK_CMD_PORT_NEW,
9207                 .doit = devlink_nl_cmd_port_new_doit,
9208                 .flags = GENL_ADMIN_PERM,
9209         },
9210         {
9211                 .cmd = DEVLINK_CMD_PORT_DEL,
9212                 .doit = devlink_nl_cmd_port_del_doit,
9213                 .flags = GENL_ADMIN_PERM,
9214         },
9215         {
9216                 .cmd = DEVLINK_CMD_LINECARD_GET,
9217                 .doit = devlink_nl_cmd_linecard_get_doit,
9218                 .dumpit = devlink_nl_cmd_linecard_get_dumpit,
9219                 .internal_flags = DEVLINK_NL_FLAG_NEED_LINECARD,
9220                 /* can be retrieved by unprivileged users */
9221         },
9222         {
9223                 .cmd = DEVLINK_CMD_LINECARD_SET,
9224                 .doit = devlink_nl_cmd_linecard_set_doit,
9225                 .flags = GENL_ADMIN_PERM,
9226                 .internal_flags = DEVLINK_NL_FLAG_NEED_LINECARD,
9227         },
9228         {
9229                 .cmd = DEVLINK_CMD_SB_GET,
9230                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9231                 .doit = devlink_nl_cmd_sb_get_doit,
9232                 .dumpit = devlink_nl_cmd_sb_get_dumpit,
9233                 /* can be retrieved by unprivileged users */
9234         },
9235         {
9236                 .cmd = DEVLINK_CMD_SB_POOL_GET,
9237                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9238                 .doit = devlink_nl_cmd_sb_pool_get_doit,
9239                 .dumpit = devlink_nl_cmd_sb_pool_get_dumpit,
9240                 /* can be retrieved by unprivileged users */
9241         },
9242         {
9243                 .cmd = DEVLINK_CMD_SB_POOL_SET,
9244                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9245                 .doit = devlink_nl_cmd_sb_pool_set_doit,
9246                 .flags = GENL_ADMIN_PERM,
9247         },
9248         {
9249                 .cmd = DEVLINK_CMD_SB_PORT_POOL_GET,
9250                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9251                 .doit = devlink_nl_cmd_sb_port_pool_get_doit,
9252                 .dumpit = devlink_nl_cmd_sb_port_pool_get_dumpit,
9253                 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
9254                 /* can be retrieved by unprivileged users */
9255         },
9256         {
9257                 .cmd = DEVLINK_CMD_SB_PORT_POOL_SET,
9258                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9259                 .doit = devlink_nl_cmd_sb_port_pool_set_doit,
9260                 .flags = GENL_ADMIN_PERM,
9261                 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
9262         },
9263         {
9264                 .cmd = DEVLINK_CMD_SB_TC_POOL_BIND_GET,
9265                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9266                 .doit = devlink_nl_cmd_sb_tc_pool_bind_get_doit,
9267                 .dumpit = devlink_nl_cmd_sb_tc_pool_bind_get_dumpit,
9268                 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
9269                 /* can be retrieved by unprivileged users */
9270         },
9271         {
9272                 .cmd = DEVLINK_CMD_SB_TC_POOL_BIND_SET,
9273                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9274                 .doit = devlink_nl_cmd_sb_tc_pool_bind_set_doit,
9275                 .flags = GENL_ADMIN_PERM,
9276                 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
9277         },
9278         {
9279                 .cmd = DEVLINK_CMD_SB_OCC_SNAPSHOT,
9280                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9281                 .doit = devlink_nl_cmd_sb_occ_snapshot_doit,
9282                 .flags = GENL_ADMIN_PERM,
9283         },
9284         {
9285                 .cmd = DEVLINK_CMD_SB_OCC_MAX_CLEAR,
9286                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9287                 .doit = devlink_nl_cmd_sb_occ_max_clear_doit,
9288                 .flags = GENL_ADMIN_PERM,
9289         },
9290         {
9291                 .cmd = DEVLINK_CMD_ESWITCH_GET,
9292                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9293                 .doit = devlink_nl_cmd_eswitch_get_doit,
9294                 .flags = GENL_ADMIN_PERM,
9295         },
9296         {
9297                 .cmd = DEVLINK_CMD_ESWITCH_SET,
9298                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9299                 .doit = devlink_nl_cmd_eswitch_set_doit,
9300                 .flags = GENL_ADMIN_PERM,
9301         },
9302         {
9303                 .cmd = DEVLINK_CMD_DPIPE_TABLE_GET,
9304                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9305                 .doit = devlink_nl_cmd_dpipe_table_get,
9306                 /* can be retrieved by unprivileged users */
9307         },
9308         {
9309                 .cmd = DEVLINK_CMD_DPIPE_ENTRIES_GET,
9310                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9311                 .doit = devlink_nl_cmd_dpipe_entries_get,
9312                 /* can be retrieved by unprivileged users */
9313         },
9314         {
9315                 .cmd = DEVLINK_CMD_DPIPE_HEADERS_GET,
9316                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9317                 .doit = devlink_nl_cmd_dpipe_headers_get,
9318                 /* can be retrieved by unprivileged users */
9319         },
9320         {
9321                 .cmd = DEVLINK_CMD_DPIPE_TABLE_COUNTERS_SET,
9322                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9323                 .doit = devlink_nl_cmd_dpipe_table_counters_set,
9324                 .flags = GENL_ADMIN_PERM,
9325         },
9326         {
9327                 .cmd = DEVLINK_CMD_RESOURCE_SET,
9328                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9329                 .doit = devlink_nl_cmd_resource_set,
9330                 .flags = GENL_ADMIN_PERM,
9331         },
9332         {
9333                 .cmd = DEVLINK_CMD_RESOURCE_DUMP,
9334                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9335                 .doit = devlink_nl_cmd_resource_dump,
9336                 /* can be retrieved by unprivileged users */
9337         },
9338         {
9339                 .cmd = DEVLINK_CMD_RELOAD,
9340                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9341                 .doit = devlink_nl_cmd_reload,
9342                 .flags = GENL_ADMIN_PERM,
9343         },
9344         {
9345                 .cmd = DEVLINK_CMD_PARAM_GET,
9346                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9347                 .doit = devlink_nl_cmd_param_get_doit,
9348                 .dumpit = devlink_nl_cmd_param_get_dumpit,
9349                 /* can be retrieved by unprivileged users */
9350         },
9351         {
9352                 .cmd = DEVLINK_CMD_PARAM_SET,
9353                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9354                 .doit = devlink_nl_cmd_param_set_doit,
9355                 .flags = GENL_ADMIN_PERM,
9356         },
9357         {
9358                 .cmd = DEVLINK_CMD_PORT_PARAM_GET,
9359                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9360                 .doit = devlink_nl_cmd_port_param_get_doit,
9361                 .dumpit = devlink_nl_cmd_port_param_get_dumpit,
9362                 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
9363                 /* can be retrieved by unprivileged users */
9364         },
9365         {
9366                 .cmd = DEVLINK_CMD_PORT_PARAM_SET,
9367                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9368                 .doit = devlink_nl_cmd_port_param_set_doit,
9369                 .flags = GENL_ADMIN_PERM,
9370                 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
9371         },
9372         {
9373                 .cmd = DEVLINK_CMD_REGION_GET,
9374                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9375                 .doit = devlink_nl_cmd_region_get_doit,
9376                 .dumpit = devlink_nl_cmd_region_get_dumpit,
9377                 .flags = GENL_ADMIN_PERM,
9378         },
9379         {
9380                 .cmd = DEVLINK_CMD_REGION_NEW,
9381                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9382                 .doit = devlink_nl_cmd_region_new,
9383                 .flags = GENL_ADMIN_PERM,
9384         },
9385         {
9386                 .cmd = DEVLINK_CMD_REGION_DEL,
9387                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9388                 .doit = devlink_nl_cmd_region_del,
9389                 .flags = GENL_ADMIN_PERM,
9390         },
9391         {
9392                 .cmd = DEVLINK_CMD_REGION_READ,
9393                 .validate = GENL_DONT_VALIDATE_STRICT |
9394                             GENL_DONT_VALIDATE_DUMP_STRICT,
9395                 .dumpit = devlink_nl_cmd_region_read_dumpit,
9396                 .flags = GENL_ADMIN_PERM,
9397         },
9398         {
9399                 .cmd = DEVLINK_CMD_INFO_GET,
9400                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9401                 .doit = devlink_nl_cmd_info_get_doit,
9402                 .dumpit = devlink_nl_cmd_info_get_dumpit,
9403                 /* can be retrieved by unprivileged users */
9404         },
9405         {
9406                 .cmd = DEVLINK_CMD_HEALTH_REPORTER_GET,
9407                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9408                 .doit = devlink_nl_cmd_health_reporter_get_doit,
9409                 .dumpit = devlink_nl_cmd_health_reporter_get_dumpit,
9410                 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK_OR_PORT,
9411                 /* can be retrieved by unprivileged users */
9412         },
9413         {
9414                 .cmd = DEVLINK_CMD_HEALTH_REPORTER_SET,
9415                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9416                 .doit = devlink_nl_cmd_health_reporter_set_doit,
9417                 .flags = GENL_ADMIN_PERM,
9418                 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK_OR_PORT,
9419         },
9420         {
9421                 .cmd = DEVLINK_CMD_HEALTH_REPORTER_RECOVER,
9422                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9423                 .doit = devlink_nl_cmd_health_reporter_recover_doit,
9424                 .flags = GENL_ADMIN_PERM,
9425                 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK_OR_PORT,
9426         },
9427         {
9428                 .cmd = DEVLINK_CMD_HEALTH_REPORTER_DIAGNOSE,
9429                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9430                 .doit = devlink_nl_cmd_health_reporter_diagnose_doit,
9431                 .flags = GENL_ADMIN_PERM,
9432                 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK_OR_PORT,
9433         },
9434         {
9435                 .cmd = DEVLINK_CMD_HEALTH_REPORTER_DUMP_GET,
9436                 .validate = GENL_DONT_VALIDATE_STRICT |
9437                             GENL_DONT_VALIDATE_DUMP_STRICT,
9438                 .dumpit = devlink_nl_cmd_health_reporter_dump_get_dumpit,
9439                 .flags = GENL_ADMIN_PERM,
9440         },
9441         {
9442                 .cmd = DEVLINK_CMD_HEALTH_REPORTER_DUMP_CLEAR,
9443                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9444                 .doit = devlink_nl_cmd_health_reporter_dump_clear_doit,
9445                 .flags = GENL_ADMIN_PERM,
9446                 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK_OR_PORT,
9447         },
9448         {
9449                 .cmd = DEVLINK_CMD_HEALTH_REPORTER_TEST,
9450                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9451                 .doit = devlink_nl_cmd_health_reporter_test_doit,
9452                 .flags = GENL_ADMIN_PERM,
9453                 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK_OR_PORT,
9454         },
9455         {
9456                 .cmd = DEVLINK_CMD_FLASH_UPDATE,
9457                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9458                 .doit = devlink_nl_cmd_flash_update,
9459                 .flags = GENL_ADMIN_PERM,
9460         },
9461         {
9462                 .cmd = DEVLINK_CMD_TRAP_GET,
9463                 .doit = devlink_nl_cmd_trap_get_doit,
9464                 .dumpit = devlink_nl_cmd_trap_get_dumpit,
9465                 /* can be retrieved by unprivileged users */
9466         },
9467         {
9468                 .cmd = DEVLINK_CMD_TRAP_SET,
9469                 .doit = devlink_nl_cmd_trap_set_doit,
9470                 .flags = GENL_ADMIN_PERM,
9471         },
9472         {
9473                 .cmd = DEVLINK_CMD_TRAP_GROUP_GET,
9474                 .doit = devlink_nl_cmd_trap_group_get_doit,
9475                 .dumpit = devlink_nl_cmd_trap_group_get_dumpit,
9476                 /* can be retrieved by unprivileged users */
9477         },
9478         {
9479                 .cmd = DEVLINK_CMD_TRAP_GROUP_SET,
9480                 .doit = devlink_nl_cmd_trap_group_set_doit,
9481                 .flags = GENL_ADMIN_PERM,
9482         },
9483         {
9484                 .cmd = DEVLINK_CMD_TRAP_POLICER_GET,
9485                 .doit = devlink_nl_cmd_trap_policer_get_doit,
9486                 .dumpit = devlink_nl_cmd_trap_policer_get_dumpit,
9487                 /* can be retrieved by unprivileged users */
9488         },
9489         {
9490                 .cmd = DEVLINK_CMD_TRAP_POLICER_SET,
9491                 .doit = devlink_nl_cmd_trap_policer_set_doit,
9492                 .flags = GENL_ADMIN_PERM,
9493         },
9494         {
9495                 .cmd = DEVLINK_CMD_SELFTESTS_GET,
9496                 .doit = devlink_nl_cmd_selftests_get_doit,
9497                 .dumpit = devlink_nl_cmd_selftests_get_dumpit
9498                 /* can be retrieved by unprivileged users */
9499         },
9500         {
9501                 .cmd = DEVLINK_CMD_SELFTESTS_RUN,
9502                 .doit = devlink_nl_cmd_selftests_run,
9503                 .flags = GENL_ADMIN_PERM,
9504         },
9505         /* -- No new ops here! Use split ops going forward! -- */
9506 };
9507
9508 bool devlink_reload_actions_valid(const struct devlink_ops *ops)
9509 {
9510         const struct devlink_reload_combination *comb;
9511         int i;
9512
9513         if (!devlink_reload_supported(ops)) {
9514                 if (WARN_ON(ops->reload_actions))
9515                         return false;
9516                 return true;
9517         }
9518
9519         if (WARN_ON(!ops->reload_actions ||
9520                     ops->reload_actions & BIT(DEVLINK_RELOAD_ACTION_UNSPEC) ||
9521                     ops->reload_actions >= BIT(__DEVLINK_RELOAD_ACTION_MAX)))
9522                 return false;
9523
9524         if (WARN_ON(ops->reload_limits & BIT(DEVLINK_RELOAD_LIMIT_UNSPEC) ||
9525                     ops->reload_limits >= BIT(__DEVLINK_RELOAD_LIMIT_MAX)))
9526                 return false;
9527
9528         for (i = 0; i < ARRAY_SIZE(devlink_reload_invalid_combinations); i++)  {
9529                 comb = &devlink_reload_invalid_combinations[i];
9530                 if (ops->reload_actions == BIT(comb->action) &&
9531                     ops->reload_limits == BIT(comb->limit))
9532                         return false;
9533         }
9534         return true;
9535 }
9536
9537 static void
9538 devlink_trap_policer_notify(struct devlink *devlink,
9539                             const struct devlink_trap_policer_item *policer_item,
9540                             enum devlink_command cmd);
9541 static void
9542 devlink_trap_group_notify(struct devlink *devlink,
9543                           const struct devlink_trap_group_item *group_item,
9544                           enum devlink_command cmd);
9545 static void devlink_trap_notify(struct devlink *devlink,
9546                                 const struct devlink_trap_item *trap_item,
9547                                 enum devlink_command cmd);
9548
9549 void devlink_notify_register(struct devlink *devlink)
9550 {
9551         struct devlink_trap_policer_item *policer_item;
9552         struct devlink_trap_group_item *group_item;
9553         struct devlink_param_item *param_item;
9554         struct devlink_trap_item *trap_item;
9555         struct devlink_port *devlink_port;
9556         struct devlink_linecard *linecard;
9557         struct devlink_rate *rate_node;
9558         struct devlink_region *region;
9559         unsigned long port_index;
9560
9561         devlink_notify(devlink, DEVLINK_CMD_NEW);
9562         list_for_each_entry(linecard, &devlink->linecard_list, list)
9563                 devlink_linecard_notify(linecard, DEVLINK_CMD_LINECARD_NEW);
9564
9565         xa_for_each(&devlink->ports, port_index, devlink_port)
9566                 devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW);
9567
9568         list_for_each_entry(policer_item, &devlink->trap_policer_list, list)
9569                 devlink_trap_policer_notify(devlink, policer_item,
9570                                             DEVLINK_CMD_TRAP_POLICER_NEW);
9571
9572         list_for_each_entry(group_item, &devlink->trap_group_list, list)
9573                 devlink_trap_group_notify(devlink, group_item,
9574                                           DEVLINK_CMD_TRAP_GROUP_NEW);
9575
9576         list_for_each_entry(trap_item, &devlink->trap_list, list)
9577                 devlink_trap_notify(devlink, trap_item, DEVLINK_CMD_TRAP_NEW);
9578
9579         list_for_each_entry(rate_node, &devlink->rate_list, list)
9580                 devlink_rate_notify(rate_node, DEVLINK_CMD_RATE_NEW);
9581
9582         list_for_each_entry(region, &devlink->region_list, list)
9583                 devlink_nl_region_notify(region, NULL, DEVLINK_CMD_REGION_NEW);
9584
9585         list_for_each_entry(param_item, &devlink->param_list, list)
9586                 devlink_param_notify(devlink, 0, param_item,
9587                                      DEVLINK_CMD_PARAM_NEW);
9588 }
9589
9590 void devlink_notify_unregister(struct devlink *devlink)
9591 {
9592         struct devlink_trap_policer_item *policer_item;
9593         struct devlink_trap_group_item *group_item;
9594         struct devlink_param_item *param_item;
9595         struct devlink_trap_item *trap_item;
9596         struct devlink_port *devlink_port;
9597         struct devlink_rate *rate_node;
9598         struct devlink_region *region;
9599         unsigned long port_index;
9600
9601         list_for_each_entry_reverse(param_item, &devlink->param_list, list)
9602                 devlink_param_notify(devlink, 0, param_item,
9603                                      DEVLINK_CMD_PARAM_DEL);
9604
9605         list_for_each_entry_reverse(region, &devlink->region_list, list)
9606                 devlink_nl_region_notify(region, NULL, DEVLINK_CMD_REGION_DEL);
9607
9608         list_for_each_entry_reverse(rate_node, &devlink->rate_list, list)
9609                 devlink_rate_notify(rate_node, DEVLINK_CMD_RATE_DEL);
9610
9611         list_for_each_entry_reverse(trap_item, &devlink->trap_list, list)
9612                 devlink_trap_notify(devlink, trap_item, DEVLINK_CMD_TRAP_DEL);
9613
9614         list_for_each_entry_reverse(group_item, &devlink->trap_group_list, list)
9615                 devlink_trap_group_notify(devlink, group_item,
9616                                           DEVLINK_CMD_TRAP_GROUP_DEL);
9617         list_for_each_entry_reverse(policer_item, &devlink->trap_policer_list,
9618                                     list)
9619                 devlink_trap_policer_notify(devlink, policer_item,
9620                                             DEVLINK_CMD_TRAP_POLICER_DEL);
9621
9622         xa_for_each(&devlink->ports, port_index, devlink_port)
9623                 devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_DEL);
9624         devlink_notify(devlink, DEVLINK_CMD_DEL);
9625 }
9626
9627 static void devlink_port_type_warn(struct work_struct *work)
9628 {
9629         WARN(true, "Type was not set for devlink port.");
9630 }
9631
9632 static bool devlink_port_type_should_warn(struct devlink_port *devlink_port)
9633 {
9634         /* Ignore CPU and DSA flavours. */
9635         return devlink_port->attrs.flavour != DEVLINK_PORT_FLAVOUR_CPU &&
9636                devlink_port->attrs.flavour != DEVLINK_PORT_FLAVOUR_DSA &&
9637                devlink_port->attrs.flavour != DEVLINK_PORT_FLAVOUR_UNUSED;
9638 }
9639
9640 #define DEVLINK_PORT_TYPE_WARN_TIMEOUT (HZ * 3600)
9641
9642 static void devlink_port_type_warn_schedule(struct devlink_port *devlink_port)
9643 {
9644         if (!devlink_port_type_should_warn(devlink_port))
9645                 return;
9646         /* Schedule a work to WARN in case driver does not set port
9647          * type within timeout.
9648          */
9649         schedule_delayed_work(&devlink_port->type_warn_dw,
9650                               DEVLINK_PORT_TYPE_WARN_TIMEOUT);
9651 }
9652
9653 static void devlink_port_type_warn_cancel(struct devlink_port *devlink_port)
9654 {
9655         if (!devlink_port_type_should_warn(devlink_port))
9656                 return;
9657         cancel_delayed_work_sync(&devlink_port->type_warn_dw);
9658 }
9659
9660 /**
9661  * devlink_port_init() - Init devlink port
9662  *
9663  * @devlink: devlink
9664  * @devlink_port: devlink port
9665  *
9666  * Initialize essencial stuff that is needed for functions
9667  * that may be called before devlink port registration.
9668  * Call to this function is optional and not needed
9669  * in case the driver does not use such functions.
9670  */
9671 void devlink_port_init(struct devlink *devlink,
9672                        struct devlink_port *devlink_port)
9673 {
9674         if (devlink_port->initialized)
9675                 return;
9676         devlink_port->devlink = devlink;
9677         INIT_LIST_HEAD(&devlink_port->region_list);
9678         devlink_port->initialized = true;
9679 }
9680 EXPORT_SYMBOL_GPL(devlink_port_init);
9681
9682 /**
9683  * devlink_port_fini() - Deinitialize devlink port
9684  *
9685  * @devlink_port: devlink port
9686  *
9687  * Deinitialize essencial stuff that is in use for functions
9688  * that may be called after devlink port unregistration.
9689  * Call to this function is optional and not needed
9690  * in case the driver does not use such functions.
9691  */
9692 void devlink_port_fini(struct devlink_port *devlink_port)
9693 {
9694         WARN_ON(!list_empty(&devlink_port->region_list));
9695 }
9696 EXPORT_SYMBOL_GPL(devlink_port_fini);
9697
9698 /**
9699  * devl_port_register() - Register devlink port
9700  *
9701  * @devlink: devlink
9702  * @devlink_port: devlink port
9703  * @port_index: driver-specific numerical identifier of the port
9704  *
9705  * Register devlink port with provided port index. User can use
9706  * any indexing, even hw-related one. devlink_port structure
9707  * is convenient to be embedded inside user driver private structure.
9708  * Note that the caller should take care of zeroing the devlink_port
9709  * structure.
9710  */
9711 int devl_port_register(struct devlink *devlink,
9712                        struct devlink_port *devlink_port,
9713                        unsigned int port_index)
9714 {
9715         int err;
9716
9717         devl_assert_locked(devlink);
9718
9719         ASSERT_DEVLINK_PORT_NOT_REGISTERED(devlink_port);
9720
9721         devlink_port_init(devlink, devlink_port);
9722         devlink_port->registered = true;
9723         devlink_port->index = port_index;
9724         spin_lock_init(&devlink_port->type_lock);
9725         INIT_LIST_HEAD(&devlink_port->reporter_list);
9726         mutex_init(&devlink_port->reporters_lock);
9727         err = xa_insert(&devlink->ports, port_index, devlink_port, GFP_KERNEL);
9728         if (err) {
9729                 mutex_destroy(&devlink_port->reporters_lock);
9730                 return err;
9731         }
9732
9733         INIT_DELAYED_WORK(&devlink_port->type_warn_dw, &devlink_port_type_warn);
9734         devlink_port_type_warn_schedule(devlink_port);
9735         devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW);
9736         return 0;
9737 }
9738 EXPORT_SYMBOL_GPL(devl_port_register);
9739
9740 /**
9741  *      devlink_port_register - Register devlink port
9742  *
9743  *      @devlink: devlink
9744  *      @devlink_port: devlink port
9745  *      @port_index: driver-specific numerical identifier of the port
9746  *
9747  *      Register devlink port with provided port index. User can use
9748  *      any indexing, even hw-related one. devlink_port structure
9749  *      is convenient to be embedded inside user driver private structure.
9750  *      Note that the caller should take care of zeroing the devlink_port
9751  *      structure.
9752  *
9753  *      Context: Takes and release devlink->lock <mutex>.
9754  */
9755 int devlink_port_register(struct devlink *devlink,
9756                           struct devlink_port *devlink_port,
9757                           unsigned int port_index)
9758 {
9759         int err;
9760
9761         devl_lock(devlink);
9762         err = devl_port_register(devlink, devlink_port, port_index);
9763         devl_unlock(devlink);
9764         return err;
9765 }
9766 EXPORT_SYMBOL_GPL(devlink_port_register);
9767
9768 /**
9769  * devl_port_unregister() - Unregister devlink port
9770  *
9771  * @devlink_port: devlink port
9772  */
9773 void devl_port_unregister(struct devlink_port *devlink_port)
9774 {
9775         lockdep_assert_held(&devlink_port->devlink->lock);
9776         WARN_ON(devlink_port->type != DEVLINK_PORT_TYPE_NOTSET);
9777
9778         devlink_port_type_warn_cancel(devlink_port);
9779         devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_DEL);
9780         xa_erase(&devlink_port->devlink->ports, devlink_port->index);
9781         WARN_ON(!list_empty(&devlink_port->reporter_list));
9782         mutex_destroy(&devlink_port->reporters_lock);
9783         devlink_port->registered = false;
9784 }
9785 EXPORT_SYMBOL_GPL(devl_port_unregister);
9786
9787 /**
9788  *      devlink_port_unregister - Unregister devlink port
9789  *
9790  *      @devlink_port: devlink port
9791  *
9792  *      Context: Takes and release devlink->lock <mutex>.
9793  */
9794 void devlink_port_unregister(struct devlink_port *devlink_port)
9795 {
9796         struct devlink *devlink = devlink_port->devlink;
9797
9798         devl_lock(devlink);
9799         devl_port_unregister(devlink_port);
9800         devl_unlock(devlink);
9801 }
9802 EXPORT_SYMBOL_GPL(devlink_port_unregister);
9803
9804 static void devlink_port_type_netdev_checks(struct devlink_port *devlink_port,
9805                                             struct net_device *netdev)
9806 {
9807         const struct net_device_ops *ops = netdev->netdev_ops;
9808
9809         /* If driver registers devlink port, it should set devlink port
9810          * attributes accordingly so the compat functions are called
9811          * and the original ops are not used.
9812          */
9813         if (ops->ndo_get_phys_port_name) {
9814                 /* Some drivers use the same set of ndos for netdevs
9815                  * that have devlink_port registered and also for
9816                  * those who don't. Make sure that ndo_get_phys_port_name
9817                  * returns -EOPNOTSUPP here in case it is defined.
9818                  * Warn if not.
9819                  */
9820                 char name[IFNAMSIZ];
9821                 int err;
9822
9823                 err = ops->ndo_get_phys_port_name(netdev, name, sizeof(name));
9824                 WARN_ON(err != -EOPNOTSUPP);
9825         }
9826         if (ops->ndo_get_port_parent_id) {
9827                 /* Some drivers use the same set of ndos for netdevs
9828                  * that have devlink_port registered and also for
9829                  * those who don't. Make sure that ndo_get_port_parent_id
9830                  * returns -EOPNOTSUPP here in case it is defined.
9831                  * Warn if not.
9832                  */
9833                 struct netdev_phys_item_id ppid;
9834                 int err;
9835
9836                 err = ops->ndo_get_port_parent_id(netdev, &ppid);
9837                 WARN_ON(err != -EOPNOTSUPP);
9838         }
9839 }
9840
9841 static void __devlink_port_type_set(struct devlink_port *devlink_port,
9842                                     enum devlink_port_type type,
9843                                     void *type_dev)
9844 {
9845         struct net_device *netdev = type_dev;
9846
9847         ASSERT_DEVLINK_PORT_REGISTERED(devlink_port);
9848
9849         if (type == DEVLINK_PORT_TYPE_NOTSET) {
9850                 devlink_port_type_warn_schedule(devlink_port);
9851         } else {
9852                 devlink_port_type_warn_cancel(devlink_port);
9853                 if (type == DEVLINK_PORT_TYPE_ETH && netdev)
9854                         devlink_port_type_netdev_checks(devlink_port, netdev);
9855         }
9856
9857         spin_lock_bh(&devlink_port->type_lock);
9858         devlink_port->type = type;
9859         switch (type) {
9860         case DEVLINK_PORT_TYPE_ETH:
9861                 devlink_port->type_eth.netdev = netdev;
9862                 if (netdev) {
9863                         ASSERT_RTNL();
9864                         devlink_port->type_eth.ifindex = netdev->ifindex;
9865                         BUILD_BUG_ON(sizeof(devlink_port->type_eth.ifname) !=
9866                                      sizeof(netdev->name));
9867                         strcpy(devlink_port->type_eth.ifname, netdev->name);
9868                 }
9869                 break;
9870         case DEVLINK_PORT_TYPE_IB:
9871                 devlink_port->type_ib.ibdev = type_dev;
9872                 break;
9873         default:
9874                 break;
9875         }
9876         spin_unlock_bh(&devlink_port->type_lock);
9877         devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW);
9878 }
9879
9880 /**
9881  *      devlink_port_type_eth_set - Set port type to Ethernet
9882  *
9883  *      @devlink_port: devlink port
9884  *
9885  *      If driver is calling this, most likely it is doing something wrong.
9886  */
9887 void devlink_port_type_eth_set(struct devlink_port *devlink_port)
9888 {
9889         dev_warn(devlink_port->devlink->dev,
9890                  "devlink port type for port %d set to Ethernet without a software interface reference, device type not supported by the kernel?\n",
9891                  devlink_port->index);
9892         __devlink_port_type_set(devlink_port, DEVLINK_PORT_TYPE_ETH, NULL);
9893 }
9894 EXPORT_SYMBOL_GPL(devlink_port_type_eth_set);
9895
9896 /**
9897  *      devlink_port_type_ib_set - Set port type to InfiniBand
9898  *
9899  *      @devlink_port: devlink port
9900  *      @ibdev: related IB device
9901  */
9902 void devlink_port_type_ib_set(struct devlink_port *devlink_port,
9903                               struct ib_device *ibdev)
9904 {
9905         __devlink_port_type_set(devlink_port, DEVLINK_PORT_TYPE_IB, ibdev);
9906 }
9907 EXPORT_SYMBOL_GPL(devlink_port_type_ib_set);
9908
9909 /**
9910  *      devlink_port_type_clear - Clear port type
9911  *
9912  *      @devlink_port: devlink port
9913  *
9914  *      If driver is calling this for clearing Ethernet type, most likely
9915  *      it is doing something wrong.
9916  */
9917 void devlink_port_type_clear(struct devlink_port *devlink_port)
9918 {
9919         if (devlink_port->type == DEVLINK_PORT_TYPE_ETH)
9920                 dev_warn(devlink_port->devlink->dev,
9921                          "devlink port type for port %d cleared without a software interface reference, device type not supported by the kernel?\n",
9922                          devlink_port->index);
9923         __devlink_port_type_set(devlink_port, DEVLINK_PORT_TYPE_NOTSET, NULL);
9924 }
9925 EXPORT_SYMBOL_GPL(devlink_port_type_clear);
9926
9927 int devlink_port_netdevice_event(struct notifier_block *nb,
9928                                  unsigned long event, void *ptr)
9929 {
9930         struct net_device *netdev = netdev_notifier_info_to_dev(ptr);
9931         struct devlink_port *devlink_port = netdev->devlink_port;
9932         struct devlink *devlink;
9933
9934         devlink = container_of(nb, struct devlink, netdevice_nb);
9935
9936         if (!devlink_port || devlink_port->devlink != devlink)
9937                 return NOTIFY_OK;
9938
9939         switch (event) {
9940         case NETDEV_POST_INIT:
9941                 /* Set the type but not netdev pointer. It is going to be set
9942                  * later on by NETDEV_REGISTER event. Happens once during
9943                  * netdevice register
9944                  */
9945                 __devlink_port_type_set(devlink_port, DEVLINK_PORT_TYPE_ETH,
9946                                         NULL);
9947                 break;
9948         case NETDEV_REGISTER:
9949         case NETDEV_CHANGENAME:
9950                 /* Set the netdev on top of previously set type. Note this
9951                  * event happens also during net namespace change so here
9952                  * we take into account netdev pointer appearing in this
9953                  * namespace.
9954                  */
9955                 __devlink_port_type_set(devlink_port, devlink_port->type,
9956                                         netdev);
9957                 break;
9958         case NETDEV_UNREGISTER:
9959                 /* Clear netdev pointer, but not the type. This event happens
9960                  * also during net namespace change so we need to clear
9961                  * pointer to netdev that is going to another net namespace.
9962                  */
9963                 __devlink_port_type_set(devlink_port, devlink_port->type,
9964                                         NULL);
9965                 break;
9966         case NETDEV_PRE_UNINIT:
9967                 /* Clear the type and the netdev pointer. Happens one during
9968                  * netdevice unregister.
9969                  */
9970                 __devlink_port_type_set(devlink_port, DEVLINK_PORT_TYPE_NOTSET,
9971                                         NULL);
9972                 break;
9973         }
9974
9975         return NOTIFY_OK;
9976 }
9977
9978 static int __devlink_port_attrs_set(struct devlink_port *devlink_port,
9979                                     enum devlink_port_flavour flavour)
9980 {
9981         struct devlink_port_attrs *attrs = &devlink_port->attrs;
9982
9983         devlink_port->attrs_set = true;
9984         attrs->flavour = flavour;
9985         if (attrs->switch_id.id_len) {
9986                 devlink_port->switch_port = true;
9987                 if (WARN_ON(attrs->switch_id.id_len > MAX_PHYS_ITEM_ID_LEN))
9988                         attrs->switch_id.id_len = MAX_PHYS_ITEM_ID_LEN;
9989         } else {
9990                 devlink_port->switch_port = false;
9991         }
9992         return 0;
9993 }
9994
9995 /**
9996  *      devlink_port_attrs_set - Set port attributes
9997  *
9998  *      @devlink_port: devlink port
9999  *      @attrs: devlink port attrs
10000  */
10001 void devlink_port_attrs_set(struct devlink_port *devlink_port,
10002                             struct devlink_port_attrs *attrs)
10003 {
10004         int ret;
10005
10006         ASSERT_DEVLINK_PORT_NOT_REGISTERED(devlink_port);
10007
10008         devlink_port->attrs = *attrs;
10009         ret = __devlink_port_attrs_set(devlink_port, attrs->flavour);
10010         if (ret)
10011                 return;
10012         WARN_ON(attrs->splittable && attrs->split);
10013 }
10014 EXPORT_SYMBOL_GPL(devlink_port_attrs_set);
10015
10016 /**
10017  *      devlink_port_attrs_pci_pf_set - Set PCI PF port attributes
10018  *
10019  *      @devlink_port: devlink port
10020  *      @controller: associated controller number for the devlink port instance
10021  *      @pf: associated PF for the devlink port instance
10022  *      @external: indicates if the port is for an external controller
10023  */
10024 void devlink_port_attrs_pci_pf_set(struct devlink_port *devlink_port, u32 controller,
10025                                    u16 pf, bool external)
10026 {
10027         struct devlink_port_attrs *attrs = &devlink_port->attrs;
10028         int ret;
10029
10030         ASSERT_DEVLINK_PORT_NOT_REGISTERED(devlink_port);
10031
10032         ret = __devlink_port_attrs_set(devlink_port,
10033                                        DEVLINK_PORT_FLAVOUR_PCI_PF);
10034         if (ret)
10035                 return;
10036         attrs->pci_pf.controller = controller;
10037         attrs->pci_pf.pf = pf;
10038         attrs->pci_pf.external = external;
10039 }
10040 EXPORT_SYMBOL_GPL(devlink_port_attrs_pci_pf_set);
10041
10042 /**
10043  *      devlink_port_attrs_pci_vf_set - Set PCI VF port attributes
10044  *
10045  *      @devlink_port: devlink port
10046  *      @controller: associated controller number for the devlink port instance
10047  *      @pf: associated PF for the devlink port instance
10048  *      @vf: associated VF of a PF for the devlink port instance
10049  *      @external: indicates if the port is for an external controller
10050  */
10051 void devlink_port_attrs_pci_vf_set(struct devlink_port *devlink_port, u32 controller,
10052                                    u16 pf, u16 vf, bool external)
10053 {
10054         struct devlink_port_attrs *attrs = &devlink_port->attrs;
10055         int ret;
10056
10057         ASSERT_DEVLINK_PORT_NOT_REGISTERED(devlink_port);
10058
10059         ret = __devlink_port_attrs_set(devlink_port,
10060                                        DEVLINK_PORT_FLAVOUR_PCI_VF);
10061         if (ret)
10062                 return;
10063         attrs->pci_vf.controller = controller;
10064         attrs->pci_vf.pf = pf;
10065         attrs->pci_vf.vf = vf;
10066         attrs->pci_vf.external = external;
10067 }
10068 EXPORT_SYMBOL_GPL(devlink_port_attrs_pci_vf_set);
10069
10070 /**
10071  *      devlink_port_attrs_pci_sf_set - Set PCI SF port attributes
10072  *
10073  *      @devlink_port: devlink port
10074  *      @controller: associated controller number for the devlink port instance
10075  *      @pf: associated PF for the devlink port instance
10076  *      @sf: associated SF of a PF for the devlink port instance
10077  *      @external: indicates if the port is for an external controller
10078  */
10079 void devlink_port_attrs_pci_sf_set(struct devlink_port *devlink_port, u32 controller,
10080                                    u16 pf, u32 sf, bool external)
10081 {
10082         struct devlink_port_attrs *attrs = &devlink_port->attrs;
10083         int ret;
10084
10085         ASSERT_DEVLINK_PORT_NOT_REGISTERED(devlink_port);
10086
10087         ret = __devlink_port_attrs_set(devlink_port,
10088                                        DEVLINK_PORT_FLAVOUR_PCI_SF);
10089         if (ret)
10090                 return;
10091         attrs->pci_sf.controller = controller;
10092         attrs->pci_sf.pf = pf;
10093         attrs->pci_sf.sf = sf;
10094         attrs->pci_sf.external = external;
10095 }
10096 EXPORT_SYMBOL_GPL(devlink_port_attrs_pci_sf_set);
10097
10098 /**
10099  * devl_rate_node_create - create devlink rate node
10100  * @devlink: devlink instance
10101  * @priv: driver private data
10102  * @node_name: name of the resulting node
10103  * @parent: parent devlink_rate struct
10104  *
10105  * Create devlink rate object of type node
10106  */
10107 struct devlink_rate *
10108 devl_rate_node_create(struct devlink *devlink, void *priv, char *node_name,
10109                       struct devlink_rate *parent)
10110 {
10111         struct devlink_rate *rate_node;
10112
10113         rate_node = devlink_rate_node_get_by_name(devlink, node_name);
10114         if (!IS_ERR(rate_node))
10115                 return ERR_PTR(-EEXIST);
10116
10117         rate_node = kzalloc(sizeof(*rate_node), GFP_KERNEL);
10118         if (!rate_node)
10119                 return ERR_PTR(-ENOMEM);
10120
10121         if (parent) {
10122                 rate_node->parent = parent;
10123                 refcount_inc(&rate_node->parent->refcnt);
10124         }
10125
10126         rate_node->type = DEVLINK_RATE_TYPE_NODE;
10127         rate_node->devlink = devlink;
10128         rate_node->priv = priv;
10129
10130         rate_node->name = kstrdup(node_name, GFP_KERNEL);
10131         if (!rate_node->name) {
10132                 kfree(rate_node);
10133                 return ERR_PTR(-ENOMEM);
10134         }
10135
10136         refcount_set(&rate_node->refcnt, 1);
10137         list_add(&rate_node->list, &devlink->rate_list);
10138         devlink_rate_notify(rate_node, DEVLINK_CMD_RATE_NEW);
10139         return rate_node;
10140 }
10141 EXPORT_SYMBOL_GPL(devl_rate_node_create);
10142
10143 /**
10144  * devl_rate_leaf_create - create devlink rate leaf
10145  * @devlink_port: devlink port object to create rate object on
10146  * @priv: driver private data
10147  * @parent: parent devlink_rate struct
10148  *
10149  * Create devlink rate object of type leaf on provided @devlink_port.
10150  */
10151 int devl_rate_leaf_create(struct devlink_port *devlink_port, void *priv,
10152                           struct devlink_rate *parent)
10153 {
10154         struct devlink *devlink = devlink_port->devlink;
10155         struct devlink_rate *devlink_rate;
10156
10157         devl_assert_locked(devlink_port->devlink);
10158
10159         if (WARN_ON(devlink_port->devlink_rate))
10160                 return -EBUSY;
10161
10162         devlink_rate = kzalloc(sizeof(*devlink_rate), GFP_KERNEL);
10163         if (!devlink_rate)
10164                 return -ENOMEM;
10165
10166         if (parent) {
10167                 devlink_rate->parent = parent;
10168                 refcount_inc(&devlink_rate->parent->refcnt);
10169         }
10170
10171         devlink_rate->type = DEVLINK_RATE_TYPE_LEAF;
10172         devlink_rate->devlink = devlink;
10173         devlink_rate->devlink_port = devlink_port;
10174         devlink_rate->priv = priv;
10175         list_add_tail(&devlink_rate->list, &devlink->rate_list);
10176         devlink_port->devlink_rate = devlink_rate;
10177         devlink_rate_notify(devlink_rate, DEVLINK_CMD_RATE_NEW);
10178
10179         return 0;
10180 }
10181 EXPORT_SYMBOL_GPL(devl_rate_leaf_create);
10182
10183 /**
10184  * devl_rate_leaf_destroy - destroy devlink rate leaf
10185  *
10186  * @devlink_port: devlink port linked to the rate object
10187  *
10188  * Destroy the devlink rate object of type leaf on provided @devlink_port.
10189  */
10190 void devl_rate_leaf_destroy(struct devlink_port *devlink_port)
10191 {
10192         struct devlink_rate *devlink_rate = devlink_port->devlink_rate;
10193
10194         devl_assert_locked(devlink_port->devlink);
10195         if (!devlink_rate)
10196                 return;
10197
10198         devlink_rate_notify(devlink_rate, DEVLINK_CMD_RATE_DEL);
10199         if (devlink_rate->parent)
10200                 refcount_dec(&devlink_rate->parent->refcnt);
10201         list_del(&devlink_rate->list);
10202         devlink_port->devlink_rate = NULL;
10203         kfree(devlink_rate);
10204 }
10205 EXPORT_SYMBOL_GPL(devl_rate_leaf_destroy);
10206
10207 /**
10208  * devl_rate_nodes_destroy - destroy all devlink rate nodes on device
10209  * @devlink: devlink instance
10210  *
10211  * Unset parent for all rate objects and destroy all rate nodes
10212  * on specified device.
10213  */
10214 void devl_rate_nodes_destroy(struct devlink *devlink)
10215 {
10216         static struct devlink_rate *devlink_rate, *tmp;
10217         const struct devlink_ops *ops = devlink->ops;
10218
10219         devl_assert_locked(devlink);
10220
10221         list_for_each_entry(devlink_rate, &devlink->rate_list, list) {
10222                 if (!devlink_rate->parent)
10223                         continue;
10224
10225                 refcount_dec(&devlink_rate->parent->refcnt);
10226                 if (devlink_rate_is_leaf(devlink_rate))
10227                         ops->rate_leaf_parent_set(devlink_rate, NULL, devlink_rate->priv,
10228                                                   NULL, NULL);
10229                 else if (devlink_rate_is_node(devlink_rate))
10230                         ops->rate_node_parent_set(devlink_rate, NULL, devlink_rate->priv,
10231                                                   NULL, NULL);
10232         }
10233         list_for_each_entry_safe(devlink_rate, tmp, &devlink->rate_list, list) {
10234                 if (devlink_rate_is_node(devlink_rate)) {
10235                         ops->rate_node_del(devlink_rate, devlink_rate->priv, NULL);
10236                         list_del(&devlink_rate->list);
10237                         kfree(devlink_rate->name);
10238                         kfree(devlink_rate);
10239                 }
10240         }
10241 }
10242 EXPORT_SYMBOL_GPL(devl_rate_nodes_destroy);
10243
10244 /**
10245  *      devlink_port_linecard_set - Link port with a linecard
10246  *
10247  *      @devlink_port: devlink port
10248  *      @linecard: devlink linecard
10249  */
10250 void devlink_port_linecard_set(struct devlink_port *devlink_port,
10251                                struct devlink_linecard *linecard)
10252 {
10253         ASSERT_DEVLINK_PORT_NOT_REGISTERED(devlink_port);
10254
10255         devlink_port->linecard = linecard;
10256 }
10257 EXPORT_SYMBOL_GPL(devlink_port_linecard_set);
10258
10259 static int __devlink_port_phys_port_name_get(struct devlink_port *devlink_port,
10260                                              char *name, size_t len)
10261 {
10262         struct devlink_port_attrs *attrs = &devlink_port->attrs;
10263         int n = 0;
10264
10265         if (!devlink_port->attrs_set)
10266                 return -EOPNOTSUPP;
10267
10268         switch (attrs->flavour) {
10269         case DEVLINK_PORT_FLAVOUR_PHYSICAL:
10270                 if (devlink_port->linecard)
10271                         n = snprintf(name, len, "l%u",
10272                                      devlink_port->linecard->index);
10273                 if (n < len)
10274                         n += snprintf(name + n, len - n, "p%u",
10275                                       attrs->phys.port_number);
10276                 if (n < len && attrs->split)
10277                         n += snprintf(name + n, len - n, "s%u",
10278                                       attrs->phys.split_subport_number);
10279                 break;
10280         case DEVLINK_PORT_FLAVOUR_CPU:
10281         case DEVLINK_PORT_FLAVOUR_DSA:
10282         case DEVLINK_PORT_FLAVOUR_UNUSED:
10283                 /* As CPU and DSA ports do not have a netdevice associated
10284                  * case should not ever happen.
10285                  */
10286                 WARN_ON(1);
10287                 return -EINVAL;
10288         case DEVLINK_PORT_FLAVOUR_PCI_PF:
10289                 if (attrs->pci_pf.external) {
10290                         n = snprintf(name, len, "c%u", attrs->pci_pf.controller);
10291                         if (n >= len)
10292                                 return -EINVAL;
10293                         len -= n;
10294                         name += n;
10295                 }
10296                 n = snprintf(name, len, "pf%u", attrs->pci_pf.pf);
10297                 break;
10298         case DEVLINK_PORT_FLAVOUR_PCI_VF:
10299                 if (attrs->pci_vf.external) {
10300                         n = snprintf(name, len, "c%u", attrs->pci_vf.controller);
10301                         if (n >= len)
10302                                 return -EINVAL;
10303                         len -= n;
10304                         name += n;
10305                 }
10306                 n = snprintf(name, len, "pf%uvf%u",
10307                              attrs->pci_vf.pf, attrs->pci_vf.vf);
10308                 break;
10309         case DEVLINK_PORT_FLAVOUR_PCI_SF:
10310                 if (attrs->pci_sf.external) {
10311                         n = snprintf(name, len, "c%u", attrs->pci_sf.controller);
10312                         if (n >= len)
10313                                 return -EINVAL;
10314                         len -= n;
10315                         name += n;
10316                 }
10317                 n = snprintf(name, len, "pf%usf%u", attrs->pci_sf.pf,
10318                              attrs->pci_sf.sf);
10319                 break;
10320         case DEVLINK_PORT_FLAVOUR_VIRTUAL:
10321                 return -EOPNOTSUPP;
10322         }
10323
10324         if (n >= len)
10325                 return -EINVAL;
10326
10327         return 0;
10328 }
10329
10330 static int devlink_linecard_types_init(struct devlink_linecard *linecard)
10331 {
10332         struct devlink_linecard_type *linecard_type;
10333         unsigned int count;
10334         int i;
10335
10336         count = linecard->ops->types_count(linecard, linecard->priv);
10337         linecard->types = kmalloc_array(count, sizeof(*linecard_type),
10338                                         GFP_KERNEL);
10339         if (!linecard->types)
10340                 return -ENOMEM;
10341         linecard->types_count = count;
10342
10343         for (i = 0; i < count; i++) {
10344                 linecard_type = &linecard->types[i];
10345                 linecard->ops->types_get(linecard, linecard->priv, i,
10346                                          &linecard_type->type,
10347                                          &linecard_type->priv);
10348         }
10349         return 0;
10350 }
10351
10352 static void devlink_linecard_types_fini(struct devlink_linecard *linecard)
10353 {
10354         kfree(linecard->types);
10355 }
10356
10357 /**
10358  *      devlink_linecard_create - Create devlink linecard
10359  *
10360  *      @devlink: devlink
10361  *      @linecard_index: driver-specific numerical identifier of the linecard
10362  *      @ops: linecards ops
10363  *      @priv: user priv pointer
10364  *
10365  *      Create devlink linecard instance with provided linecard index.
10366  *      Caller can use any indexing, even hw-related one.
10367  *
10368  *      Return: Line card structure or an ERR_PTR() encoded error code.
10369  */
10370 struct devlink_linecard *
10371 devlink_linecard_create(struct devlink *devlink, unsigned int linecard_index,
10372                         const struct devlink_linecard_ops *ops, void *priv)
10373 {
10374         struct devlink_linecard *linecard;
10375         int err;
10376
10377         if (WARN_ON(!ops || !ops->provision || !ops->unprovision ||
10378                     !ops->types_count || !ops->types_get))
10379                 return ERR_PTR(-EINVAL);
10380
10381         mutex_lock(&devlink->linecards_lock);
10382         if (devlink_linecard_index_exists(devlink, linecard_index)) {
10383                 mutex_unlock(&devlink->linecards_lock);
10384                 return ERR_PTR(-EEXIST);
10385         }
10386
10387         linecard = kzalloc(sizeof(*linecard), GFP_KERNEL);
10388         if (!linecard) {
10389                 mutex_unlock(&devlink->linecards_lock);
10390                 return ERR_PTR(-ENOMEM);
10391         }
10392
10393         linecard->devlink = devlink;
10394         linecard->index = linecard_index;
10395         linecard->ops = ops;
10396         linecard->priv = priv;
10397         linecard->state = DEVLINK_LINECARD_STATE_UNPROVISIONED;
10398         mutex_init(&linecard->state_lock);
10399
10400         err = devlink_linecard_types_init(linecard);
10401         if (err) {
10402                 mutex_destroy(&linecard->state_lock);
10403                 kfree(linecard);
10404                 mutex_unlock(&devlink->linecards_lock);
10405                 return ERR_PTR(err);
10406         }
10407
10408         list_add_tail(&linecard->list, &devlink->linecard_list);
10409         refcount_set(&linecard->refcount, 1);
10410         mutex_unlock(&devlink->linecards_lock);
10411         devlink_linecard_notify(linecard, DEVLINK_CMD_LINECARD_NEW);
10412         return linecard;
10413 }
10414 EXPORT_SYMBOL_GPL(devlink_linecard_create);
10415
10416 /**
10417  *      devlink_linecard_destroy - Destroy devlink linecard
10418  *
10419  *      @linecard: devlink linecard
10420  */
10421 void devlink_linecard_destroy(struct devlink_linecard *linecard)
10422 {
10423         struct devlink *devlink = linecard->devlink;
10424
10425         devlink_linecard_notify(linecard, DEVLINK_CMD_LINECARD_DEL);
10426         mutex_lock(&devlink->linecards_lock);
10427         list_del(&linecard->list);
10428         devlink_linecard_types_fini(linecard);
10429         mutex_unlock(&devlink->linecards_lock);
10430         devlink_linecard_put(linecard);
10431 }
10432 EXPORT_SYMBOL_GPL(devlink_linecard_destroy);
10433
10434 /**
10435  *      devlink_linecard_provision_set - Set provisioning on linecard
10436  *
10437  *      @linecard: devlink linecard
10438  *      @type: linecard type
10439  *
10440  *      This is either called directly from the provision() op call or
10441  *      as a result of the provision() op call asynchronously.
10442  */
10443 void devlink_linecard_provision_set(struct devlink_linecard *linecard,
10444                                     const char *type)
10445 {
10446         mutex_lock(&linecard->state_lock);
10447         WARN_ON(linecard->type && strcmp(linecard->type, type));
10448         linecard->state = DEVLINK_LINECARD_STATE_PROVISIONED;
10449         linecard->type = type;
10450         devlink_linecard_notify(linecard, DEVLINK_CMD_LINECARD_NEW);
10451         mutex_unlock(&linecard->state_lock);
10452 }
10453 EXPORT_SYMBOL_GPL(devlink_linecard_provision_set);
10454
10455 /**
10456  *      devlink_linecard_provision_clear - Clear provisioning on linecard
10457  *
10458  *      @linecard: devlink linecard
10459  *
10460  *      This is either called directly from the unprovision() op call or
10461  *      as a result of the unprovision() op call asynchronously.
10462  */
10463 void devlink_linecard_provision_clear(struct devlink_linecard *linecard)
10464 {
10465         mutex_lock(&linecard->state_lock);
10466         WARN_ON(linecard->nested_devlink);
10467         linecard->state = DEVLINK_LINECARD_STATE_UNPROVISIONED;
10468         linecard->type = NULL;
10469         devlink_linecard_notify(linecard, DEVLINK_CMD_LINECARD_NEW);
10470         mutex_unlock(&linecard->state_lock);
10471 }
10472 EXPORT_SYMBOL_GPL(devlink_linecard_provision_clear);
10473
10474 /**
10475  *      devlink_linecard_provision_fail - Fail provisioning on linecard
10476  *
10477  *      @linecard: devlink linecard
10478  *
10479  *      This is either called directly from the provision() op call or
10480  *      as a result of the provision() op call asynchronously.
10481  */
10482 void devlink_linecard_provision_fail(struct devlink_linecard *linecard)
10483 {
10484         mutex_lock(&linecard->state_lock);
10485         WARN_ON(linecard->nested_devlink);
10486         linecard->state = DEVLINK_LINECARD_STATE_PROVISIONING_FAILED;
10487         devlink_linecard_notify(linecard, DEVLINK_CMD_LINECARD_NEW);
10488         mutex_unlock(&linecard->state_lock);
10489 }
10490 EXPORT_SYMBOL_GPL(devlink_linecard_provision_fail);
10491
10492 /**
10493  *      devlink_linecard_activate - Set linecard active
10494  *
10495  *      @linecard: devlink linecard
10496  */
10497 void devlink_linecard_activate(struct devlink_linecard *linecard)
10498 {
10499         mutex_lock(&linecard->state_lock);
10500         WARN_ON(linecard->state != DEVLINK_LINECARD_STATE_PROVISIONED);
10501         linecard->state = DEVLINK_LINECARD_STATE_ACTIVE;
10502         devlink_linecard_notify(linecard, DEVLINK_CMD_LINECARD_NEW);
10503         mutex_unlock(&linecard->state_lock);
10504 }
10505 EXPORT_SYMBOL_GPL(devlink_linecard_activate);
10506
10507 /**
10508  *      devlink_linecard_deactivate - Set linecard inactive
10509  *
10510  *      @linecard: devlink linecard
10511  */
10512 void devlink_linecard_deactivate(struct devlink_linecard *linecard)
10513 {
10514         mutex_lock(&linecard->state_lock);
10515         switch (linecard->state) {
10516         case DEVLINK_LINECARD_STATE_ACTIVE:
10517                 linecard->state = DEVLINK_LINECARD_STATE_PROVISIONED;
10518                 devlink_linecard_notify(linecard, DEVLINK_CMD_LINECARD_NEW);
10519                 break;
10520         case DEVLINK_LINECARD_STATE_UNPROVISIONING:
10521                 /* Line card is being deactivated as part
10522                  * of unprovisioning flow.
10523                  */
10524                 break;
10525         default:
10526                 WARN_ON(1);
10527                 break;
10528         }
10529         mutex_unlock(&linecard->state_lock);
10530 }
10531 EXPORT_SYMBOL_GPL(devlink_linecard_deactivate);
10532
10533 /**
10534  *      devlink_linecard_nested_dl_set - Attach/detach nested devlink
10535  *                                       instance to linecard.
10536  *
10537  *      @linecard: devlink linecard
10538  *      @nested_devlink: devlink instance to attach or NULL to detach
10539  */
10540 void devlink_linecard_nested_dl_set(struct devlink_linecard *linecard,
10541                                     struct devlink *nested_devlink)
10542 {
10543         mutex_lock(&linecard->state_lock);
10544         linecard->nested_devlink = nested_devlink;
10545         devlink_linecard_notify(linecard, DEVLINK_CMD_LINECARD_NEW);
10546         mutex_unlock(&linecard->state_lock);
10547 }
10548 EXPORT_SYMBOL_GPL(devlink_linecard_nested_dl_set);
10549
10550 int devl_sb_register(struct devlink *devlink, unsigned int sb_index,
10551                      u32 size, u16 ingress_pools_count,
10552                      u16 egress_pools_count, u16 ingress_tc_count,
10553                      u16 egress_tc_count)
10554 {
10555         struct devlink_sb *devlink_sb;
10556
10557         lockdep_assert_held(&devlink->lock);
10558
10559         if (devlink_sb_index_exists(devlink, sb_index))
10560                 return -EEXIST;
10561
10562         devlink_sb = kzalloc(sizeof(*devlink_sb), GFP_KERNEL);
10563         if (!devlink_sb)
10564                 return -ENOMEM;
10565         devlink_sb->index = sb_index;
10566         devlink_sb->size = size;
10567         devlink_sb->ingress_pools_count = ingress_pools_count;
10568         devlink_sb->egress_pools_count = egress_pools_count;
10569         devlink_sb->ingress_tc_count = ingress_tc_count;
10570         devlink_sb->egress_tc_count = egress_tc_count;
10571         list_add_tail(&devlink_sb->list, &devlink->sb_list);
10572         return 0;
10573 }
10574 EXPORT_SYMBOL_GPL(devl_sb_register);
10575
10576 int devlink_sb_register(struct devlink *devlink, unsigned int sb_index,
10577                         u32 size, u16 ingress_pools_count,
10578                         u16 egress_pools_count, u16 ingress_tc_count,
10579                         u16 egress_tc_count)
10580 {
10581         int err;
10582
10583         devl_lock(devlink);
10584         err = devl_sb_register(devlink, sb_index, size, ingress_pools_count,
10585                                egress_pools_count, ingress_tc_count,
10586                                egress_tc_count);
10587         devl_unlock(devlink);
10588         return err;
10589 }
10590 EXPORT_SYMBOL_GPL(devlink_sb_register);
10591
10592 void devl_sb_unregister(struct devlink *devlink, unsigned int sb_index)
10593 {
10594         struct devlink_sb *devlink_sb;
10595
10596         lockdep_assert_held(&devlink->lock);
10597
10598         devlink_sb = devlink_sb_get_by_index(devlink, sb_index);
10599         WARN_ON(!devlink_sb);
10600         list_del(&devlink_sb->list);
10601         kfree(devlink_sb);
10602 }
10603 EXPORT_SYMBOL_GPL(devl_sb_unregister);
10604
10605 void devlink_sb_unregister(struct devlink *devlink, unsigned int sb_index)
10606 {
10607         devl_lock(devlink);
10608         devl_sb_unregister(devlink, sb_index);
10609         devl_unlock(devlink);
10610 }
10611 EXPORT_SYMBOL_GPL(devlink_sb_unregister);
10612
10613 /**
10614  * devl_dpipe_headers_register - register dpipe headers
10615  *
10616  * @devlink: devlink
10617  * @dpipe_headers: dpipe header array
10618  *
10619  * Register the headers supported by hardware.
10620  */
10621 void devl_dpipe_headers_register(struct devlink *devlink,
10622                                  struct devlink_dpipe_headers *dpipe_headers)
10623 {
10624         lockdep_assert_held(&devlink->lock);
10625
10626         devlink->dpipe_headers = dpipe_headers;
10627 }
10628 EXPORT_SYMBOL_GPL(devl_dpipe_headers_register);
10629
10630 /**
10631  * devl_dpipe_headers_unregister - unregister dpipe headers
10632  *
10633  * @devlink: devlink
10634  *
10635  * Unregister the headers supported by hardware.
10636  */
10637 void devl_dpipe_headers_unregister(struct devlink *devlink)
10638 {
10639         lockdep_assert_held(&devlink->lock);
10640
10641         devlink->dpipe_headers = NULL;
10642 }
10643 EXPORT_SYMBOL_GPL(devl_dpipe_headers_unregister);
10644
10645 /**
10646  *      devlink_dpipe_table_counter_enabled - check if counter allocation
10647  *                                            required
10648  *      @devlink: devlink
10649  *      @table_name: tables name
10650  *
10651  *      Used by driver to check if counter allocation is required.
10652  *      After counter allocation is turned on the table entries
10653  *      are updated to include counter statistics.
10654  *
10655  *      After that point on the driver must respect the counter
10656  *      state so that each entry added to the table is added
10657  *      with a counter.
10658  */
10659 bool devlink_dpipe_table_counter_enabled(struct devlink *devlink,
10660                                          const char *table_name)
10661 {
10662         struct devlink_dpipe_table *table;
10663         bool enabled;
10664
10665         rcu_read_lock();
10666         table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
10667                                          table_name, devlink);
10668         enabled = false;
10669         if (table)
10670                 enabled = table->counters_enabled;
10671         rcu_read_unlock();
10672         return enabled;
10673 }
10674 EXPORT_SYMBOL_GPL(devlink_dpipe_table_counter_enabled);
10675
10676 /**
10677  * devl_dpipe_table_register - register dpipe table
10678  *
10679  * @devlink: devlink
10680  * @table_name: table name
10681  * @table_ops: table ops
10682  * @priv: priv
10683  * @counter_control_extern: external control for counters
10684  */
10685 int devl_dpipe_table_register(struct devlink *devlink,
10686                               const char *table_name,
10687                               struct devlink_dpipe_table_ops *table_ops,
10688                               void *priv, bool counter_control_extern)
10689 {
10690         struct devlink_dpipe_table *table;
10691
10692         lockdep_assert_held(&devlink->lock);
10693
10694         if (WARN_ON(!table_ops->size_get))
10695                 return -EINVAL;
10696
10697         if (devlink_dpipe_table_find(&devlink->dpipe_table_list, table_name,
10698                                      devlink))
10699                 return -EEXIST;
10700
10701         table = kzalloc(sizeof(*table), GFP_KERNEL);
10702         if (!table)
10703                 return -ENOMEM;
10704
10705         table->name = table_name;
10706         table->table_ops = table_ops;
10707         table->priv = priv;
10708         table->counter_control_extern = counter_control_extern;
10709
10710         list_add_tail_rcu(&table->list, &devlink->dpipe_table_list);
10711
10712         return 0;
10713 }
10714 EXPORT_SYMBOL_GPL(devl_dpipe_table_register);
10715
10716 /**
10717  * devl_dpipe_table_unregister - unregister dpipe table
10718  *
10719  * @devlink: devlink
10720  * @table_name: table name
10721  */
10722 void devl_dpipe_table_unregister(struct devlink *devlink,
10723                                  const char *table_name)
10724 {
10725         struct devlink_dpipe_table *table;
10726
10727         lockdep_assert_held(&devlink->lock);
10728
10729         table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
10730                                          table_name, devlink);
10731         if (!table)
10732                 return;
10733         list_del_rcu(&table->list);
10734         kfree_rcu(table, rcu);
10735 }
10736 EXPORT_SYMBOL_GPL(devl_dpipe_table_unregister);
10737
10738 /**
10739  * devl_resource_register - devlink resource register
10740  *
10741  * @devlink: devlink
10742  * @resource_name: resource's name
10743  * @resource_size: resource's size
10744  * @resource_id: resource's id
10745  * @parent_resource_id: resource's parent id
10746  * @size_params: size parameters
10747  *
10748  * Generic resources should reuse the same names across drivers.
10749  * Please see the generic resources list at:
10750  * Documentation/networking/devlink/devlink-resource.rst
10751  */
10752 int devl_resource_register(struct devlink *devlink,
10753                            const char *resource_name,
10754                            u64 resource_size,
10755                            u64 resource_id,
10756                            u64 parent_resource_id,
10757                            const struct devlink_resource_size_params *size_params)
10758 {
10759         struct devlink_resource *resource;
10760         struct list_head *resource_list;
10761         bool top_hierarchy;
10762
10763         lockdep_assert_held(&devlink->lock);
10764
10765         top_hierarchy = parent_resource_id == DEVLINK_RESOURCE_ID_PARENT_TOP;
10766
10767         resource = devlink_resource_find(devlink, NULL, resource_id);
10768         if (resource)
10769                 return -EINVAL;
10770
10771         resource = kzalloc(sizeof(*resource), GFP_KERNEL);
10772         if (!resource)
10773                 return -ENOMEM;
10774
10775         if (top_hierarchy) {
10776                 resource_list = &devlink->resource_list;
10777         } else {
10778                 struct devlink_resource *parent_resource;
10779
10780                 parent_resource = devlink_resource_find(devlink, NULL,
10781                                                         parent_resource_id);
10782                 if (parent_resource) {
10783                         resource_list = &parent_resource->resource_list;
10784                         resource->parent = parent_resource;
10785                 } else {
10786                         kfree(resource);
10787                         return -EINVAL;
10788                 }
10789         }
10790
10791         resource->name = resource_name;
10792         resource->size = resource_size;
10793         resource->size_new = resource_size;
10794         resource->id = resource_id;
10795         resource->size_valid = true;
10796         memcpy(&resource->size_params, size_params,
10797                sizeof(resource->size_params));
10798         INIT_LIST_HEAD(&resource->resource_list);
10799         list_add_tail(&resource->list, resource_list);
10800
10801         return 0;
10802 }
10803 EXPORT_SYMBOL_GPL(devl_resource_register);
10804
10805 /**
10806  *      devlink_resource_register - devlink resource register
10807  *
10808  *      @devlink: devlink
10809  *      @resource_name: resource's name
10810  *      @resource_size: resource's size
10811  *      @resource_id: resource's id
10812  *      @parent_resource_id: resource's parent id
10813  *      @size_params: size parameters
10814  *
10815  *      Generic resources should reuse the same names across drivers.
10816  *      Please see the generic resources list at:
10817  *      Documentation/networking/devlink/devlink-resource.rst
10818  *
10819  *      Context: Takes and release devlink->lock <mutex>.
10820  */
10821 int devlink_resource_register(struct devlink *devlink,
10822                               const char *resource_name,
10823                               u64 resource_size,
10824                               u64 resource_id,
10825                               u64 parent_resource_id,
10826                               const struct devlink_resource_size_params *size_params)
10827 {
10828         int err;
10829
10830         devl_lock(devlink);
10831         err = devl_resource_register(devlink, resource_name, resource_size,
10832                                      resource_id, parent_resource_id, size_params);
10833         devl_unlock(devlink);
10834         return err;
10835 }
10836 EXPORT_SYMBOL_GPL(devlink_resource_register);
10837
10838 static void devlink_resource_unregister(struct devlink *devlink,
10839                                         struct devlink_resource *resource)
10840 {
10841         struct devlink_resource *tmp, *child_resource;
10842
10843         list_for_each_entry_safe(child_resource, tmp, &resource->resource_list,
10844                                  list) {
10845                 devlink_resource_unregister(devlink, child_resource);
10846                 list_del(&child_resource->list);
10847                 kfree(child_resource);
10848         }
10849 }
10850
10851 /**
10852  * devl_resources_unregister - free all resources
10853  *
10854  * @devlink: devlink
10855  */
10856 void devl_resources_unregister(struct devlink *devlink)
10857 {
10858         struct devlink_resource *tmp, *child_resource;
10859
10860         lockdep_assert_held(&devlink->lock);
10861
10862         list_for_each_entry_safe(child_resource, tmp, &devlink->resource_list,
10863                                  list) {
10864                 devlink_resource_unregister(devlink, child_resource);
10865                 list_del(&child_resource->list);
10866                 kfree(child_resource);
10867         }
10868 }
10869 EXPORT_SYMBOL_GPL(devl_resources_unregister);
10870
10871 /**
10872  *      devlink_resources_unregister - free all resources
10873  *
10874  *      @devlink: devlink
10875  *
10876  *      Context: Takes and release devlink->lock <mutex>.
10877  */
10878 void devlink_resources_unregister(struct devlink *devlink)
10879 {
10880         devl_lock(devlink);
10881         devl_resources_unregister(devlink);
10882         devl_unlock(devlink);
10883 }
10884 EXPORT_SYMBOL_GPL(devlink_resources_unregister);
10885
10886 /**
10887  * devl_resource_size_get - get and update size
10888  *
10889  * @devlink: devlink
10890  * @resource_id: the requested resource id
10891  * @p_resource_size: ptr to update
10892  */
10893 int devl_resource_size_get(struct devlink *devlink,
10894                            u64 resource_id,
10895                            u64 *p_resource_size)
10896 {
10897         struct devlink_resource *resource;
10898
10899         lockdep_assert_held(&devlink->lock);
10900
10901         resource = devlink_resource_find(devlink, NULL, resource_id);
10902         if (!resource)
10903                 return -EINVAL;
10904         *p_resource_size = resource->size_new;
10905         resource->size = resource->size_new;
10906         return 0;
10907 }
10908 EXPORT_SYMBOL_GPL(devl_resource_size_get);
10909
10910 /**
10911  * devl_dpipe_table_resource_set - set the resource id
10912  *
10913  * @devlink: devlink
10914  * @table_name: table name
10915  * @resource_id: resource id
10916  * @resource_units: number of resource's units consumed per table's entry
10917  */
10918 int devl_dpipe_table_resource_set(struct devlink *devlink,
10919                                   const char *table_name, u64 resource_id,
10920                                   u64 resource_units)
10921 {
10922         struct devlink_dpipe_table *table;
10923
10924         table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
10925                                          table_name, devlink);
10926         if (!table)
10927                 return -EINVAL;
10928
10929         table->resource_id = resource_id;
10930         table->resource_units = resource_units;
10931         table->resource_valid = true;
10932         return 0;
10933 }
10934 EXPORT_SYMBOL_GPL(devl_dpipe_table_resource_set);
10935
10936 /**
10937  * devl_resource_occ_get_register - register occupancy getter
10938  *
10939  * @devlink: devlink
10940  * @resource_id: resource id
10941  * @occ_get: occupancy getter callback
10942  * @occ_get_priv: occupancy getter callback priv
10943  */
10944 void devl_resource_occ_get_register(struct devlink *devlink,
10945                                     u64 resource_id,
10946                                     devlink_resource_occ_get_t *occ_get,
10947                                     void *occ_get_priv)
10948 {
10949         struct devlink_resource *resource;
10950
10951         lockdep_assert_held(&devlink->lock);
10952
10953         resource = devlink_resource_find(devlink, NULL, resource_id);
10954         if (WARN_ON(!resource))
10955                 return;
10956         WARN_ON(resource->occ_get);
10957
10958         resource->occ_get = occ_get;
10959         resource->occ_get_priv = occ_get_priv;
10960 }
10961 EXPORT_SYMBOL_GPL(devl_resource_occ_get_register);
10962
10963 /**
10964  *      devlink_resource_occ_get_register - register occupancy getter
10965  *
10966  *      @devlink: devlink
10967  *      @resource_id: resource id
10968  *      @occ_get: occupancy getter callback
10969  *      @occ_get_priv: occupancy getter callback priv
10970  *
10971  *      Context: Takes and release devlink->lock <mutex>.
10972  */
10973 void devlink_resource_occ_get_register(struct devlink *devlink,
10974                                        u64 resource_id,
10975                                        devlink_resource_occ_get_t *occ_get,
10976                                        void *occ_get_priv)
10977 {
10978         devl_lock(devlink);
10979         devl_resource_occ_get_register(devlink, resource_id,
10980                                        occ_get, occ_get_priv);
10981         devl_unlock(devlink);
10982 }
10983 EXPORT_SYMBOL_GPL(devlink_resource_occ_get_register);
10984
10985 /**
10986  * devl_resource_occ_get_unregister - unregister occupancy getter
10987  *
10988  * @devlink: devlink
10989  * @resource_id: resource id
10990  */
10991 void devl_resource_occ_get_unregister(struct devlink *devlink,
10992                                       u64 resource_id)
10993 {
10994         struct devlink_resource *resource;
10995
10996         lockdep_assert_held(&devlink->lock);
10997
10998         resource = devlink_resource_find(devlink, NULL, resource_id);
10999         if (WARN_ON(!resource))
11000                 return;
11001         WARN_ON(!resource->occ_get);
11002
11003         resource->occ_get = NULL;
11004         resource->occ_get_priv = NULL;
11005 }
11006 EXPORT_SYMBOL_GPL(devl_resource_occ_get_unregister);
11007
11008 /**
11009  *      devlink_resource_occ_get_unregister - unregister occupancy getter
11010  *
11011  *      @devlink: devlink
11012  *      @resource_id: resource id
11013  *
11014  *      Context: Takes and release devlink->lock <mutex>.
11015  */
11016 void devlink_resource_occ_get_unregister(struct devlink *devlink,
11017                                          u64 resource_id)
11018 {
11019         devl_lock(devlink);
11020         devl_resource_occ_get_unregister(devlink, resource_id);
11021         devl_unlock(devlink);
11022 }
11023 EXPORT_SYMBOL_GPL(devlink_resource_occ_get_unregister);
11024
11025 static int devlink_param_verify(const struct devlink_param *param)
11026 {
11027         if (!param || !param->name || !param->supported_cmodes)
11028                 return -EINVAL;
11029         if (param->generic)
11030                 return devlink_param_generic_verify(param);
11031         else
11032                 return devlink_param_driver_verify(param);
11033 }
11034
11035 /**
11036  *      devlink_params_register - register configuration parameters
11037  *
11038  *      @devlink: devlink
11039  *      @params: configuration parameters array
11040  *      @params_count: number of parameters provided
11041  *
11042  *      Register the configuration parameters supported by the driver.
11043  */
11044 int devlink_params_register(struct devlink *devlink,
11045                             const struct devlink_param *params,
11046                             size_t params_count)
11047 {
11048         const struct devlink_param *param = params;
11049         int i, err;
11050
11051         ASSERT_DEVLINK_NOT_REGISTERED(devlink);
11052
11053         for (i = 0; i < params_count; i++, param++) {
11054                 err = devlink_param_register(devlink, param);
11055                 if (err)
11056                         goto rollback;
11057         }
11058         return 0;
11059
11060 rollback:
11061         if (!i)
11062                 return err;
11063
11064         for (param--; i > 0; i--, param--)
11065                 devlink_param_unregister(devlink, param);
11066         return err;
11067 }
11068 EXPORT_SYMBOL_GPL(devlink_params_register);
11069
11070 /**
11071  *      devlink_params_unregister - unregister configuration parameters
11072  *      @devlink: devlink
11073  *      @params: configuration parameters to unregister
11074  *      @params_count: number of parameters provided
11075  */
11076 void devlink_params_unregister(struct devlink *devlink,
11077                                const struct devlink_param *params,
11078                                size_t params_count)
11079 {
11080         const struct devlink_param *param = params;
11081         int i;
11082
11083         ASSERT_DEVLINK_NOT_REGISTERED(devlink);
11084
11085         for (i = 0; i < params_count; i++, param++)
11086                 devlink_param_unregister(devlink, param);
11087 }
11088 EXPORT_SYMBOL_GPL(devlink_params_unregister);
11089
11090 /**
11091  * devlink_param_register - register one configuration parameter
11092  *
11093  * @devlink: devlink
11094  * @param: one configuration parameter
11095  *
11096  * Register the configuration parameter supported by the driver.
11097  * Return: returns 0 on successful registration or error code otherwise.
11098  */
11099 int devlink_param_register(struct devlink *devlink,
11100                            const struct devlink_param *param)
11101 {
11102         struct devlink_param_item *param_item;
11103
11104         ASSERT_DEVLINK_NOT_REGISTERED(devlink);
11105
11106         WARN_ON(devlink_param_verify(param));
11107         WARN_ON(devlink_param_find_by_name(&devlink->param_list, param->name));
11108
11109         if (param->supported_cmodes == BIT(DEVLINK_PARAM_CMODE_DRIVERINIT))
11110                 WARN_ON(param->get || param->set);
11111         else
11112                 WARN_ON(!param->get || !param->set);
11113
11114         param_item = kzalloc(sizeof(*param_item), GFP_KERNEL);
11115         if (!param_item)
11116                 return -ENOMEM;
11117
11118         param_item->param = param;
11119
11120         list_add_tail(&param_item->list, &devlink->param_list);
11121         return 0;
11122 }
11123 EXPORT_SYMBOL_GPL(devlink_param_register);
11124
11125 /**
11126  * devlink_param_unregister - unregister one configuration parameter
11127  * @devlink: devlink
11128  * @param: configuration parameter to unregister
11129  */
11130 void devlink_param_unregister(struct devlink *devlink,
11131                               const struct devlink_param *param)
11132 {
11133         struct devlink_param_item *param_item;
11134
11135         ASSERT_DEVLINK_NOT_REGISTERED(devlink);
11136
11137         param_item =
11138                 devlink_param_find_by_name(&devlink->param_list, param->name);
11139         WARN_ON(!param_item);
11140         list_del(&param_item->list);
11141         kfree(param_item);
11142 }
11143 EXPORT_SYMBOL_GPL(devlink_param_unregister);
11144
11145 /**
11146  *      devlink_param_driverinit_value_get - get configuration parameter
11147  *                                           value for driver initializing
11148  *
11149  *      @devlink: devlink
11150  *      @param_id: parameter ID
11151  *      @init_val: value of parameter in driverinit configuration mode
11152  *
11153  *      This function should be used by the driver to get driverinit
11154  *      configuration for initialization after reload command.
11155  */
11156 int devlink_param_driverinit_value_get(struct devlink *devlink, u32 param_id,
11157                                        union devlink_param_value *init_val)
11158 {
11159         struct devlink_param_item *param_item;
11160
11161         if (!devlink_reload_supported(devlink->ops))
11162                 return -EOPNOTSUPP;
11163
11164         param_item = devlink_param_find_by_id(&devlink->param_list, param_id);
11165         if (!param_item)
11166                 return -EINVAL;
11167
11168         if (!param_item->driverinit_value_valid ||
11169             !devlink_param_cmode_is_supported(param_item->param,
11170                                               DEVLINK_PARAM_CMODE_DRIVERINIT))
11171                 return -EOPNOTSUPP;
11172
11173         if (param_item->param->type == DEVLINK_PARAM_TYPE_STRING)
11174                 strcpy(init_val->vstr, param_item->driverinit_value.vstr);
11175         else
11176                 *init_val = param_item->driverinit_value;
11177
11178         return 0;
11179 }
11180 EXPORT_SYMBOL_GPL(devlink_param_driverinit_value_get);
11181
11182 /**
11183  *      devlink_param_driverinit_value_set - set value of configuration
11184  *                                           parameter for driverinit
11185  *                                           configuration mode
11186  *
11187  *      @devlink: devlink
11188  *      @param_id: parameter ID
11189  *      @init_val: value of parameter to set for driverinit configuration mode
11190  *
11191  *      This function should be used by the driver to set driverinit
11192  *      configuration mode default value.
11193  */
11194 int devlink_param_driverinit_value_set(struct devlink *devlink, u32 param_id,
11195                                        union devlink_param_value init_val)
11196 {
11197         struct devlink_param_item *param_item;
11198
11199         ASSERT_DEVLINK_NOT_REGISTERED(devlink);
11200
11201         param_item = devlink_param_find_by_id(&devlink->param_list, param_id);
11202         if (!param_item)
11203                 return -EINVAL;
11204
11205         if (!devlink_param_cmode_is_supported(param_item->param,
11206                                               DEVLINK_PARAM_CMODE_DRIVERINIT))
11207                 return -EOPNOTSUPP;
11208
11209         if (param_item->param->type == DEVLINK_PARAM_TYPE_STRING)
11210                 strcpy(param_item->driverinit_value.vstr, init_val.vstr);
11211         else
11212                 param_item->driverinit_value = init_val;
11213         param_item->driverinit_value_valid = true;
11214         return 0;
11215 }
11216 EXPORT_SYMBOL_GPL(devlink_param_driverinit_value_set);
11217
11218 /**
11219  *      devlink_param_value_changed - notify devlink on a parameter's value
11220  *                                    change. Should be called by the driver
11221  *                                    right after the change.
11222  *
11223  *      @devlink: devlink
11224  *      @param_id: parameter ID
11225  *
11226  *      This function should be used by the driver to notify devlink on value
11227  *      change, excluding driverinit configuration mode.
11228  *      For driverinit configuration mode driver should use the function
11229  */
11230 void devlink_param_value_changed(struct devlink *devlink, u32 param_id)
11231 {
11232         struct devlink_param_item *param_item;
11233
11234         param_item = devlink_param_find_by_id(&devlink->param_list, param_id);
11235         WARN_ON(!param_item);
11236
11237         devlink_param_notify(devlink, 0, param_item, DEVLINK_CMD_PARAM_NEW);
11238 }
11239 EXPORT_SYMBOL_GPL(devlink_param_value_changed);
11240
11241 /**
11242  * devl_region_create - create a new address region
11243  *
11244  * @devlink: devlink
11245  * @ops: region operations and name
11246  * @region_max_snapshots: Maximum supported number of snapshots for region
11247  * @region_size: size of region
11248  */
11249 struct devlink_region *devl_region_create(struct devlink *devlink,
11250                                           const struct devlink_region_ops *ops,
11251                                           u32 region_max_snapshots,
11252                                           u64 region_size)
11253 {
11254         struct devlink_region *region;
11255
11256         devl_assert_locked(devlink);
11257
11258         if (WARN_ON(!ops) || WARN_ON(!ops->destructor))
11259                 return ERR_PTR(-EINVAL);
11260
11261         if (devlink_region_get_by_name(devlink, ops->name))
11262                 return ERR_PTR(-EEXIST);
11263
11264         region = kzalloc(sizeof(*region), GFP_KERNEL);
11265         if (!region)
11266                 return ERR_PTR(-ENOMEM);
11267
11268         region->devlink = devlink;
11269         region->max_snapshots = region_max_snapshots;
11270         region->ops = ops;
11271         region->size = region_size;
11272         INIT_LIST_HEAD(&region->snapshot_list);
11273         mutex_init(&region->snapshot_lock);
11274         list_add_tail(&region->list, &devlink->region_list);
11275         devlink_nl_region_notify(region, NULL, DEVLINK_CMD_REGION_NEW);
11276
11277         return region;
11278 }
11279 EXPORT_SYMBOL_GPL(devl_region_create);
11280
11281 /**
11282  *      devlink_region_create - create a new address region
11283  *
11284  *      @devlink: devlink
11285  *      @ops: region operations and name
11286  *      @region_max_snapshots: Maximum supported number of snapshots for region
11287  *      @region_size: size of region
11288  *
11289  *      Context: Takes and release devlink->lock <mutex>.
11290  */
11291 struct devlink_region *
11292 devlink_region_create(struct devlink *devlink,
11293                       const struct devlink_region_ops *ops,
11294                       u32 region_max_snapshots, u64 region_size)
11295 {
11296         struct devlink_region *region;
11297
11298         devl_lock(devlink);
11299         region = devl_region_create(devlink, ops, region_max_snapshots,
11300                                     region_size);
11301         devl_unlock(devlink);
11302         return region;
11303 }
11304 EXPORT_SYMBOL_GPL(devlink_region_create);
11305
11306 /**
11307  *      devlink_port_region_create - create a new address region for a port
11308  *
11309  *      @port: devlink port
11310  *      @ops: region operations and name
11311  *      @region_max_snapshots: Maximum supported number of snapshots for region
11312  *      @region_size: size of region
11313  *
11314  *      Context: Takes and release devlink->lock <mutex>.
11315  */
11316 struct devlink_region *
11317 devlink_port_region_create(struct devlink_port *port,
11318                            const struct devlink_port_region_ops *ops,
11319                            u32 region_max_snapshots, u64 region_size)
11320 {
11321         struct devlink *devlink = port->devlink;
11322         struct devlink_region *region;
11323         int err = 0;
11324
11325         ASSERT_DEVLINK_PORT_INITIALIZED(port);
11326
11327         if (WARN_ON(!ops) || WARN_ON(!ops->destructor))
11328                 return ERR_PTR(-EINVAL);
11329
11330         devl_lock(devlink);
11331
11332         if (devlink_port_region_get_by_name(port, ops->name)) {
11333                 err = -EEXIST;
11334                 goto unlock;
11335         }
11336
11337         region = kzalloc(sizeof(*region), GFP_KERNEL);
11338         if (!region) {
11339                 err = -ENOMEM;
11340                 goto unlock;
11341         }
11342
11343         region->devlink = devlink;
11344         region->port = port;
11345         region->max_snapshots = region_max_snapshots;
11346         region->port_ops = ops;
11347         region->size = region_size;
11348         INIT_LIST_HEAD(&region->snapshot_list);
11349         mutex_init(&region->snapshot_lock);
11350         list_add_tail(&region->list, &port->region_list);
11351         devlink_nl_region_notify(region, NULL, DEVLINK_CMD_REGION_NEW);
11352
11353         devl_unlock(devlink);
11354         return region;
11355
11356 unlock:
11357         devl_unlock(devlink);
11358         return ERR_PTR(err);
11359 }
11360 EXPORT_SYMBOL_GPL(devlink_port_region_create);
11361
11362 /**
11363  * devl_region_destroy - destroy address region
11364  *
11365  * @region: devlink region to destroy
11366  */
11367 void devl_region_destroy(struct devlink_region *region)
11368 {
11369         struct devlink *devlink = region->devlink;
11370         struct devlink_snapshot *snapshot, *ts;
11371
11372         devl_assert_locked(devlink);
11373
11374         /* Free all snapshots of region */
11375         mutex_lock(&region->snapshot_lock);
11376         list_for_each_entry_safe(snapshot, ts, &region->snapshot_list, list)
11377                 devlink_region_snapshot_del(region, snapshot);
11378         mutex_unlock(&region->snapshot_lock);
11379
11380         list_del(&region->list);
11381         mutex_destroy(&region->snapshot_lock);
11382
11383         devlink_nl_region_notify(region, NULL, DEVLINK_CMD_REGION_DEL);
11384         kfree(region);
11385 }
11386 EXPORT_SYMBOL_GPL(devl_region_destroy);
11387
11388 /**
11389  *      devlink_region_destroy - destroy address region
11390  *
11391  *      @region: devlink region to destroy
11392  *
11393  *      Context: Takes and release devlink->lock <mutex>.
11394  */
11395 void devlink_region_destroy(struct devlink_region *region)
11396 {
11397         struct devlink *devlink = region->devlink;
11398
11399         devl_lock(devlink);
11400         devl_region_destroy(region);
11401         devl_unlock(devlink);
11402 }
11403 EXPORT_SYMBOL_GPL(devlink_region_destroy);
11404
11405 /**
11406  *      devlink_region_snapshot_id_get - get snapshot ID
11407  *
11408  *      This callback should be called when adding a new snapshot,
11409  *      Driver should use the same id for multiple snapshots taken
11410  *      on multiple regions at the same time/by the same trigger.
11411  *
11412  *      The caller of this function must use devlink_region_snapshot_id_put
11413  *      when finished creating regions using this id.
11414  *
11415  *      Returns zero on success, or a negative error code on failure.
11416  *
11417  *      @devlink: devlink
11418  *      @id: storage to return id
11419  */
11420 int devlink_region_snapshot_id_get(struct devlink *devlink, u32 *id)
11421 {
11422         return __devlink_region_snapshot_id_get(devlink, id);
11423 }
11424 EXPORT_SYMBOL_GPL(devlink_region_snapshot_id_get);
11425
11426 /**
11427  *      devlink_region_snapshot_id_put - put snapshot ID reference
11428  *
11429  *      This should be called by a driver after finishing creating snapshots
11430  *      with an id. Doing so ensures that the ID can later be released in the
11431  *      event that all snapshots using it have been destroyed.
11432  *
11433  *      @devlink: devlink
11434  *      @id: id to release reference on
11435  */
11436 void devlink_region_snapshot_id_put(struct devlink *devlink, u32 id)
11437 {
11438         __devlink_snapshot_id_decrement(devlink, id);
11439 }
11440 EXPORT_SYMBOL_GPL(devlink_region_snapshot_id_put);
11441
11442 /**
11443  *      devlink_region_snapshot_create - create a new snapshot
11444  *      This will add a new snapshot of a region. The snapshot
11445  *      will be stored on the region struct and can be accessed
11446  *      from devlink. This is useful for future analyses of snapshots.
11447  *      Multiple snapshots can be created on a region.
11448  *      The @snapshot_id should be obtained using the getter function.
11449  *
11450  *      @region: devlink region of the snapshot
11451  *      @data: snapshot data
11452  *      @snapshot_id: snapshot id to be created
11453  */
11454 int devlink_region_snapshot_create(struct devlink_region *region,
11455                                    u8 *data, u32 snapshot_id)
11456 {
11457         int err;
11458
11459         mutex_lock(&region->snapshot_lock);
11460         err = __devlink_region_snapshot_create(region, data, snapshot_id);
11461         mutex_unlock(&region->snapshot_lock);
11462         return err;
11463 }
11464 EXPORT_SYMBOL_GPL(devlink_region_snapshot_create);
11465
11466 #define DEVLINK_TRAP(_id, _type)                                              \
11467         {                                                                     \
11468                 .type = DEVLINK_TRAP_TYPE_##_type,                            \
11469                 .id = DEVLINK_TRAP_GENERIC_ID_##_id,                          \
11470                 .name = DEVLINK_TRAP_GENERIC_NAME_##_id,                      \
11471         }
11472
11473 static const struct devlink_trap devlink_trap_generic[] = {
11474         DEVLINK_TRAP(SMAC_MC, DROP),
11475         DEVLINK_TRAP(VLAN_TAG_MISMATCH, DROP),
11476         DEVLINK_TRAP(INGRESS_VLAN_FILTER, DROP),
11477         DEVLINK_TRAP(INGRESS_STP_FILTER, DROP),
11478         DEVLINK_TRAP(EMPTY_TX_LIST, DROP),
11479         DEVLINK_TRAP(PORT_LOOPBACK_FILTER, DROP),
11480         DEVLINK_TRAP(BLACKHOLE_ROUTE, DROP),
11481         DEVLINK_TRAP(TTL_ERROR, EXCEPTION),
11482         DEVLINK_TRAP(TAIL_DROP, DROP),
11483         DEVLINK_TRAP(NON_IP_PACKET, DROP),
11484         DEVLINK_TRAP(UC_DIP_MC_DMAC, DROP),
11485         DEVLINK_TRAP(DIP_LB, DROP),
11486         DEVLINK_TRAP(SIP_MC, DROP),
11487         DEVLINK_TRAP(SIP_LB, DROP),
11488         DEVLINK_TRAP(CORRUPTED_IP_HDR, DROP),
11489         DEVLINK_TRAP(IPV4_SIP_BC, DROP),
11490         DEVLINK_TRAP(IPV6_MC_DIP_RESERVED_SCOPE, DROP),
11491         DEVLINK_TRAP(IPV6_MC_DIP_INTERFACE_LOCAL_SCOPE, DROP),
11492         DEVLINK_TRAP(MTU_ERROR, EXCEPTION),
11493         DEVLINK_TRAP(UNRESOLVED_NEIGH, EXCEPTION),
11494         DEVLINK_TRAP(RPF, EXCEPTION),
11495         DEVLINK_TRAP(REJECT_ROUTE, EXCEPTION),
11496         DEVLINK_TRAP(IPV4_LPM_UNICAST_MISS, EXCEPTION),
11497         DEVLINK_TRAP(IPV6_LPM_UNICAST_MISS, EXCEPTION),
11498         DEVLINK_TRAP(NON_ROUTABLE, DROP),
11499         DEVLINK_TRAP(DECAP_ERROR, EXCEPTION),
11500         DEVLINK_TRAP(OVERLAY_SMAC_MC, DROP),
11501         DEVLINK_TRAP(INGRESS_FLOW_ACTION_DROP, DROP),
11502         DEVLINK_TRAP(EGRESS_FLOW_ACTION_DROP, DROP),
11503         DEVLINK_TRAP(STP, CONTROL),
11504         DEVLINK_TRAP(LACP, CONTROL),
11505         DEVLINK_TRAP(LLDP, CONTROL),
11506         DEVLINK_TRAP(IGMP_QUERY, CONTROL),
11507         DEVLINK_TRAP(IGMP_V1_REPORT, CONTROL),
11508         DEVLINK_TRAP(IGMP_V2_REPORT, CONTROL),
11509         DEVLINK_TRAP(IGMP_V3_REPORT, CONTROL),
11510         DEVLINK_TRAP(IGMP_V2_LEAVE, CONTROL),
11511         DEVLINK_TRAP(MLD_QUERY, CONTROL),
11512         DEVLINK_TRAP(MLD_V1_REPORT, CONTROL),
11513         DEVLINK_TRAP(MLD_V2_REPORT, CONTROL),
11514         DEVLINK_TRAP(MLD_V1_DONE, CONTROL),
11515         DEVLINK_TRAP(IPV4_DHCP, CONTROL),
11516         DEVLINK_TRAP(IPV6_DHCP, CONTROL),
11517         DEVLINK_TRAP(ARP_REQUEST, CONTROL),
11518         DEVLINK_TRAP(ARP_RESPONSE, CONTROL),
11519         DEVLINK_TRAP(ARP_OVERLAY, CONTROL),
11520         DEVLINK_TRAP(IPV6_NEIGH_SOLICIT, CONTROL),
11521         DEVLINK_TRAP(IPV6_NEIGH_ADVERT, CONTROL),
11522         DEVLINK_TRAP(IPV4_BFD, CONTROL),
11523         DEVLINK_TRAP(IPV6_BFD, CONTROL),
11524         DEVLINK_TRAP(IPV4_OSPF, CONTROL),
11525         DEVLINK_TRAP(IPV6_OSPF, CONTROL),
11526         DEVLINK_TRAP(IPV4_BGP, CONTROL),
11527         DEVLINK_TRAP(IPV6_BGP, CONTROL),
11528         DEVLINK_TRAP(IPV4_VRRP, CONTROL),
11529         DEVLINK_TRAP(IPV6_VRRP, CONTROL),
11530         DEVLINK_TRAP(IPV4_PIM, CONTROL),
11531         DEVLINK_TRAP(IPV6_PIM, CONTROL),
11532         DEVLINK_TRAP(UC_LB, CONTROL),
11533         DEVLINK_TRAP(LOCAL_ROUTE, CONTROL),
11534         DEVLINK_TRAP(EXTERNAL_ROUTE, CONTROL),
11535         DEVLINK_TRAP(IPV6_UC_DIP_LINK_LOCAL_SCOPE, CONTROL),
11536         DEVLINK_TRAP(IPV6_DIP_ALL_NODES, CONTROL),
11537         DEVLINK_TRAP(IPV6_DIP_ALL_ROUTERS, CONTROL),
11538         DEVLINK_TRAP(IPV6_ROUTER_SOLICIT, CONTROL),
11539         DEVLINK_TRAP(IPV6_ROUTER_ADVERT, CONTROL),
11540         DEVLINK_TRAP(IPV6_REDIRECT, CONTROL),
11541         DEVLINK_TRAP(IPV4_ROUTER_ALERT, CONTROL),
11542         DEVLINK_TRAP(IPV6_ROUTER_ALERT, CONTROL),
11543         DEVLINK_TRAP(PTP_EVENT, CONTROL),
11544         DEVLINK_TRAP(PTP_GENERAL, CONTROL),
11545         DEVLINK_TRAP(FLOW_ACTION_SAMPLE, CONTROL),
11546         DEVLINK_TRAP(FLOW_ACTION_TRAP, CONTROL),
11547         DEVLINK_TRAP(EARLY_DROP, DROP),
11548         DEVLINK_TRAP(VXLAN_PARSING, DROP),
11549         DEVLINK_TRAP(LLC_SNAP_PARSING, DROP),
11550         DEVLINK_TRAP(VLAN_PARSING, DROP),
11551         DEVLINK_TRAP(PPPOE_PPP_PARSING, DROP),
11552         DEVLINK_TRAP(MPLS_PARSING, DROP),
11553         DEVLINK_TRAP(ARP_PARSING, DROP),
11554         DEVLINK_TRAP(IP_1_PARSING, DROP),
11555         DEVLINK_TRAP(IP_N_PARSING, DROP),
11556         DEVLINK_TRAP(GRE_PARSING, DROP),
11557         DEVLINK_TRAP(UDP_PARSING, DROP),
11558         DEVLINK_TRAP(TCP_PARSING, DROP),
11559         DEVLINK_TRAP(IPSEC_PARSING, DROP),
11560         DEVLINK_TRAP(SCTP_PARSING, DROP),
11561         DEVLINK_TRAP(DCCP_PARSING, DROP),
11562         DEVLINK_TRAP(GTP_PARSING, DROP),
11563         DEVLINK_TRAP(ESP_PARSING, DROP),
11564         DEVLINK_TRAP(BLACKHOLE_NEXTHOP, DROP),
11565         DEVLINK_TRAP(DMAC_FILTER, DROP),
11566         DEVLINK_TRAP(EAPOL, CONTROL),
11567         DEVLINK_TRAP(LOCKED_PORT, DROP),
11568 };
11569
11570 #define DEVLINK_TRAP_GROUP(_id)                                               \
11571         {                                                                     \
11572                 .id = DEVLINK_TRAP_GROUP_GENERIC_ID_##_id,                    \
11573                 .name = DEVLINK_TRAP_GROUP_GENERIC_NAME_##_id,                \
11574         }
11575
11576 static const struct devlink_trap_group devlink_trap_group_generic[] = {
11577         DEVLINK_TRAP_GROUP(L2_DROPS),
11578         DEVLINK_TRAP_GROUP(L3_DROPS),
11579         DEVLINK_TRAP_GROUP(L3_EXCEPTIONS),
11580         DEVLINK_TRAP_GROUP(BUFFER_DROPS),
11581         DEVLINK_TRAP_GROUP(TUNNEL_DROPS),
11582         DEVLINK_TRAP_GROUP(ACL_DROPS),
11583         DEVLINK_TRAP_GROUP(STP),
11584         DEVLINK_TRAP_GROUP(LACP),
11585         DEVLINK_TRAP_GROUP(LLDP),
11586         DEVLINK_TRAP_GROUP(MC_SNOOPING),
11587         DEVLINK_TRAP_GROUP(DHCP),
11588         DEVLINK_TRAP_GROUP(NEIGH_DISCOVERY),
11589         DEVLINK_TRAP_GROUP(BFD),
11590         DEVLINK_TRAP_GROUP(OSPF),
11591         DEVLINK_TRAP_GROUP(BGP),
11592         DEVLINK_TRAP_GROUP(VRRP),
11593         DEVLINK_TRAP_GROUP(PIM),
11594         DEVLINK_TRAP_GROUP(UC_LB),
11595         DEVLINK_TRAP_GROUP(LOCAL_DELIVERY),
11596         DEVLINK_TRAP_GROUP(EXTERNAL_DELIVERY),
11597         DEVLINK_TRAP_GROUP(IPV6),
11598         DEVLINK_TRAP_GROUP(PTP_EVENT),
11599         DEVLINK_TRAP_GROUP(PTP_GENERAL),
11600         DEVLINK_TRAP_GROUP(ACL_SAMPLE),
11601         DEVLINK_TRAP_GROUP(ACL_TRAP),
11602         DEVLINK_TRAP_GROUP(PARSER_ERROR_DROPS),
11603         DEVLINK_TRAP_GROUP(EAPOL),
11604 };
11605
11606 static int devlink_trap_generic_verify(const struct devlink_trap *trap)
11607 {
11608         if (trap->id > DEVLINK_TRAP_GENERIC_ID_MAX)
11609                 return -EINVAL;
11610
11611         if (strcmp(trap->name, devlink_trap_generic[trap->id].name))
11612                 return -EINVAL;
11613
11614         if (trap->type != devlink_trap_generic[trap->id].type)
11615                 return -EINVAL;
11616
11617         return 0;
11618 }
11619
11620 static int devlink_trap_driver_verify(const struct devlink_trap *trap)
11621 {
11622         int i;
11623
11624         if (trap->id <= DEVLINK_TRAP_GENERIC_ID_MAX)
11625                 return -EINVAL;
11626
11627         for (i = 0; i < ARRAY_SIZE(devlink_trap_generic); i++) {
11628                 if (!strcmp(trap->name, devlink_trap_generic[i].name))
11629                         return -EEXIST;
11630         }
11631
11632         return 0;
11633 }
11634
11635 static int devlink_trap_verify(const struct devlink_trap *trap)
11636 {
11637         if (!trap || !trap->name)
11638                 return -EINVAL;
11639
11640         if (trap->generic)
11641                 return devlink_trap_generic_verify(trap);
11642         else
11643                 return devlink_trap_driver_verify(trap);
11644 }
11645
11646 static int
11647 devlink_trap_group_generic_verify(const struct devlink_trap_group *group)
11648 {
11649         if (group->id > DEVLINK_TRAP_GROUP_GENERIC_ID_MAX)
11650                 return -EINVAL;
11651
11652         if (strcmp(group->name, devlink_trap_group_generic[group->id].name))
11653                 return -EINVAL;
11654
11655         return 0;
11656 }
11657
11658 static int
11659 devlink_trap_group_driver_verify(const struct devlink_trap_group *group)
11660 {
11661         int i;
11662
11663         if (group->id <= DEVLINK_TRAP_GROUP_GENERIC_ID_MAX)
11664                 return -EINVAL;
11665
11666         for (i = 0; i < ARRAY_SIZE(devlink_trap_group_generic); i++) {
11667                 if (!strcmp(group->name, devlink_trap_group_generic[i].name))
11668                         return -EEXIST;
11669         }
11670
11671         return 0;
11672 }
11673
11674 static int devlink_trap_group_verify(const struct devlink_trap_group *group)
11675 {
11676         if (group->generic)
11677                 return devlink_trap_group_generic_verify(group);
11678         else
11679                 return devlink_trap_group_driver_verify(group);
11680 }
11681
11682 static void
11683 devlink_trap_group_notify(struct devlink *devlink,
11684                           const struct devlink_trap_group_item *group_item,
11685                           enum devlink_command cmd)
11686 {
11687         struct sk_buff *msg;
11688         int err;
11689
11690         WARN_ON_ONCE(cmd != DEVLINK_CMD_TRAP_GROUP_NEW &&
11691                      cmd != DEVLINK_CMD_TRAP_GROUP_DEL);
11692         if (!xa_get_mark(&devlinks, devlink->index, DEVLINK_REGISTERED))
11693                 return;
11694
11695         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
11696         if (!msg)
11697                 return;
11698
11699         err = devlink_nl_trap_group_fill(msg, devlink, group_item, cmd, 0, 0,
11700                                          0);
11701         if (err) {
11702                 nlmsg_free(msg);
11703                 return;
11704         }
11705
11706         genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
11707                                 msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
11708 }
11709
11710 static int
11711 devlink_trap_item_group_link(struct devlink *devlink,
11712                              struct devlink_trap_item *trap_item)
11713 {
11714         u16 group_id = trap_item->trap->init_group_id;
11715         struct devlink_trap_group_item *group_item;
11716
11717         group_item = devlink_trap_group_item_lookup_by_id(devlink, group_id);
11718         if (WARN_ON_ONCE(!group_item))
11719                 return -EINVAL;
11720
11721         trap_item->group_item = group_item;
11722
11723         return 0;
11724 }
11725
11726 static void devlink_trap_notify(struct devlink *devlink,
11727                                 const struct devlink_trap_item *trap_item,
11728                                 enum devlink_command cmd)
11729 {
11730         struct sk_buff *msg;
11731         int err;
11732
11733         WARN_ON_ONCE(cmd != DEVLINK_CMD_TRAP_NEW &&
11734                      cmd != DEVLINK_CMD_TRAP_DEL);
11735         if (!xa_get_mark(&devlinks, devlink->index, DEVLINK_REGISTERED))
11736                 return;
11737
11738         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
11739         if (!msg)
11740                 return;
11741
11742         err = devlink_nl_trap_fill(msg, devlink, trap_item, cmd, 0, 0, 0);
11743         if (err) {
11744                 nlmsg_free(msg);
11745                 return;
11746         }
11747
11748         genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
11749                                 msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
11750 }
11751
11752 static int
11753 devlink_trap_register(struct devlink *devlink,
11754                       const struct devlink_trap *trap, void *priv)
11755 {
11756         struct devlink_trap_item *trap_item;
11757         int err;
11758
11759         if (devlink_trap_item_lookup(devlink, trap->name))
11760                 return -EEXIST;
11761
11762         trap_item = kzalloc(sizeof(*trap_item), GFP_KERNEL);
11763         if (!trap_item)
11764                 return -ENOMEM;
11765
11766         trap_item->stats = netdev_alloc_pcpu_stats(struct devlink_stats);
11767         if (!trap_item->stats) {
11768                 err = -ENOMEM;
11769                 goto err_stats_alloc;
11770         }
11771
11772         trap_item->trap = trap;
11773         trap_item->action = trap->init_action;
11774         trap_item->priv = priv;
11775
11776         err = devlink_trap_item_group_link(devlink, trap_item);
11777         if (err)
11778                 goto err_group_link;
11779
11780         err = devlink->ops->trap_init(devlink, trap, trap_item);
11781         if (err)
11782                 goto err_trap_init;
11783
11784         list_add_tail(&trap_item->list, &devlink->trap_list);
11785         devlink_trap_notify(devlink, trap_item, DEVLINK_CMD_TRAP_NEW);
11786
11787         return 0;
11788
11789 err_trap_init:
11790 err_group_link:
11791         free_percpu(trap_item->stats);
11792 err_stats_alloc:
11793         kfree(trap_item);
11794         return err;
11795 }
11796
11797 static void devlink_trap_unregister(struct devlink *devlink,
11798                                     const struct devlink_trap *trap)
11799 {
11800         struct devlink_trap_item *trap_item;
11801
11802         trap_item = devlink_trap_item_lookup(devlink, trap->name);
11803         if (WARN_ON_ONCE(!trap_item))
11804                 return;
11805
11806         devlink_trap_notify(devlink, trap_item, DEVLINK_CMD_TRAP_DEL);
11807         list_del(&trap_item->list);
11808         if (devlink->ops->trap_fini)
11809                 devlink->ops->trap_fini(devlink, trap, trap_item);
11810         free_percpu(trap_item->stats);
11811         kfree(trap_item);
11812 }
11813
11814 static void devlink_trap_disable(struct devlink *devlink,
11815                                  const struct devlink_trap *trap)
11816 {
11817         struct devlink_trap_item *trap_item;
11818
11819         trap_item = devlink_trap_item_lookup(devlink, trap->name);
11820         if (WARN_ON_ONCE(!trap_item))
11821                 return;
11822
11823         devlink->ops->trap_action_set(devlink, trap, DEVLINK_TRAP_ACTION_DROP,
11824                                       NULL);
11825         trap_item->action = DEVLINK_TRAP_ACTION_DROP;
11826 }
11827
11828 /**
11829  * devl_traps_register - Register packet traps with devlink.
11830  * @devlink: devlink.
11831  * @traps: Packet traps.
11832  * @traps_count: Count of provided packet traps.
11833  * @priv: Driver private information.
11834  *
11835  * Return: Non-zero value on failure.
11836  */
11837 int devl_traps_register(struct devlink *devlink,
11838                         const struct devlink_trap *traps,
11839                         size_t traps_count, void *priv)
11840 {
11841         int i, err;
11842
11843         if (!devlink->ops->trap_init || !devlink->ops->trap_action_set)
11844                 return -EINVAL;
11845
11846         devl_assert_locked(devlink);
11847         for (i = 0; i < traps_count; i++) {
11848                 const struct devlink_trap *trap = &traps[i];
11849
11850                 err = devlink_trap_verify(trap);
11851                 if (err)
11852                         goto err_trap_verify;
11853
11854                 err = devlink_trap_register(devlink, trap, priv);
11855                 if (err)
11856                         goto err_trap_register;
11857         }
11858
11859         return 0;
11860
11861 err_trap_register:
11862 err_trap_verify:
11863         for (i--; i >= 0; i--)
11864                 devlink_trap_unregister(devlink, &traps[i]);
11865         return err;
11866 }
11867 EXPORT_SYMBOL_GPL(devl_traps_register);
11868
11869 /**
11870  * devlink_traps_register - Register packet traps with devlink.
11871  * @devlink: devlink.
11872  * @traps: Packet traps.
11873  * @traps_count: Count of provided packet traps.
11874  * @priv: Driver private information.
11875  *
11876  * Context: Takes and release devlink->lock <mutex>.
11877  *
11878  * Return: Non-zero value on failure.
11879  */
11880 int devlink_traps_register(struct devlink *devlink,
11881                            const struct devlink_trap *traps,
11882                            size_t traps_count, void *priv)
11883 {
11884         int err;
11885
11886         devl_lock(devlink);
11887         err = devl_traps_register(devlink, traps, traps_count, priv);
11888         devl_unlock(devlink);
11889         return err;
11890 }
11891 EXPORT_SYMBOL_GPL(devlink_traps_register);
11892
11893 /**
11894  * devl_traps_unregister - Unregister packet traps from devlink.
11895  * @devlink: devlink.
11896  * @traps: Packet traps.
11897  * @traps_count: Count of provided packet traps.
11898  */
11899 void devl_traps_unregister(struct devlink *devlink,
11900                            const struct devlink_trap *traps,
11901                            size_t traps_count)
11902 {
11903         int i;
11904
11905         devl_assert_locked(devlink);
11906         /* Make sure we do not have any packets in-flight while unregistering
11907          * traps by disabling all of them and waiting for a grace period.
11908          */
11909         for (i = traps_count - 1; i >= 0; i--)
11910                 devlink_trap_disable(devlink, &traps[i]);
11911         synchronize_rcu();
11912         for (i = traps_count - 1; i >= 0; i--)
11913                 devlink_trap_unregister(devlink, &traps[i]);
11914 }
11915 EXPORT_SYMBOL_GPL(devl_traps_unregister);
11916
11917 /**
11918  * devlink_traps_unregister - Unregister packet traps from devlink.
11919  * @devlink: devlink.
11920  * @traps: Packet traps.
11921  * @traps_count: Count of provided packet traps.
11922  *
11923  * Context: Takes and release devlink->lock <mutex>.
11924  */
11925 void devlink_traps_unregister(struct devlink *devlink,
11926                               const struct devlink_trap *traps,
11927                               size_t traps_count)
11928 {
11929         devl_lock(devlink);
11930         devl_traps_unregister(devlink, traps, traps_count);
11931         devl_unlock(devlink);
11932 }
11933 EXPORT_SYMBOL_GPL(devlink_traps_unregister);
11934
11935 static void
11936 devlink_trap_stats_update(struct devlink_stats __percpu *trap_stats,
11937                           size_t skb_len)
11938 {
11939         struct devlink_stats *stats;
11940
11941         stats = this_cpu_ptr(trap_stats);
11942         u64_stats_update_begin(&stats->syncp);
11943         u64_stats_add(&stats->rx_bytes, skb_len);
11944         u64_stats_inc(&stats->rx_packets);
11945         u64_stats_update_end(&stats->syncp);
11946 }
11947
11948 static void
11949 devlink_trap_report_metadata_set(struct devlink_trap_metadata *metadata,
11950                                  const struct devlink_trap_item *trap_item,
11951                                  struct devlink_port *in_devlink_port,
11952                                  const struct flow_action_cookie *fa_cookie)
11953 {
11954         metadata->trap_name = trap_item->trap->name;
11955         metadata->trap_group_name = trap_item->group_item->group->name;
11956         metadata->fa_cookie = fa_cookie;
11957         metadata->trap_type = trap_item->trap->type;
11958
11959         spin_lock(&in_devlink_port->type_lock);
11960         if (in_devlink_port->type == DEVLINK_PORT_TYPE_ETH)
11961                 metadata->input_dev = in_devlink_port->type_eth.netdev;
11962         spin_unlock(&in_devlink_port->type_lock);
11963 }
11964
11965 /**
11966  * devlink_trap_report - Report trapped packet to drop monitor.
11967  * @devlink: devlink.
11968  * @skb: Trapped packet.
11969  * @trap_ctx: Trap context.
11970  * @in_devlink_port: Input devlink port.
11971  * @fa_cookie: Flow action cookie. Could be NULL.
11972  */
11973 void devlink_trap_report(struct devlink *devlink, struct sk_buff *skb,
11974                          void *trap_ctx, struct devlink_port *in_devlink_port,
11975                          const struct flow_action_cookie *fa_cookie)
11976
11977 {
11978         struct devlink_trap_item *trap_item = trap_ctx;
11979
11980         devlink_trap_stats_update(trap_item->stats, skb->len);
11981         devlink_trap_stats_update(trap_item->group_item->stats, skb->len);
11982
11983         if (trace_devlink_trap_report_enabled()) {
11984                 struct devlink_trap_metadata metadata = {};
11985
11986                 devlink_trap_report_metadata_set(&metadata, trap_item,
11987                                                  in_devlink_port, fa_cookie);
11988                 trace_devlink_trap_report(devlink, skb, &metadata);
11989         }
11990 }
11991 EXPORT_SYMBOL_GPL(devlink_trap_report);
11992
11993 /**
11994  * devlink_trap_ctx_priv - Trap context to driver private information.
11995  * @trap_ctx: Trap context.
11996  *
11997  * Return: Driver private information passed during registration.
11998  */
11999 void *devlink_trap_ctx_priv(void *trap_ctx)
12000 {
12001         struct devlink_trap_item *trap_item = trap_ctx;
12002
12003         return trap_item->priv;
12004 }
12005 EXPORT_SYMBOL_GPL(devlink_trap_ctx_priv);
12006
12007 static int
12008 devlink_trap_group_item_policer_link(struct devlink *devlink,
12009                                      struct devlink_trap_group_item *group_item)
12010 {
12011         u32 policer_id = group_item->group->init_policer_id;
12012         struct devlink_trap_policer_item *policer_item;
12013
12014         if (policer_id == 0)
12015                 return 0;
12016
12017         policer_item = devlink_trap_policer_item_lookup(devlink, policer_id);
12018         if (WARN_ON_ONCE(!policer_item))
12019                 return -EINVAL;
12020
12021         group_item->policer_item = policer_item;
12022
12023         return 0;
12024 }
12025
12026 static int
12027 devlink_trap_group_register(struct devlink *devlink,
12028                             const struct devlink_trap_group *group)
12029 {
12030         struct devlink_trap_group_item *group_item;
12031         int err;
12032
12033         if (devlink_trap_group_item_lookup(devlink, group->name))
12034                 return -EEXIST;
12035
12036         group_item = kzalloc(sizeof(*group_item), GFP_KERNEL);
12037         if (!group_item)
12038                 return -ENOMEM;
12039
12040         group_item->stats = netdev_alloc_pcpu_stats(struct devlink_stats);
12041         if (!group_item->stats) {
12042                 err = -ENOMEM;
12043                 goto err_stats_alloc;
12044         }
12045
12046         group_item->group = group;
12047
12048         err = devlink_trap_group_item_policer_link(devlink, group_item);
12049         if (err)
12050                 goto err_policer_link;
12051
12052         if (devlink->ops->trap_group_init) {
12053                 err = devlink->ops->trap_group_init(devlink, group);
12054                 if (err)
12055                         goto err_group_init;
12056         }
12057
12058         list_add_tail(&group_item->list, &devlink->trap_group_list);
12059         devlink_trap_group_notify(devlink, group_item,
12060                                   DEVLINK_CMD_TRAP_GROUP_NEW);
12061
12062         return 0;
12063
12064 err_group_init:
12065 err_policer_link:
12066         free_percpu(group_item->stats);
12067 err_stats_alloc:
12068         kfree(group_item);
12069         return err;
12070 }
12071
12072 static void
12073 devlink_trap_group_unregister(struct devlink *devlink,
12074                               const struct devlink_trap_group *group)
12075 {
12076         struct devlink_trap_group_item *group_item;
12077
12078         group_item = devlink_trap_group_item_lookup(devlink, group->name);
12079         if (WARN_ON_ONCE(!group_item))
12080                 return;
12081
12082         devlink_trap_group_notify(devlink, group_item,
12083                                   DEVLINK_CMD_TRAP_GROUP_DEL);
12084         list_del(&group_item->list);
12085         free_percpu(group_item->stats);
12086         kfree(group_item);
12087 }
12088
12089 /**
12090  * devl_trap_groups_register - Register packet trap groups with devlink.
12091  * @devlink: devlink.
12092  * @groups: Packet trap groups.
12093  * @groups_count: Count of provided packet trap groups.
12094  *
12095  * Return: Non-zero value on failure.
12096  */
12097 int devl_trap_groups_register(struct devlink *devlink,
12098                               const struct devlink_trap_group *groups,
12099                               size_t groups_count)
12100 {
12101         int i, err;
12102
12103         devl_assert_locked(devlink);
12104         for (i = 0; i < groups_count; i++) {
12105                 const struct devlink_trap_group *group = &groups[i];
12106
12107                 err = devlink_trap_group_verify(group);
12108                 if (err)
12109                         goto err_trap_group_verify;
12110
12111                 err = devlink_trap_group_register(devlink, group);
12112                 if (err)
12113                         goto err_trap_group_register;
12114         }
12115
12116         return 0;
12117
12118 err_trap_group_register:
12119 err_trap_group_verify:
12120         for (i--; i >= 0; i--)
12121                 devlink_trap_group_unregister(devlink, &groups[i]);
12122         return err;
12123 }
12124 EXPORT_SYMBOL_GPL(devl_trap_groups_register);
12125
12126 /**
12127  * devlink_trap_groups_register - Register packet trap groups with devlink.
12128  * @devlink: devlink.
12129  * @groups: Packet trap groups.
12130  * @groups_count: Count of provided packet trap groups.
12131  *
12132  * Context: Takes and release devlink->lock <mutex>.
12133  *
12134  * Return: Non-zero value on failure.
12135  */
12136 int devlink_trap_groups_register(struct devlink *devlink,
12137                                  const struct devlink_trap_group *groups,
12138                                  size_t groups_count)
12139 {
12140         int err;
12141
12142         devl_lock(devlink);
12143         err = devl_trap_groups_register(devlink, groups, groups_count);
12144         devl_unlock(devlink);
12145         return err;
12146 }
12147 EXPORT_SYMBOL_GPL(devlink_trap_groups_register);
12148
12149 /**
12150  * devl_trap_groups_unregister - Unregister packet trap groups from devlink.
12151  * @devlink: devlink.
12152  * @groups: Packet trap groups.
12153  * @groups_count: Count of provided packet trap groups.
12154  */
12155 void devl_trap_groups_unregister(struct devlink *devlink,
12156                                  const struct devlink_trap_group *groups,
12157                                  size_t groups_count)
12158 {
12159         int i;
12160
12161         devl_assert_locked(devlink);
12162         for (i = groups_count - 1; i >= 0; i--)
12163                 devlink_trap_group_unregister(devlink, &groups[i]);
12164 }
12165 EXPORT_SYMBOL_GPL(devl_trap_groups_unregister);
12166
12167 /**
12168  * devlink_trap_groups_unregister - Unregister packet trap groups from devlink.
12169  * @devlink: devlink.
12170  * @groups: Packet trap groups.
12171  * @groups_count: Count of provided packet trap groups.
12172  *
12173  * Context: Takes and release devlink->lock <mutex>.
12174  */
12175 void devlink_trap_groups_unregister(struct devlink *devlink,
12176                                     const struct devlink_trap_group *groups,
12177                                     size_t groups_count)
12178 {
12179         devl_lock(devlink);
12180         devl_trap_groups_unregister(devlink, groups, groups_count);
12181         devl_unlock(devlink);
12182 }
12183 EXPORT_SYMBOL_GPL(devlink_trap_groups_unregister);
12184
12185 static void
12186 devlink_trap_policer_notify(struct devlink *devlink,
12187                             const struct devlink_trap_policer_item *policer_item,
12188                             enum devlink_command cmd)
12189 {
12190         struct sk_buff *msg;
12191         int err;
12192
12193         WARN_ON_ONCE(cmd != DEVLINK_CMD_TRAP_POLICER_NEW &&
12194                      cmd != DEVLINK_CMD_TRAP_POLICER_DEL);
12195         if (!xa_get_mark(&devlinks, devlink->index, DEVLINK_REGISTERED))
12196                 return;
12197
12198         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
12199         if (!msg)
12200                 return;
12201
12202         err = devlink_nl_trap_policer_fill(msg, devlink, policer_item, cmd, 0,
12203                                            0, 0);
12204         if (err) {
12205                 nlmsg_free(msg);
12206                 return;
12207         }
12208
12209         genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
12210                                 msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
12211 }
12212
12213 static int
12214 devlink_trap_policer_register(struct devlink *devlink,
12215                               const struct devlink_trap_policer *policer)
12216 {
12217         struct devlink_trap_policer_item *policer_item;
12218         int err;
12219
12220         if (devlink_trap_policer_item_lookup(devlink, policer->id))
12221                 return -EEXIST;
12222
12223         policer_item = kzalloc(sizeof(*policer_item), GFP_KERNEL);
12224         if (!policer_item)
12225                 return -ENOMEM;
12226
12227         policer_item->policer = policer;
12228         policer_item->rate = policer->init_rate;
12229         policer_item->burst = policer->init_burst;
12230
12231         if (devlink->ops->trap_policer_init) {
12232                 err = devlink->ops->trap_policer_init(devlink, policer);
12233                 if (err)
12234                         goto err_policer_init;
12235         }
12236
12237         list_add_tail(&policer_item->list, &devlink->trap_policer_list);
12238         devlink_trap_policer_notify(devlink, policer_item,
12239                                     DEVLINK_CMD_TRAP_POLICER_NEW);
12240
12241         return 0;
12242
12243 err_policer_init:
12244         kfree(policer_item);
12245         return err;
12246 }
12247
12248 static void
12249 devlink_trap_policer_unregister(struct devlink *devlink,
12250                                 const struct devlink_trap_policer *policer)
12251 {
12252         struct devlink_trap_policer_item *policer_item;
12253
12254         policer_item = devlink_trap_policer_item_lookup(devlink, policer->id);
12255         if (WARN_ON_ONCE(!policer_item))
12256                 return;
12257
12258         devlink_trap_policer_notify(devlink, policer_item,
12259                                     DEVLINK_CMD_TRAP_POLICER_DEL);
12260         list_del(&policer_item->list);
12261         if (devlink->ops->trap_policer_fini)
12262                 devlink->ops->trap_policer_fini(devlink, policer);
12263         kfree(policer_item);
12264 }
12265
12266 /**
12267  * devl_trap_policers_register - Register packet trap policers with devlink.
12268  * @devlink: devlink.
12269  * @policers: Packet trap policers.
12270  * @policers_count: Count of provided packet trap policers.
12271  *
12272  * Return: Non-zero value on failure.
12273  */
12274 int
12275 devl_trap_policers_register(struct devlink *devlink,
12276                             const struct devlink_trap_policer *policers,
12277                             size_t policers_count)
12278 {
12279         int i, err;
12280
12281         devl_assert_locked(devlink);
12282         for (i = 0; i < policers_count; i++) {
12283                 const struct devlink_trap_policer *policer = &policers[i];
12284
12285                 if (WARN_ON(policer->id == 0 ||
12286                             policer->max_rate < policer->min_rate ||
12287                             policer->max_burst < policer->min_burst)) {
12288                         err = -EINVAL;
12289                         goto err_trap_policer_verify;
12290                 }
12291
12292                 err = devlink_trap_policer_register(devlink, policer);
12293                 if (err)
12294                         goto err_trap_policer_register;
12295         }
12296         return 0;
12297
12298 err_trap_policer_register:
12299 err_trap_policer_verify:
12300         for (i--; i >= 0; i--)
12301                 devlink_trap_policer_unregister(devlink, &policers[i]);
12302         return err;
12303 }
12304 EXPORT_SYMBOL_GPL(devl_trap_policers_register);
12305
12306 /**
12307  * devl_trap_policers_unregister - Unregister packet trap policers from devlink.
12308  * @devlink: devlink.
12309  * @policers: Packet trap policers.
12310  * @policers_count: Count of provided packet trap policers.
12311  */
12312 void
12313 devl_trap_policers_unregister(struct devlink *devlink,
12314                               const struct devlink_trap_policer *policers,
12315                               size_t policers_count)
12316 {
12317         int i;
12318
12319         devl_assert_locked(devlink);
12320         for (i = policers_count - 1; i >= 0; i--)
12321                 devlink_trap_policer_unregister(devlink, &policers[i]);
12322 }
12323 EXPORT_SYMBOL_GPL(devl_trap_policers_unregister);
12324
12325 static void __devlink_compat_running_version(struct devlink *devlink,
12326                                              char *buf, size_t len)
12327 {
12328         struct devlink_info_req req = {};
12329         const struct nlattr *nlattr;
12330         struct sk_buff *msg;
12331         int rem, err;
12332
12333         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
12334         if (!msg)
12335                 return;
12336
12337         req.msg = msg;
12338         err = devlink->ops->info_get(devlink, &req, NULL);
12339         if (err)
12340                 goto free_msg;
12341
12342         nla_for_each_attr(nlattr, (void *)msg->data, msg->len, rem) {
12343                 const struct nlattr *kv;
12344                 int rem_kv;
12345
12346                 if (nla_type(nlattr) != DEVLINK_ATTR_INFO_VERSION_RUNNING)
12347                         continue;
12348
12349                 nla_for_each_nested(kv, nlattr, rem_kv) {
12350                         if (nla_type(kv) != DEVLINK_ATTR_INFO_VERSION_VALUE)
12351                                 continue;
12352
12353                         strlcat(buf, nla_data(kv), len);
12354                         strlcat(buf, " ", len);
12355                 }
12356         }
12357 free_msg:
12358         nlmsg_free(msg);
12359 }
12360
12361 void devlink_compat_running_version(struct devlink *devlink,
12362                                     char *buf, size_t len)
12363 {
12364         if (!devlink->ops->info_get)
12365                 return;
12366
12367         devl_lock(devlink);
12368         __devlink_compat_running_version(devlink, buf, len);
12369         devl_unlock(devlink);
12370 }
12371
12372 int devlink_compat_flash_update(struct devlink *devlink, const char *file_name)
12373 {
12374         struct devlink_flash_update_params params = {};
12375         int ret;
12376
12377         if (!devlink->ops->flash_update)
12378                 return -EOPNOTSUPP;
12379
12380         ret = request_firmware(&params.fw, file_name, devlink->dev);
12381         if (ret)
12382                 return ret;
12383
12384         devl_lock(devlink);
12385         devlink_flash_update_begin_notify(devlink);
12386         ret = devlink->ops->flash_update(devlink, &params, NULL);
12387         devlink_flash_update_end_notify(devlink);
12388         devl_unlock(devlink);
12389
12390         release_firmware(params.fw);
12391
12392         return ret;
12393 }
12394
12395 int devlink_compat_phys_port_name_get(struct net_device *dev,
12396                                       char *name, size_t len)
12397 {
12398         struct devlink_port *devlink_port;
12399
12400         /* RTNL mutex is held here which ensures that devlink_port
12401          * instance cannot disappear in the middle. No need to take
12402          * any devlink lock as only permanent values are accessed.
12403          */
12404         ASSERT_RTNL();
12405
12406         devlink_port = dev->devlink_port;
12407         if (!devlink_port)
12408                 return -EOPNOTSUPP;
12409
12410         return __devlink_port_phys_port_name_get(devlink_port, name, len);
12411 }
12412
12413 int devlink_compat_switch_id_get(struct net_device *dev,
12414                                  struct netdev_phys_item_id *ppid)
12415 {
12416         struct devlink_port *devlink_port;
12417
12418         /* Caller must hold RTNL mutex or reference to dev, which ensures that
12419          * devlink_port instance cannot disappear in the middle. No need to take
12420          * any devlink lock as only permanent values are accessed.
12421          */
12422         devlink_port = dev->devlink_port;
12423         if (!devlink_port || !devlink_port->switch_port)
12424                 return -EOPNOTSUPP;
12425
12426         memcpy(ppid, &devlink_port->attrs.switch_id, sizeof(*ppid));
12427
12428         return 0;
12429 }