]> Git Repo - linux.git/blob - net/core/devlink.c
libbpf: fix GCC8 warning for strncpy
[linux.git] / net / core / devlink.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 <[email protected]>
8  */
9
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/types.h>
13 #include <linux/slab.h>
14 #include <linux/gfp.h>
15 #include <linux/device.h>
16 #include <linux/list.h>
17 #include <linux/netdevice.h>
18 #include <linux/spinlock.h>
19 #include <linux/refcount.h>
20 #include <linux/workqueue.h>
21 #include <rdma/ib_verbs.h>
22 #include <net/netlink.h>
23 #include <net/genetlink.h>
24 #include <net/rtnetlink.h>
25 #include <net/net_namespace.h>
26 #include <net/sock.h>
27 #include <net/devlink.h>
28 #define CREATE_TRACE_POINTS
29 #include <trace/events/devlink.h>
30
31 static struct devlink_dpipe_field devlink_dpipe_fields_ethernet[] = {
32         {
33                 .name = "destination mac",
34                 .id = DEVLINK_DPIPE_FIELD_ETHERNET_DST_MAC,
35                 .bitwidth = 48,
36         },
37 };
38
39 struct devlink_dpipe_header devlink_dpipe_header_ethernet = {
40         .name = "ethernet",
41         .id = DEVLINK_DPIPE_HEADER_ETHERNET,
42         .fields = devlink_dpipe_fields_ethernet,
43         .fields_count = ARRAY_SIZE(devlink_dpipe_fields_ethernet),
44         .global = true,
45 };
46 EXPORT_SYMBOL(devlink_dpipe_header_ethernet);
47
48 static struct devlink_dpipe_field devlink_dpipe_fields_ipv4[] = {
49         {
50                 .name = "destination ip",
51                 .id = DEVLINK_DPIPE_FIELD_IPV4_DST_IP,
52                 .bitwidth = 32,
53         },
54 };
55
56 struct devlink_dpipe_header devlink_dpipe_header_ipv4 = {
57         .name = "ipv4",
58         .id = DEVLINK_DPIPE_HEADER_IPV4,
59         .fields = devlink_dpipe_fields_ipv4,
60         .fields_count = ARRAY_SIZE(devlink_dpipe_fields_ipv4),
61         .global = true,
62 };
63 EXPORT_SYMBOL(devlink_dpipe_header_ipv4);
64
65 static struct devlink_dpipe_field devlink_dpipe_fields_ipv6[] = {
66         {
67                 .name = "destination ip",
68                 .id = DEVLINK_DPIPE_FIELD_IPV6_DST_IP,
69                 .bitwidth = 128,
70         },
71 };
72
73 struct devlink_dpipe_header devlink_dpipe_header_ipv6 = {
74         .name = "ipv6",
75         .id = DEVLINK_DPIPE_HEADER_IPV6,
76         .fields = devlink_dpipe_fields_ipv6,
77         .fields_count = ARRAY_SIZE(devlink_dpipe_fields_ipv6),
78         .global = true,
79 };
80 EXPORT_SYMBOL(devlink_dpipe_header_ipv6);
81
82 EXPORT_TRACEPOINT_SYMBOL_GPL(devlink_hwmsg);
83 EXPORT_TRACEPOINT_SYMBOL_GPL(devlink_hwerr);
84
85 static LIST_HEAD(devlink_list);
86
87 /* devlink_mutex
88  *
89  * An overall lock guarding every operation coming from userspace.
90  * It also guards devlink devices list and it is taken when
91  * driver registers/unregisters it.
92  */
93 static DEFINE_MUTEX(devlink_mutex);
94
95 static struct net *devlink_net(const struct devlink *devlink)
96 {
97         return read_pnet(&devlink->_net);
98 }
99
100 static void devlink_net_set(struct devlink *devlink, struct net *net)
101 {
102         write_pnet(&devlink->_net, net);
103 }
104
105 static struct devlink *devlink_get_from_attrs(struct net *net,
106                                               struct nlattr **attrs)
107 {
108         struct devlink *devlink;
109         char *busname;
110         char *devname;
111
112         if (!attrs[DEVLINK_ATTR_BUS_NAME] || !attrs[DEVLINK_ATTR_DEV_NAME])
113                 return ERR_PTR(-EINVAL);
114
115         busname = nla_data(attrs[DEVLINK_ATTR_BUS_NAME]);
116         devname = nla_data(attrs[DEVLINK_ATTR_DEV_NAME]);
117
118         lockdep_assert_held(&devlink_mutex);
119
120         list_for_each_entry(devlink, &devlink_list, list) {
121                 if (strcmp(devlink->dev->bus->name, busname) == 0 &&
122                     strcmp(dev_name(devlink->dev), devname) == 0 &&
123                     net_eq(devlink_net(devlink), net))
124                         return devlink;
125         }
126
127         return ERR_PTR(-ENODEV);
128 }
129
130 static struct devlink *devlink_get_from_info(struct genl_info *info)
131 {
132         return devlink_get_from_attrs(genl_info_net(info), info->attrs);
133 }
134
135 static struct devlink_port *devlink_port_get_by_index(struct devlink *devlink,
136                                                       int port_index)
137 {
138         struct devlink_port *devlink_port;
139
140         list_for_each_entry(devlink_port, &devlink->port_list, list) {
141                 if (devlink_port->index == port_index)
142                         return devlink_port;
143         }
144         return NULL;
145 }
146
147 static bool devlink_port_index_exists(struct devlink *devlink, int port_index)
148 {
149         return devlink_port_get_by_index(devlink, port_index);
150 }
151
152 static struct devlink_port *devlink_port_get_from_attrs(struct devlink *devlink,
153                                                         struct nlattr **attrs)
154 {
155         if (attrs[DEVLINK_ATTR_PORT_INDEX]) {
156                 u32 port_index = nla_get_u32(attrs[DEVLINK_ATTR_PORT_INDEX]);
157                 struct devlink_port *devlink_port;
158
159                 devlink_port = devlink_port_get_by_index(devlink, port_index);
160                 if (!devlink_port)
161                         return ERR_PTR(-ENODEV);
162                 return devlink_port;
163         }
164         return ERR_PTR(-EINVAL);
165 }
166
167 static struct devlink_port *devlink_port_get_from_info(struct devlink *devlink,
168                                                        struct genl_info *info)
169 {
170         return devlink_port_get_from_attrs(devlink, info->attrs);
171 }
172
173 struct devlink_sb {
174         struct list_head list;
175         unsigned int index;
176         u32 size;
177         u16 ingress_pools_count;
178         u16 egress_pools_count;
179         u16 ingress_tc_count;
180         u16 egress_tc_count;
181 };
182
183 static u16 devlink_sb_pool_count(struct devlink_sb *devlink_sb)
184 {
185         return devlink_sb->ingress_pools_count + devlink_sb->egress_pools_count;
186 }
187
188 static struct devlink_sb *devlink_sb_get_by_index(struct devlink *devlink,
189                                                   unsigned int sb_index)
190 {
191         struct devlink_sb *devlink_sb;
192
193         list_for_each_entry(devlink_sb, &devlink->sb_list, list) {
194                 if (devlink_sb->index == sb_index)
195                         return devlink_sb;
196         }
197         return NULL;
198 }
199
200 static bool devlink_sb_index_exists(struct devlink *devlink,
201                                     unsigned int sb_index)
202 {
203         return devlink_sb_get_by_index(devlink, sb_index);
204 }
205
206 static struct devlink_sb *devlink_sb_get_from_attrs(struct devlink *devlink,
207                                                     struct nlattr **attrs)
208 {
209         if (attrs[DEVLINK_ATTR_SB_INDEX]) {
210                 u32 sb_index = nla_get_u32(attrs[DEVLINK_ATTR_SB_INDEX]);
211                 struct devlink_sb *devlink_sb;
212
213                 devlink_sb = devlink_sb_get_by_index(devlink, sb_index);
214                 if (!devlink_sb)
215                         return ERR_PTR(-ENODEV);
216                 return devlink_sb;
217         }
218         return ERR_PTR(-EINVAL);
219 }
220
221 static struct devlink_sb *devlink_sb_get_from_info(struct devlink *devlink,
222                                                    struct genl_info *info)
223 {
224         return devlink_sb_get_from_attrs(devlink, info->attrs);
225 }
226
227 static int devlink_sb_pool_index_get_from_attrs(struct devlink_sb *devlink_sb,
228                                                 struct nlattr **attrs,
229                                                 u16 *p_pool_index)
230 {
231         u16 val;
232
233         if (!attrs[DEVLINK_ATTR_SB_POOL_INDEX])
234                 return -EINVAL;
235
236         val = nla_get_u16(attrs[DEVLINK_ATTR_SB_POOL_INDEX]);
237         if (val >= devlink_sb_pool_count(devlink_sb))
238                 return -EINVAL;
239         *p_pool_index = val;
240         return 0;
241 }
242
243 static int devlink_sb_pool_index_get_from_info(struct devlink_sb *devlink_sb,
244                                                struct genl_info *info,
245                                                u16 *p_pool_index)
246 {
247         return devlink_sb_pool_index_get_from_attrs(devlink_sb, info->attrs,
248                                                     p_pool_index);
249 }
250
251 static int
252 devlink_sb_pool_type_get_from_attrs(struct nlattr **attrs,
253                                     enum devlink_sb_pool_type *p_pool_type)
254 {
255         u8 val;
256
257         if (!attrs[DEVLINK_ATTR_SB_POOL_TYPE])
258                 return -EINVAL;
259
260         val = nla_get_u8(attrs[DEVLINK_ATTR_SB_POOL_TYPE]);
261         if (val != DEVLINK_SB_POOL_TYPE_INGRESS &&
262             val != DEVLINK_SB_POOL_TYPE_EGRESS)
263                 return -EINVAL;
264         *p_pool_type = val;
265         return 0;
266 }
267
268 static int
269 devlink_sb_pool_type_get_from_info(struct genl_info *info,
270                                    enum devlink_sb_pool_type *p_pool_type)
271 {
272         return devlink_sb_pool_type_get_from_attrs(info->attrs, p_pool_type);
273 }
274
275 static int
276 devlink_sb_th_type_get_from_attrs(struct nlattr **attrs,
277                                   enum devlink_sb_threshold_type *p_th_type)
278 {
279         u8 val;
280
281         if (!attrs[DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE])
282                 return -EINVAL;
283
284         val = nla_get_u8(attrs[DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE]);
285         if (val != DEVLINK_SB_THRESHOLD_TYPE_STATIC &&
286             val != DEVLINK_SB_THRESHOLD_TYPE_DYNAMIC)
287                 return -EINVAL;
288         *p_th_type = val;
289         return 0;
290 }
291
292 static int
293 devlink_sb_th_type_get_from_info(struct genl_info *info,
294                                  enum devlink_sb_threshold_type *p_th_type)
295 {
296         return devlink_sb_th_type_get_from_attrs(info->attrs, p_th_type);
297 }
298
299 static int
300 devlink_sb_tc_index_get_from_attrs(struct devlink_sb *devlink_sb,
301                                    struct nlattr **attrs,
302                                    enum devlink_sb_pool_type pool_type,
303                                    u16 *p_tc_index)
304 {
305         u16 val;
306
307         if (!attrs[DEVLINK_ATTR_SB_TC_INDEX])
308                 return -EINVAL;
309
310         val = nla_get_u16(attrs[DEVLINK_ATTR_SB_TC_INDEX]);
311         if (pool_type == DEVLINK_SB_POOL_TYPE_INGRESS &&
312             val >= devlink_sb->ingress_tc_count)
313                 return -EINVAL;
314         if (pool_type == DEVLINK_SB_POOL_TYPE_EGRESS &&
315             val >= devlink_sb->egress_tc_count)
316                 return -EINVAL;
317         *p_tc_index = val;
318         return 0;
319 }
320
321 static int
322 devlink_sb_tc_index_get_from_info(struct devlink_sb *devlink_sb,
323                                   struct genl_info *info,
324                                   enum devlink_sb_pool_type pool_type,
325                                   u16 *p_tc_index)
326 {
327         return devlink_sb_tc_index_get_from_attrs(devlink_sb, info->attrs,
328                                                   pool_type, p_tc_index);
329 }
330
331 struct devlink_region {
332         struct devlink *devlink;
333         struct list_head list;
334         const char *name;
335         struct list_head snapshot_list;
336         u32 max_snapshots;
337         u32 cur_snapshots;
338         u64 size;
339 };
340
341 struct devlink_snapshot {
342         struct list_head list;
343         struct devlink_region *region;
344         devlink_snapshot_data_dest_t *data_destructor;
345         u64 data_len;
346         u8 *data;
347         u32 id;
348 };
349
350 static struct devlink_region *
351 devlink_region_get_by_name(struct devlink *devlink, const char *region_name)
352 {
353         struct devlink_region *region;
354
355         list_for_each_entry(region, &devlink->region_list, list)
356                 if (!strcmp(region->name, region_name))
357                         return region;
358
359         return NULL;
360 }
361
362 static struct devlink_snapshot *
363 devlink_region_snapshot_get_by_id(struct devlink_region *region, u32 id)
364 {
365         struct devlink_snapshot *snapshot;
366
367         list_for_each_entry(snapshot, &region->snapshot_list, list)
368                 if (snapshot->id == id)
369                         return snapshot;
370
371         return NULL;
372 }
373
374 static void devlink_region_snapshot_del(struct devlink_snapshot *snapshot)
375 {
376         snapshot->region->cur_snapshots--;
377         list_del(&snapshot->list);
378         (*snapshot->data_destructor)(snapshot->data);
379         kfree(snapshot);
380 }
381
382 #define DEVLINK_NL_FLAG_NEED_DEVLINK    BIT(0)
383 #define DEVLINK_NL_FLAG_NEED_PORT       BIT(1)
384 #define DEVLINK_NL_FLAG_NEED_SB         BIT(2)
385
386 /* The per devlink instance lock is taken by default in the pre-doit
387  * operation, yet several commands do not require this. The global
388  * devlink lock is taken and protects from disruption by user-calls.
389  */
390 #define DEVLINK_NL_FLAG_NO_LOCK         BIT(3)
391
392 static int devlink_nl_pre_doit(const struct genl_ops *ops,
393                                struct sk_buff *skb, struct genl_info *info)
394 {
395         struct devlink *devlink;
396         int err;
397
398         mutex_lock(&devlink_mutex);
399         devlink = devlink_get_from_info(info);
400         if (IS_ERR(devlink)) {
401                 mutex_unlock(&devlink_mutex);
402                 return PTR_ERR(devlink);
403         }
404         if (~ops->internal_flags & DEVLINK_NL_FLAG_NO_LOCK)
405                 mutex_lock(&devlink->lock);
406         if (ops->internal_flags & DEVLINK_NL_FLAG_NEED_DEVLINK) {
407                 info->user_ptr[0] = devlink;
408         } else if (ops->internal_flags & DEVLINK_NL_FLAG_NEED_PORT) {
409                 struct devlink_port *devlink_port;
410
411                 devlink_port = devlink_port_get_from_info(devlink, info);
412                 if (IS_ERR(devlink_port)) {
413                         err = PTR_ERR(devlink_port);
414                         goto unlock;
415                 }
416                 info->user_ptr[0] = devlink_port;
417         }
418         if (ops->internal_flags & DEVLINK_NL_FLAG_NEED_SB) {
419                 struct devlink_sb *devlink_sb;
420
421                 devlink_sb = devlink_sb_get_from_info(devlink, info);
422                 if (IS_ERR(devlink_sb)) {
423                         err = PTR_ERR(devlink_sb);
424                         goto unlock;
425                 }
426                 info->user_ptr[1] = devlink_sb;
427         }
428         return 0;
429
430 unlock:
431         if (~ops->internal_flags & DEVLINK_NL_FLAG_NO_LOCK)
432                 mutex_unlock(&devlink->lock);
433         mutex_unlock(&devlink_mutex);
434         return err;
435 }
436
437 static void devlink_nl_post_doit(const struct genl_ops *ops,
438                                  struct sk_buff *skb, struct genl_info *info)
439 {
440         struct devlink *devlink;
441
442         devlink = devlink_get_from_info(info);
443         if (~ops->internal_flags & DEVLINK_NL_FLAG_NO_LOCK)
444                 mutex_unlock(&devlink->lock);
445         mutex_unlock(&devlink_mutex);
446 }
447
448 static struct genl_family devlink_nl_family;
449
450 enum devlink_multicast_groups {
451         DEVLINK_MCGRP_CONFIG,
452 };
453
454 static const struct genl_multicast_group devlink_nl_mcgrps[] = {
455         [DEVLINK_MCGRP_CONFIG] = { .name = DEVLINK_GENL_MCGRP_CONFIG_NAME },
456 };
457
458 static int devlink_nl_put_handle(struct sk_buff *msg, struct devlink *devlink)
459 {
460         if (nla_put_string(msg, DEVLINK_ATTR_BUS_NAME, devlink->dev->bus->name))
461                 return -EMSGSIZE;
462         if (nla_put_string(msg, DEVLINK_ATTR_DEV_NAME, dev_name(devlink->dev)))
463                 return -EMSGSIZE;
464         return 0;
465 }
466
467 static int devlink_nl_fill(struct sk_buff *msg, struct devlink *devlink,
468                            enum devlink_command cmd, u32 portid,
469                            u32 seq, int flags)
470 {
471         void *hdr;
472
473         hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
474         if (!hdr)
475                 return -EMSGSIZE;
476
477         if (devlink_nl_put_handle(msg, devlink))
478                 goto nla_put_failure;
479
480         genlmsg_end(msg, hdr);
481         return 0;
482
483 nla_put_failure:
484         genlmsg_cancel(msg, hdr);
485         return -EMSGSIZE;
486 }
487
488 static void devlink_notify(struct devlink *devlink, enum devlink_command cmd)
489 {
490         struct sk_buff *msg;
491         int err;
492
493         WARN_ON(cmd != DEVLINK_CMD_NEW && cmd != DEVLINK_CMD_DEL);
494
495         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
496         if (!msg)
497                 return;
498
499         err = devlink_nl_fill(msg, devlink, cmd, 0, 0, 0);
500         if (err) {
501                 nlmsg_free(msg);
502                 return;
503         }
504
505         genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
506                                 msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
507 }
508
509 static int devlink_nl_port_attrs_put(struct sk_buff *msg,
510                                      struct devlink_port *devlink_port)
511 {
512         struct devlink_port_attrs *attrs = &devlink_port->attrs;
513
514         if (!attrs->set)
515                 return 0;
516         if (nla_put_u16(msg, DEVLINK_ATTR_PORT_FLAVOUR, attrs->flavour))
517                 return -EMSGSIZE;
518         if (nla_put_u32(msg, DEVLINK_ATTR_PORT_NUMBER, attrs->port_number))
519                 return -EMSGSIZE;
520         if (!attrs->split)
521                 return 0;
522         if (nla_put_u32(msg, DEVLINK_ATTR_PORT_SPLIT_GROUP, attrs->port_number))
523                 return -EMSGSIZE;
524         if (nla_put_u32(msg, DEVLINK_ATTR_PORT_SPLIT_SUBPORT_NUMBER,
525                         attrs->split_subport_number))
526                 return -EMSGSIZE;
527         return 0;
528 }
529
530 static int devlink_nl_port_fill(struct sk_buff *msg, struct devlink *devlink,
531                                 struct devlink_port *devlink_port,
532                                 enum devlink_command cmd, u32 portid,
533                                 u32 seq, int flags)
534 {
535         void *hdr;
536
537         hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
538         if (!hdr)
539                 return -EMSGSIZE;
540
541         if (devlink_nl_put_handle(msg, devlink))
542                 goto nla_put_failure;
543         if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, devlink_port->index))
544                 goto nla_put_failure;
545
546         spin_lock(&devlink_port->type_lock);
547         if (nla_put_u16(msg, DEVLINK_ATTR_PORT_TYPE, devlink_port->type))
548                 goto nla_put_failure_type_locked;
549         if (devlink_port->desired_type != DEVLINK_PORT_TYPE_NOTSET &&
550             nla_put_u16(msg, DEVLINK_ATTR_PORT_DESIRED_TYPE,
551                         devlink_port->desired_type))
552                 goto nla_put_failure_type_locked;
553         if (devlink_port->type == DEVLINK_PORT_TYPE_ETH) {
554                 struct net_device *netdev = devlink_port->type_dev;
555
556                 if (netdev &&
557                     (nla_put_u32(msg, DEVLINK_ATTR_PORT_NETDEV_IFINDEX,
558                                  netdev->ifindex) ||
559                      nla_put_string(msg, DEVLINK_ATTR_PORT_NETDEV_NAME,
560                                     netdev->name)))
561                         goto nla_put_failure_type_locked;
562         }
563         if (devlink_port->type == DEVLINK_PORT_TYPE_IB) {
564                 struct ib_device *ibdev = devlink_port->type_dev;
565
566                 if (ibdev &&
567                     nla_put_string(msg, DEVLINK_ATTR_PORT_IBDEV_NAME,
568                                    ibdev->name))
569                         goto nla_put_failure_type_locked;
570         }
571         spin_unlock(&devlink_port->type_lock);
572         if (devlink_nl_port_attrs_put(msg, devlink_port))
573                 goto nla_put_failure;
574
575         genlmsg_end(msg, hdr);
576         return 0;
577
578 nla_put_failure_type_locked:
579         spin_unlock(&devlink_port->type_lock);
580 nla_put_failure:
581         genlmsg_cancel(msg, hdr);
582         return -EMSGSIZE;
583 }
584
585 static void devlink_port_notify(struct devlink_port *devlink_port,
586                                 enum devlink_command cmd)
587 {
588         struct devlink *devlink = devlink_port->devlink;
589         struct sk_buff *msg;
590         int err;
591
592         if (!devlink_port->registered)
593                 return;
594
595         WARN_ON(cmd != DEVLINK_CMD_PORT_NEW && cmd != DEVLINK_CMD_PORT_DEL);
596
597         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
598         if (!msg)
599                 return;
600
601         err = devlink_nl_port_fill(msg, devlink, devlink_port, cmd, 0, 0, 0);
602         if (err) {
603                 nlmsg_free(msg);
604                 return;
605         }
606
607         genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
608                                 msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
609 }
610
611 static int devlink_nl_cmd_get_doit(struct sk_buff *skb, struct genl_info *info)
612 {
613         struct devlink *devlink = info->user_ptr[0];
614         struct sk_buff *msg;
615         int err;
616
617         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
618         if (!msg)
619                 return -ENOMEM;
620
621         err = devlink_nl_fill(msg, devlink, DEVLINK_CMD_NEW,
622                               info->snd_portid, info->snd_seq, 0);
623         if (err) {
624                 nlmsg_free(msg);
625                 return err;
626         }
627
628         return genlmsg_reply(msg, info);
629 }
630
631 static int devlink_nl_cmd_get_dumpit(struct sk_buff *msg,
632                                      struct netlink_callback *cb)
633 {
634         struct devlink *devlink;
635         int start = cb->args[0];
636         int idx = 0;
637         int err;
638
639         mutex_lock(&devlink_mutex);
640         list_for_each_entry(devlink, &devlink_list, list) {
641                 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
642                         continue;
643                 if (idx < start) {
644                         idx++;
645                         continue;
646                 }
647                 err = devlink_nl_fill(msg, devlink, DEVLINK_CMD_NEW,
648                                       NETLINK_CB(cb->skb).portid,
649                                       cb->nlh->nlmsg_seq, NLM_F_MULTI);
650                 if (err)
651                         goto out;
652                 idx++;
653         }
654 out:
655         mutex_unlock(&devlink_mutex);
656
657         cb->args[0] = idx;
658         return msg->len;
659 }
660
661 static int devlink_nl_cmd_port_get_doit(struct sk_buff *skb,
662                                         struct genl_info *info)
663 {
664         struct devlink_port *devlink_port = info->user_ptr[0];
665         struct devlink *devlink = devlink_port->devlink;
666         struct sk_buff *msg;
667         int err;
668
669         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
670         if (!msg)
671                 return -ENOMEM;
672
673         err = devlink_nl_port_fill(msg, devlink, devlink_port,
674                                    DEVLINK_CMD_PORT_NEW,
675                                    info->snd_portid, info->snd_seq, 0);
676         if (err) {
677                 nlmsg_free(msg);
678                 return err;
679         }
680
681         return genlmsg_reply(msg, info);
682 }
683
684 static int devlink_nl_cmd_port_get_dumpit(struct sk_buff *msg,
685                                           struct netlink_callback *cb)
686 {
687         struct devlink *devlink;
688         struct devlink_port *devlink_port;
689         int start = cb->args[0];
690         int idx = 0;
691         int err;
692
693         mutex_lock(&devlink_mutex);
694         list_for_each_entry(devlink, &devlink_list, list) {
695                 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
696                         continue;
697                 mutex_lock(&devlink->lock);
698                 list_for_each_entry(devlink_port, &devlink->port_list, list) {
699                         if (idx < start) {
700                                 idx++;
701                                 continue;
702                         }
703                         err = devlink_nl_port_fill(msg, devlink, devlink_port,
704                                                    DEVLINK_CMD_NEW,
705                                                    NETLINK_CB(cb->skb).portid,
706                                                    cb->nlh->nlmsg_seq,
707                                                    NLM_F_MULTI);
708                         if (err) {
709                                 mutex_unlock(&devlink->lock);
710                                 goto out;
711                         }
712                         idx++;
713                 }
714                 mutex_unlock(&devlink->lock);
715         }
716 out:
717         mutex_unlock(&devlink_mutex);
718
719         cb->args[0] = idx;
720         return msg->len;
721 }
722
723 static int devlink_port_type_set(struct devlink *devlink,
724                                  struct devlink_port *devlink_port,
725                                  enum devlink_port_type port_type)
726
727 {
728         int err;
729
730         if (devlink->ops->port_type_set) {
731                 if (port_type == DEVLINK_PORT_TYPE_NOTSET)
732                         return -EINVAL;
733                 if (port_type == devlink_port->type)
734                         return 0;
735                 err = devlink->ops->port_type_set(devlink_port, port_type);
736                 if (err)
737                         return err;
738                 devlink_port->desired_type = port_type;
739                 devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW);
740                 return 0;
741         }
742         return -EOPNOTSUPP;
743 }
744
745 static int devlink_nl_cmd_port_set_doit(struct sk_buff *skb,
746                                         struct genl_info *info)
747 {
748         struct devlink_port *devlink_port = info->user_ptr[0];
749         struct devlink *devlink = devlink_port->devlink;
750         int err;
751
752         if (info->attrs[DEVLINK_ATTR_PORT_TYPE]) {
753                 enum devlink_port_type port_type;
754
755                 port_type = nla_get_u16(info->attrs[DEVLINK_ATTR_PORT_TYPE]);
756                 err = devlink_port_type_set(devlink, devlink_port, port_type);
757                 if (err)
758                         return err;
759         }
760         return 0;
761 }
762
763 static int devlink_port_split(struct devlink *devlink, u32 port_index,
764                               u32 count, struct netlink_ext_ack *extack)
765
766 {
767         if (devlink->ops->port_split)
768                 return devlink->ops->port_split(devlink, port_index, count,
769                                                 extack);
770         return -EOPNOTSUPP;
771 }
772
773 static int devlink_nl_cmd_port_split_doit(struct sk_buff *skb,
774                                           struct genl_info *info)
775 {
776         struct devlink *devlink = info->user_ptr[0];
777         u32 port_index;
778         u32 count;
779
780         if (!info->attrs[DEVLINK_ATTR_PORT_INDEX] ||
781             !info->attrs[DEVLINK_ATTR_PORT_SPLIT_COUNT])
782                 return -EINVAL;
783
784         port_index = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_INDEX]);
785         count = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_SPLIT_COUNT]);
786         return devlink_port_split(devlink, port_index, count, info->extack);
787 }
788
789 static int devlink_port_unsplit(struct devlink *devlink, u32 port_index,
790                                 struct netlink_ext_ack *extack)
791
792 {
793         if (devlink->ops->port_unsplit)
794                 return devlink->ops->port_unsplit(devlink, port_index, extack);
795         return -EOPNOTSUPP;
796 }
797
798 static int devlink_nl_cmd_port_unsplit_doit(struct sk_buff *skb,
799                                             struct genl_info *info)
800 {
801         struct devlink *devlink = info->user_ptr[0];
802         u32 port_index;
803
804         if (!info->attrs[DEVLINK_ATTR_PORT_INDEX])
805                 return -EINVAL;
806
807         port_index = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_INDEX]);
808         return devlink_port_unsplit(devlink, port_index, info->extack);
809 }
810
811 static int devlink_nl_sb_fill(struct sk_buff *msg, struct devlink *devlink,
812                               struct devlink_sb *devlink_sb,
813                               enum devlink_command cmd, u32 portid,
814                               u32 seq, int flags)
815 {
816         void *hdr;
817
818         hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
819         if (!hdr)
820                 return -EMSGSIZE;
821
822         if (devlink_nl_put_handle(msg, devlink))
823                 goto nla_put_failure;
824         if (nla_put_u32(msg, DEVLINK_ATTR_SB_INDEX, devlink_sb->index))
825                 goto nla_put_failure;
826         if (nla_put_u32(msg, DEVLINK_ATTR_SB_SIZE, devlink_sb->size))
827                 goto nla_put_failure;
828         if (nla_put_u16(msg, DEVLINK_ATTR_SB_INGRESS_POOL_COUNT,
829                         devlink_sb->ingress_pools_count))
830                 goto nla_put_failure;
831         if (nla_put_u16(msg, DEVLINK_ATTR_SB_EGRESS_POOL_COUNT,
832                         devlink_sb->egress_pools_count))
833                 goto nla_put_failure;
834         if (nla_put_u16(msg, DEVLINK_ATTR_SB_INGRESS_TC_COUNT,
835                         devlink_sb->ingress_tc_count))
836                 goto nla_put_failure;
837         if (nla_put_u16(msg, DEVLINK_ATTR_SB_EGRESS_TC_COUNT,
838                         devlink_sb->egress_tc_count))
839                 goto nla_put_failure;
840
841         genlmsg_end(msg, hdr);
842         return 0;
843
844 nla_put_failure:
845         genlmsg_cancel(msg, hdr);
846         return -EMSGSIZE;
847 }
848
849 static int devlink_nl_cmd_sb_get_doit(struct sk_buff *skb,
850                                       struct genl_info *info)
851 {
852         struct devlink *devlink = info->user_ptr[0];
853         struct devlink_sb *devlink_sb = info->user_ptr[1];
854         struct sk_buff *msg;
855         int err;
856
857         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
858         if (!msg)
859                 return -ENOMEM;
860
861         err = devlink_nl_sb_fill(msg, devlink, devlink_sb,
862                                  DEVLINK_CMD_SB_NEW,
863                                  info->snd_portid, info->snd_seq, 0);
864         if (err) {
865                 nlmsg_free(msg);
866                 return err;
867         }
868
869         return genlmsg_reply(msg, info);
870 }
871
872 static int devlink_nl_cmd_sb_get_dumpit(struct sk_buff *msg,
873                                         struct netlink_callback *cb)
874 {
875         struct devlink *devlink;
876         struct devlink_sb *devlink_sb;
877         int start = cb->args[0];
878         int idx = 0;
879         int err;
880
881         mutex_lock(&devlink_mutex);
882         list_for_each_entry(devlink, &devlink_list, list) {
883                 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
884                         continue;
885                 mutex_lock(&devlink->lock);
886                 list_for_each_entry(devlink_sb, &devlink->sb_list, list) {
887                         if (idx < start) {
888                                 idx++;
889                                 continue;
890                         }
891                         err = devlink_nl_sb_fill(msg, devlink, devlink_sb,
892                                                  DEVLINK_CMD_SB_NEW,
893                                                  NETLINK_CB(cb->skb).portid,
894                                                  cb->nlh->nlmsg_seq,
895                                                  NLM_F_MULTI);
896                         if (err) {
897                                 mutex_unlock(&devlink->lock);
898                                 goto out;
899                         }
900                         idx++;
901                 }
902                 mutex_unlock(&devlink->lock);
903         }
904 out:
905         mutex_unlock(&devlink_mutex);
906
907         cb->args[0] = idx;
908         return msg->len;
909 }
910
911 static int devlink_nl_sb_pool_fill(struct sk_buff *msg, struct devlink *devlink,
912                                    struct devlink_sb *devlink_sb,
913                                    u16 pool_index, enum devlink_command cmd,
914                                    u32 portid, u32 seq, int flags)
915 {
916         struct devlink_sb_pool_info pool_info;
917         void *hdr;
918         int err;
919
920         err = devlink->ops->sb_pool_get(devlink, devlink_sb->index,
921                                         pool_index, &pool_info);
922         if (err)
923                 return err;
924
925         hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
926         if (!hdr)
927                 return -EMSGSIZE;
928
929         if (devlink_nl_put_handle(msg, devlink))
930                 goto nla_put_failure;
931         if (nla_put_u32(msg, DEVLINK_ATTR_SB_INDEX, devlink_sb->index))
932                 goto nla_put_failure;
933         if (nla_put_u16(msg, DEVLINK_ATTR_SB_POOL_INDEX, pool_index))
934                 goto nla_put_failure;
935         if (nla_put_u8(msg, DEVLINK_ATTR_SB_POOL_TYPE, pool_info.pool_type))
936                 goto nla_put_failure;
937         if (nla_put_u32(msg, DEVLINK_ATTR_SB_POOL_SIZE, pool_info.size))
938                 goto nla_put_failure;
939         if (nla_put_u8(msg, DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE,
940                        pool_info.threshold_type))
941                 goto nla_put_failure;
942         if (nla_put_u32(msg, DEVLINK_ATTR_SB_POOL_CELL_SIZE,
943                         pool_info.cell_size))
944                 goto nla_put_failure;
945
946         genlmsg_end(msg, hdr);
947         return 0;
948
949 nla_put_failure:
950         genlmsg_cancel(msg, hdr);
951         return -EMSGSIZE;
952 }
953
954 static int devlink_nl_cmd_sb_pool_get_doit(struct sk_buff *skb,
955                                            struct genl_info *info)
956 {
957         struct devlink *devlink = info->user_ptr[0];
958         struct devlink_sb *devlink_sb = info->user_ptr[1];
959         struct sk_buff *msg;
960         u16 pool_index;
961         int err;
962
963         err = devlink_sb_pool_index_get_from_info(devlink_sb, info,
964                                                   &pool_index);
965         if (err)
966                 return err;
967
968         if (!devlink->ops->sb_pool_get)
969                 return -EOPNOTSUPP;
970
971         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
972         if (!msg)
973                 return -ENOMEM;
974
975         err = devlink_nl_sb_pool_fill(msg, devlink, devlink_sb, pool_index,
976                                       DEVLINK_CMD_SB_POOL_NEW,
977                                       info->snd_portid, info->snd_seq, 0);
978         if (err) {
979                 nlmsg_free(msg);
980                 return err;
981         }
982
983         return genlmsg_reply(msg, info);
984 }
985
986 static int __sb_pool_get_dumpit(struct sk_buff *msg, int start, int *p_idx,
987                                 struct devlink *devlink,
988                                 struct devlink_sb *devlink_sb,
989                                 u32 portid, u32 seq)
990 {
991         u16 pool_count = devlink_sb_pool_count(devlink_sb);
992         u16 pool_index;
993         int err;
994
995         for (pool_index = 0; pool_index < pool_count; pool_index++) {
996                 if (*p_idx < start) {
997                         (*p_idx)++;
998                         continue;
999                 }
1000                 err = devlink_nl_sb_pool_fill(msg, devlink,
1001                                               devlink_sb,
1002                                               pool_index,
1003                                               DEVLINK_CMD_SB_POOL_NEW,
1004                                               portid, seq, NLM_F_MULTI);
1005                 if (err)
1006                         return err;
1007                 (*p_idx)++;
1008         }
1009         return 0;
1010 }
1011
1012 static int devlink_nl_cmd_sb_pool_get_dumpit(struct sk_buff *msg,
1013                                              struct netlink_callback *cb)
1014 {
1015         struct devlink *devlink;
1016         struct devlink_sb *devlink_sb;
1017         int start = cb->args[0];
1018         int idx = 0;
1019         int err;
1020
1021         mutex_lock(&devlink_mutex);
1022         list_for_each_entry(devlink, &devlink_list, list) {
1023                 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)) ||
1024                     !devlink->ops->sb_pool_get)
1025                         continue;
1026                 mutex_lock(&devlink->lock);
1027                 list_for_each_entry(devlink_sb, &devlink->sb_list, list) {
1028                         err = __sb_pool_get_dumpit(msg, start, &idx, devlink,
1029                                                    devlink_sb,
1030                                                    NETLINK_CB(cb->skb).portid,
1031                                                    cb->nlh->nlmsg_seq);
1032                         if (err && err != -EOPNOTSUPP) {
1033                                 mutex_unlock(&devlink->lock);
1034                                 goto out;
1035                         }
1036                 }
1037                 mutex_unlock(&devlink->lock);
1038         }
1039 out:
1040         mutex_unlock(&devlink_mutex);
1041
1042         cb->args[0] = idx;
1043         return msg->len;
1044 }
1045
1046 static int devlink_sb_pool_set(struct devlink *devlink, unsigned int sb_index,
1047                                u16 pool_index, u32 size,
1048                                enum devlink_sb_threshold_type threshold_type,
1049                                struct netlink_ext_ack *extack)
1050
1051 {
1052         const struct devlink_ops *ops = devlink->ops;
1053
1054         if (ops->sb_pool_set)
1055                 return ops->sb_pool_set(devlink, sb_index, pool_index,
1056                                         size, threshold_type, extack);
1057         return -EOPNOTSUPP;
1058 }
1059
1060 static int devlink_nl_cmd_sb_pool_set_doit(struct sk_buff *skb,
1061                                            struct genl_info *info)
1062 {
1063         struct devlink *devlink = info->user_ptr[0];
1064         struct devlink_sb *devlink_sb = info->user_ptr[1];
1065         enum devlink_sb_threshold_type threshold_type;
1066         u16 pool_index;
1067         u32 size;
1068         int err;
1069
1070         err = devlink_sb_pool_index_get_from_info(devlink_sb, info,
1071                                                   &pool_index);
1072         if (err)
1073                 return err;
1074
1075         err = devlink_sb_th_type_get_from_info(info, &threshold_type);
1076         if (err)
1077                 return err;
1078
1079         if (!info->attrs[DEVLINK_ATTR_SB_POOL_SIZE])
1080                 return -EINVAL;
1081
1082         size = nla_get_u32(info->attrs[DEVLINK_ATTR_SB_POOL_SIZE]);
1083         return devlink_sb_pool_set(devlink, devlink_sb->index,
1084                                    pool_index, size, threshold_type,
1085                                    info->extack);
1086 }
1087
1088 static int devlink_nl_sb_port_pool_fill(struct sk_buff *msg,
1089                                         struct devlink *devlink,
1090                                         struct devlink_port *devlink_port,
1091                                         struct devlink_sb *devlink_sb,
1092                                         u16 pool_index,
1093                                         enum devlink_command cmd,
1094                                         u32 portid, u32 seq, int flags)
1095 {
1096         const struct devlink_ops *ops = devlink->ops;
1097         u32 threshold;
1098         void *hdr;
1099         int err;
1100
1101         err = ops->sb_port_pool_get(devlink_port, devlink_sb->index,
1102                                     pool_index, &threshold);
1103         if (err)
1104                 return err;
1105
1106         hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
1107         if (!hdr)
1108                 return -EMSGSIZE;
1109
1110         if (devlink_nl_put_handle(msg, devlink))
1111                 goto nla_put_failure;
1112         if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, devlink_port->index))
1113                 goto nla_put_failure;
1114         if (nla_put_u32(msg, DEVLINK_ATTR_SB_INDEX, devlink_sb->index))
1115                 goto nla_put_failure;
1116         if (nla_put_u16(msg, DEVLINK_ATTR_SB_POOL_INDEX, pool_index))
1117                 goto nla_put_failure;
1118         if (nla_put_u32(msg, DEVLINK_ATTR_SB_THRESHOLD, threshold))
1119                 goto nla_put_failure;
1120
1121         if (ops->sb_occ_port_pool_get) {
1122                 u32 cur;
1123                 u32 max;
1124
1125                 err = ops->sb_occ_port_pool_get(devlink_port, devlink_sb->index,
1126                                                 pool_index, &cur, &max);
1127                 if (err && err != -EOPNOTSUPP)
1128                         return err;
1129                 if (!err) {
1130                         if (nla_put_u32(msg, DEVLINK_ATTR_SB_OCC_CUR, cur))
1131                                 goto nla_put_failure;
1132                         if (nla_put_u32(msg, DEVLINK_ATTR_SB_OCC_MAX, max))
1133                                 goto nla_put_failure;
1134                 }
1135         }
1136
1137         genlmsg_end(msg, hdr);
1138         return 0;
1139
1140 nla_put_failure:
1141         genlmsg_cancel(msg, hdr);
1142         return -EMSGSIZE;
1143 }
1144
1145 static int devlink_nl_cmd_sb_port_pool_get_doit(struct sk_buff *skb,
1146                                                 struct genl_info *info)
1147 {
1148         struct devlink_port *devlink_port = info->user_ptr[0];
1149         struct devlink *devlink = devlink_port->devlink;
1150         struct devlink_sb *devlink_sb = info->user_ptr[1];
1151         struct sk_buff *msg;
1152         u16 pool_index;
1153         int err;
1154
1155         err = devlink_sb_pool_index_get_from_info(devlink_sb, info,
1156                                                   &pool_index);
1157         if (err)
1158                 return err;
1159
1160         if (!devlink->ops->sb_port_pool_get)
1161                 return -EOPNOTSUPP;
1162
1163         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1164         if (!msg)
1165                 return -ENOMEM;
1166
1167         err = devlink_nl_sb_port_pool_fill(msg, devlink, devlink_port,
1168                                            devlink_sb, pool_index,
1169                                            DEVLINK_CMD_SB_PORT_POOL_NEW,
1170                                            info->snd_portid, info->snd_seq, 0);
1171         if (err) {
1172                 nlmsg_free(msg);
1173                 return err;
1174         }
1175
1176         return genlmsg_reply(msg, info);
1177 }
1178
1179 static int __sb_port_pool_get_dumpit(struct sk_buff *msg, int start, int *p_idx,
1180                                      struct devlink *devlink,
1181                                      struct devlink_sb *devlink_sb,
1182                                      u32 portid, u32 seq)
1183 {
1184         struct devlink_port *devlink_port;
1185         u16 pool_count = devlink_sb_pool_count(devlink_sb);
1186         u16 pool_index;
1187         int err;
1188
1189         list_for_each_entry(devlink_port, &devlink->port_list, list) {
1190                 for (pool_index = 0; pool_index < pool_count; pool_index++) {
1191                         if (*p_idx < start) {
1192                                 (*p_idx)++;
1193                                 continue;
1194                         }
1195                         err = devlink_nl_sb_port_pool_fill(msg, devlink,
1196                                                            devlink_port,
1197                                                            devlink_sb,
1198                                                            pool_index,
1199                                                            DEVLINK_CMD_SB_PORT_POOL_NEW,
1200                                                            portid, seq,
1201                                                            NLM_F_MULTI);
1202                         if (err)
1203                                 return err;
1204                         (*p_idx)++;
1205                 }
1206         }
1207         return 0;
1208 }
1209
1210 static int devlink_nl_cmd_sb_port_pool_get_dumpit(struct sk_buff *msg,
1211                                                   struct netlink_callback *cb)
1212 {
1213         struct devlink *devlink;
1214         struct devlink_sb *devlink_sb;
1215         int start = cb->args[0];
1216         int idx = 0;
1217         int err;
1218
1219         mutex_lock(&devlink_mutex);
1220         list_for_each_entry(devlink, &devlink_list, list) {
1221                 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)) ||
1222                     !devlink->ops->sb_port_pool_get)
1223                         continue;
1224                 mutex_lock(&devlink->lock);
1225                 list_for_each_entry(devlink_sb, &devlink->sb_list, list) {
1226                         err = __sb_port_pool_get_dumpit(msg, start, &idx,
1227                                                         devlink, devlink_sb,
1228                                                         NETLINK_CB(cb->skb).portid,
1229                                                         cb->nlh->nlmsg_seq);
1230                         if (err && err != -EOPNOTSUPP) {
1231                                 mutex_unlock(&devlink->lock);
1232                                 goto out;
1233                         }
1234                 }
1235                 mutex_unlock(&devlink->lock);
1236         }
1237 out:
1238         mutex_unlock(&devlink_mutex);
1239
1240         cb->args[0] = idx;
1241         return msg->len;
1242 }
1243
1244 static int devlink_sb_port_pool_set(struct devlink_port *devlink_port,
1245                                     unsigned int sb_index, u16 pool_index,
1246                                     u32 threshold,
1247                                     struct netlink_ext_ack *extack)
1248
1249 {
1250         const struct devlink_ops *ops = devlink_port->devlink->ops;
1251
1252         if (ops->sb_port_pool_set)
1253                 return ops->sb_port_pool_set(devlink_port, sb_index,
1254                                              pool_index, threshold, extack);
1255         return -EOPNOTSUPP;
1256 }
1257
1258 static int devlink_nl_cmd_sb_port_pool_set_doit(struct sk_buff *skb,
1259                                                 struct genl_info *info)
1260 {
1261         struct devlink_port *devlink_port = info->user_ptr[0];
1262         struct devlink_sb *devlink_sb = info->user_ptr[1];
1263         u16 pool_index;
1264         u32 threshold;
1265         int err;
1266
1267         err = devlink_sb_pool_index_get_from_info(devlink_sb, info,
1268                                                   &pool_index);
1269         if (err)
1270                 return err;
1271
1272         if (!info->attrs[DEVLINK_ATTR_SB_THRESHOLD])
1273                 return -EINVAL;
1274
1275         threshold = nla_get_u32(info->attrs[DEVLINK_ATTR_SB_THRESHOLD]);
1276         return devlink_sb_port_pool_set(devlink_port, devlink_sb->index,
1277                                         pool_index, threshold, info->extack);
1278 }
1279
1280 static int
1281 devlink_nl_sb_tc_pool_bind_fill(struct sk_buff *msg, struct devlink *devlink,
1282                                 struct devlink_port *devlink_port,
1283                                 struct devlink_sb *devlink_sb, u16 tc_index,
1284                                 enum devlink_sb_pool_type pool_type,
1285                                 enum devlink_command cmd,
1286                                 u32 portid, u32 seq, int flags)
1287 {
1288         const struct devlink_ops *ops = devlink->ops;
1289         u16 pool_index;
1290         u32 threshold;
1291         void *hdr;
1292         int err;
1293
1294         err = ops->sb_tc_pool_bind_get(devlink_port, devlink_sb->index,
1295                                        tc_index, pool_type,
1296                                        &pool_index, &threshold);
1297         if (err)
1298                 return err;
1299
1300         hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
1301         if (!hdr)
1302                 return -EMSGSIZE;
1303
1304         if (devlink_nl_put_handle(msg, devlink))
1305                 goto nla_put_failure;
1306         if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, devlink_port->index))
1307                 goto nla_put_failure;
1308         if (nla_put_u32(msg, DEVLINK_ATTR_SB_INDEX, devlink_sb->index))
1309                 goto nla_put_failure;
1310         if (nla_put_u16(msg, DEVLINK_ATTR_SB_TC_INDEX, tc_index))
1311                 goto nla_put_failure;
1312         if (nla_put_u8(msg, DEVLINK_ATTR_SB_POOL_TYPE, pool_type))
1313                 goto nla_put_failure;
1314         if (nla_put_u16(msg, DEVLINK_ATTR_SB_POOL_INDEX, pool_index))
1315                 goto nla_put_failure;
1316         if (nla_put_u32(msg, DEVLINK_ATTR_SB_THRESHOLD, threshold))
1317                 goto nla_put_failure;
1318
1319         if (ops->sb_occ_tc_port_bind_get) {
1320                 u32 cur;
1321                 u32 max;
1322
1323                 err = ops->sb_occ_tc_port_bind_get(devlink_port,
1324                                                    devlink_sb->index,
1325                                                    tc_index, pool_type,
1326                                                    &cur, &max);
1327                 if (err && err != -EOPNOTSUPP)
1328                         return err;
1329                 if (!err) {
1330                         if (nla_put_u32(msg, DEVLINK_ATTR_SB_OCC_CUR, cur))
1331                                 goto nla_put_failure;
1332                         if (nla_put_u32(msg, DEVLINK_ATTR_SB_OCC_MAX, max))
1333                                 goto nla_put_failure;
1334                 }
1335         }
1336
1337         genlmsg_end(msg, hdr);
1338         return 0;
1339
1340 nla_put_failure:
1341         genlmsg_cancel(msg, hdr);
1342         return -EMSGSIZE;
1343 }
1344
1345 static int devlink_nl_cmd_sb_tc_pool_bind_get_doit(struct sk_buff *skb,
1346                                                    struct genl_info *info)
1347 {
1348         struct devlink_port *devlink_port = info->user_ptr[0];
1349         struct devlink *devlink = devlink_port->devlink;
1350         struct devlink_sb *devlink_sb = info->user_ptr[1];
1351         struct sk_buff *msg;
1352         enum devlink_sb_pool_type pool_type;
1353         u16 tc_index;
1354         int err;
1355
1356         err = devlink_sb_pool_type_get_from_info(info, &pool_type);
1357         if (err)
1358                 return err;
1359
1360         err = devlink_sb_tc_index_get_from_info(devlink_sb, info,
1361                                                 pool_type, &tc_index);
1362         if (err)
1363                 return err;
1364
1365         if (!devlink->ops->sb_tc_pool_bind_get)
1366                 return -EOPNOTSUPP;
1367
1368         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1369         if (!msg)
1370                 return -ENOMEM;
1371
1372         err = devlink_nl_sb_tc_pool_bind_fill(msg, devlink, devlink_port,
1373                                               devlink_sb, tc_index, pool_type,
1374                                               DEVLINK_CMD_SB_TC_POOL_BIND_NEW,
1375                                               info->snd_portid,
1376                                               info->snd_seq, 0);
1377         if (err) {
1378                 nlmsg_free(msg);
1379                 return err;
1380         }
1381
1382         return genlmsg_reply(msg, info);
1383 }
1384
1385 static int __sb_tc_pool_bind_get_dumpit(struct sk_buff *msg,
1386                                         int start, int *p_idx,
1387                                         struct devlink *devlink,
1388                                         struct devlink_sb *devlink_sb,
1389                                         u32 portid, u32 seq)
1390 {
1391         struct devlink_port *devlink_port;
1392         u16 tc_index;
1393         int err;
1394
1395         list_for_each_entry(devlink_port, &devlink->port_list, list) {
1396                 for (tc_index = 0;
1397                      tc_index < devlink_sb->ingress_tc_count; tc_index++) {
1398                         if (*p_idx < start) {
1399                                 (*p_idx)++;
1400                                 continue;
1401                         }
1402                         err = devlink_nl_sb_tc_pool_bind_fill(msg, devlink,
1403                                                               devlink_port,
1404                                                               devlink_sb,
1405                                                               tc_index,
1406                                                               DEVLINK_SB_POOL_TYPE_INGRESS,
1407                                                               DEVLINK_CMD_SB_TC_POOL_BIND_NEW,
1408                                                               portid, seq,
1409                                                               NLM_F_MULTI);
1410                         if (err)
1411                                 return err;
1412                         (*p_idx)++;
1413                 }
1414                 for (tc_index = 0;
1415                      tc_index < devlink_sb->egress_tc_count; tc_index++) {
1416                         if (*p_idx < start) {
1417                                 (*p_idx)++;
1418                                 continue;
1419                         }
1420                         err = devlink_nl_sb_tc_pool_bind_fill(msg, devlink,
1421                                                               devlink_port,
1422                                                               devlink_sb,
1423                                                               tc_index,
1424                                                               DEVLINK_SB_POOL_TYPE_EGRESS,
1425                                                               DEVLINK_CMD_SB_TC_POOL_BIND_NEW,
1426                                                               portid, seq,
1427                                                               NLM_F_MULTI);
1428                         if (err)
1429                                 return err;
1430                         (*p_idx)++;
1431                 }
1432         }
1433         return 0;
1434 }
1435
1436 static int
1437 devlink_nl_cmd_sb_tc_pool_bind_get_dumpit(struct sk_buff *msg,
1438                                           struct netlink_callback *cb)
1439 {
1440         struct devlink *devlink;
1441         struct devlink_sb *devlink_sb;
1442         int start = cb->args[0];
1443         int idx = 0;
1444         int err;
1445
1446         mutex_lock(&devlink_mutex);
1447         list_for_each_entry(devlink, &devlink_list, list) {
1448                 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)) ||
1449                     !devlink->ops->sb_tc_pool_bind_get)
1450                         continue;
1451
1452                 mutex_lock(&devlink->lock);
1453                 list_for_each_entry(devlink_sb, &devlink->sb_list, list) {
1454                         err = __sb_tc_pool_bind_get_dumpit(msg, start, &idx,
1455                                                            devlink,
1456                                                            devlink_sb,
1457                                                            NETLINK_CB(cb->skb).portid,
1458                                                            cb->nlh->nlmsg_seq);
1459                         if (err && err != -EOPNOTSUPP) {
1460                                 mutex_unlock(&devlink->lock);
1461                                 goto out;
1462                         }
1463                 }
1464                 mutex_unlock(&devlink->lock);
1465         }
1466 out:
1467         mutex_unlock(&devlink_mutex);
1468
1469         cb->args[0] = idx;
1470         return msg->len;
1471 }
1472
1473 static int devlink_sb_tc_pool_bind_set(struct devlink_port *devlink_port,
1474                                        unsigned int sb_index, u16 tc_index,
1475                                        enum devlink_sb_pool_type pool_type,
1476                                        u16 pool_index, u32 threshold,
1477                                        struct netlink_ext_ack *extack)
1478
1479 {
1480         const struct devlink_ops *ops = devlink_port->devlink->ops;
1481
1482         if (ops->sb_tc_pool_bind_set)
1483                 return ops->sb_tc_pool_bind_set(devlink_port, sb_index,
1484                                                 tc_index, pool_type,
1485                                                 pool_index, threshold, extack);
1486         return -EOPNOTSUPP;
1487 }
1488
1489 static int devlink_nl_cmd_sb_tc_pool_bind_set_doit(struct sk_buff *skb,
1490                                                    struct genl_info *info)
1491 {
1492         struct devlink_port *devlink_port = info->user_ptr[0];
1493         struct devlink_sb *devlink_sb = info->user_ptr[1];
1494         enum devlink_sb_pool_type pool_type;
1495         u16 tc_index;
1496         u16 pool_index;
1497         u32 threshold;
1498         int err;
1499
1500         err = devlink_sb_pool_type_get_from_info(info, &pool_type);
1501         if (err)
1502                 return err;
1503
1504         err = devlink_sb_tc_index_get_from_info(devlink_sb, info,
1505                                                 pool_type, &tc_index);
1506         if (err)
1507                 return err;
1508
1509         err = devlink_sb_pool_index_get_from_info(devlink_sb, info,
1510                                                   &pool_index);
1511         if (err)
1512                 return err;
1513
1514         if (!info->attrs[DEVLINK_ATTR_SB_THRESHOLD])
1515                 return -EINVAL;
1516
1517         threshold = nla_get_u32(info->attrs[DEVLINK_ATTR_SB_THRESHOLD]);
1518         return devlink_sb_tc_pool_bind_set(devlink_port, devlink_sb->index,
1519                                            tc_index, pool_type,
1520                                            pool_index, threshold, info->extack);
1521 }
1522
1523 static int devlink_nl_cmd_sb_occ_snapshot_doit(struct sk_buff *skb,
1524                                                struct genl_info *info)
1525 {
1526         struct devlink *devlink = info->user_ptr[0];
1527         struct devlink_sb *devlink_sb = info->user_ptr[1];
1528         const struct devlink_ops *ops = devlink->ops;
1529
1530         if (ops->sb_occ_snapshot)
1531                 return ops->sb_occ_snapshot(devlink, devlink_sb->index);
1532         return -EOPNOTSUPP;
1533 }
1534
1535 static int devlink_nl_cmd_sb_occ_max_clear_doit(struct sk_buff *skb,
1536                                                 struct genl_info *info)
1537 {
1538         struct devlink *devlink = info->user_ptr[0];
1539         struct devlink_sb *devlink_sb = info->user_ptr[1];
1540         const struct devlink_ops *ops = devlink->ops;
1541
1542         if (ops->sb_occ_max_clear)
1543                 return ops->sb_occ_max_clear(devlink, devlink_sb->index);
1544         return -EOPNOTSUPP;
1545 }
1546
1547 static int devlink_nl_eswitch_fill(struct sk_buff *msg, struct devlink *devlink,
1548                                    enum devlink_command cmd, u32 portid,
1549                                    u32 seq, int flags)
1550 {
1551         const struct devlink_ops *ops = devlink->ops;
1552         u8 inline_mode, encap_mode;
1553         void *hdr;
1554         int err = 0;
1555         u16 mode;
1556
1557         hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
1558         if (!hdr)
1559                 return -EMSGSIZE;
1560
1561         err = devlink_nl_put_handle(msg, devlink);
1562         if (err)
1563                 goto nla_put_failure;
1564
1565         if (ops->eswitch_mode_get) {
1566                 err = ops->eswitch_mode_get(devlink, &mode);
1567                 if (err)
1568                         goto nla_put_failure;
1569                 err = nla_put_u16(msg, DEVLINK_ATTR_ESWITCH_MODE, mode);
1570                 if (err)
1571                         goto nla_put_failure;
1572         }
1573
1574         if (ops->eswitch_inline_mode_get) {
1575                 err = ops->eswitch_inline_mode_get(devlink, &inline_mode);
1576                 if (err)
1577                         goto nla_put_failure;
1578                 err = nla_put_u8(msg, DEVLINK_ATTR_ESWITCH_INLINE_MODE,
1579                                  inline_mode);
1580                 if (err)
1581                         goto nla_put_failure;
1582         }
1583
1584         if (ops->eswitch_encap_mode_get) {
1585                 err = ops->eswitch_encap_mode_get(devlink, &encap_mode);
1586                 if (err)
1587                         goto nla_put_failure;
1588                 err = nla_put_u8(msg, DEVLINK_ATTR_ESWITCH_ENCAP_MODE, encap_mode);
1589                 if (err)
1590                         goto nla_put_failure;
1591         }
1592
1593         genlmsg_end(msg, hdr);
1594         return 0;
1595
1596 nla_put_failure:
1597         genlmsg_cancel(msg, hdr);
1598         return err;
1599 }
1600
1601 static int devlink_nl_cmd_eswitch_get_doit(struct sk_buff *skb,
1602                                            struct genl_info *info)
1603 {
1604         struct devlink *devlink = info->user_ptr[0];
1605         struct sk_buff *msg;
1606         int err;
1607
1608         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1609         if (!msg)
1610                 return -ENOMEM;
1611
1612         err = devlink_nl_eswitch_fill(msg, devlink, DEVLINK_CMD_ESWITCH_GET,
1613                                       info->snd_portid, info->snd_seq, 0);
1614
1615         if (err) {
1616                 nlmsg_free(msg);
1617                 return err;
1618         }
1619
1620         return genlmsg_reply(msg, info);
1621 }
1622
1623 static int devlink_nl_cmd_eswitch_set_doit(struct sk_buff *skb,
1624                                            struct genl_info *info)
1625 {
1626         struct devlink *devlink = info->user_ptr[0];
1627         const struct devlink_ops *ops = devlink->ops;
1628         u8 inline_mode, encap_mode;
1629         int err = 0;
1630         u16 mode;
1631
1632         if (info->attrs[DEVLINK_ATTR_ESWITCH_MODE]) {
1633                 if (!ops->eswitch_mode_set)
1634                         return -EOPNOTSUPP;
1635                 mode = nla_get_u16(info->attrs[DEVLINK_ATTR_ESWITCH_MODE]);
1636                 err = ops->eswitch_mode_set(devlink, mode, info->extack);
1637                 if (err)
1638                         return err;
1639         }
1640
1641         if (info->attrs[DEVLINK_ATTR_ESWITCH_INLINE_MODE]) {
1642                 if (!ops->eswitch_inline_mode_set)
1643                         return -EOPNOTSUPP;
1644                 inline_mode = nla_get_u8(
1645                                 info->attrs[DEVLINK_ATTR_ESWITCH_INLINE_MODE]);
1646                 err = ops->eswitch_inline_mode_set(devlink, inline_mode,
1647                                                    info->extack);
1648                 if (err)
1649                         return err;
1650         }
1651
1652         if (info->attrs[DEVLINK_ATTR_ESWITCH_ENCAP_MODE]) {
1653                 if (!ops->eswitch_encap_mode_set)
1654                         return -EOPNOTSUPP;
1655                 encap_mode = nla_get_u8(info->attrs[DEVLINK_ATTR_ESWITCH_ENCAP_MODE]);
1656                 err = ops->eswitch_encap_mode_set(devlink, encap_mode,
1657                                                   info->extack);
1658                 if (err)
1659                         return err;
1660         }
1661
1662         return 0;
1663 }
1664
1665 int devlink_dpipe_match_put(struct sk_buff *skb,
1666                             struct devlink_dpipe_match *match)
1667 {
1668         struct devlink_dpipe_header *header = match->header;
1669         struct devlink_dpipe_field *field = &header->fields[match->field_id];
1670         struct nlattr *match_attr;
1671
1672         match_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_DPIPE_MATCH);
1673         if (!match_attr)
1674                 return -EMSGSIZE;
1675
1676         if (nla_put_u32(skb, DEVLINK_ATTR_DPIPE_MATCH_TYPE, match->type) ||
1677             nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_INDEX, match->header_index) ||
1678             nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_ID, header->id) ||
1679             nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_ID, field->id) ||
1680             nla_put_u8(skb, DEVLINK_ATTR_DPIPE_HEADER_GLOBAL, header->global))
1681                 goto nla_put_failure;
1682
1683         nla_nest_end(skb, match_attr);
1684         return 0;
1685
1686 nla_put_failure:
1687         nla_nest_cancel(skb, match_attr);
1688         return -EMSGSIZE;
1689 }
1690 EXPORT_SYMBOL_GPL(devlink_dpipe_match_put);
1691
1692 static int devlink_dpipe_matches_put(struct devlink_dpipe_table *table,
1693                                      struct sk_buff *skb)
1694 {
1695         struct nlattr *matches_attr;
1696
1697         matches_attr = nla_nest_start_noflag(skb,
1698                                              DEVLINK_ATTR_DPIPE_TABLE_MATCHES);
1699         if (!matches_attr)
1700                 return -EMSGSIZE;
1701
1702         if (table->table_ops->matches_dump(table->priv, skb))
1703                 goto nla_put_failure;
1704
1705         nla_nest_end(skb, matches_attr);
1706         return 0;
1707
1708 nla_put_failure:
1709         nla_nest_cancel(skb, matches_attr);
1710         return -EMSGSIZE;
1711 }
1712
1713 int devlink_dpipe_action_put(struct sk_buff *skb,
1714                              struct devlink_dpipe_action *action)
1715 {
1716         struct devlink_dpipe_header *header = action->header;
1717         struct devlink_dpipe_field *field = &header->fields[action->field_id];
1718         struct nlattr *action_attr;
1719
1720         action_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_DPIPE_ACTION);
1721         if (!action_attr)
1722                 return -EMSGSIZE;
1723
1724         if (nla_put_u32(skb, DEVLINK_ATTR_DPIPE_ACTION_TYPE, action->type) ||
1725             nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_INDEX, action->header_index) ||
1726             nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_ID, header->id) ||
1727             nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_ID, field->id) ||
1728             nla_put_u8(skb, DEVLINK_ATTR_DPIPE_HEADER_GLOBAL, header->global))
1729                 goto nla_put_failure;
1730
1731         nla_nest_end(skb, action_attr);
1732         return 0;
1733
1734 nla_put_failure:
1735         nla_nest_cancel(skb, action_attr);
1736         return -EMSGSIZE;
1737 }
1738 EXPORT_SYMBOL_GPL(devlink_dpipe_action_put);
1739
1740 static int devlink_dpipe_actions_put(struct devlink_dpipe_table *table,
1741                                      struct sk_buff *skb)
1742 {
1743         struct nlattr *actions_attr;
1744
1745         actions_attr = nla_nest_start_noflag(skb,
1746                                              DEVLINK_ATTR_DPIPE_TABLE_ACTIONS);
1747         if (!actions_attr)
1748                 return -EMSGSIZE;
1749
1750         if (table->table_ops->actions_dump(table->priv, skb))
1751                 goto nla_put_failure;
1752
1753         nla_nest_end(skb, actions_attr);
1754         return 0;
1755
1756 nla_put_failure:
1757         nla_nest_cancel(skb, actions_attr);
1758         return -EMSGSIZE;
1759 }
1760
1761 static int devlink_dpipe_table_put(struct sk_buff *skb,
1762                                    struct devlink_dpipe_table *table)
1763 {
1764         struct nlattr *table_attr;
1765         u64 table_size;
1766
1767         table_size = table->table_ops->size_get(table->priv);
1768         table_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_DPIPE_TABLE);
1769         if (!table_attr)
1770                 return -EMSGSIZE;
1771
1772         if (nla_put_string(skb, DEVLINK_ATTR_DPIPE_TABLE_NAME, table->name) ||
1773             nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_TABLE_SIZE, table_size,
1774                               DEVLINK_ATTR_PAD))
1775                 goto nla_put_failure;
1776         if (nla_put_u8(skb, DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED,
1777                        table->counters_enabled))
1778                 goto nla_put_failure;
1779
1780         if (table->resource_valid) {
1781                 if (nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_TABLE_RESOURCE_ID,
1782                                       table->resource_id, DEVLINK_ATTR_PAD) ||
1783                     nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_TABLE_RESOURCE_UNITS,
1784                                       table->resource_units, DEVLINK_ATTR_PAD))
1785                         goto nla_put_failure;
1786         }
1787         if (devlink_dpipe_matches_put(table, skb))
1788                 goto nla_put_failure;
1789
1790         if (devlink_dpipe_actions_put(table, skb))
1791                 goto nla_put_failure;
1792
1793         nla_nest_end(skb, table_attr);
1794         return 0;
1795
1796 nla_put_failure:
1797         nla_nest_cancel(skb, table_attr);
1798         return -EMSGSIZE;
1799 }
1800
1801 static int devlink_dpipe_send_and_alloc_skb(struct sk_buff **pskb,
1802                                             struct genl_info *info)
1803 {
1804         int err;
1805
1806         if (*pskb) {
1807                 err = genlmsg_reply(*pskb, info);
1808                 if (err)
1809                         return err;
1810         }
1811         *pskb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
1812         if (!*pskb)
1813                 return -ENOMEM;
1814         return 0;
1815 }
1816
1817 static int devlink_dpipe_tables_fill(struct genl_info *info,
1818                                      enum devlink_command cmd, int flags,
1819                                      struct list_head *dpipe_tables,
1820                                      const char *table_name)
1821 {
1822         struct devlink *devlink = info->user_ptr[0];
1823         struct devlink_dpipe_table *table;
1824         struct nlattr *tables_attr;
1825         struct sk_buff *skb = NULL;
1826         struct nlmsghdr *nlh;
1827         bool incomplete;
1828         void *hdr;
1829         int i;
1830         int err;
1831
1832         table = list_first_entry(dpipe_tables,
1833                                  struct devlink_dpipe_table, list);
1834 start_again:
1835         err = devlink_dpipe_send_and_alloc_skb(&skb, info);
1836         if (err)
1837                 return err;
1838
1839         hdr = genlmsg_put(skb, info->snd_portid, info->snd_seq,
1840                           &devlink_nl_family, NLM_F_MULTI, cmd);
1841         if (!hdr) {
1842                 nlmsg_free(skb);
1843                 return -EMSGSIZE;
1844         }
1845
1846         if (devlink_nl_put_handle(skb, devlink))
1847                 goto nla_put_failure;
1848         tables_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_DPIPE_TABLES);
1849         if (!tables_attr)
1850                 goto nla_put_failure;
1851
1852         i = 0;
1853         incomplete = false;
1854         list_for_each_entry_from(table, dpipe_tables, list) {
1855                 if (!table_name) {
1856                         err = devlink_dpipe_table_put(skb, table);
1857                         if (err) {
1858                                 if (!i)
1859                                         goto err_table_put;
1860                                 incomplete = true;
1861                                 break;
1862                         }
1863                 } else {
1864                         if (!strcmp(table->name, table_name)) {
1865                                 err = devlink_dpipe_table_put(skb, table);
1866                                 if (err)
1867                                         break;
1868                         }
1869                 }
1870                 i++;
1871         }
1872
1873         nla_nest_end(skb, tables_attr);
1874         genlmsg_end(skb, hdr);
1875         if (incomplete)
1876                 goto start_again;
1877
1878 send_done:
1879         nlh = nlmsg_put(skb, info->snd_portid, info->snd_seq,
1880                         NLMSG_DONE, 0, flags | NLM_F_MULTI);
1881         if (!nlh) {
1882                 err = devlink_dpipe_send_and_alloc_skb(&skb, info);
1883                 if (err)
1884                         return err;
1885                 goto send_done;
1886         }
1887
1888         return genlmsg_reply(skb, info);
1889
1890 nla_put_failure:
1891         err = -EMSGSIZE;
1892 err_table_put:
1893         nlmsg_free(skb);
1894         return err;
1895 }
1896
1897 static int devlink_nl_cmd_dpipe_table_get(struct sk_buff *skb,
1898                                           struct genl_info *info)
1899 {
1900         struct devlink *devlink = info->user_ptr[0];
1901         const char *table_name =  NULL;
1902
1903         if (info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME])
1904                 table_name = nla_data(info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME]);
1905
1906         return devlink_dpipe_tables_fill(info, DEVLINK_CMD_DPIPE_TABLE_GET, 0,
1907                                          &devlink->dpipe_table_list,
1908                                          table_name);
1909 }
1910
1911 static int devlink_dpipe_value_put(struct sk_buff *skb,
1912                                    struct devlink_dpipe_value *value)
1913 {
1914         if (nla_put(skb, DEVLINK_ATTR_DPIPE_VALUE,
1915                     value->value_size, value->value))
1916                 return -EMSGSIZE;
1917         if (value->mask)
1918                 if (nla_put(skb, DEVLINK_ATTR_DPIPE_VALUE_MASK,
1919                             value->value_size, value->mask))
1920                         return -EMSGSIZE;
1921         if (value->mapping_valid)
1922                 if (nla_put_u32(skb, DEVLINK_ATTR_DPIPE_VALUE_MAPPING,
1923                                 value->mapping_value))
1924                         return -EMSGSIZE;
1925         return 0;
1926 }
1927
1928 static int devlink_dpipe_action_value_put(struct sk_buff *skb,
1929                                           struct devlink_dpipe_value *value)
1930 {
1931         if (!value->action)
1932                 return -EINVAL;
1933         if (devlink_dpipe_action_put(skb, value->action))
1934                 return -EMSGSIZE;
1935         if (devlink_dpipe_value_put(skb, value))
1936                 return -EMSGSIZE;
1937         return 0;
1938 }
1939
1940 static int devlink_dpipe_action_values_put(struct sk_buff *skb,
1941                                            struct devlink_dpipe_value *values,
1942                                            unsigned int values_count)
1943 {
1944         struct nlattr *action_attr;
1945         int i;
1946         int err;
1947
1948         for (i = 0; i < values_count; i++) {
1949                 action_attr = nla_nest_start_noflag(skb,
1950                                                     DEVLINK_ATTR_DPIPE_ACTION_VALUE);
1951                 if (!action_attr)
1952                         return -EMSGSIZE;
1953                 err = devlink_dpipe_action_value_put(skb, &values[i]);
1954                 if (err)
1955                         goto err_action_value_put;
1956                 nla_nest_end(skb, action_attr);
1957         }
1958         return 0;
1959
1960 err_action_value_put:
1961         nla_nest_cancel(skb, action_attr);
1962         return err;
1963 }
1964
1965 static int devlink_dpipe_match_value_put(struct sk_buff *skb,
1966                                          struct devlink_dpipe_value *value)
1967 {
1968         if (!value->match)
1969                 return -EINVAL;
1970         if (devlink_dpipe_match_put(skb, value->match))
1971                 return -EMSGSIZE;
1972         if (devlink_dpipe_value_put(skb, value))
1973                 return -EMSGSIZE;
1974         return 0;
1975 }
1976
1977 static int devlink_dpipe_match_values_put(struct sk_buff *skb,
1978                                           struct devlink_dpipe_value *values,
1979                                           unsigned int values_count)
1980 {
1981         struct nlattr *match_attr;
1982         int i;
1983         int err;
1984
1985         for (i = 0; i < values_count; i++) {
1986                 match_attr = nla_nest_start_noflag(skb,
1987                                                    DEVLINK_ATTR_DPIPE_MATCH_VALUE);
1988                 if (!match_attr)
1989                         return -EMSGSIZE;
1990                 err = devlink_dpipe_match_value_put(skb, &values[i]);
1991                 if (err)
1992                         goto err_match_value_put;
1993                 nla_nest_end(skb, match_attr);
1994         }
1995         return 0;
1996
1997 err_match_value_put:
1998         nla_nest_cancel(skb, match_attr);
1999         return err;
2000 }
2001
2002 static int devlink_dpipe_entry_put(struct sk_buff *skb,
2003                                    struct devlink_dpipe_entry *entry)
2004 {
2005         struct nlattr *entry_attr, *matches_attr, *actions_attr;
2006         int err;
2007
2008         entry_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_DPIPE_ENTRY);
2009         if (!entry_attr)
2010                 return  -EMSGSIZE;
2011
2012         if (nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_ENTRY_INDEX, entry->index,
2013                               DEVLINK_ATTR_PAD))
2014                 goto nla_put_failure;
2015         if (entry->counter_valid)
2016                 if (nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_ENTRY_COUNTER,
2017                                       entry->counter, DEVLINK_ATTR_PAD))
2018                         goto nla_put_failure;
2019
2020         matches_attr = nla_nest_start_noflag(skb,
2021                                              DEVLINK_ATTR_DPIPE_ENTRY_MATCH_VALUES);
2022         if (!matches_attr)
2023                 goto nla_put_failure;
2024
2025         err = devlink_dpipe_match_values_put(skb, entry->match_values,
2026                                              entry->match_values_count);
2027         if (err) {
2028                 nla_nest_cancel(skb, matches_attr);
2029                 goto err_match_values_put;
2030         }
2031         nla_nest_end(skb, matches_attr);
2032
2033         actions_attr = nla_nest_start_noflag(skb,
2034                                              DEVLINK_ATTR_DPIPE_ENTRY_ACTION_VALUES);
2035         if (!actions_attr)
2036                 goto nla_put_failure;
2037
2038         err = devlink_dpipe_action_values_put(skb, entry->action_values,
2039                                               entry->action_values_count);
2040         if (err) {
2041                 nla_nest_cancel(skb, actions_attr);
2042                 goto err_action_values_put;
2043         }
2044         nla_nest_end(skb, actions_attr);
2045
2046         nla_nest_end(skb, entry_attr);
2047         return 0;
2048
2049 nla_put_failure:
2050         err = -EMSGSIZE;
2051 err_match_values_put:
2052 err_action_values_put:
2053         nla_nest_cancel(skb, entry_attr);
2054         return err;
2055 }
2056
2057 static struct devlink_dpipe_table *
2058 devlink_dpipe_table_find(struct list_head *dpipe_tables,
2059                          const char *table_name)
2060 {
2061         struct devlink_dpipe_table *table;
2062
2063         list_for_each_entry_rcu(table, dpipe_tables, list) {
2064                 if (!strcmp(table->name, table_name))
2065                         return table;
2066         }
2067         return NULL;
2068 }
2069
2070 int devlink_dpipe_entry_ctx_prepare(struct devlink_dpipe_dump_ctx *dump_ctx)
2071 {
2072         struct devlink *devlink;
2073         int err;
2074
2075         err = devlink_dpipe_send_and_alloc_skb(&dump_ctx->skb,
2076                                                dump_ctx->info);
2077         if (err)
2078                 return err;
2079
2080         dump_ctx->hdr = genlmsg_put(dump_ctx->skb,
2081                                     dump_ctx->info->snd_portid,
2082                                     dump_ctx->info->snd_seq,
2083                                     &devlink_nl_family, NLM_F_MULTI,
2084                                     dump_ctx->cmd);
2085         if (!dump_ctx->hdr)
2086                 goto nla_put_failure;
2087
2088         devlink = dump_ctx->info->user_ptr[0];
2089         if (devlink_nl_put_handle(dump_ctx->skb, devlink))
2090                 goto nla_put_failure;
2091         dump_ctx->nest = nla_nest_start_noflag(dump_ctx->skb,
2092                                                DEVLINK_ATTR_DPIPE_ENTRIES);
2093         if (!dump_ctx->nest)
2094                 goto nla_put_failure;
2095         return 0;
2096
2097 nla_put_failure:
2098         nlmsg_free(dump_ctx->skb);
2099         return -EMSGSIZE;
2100 }
2101 EXPORT_SYMBOL_GPL(devlink_dpipe_entry_ctx_prepare);
2102
2103 int devlink_dpipe_entry_ctx_append(struct devlink_dpipe_dump_ctx *dump_ctx,
2104                                    struct devlink_dpipe_entry *entry)
2105 {
2106         return devlink_dpipe_entry_put(dump_ctx->skb, entry);
2107 }
2108 EXPORT_SYMBOL_GPL(devlink_dpipe_entry_ctx_append);
2109
2110 int devlink_dpipe_entry_ctx_close(struct devlink_dpipe_dump_ctx *dump_ctx)
2111 {
2112         nla_nest_end(dump_ctx->skb, dump_ctx->nest);
2113         genlmsg_end(dump_ctx->skb, dump_ctx->hdr);
2114         return 0;
2115 }
2116 EXPORT_SYMBOL_GPL(devlink_dpipe_entry_ctx_close);
2117
2118 void devlink_dpipe_entry_clear(struct devlink_dpipe_entry *entry)
2119
2120 {
2121         unsigned int value_count, value_index;
2122         struct devlink_dpipe_value *value;
2123
2124         value = entry->action_values;
2125         value_count = entry->action_values_count;
2126         for (value_index = 0; value_index < value_count; value_index++) {
2127                 kfree(value[value_index].value);
2128                 kfree(value[value_index].mask);
2129         }
2130
2131         value = entry->match_values;
2132         value_count = entry->match_values_count;
2133         for (value_index = 0; value_index < value_count; value_index++) {
2134                 kfree(value[value_index].value);
2135                 kfree(value[value_index].mask);
2136         }
2137 }
2138 EXPORT_SYMBOL(devlink_dpipe_entry_clear);
2139
2140 static int devlink_dpipe_entries_fill(struct genl_info *info,
2141                                       enum devlink_command cmd, int flags,
2142                                       struct devlink_dpipe_table *table)
2143 {
2144         struct devlink_dpipe_dump_ctx dump_ctx;
2145         struct nlmsghdr *nlh;
2146         int err;
2147
2148         dump_ctx.skb = NULL;
2149         dump_ctx.cmd = cmd;
2150         dump_ctx.info = info;
2151
2152         err = table->table_ops->entries_dump(table->priv,
2153                                              table->counters_enabled,
2154                                              &dump_ctx);
2155         if (err)
2156                 return err;
2157
2158 send_done:
2159         nlh = nlmsg_put(dump_ctx.skb, info->snd_portid, info->snd_seq,
2160                         NLMSG_DONE, 0, flags | NLM_F_MULTI);
2161         if (!nlh) {
2162                 err = devlink_dpipe_send_and_alloc_skb(&dump_ctx.skb, info);
2163                 if (err)
2164                         return err;
2165                 goto send_done;
2166         }
2167         return genlmsg_reply(dump_ctx.skb, info);
2168 }
2169
2170 static int devlink_nl_cmd_dpipe_entries_get(struct sk_buff *skb,
2171                                             struct genl_info *info)
2172 {
2173         struct devlink *devlink = info->user_ptr[0];
2174         struct devlink_dpipe_table *table;
2175         const char *table_name;
2176
2177         if (!info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME])
2178                 return -EINVAL;
2179
2180         table_name = nla_data(info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME]);
2181         table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
2182                                          table_name);
2183         if (!table)
2184                 return -EINVAL;
2185
2186         if (!table->table_ops->entries_dump)
2187                 return -EINVAL;
2188
2189         return devlink_dpipe_entries_fill(info, DEVLINK_CMD_DPIPE_ENTRIES_GET,
2190                                           0, table);
2191 }
2192
2193 static int devlink_dpipe_fields_put(struct sk_buff *skb,
2194                                     const struct devlink_dpipe_header *header)
2195 {
2196         struct devlink_dpipe_field *field;
2197         struct nlattr *field_attr;
2198         int i;
2199
2200         for (i = 0; i < header->fields_count; i++) {
2201                 field = &header->fields[i];
2202                 field_attr = nla_nest_start_noflag(skb,
2203                                                    DEVLINK_ATTR_DPIPE_FIELD);
2204                 if (!field_attr)
2205                         return -EMSGSIZE;
2206                 if (nla_put_string(skb, DEVLINK_ATTR_DPIPE_FIELD_NAME, field->name) ||
2207                     nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_ID, field->id) ||
2208                     nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_BITWIDTH, field->bitwidth) ||
2209                     nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_MAPPING_TYPE, field->mapping_type))
2210                         goto nla_put_failure;
2211                 nla_nest_end(skb, field_attr);
2212         }
2213         return 0;
2214
2215 nla_put_failure:
2216         nla_nest_cancel(skb, field_attr);
2217         return -EMSGSIZE;
2218 }
2219
2220 static int devlink_dpipe_header_put(struct sk_buff *skb,
2221                                     struct devlink_dpipe_header *header)
2222 {
2223         struct nlattr *fields_attr, *header_attr;
2224         int err;
2225
2226         header_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_DPIPE_HEADER);
2227         if (!header_attr)
2228                 return -EMSGSIZE;
2229
2230         if (nla_put_string(skb, DEVLINK_ATTR_DPIPE_HEADER_NAME, header->name) ||
2231             nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_ID, header->id) ||
2232             nla_put_u8(skb, DEVLINK_ATTR_DPIPE_HEADER_GLOBAL, header->global))
2233                 goto nla_put_failure;
2234
2235         fields_attr = nla_nest_start_noflag(skb,
2236                                             DEVLINK_ATTR_DPIPE_HEADER_FIELDS);
2237         if (!fields_attr)
2238                 goto nla_put_failure;
2239
2240         err = devlink_dpipe_fields_put(skb, header);
2241         if (err) {
2242                 nla_nest_cancel(skb, fields_attr);
2243                 goto nla_put_failure;
2244         }
2245         nla_nest_end(skb, fields_attr);
2246         nla_nest_end(skb, header_attr);
2247         return 0;
2248
2249 nla_put_failure:
2250         err = -EMSGSIZE;
2251         nla_nest_cancel(skb, header_attr);
2252         return err;
2253 }
2254
2255 static int devlink_dpipe_headers_fill(struct genl_info *info,
2256                                       enum devlink_command cmd, int flags,
2257                                       struct devlink_dpipe_headers *
2258                                       dpipe_headers)
2259 {
2260         struct devlink *devlink = info->user_ptr[0];
2261         struct nlattr *headers_attr;
2262         struct sk_buff *skb = NULL;
2263         struct nlmsghdr *nlh;
2264         void *hdr;
2265         int i, j;
2266         int err;
2267
2268         i = 0;
2269 start_again:
2270         err = devlink_dpipe_send_and_alloc_skb(&skb, info);
2271         if (err)
2272                 return err;
2273
2274         hdr = genlmsg_put(skb, info->snd_portid, info->snd_seq,
2275                           &devlink_nl_family, NLM_F_MULTI, cmd);
2276         if (!hdr) {
2277                 nlmsg_free(skb);
2278                 return -EMSGSIZE;
2279         }
2280
2281         if (devlink_nl_put_handle(skb, devlink))
2282                 goto nla_put_failure;
2283         headers_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_DPIPE_HEADERS);
2284         if (!headers_attr)
2285                 goto nla_put_failure;
2286
2287         j = 0;
2288         for (; i < dpipe_headers->headers_count; i++) {
2289                 err = devlink_dpipe_header_put(skb, dpipe_headers->headers[i]);
2290                 if (err) {
2291                         if (!j)
2292                                 goto err_table_put;
2293                         break;
2294                 }
2295                 j++;
2296         }
2297         nla_nest_end(skb, headers_attr);
2298         genlmsg_end(skb, hdr);
2299         if (i != dpipe_headers->headers_count)
2300                 goto start_again;
2301
2302 send_done:
2303         nlh = nlmsg_put(skb, info->snd_portid, info->snd_seq,
2304                         NLMSG_DONE, 0, flags | NLM_F_MULTI);
2305         if (!nlh) {
2306                 err = devlink_dpipe_send_and_alloc_skb(&skb, info);
2307                 if (err)
2308                         return err;
2309                 goto send_done;
2310         }
2311         return genlmsg_reply(skb, info);
2312
2313 nla_put_failure:
2314         err = -EMSGSIZE;
2315 err_table_put:
2316         nlmsg_free(skb);
2317         return err;
2318 }
2319
2320 static int devlink_nl_cmd_dpipe_headers_get(struct sk_buff *skb,
2321                                             struct genl_info *info)
2322 {
2323         struct devlink *devlink = info->user_ptr[0];
2324
2325         if (!devlink->dpipe_headers)
2326                 return -EOPNOTSUPP;
2327         return devlink_dpipe_headers_fill(info, DEVLINK_CMD_DPIPE_HEADERS_GET,
2328                                           0, devlink->dpipe_headers);
2329 }
2330
2331 static int devlink_dpipe_table_counters_set(struct devlink *devlink,
2332                                             const char *table_name,
2333                                             bool enable)
2334 {
2335         struct devlink_dpipe_table *table;
2336
2337         table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
2338                                          table_name);
2339         if (!table)
2340                 return -EINVAL;
2341
2342         if (table->counter_control_extern)
2343                 return -EOPNOTSUPP;
2344
2345         if (!(table->counters_enabled ^ enable))
2346                 return 0;
2347
2348         table->counters_enabled = enable;
2349         if (table->table_ops->counters_set_update)
2350                 table->table_ops->counters_set_update(table->priv, enable);
2351         return 0;
2352 }
2353
2354 static int devlink_nl_cmd_dpipe_table_counters_set(struct sk_buff *skb,
2355                                                    struct genl_info *info)
2356 {
2357         struct devlink *devlink = info->user_ptr[0];
2358         const char *table_name;
2359         bool counters_enable;
2360
2361         if (!info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME] ||
2362             !info->attrs[DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED])
2363                 return -EINVAL;
2364
2365         table_name = nla_data(info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME]);
2366         counters_enable = !!nla_get_u8(info->attrs[DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED]);
2367
2368         return devlink_dpipe_table_counters_set(devlink, table_name,
2369                                                 counters_enable);
2370 }
2371
2372 static struct devlink_resource *
2373 devlink_resource_find(struct devlink *devlink,
2374                       struct devlink_resource *resource, u64 resource_id)
2375 {
2376         struct list_head *resource_list;
2377
2378         if (resource)
2379                 resource_list = &resource->resource_list;
2380         else
2381                 resource_list = &devlink->resource_list;
2382
2383         list_for_each_entry(resource, resource_list, list) {
2384                 struct devlink_resource *child_resource;
2385
2386                 if (resource->id == resource_id)
2387                         return resource;
2388
2389                 child_resource = devlink_resource_find(devlink, resource,
2390                                                        resource_id);
2391                 if (child_resource)
2392                         return child_resource;
2393         }
2394         return NULL;
2395 }
2396
2397 static void
2398 devlink_resource_validate_children(struct devlink_resource *resource)
2399 {
2400         struct devlink_resource *child_resource;
2401         bool size_valid = true;
2402         u64 parts_size = 0;
2403
2404         if (list_empty(&resource->resource_list))
2405                 goto out;
2406
2407         list_for_each_entry(child_resource, &resource->resource_list, list)
2408                 parts_size += child_resource->size_new;
2409
2410         if (parts_size > resource->size_new)
2411                 size_valid = false;
2412 out:
2413         resource->size_valid = size_valid;
2414 }
2415
2416 static int
2417 devlink_resource_validate_size(struct devlink_resource *resource, u64 size,
2418                                struct netlink_ext_ack *extack)
2419 {
2420         u64 reminder;
2421         int err = 0;
2422
2423         if (size > resource->size_params.size_max) {
2424                 NL_SET_ERR_MSG_MOD(extack, "Size larger than maximum");
2425                 err = -EINVAL;
2426         }
2427
2428         if (size < resource->size_params.size_min) {
2429                 NL_SET_ERR_MSG_MOD(extack, "Size smaller than minimum");
2430                 err = -EINVAL;
2431         }
2432
2433         div64_u64_rem(size, resource->size_params.size_granularity, &reminder);
2434         if (reminder) {
2435                 NL_SET_ERR_MSG_MOD(extack, "Wrong granularity");
2436                 err = -EINVAL;
2437         }
2438
2439         return err;
2440 }
2441
2442 static int devlink_nl_cmd_resource_set(struct sk_buff *skb,
2443                                        struct genl_info *info)
2444 {
2445         struct devlink *devlink = info->user_ptr[0];
2446         struct devlink_resource *resource;
2447         u64 resource_id;
2448         u64 size;
2449         int err;
2450
2451         if (!info->attrs[DEVLINK_ATTR_RESOURCE_ID] ||
2452             !info->attrs[DEVLINK_ATTR_RESOURCE_SIZE])
2453                 return -EINVAL;
2454         resource_id = nla_get_u64(info->attrs[DEVLINK_ATTR_RESOURCE_ID]);
2455
2456         resource = devlink_resource_find(devlink, NULL, resource_id);
2457         if (!resource)
2458                 return -EINVAL;
2459
2460         size = nla_get_u64(info->attrs[DEVLINK_ATTR_RESOURCE_SIZE]);
2461         err = devlink_resource_validate_size(resource, size, info->extack);
2462         if (err)
2463                 return err;
2464
2465         resource->size_new = size;
2466         devlink_resource_validate_children(resource);
2467         if (resource->parent)
2468                 devlink_resource_validate_children(resource->parent);
2469         return 0;
2470 }
2471
2472 static int
2473 devlink_resource_size_params_put(struct devlink_resource *resource,
2474                                  struct sk_buff *skb)
2475 {
2476         struct devlink_resource_size_params *size_params;
2477
2478         size_params = &resource->size_params;
2479         if (nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_SIZE_GRAN,
2480                               size_params->size_granularity, DEVLINK_ATTR_PAD) ||
2481             nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_SIZE_MAX,
2482                               size_params->size_max, DEVLINK_ATTR_PAD) ||
2483             nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_SIZE_MIN,
2484                               size_params->size_min, DEVLINK_ATTR_PAD) ||
2485             nla_put_u8(skb, DEVLINK_ATTR_RESOURCE_UNIT, size_params->unit))
2486                 return -EMSGSIZE;
2487         return 0;
2488 }
2489
2490 static int devlink_resource_occ_put(struct devlink_resource *resource,
2491                                     struct sk_buff *skb)
2492 {
2493         if (!resource->occ_get)
2494                 return 0;
2495         return nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_OCC,
2496                                  resource->occ_get(resource->occ_get_priv),
2497                                  DEVLINK_ATTR_PAD);
2498 }
2499
2500 static int devlink_resource_put(struct devlink *devlink, struct sk_buff *skb,
2501                                 struct devlink_resource *resource)
2502 {
2503         struct devlink_resource *child_resource;
2504         struct nlattr *child_resource_attr;
2505         struct nlattr *resource_attr;
2506
2507         resource_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_RESOURCE);
2508         if (!resource_attr)
2509                 return -EMSGSIZE;
2510
2511         if (nla_put_string(skb, DEVLINK_ATTR_RESOURCE_NAME, resource->name) ||
2512             nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_SIZE, resource->size,
2513                               DEVLINK_ATTR_PAD) ||
2514             nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_ID, resource->id,
2515                               DEVLINK_ATTR_PAD))
2516                 goto nla_put_failure;
2517         if (resource->size != resource->size_new)
2518                 nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_SIZE_NEW,
2519                                   resource->size_new, DEVLINK_ATTR_PAD);
2520         if (devlink_resource_occ_put(resource, skb))
2521                 goto nla_put_failure;
2522         if (devlink_resource_size_params_put(resource, skb))
2523                 goto nla_put_failure;
2524         if (list_empty(&resource->resource_list))
2525                 goto out;
2526
2527         if (nla_put_u8(skb, DEVLINK_ATTR_RESOURCE_SIZE_VALID,
2528                        resource->size_valid))
2529                 goto nla_put_failure;
2530
2531         child_resource_attr = nla_nest_start_noflag(skb,
2532                                                     DEVLINK_ATTR_RESOURCE_LIST);
2533         if (!child_resource_attr)
2534                 goto nla_put_failure;
2535
2536         list_for_each_entry(child_resource, &resource->resource_list, list) {
2537                 if (devlink_resource_put(devlink, skb, child_resource))
2538                         goto resource_put_failure;
2539         }
2540
2541         nla_nest_end(skb, child_resource_attr);
2542 out:
2543         nla_nest_end(skb, resource_attr);
2544         return 0;
2545
2546 resource_put_failure:
2547         nla_nest_cancel(skb, child_resource_attr);
2548 nla_put_failure:
2549         nla_nest_cancel(skb, resource_attr);
2550         return -EMSGSIZE;
2551 }
2552
2553 static int devlink_resource_fill(struct genl_info *info,
2554                                  enum devlink_command cmd, int flags)
2555 {
2556         struct devlink *devlink = info->user_ptr[0];
2557         struct devlink_resource *resource;
2558         struct nlattr *resources_attr;
2559         struct sk_buff *skb = NULL;
2560         struct nlmsghdr *nlh;
2561         bool incomplete;
2562         void *hdr;
2563         int i;
2564         int err;
2565
2566         resource = list_first_entry(&devlink->resource_list,
2567                                     struct devlink_resource, list);
2568 start_again:
2569         err = devlink_dpipe_send_and_alloc_skb(&skb, info);
2570         if (err)
2571                 return err;
2572
2573         hdr = genlmsg_put(skb, info->snd_portid, info->snd_seq,
2574                           &devlink_nl_family, NLM_F_MULTI, cmd);
2575         if (!hdr) {
2576                 nlmsg_free(skb);
2577                 return -EMSGSIZE;
2578         }
2579
2580         if (devlink_nl_put_handle(skb, devlink))
2581                 goto nla_put_failure;
2582
2583         resources_attr = nla_nest_start_noflag(skb,
2584                                                DEVLINK_ATTR_RESOURCE_LIST);
2585         if (!resources_attr)
2586                 goto nla_put_failure;
2587
2588         incomplete = false;
2589         i = 0;
2590         list_for_each_entry_from(resource, &devlink->resource_list, list) {
2591                 err = devlink_resource_put(devlink, skb, resource);
2592                 if (err) {
2593                         if (!i)
2594                                 goto err_resource_put;
2595                         incomplete = true;
2596                         break;
2597                 }
2598                 i++;
2599         }
2600         nla_nest_end(skb, resources_attr);
2601         genlmsg_end(skb, hdr);
2602         if (incomplete)
2603                 goto start_again;
2604 send_done:
2605         nlh = nlmsg_put(skb, info->snd_portid, info->snd_seq,
2606                         NLMSG_DONE, 0, flags | NLM_F_MULTI);
2607         if (!nlh) {
2608                 err = devlink_dpipe_send_and_alloc_skb(&skb, info);
2609                 if (err)
2610                         return err;
2611                 goto send_done;
2612         }
2613         return genlmsg_reply(skb, info);
2614
2615 nla_put_failure:
2616         err = -EMSGSIZE;
2617 err_resource_put:
2618         nlmsg_free(skb);
2619         return err;
2620 }
2621
2622 static int devlink_nl_cmd_resource_dump(struct sk_buff *skb,
2623                                         struct genl_info *info)
2624 {
2625         struct devlink *devlink = info->user_ptr[0];
2626
2627         if (list_empty(&devlink->resource_list))
2628                 return -EOPNOTSUPP;
2629
2630         return devlink_resource_fill(info, DEVLINK_CMD_RESOURCE_DUMP, 0);
2631 }
2632
2633 static int
2634 devlink_resources_validate(struct devlink *devlink,
2635                            struct devlink_resource *resource,
2636                            struct genl_info *info)
2637 {
2638         struct list_head *resource_list;
2639         int err = 0;
2640
2641         if (resource)
2642                 resource_list = &resource->resource_list;
2643         else
2644                 resource_list = &devlink->resource_list;
2645
2646         list_for_each_entry(resource, resource_list, list) {
2647                 if (!resource->size_valid)
2648                         return -EINVAL;
2649                 err = devlink_resources_validate(devlink, resource, info);
2650                 if (err)
2651                         return err;
2652         }
2653         return err;
2654 }
2655
2656 static int devlink_nl_cmd_reload(struct sk_buff *skb, struct genl_info *info)
2657 {
2658         struct devlink *devlink = info->user_ptr[0];
2659         int err;
2660
2661         if (!devlink->ops->reload)
2662                 return -EOPNOTSUPP;
2663
2664         err = devlink_resources_validate(devlink, NULL, info);
2665         if (err) {
2666                 NL_SET_ERR_MSG_MOD(info->extack, "resources size validation failed");
2667                 return err;
2668         }
2669         return devlink->ops->reload(devlink, info->extack);
2670 }
2671
2672 static int devlink_nl_flash_update_fill(struct sk_buff *msg,
2673                                         struct devlink *devlink,
2674                                         enum devlink_command cmd,
2675                                         const char *status_msg,
2676                                         const char *component,
2677                                         unsigned long done, unsigned long total)
2678 {
2679         void *hdr;
2680
2681         hdr = genlmsg_put(msg, 0, 0, &devlink_nl_family, 0, cmd);
2682         if (!hdr)
2683                 return -EMSGSIZE;
2684
2685         if (devlink_nl_put_handle(msg, devlink))
2686                 goto nla_put_failure;
2687
2688         if (cmd != DEVLINK_CMD_FLASH_UPDATE_STATUS)
2689                 goto out;
2690
2691         if (status_msg &&
2692             nla_put_string(msg, DEVLINK_ATTR_FLASH_UPDATE_STATUS_MSG,
2693                            status_msg))
2694                 goto nla_put_failure;
2695         if (component &&
2696             nla_put_string(msg, DEVLINK_ATTR_FLASH_UPDATE_COMPONENT,
2697                            component))
2698                 goto nla_put_failure;
2699         if (nla_put_u64_64bit(msg, DEVLINK_ATTR_FLASH_UPDATE_STATUS_DONE,
2700                               done, DEVLINK_ATTR_PAD))
2701                 goto nla_put_failure;
2702         if (nla_put_u64_64bit(msg, DEVLINK_ATTR_FLASH_UPDATE_STATUS_TOTAL,
2703                               total, DEVLINK_ATTR_PAD))
2704                 goto nla_put_failure;
2705
2706 out:
2707         genlmsg_end(msg, hdr);
2708         return 0;
2709
2710 nla_put_failure:
2711         genlmsg_cancel(msg, hdr);
2712         return -EMSGSIZE;
2713 }
2714
2715 static void __devlink_flash_update_notify(struct devlink *devlink,
2716                                           enum devlink_command cmd,
2717                                           const char *status_msg,
2718                                           const char *component,
2719                                           unsigned long done,
2720                                           unsigned long total)
2721 {
2722         struct sk_buff *msg;
2723         int err;
2724
2725         WARN_ON(cmd != DEVLINK_CMD_FLASH_UPDATE &&
2726                 cmd != DEVLINK_CMD_FLASH_UPDATE_END &&
2727                 cmd != DEVLINK_CMD_FLASH_UPDATE_STATUS);
2728
2729         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2730         if (!msg)
2731                 return;
2732
2733         err = devlink_nl_flash_update_fill(msg, devlink, cmd, status_msg,
2734                                            component, done, total);
2735         if (err)
2736                 goto out_free_msg;
2737
2738         genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
2739                                 msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
2740         return;
2741
2742 out_free_msg:
2743         nlmsg_free(msg);
2744 }
2745
2746 void devlink_flash_update_begin_notify(struct devlink *devlink)
2747 {
2748         __devlink_flash_update_notify(devlink,
2749                                       DEVLINK_CMD_FLASH_UPDATE,
2750                                       NULL, NULL, 0, 0);
2751 }
2752 EXPORT_SYMBOL_GPL(devlink_flash_update_begin_notify);
2753
2754 void devlink_flash_update_end_notify(struct devlink *devlink)
2755 {
2756         __devlink_flash_update_notify(devlink,
2757                                       DEVLINK_CMD_FLASH_UPDATE_END,
2758                                       NULL, NULL, 0, 0);
2759 }
2760 EXPORT_SYMBOL_GPL(devlink_flash_update_end_notify);
2761
2762 void devlink_flash_update_status_notify(struct devlink *devlink,
2763                                         const char *status_msg,
2764                                         const char *component,
2765                                         unsigned long done,
2766                                         unsigned long total)
2767 {
2768         __devlink_flash_update_notify(devlink,
2769                                       DEVLINK_CMD_FLASH_UPDATE_STATUS,
2770                                       status_msg, component, done, total);
2771 }
2772 EXPORT_SYMBOL_GPL(devlink_flash_update_status_notify);
2773
2774 static int devlink_nl_cmd_flash_update(struct sk_buff *skb,
2775                                        struct genl_info *info)
2776 {
2777         struct devlink *devlink = info->user_ptr[0];
2778         const char *file_name, *component;
2779         struct nlattr *nla_component;
2780
2781         if (!devlink->ops->flash_update)
2782                 return -EOPNOTSUPP;
2783
2784         if (!info->attrs[DEVLINK_ATTR_FLASH_UPDATE_FILE_NAME])
2785                 return -EINVAL;
2786         file_name = nla_data(info->attrs[DEVLINK_ATTR_FLASH_UPDATE_FILE_NAME]);
2787
2788         nla_component = info->attrs[DEVLINK_ATTR_FLASH_UPDATE_COMPONENT];
2789         component = nla_component ? nla_data(nla_component) : NULL;
2790
2791         return devlink->ops->flash_update(devlink, file_name, component,
2792                                           info->extack);
2793 }
2794
2795 static const struct devlink_param devlink_param_generic[] = {
2796         {
2797                 .id = DEVLINK_PARAM_GENERIC_ID_INT_ERR_RESET,
2798                 .name = DEVLINK_PARAM_GENERIC_INT_ERR_RESET_NAME,
2799                 .type = DEVLINK_PARAM_GENERIC_INT_ERR_RESET_TYPE,
2800         },
2801         {
2802                 .id = DEVLINK_PARAM_GENERIC_ID_MAX_MACS,
2803                 .name = DEVLINK_PARAM_GENERIC_MAX_MACS_NAME,
2804                 .type = DEVLINK_PARAM_GENERIC_MAX_MACS_TYPE,
2805         },
2806         {
2807                 .id = DEVLINK_PARAM_GENERIC_ID_ENABLE_SRIOV,
2808                 .name = DEVLINK_PARAM_GENERIC_ENABLE_SRIOV_NAME,
2809                 .type = DEVLINK_PARAM_GENERIC_ENABLE_SRIOV_TYPE,
2810         },
2811         {
2812                 .id = DEVLINK_PARAM_GENERIC_ID_REGION_SNAPSHOT,
2813                 .name = DEVLINK_PARAM_GENERIC_REGION_SNAPSHOT_NAME,
2814                 .type = DEVLINK_PARAM_GENERIC_REGION_SNAPSHOT_TYPE,
2815         },
2816         {
2817                 .id = DEVLINK_PARAM_GENERIC_ID_IGNORE_ARI,
2818                 .name = DEVLINK_PARAM_GENERIC_IGNORE_ARI_NAME,
2819                 .type = DEVLINK_PARAM_GENERIC_IGNORE_ARI_TYPE,
2820         },
2821         {
2822                 .id = DEVLINK_PARAM_GENERIC_ID_MSIX_VEC_PER_PF_MAX,
2823                 .name = DEVLINK_PARAM_GENERIC_MSIX_VEC_PER_PF_MAX_NAME,
2824                 .type = DEVLINK_PARAM_GENERIC_MSIX_VEC_PER_PF_MAX_TYPE,
2825         },
2826         {
2827                 .id = DEVLINK_PARAM_GENERIC_ID_MSIX_VEC_PER_PF_MIN,
2828                 .name = DEVLINK_PARAM_GENERIC_MSIX_VEC_PER_PF_MIN_NAME,
2829                 .type = DEVLINK_PARAM_GENERIC_MSIX_VEC_PER_PF_MIN_TYPE,
2830         },
2831         {
2832                 .id = DEVLINK_PARAM_GENERIC_ID_FW_LOAD_POLICY,
2833                 .name = DEVLINK_PARAM_GENERIC_FW_LOAD_POLICY_NAME,
2834                 .type = DEVLINK_PARAM_GENERIC_FW_LOAD_POLICY_TYPE,
2835         },
2836 };
2837
2838 static int devlink_param_generic_verify(const struct devlink_param *param)
2839 {
2840         /* verify it match generic parameter by id and name */
2841         if (param->id > DEVLINK_PARAM_GENERIC_ID_MAX)
2842                 return -EINVAL;
2843         if (strcmp(param->name, devlink_param_generic[param->id].name))
2844                 return -ENOENT;
2845
2846         WARN_ON(param->type != devlink_param_generic[param->id].type);
2847
2848         return 0;
2849 }
2850
2851 static int devlink_param_driver_verify(const struct devlink_param *param)
2852 {
2853         int i;
2854
2855         if (param->id <= DEVLINK_PARAM_GENERIC_ID_MAX)
2856                 return -EINVAL;
2857         /* verify no such name in generic params */
2858         for (i = 0; i <= DEVLINK_PARAM_GENERIC_ID_MAX; i++)
2859                 if (!strcmp(param->name, devlink_param_generic[i].name))
2860                         return -EEXIST;
2861
2862         return 0;
2863 }
2864
2865 static struct devlink_param_item *
2866 devlink_param_find_by_name(struct list_head *param_list,
2867                            const char *param_name)
2868 {
2869         struct devlink_param_item *param_item;
2870
2871         list_for_each_entry(param_item, param_list, list)
2872                 if (!strcmp(param_item->param->name, param_name))
2873                         return param_item;
2874         return NULL;
2875 }
2876
2877 static struct devlink_param_item *
2878 devlink_param_find_by_id(struct list_head *param_list, u32 param_id)
2879 {
2880         struct devlink_param_item *param_item;
2881
2882         list_for_each_entry(param_item, param_list, list)
2883                 if (param_item->param->id == param_id)
2884                         return param_item;
2885         return NULL;
2886 }
2887
2888 static bool
2889 devlink_param_cmode_is_supported(const struct devlink_param *param,
2890                                  enum devlink_param_cmode cmode)
2891 {
2892         return test_bit(cmode, &param->supported_cmodes);
2893 }
2894
2895 static int devlink_param_get(struct devlink *devlink,
2896                              const struct devlink_param *param,
2897                              struct devlink_param_gset_ctx *ctx)
2898 {
2899         if (!param->get)
2900                 return -EOPNOTSUPP;
2901         return param->get(devlink, param->id, ctx);
2902 }
2903
2904 static int devlink_param_set(struct devlink *devlink,
2905                              const struct devlink_param *param,
2906                              struct devlink_param_gset_ctx *ctx)
2907 {
2908         if (!param->set)
2909                 return -EOPNOTSUPP;
2910         return param->set(devlink, param->id, ctx);
2911 }
2912
2913 static int
2914 devlink_param_type_to_nla_type(enum devlink_param_type param_type)
2915 {
2916         switch (param_type) {
2917         case DEVLINK_PARAM_TYPE_U8:
2918                 return NLA_U8;
2919         case DEVLINK_PARAM_TYPE_U16:
2920                 return NLA_U16;
2921         case DEVLINK_PARAM_TYPE_U32:
2922                 return NLA_U32;
2923         case DEVLINK_PARAM_TYPE_STRING:
2924                 return NLA_STRING;
2925         case DEVLINK_PARAM_TYPE_BOOL:
2926                 return NLA_FLAG;
2927         default:
2928                 return -EINVAL;
2929         }
2930 }
2931
2932 static int
2933 devlink_nl_param_value_fill_one(struct sk_buff *msg,
2934                                 enum devlink_param_type type,
2935                                 enum devlink_param_cmode cmode,
2936                                 union devlink_param_value val)
2937 {
2938         struct nlattr *param_value_attr;
2939
2940         param_value_attr = nla_nest_start_noflag(msg,
2941                                                  DEVLINK_ATTR_PARAM_VALUE);
2942         if (!param_value_attr)
2943                 goto nla_put_failure;
2944
2945         if (nla_put_u8(msg, DEVLINK_ATTR_PARAM_VALUE_CMODE, cmode))
2946                 goto value_nest_cancel;
2947
2948         switch (type) {
2949         case DEVLINK_PARAM_TYPE_U8:
2950                 if (nla_put_u8(msg, DEVLINK_ATTR_PARAM_VALUE_DATA, val.vu8))
2951                         goto value_nest_cancel;
2952                 break;
2953         case DEVLINK_PARAM_TYPE_U16:
2954                 if (nla_put_u16(msg, DEVLINK_ATTR_PARAM_VALUE_DATA, val.vu16))
2955                         goto value_nest_cancel;
2956                 break;
2957         case DEVLINK_PARAM_TYPE_U32:
2958                 if (nla_put_u32(msg, DEVLINK_ATTR_PARAM_VALUE_DATA, val.vu32))
2959                         goto value_nest_cancel;
2960                 break;
2961         case DEVLINK_PARAM_TYPE_STRING:
2962                 if (nla_put_string(msg, DEVLINK_ATTR_PARAM_VALUE_DATA,
2963                                    val.vstr))
2964                         goto value_nest_cancel;
2965                 break;
2966         case DEVLINK_PARAM_TYPE_BOOL:
2967                 if (val.vbool &&
2968                     nla_put_flag(msg, DEVLINK_ATTR_PARAM_VALUE_DATA))
2969                         goto value_nest_cancel;
2970                 break;
2971         }
2972
2973         nla_nest_end(msg, param_value_attr);
2974         return 0;
2975
2976 value_nest_cancel:
2977         nla_nest_cancel(msg, param_value_attr);
2978 nla_put_failure:
2979         return -EMSGSIZE;
2980 }
2981
2982 static int devlink_nl_param_fill(struct sk_buff *msg, struct devlink *devlink,
2983                                  unsigned int port_index,
2984                                  struct devlink_param_item *param_item,
2985                                  enum devlink_command cmd,
2986                                  u32 portid, u32 seq, int flags)
2987 {
2988         union devlink_param_value param_value[DEVLINK_PARAM_CMODE_MAX + 1];
2989         bool param_value_set[DEVLINK_PARAM_CMODE_MAX + 1] = {};
2990         const struct devlink_param *param = param_item->param;
2991         struct devlink_param_gset_ctx ctx;
2992         struct nlattr *param_values_list;
2993         struct nlattr *param_attr;
2994         int nla_type;
2995         void *hdr;
2996         int err;
2997         int i;
2998
2999         /* Get value from driver part to driverinit configuration mode */
3000         for (i = 0; i <= DEVLINK_PARAM_CMODE_MAX; i++) {
3001                 if (!devlink_param_cmode_is_supported(param, i))
3002                         continue;
3003                 if (i == DEVLINK_PARAM_CMODE_DRIVERINIT) {
3004                         if (!param_item->driverinit_value_valid)
3005                                 return -EOPNOTSUPP;
3006                         param_value[i] = param_item->driverinit_value;
3007                 } else {
3008                         if (!param_item->published)
3009                                 continue;
3010                         ctx.cmode = i;
3011                         err = devlink_param_get(devlink, param, &ctx);
3012                         if (err)
3013                                 return err;
3014                         param_value[i] = ctx.val;
3015                 }
3016                 param_value_set[i] = true;
3017         }
3018
3019         hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
3020         if (!hdr)
3021                 return -EMSGSIZE;
3022
3023         if (devlink_nl_put_handle(msg, devlink))
3024                 goto genlmsg_cancel;
3025
3026         if (cmd == DEVLINK_CMD_PORT_PARAM_GET ||
3027             cmd == DEVLINK_CMD_PORT_PARAM_NEW ||
3028             cmd == DEVLINK_CMD_PORT_PARAM_DEL)
3029                 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, port_index))
3030                         goto genlmsg_cancel;
3031
3032         param_attr = nla_nest_start_noflag(msg, DEVLINK_ATTR_PARAM);
3033         if (!param_attr)
3034                 goto genlmsg_cancel;
3035         if (nla_put_string(msg, DEVLINK_ATTR_PARAM_NAME, param->name))
3036                 goto param_nest_cancel;
3037         if (param->generic && nla_put_flag(msg, DEVLINK_ATTR_PARAM_GENERIC))
3038                 goto param_nest_cancel;
3039
3040         nla_type = devlink_param_type_to_nla_type(param->type);
3041         if (nla_type < 0)
3042                 goto param_nest_cancel;
3043         if (nla_put_u8(msg, DEVLINK_ATTR_PARAM_TYPE, nla_type))
3044                 goto param_nest_cancel;
3045
3046         param_values_list = nla_nest_start_noflag(msg,
3047                                                   DEVLINK_ATTR_PARAM_VALUES_LIST);
3048         if (!param_values_list)
3049                 goto param_nest_cancel;
3050
3051         for (i = 0; i <= DEVLINK_PARAM_CMODE_MAX; i++) {
3052                 if (!param_value_set[i])
3053                         continue;
3054                 err = devlink_nl_param_value_fill_one(msg, param->type,
3055                                                       i, param_value[i]);
3056                 if (err)
3057                         goto values_list_nest_cancel;
3058         }
3059
3060         nla_nest_end(msg, param_values_list);
3061         nla_nest_end(msg, param_attr);
3062         genlmsg_end(msg, hdr);
3063         return 0;
3064
3065 values_list_nest_cancel:
3066         nla_nest_end(msg, param_values_list);
3067 param_nest_cancel:
3068         nla_nest_cancel(msg, param_attr);
3069 genlmsg_cancel:
3070         genlmsg_cancel(msg, hdr);
3071         return -EMSGSIZE;
3072 }
3073
3074 static void devlink_param_notify(struct devlink *devlink,
3075                                  unsigned int port_index,
3076                                  struct devlink_param_item *param_item,
3077                                  enum devlink_command cmd)
3078 {
3079         struct sk_buff *msg;
3080         int err;
3081
3082         WARN_ON(cmd != DEVLINK_CMD_PARAM_NEW && cmd != DEVLINK_CMD_PARAM_DEL &&
3083                 cmd != DEVLINK_CMD_PORT_PARAM_NEW &&
3084                 cmd != DEVLINK_CMD_PORT_PARAM_DEL);
3085
3086         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
3087         if (!msg)
3088                 return;
3089         err = devlink_nl_param_fill(msg, devlink, port_index, param_item, cmd,
3090                                     0, 0, 0);
3091         if (err) {
3092                 nlmsg_free(msg);
3093                 return;
3094         }
3095
3096         genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
3097                                 msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
3098 }
3099
3100 static int devlink_nl_cmd_param_get_dumpit(struct sk_buff *msg,
3101                                            struct netlink_callback *cb)
3102 {
3103         struct devlink_param_item *param_item;
3104         struct devlink *devlink;
3105         int start = cb->args[0];
3106         int idx = 0;
3107         int err;
3108
3109         mutex_lock(&devlink_mutex);
3110         list_for_each_entry(devlink, &devlink_list, list) {
3111                 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
3112                         continue;
3113                 mutex_lock(&devlink->lock);
3114                 list_for_each_entry(param_item, &devlink->param_list, list) {
3115                         if (idx < start) {
3116                                 idx++;
3117                                 continue;
3118                         }
3119                         err = devlink_nl_param_fill(msg, devlink, 0, param_item,
3120                                                     DEVLINK_CMD_PARAM_GET,
3121                                                     NETLINK_CB(cb->skb).portid,
3122                                                     cb->nlh->nlmsg_seq,
3123                                                     NLM_F_MULTI);
3124                         if (err) {
3125                                 mutex_unlock(&devlink->lock);
3126                                 goto out;
3127                         }
3128                         idx++;
3129                 }
3130                 mutex_unlock(&devlink->lock);
3131         }
3132 out:
3133         mutex_unlock(&devlink_mutex);
3134
3135         cb->args[0] = idx;
3136         return msg->len;
3137 }
3138
3139 static int
3140 devlink_param_type_get_from_info(struct genl_info *info,
3141                                  enum devlink_param_type *param_type)
3142 {
3143         if (!info->attrs[DEVLINK_ATTR_PARAM_TYPE])
3144                 return -EINVAL;
3145
3146         switch (nla_get_u8(info->attrs[DEVLINK_ATTR_PARAM_TYPE])) {
3147         case NLA_U8:
3148                 *param_type = DEVLINK_PARAM_TYPE_U8;
3149                 break;
3150         case NLA_U16:
3151                 *param_type = DEVLINK_PARAM_TYPE_U16;
3152                 break;
3153         case NLA_U32:
3154                 *param_type = DEVLINK_PARAM_TYPE_U32;
3155                 break;
3156         case NLA_STRING:
3157                 *param_type = DEVLINK_PARAM_TYPE_STRING;
3158                 break;
3159         case NLA_FLAG:
3160                 *param_type = DEVLINK_PARAM_TYPE_BOOL;
3161                 break;
3162         default:
3163                 return -EINVAL;
3164         }
3165
3166         return 0;
3167 }
3168
3169 static int
3170 devlink_param_value_get_from_info(const struct devlink_param *param,
3171                                   struct genl_info *info,
3172                                   union devlink_param_value *value)
3173 {
3174         int len;
3175
3176         if (param->type != DEVLINK_PARAM_TYPE_BOOL &&
3177             !info->attrs[DEVLINK_ATTR_PARAM_VALUE_DATA])
3178                 return -EINVAL;
3179
3180         switch (param->type) {
3181         case DEVLINK_PARAM_TYPE_U8:
3182                 value->vu8 = nla_get_u8(info->attrs[DEVLINK_ATTR_PARAM_VALUE_DATA]);
3183                 break;
3184         case DEVLINK_PARAM_TYPE_U16:
3185                 value->vu16 = nla_get_u16(info->attrs[DEVLINK_ATTR_PARAM_VALUE_DATA]);
3186                 break;
3187         case DEVLINK_PARAM_TYPE_U32:
3188                 value->vu32 = nla_get_u32(info->attrs[DEVLINK_ATTR_PARAM_VALUE_DATA]);
3189                 break;
3190         case DEVLINK_PARAM_TYPE_STRING:
3191                 len = strnlen(nla_data(info->attrs[DEVLINK_ATTR_PARAM_VALUE_DATA]),
3192                               nla_len(info->attrs[DEVLINK_ATTR_PARAM_VALUE_DATA]));
3193                 if (len == nla_len(info->attrs[DEVLINK_ATTR_PARAM_VALUE_DATA]) ||
3194                     len >= __DEVLINK_PARAM_MAX_STRING_VALUE)
3195                         return -EINVAL;
3196                 strcpy(value->vstr,
3197                        nla_data(info->attrs[DEVLINK_ATTR_PARAM_VALUE_DATA]));
3198                 break;
3199         case DEVLINK_PARAM_TYPE_BOOL:
3200                 value->vbool = info->attrs[DEVLINK_ATTR_PARAM_VALUE_DATA] ?
3201                                true : false;
3202                 break;
3203         }
3204         return 0;
3205 }
3206
3207 static struct devlink_param_item *
3208 devlink_param_get_from_info(struct list_head *param_list,
3209                             struct genl_info *info)
3210 {
3211         char *param_name;
3212
3213         if (!info->attrs[DEVLINK_ATTR_PARAM_NAME])
3214                 return NULL;
3215
3216         param_name = nla_data(info->attrs[DEVLINK_ATTR_PARAM_NAME]);
3217         return devlink_param_find_by_name(param_list, param_name);
3218 }
3219
3220 static int devlink_nl_cmd_param_get_doit(struct sk_buff *skb,
3221                                          struct genl_info *info)
3222 {
3223         struct devlink *devlink = info->user_ptr[0];
3224         struct devlink_param_item *param_item;
3225         struct sk_buff *msg;
3226         int err;
3227
3228         param_item = devlink_param_get_from_info(&devlink->param_list, info);
3229         if (!param_item)
3230                 return -EINVAL;
3231
3232         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
3233         if (!msg)
3234                 return -ENOMEM;
3235
3236         err = devlink_nl_param_fill(msg, devlink, 0, param_item,
3237                                     DEVLINK_CMD_PARAM_GET,
3238                                     info->snd_portid, info->snd_seq, 0);
3239         if (err) {
3240                 nlmsg_free(msg);
3241                 return err;
3242         }
3243
3244         return genlmsg_reply(msg, info);
3245 }
3246
3247 static int __devlink_nl_cmd_param_set_doit(struct devlink *devlink,
3248                                            unsigned int port_index,
3249                                            struct list_head *param_list,
3250                                            struct genl_info *info,
3251                                            enum devlink_command cmd)
3252 {
3253         enum devlink_param_type param_type;
3254         struct devlink_param_gset_ctx ctx;
3255         enum devlink_param_cmode cmode;
3256         struct devlink_param_item *param_item;
3257         const struct devlink_param *param;
3258         union devlink_param_value value;
3259         int err = 0;
3260
3261         param_item = devlink_param_get_from_info(param_list, info);
3262         if (!param_item)
3263                 return -EINVAL;
3264         param = param_item->param;
3265         err = devlink_param_type_get_from_info(info, &param_type);
3266         if (err)
3267                 return err;
3268         if (param_type != param->type)
3269                 return -EINVAL;
3270         err = devlink_param_value_get_from_info(param, info, &value);
3271         if (err)
3272                 return err;
3273         if (param->validate) {
3274                 err = param->validate(devlink, param->id, value, info->extack);
3275                 if (err)
3276                         return err;
3277         }
3278
3279         if (!info->attrs[DEVLINK_ATTR_PARAM_VALUE_CMODE])
3280                 return -EINVAL;
3281         cmode = nla_get_u8(info->attrs[DEVLINK_ATTR_PARAM_VALUE_CMODE]);
3282         if (!devlink_param_cmode_is_supported(param, cmode))
3283                 return -EOPNOTSUPP;
3284
3285         if (cmode == DEVLINK_PARAM_CMODE_DRIVERINIT) {
3286                 if (param->type == DEVLINK_PARAM_TYPE_STRING)
3287                         strcpy(param_item->driverinit_value.vstr, value.vstr);
3288                 else
3289                         param_item->driverinit_value = value;
3290                 param_item->driverinit_value_valid = true;
3291         } else {
3292                 if (!param->set)
3293                         return -EOPNOTSUPP;
3294                 ctx.val = value;
3295                 ctx.cmode = cmode;
3296                 err = devlink_param_set(devlink, param, &ctx);
3297                 if (err)
3298                         return err;
3299         }
3300
3301         devlink_param_notify(devlink, port_index, param_item, cmd);
3302         return 0;
3303 }
3304
3305 static int devlink_nl_cmd_param_set_doit(struct sk_buff *skb,
3306                                          struct genl_info *info)
3307 {
3308         struct devlink *devlink = info->user_ptr[0];
3309
3310         return __devlink_nl_cmd_param_set_doit(devlink, 0, &devlink->param_list,
3311                                                info, DEVLINK_CMD_PARAM_NEW);
3312 }
3313
3314 static int devlink_param_register_one(struct devlink *devlink,
3315                                       unsigned int port_index,
3316                                       struct list_head *param_list,
3317                                       const struct devlink_param *param,
3318                                       enum devlink_command cmd)
3319 {
3320         struct devlink_param_item *param_item;
3321
3322         if (devlink_param_find_by_name(param_list, param->name))
3323                 return -EEXIST;
3324
3325         if (param->supported_cmodes == BIT(DEVLINK_PARAM_CMODE_DRIVERINIT))
3326                 WARN_ON(param->get || param->set);
3327         else
3328                 WARN_ON(!param->get || !param->set);
3329
3330         param_item = kzalloc(sizeof(*param_item), GFP_KERNEL);
3331         if (!param_item)
3332                 return -ENOMEM;
3333         param_item->param = param;
3334
3335         list_add_tail(&param_item->list, param_list);
3336         devlink_param_notify(devlink, port_index, param_item, cmd);
3337         return 0;
3338 }
3339
3340 static void devlink_param_unregister_one(struct devlink *devlink,
3341                                          unsigned int port_index,
3342                                          struct list_head *param_list,
3343                                          const struct devlink_param *param,
3344                                          enum devlink_command cmd)
3345 {
3346         struct devlink_param_item *param_item;
3347
3348         param_item = devlink_param_find_by_name(param_list, param->name);
3349         WARN_ON(!param_item);
3350         devlink_param_notify(devlink, port_index, param_item, cmd);
3351         list_del(&param_item->list);
3352         kfree(param_item);
3353 }
3354
3355 static int devlink_nl_cmd_port_param_get_dumpit(struct sk_buff *msg,
3356                                                 struct netlink_callback *cb)
3357 {
3358         struct devlink_param_item *param_item;
3359         struct devlink_port *devlink_port;
3360         struct devlink *devlink;
3361         int start = cb->args[0];
3362         int idx = 0;
3363         int err;
3364
3365         mutex_lock(&devlink_mutex);
3366         list_for_each_entry(devlink, &devlink_list, list) {
3367                 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
3368                         continue;
3369                 mutex_lock(&devlink->lock);
3370                 list_for_each_entry(devlink_port, &devlink->port_list, list) {
3371                         list_for_each_entry(param_item,
3372                                             &devlink_port->param_list, list) {
3373                                 if (idx < start) {
3374                                         idx++;
3375                                         continue;
3376                                 }
3377                                 err = devlink_nl_param_fill(msg,
3378                                                 devlink_port->devlink,
3379                                                 devlink_port->index, param_item,
3380                                                 DEVLINK_CMD_PORT_PARAM_GET,
3381                                                 NETLINK_CB(cb->skb).portid,
3382                                                 cb->nlh->nlmsg_seq,
3383                                                 NLM_F_MULTI);
3384                                 if (err) {
3385                                         mutex_unlock(&devlink->lock);
3386                                         goto out;
3387                                 }
3388                                 idx++;
3389                         }
3390                 }
3391                 mutex_unlock(&devlink->lock);
3392         }
3393 out:
3394         mutex_unlock(&devlink_mutex);
3395
3396         cb->args[0] = idx;
3397         return msg->len;
3398 }
3399
3400 static int devlink_nl_cmd_port_param_get_doit(struct sk_buff *skb,
3401                                               struct genl_info *info)
3402 {
3403         struct devlink_port *devlink_port = info->user_ptr[0];
3404         struct devlink_param_item *param_item;
3405         struct sk_buff *msg;
3406         int err;
3407
3408         param_item = devlink_param_get_from_info(&devlink_port->param_list,
3409                                                  info);
3410         if (!param_item)
3411                 return -EINVAL;
3412
3413         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
3414         if (!msg)
3415                 return -ENOMEM;
3416
3417         err = devlink_nl_param_fill(msg, devlink_port->devlink,
3418                                     devlink_port->index, param_item,
3419                                     DEVLINK_CMD_PORT_PARAM_GET,
3420                                     info->snd_portid, info->snd_seq, 0);
3421         if (err) {
3422                 nlmsg_free(msg);
3423                 return err;
3424         }
3425
3426         return genlmsg_reply(msg, info);
3427 }
3428
3429 static int devlink_nl_cmd_port_param_set_doit(struct sk_buff *skb,
3430                                               struct genl_info *info)
3431 {
3432         struct devlink_port *devlink_port = info->user_ptr[0];
3433
3434         return __devlink_nl_cmd_param_set_doit(devlink_port->devlink,
3435                                                devlink_port->index,
3436                                                &devlink_port->param_list, info,
3437                                                DEVLINK_CMD_PORT_PARAM_NEW);
3438 }
3439
3440 static int devlink_nl_region_snapshot_id_put(struct sk_buff *msg,
3441                                              struct devlink *devlink,
3442                                              struct devlink_snapshot *snapshot)
3443 {
3444         struct nlattr *snap_attr;
3445         int err;
3446
3447         snap_attr = nla_nest_start_noflag(msg, DEVLINK_ATTR_REGION_SNAPSHOT);
3448         if (!snap_attr)
3449                 return -EINVAL;
3450
3451         err = nla_put_u32(msg, DEVLINK_ATTR_REGION_SNAPSHOT_ID, snapshot->id);
3452         if (err)
3453                 goto nla_put_failure;
3454
3455         nla_nest_end(msg, snap_attr);
3456         return 0;
3457
3458 nla_put_failure:
3459         nla_nest_cancel(msg, snap_attr);
3460         return err;
3461 }
3462
3463 static int devlink_nl_region_snapshots_id_put(struct sk_buff *msg,
3464                                               struct devlink *devlink,
3465                                               struct devlink_region *region)
3466 {
3467         struct devlink_snapshot *snapshot;
3468         struct nlattr *snapshots_attr;
3469         int err;
3470
3471         snapshots_attr = nla_nest_start_noflag(msg,
3472                                                DEVLINK_ATTR_REGION_SNAPSHOTS);
3473         if (!snapshots_attr)
3474                 return -EINVAL;
3475
3476         list_for_each_entry(snapshot, &region->snapshot_list, list) {
3477                 err = devlink_nl_region_snapshot_id_put(msg, devlink, snapshot);
3478                 if (err)
3479                         goto nla_put_failure;
3480         }
3481
3482         nla_nest_end(msg, snapshots_attr);
3483         return 0;
3484
3485 nla_put_failure:
3486         nla_nest_cancel(msg, snapshots_attr);
3487         return err;
3488 }
3489
3490 static int devlink_nl_region_fill(struct sk_buff *msg, struct devlink *devlink,
3491                                   enum devlink_command cmd, u32 portid,
3492                                   u32 seq, int flags,
3493                                   struct devlink_region *region)
3494 {
3495         void *hdr;
3496         int err;
3497
3498         hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
3499         if (!hdr)
3500                 return -EMSGSIZE;
3501
3502         err = devlink_nl_put_handle(msg, devlink);
3503         if (err)
3504                 goto nla_put_failure;
3505
3506         err = nla_put_string(msg, DEVLINK_ATTR_REGION_NAME, region->name);
3507         if (err)
3508                 goto nla_put_failure;
3509
3510         err = nla_put_u64_64bit(msg, DEVLINK_ATTR_REGION_SIZE,
3511                                 region->size,
3512                                 DEVLINK_ATTR_PAD);
3513         if (err)
3514                 goto nla_put_failure;
3515
3516         err = devlink_nl_region_snapshots_id_put(msg, devlink, region);
3517         if (err)
3518                 goto nla_put_failure;
3519
3520         genlmsg_end(msg, hdr);
3521         return 0;
3522
3523 nla_put_failure:
3524         genlmsg_cancel(msg, hdr);
3525         return err;
3526 }
3527
3528 static void devlink_nl_region_notify(struct devlink_region *region,
3529                                      struct devlink_snapshot *snapshot,
3530                                      enum devlink_command cmd)
3531 {
3532         struct devlink *devlink = region->devlink;
3533         struct sk_buff *msg;
3534         void *hdr;
3535         int err;
3536
3537         WARN_ON(cmd != DEVLINK_CMD_REGION_NEW && cmd != DEVLINK_CMD_REGION_DEL);
3538
3539         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
3540         if (!msg)
3541                 return;
3542
3543         hdr = genlmsg_put(msg, 0, 0, &devlink_nl_family, 0, cmd);
3544         if (!hdr)
3545                 goto out_free_msg;
3546
3547         err = devlink_nl_put_handle(msg, devlink);
3548         if (err)
3549                 goto out_cancel_msg;
3550
3551         err = nla_put_string(msg, DEVLINK_ATTR_REGION_NAME,
3552                              region->name);
3553         if (err)
3554                 goto out_cancel_msg;
3555
3556         if (snapshot) {
3557                 err = nla_put_u32(msg, DEVLINK_ATTR_REGION_SNAPSHOT_ID,
3558                                   snapshot->id);
3559                 if (err)
3560                         goto out_cancel_msg;
3561         } else {
3562                 err = nla_put_u64_64bit(msg, DEVLINK_ATTR_REGION_SIZE,
3563                                         region->size, DEVLINK_ATTR_PAD);
3564                 if (err)
3565                         goto out_cancel_msg;
3566         }
3567         genlmsg_end(msg, hdr);
3568
3569         genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
3570                                 msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
3571
3572         return;
3573
3574 out_cancel_msg:
3575         genlmsg_cancel(msg, hdr);
3576 out_free_msg:
3577         nlmsg_free(msg);
3578 }
3579
3580 static int devlink_nl_cmd_region_get_doit(struct sk_buff *skb,
3581                                           struct genl_info *info)
3582 {
3583         struct devlink *devlink = info->user_ptr[0];
3584         struct devlink_region *region;
3585         const char *region_name;
3586         struct sk_buff *msg;
3587         int err;
3588
3589         if (!info->attrs[DEVLINK_ATTR_REGION_NAME])
3590                 return -EINVAL;
3591
3592         region_name = nla_data(info->attrs[DEVLINK_ATTR_REGION_NAME]);
3593         region = devlink_region_get_by_name(devlink, region_name);
3594         if (!region)
3595                 return -EINVAL;
3596
3597         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
3598         if (!msg)
3599                 return -ENOMEM;
3600
3601         err = devlink_nl_region_fill(msg, devlink, DEVLINK_CMD_REGION_GET,
3602                                      info->snd_portid, info->snd_seq, 0,
3603                                      region);
3604         if (err) {
3605                 nlmsg_free(msg);
3606                 return err;
3607         }
3608
3609         return genlmsg_reply(msg, info);
3610 }
3611
3612 static int devlink_nl_cmd_region_get_dumpit(struct sk_buff *msg,
3613                                             struct netlink_callback *cb)
3614 {
3615         struct devlink_region *region;
3616         struct devlink *devlink;
3617         int start = cb->args[0];
3618         int idx = 0;
3619         int err;
3620
3621         mutex_lock(&devlink_mutex);
3622         list_for_each_entry(devlink, &devlink_list, list) {
3623                 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
3624                         continue;
3625
3626                 mutex_lock(&devlink->lock);
3627                 list_for_each_entry(region, &devlink->region_list, list) {
3628                         if (idx < start) {
3629                                 idx++;
3630                                 continue;
3631                         }
3632                         err = devlink_nl_region_fill(msg, devlink,
3633                                                      DEVLINK_CMD_REGION_GET,
3634                                                      NETLINK_CB(cb->skb).portid,
3635                                                      cb->nlh->nlmsg_seq,
3636                                                      NLM_F_MULTI, region);
3637                         if (err) {
3638                                 mutex_unlock(&devlink->lock);
3639                                 goto out;
3640                         }
3641                         idx++;
3642                 }
3643                 mutex_unlock(&devlink->lock);
3644         }
3645 out:
3646         mutex_unlock(&devlink_mutex);
3647         cb->args[0] = idx;
3648         return msg->len;
3649 }
3650
3651 static int devlink_nl_cmd_region_del(struct sk_buff *skb,
3652                                      struct genl_info *info)
3653 {
3654         struct devlink *devlink = info->user_ptr[0];
3655         struct devlink_snapshot *snapshot;
3656         struct devlink_region *region;
3657         const char *region_name;
3658         u32 snapshot_id;
3659
3660         if (!info->attrs[DEVLINK_ATTR_REGION_NAME] ||
3661             !info->attrs[DEVLINK_ATTR_REGION_SNAPSHOT_ID])
3662                 return -EINVAL;
3663
3664         region_name = nla_data(info->attrs[DEVLINK_ATTR_REGION_NAME]);
3665         snapshot_id = nla_get_u32(info->attrs[DEVLINK_ATTR_REGION_SNAPSHOT_ID]);
3666
3667         region = devlink_region_get_by_name(devlink, region_name);
3668         if (!region)
3669                 return -EINVAL;
3670
3671         snapshot = devlink_region_snapshot_get_by_id(region, snapshot_id);
3672         if (!snapshot)
3673                 return -EINVAL;
3674
3675         devlink_nl_region_notify(region, snapshot, DEVLINK_CMD_REGION_DEL);
3676         devlink_region_snapshot_del(snapshot);
3677         return 0;
3678 }
3679
3680 static int devlink_nl_cmd_region_read_chunk_fill(struct sk_buff *msg,
3681                                                  struct devlink *devlink,
3682                                                  u8 *chunk, u32 chunk_size,
3683                                                  u64 addr)
3684 {
3685         struct nlattr *chunk_attr;
3686         int err;
3687
3688         chunk_attr = nla_nest_start_noflag(msg, DEVLINK_ATTR_REGION_CHUNK);
3689         if (!chunk_attr)
3690                 return -EINVAL;
3691
3692         err = nla_put(msg, DEVLINK_ATTR_REGION_CHUNK_DATA, chunk_size, chunk);
3693         if (err)
3694                 goto nla_put_failure;
3695
3696         err = nla_put_u64_64bit(msg, DEVLINK_ATTR_REGION_CHUNK_ADDR, addr,
3697                                 DEVLINK_ATTR_PAD);
3698         if (err)
3699                 goto nla_put_failure;
3700
3701         nla_nest_end(msg, chunk_attr);
3702         return 0;
3703
3704 nla_put_failure:
3705         nla_nest_cancel(msg, chunk_attr);
3706         return err;
3707 }
3708
3709 #define DEVLINK_REGION_READ_CHUNK_SIZE 256
3710
3711 static int devlink_nl_region_read_snapshot_fill(struct sk_buff *skb,
3712                                                 struct devlink *devlink,
3713                                                 struct devlink_region *region,
3714                                                 struct nlattr **attrs,
3715                                                 u64 start_offset,
3716                                                 u64 end_offset,
3717                                                 bool dump,
3718                                                 u64 *new_offset)
3719 {
3720         struct devlink_snapshot *snapshot;
3721         u64 curr_offset = start_offset;
3722         u32 snapshot_id;
3723         int err = 0;
3724
3725         *new_offset = start_offset;
3726
3727         snapshot_id = nla_get_u32(attrs[DEVLINK_ATTR_REGION_SNAPSHOT_ID]);
3728         snapshot = devlink_region_snapshot_get_by_id(region, snapshot_id);
3729         if (!snapshot)
3730                 return -EINVAL;
3731
3732         if (end_offset > snapshot->data_len || dump)
3733                 end_offset = snapshot->data_len;
3734
3735         while (curr_offset < end_offset) {
3736                 u32 data_size;
3737                 u8 *data;
3738
3739                 if (end_offset - curr_offset < DEVLINK_REGION_READ_CHUNK_SIZE)
3740                         data_size = end_offset - curr_offset;
3741                 else
3742                         data_size = DEVLINK_REGION_READ_CHUNK_SIZE;
3743
3744                 data = &snapshot->data[curr_offset];
3745                 err = devlink_nl_cmd_region_read_chunk_fill(skb, devlink,
3746                                                             data, data_size,
3747                                                             curr_offset);
3748                 if (err)
3749                         break;
3750
3751                 curr_offset += data_size;
3752         }
3753         *new_offset = curr_offset;
3754
3755         return err;
3756 }
3757
3758 static int devlink_nl_cmd_region_read_dumpit(struct sk_buff *skb,
3759                                              struct netlink_callback *cb)
3760 {
3761         u64 ret_offset, start_offset, end_offset = 0;
3762         struct devlink_region *region;
3763         struct nlattr *chunks_attr;
3764         const char *region_name;
3765         struct devlink *devlink;
3766         struct nlattr **attrs;
3767         bool dump = true;
3768         void *hdr;
3769         int err;
3770
3771         start_offset = *((u64 *)&cb->args[0]);
3772
3773         attrs = kmalloc_array(DEVLINK_ATTR_MAX + 1, sizeof(*attrs), GFP_KERNEL);
3774         if (!attrs)
3775                 return -ENOMEM;
3776
3777         err = nlmsg_parse_deprecated(cb->nlh,
3778                                      GENL_HDRLEN + devlink_nl_family.hdrsize,
3779                                      attrs, DEVLINK_ATTR_MAX,
3780                                      devlink_nl_family.policy, cb->extack);
3781         if (err)
3782                 goto out_free;
3783
3784         mutex_lock(&devlink_mutex);
3785         devlink = devlink_get_from_attrs(sock_net(cb->skb->sk), attrs);
3786         if (IS_ERR(devlink)) {
3787                 err = PTR_ERR(devlink);
3788                 goto out_dev;
3789         }
3790
3791         mutex_lock(&devlink->lock);
3792
3793         if (!attrs[DEVLINK_ATTR_REGION_NAME] ||
3794             !attrs[DEVLINK_ATTR_REGION_SNAPSHOT_ID]) {
3795                 err = -EINVAL;
3796                 goto out_unlock;
3797         }
3798
3799         region_name = nla_data(attrs[DEVLINK_ATTR_REGION_NAME]);
3800         region = devlink_region_get_by_name(devlink, region_name);
3801         if (!region) {
3802                 err = -EINVAL;
3803                 goto out_unlock;
3804         }
3805
3806         hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
3807                           &devlink_nl_family, NLM_F_ACK | NLM_F_MULTI,
3808                           DEVLINK_CMD_REGION_READ);
3809         if (!hdr) {
3810                 err = -EMSGSIZE;
3811                 goto out_unlock;
3812         }
3813
3814         err = devlink_nl_put_handle(skb, devlink);
3815         if (err)
3816                 goto nla_put_failure;
3817
3818         err = nla_put_string(skb, DEVLINK_ATTR_REGION_NAME, region_name);
3819         if (err)
3820                 goto nla_put_failure;
3821
3822         chunks_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_REGION_CHUNKS);
3823         if (!chunks_attr) {
3824                 err = -EMSGSIZE;
3825                 goto nla_put_failure;
3826         }
3827
3828         if (attrs[DEVLINK_ATTR_REGION_CHUNK_ADDR] &&
3829             attrs[DEVLINK_ATTR_REGION_CHUNK_LEN]) {
3830                 if (!start_offset)
3831                         start_offset =
3832                                 nla_get_u64(attrs[DEVLINK_ATTR_REGION_CHUNK_ADDR]);
3833
3834                 end_offset = nla_get_u64(attrs[DEVLINK_ATTR_REGION_CHUNK_ADDR]);
3835                 end_offset += nla_get_u64(attrs[DEVLINK_ATTR_REGION_CHUNK_LEN]);
3836                 dump = false;
3837         }
3838
3839         err = devlink_nl_region_read_snapshot_fill(skb, devlink,
3840                                                    region, attrs,
3841                                                    start_offset,
3842                                                    end_offset, dump,
3843                                                    &ret_offset);
3844
3845         if (err && err != -EMSGSIZE)
3846                 goto nla_put_failure;
3847
3848         /* Check if there was any progress done to prevent infinite loop */
3849         if (ret_offset == start_offset) {
3850                 err = -EINVAL;
3851                 goto nla_put_failure;
3852         }
3853
3854         *((u64 *)&cb->args[0]) = ret_offset;
3855
3856         nla_nest_end(skb, chunks_attr);
3857         genlmsg_end(skb, hdr);
3858         mutex_unlock(&devlink->lock);
3859         mutex_unlock(&devlink_mutex);
3860         kfree(attrs);
3861
3862         return skb->len;
3863
3864 nla_put_failure:
3865         genlmsg_cancel(skb, hdr);
3866 out_unlock:
3867         mutex_unlock(&devlink->lock);
3868 out_dev:
3869         mutex_unlock(&devlink_mutex);
3870 out_free:
3871         kfree(attrs);
3872         return err;
3873 }
3874
3875 struct devlink_info_req {
3876         struct sk_buff *msg;
3877 };
3878
3879 int devlink_info_driver_name_put(struct devlink_info_req *req, const char *name)
3880 {
3881         return nla_put_string(req->msg, DEVLINK_ATTR_INFO_DRIVER_NAME, name);
3882 }
3883 EXPORT_SYMBOL_GPL(devlink_info_driver_name_put);
3884
3885 int devlink_info_serial_number_put(struct devlink_info_req *req, const char *sn)
3886 {
3887         return nla_put_string(req->msg, DEVLINK_ATTR_INFO_SERIAL_NUMBER, sn);
3888 }
3889 EXPORT_SYMBOL_GPL(devlink_info_serial_number_put);
3890
3891 static int devlink_info_version_put(struct devlink_info_req *req, int attr,
3892                                     const char *version_name,
3893                                     const char *version_value)
3894 {
3895         struct nlattr *nest;
3896         int err;
3897
3898         nest = nla_nest_start_noflag(req->msg, attr);
3899         if (!nest)
3900                 return -EMSGSIZE;
3901
3902         err = nla_put_string(req->msg, DEVLINK_ATTR_INFO_VERSION_NAME,
3903                              version_name);
3904         if (err)
3905                 goto nla_put_failure;
3906
3907         err = nla_put_string(req->msg, DEVLINK_ATTR_INFO_VERSION_VALUE,
3908                              version_value);
3909         if (err)
3910                 goto nla_put_failure;
3911
3912         nla_nest_end(req->msg, nest);
3913
3914         return 0;
3915
3916 nla_put_failure:
3917         nla_nest_cancel(req->msg, nest);
3918         return err;
3919 }
3920
3921 int devlink_info_version_fixed_put(struct devlink_info_req *req,
3922                                    const char *version_name,
3923                                    const char *version_value)
3924 {
3925         return devlink_info_version_put(req, DEVLINK_ATTR_INFO_VERSION_FIXED,
3926                                         version_name, version_value);
3927 }
3928 EXPORT_SYMBOL_GPL(devlink_info_version_fixed_put);
3929
3930 int devlink_info_version_stored_put(struct devlink_info_req *req,
3931                                     const char *version_name,
3932                                     const char *version_value)
3933 {
3934         return devlink_info_version_put(req, DEVLINK_ATTR_INFO_VERSION_STORED,
3935                                         version_name, version_value);
3936 }
3937 EXPORT_SYMBOL_GPL(devlink_info_version_stored_put);
3938
3939 int devlink_info_version_running_put(struct devlink_info_req *req,
3940                                      const char *version_name,
3941                                      const char *version_value)
3942 {
3943         return devlink_info_version_put(req, DEVLINK_ATTR_INFO_VERSION_RUNNING,
3944                                         version_name, version_value);
3945 }
3946 EXPORT_SYMBOL_GPL(devlink_info_version_running_put);
3947
3948 static int
3949 devlink_nl_info_fill(struct sk_buff *msg, struct devlink *devlink,
3950                      enum devlink_command cmd, u32 portid,
3951                      u32 seq, int flags, struct netlink_ext_ack *extack)
3952 {
3953         struct devlink_info_req req;
3954         void *hdr;
3955         int err;
3956
3957         hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
3958         if (!hdr)
3959                 return -EMSGSIZE;
3960
3961         err = -EMSGSIZE;
3962         if (devlink_nl_put_handle(msg, devlink))
3963                 goto err_cancel_msg;
3964
3965         req.msg = msg;
3966         err = devlink->ops->info_get(devlink, &req, extack);
3967         if (err)
3968                 goto err_cancel_msg;
3969
3970         genlmsg_end(msg, hdr);
3971         return 0;
3972
3973 err_cancel_msg:
3974         genlmsg_cancel(msg, hdr);
3975         return err;
3976 }
3977
3978 static int devlink_nl_cmd_info_get_doit(struct sk_buff *skb,
3979                                         struct genl_info *info)
3980 {
3981         struct devlink *devlink = info->user_ptr[0];
3982         struct sk_buff *msg;
3983         int err;
3984
3985         if (!devlink->ops->info_get)
3986                 return -EOPNOTSUPP;
3987
3988         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
3989         if (!msg)
3990                 return -ENOMEM;
3991
3992         err = devlink_nl_info_fill(msg, devlink, DEVLINK_CMD_INFO_GET,
3993                                    info->snd_portid, info->snd_seq, 0,
3994                                    info->extack);
3995         if (err) {
3996                 nlmsg_free(msg);
3997                 return err;
3998         }
3999
4000         return genlmsg_reply(msg, info);
4001 }
4002
4003 static int devlink_nl_cmd_info_get_dumpit(struct sk_buff *msg,
4004                                           struct netlink_callback *cb)
4005 {
4006         struct devlink *devlink;
4007         int start = cb->args[0];
4008         int idx = 0;
4009         int err;
4010
4011         mutex_lock(&devlink_mutex);
4012         list_for_each_entry(devlink, &devlink_list, list) {
4013                 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
4014                         continue;
4015                 if (idx < start) {
4016                         idx++;
4017                         continue;
4018                 }
4019
4020                 if (!devlink->ops->info_get) {
4021                         idx++;
4022                         continue;
4023                 }
4024
4025                 mutex_lock(&devlink->lock);
4026                 err = devlink_nl_info_fill(msg, devlink, DEVLINK_CMD_INFO_GET,
4027                                            NETLINK_CB(cb->skb).portid,
4028                                            cb->nlh->nlmsg_seq, NLM_F_MULTI,
4029                                            cb->extack);
4030                 mutex_unlock(&devlink->lock);
4031                 if (err)
4032                         break;
4033                 idx++;
4034         }
4035         mutex_unlock(&devlink_mutex);
4036
4037         cb->args[0] = idx;
4038         return msg->len;
4039 }
4040
4041 struct devlink_fmsg_item {
4042         struct list_head list;
4043         int attrtype;
4044         u8 nla_type;
4045         u16 len;
4046         int value[0];
4047 };
4048
4049 struct devlink_fmsg {
4050         struct list_head item_list;
4051 };
4052
4053 static struct devlink_fmsg *devlink_fmsg_alloc(void)
4054 {
4055         struct devlink_fmsg *fmsg;
4056
4057         fmsg = kzalloc(sizeof(*fmsg), GFP_KERNEL);
4058         if (!fmsg)
4059                 return NULL;
4060
4061         INIT_LIST_HEAD(&fmsg->item_list);
4062
4063         return fmsg;
4064 }
4065
4066 static void devlink_fmsg_free(struct devlink_fmsg *fmsg)
4067 {
4068         struct devlink_fmsg_item *item, *tmp;
4069
4070         list_for_each_entry_safe(item, tmp, &fmsg->item_list, list) {
4071                 list_del(&item->list);
4072                 kfree(item);
4073         }
4074         kfree(fmsg);
4075 }
4076
4077 static int devlink_fmsg_nest_common(struct devlink_fmsg *fmsg,
4078                                     int attrtype)
4079 {
4080         struct devlink_fmsg_item *item;
4081
4082         item = kzalloc(sizeof(*item), GFP_KERNEL);
4083         if (!item)
4084                 return -ENOMEM;
4085
4086         item->attrtype = attrtype;
4087         list_add_tail(&item->list, &fmsg->item_list);
4088
4089         return 0;
4090 }
4091
4092 int devlink_fmsg_obj_nest_start(struct devlink_fmsg *fmsg)
4093 {
4094         return devlink_fmsg_nest_common(fmsg, DEVLINK_ATTR_FMSG_OBJ_NEST_START);
4095 }
4096 EXPORT_SYMBOL_GPL(devlink_fmsg_obj_nest_start);
4097
4098 static int devlink_fmsg_nest_end(struct devlink_fmsg *fmsg)
4099 {
4100         return devlink_fmsg_nest_common(fmsg, DEVLINK_ATTR_FMSG_NEST_END);
4101 }
4102
4103 int devlink_fmsg_obj_nest_end(struct devlink_fmsg *fmsg)
4104 {
4105         return devlink_fmsg_nest_end(fmsg);
4106 }
4107 EXPORT_SYMBOL_GPL(devlink_fmsg_obj_nest_end);
4108
4109 #define DEVLINK_FMSG_MAX_SIZE (GENLMSG_DEFAULT_SIZE - GENL_HDRLEN - NLA_HDRLEN)
4110
4111 static int devlink_fmsg_put_name(struct devlink_fmsg *fmsg, const char *name)
4112 {
4113         struct devlink_fmsg_item *item;
4114
4115         if (strlen(name) + 1 > DEVLINK_FMSG_MAX_SIZE)
4116                 return -EMSGSIZE;
4117
4118         item = kzalloc(sizeof(*item) + strlen(name) + 1, GFP_KERNEL);
4119         if (!item)
4120                 return -ENOMEM;
4121
4122         item->nla_type = NLA_NUL_STRING;
4123         item->len = strlen(name) + 1;
4124         item->attrtype = DEVLINK_ATTR_FMSG_OBJ_NAME;
4125         memcpy(&item->value, name, item->len);
4126         list_add_tail(&item->list, &fmsg->item_list);
4127
4128         return 0;
4129 }
4130
4131 int devlink_fmsg_pair_nest_start(struct devlink_fmsg *fmsg, const char *name)
4132 {
4133         int err;
4134
4135         err = devlink_fmsg_nest_common(fmsg, DEVLINK_ATTR_FMSG_PAIR_NEST_START);
4136         if (err)
4137                 return err;
4138
4139         err = devlink_fmsg_put_name(fmsg, name);
4140         if (err)
4141                 return err;
4142
4143         return 0;
4144 }
4145 EXPORT_SYMBOL_GPL(devlink_fmsg_pair_nest_start);
4146
4147 int devlink_fmsg_pair_nest_end(struct devlink_fmsg *fmsg)
4148 {
4149         return devlink_fmsg_nest_end(fmsg);
4150 }
4151 EXPORT_SYMBOL_GPL(devlink_fmsg_pair_nest_end);
4152
4153 int devlink_fmsg_arr_pair_nest_start(struct devlink_fmsg *fmsg,
4154                                      const char *name)
4155 {
4156         int err;
4157
4158         err = devlink_fmsg_pair_nest_start(fmsg, name);
4159         if (err)
4160                 return err;
4161
4162         err = devlink_fmsg_nest_common(fmsg, DEVLINK_ATTR_FMSG_ARR_NEST_START);
4163         if (err)
4164                 return err;
4165
4166         return 0;
4167 }
4168 EXPORT_SYMBOL_GPL(devlink_fmsg_arr_pair_nest_start);
4169
4170 int devlink_fmsg_arr_pair_nest_end(struct devlink_fmsg *fmsg)
4171 {
4172         int err;
4173
4174         err = devlink_fmsg_nest_end(fmsg);
4175         if (err)
4176                 return err;
4177
4178         err = devlink_fmsg_nest_end(fmsg);
4179         if (err)
4180                 return err;
4181
4182         return 0;
4183 }
4184 EXPORT_SYMBOL_GPL(devlink_fmsg_arr_pair_nest_end);
4185
4186 static int devlink_fmsg_put_value(struct devlink_fmsg *fmsg,
4187                                   const void *value, u16 value_len,
4188                                   u8 value_nla_type)
4189 {
4190         struct devlink_fmsg_item *item;
4191
4192         if (value_len > DEVLINK_FMSG_MAX_SIZE)
4193                 return -EMSGSIZE;
4194
4195         item = kzalloc(sizeof(*item) + value_len, GFP_KERNEL);
4196         if (!item)
4197                 return -ENOMEM;
4198
4199         item->nla_type = value_nla_type;
4200         item->len = value_len;
4201         item->attrtype = DEVLINK_ATTR_FMSG_OBJ_VALUE_DATA;
4202         memcpy(&item->value, value, item->len);
4203         list_add_tail(&item->list, &fmsg->item_list);
4204
4205         return 0;
4206 }
4207
4208 int devlink_fmsg_bool_put(struct devlink_fmsg *fmsg, bool value)
4209 {
4210         return devlink_fmsg_put_value(fmsg, &value, sizeof(value), NLA_FLAG);
4211 }
4212 EXPORT_SYMBOL_GPL(devlink_fmsg_bool_put);
4213
4214 int devlink_fmsg_u8_put(struct devlink_fmsg *fmsg, u8 value)
4215 {
4216         return devlink_fmsg_put_value(fmsg, &value, sizeof(value), NLA_U8);
4217 }
4218 EXPORT_SYMBOL_GPL(devlink_fmsg_u8_put);
4219
4220 int devlink_fmsg_u32_put(struct devlink_fmsg *fmsg, u32 value)
4221 {
4222         return devlink_fmsg_put_value(fmsg, &value, sizeof(value), NLA_U32);
4223 }
4224 EXPORT_SYMBOL_GPL(devlink_fmsg_u32_put);
4225
4226 int devlink_fmsg_u64_put(struct devlink_fmsg *fmsg, u64 value)
4227 {
4228         return devlink_fmsg_put_value(fmsg, &value, sizeof(value), NLA_U64);
4229 }
4230 EXPORT_SYMBOL_GPL(devlink_fmsg_u64_put);
4231
4232 int devlink_fmsg_string_put(struct devlink_fmsg *fmsg, const char *value)
4233 {
4234         return devlink_fmsg_put_value(fmsg, value, strlen(value) + 1,
4235                                       NLA_NUL_STRING);
4236 }
4237 EXPORT_SYMBOL_GPL(devlink_fmsg_string_put);
4238
4239 int devlink_fmsg_binary_put(struct devlink_fmsg *fmsg, const void *value,
4240                             u16 value_len)
4241 {
4242         return devlink_fmsg_put_value(fmsg, value, value_len, NLA_BINARY);
4243 }
4244 EXPORT_SYMBOL_GPL(devlink_fmsg_binary_put);
4245
4246 int devlink_fmsg_bool_pair_put(struct devlink_fmsg *fmsg, const char *name,
4247                                bool value)
4248 {
4249         int err;
4250
4251         err = devlink_fmsg_pair_nest_start(fmsg, name);
4252         if (err)
4253                 return err;
4254
4255         err = devlink_fmsg_bool_put(fmsg, value);
4256         if (err)
4257                 return err;
4258
4259         err = devlink_fmsg_pair_nest_end(fmsg);
4260         if (err)
4261                 return err;
4262
4263         return 0;
4264 }
4265 EXPORT_SYMBOL_GPL(devlink_fmsg_bool_pair_put);
4266
4267 int devlink_fmsg_u8_pair_put(struct devlink_fmsg *fmsg, const char *name,
4268                              u8 value)
4269 {
4270         int err;
4271
4272         err = devlink_fmsg_pair_nest_start(fmsg, name);
4273         if (err)
4274                 return err;
4275
4276         err = devlink_fmsg_u8_put(fmsg, value);
4277         if (err)
4278                 return err;
4279
4280         err = devlink_fmsg_pair_nest_end(fmsg);
4281         if (err)
4282                 return err;
4283
4284         return 0;
4285 }
4286 EXPORT_SYMBOL_GPL(devlink_fmsg_u8_pair_put);
4287
4288 int devlink_fmsg_u32_pair_put(struct devlink_fmsg *fmsg, const char *name,
4289                               u32 value)
4290 {
4291         int err;
4292
4293         err = devlink_fmsg_pair_nest_start(fmsg, name);
4294         if (err)
4295                 return err;
4296
4297         err = devlink_fmsg_u32_put(fmsg, value);
4298         if (err)
4299                 return err;
4300
4301         err = devlink_fmsg_pair_nest_end(fmsg);
4302         if (err)
4303                 return err;
4304
4305         return 0;
4306 }
4307 EXPORT_SYMBOL_GPL(devlink_fmsg_u32_pair_put);
4308
4309 int devlink_fmsg_u64_pair_put(struct devlink_fmsg *fmsg, const char *name,
4310                               u64 value)
4311 {
4312         int err;
4313
4314         err = devlink_fmsg_pair_nest_start(fmsg, name);
4315         if (err)
4316                 return err;
4317
4318         err = devlink_fmsg_u64_put(fmsg, value);
4319         if (err)
4320                 return err;
4321
4322         err = devlink_fmsg_pair_nest_end(fmsg);
4323         if (err)
4324                 return err;
4325
4326         return 0;
4327 }
4328 EXPORT_SYMBOL_GPL(devlink_fmsg_u64_pair_put);
4329
4330 int devlink_fmsg_string_pair_put(struct devlink_fmsg *fmsg, const char *name,
4331                                  const char *value)
4332 {
4333         int err;
4334
4335         err = devlink_fmsg_pair_nest_start(fmsg, name);
4336         if (err)
4337                 return err;
4338
4339         err = devlink_fmsg_string_put(fmsg, value);
4340         if (err)
4341                 return err;
4342
4343         err = devlink_fmsg_pair_nest_end(fmsg);
4344         if (err)
4345                 return err;
4346
4347         return 0;
4348 }
4349 EXPORT_SYMBOL_GPL(devlink_fmsg_string_pair_put);
4350
4351 int devlink_fmsg_binary_pair_put(struct devlink_fmsg *fmsg, const char *name,
4352                                  const void *value, u16 value_len)
4353 {
4354         int err;
4355
4356         err = devlink_fmsg_pair_nest_start(fmsg, name);
4357         if (err)
4358                 return err;
4359
4360         err = devlink_fmsg_binary_put(fmsg, value, value_len);
4361         if (err)
4362                 return err;
4363
4364         err = devlink_fmsg_pair_nest_end(fmsg);
4365         if (err)
4366                 return err;
4367
4368         return 0;
4369 }
4370 EXPORT_SYMBOL_GPL(devlink_fmsg_binary_pair_put);
4371
4372 static int
4373 devlink_fmsg_item_fill_type(struct devlink_fmsg_item *msg, struct sk_buff *skb)
4374 {
4375         switch (msg->nla_type) {
4376         case NLA_FLAG:
4377         case NLA_U8:
4378         case NLA_U32:
4379         case NLA_U64:
4380         case NLA_NUL_STRING:
4381         case NLA_BINARY:
4382                 return nla_put_u8(skb, DEVLINK_ATTR_FMSG_OBJ_VALUE_TYPE,
4383                                   msg->nla_type);
4384         default:
4385                 return -EINVAL;
4386         }
4387 }
4388
4389 static int
4390 devlink_fmsg_item_fill_data(struct devlink_fmsg_item *msg, struct sk_buff *skb)
4391 {
4392         int attrtype = DEVLINK_ATTR_FMSG_OBJ_VALUE_DATA;
4393         u8 tmp;
4394
4395         switch (msg->nla_type) {
4396         case NLA_FLAG:
4397                 /* Always provide flag data, regardless of its value */
4398                 tmp = *(bool *) msg->value;
4399
4400                 return nla_put_u8(skb, attrtype, tmp);
4401         case NLA_U8:
4402                 return nla_put_u8(skb, attrtype, *(u8 *) msg->value);
4403         case NLA_U32:
4404                 return nla_put_u32(skb, attrtype, *(u32 *) msg->value);
4405         case NLA_U64:
4406                 return nla_put_u64_64bit(skb, attrtype, *(u64 *) msg->value,
4407                                          DEVLINK_ATTR_PAD);
4408         case NLA_NUL_STRING:
4409                 return nla_put_string(skb, attrtype, (char *) &msg->value);
4410         case NLA_BINARY:
4411                 return nla_put(skb, attrtype, msg->len, (void *) &msg->value);
4412         default:
4413                 return -EINVAL;
4414         }
4415 }
4416
4417 static int
4418 devlink_fmsg_prepare_skb(struct devlink_fmsg *fmsg, struct sk_buff *skb,
4419                          int *start)
4420 {
4421         struct devlink_fmsg_item *item;
4422         struct nlattr *fmsg_nlattr;
4423         int i = 0;
4424         int err;
4425
4426         fmsg_nlattr = nla_nest_start_noflag(skb, DEVLINK_ATTR_FMSG);
4427         if (!fmsg_nlattr)
4428                 return -EMSGSIZE;
4429
4430         list_for_each_entry(item, &fmsg->item_list, list) {
4431                 if (i < *start) {
4432                         i++;
4433                         continue;
4434                 }
4435
4436                 switch (item->attrtype) {
4437                 case DEVLINK_ATTR_FMSG_OBJ_NEST_START:
4438                 case DEVLINK_ATTR_FMSG_PAIR_NEST_START:
4439                 case DEVLINK_ATTR_FMSG_ARR_NEST_START:
4440                 case DEVLINK_ATTR_FMSG_NEST_END:
4441                         err = nla_put_flag(skb, item->attrtype);
4442                         break;
4443                 case DEVLINK_ATTR_FMSG_OBJ_VALUE_DATA:
4444                         err = devlink_fmsg_item_fill_type(item, skb);
4445                         if (err)
4446                                 break;
4447                         err = devlink_fmsg_item_fill_data(item, skb);
4448                         break;
4449                 case DEVLINK_ATTR_FMSG_OBJ_NAME:
4450                         err = nla_put_string(skb, item->attrtype,
4451                                              (char *) &item->value);
4452                         break;
4453                 default:
4454                         err = -EINVAL;
4455                         break;
4456                 }
4457                 if (!err)
4458                         *start = ++i;
4459                 else
4460                         break;
4461         }
4462
4463         nla_nest_end(skb, fmsg_nlattr);
4464         return err;
4465 }
4466
4467 static int devlink_fmsg_snd(struct devlink_fmsg *fmsg,
4468                             struct genl_info *info,
4469                             enum devlink_command cmd, int flags)
4470 {
4471         struct nlmsghdr *nlh;
4472         struct sk_buff *skb;
4473         bool last = false;
4474         int index = 0;
4475         void *hdr;
4476         int err;
4477
4478         while (!last) {
4479                 int tmp_index = index;
4480
4481                 skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
4482                 if (!skb)
4483                         return -ENOMEM;
4484
4485                 hdr = genlmsg_put(skb, info->snd_portid, info->snd_seq,
4486                                   &devlink_nl_family, flags | NLM_F_MULTI, cmd);
4487                 if (!hdr) {
4488                         err = -EMSGSIZE;
4489                         goto nla_put_failure;
4490                 }
4491
4492                 err = devlink_fmsg_prepare_skb(fmsg, skb, &index);
4493                 if (!err)
4494                         last = true;
4495                 else if (err != -EMSGSIZE || tmp_index == index)
4496                         goto nla_put_failure;
4497
4498                 genlmsg_end(skb, hdr);
4499                 err = genlmsg_reply(skb, info);
4500                 if (err)
4501                         return err;
4502         }
4503
4504         skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
4505         if (!skb)
4506                 return -ENOMEM;
4507         nlh = nlmsg_put(skb, info->snd_portid, info->snd_seq,
4508                         NLMSG_DONE, 0, flags | NLM_F_MULTI);
4509         if (!nlh) {
4510                 err = -EMSGSIZE;
4511                 goto nla_put_failure;
4512         }
4513
4514         return genlmsg_reply(skb, info);
4515
4516 nla_put_failure:
4517         nlmsg_free(skb);
4518         return err;
4519 }
4520
4521 static int devlink_fmsg_dumpit(struct devlink_fmsg *fmsg, struct sk_buff *skb,
4522                                struct netlink_callback *cb,
4523                                enum devlink_command cmd)
4524 {
4525         int index = cb->args[0];
4526         int tmp_index = index;
4527         void *hdr;
4528         int err;
4529
4530         hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
4531                           &devlink_nl_family, NLM_F_ACK | NLM_F_MULTI, cmd);
4532         if (!hdr) {
4533                 err = -EMSGSIZE;
4534                 goto nla_put_failure;
4535         }
4536
4537         err = devlink_fmsg_prepare_skb(fmsg, skb, &index);
4538         if ((err && err != -EMSGSIZE) || tmp_index == index)
4539                 goto nla_put_failure;
4540
4541         cb->args[0] = index;
4542         genlmsg_end(skb, hdr);
4543         return skb->len;
4544
4545 nla_put_failure:
4546         genlmsg_cancel(skb, hdr);
4547         return err;
4548 }
4549
4550 struct devlink_health_reporter {
4551         struct list_head list;
4552         void *priv;
4553         const struct devlink_health_reporter_ops *ops;
4554         struct devlink *devlink;
4555         struct devlink_fmsg *dump_fmsg;
4556         struct mutex dump_lock; /* lock parallel read/write from dump buffers */
4557         u64 graceful_period;
4558         bool auto_recover;
4559         u8 health_state;
4560         u64 dump_ts;
4561         u64 error_count;
4562         u64 recovery_count;
4563         u64 last_recovery_ts;
4564         refcount_t refcount;
4565 };
4566
4567 void *
4568 devlink_health_reporter_priv(struct devlink_health_reporter *reporter)
4569 {
4570         return reporter->priv;
4571 }
4572 EXPORT_SYMBOL_GPL(devlink_health_reporter_priv);
4573
4574 static struct devlink_health_reporter *
4575 devlink_health_reporter_find_by_name(struct devlink *devlink,
4576                                      const char *reporter_name)
4577 {
4578         struct devlink_health_reporter *reporter;
4579
4580         lockdep_assert_held(&devlink->reporters_lock);
4581         list_for_each_entry(reporter, &devlink->reporter_list, list)
4582                 if (!strcmp(reporter->ops->name, reporter_name))
4583                         return reporter;
4584         return NULL;
4585 }
4586
4587 /**
4588  *      devlink_health_reporter_create - create devlink health reporter
4589  *
4590  *      @devlink: devlink
4591  *      @ops: ops
4592  *      @graceful_period: to avoid recovery loops, in msecs
4593  *      @auto_recover: auto recover when error occurs
4594  *      @priv: priv
4595  */
4596 struct devlink_health_reporter *
4597 devlink_health_reporter_create(struct devlink *devlink,
4598                                const struct devlink_health_reporter_ops *ops,
4599                                u64 graceful_period, bool auto_recover,
4600                                void *priv)
4601 {
4602         struct devlink_health_reporter *reporter;
4603
4604         mutex_lock(&devlink->reporters_lock);
4605         if (devlink_health_reporter_find_by_name(devlink, ops->name)) {
4606                 reporter = ERR_PTR(-EEXIST);
4607                 goto unlock;
4608         }
4609
4610         if (WARN_ON(auto_recover && !ops->recover) ||
4611             WARN_ON(graceful_period && !ops->recover)) {
4612                 reporter = ERR_PTR(-EINVAL);
4613                 goto unlock;
4614         }
4615
4616         reporter = kzalloc(sizeof(*reporter), GFP_KERNEL);
4617         if (!reporter) {
4618                 reporter = ERR_PTR(-ENOMEM);
4619                 goto unlock;
4620         }
4621
4622         reporter->priv = priv;
4623         reporter->ops = ops;
4624         reporter->devlink = devlink;
4625         reporter->graceful_period = graceful_period;
4626         reporter->auto_recover = auto_recover;
4627         mutex_init(&reporter->dump_lock);
4628         refcount_set(&reporter->refcount, 1);
4629         list_add_tail(&reporter->list, &devlink->reporter_list);
4630 unlock:
4631         mutex_unlock(&devlink->reporters_lock);
4632         return reporter;
4633 }
4634 EXPORT_SYMBOL_GPL(devlink_health_reporter_create);
4635
4636 /**
4637  *      devlink_health_reporter_destroy - destroy devlink health reporter
4638  *
4639  *      @reporter: devlink health reporter to destroy
4640  */
4641 void
4642 devlink_health_reporter_destroy(struct devlink_health_reporter *reporter)
4643 {
4644         mutex_lock(&reporter->devlink->reporters_lock);
4645         list_del(&reporter->list);
4646         mutex_unlock(&reporter->devlink->reporters_lock);
4647         while (refcount_read(&reporter->refcount) > 1)
4648                 msleep(100);
4649         mutex_destroy(&reporter->dump_lock);
4650         if (reporter->dump_fmsg)
4651                 devlink_fmsg_free(reporter->dump_fmsg);
4652         kfree(reporter);
4653 }
4654 EXPORT_SYMBOL_GPL(devlink_health_reporter_destroy);
4655
4656 void
4657 devlink_health_reporter_state_update(struct devlink_health_reporter *reporter,
4658                                      enum devlink_health_reporter_state state)
4659 {
4660         if (WARN_ON(state != DEVLINK_HEALTH_REPORTER_STATE_HEALTHY &&
4661                     state != DEVLINK_HEALTH_REPORTER_STATE_ERROR))
4662                 return;
4663
4664         if (reporter->health_state == state)
4665                 return;
4666
4667         reporter->health_state = state;
4668         trace_devlink_health_reporter_state_update(reporter->devlink,
4669                                                    reporter->ops->name, state);
4670 }
4671 EXPORT_SYMBOL_GPL(devlink_health_reporter_state_update);
4672
4673 static int
4674 devlink_health_reporter_recover(struct devlink_health_reporter *reporter,
4675                                 void *priv_ctx)
4676 {
4677         int err;
4678
4679         if (!reporter->ops->recover)
4680                 return -EOPNOTSUPP;
4681
4682         err = reporter->ops->recover(reporter, priv_ctx);
4683         if (err)
4684                 return err;
4685
4686         reporter->recovery_count++;
4687         reporter->health_state = DEVLINK_HEALTH_REPORTER_STATE_HEALTHY;
4688         reporter->last_recovery_ts = jiffies;
4689
4690         return 0;
4691 }
4692
4693 static void
4694 devlink_health_dump_clear(struct devlink_health_reporter *reporter)
4695 {
4696         if (!reporter->dump_fmsg)
4697                 return;
4698         devlink_fmsg_free(reporter->dump_fmsg);
4699         reporter->dump_fmsg = NULL;
4700 }
4701
4702 static int devlink_health_do_dump(struct devlink_health_reporter *reporter,
4703                                   void *priv_ctx)
4704 {
4705         int err;
4706
4707         if (!reporter->ops->dump)
4708                 return 0;
4709
4710         if (reporter->dump_fmsg)
4711                 return 0;
4712
4713         reporter->dump_fmsg = devlink_fmsg_alloc();
4714         if (!reporter->dump_fmsg) {
4715                 err = -ENOMEM;
4716                 return err;
4717         }
4718
4719         err = devlink_fmsg_obj_nest_start(reporter->dump_fmsg);
4720         if (err)
4721                 goto dump_err;
4722
4723         err = reporter->ops->dump(reporter, reporter->dump_fmsg,
4724                                   priv_ctx);
4725         if (err)
4726                 goto dump_err;
4727
4728         err = devlink_fmsg_obj_nest_end(reporter->dump_fmsg);
4729         if (err)
4730                 goto dump_err;
4731
4732         reporter->dump_ts = jiffies;
4733
4734         return 0;
4735
4736 dump_err:
4737         devlink_health_dump_clear(reporter);
4738         return err;
4739 }
4740
4741 int devlink_health_report(struct devlink_health_reporter *reporter,
4742                           const char *msg, void *priv_ctx)
4743 {
4744         enum devlink_health_reporter_state prev_health_state;
4745         struct devlink *devlink = reporter->devlink;
4746
4747         /* write a log message of the current error */
4748         WARN_ON(!msg);
4749         trace_devlink_health_report(devlink, reporter->ops->name, msg);
4750         reporter->error_count++;
4751         prev_health_state = reporter->health_state;
4752         reporter->health_state = DEVLINK_HEALTH_REPORTER_STATE_ERROR;
4753
4754         /* abort if the previous error wasn't recovered */
4755         if (reporter->auto_recover &&
4756             (prev_health_state != DEVLINK_HEALTH_REPORTER_STATE_HEALTHY ||
4757              jiffies - reporter->last_recovery_ts <
4758              msecs_to_jiffies(reporter->graceful_period))) {
4759                 trace_devlink_health_recover_aborted(devlink,
4760                                                      reporter->ops->name,
4761                                                      reporter->health_state,
4762                                                      jiffies -
4763                                                      reporter->last_recovery_ts);
4764                 return -ECANCELED;
4765         }
4766
4767         reporter->health_state = DEVLINK_HEALTH_REPORTER_STATE_ERROR;
4768
4769         mutex_lock(&reporter->dump_lock);
4770         /* store current dump of current error, for later analysis */
4771         devlink_health_do_dump(reporter, priv_ctx);
4772         mutex_unlock(&reporter->dump_lock);
4773
4774         if (reporter->auto_recover)
4775                 return devlink_health_reporter_recover(reporter, priv_ctx);
4776
4777         return 0;
4778 }
4779 EXPORT_SYMBOL_GPL(devlink_health_report);
4780
4781 static struct devlink_health_reporter *
4782 devlink_health_reporter_get_from_attrs(struct devlink *devlink,
4783                                        struct nlattr **attrs)
4784 {
4785         struct devlink_health_reporter *reporter;
4786         char *reporter_name;
4787
4788         if (!attrs[DEVLINK_ATTR_HEALTH_REPORTER_NAME])
4789                 return NULL;
4790
4791         reporter_name = nla_data(attrs[DEVLINK_ATTR_HEALTH_REPORTER_NAME]);
4792         mutex_lock(&devlink->reporters_lock);
4793         reporter = devlink_health_reporter_find_by_name(devlink, reporter_name);
4794         if (reporter)
4795                 refcount_inc(&reporter->refcount);
4796         mutex_unlock(&devlink->reporters_lock);
4797         return reporter;
4798 }
4799
4800 static struct devlink_health_reporter *
4801 devlink_health_reporter_get_from_info(struct devlink *devlink,
4802                                       struct genl_info *info)
4803 {
4804         return devlink_health_reporter_get_from_attrs(devlink, info->attrs);
4805 }
4806
4807 static struct devlink_health_reporter *
4808 devlink_health_reporter_get_from_cb(struct netlink_callback *cb)
4809 {
4810         struct devlink_health_reporter *reporter;
4811         struct devlink *devlink;
4812         struct nlattr **attrs;
4813         int err;
4814
4815         attrs = kmalloc_array(DEVLINK_ATTR_MAX + 1, sizeof(*attrs), GFP_KERNEL);
4816         if (!attrs)
4817                 return NULL;
4818
4819         err = nlmsg_parse_deprecated(cb->nlh,
4820                                      GENL_HDRLEN + devlink_nl_family.hdrsize,
4821                                      attrs, DEVLINK_ATTR_MAX,
4822                                      devlink_nl_family.policy, cb->extack);
4823         if (err)
4824                 goto free;
4825
4826         mutex_lock(&devlink_mutex);
4827         devlink = devlink_get_from_attrs(sock_net(cb->skb->sk), attrs);
4828         if (IS_ERR(devlink))
4829                 goto unlock;
4830
4831         reporter = devlink_health_reporter_get_from_attrs(devlink, attrs);
4832         mutex_unlock(&devlink_mutex);
4833         kfree(attrs);
4834         return reporter;
4835 unlock:
4836         mutex_unlock(&devlink_mutex);
4837 free:
4838         kfree(attrs);
4839         return NULL;
4840 }
4841
4842 static void
4843 devlink_health_reporter_put(struct devlink_health_reporter *reporter)
4844 {
4845         refcount_dec(&reporter->refcount);
4846 }
4847
4848 static int
4849 devlink_nl_health_reporter_fill(struct sk_buff *msg,
4850                                 struct devlink *devlink,
4851                                 struct devlink_health_reporter *reporter,
4852                                 enum devlink_command cmd, u32 portid,
4853                                 u32 seq, int flags)
4854 {
4855         struct nlattr *reporter_attr;
4856         void *hdr;
4857
4858         hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
4859         if (!hdr)
4860                 return -EMSGSIZE;
4861
4862         if (devlink_nl_put_handle(msg, devlink))
4863                 goto genlmsg_cancel;
4864
4865         reporter_attr = nla_nest_start_noflag(msg,
4866                                               DEVLINK_ATTR_HEALTH_REPORTER);
4867         if (!reporter_attr)
4868                 goto genlmsg_cancel;
4869         if (nla_put_string(msg, DEVLINK_ATTR_HEALTH_REPORTER_NAME,
4870                            reporter->ops->name))
4871                 goto reporter_nest_cancel;
4872         if (nla_put_u8(msg, DEVLINK_ATTR_HEALTH_REPORTER_STATE,
4873                        reporter->health_state))
4874                 goto reporter_nest_cancel;
4875         if (nla_put_u64_64bit(msg, DEVLINK_ATTR_HEALTH_REPORTER_ERR_COUNT,
4876                               reporter->error_count, DEVLINK_ATTR_PAD))
4877                 goto reporter_nest_cancel;
4878         if (nla_put_u64_64bit(msg, DEVLINK_ATTR_HEALTH_REPORTER_RECOVER_COUNT,
4879                               reporter->recovery_count, DEVLINK_ATTR_PAD))
4880                 goto reporter_nest_cancel;
4881         if (reporter->ops->recover &&
4882             nla_put_u64_64bit(msg, DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD,
4883                               reporter->graceful_period,
4884                               DEVLINK_ATTR_PAD))
4885                 goto reporter_nest_cancel;
4886         if (reporter->ops->recover &&
4887             nla_put_u8(msg, DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER,
4888                        reporter->auto_recover))
4889                 goto reporter_nest_cancel;
4890         if (reporter->dump_fmsg &&
4891             nla_put_u64_64bit(msg, DEVLINK_ATTR_HEALTH_REPORTER_DUMP_TS,
4892                               jiffies_to_msecs(reporter->dump_ts),
4893                               DEVLINK_ATTR_PAD))
4894                 goto reporter_nest_cancel;
4895
4896         nla_nest_end(msg, reporter_attr);
4897         genlmsg_end(msg, hdr);
4898         return 0;
4899
4900 reporter_nest_cancel:
4901         nla_nest_end(msg, reporter_attr);
4902 genlmsg_cancel:
4903         genlmsg_cancel(msg, hdr);
4904         return -EMSGSIZE;
4905 }
4906
4907 static int devlink_nl_cmd_health_reporter_get_doit(struct sk_buff *skb,
4908                                                    struct genl_info *info)
4909 {
4910         struct devlink *devlink = info->user_ptr[0];
4911         struct devlink_health_reporter *reporter;
4912         struct sk_buff *msg;
4913         int err;
4914
4915         reporter = devlink_health_reporter_get_from_info(devlink, info);
4916         if (!reporter)
4917                 return -EINVAL;
4918
4919         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
4920         if (!msg) {
4921                 err = -ENOMEM;
4922                 goto out;
4923         }
4924
4925         err = devlink_nl_health_reporter_fill(msg, devlink, reporter,
4926                                               DEVLINK_CMD_HEALTH_REPORTER_GET,
4927                                               info->snd_portid, info->snd_seq,
4928                                               0);
4929         if (err) {
4930                 nlmsg_free(msg);
4931                 goto out;
4932         }
4933
4934         err = genlmsg_reply(msg, info);
4935 out:
4936         devlink_health_reporter_put(reporter);
4937         return err;
4938 }
4939
4940 static int
4941 devlink_nl_cmd_health_reporter_get_dumpit(struct sk_buff *msg,
4942                                           struct netlink_callback *cb)
4943 {
4944         struct devlink_health_reporter *reporter;
4945         struct devlink *devlink;
4946         int start = cb->args[0];
4947         int idx = 0;
4948         int err;
4949
4950         mutex_lock(&devlink_mutex);
4951         list_for_each_entry(devlink, &devlink_list, list) {
4952                 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
4953                         continue;
4954                 mutex_lock(&devlink->reporters_lock);
4955                 list_for_each_entry(reporter, &devlink->reporter_list,
4956                                     list) {
4957                         if (idx < start) {
4958                                 idx++;
4959                                 continue;
4960                         }
4961                         err = devlink_nl_health_reporter_fill(msg, devlink,
4962                                                               reporter,
4963                                                               DEVLINK_CMD_HEALTH_REPORTER_GET,
4964                                                               NETLINK_CB(cb->skb).portid,
4965                                                               cb->nlh->nlmsg_seq,
4966                                                               NLM_F_MULTI);
4967                         if (err) {
4968                                 mutex_unlock(&devlink->reporters_lock);
4969                                 goto out;
4970                         }
4971                         idx++;
4972                 }
4973                 mutex_unlock(&devlink->reporters_lock);
4974         }
4975 out:
4976         mutex_unlock(&devlink_mutex);
4977
4978         cb->args[0] = idx;
4979         return msg->len;
4980 }
4981
4982 static int
4983 devlink_nl_cmd_health_reporter_set_doit(struct sk_buff *skb,
4984                                         struct genl_info *info)
4985 {
4986         struct devlink *devlink = info->user_ptr[0];
4987         struct devlink_health_reporter *reporter;
4988         int err;
4989
4990         reporter = devlink_health_reporter_get_from_info(devlink, info);
4991         if (!reporter)
4992                 return -EINVAL;
4993
4994         if (!reporter->ops->recover &&
4995             (info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD] ||
4996              info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER])) {
4997                 err = -EOPNOTSUPP;
4998                 goto out;
4999         }
5000
5001         if (info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD])
5002                 reporter->graceful_period =
5003                         nla_get_u64(info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD]);
5004
5005         if (info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER])
5006                 reporter->auto_recover =
5007                         nla_get_u8(info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER]);
5008
5009         devlink_health_reporter_put(reporter);
5010         return 0;
5011 out:
5012         devlink_health_reporter_put(reporter);
5013         return err;
5014 }
5015
5016 static int devlink_nl_cmd_health_reporter_recover_doit(struct sk_buff *skb,
5017                                                        struct genl_info *info)
5018 {
5019         struct devlink *devlink = info->user_ptr[0];
5020         struct devlink_health_reporter *reporter;
5021         int err;
5022
5023         reporter = devlink_health_reporter_get_from_info(devlink, info);
5024         if (!reporter)
5025                 return -EINVAL;
5026
5027         err = devlink_health_reporter_recover(reporter, NULL);
5028
5029         devlink_health_reporter_put(reporter);
5030         return err;
5031 }
5032
5033 static int devlink_nl_cmd_health_reporter_diagnose_doit(struct sk_buff *skb,
5034                                                         struct genl_info *info)
5035 {
5036         struct devlink *devlink = info->user_ptr[0];
5037         struct devlink_health_reporter *reporter;
5038         struct devlink_fmsg *fmsg;
5039         int err;
5040
5041         reporter = devlink_health_reporter_get_from_info(devlink, info);
5042         if (!reporter)
5043                 return -EINVAL;
5044
5045         if (!reporter->ops->diagnose) {
5046                 devlink_health_reporter_put(reporter);
5047                 return -EOPNOTSUPP;
5048         }
5049
5050         fmsg = devlink_fmsg_alloc();
5051         if (!fmsg) {
5052                 devlink_health_reporter_put(reporter);
5053                 return -ENOMEM;
5054         }
5055
5056         err = devlink_fmsg_obj_nest_start(fmsg);
5057         if (err)
5058                 goto out;
5059
5060         err = reporter->ops->diagnose(reporter, fmsg);
5061         if (err)
5062                 goto out;
5063
5064         err = devlink_fmsg_obj_nest_end(fmsg);
5065         if (err)
5066                 goto out;
5067
5068         err = devlink_fmsg_snd(fmsg, info,
5069                                DEVLINK_CMD_HEALTH_REPORTER_DIAGNOSE, 0);
5070
5071 out:
5072         devlink_fmsg_free(fmsg);
5073         devlink_health_reporter_put(reporter);
5074         return err;
5075 }
5076
5077 static int
5078 devlink_nl_cmd_health_reporter_dump_get_dumpit(struct sk_buff *skb,
5079                                                struct netlink_callback *cb)
5080 {
5081         struct devlink_health_reporter *reporter;
5082         u64 start = cb->args[0];
5083         int err;
5084
5085         reporter = devlink_health_reporter_get_from_cb(cb);
5086         if (!reporter)
5087                 return -EINVAL;
5088
5089         if (!reporter->ops->dump) {
5090                 err = -EOPNOTSUPP;
5091                 goto out;
5092         }
5093         mutex_lock(&reporter->dump_lock);
5094         if (!start) {
5095                 err = devlink_health_do_dump(reporter, NULL);
5096                 if (err)
5097                         goto unlock;
5098                 cb->args[1] = reporter->dump_ts;
5099         }
5100         if (!reporter->dump_fmsg || cb->args[1] != reporter->dump_ts) {
5101                 NL_SET_ERR_MSG_MOD(cb->extack, "Dump trampled, please retry");
5102                 err = -EAGAIN;
5103                 goto unlock;
5104         }
5105
5106         err = devlink_fmsg_dumpit(reporter->dump_fmsg, skb, cb,
5107                                   DEVLINK_CMD_HEALTH_REPORTER_DUMP_GET);
5108 unlock:
5109         mutex_unlock(&reporter->dump_lock);
5110 out:
5111         devlink_health_reporter_put(reporter);
5112         return err;
5113 }
5114
5115 static int
5116 devlink_nl_cmd_health_reporter_dump_clear_doit(struct sk_buff *skb,
5117                                                struct genl_info *info)
5118 {
5119         struct devlink *devlink = info->user_ptr[0];
5120         struct devlink_health_reporter *reporter;
5121
5122         reporter = devlink_health_reporter_get_from_info(devlink, info);
5123         if (!reporter)
5124                 return -EINVAL;
5125
5126         if (!reporter->ops->dump) {
5127                 devlink_health_reporter_put(reporter);
5128                 return -EOPNOTSUPP;
5129         }
5130
5131         mutex_lock(&reporter->dump_lock);
5132         devlink_health_dump_clear(reporter);
5133         mutex_unlock(&reporter->dump_lock);
5134         devlink_health_reporter_put(reporter);
5135         return 0;
5136 }
5137
5138 static const struct nla_policy devlink_nl_policy[DEVLINK_ATTR_MAX + 1] = {
5139         [DEVLINK_ATTR_BUS_NAME] = { .type = NLA_NUL_STRING },
5140         [DEVLINK_ATTR_DEV_NAME] = { .type = NLA_NUL_STRING },
5141         [DEVLINK_ATTR_PORT_INDEX] = { .type = NLA_U32 },
5142         [DEVLINK_ATTR_PORT_TYPE] = { .type = NLA_U16 },
5143         [DEVLINK_ATTR_PORT_SPLIT_COUNT] = { .type = NLA_U32 },
5144         [DEVLINK_ATTR_SB_INDEX] = { .type = NLA_U32 },
5145         [DEVLINK_ATTR_SB_POOL_INDEX] = { .type = NLA_U16 },
5146         [DEVLINK_ATTR_SB_POOL_TYPE] = { .type = NLA_U8 },
5147         [DEVLINK_ATTR_SB_POOL_SIZE] = { .type = NLA_U32 },
5148         [DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE] = { .type = NLA_U8 },
5149         [DEVLINK_ATTR_SB_THRESHOLD] = { .type = NLA_U32 },
5150         [DEVLINK_ATTR_SB_TC_INDEX] = { .type = NLA_U16 },
5151         [DEVLINK_ATTR_ESWITCH_MODE] = { .type = NLA_U16 },
5152         [DEVLINK_ATTR_ESWITCH_INLINE_MODE] = { .type = NLA_U8 },
5153         [DEVLINK_ATTR_ESWITCH_ENCAP_MODE] = { .type = NLA_U8 },
5154         [DEVLINK_ATTR_DPIPE_TABLE_NAME] = { .type = NLA_NUL_STRING },
5155         [DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED] = { .type = NLA_U8 },
5156         [DEVLINK_ATTR_RESOURCE_ID] = { .type = NLA_U64},
5157         [DEVLINK_ATTR_RESOURCE_SIZE] = { .type = NLA_U64},
5158         [DEVLINK_ATTR_PARAM_NAME] = { .type = NLA_NUL_STRING },
5159         [DEVLINK_ATTR_PARAM_TYPE] = { .type = NLA_U8 },
5160         [DEVLINK_ATTR_PARAM_VALUE_CMODE] = { .type = NLA_U8 },
5161         [DEVLINK_ATTR_REGION_NAME] = { .type = NLA_NUL_STRING },
5162         [DEVLINK_ATTR_REGION_SNAPSHOT_ID] = { .type = NLA_U32 },
5163         [DEVLINK_ATTR_HEALTH_REPORTER_NAME] = { .type = NLA_NUL_STRING },
5164         [DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD] = { .type = NLA_U64 },
5165         [DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER] = { .type = NLA_U8 },
5166         [DEVLINK_ATTR_FLASH_UPDATE_FILE_NAME] = { .type = NLA_NUL_STRING },
5167         [DEVLINK_ATTR_FLASH_UPDATE_COMPONENT] = { .type = NLA_NUL_STRING },
5168 };
5169
5170 static const struct genl_ops devlink_nl_ops[] = {
5171         {
5172                 .cmd = DEVLINK_CMD_GET,
5173                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
5174                 .doit = devlink_nl_cmd_get_doit,
5175                 .dumpit = devlink_nl_cmd_get_dumpit,
5176                 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
5177                 /* can be retrieved by unprivileged users */
5178         },
5179         {
5180                 .cmd = DEVLINK_CMD_PORT_GET,
5181                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
5182                 .doit = devlink_nl_cmd_port_get_doit,
5183                 .dumpit = devlink_nl_cmd_port_get_dumpit,
5184                 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
5185                 /* can be retrieved by unprivileged users */
5186         },
5187         {
5188                 .cmd = DEVLINK_CMD_PORT_SET,
5189                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
5190                 .doit = devlink_nl_cmd_port_set_doit,
5191                 .flags = GENL_ADMIN_PERM,
5192                 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
5193         },
5194         {
5195                 .cmd = DEVLINK_CMD_PORT_SPLIT,
5196                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
5197                 .doit = devlink_nl_cmd_port_split_doit,
5198                 .flags = GENL_ADMIN_PERM,
5199                 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
5200                                   DEVLINK_NL_FLAG_NO_LOCK,
5201         },
5202         {
5203                 .cmd = DEVLINK_CMD_PORT_UNSPLIT,
5204                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
5205                 .doit = devlink_nl_cmd_port_unsplit_doit,
5206                 .flags = GENL_ADMIN_PERM,
5207                 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
5208                                   DEVLINK_NL_FLAG_NO_LOCK,
5209         },
5210         {
5211                 .cmd = DEVLINK_CMD_SB_GET,
5212                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
5213                 .doit = devlink_nl_cmd_sb_get_doit,
5214                 .dumpit = devlink_nl_cmd_sb_get_dumpit,
5215                 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
5216                                   DEVLINK_NL_FLAG_NEED_SB,
5217                 /* can be retrieved by unprivileged users */
5218         },
5219         {
5220                 .cmd = DEVLINK_CMD_SB_POOL_GET,
5221                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
5222                 .doit = devlink_nl_cmd_sb_pool_get_doit,
5223                 .dumpit = devlink_nl_cmd_sb_pool_get_dumpit,
5224                 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
5225                                   DEVLINK_NL_FLAG_NEED_SB,
5226                 /* can be retrieved by unprivileged users */
5227         },
5228         {
5229                 .cmd = DEVLINK_CMD_SB_POOL_SET,
5230                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
5231                 .doit = devlink_nl_cmd_sb_pool_set_doit,
5232                 .flags = GENL_ADMIN_PERM,
5233                 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
5234                                   DEVLINK_NL_FLAG_NEED_SB,
5235         },
5236         {
5237                 .cmd = DEVLINK_CMD_SB_PORT_POOL_GET,
5238                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
5239                 .doit = devlink_nl_cmd_sb_port_pool_get_doit,
5240                 .dumpit = devlink_nl_cmd_sb_port_pool_get_dumpit,
5241                 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT |
5242                                   DEVLINK_NL_FLAG_NEED_SB,
5243                 /* can be retrieved by unprivileged users */
5244         },
5245         {
5246                 .cmd = DEVLINK_CMD_SB_PORT_POOL_SET,
5247                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
5248                 .doit = devlink_nl_cmd_sb_port_pool_set_doit,
5249                 .flags = GENL_ADMIN_PERM,
5250                 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT |
5251                                   DEVLINK_NL_FLAG_NEED_SB,
5252         },
5253         {
5254                 .cmd = DEVLINK_CMD_SB_TC_POOL_BIND_GET,
5255                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
5256                 .doit = devlink_nl_cmd_sb_tc_pool_bind_get_doit,
5257                 .dumpit = devlink_nl_cmd_sb_tc_pool_bind_get_dumpit,
5258                 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT |
5259                                   DEVLINK_NL_FLAG_NEED_SB,
5260                 /* can be retrieved by unprivileged users */
5261         },
5262         {
5263                 .cmd = DEVLINK_CMD_SB_TC_POOL_BIND_SET,
5264                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
5265                 .doit = devlink_nl_cmd_sb_tc_pool_bind_set_doit,
5266                 .flags = GENL_ADMIN_PERM,
5267                 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT |
5268                                   DEVLINK_NL_FLAG_NEED_SB,
5269         },
5270         {
5271                 .cmd = DEVLINK_CMD_SB_OCC_SNAPSHOT,
5272                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
5273                 .doit = devlink_nl_cmd_sb_occ_snapshot_doit,
5274                 .flags = GENL_ADMIN_PERM,
5275                 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
5276                                   DEVLINK_NL_FLAG_NEED_SB,
5277         },
5278         {
5279                 .cmd = DEVLINK_CMD_SB_OCC_MAX_CLEAR,
5280                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
5281                 .doit = devlink_nl_cmd_sb_occ_max_clear_doit,
5282                 .flags = GENL_ADMIN_PERM,
5283                 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
5284                                   DEVLINK_NL_FLAG_NEED_SB,
5285         },
5286         {
5287                 .cmd = DEVLINK_CMD_ESWITCH_GET,
5288                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
5289                 .doit = devlink_nl_cmd_eswitch_get_doit,
5290                 .flags = GENL_ADMIN_PERM,
5291                 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
5292         },
5293         {
5294                 .cmd = DEVLINK_CMD_ESWITCH_SET,
5295                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
5296                 .doit = devlink_nl_cmd_eswitch_set_doit,
5297                 .flags = GENL_ADMIN_PERM,
5298                 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
5299                                   DEVLINK_NL_FLAG_NO_LOCK,
5300         },
5301         {
5302                 .cmd = DEVLINK_CMD_DPIPE_TABLE_GET,
5303                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
5304                 .doit = devlink_nl_cmd_dpipe_table_get,
5305                 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
5306                 /* can be retrieved by unprivileged users */
5307         },
5308         {
5309                 .cmd = DEVLINK_CMD_DPIPE_ENTRIES_GET,
5310                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
5311                 .doit = devlink_nl_cmd_dpipe_entries_get,
5312                 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
5313                 /* can be retrieved by unprivileged users */
5314         },
5315         {
5316                 .cmd = DEVLINK_CMD_DPIPE_HEADERS_GET,
5317                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
5318                 .doit = devlink_nl_cmd_dpipe_headers_get,
5319                 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
5320                 /* can be retrieved by unprivileged users */
5321         },
5322         {
5323                 .cmd = DEVLINK_CMD_DPIPE_TABLE_COUNTERS_SET,
5324                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
5325                 .doit = devlink_nl_cmd_dpipe_table_counters_set,
5326                 .flags = GENL_ADMIN_PERM,
5327                 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
5328         },
5329         {
5330                 .cmd = DEVLINK_CMD_RESOURCE_SET,
5331                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
5332                 .doit = devlink_nl_cmd_resource_set,
5333                 .flags = GENL_ADMIN_PERM,
5334                 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
5335         },
5336         {
5337                 .cmd = DEVLINK_CMD_RESOURCE_DUMP,
5338                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
5339                 .doit = devlink_nl_cmd_resource_dump,
5340                 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
5341                 /* can be retrieved by unprivileged users */
5342         },
5343         {
5344                 .cmd = DEVLINK_CMD_RELOAD,
5345                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
5346                 .doit = devlink_nl_cmd_reload,
5347                 .flags = GENL_ADMIN_PERM,
5348                 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
5349                                   DEVLINK_NL_FLAG_NO_LOCK,
5350         },
5351         {
5352                 .cmd = DEVLINK_CMD_PARAM_GET,
5353                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
5354                 .doit = devlink_nl_cmd_param_get_doit,
5355                 .dumpit = devlink_nl_cmd_param_get_dumpit,
5356                 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
5357                 /* can be retrieved by unprivileged users */
5358         },
5359         {
5360                 .cmd = DEVLINK_CMD_PARAM_SET,
5361                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
5362                 .doit = devlink_nl_cmd_param_set_doit,
5363                 .flags = GENL_ADMIN_PERM,
5364                 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
5365         },
5366         {
5367                 .cmd = DEVLINK_CMD_PORT_PARAM_GET,
5368                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
5369                 .doit = devlink_nl_cmd_port_param_get_doit,
5370                 .dumpit = devlink_nl_cmd_port_param_get_dumpit,
5371                 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
5372                 /* can be retrieved by unprivileged users */
5373         },
5374         {
5375                 .cmd = DEVLINK_CMD_PORT_PARAM_SET,
5376                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
5377                 .doit = devlink_nl_cmd_port_param_set_doit,
5378                 .flags = GENL_ADMIN_PERM,
5379                 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
5380         },
5381         {
5382                 .cmd = DEVLINK_CMD_REGION_GET,
5383                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
5384                 .doit = devlink_nl_cmd_region_get_doit,
5385                 .dumpit = devlink_nl_cmd_region_get_dumpit,
5386                 .flags = GENL_ADMIN_PERM,
5387                 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
5388         },
5389         {
5390                 .cmd = DEVLINK_CMD_REGION_DEL,
5391                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
5392                 .doit = devlink_nl_cmd_region_del,
5393                 .flags = GENL_ADMIN_PERM,
5394                 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
5395         },
5396         {
5397                 .cmd = DEVLINK_CMD_REGION_READ,
5398                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
5399                 .dumpit = devlink_nl_cmd_region_read_dumpit,
5400                 .flags = GENL_ADMIN_PERM,
5401                 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
5402         },
5403         {
5404                 .cmd = DEVLINK_CMD_INFO_GET,
5405                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
5406                 .doit = devlink_nl_cmd_info_get_doit,
5407                 .dumpit = devlink_nl_cmd_info_get_dumpit,
5408                 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
5409                 /* can be retrieved by unprivileged users */
5410         },
5411         {
5412                 .cmd = DEVLINK_CMD_HEALTH_REPORTER_GET,
5413                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
5414                 .doit = devlink_nl_cmd_health_reporter_get_doit,
5415                 .dumpit = devlink_nl_cmd_health_reporter_get_dumpit,
5416                 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
5417                                   DEVLINK_NL_FLAG_NO_LOCK,
5418                 /* can be retrieved by unprivileged users */
5419         },
5420         {
5421                 .cmd = DEVLINK_CMD_HEALTH_REPORTER_SET,
5422                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
5423                 .doit = devlink_nl_cmd_health_reporter_set_doit,
5424                 .flags = GENL_ADMIN_PERM,
5425                 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
5426                                   DEVLINK_NL_FLAG_NO_LOCK,
5427         },
5428         {
5429                 .cmd = DEVLINK_CMD_HEALTH_REPORTER_RECOVER,
5430                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
5431                 .doit = devlink_nl_cmd_health_reporter_recover_doit,
5432                 .flags = GENL_ADMIN_PERM,
5433                 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
5434                                   DEVLINK_NL_FLAG_NO_LOCK,
5435         },
5436         {
5437                 .cmd = DEVLINK_CMD_HEALTH_REPORTER_DIAGNOSE,
5438                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
5439                 .doit = devlink_nl_cmd_health_reporter_diagnose_doit,
5440                 .flags = GENL_ADMIN_PERM,
5441                 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
5442                                   DEVLINK_NL_FLAG_NO_LOCK,
5443         },
5444         {
5445                 .cmd = DEVLINK_CMD_HEALTH_REPORTER_DUMP_GET,
5446                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
5447                 .dumpit = devlink_nl_cmd_health_reporter_dump_get_dumpit,
5448                 .flags = GENL_ADMIN_PERM,
5449                 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
5450                                   DEVLINK_NL_FLAG_NO_LOCK,
5451         },
5452         {
5453                 .cmd = DEVLINK_CMD_HEALTH_REPORTER_DUMP_CLEAR,
5454                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
5455                 .doit = devlink_nl_cmd_health_reporter_dump_clear_doit,
5456                 .flags = GENL_ADMIN_PERM,
5457                 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
5458                                   DEVLINK_NL_FLAG_NO_LOCK,
5459         },
5460         {
5461                 .cmd = DEVLINK_CMD_FLASH_UPDATE,
5462                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
5463                 .doit = devlink_nl_cmd_flash_update,
5464                 .flags = GENL_ADMIN_PERM,
5465                 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
5466         },
5467 };
5468
5469 static struct genl_family devlink_nl_family __ro_after_init = {
5470         .name           = DEVLINK_GENL_NAME,
5471         .version        = DEVLINK_GENL_VERSION,
5472         .maxattr        = DEVLINK_ATTR_MAX,
5473         .policy = devlink_nl_policy,
5474         .netnsok        = true,
5475         .pre_doit       = devlink_nl_pre_doit,
5476         .post_doit      = devlink_nl_post_doit,
5477         .module         = THIS_MODULE,
5478         .ops            = devlink_nl_ops,
5479         .n_ops          = ARRAY_SIZE(devlink_nl_ops),
5480         .mcgrps         = devlink_nl_mcgrps,
5481         .n_mcgrps       = ARRAY_SIZE(devlink_nl_mcgrps),
5482 };
5483
5484 /**
5485  *      devlink_alloc - Allocate new devlink instance resources
5486  *
5487  *      @ops: ops
5488  *      @priv_size: size of user private data
5489  *
5490  *      Allocate new devlink instance resources, including devlink index
5491  *      and name.
5492  */
5493 struct devlink *devlink_alloc(const struct devlink_ops *ops, size_t priv_size)
5494 {
5495         struct devlink *devlink;
5496
5497         if (WARN_ON(!ops))
5498                 return NULL;
5499
5500         devlink = kzalloc(sizeof(*devlink) + priv_size, GFP_KERNEL);
5501         if (!devlink)
5502                 return NULL;
5503         devlink->ops = ops;
5504         devlink_net_set(devlink, &init_net);
5505         INIT_LIST_HEAD(&devlink->port_list);
5506         INIT_LIST_HEAD(&devlink->sb_list);
5507         INIT_LIST_HEAD_RCU(&devlink->dpipe_table_list);
5508         INIT_LIST_HEAD(&devlink->resource_list);
5509         INIT_LIST_HEAD(&devlink->param_list);
5510         INIT_LIST_HEAD(&devlink->region_list);
5511         INIT_LIST_HEAD(&devlink->reporter_list);
5512         mutex_init(&devlink->lock);
5513         mutex_init(&devlink->reporters_lock);
5514         return devlink;
5515 }
5516 EXPORT_SYMBOL_GPL(devlink_alloc);
5517
5518 /**
5519  *      devlink_register - Register devlink instance
5520  *
5521  *      @devlink: devlink
5522  *      @dev: parent device
5523  */
5524 int devlink_register(struct devlink *devlink, struct device *dev)
5525 {
5526         mutex_lock(&devlink_mutex);
5527         devlink->dev = dev;
5528         list_add_tail(&devlink->list, &devlink_list);
5529         devlink_notify(devlink, DEVLINK_CMD_NEW);
5530         mutex_unlock(&devlink_mutex);
5531         return 0;
5532 }
5533 EXPORT_SYMBOL_GPL(devlink_register);
5534
5535 /**
5536  *      devlink_unregister - Unregister devlink instance
5537  *
5538  *      @devlink: devlink
5539  */
5540 void devlink_unregister(struct devlink *devlink)
5541 {
5542         mutex_lock(&devlink_mutex);
5543         devlink_notify(devlink, DEVLINK_CMD_DEL);
5544         list_del(&devlink->list);
5545         mutex_unlock(&devlink_mutex);
5546 }
5547 EXPORT_SYMBOL_GPL(devlink_unregister);
5548
5549 /**
5550  *      devlink_free - Free devlink instance resources
5551  *
5552  *      @devlink: devlink
5553  */
5554 void devlink_free(struct devlink *devlink)
5555 {
5556         mutex_destroy(&devlink->reporters_lock);
5557         mutex_destroy(&devlink->lock);
5558         WARN_ON(!list_empty(&devlink->reporter_list));
5559         WARN_ON(!list_empty(&devlink->region_list));
5560         WARN_ON(!list_empty(&devlink->param_list));
5561         WARN_ON(!list_empty(&devlink->resource_list));
5562         WARN_ON(!list_empty(&devlink->dpipe_table_list));
5563         WARN_ON(!list_empty(&devlink->sb_list));
5564         WARN_ON(!list_empty(&devlink->port_list));
5565
5566         kfree(devlink);
5567 }
5568 EXPORT_SYMBOL_GPL(devlink_free);
5569
5570 static void devlink_port_type_warn(struct work_struct *work)
5571 {
5572         WARN(true, "Type was not set for devlink port.");
5573 }
5574
5575 static bool devlink_port_type_should_warn(struct devlink_port *devlink_port)
5576 {
5577         /* Ignore CPU and DSA flavours. */
5578         return devlink_port->attrs.flavour != DEVLINK_PORT_FLAVOUR_CPU &&
5579                devlink_port->attrs.flavour != DEVLINK_PORT_FLAVOUR_DSA;
5580 }
5581
5582 #define DEVLINK_PORT_TYPE_WARN_TIMEOUT (HZ * 30)
5583
5584 static void devlink_port_type_warn_schedule(struct devlink_port *devlink_port)
5585 {
5586         if (!devlink_port_type_should_warn(devlink_port))
5587                 return;
5588         /* Schedule a work to WARN in case driver does not set port
5589          * type within timeout.
5590          */
5591         schedule_delayed_work(&devlink_port->type_warn_dw,
5592                               DEVLINK_PORT_TYPE_WARN_TIMEOUT);
5593 }
5594
5595 static void devlink_port_type_warn_cancel(struct devlink_port *devlink_port)
5596 {
5597         if (!devlink_port_type_should_warn(devlink_port))
5598                 return;
5599         cancel_delayed_work_sync(&devlink_port->type_warn_dw);
5600 }
5601
5602 /**
5603  *      devlink_port_register - Register devlink port
5604  *
5605  *      @devlink: devlink
5606  *      @devlink_port: devlink port
5607  *      @port_index: driver-specific numerical identifier of the port
5608  *
5609  *      Register devlink port with provided port index. User can use
5610  *      any indexing, even hw-related one. devlink_port structure
5611  *      is convenient to be embedded inside user driver private structure.
5612  *      Note that the caller should take care of zeroing the devlink_port
5613  *      structure.
5614  */
5615 int devlink_port_register(struct devlink *devlink,
5616                           struct devlink_port *devlink_port,
5617                           unsigned int port_index)
5618 {
5619         mutex_lock(&devlink->lock);
5620         if (devlink_port_index_exists(devlink, port_index)) {
5621                 mutex_unlock(&devlink->lock);
5622                 return -EEXIST;
5623         }
5624         devlink_port->devlink = devlink;
5625         devlink_port->index = port_index;
5626         devlink_port->registered = true;
5627         spin_lock_init(&devlink_port->type_lock);
5628         list_add_tail(&devlink_port->list, &devlink->port_list);
5629         INIT_LIST_HEAD(&devlink_port->param_list);
5630         mutex_unlock(&devlink->lock);
5631         INIT_DELAYED_WORK(&devlink_port->type_warn_dw, &devlink_port_type_warn);
5632         devlink_port_type_warn_schedule(devlink_port);
5633         devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW);
5634         return 0;
5635 }
5636 EXPORT_SYMBOL_GPL(devlink_port_register);
5637
5638 /**
5639  *      devlink_port_unregister - Unregister devlink port
5640  *
5641  *      @devlink_port: devlink port
5642  */
5643 void devlink_port_unregister(struct devlink_port *devlink_port)
5644 {
5645         struct devlink *devlink = devlink_port->devlink;
5646
5647         devlink_port_type_warn_cancel(devlink_port);
5648         devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_DEL);
5649         mutex_lock(&devlink->lock);
5650         list_del(&devlink_port->list);
5651         mutex_unlock(&devlink->lock);
5652 }
5653 EXPORT_SYMBOL_GPL(devlink_port_unregister);
5654
5655 static void __devlink_port_type_set(struct devlink_port *devlink_port,
5656                                     enum devlink_port_type type,
5657                                     void *type_dev)
5658 {
5659         if (WARN_ON(!devlink_port->registered))
5660                 return;
5661         devlink_port_type_warn_cancel(devlink_port);
5662         spin_lock(&devlink_port->type_lock);
5663         devlink_port->type = type;
5664         devlink_port->type_dev = type_dev;
5665         spin_unlock(&devlink_port->type_lock);
5666         devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW);
5667 }
5668
5669 /**
5670  *      devlink_port_type_eth_set - Set port type to Ethernet
5671  *
5672  *      @devlink_port: devlink port
5673  *      @netdev: related netdevice
5674  */
5675 void devlink_port_type_eth_set(struct devlink_port *devlink_port,
5676                                struct net_device *netdev)
5677 {
5678         const struct net_device_ops *ops = netdev->netdev_ops;
5679
5680         /* If driver registers devlink port, it should set devlink port
5681          * attributes accordingly so the compat functions are called
5682          * and the original ops are not used.
5683          */
5684         if (ops->ndo_get_phys_port_name) {
5685                 /* Some drivers use the same set of ndos for netdevs
5686                  * that have devlink_port registered and also for
5687                  * those who don't. Make sure that ndo_get_phys_port_name
5688                  * returns -EOPNOTSUPP here in case it is defined.
5689                  * Warn if not.
5690                  */
5691                 char name[IFNAMSIZ];
5692                 int err;
5693
5694                 err = ops->ndo_get_phys_port_name(netdev, name, sizeof(name));
5695                 WARN_ON(err != -EOPNOTSUPP);
5696         }
5697         if (ops->ndo_get_port_parent_id) {
5698                 /* Some drivers use the same set of ndos for netdevs
5699                  * that have devlink_port registered and also for
5700                  * those who don't. Make sure that ndo_get_port_parent_id
5701                  * returns -EOPNOTSUPP here in case it is defined.
5702                  * Warn if not.
5703                  */
5704                 struct netdev_phys_item_id ppid;
5705                 int err;
5706
5707                 err = ops->ndo_get_port_parent_id(netdev, &ppid);
5708                 WARN_ON(err != -EOPNOTSUPP);
5709         }
5710         __devlink_port_type_set(devlink_port, DEVLINK_PORT_TYPE_ETH, netdev);
5711 }
5712 EXPORT_SYMBOL_GPL(devlink_port_type_eth_set);
5713
5714 /**
5715  *      devlink_port_type_ib_set - Set port type to InfiniBand
5716  *
5717  *      @devlink_port: devlink port
5718  *      @ibdev: related IB device
5719  */
5720 void devlink_port_type_ib_set(struct devlink_port *devlink_port,
5721                               struct ib_device *ibdev)
5722 {
5723         __devlink_port_type_set(devlink_port, DEVLINK_PORT_TYPE_IB, ibdev);
5724 }
5725 EXPORT_SYMBOL_GPL(devlink_port_type_ib_set);
5726
5727 /**
5728  *      devlink_port_type_clear - Clear port type
5729  *
5730  *      @devlink_port: devlink port
5731  */
5732 void devlink_port_type_clear(struct devlink_port *devlink_port)
5733 {
5734         __devlink_port_type_set(devlink_port, DEVLINK_PORT_TYPE_NOTSET, NULL);
5735         devlink_port_type_warn_schedule(devlink_port);
5736 }
5737 EXPORT_SYMBOL_GPL(devlink_port_type_clear);
5738
5739 /**
5740  *      devlink_port_attrs_set - Set port attributes
5741  *
5742  *      @devlink_port: devlink port
5743  *      @flavour: flavour of the port
5744  *      @port_number: number of the port that is facing user, for example
5745  *                    the front panel port number
5746  *      @split: indicates if this is split port
5747  *      @split_subport_number: if the port is split, this is the number
5748  *                             of subport.
5749  *      @switch_id: if the port is part of switch, this is buffer with ID,
5750  *                  otwerwise this is NULL
5751  *      @switch_id_len: length of the switch_id buffer
5752  */
5753 void devlink_port_attrs_set(struct devlink_port *devlink_port,
5754                             enum devlink_port_flavour flavour,
5755                             u32 port_number, bool split,
5756                             u32 split_subport_number,
5757                             const unsigned char *switch_id,
5758                             unsigned char switch_id_len)
5759 {
5760         struct devlink_port_attrs *attrs = &devlink_port->attrs;
5761
5762         if (WARN_ON(devlink_port->registered))
5763                 return;
5764         attrs->set = true;
5765         attrs->flavour = flavour;
5766         attrs->port_number = port_number;
5767         attrs->split = split;
5768         attrs->split_subport_number = split_subport_number;
5769         if (switch_id) {
5770                 attrs->switch_port = true;
5771                 if (WARN_ON(switch_id_len > MAX_PHYS_ITEM_ID_LEN))
5772                         switch_id_len = MAX_PHYS_ITEM_ID_LEN;
5773                 memcpy(attrs->switch_id.id, switch_id, switch_id_len);
5774                 attrs->switch_id.id_len = switch_id_len;
5775         } else {
5776                 attrs->switch_port = false;
5777         }
5778 }
5779 EXPORT_SYMBOL_GPL(devlink_port_attrs_set);
5780
5781 static int __devlink_port_phys_port_name_get(struct devlink_port *devlink_port,
5782                                              char *name, size_t len)
5783 {
5784         struct devlink_port_attrs *attrs = &devlink_port->attrs;
5785         int n = 0;
5786
5787         if (!attrs->set)
5788                 return -EOPNOTSUPP;
5789
5790         switch (attrs->flavour) {
5791         case DEVLINK_PORT_FLAVOUR_PHYSICAL:
5792                 if (!attrs->split)
5793                         n = snprintf(name, len, "p%u", attrs->port_number);
5794                 else
5795                         n = snprintf(name, len, "p%us%u", attrs->port_number,
5796                                      attrs->split_subport_number);
5797                 break;
5798         case DEVLINK_PORT_FLAVOUR_CPU:
5799         case DEVLINK_PORT_FLAVOUR_DSA:
5800                 /* As CPU and DSA ports do not have a netdevice associated
5801                  * case should not ever happen.
5802                  */
5803                 WARN_ON(1);
5804                 return -EINVAL;
5805         }
5806
5807         if (n >= len)
5808                 return -EINVAL;
5809
5810         return 0;
5811 }
5812
5813 int devlink_sb_register(struct devlink *devlink, unsigned int sb_index,
5814                         u32 size, u16 ingress_pools_count,
5815                         u16 egress_pools_count, u16 ingress_tc_count,
5816                         u16 egress_tc_count)
5817 {
5818         struct devlink_sb *devlink_sb;
5819         int err = 0;
5820
5821         mutex_lock(&devlink->lock);
5822         if (devlink_sb_index_exists(devlink, sb_index)) {
5823                 err = -EEXIST;
5824                 goto unlock;
5825         }
5826
5827         devlink_sb = kzalloc(sizeof(*devlink_sb), GFP_KERNEL);
5828         if (!devlink_sb) {
5829                 err = -ENOMEM;
5830                 goto unlock;
5831         }
5832         devlink_sb->index = sb_index;
5833         devlink_sb->size = size;
5834         devlink_sb->ingress_pools_count = ingress_pools_count;
5835         devlink_sb->egress_pools_count = egress_pools_count;
5836         devlink_sb->ingress_tc_count = ingress_tc_count;
5837         devlink_sb->egress_tc_count = egress_tc_count;
5838         list_add_tail(&devlink_sb->list, &devlink->sb_list);
5839 unlock:
5840         mutex_unlock(&devlink->lock);
5841         return err;
5842 }
5843 EXPORT_SYMBOL_GPL(devlink_sb_register);
5844
5845 void devlink_sb_unregister(struct devlink *devlink, unsigned int sb_index)
5846 {
5847         struct devlink_sb *devlink_sb;
5848
5849         mutex_lock(&devlink->lock);
5850         devlink_sb = devlink_sb_get_by_index(devlink, sb_index);
5851         WARN_ON(!devlink_sb);
5852         list_del(&devlink_sb->list);
5853         mutex_unlock(&devlink->lock);
5854         kfree(devlink_sb);
5855 }
5856 EXPORT_SYMBOL_GPL(devlink_sb_unregister);
5857
5858 /**
5859  *      devlink_dpipe_headers_register - register dpipe headers
5860  *
5861  *      @devlink: devlink
5862  *      @dpipe_headers: dpipe header array
5863  *
5864  *      Register the headers supported by hardware.
5865  */
5866 int devlink_dpipe_headers_register(struct devlink *devlink,
5867                                    struct devlink_dpipe_headers *dpipe_headers)
5868 {
5869         mutex_lock(&devlink->lock);
5870         devlink->dpipe_headers = dpipe_headers;
5871         mutex_unlock(&devlink->lock);
5872         return 0;
5873 }
5874 EXPORT_SYMBOL_GPL(devlink_dpipe_headers_register);
5875
5876 /**
5877  *      devlink_dpipe_headers_unregister - unregister dpipe headers
5878  *
5879  *      @devlink: devlink
5880  *
5881  *      Unregister the headers supported by hardware.
5882  */
5883 void devlink_dpipe_headers_unregister(struct devlink *devlink)
5884 {
5885         mutex_lock(&devlink->lock);
5886         devlink->dpipe_headers = NULL;
5887         mutex_unlock(&devlink->lock);
5888 }
5889 EXPORT_SYMBOL_GPL(devlink_dpipe_headers_unregister);
5890
5891 /**
5892  *      devlink_dpipe_table_counter_enabled - check if counter allocation
5893  *                                            required
5894  *      @devlink: devlink
5895  *      @table_name: tables name
5896  *
5897  *      Used by driver to check if counter allocation is required.
5898  *      After counter allocation is turned on the table entries
5899  *      are updated to include counter statistics.
5900  *
5901  *      After that point on the driver must respect the counter
5902  *      state so that each entry added to the table is added
5903  *      with a counter.
5904  */
5905 bool devlink_dpipe_table_counter_enabled(struct devlink *devlink,
5906                                          const char *table_name)
5907 {
5908         struct devlink_dpipe_table *table;
5909         bool enabled;
5910
5911         rcu_read_lock();
5912         table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
5913                                          table_name);
5914         enabled = false;
5915         if (table)
5916                 enabled = table->counters_enabled;
5917         rcu_read_unlock();
5918         return enabled;
5919 }
5920 EXPORT_SYMBOL_GPL(devlink_dpipe_table_counter_enabled);
5921
5922 /**
5923  *      devlink_dpipe_table_register - register dpipe table
5924  *
5925  *      @devlink: devlink
5926  *      @table_name: table name
5927  *      @table_ops: table ops
5928  *      @priv: priv
5929  *      @counter_control_extern: external control for counters
5930  */
5931 int devlink_dpipe_table_register(struct devlink *devlink,
5932                                  const char *table_name,
5933                                  struct devlink_dpipe_table_ops *table_ops,
5934                                  void *priv, bool counter_control_extern)
5935 {
5936         struct devlink_dpipe_table *table;
5937
5938         if (devlink_dpipe_table_find(&devlink->dpipe_table_list, table_name))
5939                 return -EEXIST;
5940
5941         if (WARN_ON(!table_ops->size_get))
5942                 return -EINVAL;
5943
5944         table = kzalloc(sizeof(*table), GFP_KERNEL);
5945         if (!table)
5946                 return -ENOMEM;
5947
5948         table->name = table_name;
5949         table->table_ops = table_ops;
5950         table->priv = priv;
5951         table->counter_control_extern = counter_control_extern;
5952
5953         mutex_lock(&devlink->lock);
5954         list_add_tail_rcu(&table->list, &devlink->dpipe_table_list);
5955         mutex_unlock(&devlink->lock);
5956         return 0;
5957 }
5958 EXPORT_SYMBOL_GPL(devlink_dpipe_table_register);
5959
5960 /**
5961  *      devlink_dpipe_table_unregister - unregister dpipe table
5962  *
5963  *      @devlink: devlink
5964  *      @table_name: table name
5965  */
5966 void devlink_dpipe_table_unregister(struct devlink *devlink,
5967                                     const char *table_name)
5968 {
5969         struct devlink_dpipe_table *table;
5970
5971         mutex_lock(&devlink->lock);
5972         table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
5973                                          table_name);
5974         if (!table)
5975                 goto unlock;
5976         list_del_rcu(&table->list);
5977         mutex_unlock(&devlink->lock);
5978         kfree_rcu(table, rcu);
5979         return;
5980 unlock:
5981         mutex_unlock(&devlink->lock);
5982 }
5983 EXPORT_SYMBOL_GPL(devlink_dpipe_table_unregister);
5984
5985 /**
5986  *      devlink_resource_register - devlink resource register
5987  *
5988  *      @devlink: devlink
5989  *      @resource_name: resource's name
5990  *      @resource_size: resource's size
5991  *      @resource_id: resource's id
5992  *      @parent_resource_id: resource's parent id
5993  *      @size_params: size parameters
5994  */
5995 int devlink_resource_register(struct devlink *devlink,
5996                               const char *resource_name,
5997                               u64 resource_size,
5998                               u64 resource_id,
5999                               u64 parent_resource_id,
6000                               const struct devlink_resource_size_params *size_params)
6001 {
6002         struct devlink_resource *resource;
6003         struct list_head *resource_list;
6004         bool top_hierarchy;
6005         int err = 0;
6006
6007         top_hierarchy = parent_resource_id == DEVLINK_RESOURCE_ID_PARENT_TOP;
6008
6009         mutex_lock(&devlink->lock);
6010         resource = devlink_resource_find(devlink, NULL, resource_id);
6011         if (resource) {
6012                 err = -EINVAL;
6013                 goto out;
6014         }
6015
6016         resource = kzalloc(sizeof(*resource), GFP_KERNEL);
6017         if (!resource) {
6018                 err = -ENOMEM;
6019                 goto out;
6020         }
6021
6022         if (top_hierarchy) {
6023                 resource_list = &devlink->resource_list;
6024         } else {
6025                 struct devlink_resource *parent_resource;
6026
6027                 parent_resource = devlink_resource_find(devlink, NULL,
6028                                                         parent_resource_id);
6029                 if (parent_resource) {
6030                         resource_list = &parent_resource->resource_list;
6031                         resource->parent = parent_resource;
6032                 } else {
6033                         kfree(resource);
6034                         err = -EINVAL;
6035                         goto out;
6036                 }
6037         }
6038
6039         resource->name = resource_name;
6040         resource->size = resource_size;
6041         resource->size_new = resource_size;
6042         resource->id = resource_id;
6043         resource->size_valid = true;
6044         memcpy(&resource->size_params, size_params,
6045                sizeof(resource->size_params));
6046         INIT_LIST_HEAD(&resource->resource_list);
6047         list_add_tail(&resource->list, resource_list);
6048 out:
6049         mutex_unlock(&devlink->lock);
6050         return err;
6051 }
6052 EXPORT_SYMBOL_GPL(devlink_resource_register);
6053
6054 /**
6055  *      devlink_resources_unregister - free all resources
6056  *
6057  *      @devlink: devlink
6058  *      @resource: resource
6059  */
6060 void devlink_resources_unregister(struct devlink *devlink,
6061                                   struct devlink_resource *resource)
6062 {
6063         struct devlink_resource *tmp, *child_resource;
6064         struct list_head *resource_list;
6065
6066         if (resource)
6067                 resource_list = &resource->resource_list;
6068         else
6069                 resource_list = &devlink->resource_list;
6070
6071         if (!resource)
6072                 mutex_lock(&devlink->lock);
6073
6074         list_for_each_entry_safe(child_resource, tmp, resource_list, list) {
6075                 devlink_resources_unregister(devlink, child_resource);
6076                 list_del(&child_resource->list);
6077                 kfree(child_resource);
6078         }
6079
6080         if (!resource)
6081                 mutex_unlock(&devlink->lock);
6082 }
6083 EXPORT_SYMBOL_GPL(devlink_resources_unregister);
6084
6085 /**
6086  *      devlink_resource_size_get - get and update size
6087  *
6088  *      @devlink: devlink
6089  *      @resource_id: the requested resource id
6090  *      @p_resource_size: ptr to update
6091  */
6092 int devlink_resource_size_get(struct devlink *devlink,
6093                               u64 resource_id,
6094                               u64 *p_resource_size)
6095 {
6096         struct devlink_resource *resource;
6097         int err = 0;
6098
6099         mutex_lock(&devlink->lock);
6100         resource = devlink_resource_find(devlink, NULL, resource_id);
6101         if (!resource) {
6102                 err = -EINVAL;
6103                 goto out;
6104         }
6105         *p_resource_size = resource->size_new;
6106         resource->size = resource->size_new;
6107 out:
6108         mutex_unlock(&devlink->lock);
6109         return err;
6110 }
6111 EXPORT_SYMBOL_GPL(devlink_resource_size_get);
6112
6113 /**
6114  *      devlink_dpipe_table_resource_set - set the resource id
6115  *
6116  *      @devlink: devlink
6117  *      @table_name: table name
6118  *      @resource_id: resource id
6119  *      @resource_units: number of resource's units consumed per table's entry
6120  */
6121 int devlink_dpipe_table_resource_set(struct devlink *devlink,
6122                                      const char *table_name, u64 resource_id,
6123                                      u64 resource_units)
6124 {
6125         struct devlink_dpipe_table *table;
6126         int err = 0;
6127
6128         mutex_lock(&devlink->lock);
6129         table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
6130                                          table_name);
6131         if (!table) {
6132                 err = -EINVAL;
6133                 goto out;
6134         }
6135         table->resource_id = resource_id;
6136         table->resource_units = resource_units;
6137         table->resource_valid = true;
6138 out:
6139         mutex_unlock(&devlink->lock);
6140         return err;
6141 }
6142 EXPORT_SYMBOL_GPL(devlink_dpipe_table_resource_set);
6143
6144 /**
6145  *      devlink_resource_occ_get_register - register occupancy getter
6146  *
6147  *      @devlink: devlink
6148  *      @resource_id: resource id
6149  *      @occ_get: occupancy getter callback
6150  *      @occ_get_priv: occupancy getter callback priv
6151  */
6152 void devlink_resource_occ_get_register(struct devlink *devlink,
6153                                        u64 resource_id,
6154                                        devlink_resource_occ_get_t *occ_get,
6155                                        void *occ_get_priv)
6156 {
6157         struct devlink_resource *resource;
6158
6159         mutex_lock(&devlink->lock);
6160         resource = devlink_resource_find(devlink, NULL, resource_id);
6161         if (WARN_ON(!resource))
6162                 goto out;
6163         WARN_ON(resource->occ_get);
6164
6165         resource->occ_get = occ_get;
6166         resource->occ_get_priv = occ_get_priv;
6167 out:
6168         mutex_unlock(&devlink->lock);
6169 }
6170 EXPORT_SYMBOL_GPL(devlink_resource_occ_get_register);
6171
6172 /**
6173  *      devlink_resource_occ_get_unregister - unregister occupancy getter
6174  *
6175  *      @devlink: devlink
6176  *      @resource_id: resource id
6177  */
6178 void devlink_resource_occ_get_unregister(struct devlink *devlink,
6179                                          u64 resource_id)
6180 {
6181         struct devlink_resource *resource;
6182
6183         mutex_lock(&devlink->lock);
6184         resource = devlink_resource_find(devlink, NULL, resource_id);
6185         if (WARN_ON(!resource))
6186                 goto out;
6187         WARN_ON(!resource->occ_get);
6188
6189         resource->occ_get = NULL;
6190         resource->occ_get_priv = NULL;
6191 out:
6192         mutex_unlock(&devlink->lock);
6193 }
6194 EXPORT_SYMBOL_GPL(devlink_resource_occ_get_unregister);
6195
6196 static int devlink_param_verify(const struct devlink_param *param)
6197 {
6198         if (!param || !param->name || !param->supported_cmodes)
6199                 return -EINVAL;
6200         if (param->generic)
6201                 return devlink_param_generic_verify(param);
6202         else
6203                 return devlink_param_driver_verify(param);
6204 }
6205
6206 static int __devlink_params_register(struct devlink *devlink,
6207                                      unsigned int port_index,
6208                                      struct list_head *param_list,
6209                                      const struct devlink_param *params,
6210                                      size_t params_count,
6211                                      enum devlink_command reg_cmd,
6212                                      enum devlink_command unreg_cmd)
6213 {
6214         const struct devlink_param *param = params;
6215         int i;
6216         int err;
6217
6218         mutex_lock(&devlink->lock);
6219         for (i = 0; i < params_count; i++, param++) {
6220                 err = devlink_param_verify(param);
6221                 if (err)
6222                         goto rollback;
6223
6224                 err = devlink_param_register_one(devlink, port_index,
6225                                                  param_list, param, reg_cmd);
6226                 if (err)
6227                         goto rollback;
6228         }
6229
6230         mutex_unlock(&devlink->lock);
6231         return 0;
6232
6233 rollback:
6234         if (!i)
6235                 goto unlock;
6236         for (param--; i > 0; i--, param--)
6237                 devlink_param_unregister_one(devlink, port_index, param_list,
6238                                              param, unreg_cmd);
6239 unlock:
6240         mutex_unlock(&devlink->lock);
6241         return err;
6242 }
6243
6244 static void __devlink_params_unregister(struct devlink *devlink,
6245                                         unsigned int port_index,
6246                                         struct list_head *param_list,
6247                                         const struct devlink_param *params,
6248                                         size_t params_count,
6249                                         enum devlink_command cmd)
6250 {
6251         const struct devlink_param *param = params;
6252         int i;
6253
6254         mutex_lock(&devlink->lock);
6255         for (i = 0; i < params_count; i++, param++)
6256                 devlink_param_unregister_one(devlink, 0, param_list, param,
6257                                              cmd);
6258         mutex_unlock(&devlink->lock);
6259 }
6260
6261 /**
6262  *      devlink_params_register - register configuration parameters
6263  *
6264  *      @devlink: devlink
6265  *      @params: configuration parameters array
6266  *      @params_count: number of parameters provided
6267  *
6268  *      Register the configuration parameters supported by the driver.
6269  */
6270 int devlink_params_register(struct devlink *devlink,
6271                             const struct devlink_param *params,
6272                             size_t params_count)
6273 {
6274         return __devlink_params_register(devlink, 0, &devlink->param_list,
6275                                          params, params_count,
6276                                          DEVLINK_CMD_PARAM_NEW,
6277                                          DEVLINK_CMD_PARAM_DEL);
6278 }
6279 EXPORT_SYMBOL_GPL(devlink_params_register);
6280
6281 /**
6282  *      devlink_params_unregister - unregister configuration parameters
6283  *      @devlink: devlink
6284  *      @params: configuration parameters to unregister
6285  *      @params_count: number of parameters provided
6286  */
6287 void devlink_params_unregister(struct devlink *devlink,
6288                                const struct devlink_param *params,
6289                                size_t params_count)
6290 {
6291         return __devlink_params_unregister(devlink, 0, &devlink->param_list,
6292                                            params, params_count,
6293                                            DEVLINK_CMD_PARAM_DEL);
6294 }
6295 EXPORT_SYMBOL_GPL(devlink_params_unregister);
6296
6297 /**
6298  *      devlink_params_publish - publish configuration parameters
6299  *
6300  *      @devlink: devlink
6301  *
6302  *      Publish previously registered configuration parameters.
6303  */
6304 void devlink_params_publish(struct devlink *devlink)
6305 {
6306         struct devlink_param_item *param_item;
6307
6308         list_for_each_entry(param_item, &devlink->param_list, list) {
6309                 if (param_item->published)
6310                         continue;
6311                 param_item->published = true;
6312                 devlink_param_notify(devlink, 0, param_item,
6313                                      DEVLINK_CMD_PARAM_NEW);
6314         }
6315 }
6316 EXPORT_SYMBOL_GPL(devlink_params_publish);
6317
6318 /**
6319  *      devlink_params_unpublish - unpublish configuration parameters
6320  *
6321  *      @devlink: devlink
6322  *
6323  *      Unpublish previously registered configuration parameters.
6324  */
6325 void devlink_params_unpublish(struct devlink *devlink)
6326 {
6327         struct devlink_param_item *param_item;
6328
6329         list_for_each_entry(param_item, &devlink->param_list, list) {
6330                 if (!param_item->published)
6331                         continue;
6332                 param_item->published = false;
6333                 devlink_param_notify(devlink, 0, param_item,
6334                                      DEVLINK_CMD_PARAM_DEL);
6335         }
6336 }
6337 EXPORT_SYMBOL_GPL(devlink_params_unpublish);
6338
6339 /**
6340  *      devlink_port_params_register - register port configuration parameters
6341  *
6342  *      @devlink_port: devlink port
6343  *      @params: configuration parameters array
6344  *      @params_count: number of parameters provided
6345  *
6346  *      Register the configuration parameters supported by the port.
6347  */
6348 int devlink_port_params_register(struct devlink_port *devlink_port,
6349                                  const struct devlink_param *params,
6350                                  size_t params_count)
6351 {
6352         return __devlink_params_register(devlink_port->devlink,
6353                                          devlink_port->index,
6354                                          &devlink_port->param_list, params,
6355                                          params_count,
6356                                          DEVLINK_CMD_PORT_PARAM_NEW,
6357                                          DEVLINK_CMD_PORT_PARAM_DEL);
6358 }
6359 EXPORT_SYMBOL_GPL(devlink_port_params_register);
6360
6361 /**
6362  *      devlink_port_params_unregister - unregister port configuration
6363  *      parameters
6364  *
6365  *      @devlink_port: devlink port
6366  *      @params: configuration parameters array
6367  *      @params_count: number of parameters provided
6368  */
6369 void devlink_port_params_unregister(struct devlink_port *devlink_port,
6370                                     const struct devlink_param *params,
6371                                     size_t params_count)
6372 {
6373         return __devlink_params_unregister(devlink_port->devlink,
6374                                            devlink_port->index,
6375                                            &devlink_port->param_list,
6376                                            params, params_count,
6377                                            DEVLINK_CMD_PORT_PARAM_DEL);
6378 }
6379 EXPORT_SYMBOL_GPL(devlink_port_params_unregister);
6380
6381 static int
6382 __devlink_param_driverinit_value_get(struct list_head *param_list, u32 param_id,
6383                                      union devlink_param_value *init_val)
6384 {
6385         struct devlink_param_item *param_item;
6386
6387         param_item = devlink_param_find_by_id(param_list, param_id);
6388         if (!param_item)
6389                 return -EINVAL;
6390
6391         if (!param_item->driverinit_value_valid ||
6392             !devlink_param_cmode_is_supported(param_item->param,
6393                                               DEVLINK_PARAM_CMODE_DRIVERINIT))
6394                 return -EOPNOTSUPP;
6395
6396         if (param_item->param->type == DEVLINK_PARAM_TYPE_STRING)
6397                 strcpy(init_val->vstr, param_item->driverinit_value.vstr);
6398         else
6399                 *init_val = param_item->driverinit_value;
6400
6401         return 0;
6402 }
6403
6404 static int
6405 __devlink_param_driverinit_value_set(struct devlink *devlink,
6406                                      unsigned int port_index,
6407                                      struct list_head *param_list, u32 param_id,
6408                                      union devlink_param_value init_val,
6409                                      enum devlink_command cmd)
6410 {
6411         struct devlink_param_item *param_item;
6412
6413         param_item = devlink_param_find_by_id(param_list, param_id);
6414         if (!param_item)
6415                 return -EINVAL;
6416
6417         if (!devlink_param_cmode_is_supported(param_item->param,
6418                                               DEVLINK_PARAM_CMODE_DRIVERINIT))
6419                 return -EOPNOTSUPP;
6420
6421         if (param_item->param->type == DEVLINK_PARAM_TYPE_STRING)
6422                 strcpy(param_item->driverinit_value.vstr, init_val.vstr);
6423         else
6424                 param_item->driverinit_value = init_val;
6425         param_item->driverinit_value_valid = true;
6426
6427         devlink_param_notify(devlink, port_index, param_item, cmd);
6428         return 0;
6429 }
6430
6431 /**
6432  *      devlink_param_driverinit_value_get - get configuration parameter
6433  *                                           value for driver initializing
6434  *
6435  *      @devlink: devlink
6436  *      @param_id: parameter ID
6437  *      @init_val: value of parameter in driverinit configuration mode
6438  *
6439  *      This function should be used by the driver to get driverinit
6440  *      configuration for initialization after reload command.
6441  */
6442 int devlink_param_driverinit_value_get(struct devlink *devlink, u32 param_id,
6443                                        union devlink_param_value *init_val)
6444 {
6445         if (!devlink->ops->reload)
6446                 return -EOPNOTSUPP;
6447
6448         return __devlink_param_driverinit_value_get(&devlink->param_list,
6449                                                     param_id, init_val);
6450 }
6451 EXPORT_SYMBOL_GPL(devlink_param_driverinit_value_get);
6452
6453 /**
6454  *      devlink_param_driverinit_value_set - set value of configuration
6455  *                                           parameter for driverinit
6456  *                                           configuration mode
6457  *
6458  *      @devlink: devlink
6459  *      @param_id: parameter ID
6460  *      @init_val: value of parameter to set for driverinit configuration mode
6461  *
6462  *      This function should be used by the driver to set driverinit
6463  *      configuration mode default value.
6464  */
6465 int devlink_param_driverinit_value_set(struct devlink *devlink, u32 param_id,
6466                                        union devlink_param_value init_val)
6467 {
6468         return __devlink_param_driverinit_value_set(devlink, 0,
6469                                                     &devlink->param_list,
6470                                                     param_id, init_val,
6471                                                     DEVLINK_CMD_PARAM_NEW);
6472 }
6473 EXPORT_SYMBOL_GPL(devlink_param_driverinit_value_set);
6474
6475 /**
6476  *      devlink_port_param_driverinit_value_get - get configuration parameter
6477  *                                              value for driver initializing
6478  *
6479  *      @devlink_port: devlink_port
6480  *      @param_id: parameter ID
6481  *      @init_val: value of parameter in driverinit configuration mode
6482  *
6483  *      This function should be used by the driver to get driverinit
6484  *      configuration for initialization after reload command.
6485  */
6486 int devlink_port_param_driverinit_value_get(struct devlink_port *devlink_port,
6487                                             u32 param_id,
6488                                             union devlink_param_value *init_val)
6489 {
6490         struct devlink *devlink = devlink_port->devlink;
6491
6492         if (!devlink->ops->reload)
6493                 return -EOPNOTSUPP;
6494
6495         return __devlink_param_driverinit_value_get(&devlink_port->param_list,
6496                                                     param_id, init_val);
6497 }
6498 EXPORT_SYMBOL_GPL(devlink_port_param_driverinit_value_get);
6499
6500 /**
6501  *     devlink_port_param_driverinit_value_set - set value of configuration
6502  *                                               parameter for driverinit
6503  *                                               configuration mode
6504  *
6505  *     @devlink_port: devlink_port
6506  *     @param_id: parameter ID
6507  *     @init_val: value of parameter to set for driverinit configuration mode
6508  *
6509  *     This function should be used by the driver to set driverinit
6510  *     configuration mode default value.
6511  */
6512 int devlink_port_param_driverinit_value_set(struct devlink_port *devlink_port,
6513                                             u32 param_id,
6514                                             union devlink_param_value init_val)
6515 {
6516         return __devlink_param_driverinit_value_set(devlink_port->devlink,
6517                                                     devlink_port->index,
6518                                                     &devlink_port->param_list,
6519                                                     param_id, init_val,
6520                                                     DEVLINK_CMD_PORT_PARAM_NEW);
6521 }
6522 EXPORT_SYMBOL_GPL(devlink_port_param_driverinit_value_set);
6523
6524 /**
6525  *      devlink_param_value_changed - notify devlink on a parameter's value
6526  *                                    change. Should be called by the driver
6527  *                                    right after the change.
6528  *
6529  *      @devlink: devlink
6530  *      @param_id: parameter ID
6531  *
6532  *      This function should be used by the driver to notify devlink on value
6533  *      change, excluding driverinit configuration mode.
6534  *      For driverinit configuration mode driver should use the function
6535  */
6536 void devlink_param_value_changed(struct devlink *devlink, u32 param_id)
6537 {
6538         struct devlink_param_item *param_item;
6539
6540         param_item = devlink_param_find_by_id(&devlink->param_list, param_id);
6541         WARN_ON(!param_item);
6542
6543         devlink_param_notify(devlink, 0, param_item, DEVLINK_CMD_PARAM_NEW);
6544 }
6545 EXPORT_SYMBOL_GPL(devlink_param_value_changed);
6546
6547 /**
6548  *     devlink_port_param_value_changed - notify devlink on a parameter's value
6549  *                                      change. Should be called by the driver
6550  *                                      right after the change.
6551  *
6552  *     @devlink_port: devlink_port
6553  *     @param_id: parameter ID
6554  *
6555  *     This function should be used by the driver to notify devlink on value
6556  *     change, excluding driverinit configuration mode.
6557  *     For driverinit configuration mode driver should use the function
6558  *     devlink_port_param_driverinit_value_set() instead.
6559  */
6560 void devlink_port_param_value_changed(struct devlink_port *devlink_port,
6561                                       u32 param_id)
6562 {
6563         struct devlink_param_item *param_item;
6564
6565         param_item = devlink_param_find_by_id(&devlink_port->param_list,
6566                                               param_id);
6567         WARN_ON(!param_item);
6568
6569         devlink_param_notify(devlink_port->devlink, devlink_port->index,
6570                              param_item, DEVLINK_CMD_PORT_PARAM_NEW);
6571 }
6572 EXPORT_SYMBOL_GPL(devlink_port_param_value_changed);
6573
6574 /**
6575  *      devlink_param_value_str_fill - Safely fill-up the string preventing
6576  *                                     from overflow of the preallocated buffer
6577  *
6578  *      @dst_val: destination devlink_param_value
6579  *      @src: source buffer
6580  */
6581 void devlink_param_value_str_fill(union devlink_param_value *dst_val,
6582                                   const char *src)
6583 {
6584         size_t len;
6585
6586         len = strlcpy(dst_val->vstr, src, __DEVLINK_PARAM_MAX_STRING_VALUE);
6587         WARN_ON(len >= __DEVLINK_PARAM_MAX_STRING_VALUE);
6588 }
6589 EXPORT_SYMBOL_GPL(devlink_param_value_str_fill);
6590
6591 /**
6592  *      devlink_region_create - create a new address region
6593  *
6594  *      @devlink: devlink
6595  *      @region_name: region name
6596  *      @region_max_snapshots: Maximum supported number of snapshots for region
6597  *      @region_size: size of region
6598  */
6599 struct devlink_region *devlink_region_create(struct devlink *devlink,
6600                                              const char *region_name,
6601                                              u32 region_max_snapshots,
6602                                              u64 region_size)
6603 {
6604         struct devlink_region *region;
6605         int err = 0;
6606
6607         mutex_lock(&devlink->lock);
6608
6609         if (devlink_region_get_by_name(devlink, region_name)) {
6610                 err = -EEXIST;
6611                 goto unlock;
6612         }
6613
6614         region = kzalloc(sizeof(*region), GFP_KERNEL);
6615         if (!region) {
6616                 err = -ENOMEM;
6617                 goto unlock;
6618         }
6619
6620         region->devlink = devlink;
6621         region->max_snapshots = region_max_snapshots;
6622         region->name = region_name;
6623         region->size = region_size;
6624         INIT_LIST_HEAD(&region->snapshot_list);
6625         list_add_tail(&region->list, &devlink->region_list);
6626         devlink_nl_region_notify(region, NULL, DEVLINK_CMD_REGION_NEW);
6627
6628         mutex_unlock(&devlink->lock);
6629         return region;
6630
6631 unlock:
6632         mutex_unlock(&devlink->lock);
6633         return ERR_PTR(err);
6634 }
6635 EXPORT_SYMBOL_GPL(devlink_region_create);
6636
6637 /**
6638  *      devlink_region_destroy - destroy address region
6639  *
6640  *      @region: devlink region to destroy
6641  */
6642 void devlink_region_destroy(struct devlink_region *region)
6643 {
6644         struct devlink *devlink = region->devlink;
6645         struct devlink_snapshot *snapshot, *ts;
6646
6647         mutex_lock(&devlink->lock);
6648
6649         /* Free all snapshots of region */
6650         list_for_each_entry_safe(snapshot, ts, &region->snapshot_list, list)
6651                 devlink_region_snapshot_del(snapshot);
6652
6653         list_del(&region->list);
6654
6655         devlink_nl_region_notify(region, NULL, DEVLINK_CMD_REGION_DEL);
6656         mutex_unlock(&devlink->lock);
6657         kfree(region);
6658 }
6659 EXPORT_SYMBOL_GPL(devlink_region_destroy);
6660
6661 /**
6662  *      devlink_region_shapshot_id_get - get snapshot ID
6663  *
6664  *      This callback should be called when adding a new snapshot,
6665  *      Driver should use the same id for multiple snapshots taken
6666  *      on multiple regions at the same time/by the same trigger.
6667  *
6668  *      @devlink: devlink
6669  */
6670 u32 devlink_region_shapshot_id_get(struct devlink *devlink)
6671 {
6672         u32 id;
6673
6674         mutex_lock(&devlink->lock);
6675         id = ++devlink->snapshot_id;
6676         mutex_unlock(&devlink->lock);
6677
6678         return id;
6679 }
6680 EXPORT_SYMBOL_GPL(devlink_region_shapshot_id_get);
6681
6682 /**
6683  *      devlink_region_snapshot_create - create a new snapshot
6684  *      This will add a new snapshot of a region. The snapshot
6685  *      will be stored on the region struct and can be accessed
6686  *      from devlink. This is useful for future analyses of snapshots.
6687  *      Multiple snapshots can be created on a region.
6688  *      The @snapshot_id should be obtained using the getter function.
6689  *
6690  *      @region: devlink region of the snapshot
6691  *      @data_len: size of snapshot data
6692  *      @data: snapshot data
6693  *      @snapshot_id: snapshot id to be created
6694  *      @data_destructor: pointer to destructor function to free data
6695  */
6696 int devlink_region_snapshot_create(struct devlink_region *region, u64 data_len,
6697                                    u8 *data, u32 snapshot_id,
6698                                    devlink_snapshot_data_dest_t *data_destructor)
6699 {
6700         struct devlink *devlink = region->devlink;
6701         struct devlink_snapshot *snapshot;
6702         int err;
6703
6704         mutex_lock(&devlink->lock);
6705
6706         /* check if region can hold one more snapshot */
6707         if (region->cur_snapshots == region->max_snapshots) {
6708                 err = -ENOMEM;
6709                 goto unlock;
6710         }
6711
6712         if (devlink_region_snapshot_get_by_id(region, snapshot_id)) {
6713                 err = -EEXIST;
6714                 goto unlock;
6715         }
6716
6717         snapshot = kzalloc(sizeof(*snapshot), GFP_KERNEL);
6718         if (!snapshot) {
6719                 err = -ENOMEM;
6720                 goto unlock;
6721         }
6722
6723         snapshot->id = snapshot_id;
6724         snapshot->region = region;
6725         snapshot->data = data;
6726         snapshot->data_len = data_len;
6727         snapshot->data_destructor = data_destructor;
6728
6729         list_add_tail(&snapshot->list, &region->snapshot_list);
6730
6731         region->cur_snapshots++;
6732
6733         devlink_nl_region_notify(region, snapshot, DEVLINK_CMD_REGION_NEW);
6734         mutex_unlock(&devlink->lock);
6735         return 0;
6736
6737 unlock:
6738         mutex_unlock(&devlink->lock);
6739         return err;
6740 }
6741 EXPORT_SYMBOL_GPL(devlink_region_snapshot_create);
6742
6743 static void __devlink_compat_running_version(struct devlink *devlink,
6744                                              char *buf, size_t len)
6745 {
6746         const struct nlattr *nlattr;
6747         struct devlink_info_req req;
6748         struct sk_buff *msg;
6749         int rem, err;
6750
6751         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6752         if (!msg)
6753                 return;
6754
6755         req.msg = msg;
6756         err = devlink->ops->info_get(devlink, &req, NULL);
6757         if (err)
6758                 goto free_msg;
6759
6760         nla_for_each_attr(nlattr, (void *)msg->data, msg->len, rem) {
6761                 const struct nlattr *kv;
6762                 int rem_kv;
6763
6764                 if (nla_type(nlattr) != DEVLINK_ATTR_INFO_VERSION_RUNNING)
6765                         continue;
6766
6767                 nla_for_each_nested(kv, nlattr, rem_kv) {
6768                         if (nla_type(kv) != DEVLINK_ATTR_INFO_VERSION_VALUE)
6769                                 continue;
6770
6771                         strlcat(buf, nla_data(kv), len);
6772                         strlcat(buf, " ", len);
6773                 }
6774         }
6775 free_msg:
6776         nlmsg_free(msg);
6777 }
6778
6779 void devlink_compat_running_version(struct net_device *dev,
6780                                     char *buf, size_t len)
6781 {
6782         struct devlink *devlink;
6783
6784         dev_hold(dev);
6785         rtnl_unlock();
6786
6787         devlink = netdev_to_devlink(dev);
6788         if (!devlink || !devlink->ops->info_get)
6789                 goto out;
6790
6791         mutex_lock(&devlink->lock);
6792         __devlink_compat_running_version(devlink, buf, len);
6793         mutex_unlock(&devlink->lock);
6794
6795 out:
6796         rtnl_lock();
6797         dev_put(dev);
6798 }
6799
6800 int devlink_compat_flash_update(struct net_device *dev, const char *file_name)
6801 {
6802         struct devlink *devlink;
6803         int ret;
6804
6805         dev_hold(dev);
6806         rtnl_unlock();
6807
6808         devlink = netdev_to_devlink(dev);
6809         if (!devlink || !devlink->ops->flash_update) {
6810                 ret = -EOPNOTSUPP;
6811                 goto out;
6812         }
6813
6814         mutex_lock(&devlink->lock);
6815         ret = devlink->ops->flash_update(devlink, file_name, NULL, NULL);
6816         mutex_unlock(&devlink->lock);
6817
6818 out:
6819         rtnl_lock();
6820         dev_put(dev);
6821
6822         return ret;
6823 }
6824
6825 int devlink_compat_phys_port_name_get(struct net_device *dev,
6826                                       char *name, size_t len)
6827 {
6828         struct devlink_port *devlink_port;
6829
6830         /* RTNL mutex is held here which ensures that devlink_port
6831          * instance cannot disappear in the middle. No need to take
6832          * any devlink lock as only permanent values are accessed.
6833          */
6834         ASSERT_RTNL();
6835
6836         devlink_port = netdev_to_devlink_port(dev);
6837         if (!devlink_port)
6838                 return -EOPNOTSUPP;
6839
6840         return __devlink_port_phys_port_name_get(devlink_port, name, len);
6841 }
6842
6843 int devlink_compat_switch_id_get(struct net_device *dev,
6844                                  struct netdev_phys_item_id *ppid)
6845 {
6846         struct devlink_port *devlink_port;
6847
6848         /* RTNL mutex is held here which ensures that devlink_port
6849          * instance cannot disappear in the middle. No need to take
6850          * any devlink lock as only permanent values are accessed.
6851          */
6852         ASSERT_RTNL();
6853         devlink_port = netdev_to_devlink_port(dev);
6854         if (!devlink_port || !devlink_port->attrs.switch_port)
6855                 return -EOPNOTSUPP;
6856
6857         memcpy(ppid, &devlink_port->attrs.switch_id, sizeof(*ppid));
6858
6859         return 0;
6860 }
6861
6862 static int __init devlink_init(void)
6863 {
6864         return genl_register_family(&devlink_nl_family);
6865 }
6866
6867 subsys_initcall(devlink_init);
This page took 0.434852 seconds and 4 git commands to generate.