GNU Linux-libre 4.19.211-gnu1
[releases.git] / net / core / devlink.c
1 /*
2  * net/core/devlink.c - Network physical/parent device Netlink interface
3  *
4  * Heavily inspired by net/wireless/
5  * Copyright (c) 2016 Mellanox Technologies. All rights reserved.
6  * Copyright (c) 2016 Jiri Pirko <jiri@mellanox.com>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  */
13
14 #include <linux/kernel.h>
15 #include <linux/module.h>
16 #include <linux/types.h>
17 #include <linux/slab.h>
18 #include <linux/gfp.h>
19 #include <linux/device.h>
20 #include <linux/list.h>
21 #include <linux/netdevice.h>
22 #include <rdma/ib_verbs.h>
23 #include <net/netlink.h>
24 #include <net/genetlink.h>
25 #include <net/rtnetlink.h>
26 #include <net/net_namespace.h>
27 #include <net/sock.h>
28 #include <net/devlink.h>
29 #define CREATE_TRACE_POINTS
30 #include <trace/events/devlink.h>
31
32 static struct devlink_dpipe_field devlink_dpipe_fields_ethernet[] = {
33         {
34                 .name = "destination mac",
35                 .id = DEVLINK_DPIPE_FIELD_ETHERNET_DST_MAC,
36                 .bitwidth = 48,
37         },
38 };
39
40 struct devlink_dpipe_header devlink_dpipe_header_ethernet = {
41         .name = "ethernet",
42         .id = DEVLINK_DPIPE_HEADER_ETHERNET,
43         .fields = devlink_dpipe_fields_ethernet,
44         .fields_count = ARRAY_SIZE(devlink_dpipe_fields_ethernet),
45         .global = true,
46 };
47 EXPORT_SYMBOL(devlink_dpipe_header_ethernet);
48
49 static struct devlink_dpipe_field devlink_dpipe_fields_ipv4[] = {
50         {
51                 .name = "destination ip",
52                 .id = DEVLINK_DPIPE_FIELD_IPV4_DST_IP,
53                 .bitwidth = 32,
54         },
55 };
56
57 struct devlink_dpipe_header devlink_dpipe_header_ipv4 = {
58         .name = "ipv4",
59         .id = DEVLINK_DPIPE_HEADER_IPV4,
60         .fields = devlink_dpipe_fields_ipv4,
61         .fields_count = ARRAY_SIZE(devlink_dpipe_fields_ipv4),
62         .global = true,
63 };
64 EXPORT_SYMBOL(devlink_dpipe_header_ipv4);
65
66 static struct devlink_dpipe_field devlink_dpipe_fields_ipv6[] = {
67         {
68                 .name = "destination ip",
69                 .id = DEVLINK_DPIPE_FIELD_IPV6_DST_IP,
70                 .bitwidth = 128,
71         },
72 };
73
74 struct devlink_dpipe_header devlink_dpipe_header_ipv6 = {
75         .name = "ipv6",
76         .id = DEVLINK_DPIPE_HEADER_IPV6,
77         .fields = devlink_dpipe_fields_ipv6,
78         .fields_count = ARRAY_SIZE(devlink_dpipe_fields_ipv6),
79         .global = true,
80 };
81 EXPORT_SYMBOL(devlink_dpipe_header_ipv6);
82
83 EXPORT_TRACEPOINT_SYMBOL_GPL(devlink_hwmsg);
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         list_for_each_entry(devlink, &devlink_list, list) {
119                 if (strcmp(devlink->dev->bus->name, busname) == 0 &&
120                     strcmp(dev_name(devlink->dev), devname) == 0 &&
121                     net_eq(devlink_net(devlink), net))
122                         return devlink;
123         }
124
125         return ERR_PTR(-ENODEV);
126 }
127
128 static struct devlink *devlink_get_from_info(struct genl_info *info)
129 {
130         return devlink_get_from_attrs(genl_info_net(info), info->attrs);
131 }
132
133 static struct devlink_port *devlink_port_get_by_index(struct devlink *devlink,
134                                                       int port_index)
135 {
136         struct devlink_port *devlink_port;
137
138         list_for_each_entry(devlink_port, &devlink->port_list, list) {
139                 if (devlink_port->index == port_index)
140                         return devlink_port;
141         }
142         return NULL;
143 }
144
145 static bool devlink_port_index_exists(struct devlink *devlink, int port_index)
146 {
147         return devlink_port_get_by_index(devlink, port_index);
148 }
149
150 static struct devlink_port *devlink_port_get_from_attrs(struct devlink *devlink,
151                                                         struct nlattr **attrs)
152 {
153         if (attrs[DEVLINK_ATTR_PORT_INDEX]) {
154                 u32 port_index = nla_get_u32(attrs[DEVLINK_ATTR_PORT_INDEX]);
155                 struct devlink_port *devlink_port;
156
157                 devlink_port = devlink_port_get_by_index(devlink, port_index);
158                 if (!devlink_port)
159                         return ERR_PTR(-ENODEV);
160                 return devlink_port;
161         }
162         return ERR_PTR(-EINVAL);
163 }
164
165 static struct devlink_port *devlink_port_get_from_info(struct devlink *devlink,
166                                                        struct genl_info *info)
167 {
168         return devlink_port_get_from_attrs(devlink, info->attrs);
169 }
170
171 struct devlink_sb {
172         struct list_head list;
173         unsigned int index;
174         u32 size;
175         u16 ingress_pools_count;
176         u16 egress_pools_count;
177         u16 ingress_tc_count;
178         u16 egress_tc_count;
179 };
180
181 static u16 devlink_sb_pool_count(struct devlink_sb *devlink_sb)
182 {
183         return devlink_sb->ingress_pools_count + devlink_sb->egress_pools_count;
184 }
185
186 static struct devlink_sb *devlink_sb_get_by_index(struct devlink *devlink,
187                                                   unsigned int sb_index)
188 {
189         struct devlink_sb *devlink_sb;
190
191         list_for_each_entry(devlink_sb, &devlink->sb_list, list) {
192                 if (devlink_sb->index == sb_index)
193                         return devlink_sb;
194         }
195         return NULL;
196 }
197
198 static bool devlink_sb_index_exists(struct devlink *devlink,
199                                     unsigned int sb_index)
200 {
201         return devlink_sb_get_by_index(devlink, sb_index);
202 }
203
204 static struct devlink_sb *devlink_sb_get_from_attrs(struct devlink *devlink,
205                                                     struct nlattr **attrs)
206 {
207         if (attrs[DEVLINK_ATTR_SB_INDEX]) {
208                 u32 sb_index = nla_get_u32(attrs[DEVLINK_ATTR_SB_INDEX]);
209                 struct devlink_sb *devlink_sb;
210
211                 devlink_sb = devlink_sb_get_by_index(devlink, sb_index);
212                 if (!devlink_sb)
213                         return ERR_PTR(-ENODEV);
214                 return devlink_sb;
215         }
216         return ERR_PTR(-EINVAL);
217 }
218
219 static struct devlink_sb *devlink_sb_get_from_info(struct devlink *devlink,
220                                                    struct genl_info *info)
221 {
222         return devlink_sb_get_from_attrs(devlink, info->attrs);
223 }
224
225 static int devlink_sb_pool_index_get_from_attrs(struct devlink_sb *devlink_sb,
226                                                 struct nlattr **attrs,
227                                                 u16 *p_pool_index)
228 {
229         u16 val;
230
231         if (!attrs[DEVLINK_ATTR_SB_POOL_INDEX])
232                 return -EINVAL;
233
234         val = nla_get_u16(attrs[DEVLINK_ATTR_SB_POOL_INDEX]);
235         if (val >= devlink_sb_pool_count(devlink_sb))
236                 return -EINVAL;
237         *p_pool_index = val;
238         return 0;
239 }
240
241 static int devlink_sb_pool_index_get_from_info(struct devlink_sb *devlink_sb,
242                                                struct genl_info *info,
243                                                u16 *p_pool_index)
244 {
245         return devlink_sb_pool_index_get_from_attrs(devlink_sb, info->attrs,
246                                                     p_pool_index);
247 }
248
249 static int
250 devlink_sb_pool_type_get_from_attrs(struct nlattr **attrs,
251                                     enum devlink_sb_pool_type *p_pool_type)
252 {
253         u8 val;
254
255         if (!attrs[DEVLINK_ATTR_SB_POOL_TYPE])
256                 return -EINVAL;
257
258         val = nla_get_u8(attrs[DEVLINK_ATTR_SB_POOL_TYPE]);
259         if (val != DEVLINK_SB_POOL_TYPE_INGRESS &&
260             val != DEVLINK_SB_POOL_TYPE_EGRESS)
261                 return -EINVAL;
262         *p_pool_type = val;
263         return 0;
264 }
265
266 static int
267 devlink_sb_pool_type_get_from_info(struct genl_info *info,
268                                    enum devlink_sb_pool_type *p_pool_type)
269 {
270         return devlink_sb_pool_type_get_from_attrs(info->attrs, p_pool_type);
271 }
272
273 static int
274 devlink_sb_th_type_get_from_attrs(struct nlattr **attrs,
275                                   enum devlink_sb_threshold_type *p_th_type)
276 {
277         u8 val;
278
279         if (!attrs[DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE])
280                 return -EINVAL;
281
282         val = nla_get_u8(attrs[DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE]);
283         if (val != DEVLINK_SB_THRESHOLD_TYPE_STATIC &&
284             val != DEVLINK_SB_THRESHOLD_TYPE_DYNAMIC)
285                 return -EINVAL;
286         *p_th_type = val;
287         return 0;
288 }
289
290 static int
291 devlink_sb_th_type_get_from_info(struct genl_info *info,
292                                  enum devlink_sb_threshold_type *p_th_type)
293 {
294         return devlink_sb_th_type_get_from_attrs(info->attrs, p_th_type);
295 }
296
297 static int
298 devlink_sb_tc_index_get_from_attrs(struct devlink_sb *devlink_sb,
299                                    struct nlattr **attrs,
300                                    enum devlink_sb_pool_type pool_type,
301                                    u16 *p_tc_index)
302 {
303         u16 val;
304
305         if (!attrs[DEVLINK_ATTR_SB_TC_INDEX])
306                 return -EINVAL;
307
308         val = nla_get_u16(attrs[DEVLINK_ATTR_SB_TC_INDEX]);
309         if (pool_type == DEVLINK_SB_POOL_TYPE_INGRESS &&
310             val >= devlink_sb->ingress_tc_count)
311                 return -EINVAL;
312         if (pool_type == DEVLINK_SB_POOL_TYPE_EGRESS &&
313             val >= devlink_sb->egress_tc_count)
314                 return -EINVAL;
315         *p_tc_index = val;
316         return 0;
317 }
318
319 static int
320 devlink_sb_tc_index_get_from_info(struct devlink_sb *devlink_sb,
321                                   struct genl_info *info,
322                                   enum devlink_sb_pool_type pool_type,
323                                   u16 *p_tc_index)
324 {
325         return devlink_sb_tc_index_get_from_attrs(devlink_sb, info->attrs,
326                                                   pool_type, p_tc_index);
327 }
328
329 struct devlink_region {
330         struct devlink *devlink;
331         struct list_head list;
332         const char *name;
333         struct list_head snapshot_list;
334         u32 max_snapshots;
335         u32 cur_snapshots;
336         u64 size;
337 };
338
339 struct devlink_snapshot {
340         struct list_head list;
341         struct devlink_region *region;
342         devlink_snapshot_data_dest_t *data_destructor;
343         u64 data_len;
344         u8 *data;
345         u32 id;
346 };
347
348 static struct devlink_region *
349 devlink_region_get_by_name(struct devlink *devlink, const char *region_name)
350 {
351         struct devlink_region *region;
352
353         list_for_each_entry(region, &devlink->region_list, list)
354                 if (!strcmp(region->name, region_name))
355                         return region;
356
357         return NULL;
358 }
359
360 static struct devlink_snapshot *
361 devlink_region_snapshot_get_by_id(struct devlink_region *region, u32 id)
362 {
363         struct devlink_snapshot *snapshot;
364
365         list_for_each_entry(snapshot, &region->snapshot_list, list)
366                 if (snapshot->id == id)
367                         return snapshot;
368
369         return NULL;
370 }
371
372 static void devlink_region_snapshot_del(struct devlink_snapshot *snapshot)
373 {
374         snapshot->region->cur_snapshots--;
375         list_del(&snapshot->list);
376         (*snapshot->data_destructor)(snapshot->data);
377         kfree(snapshot);
378 }
379
380 #define DEVLINK_NL_FLAG_NEED_DEVLINK    BIT(0)
381 #define DEVLINK_NL_FLAG_NEED_PORT       BIT(1)
382 #define DEVLINK_NL_FLAG_NEED_SB         BIT(2)
383
384 /* The per devlink instance lock is taken by default in the pre-doit
385  * operation, yet several commands do not require this. The global
386  * devlink lock is taken and protects from disruption by user-calls.
387  */
388 #define DEVLINK_NL_FLAG_NO_LOCK         BIT(3)
389
390 static int devlink_nl_pre_doit(const struct genl_ops *ops,
391                                struct sk_buff *skb, struct genl_info *info)
392 {
393         struct devlink *devlink;
394         int err;
395
396         mutex_lock(&devlink_mutex);
397         devlink = devlink_get_from_info(info);
398         if (IS_ERR(devlink)) {
399                 mutex_unlock(&devlink_mutex);
400                 return PTR_ERR(devlink);
401         }
402         if (~ops->internal_flags & DEVLINK_NL_FLAG_NO_LOCK)
403                 mutex_lock(&devlink->lock);
404         if (ops->internal_flags & DEVLINK_NL_FLAG_NEED_DEVLINK) {
405                 info->user_ptr[0] = devlink;
406         } else if (ops->internal_flags & DEVLINK_NL_FLAG_NEED_PORT) {
407                 struct devlink_port *devlink_port;
408
409                 devlink_port = devlink_port_get_from_info(devlink, info);
410                 if (IS_ERR(devlink_port)) {
411                         err = PTR_ERR(devlink_port);
412                         goto unlock;
413                 }
414                 info->user_ptr[0] = devlink_port;
415         }
416         if (ops->internal_flags & DEVLINK_NL_FLAG_NEED_SB) {
417                 struct devlink_sb *devlink_sb;
418
419                 devlink_sb = devlink_sb_get_from_info(devlink, info);
420                 if (IS_ERR(devlink_sb)) {
421                         err = PTR_ERR(devlink_sb);
422                         goto unlock;
423                 }
424                 info->user_ptr[1] = devlink_sb;
425         }
426         return 0;
427
428 unlock:
429         if (~ops->internal_flags & DEVLINK_NL_FLAG_NO_LOCK)
430                 mutex_unlock(&devlink->lock);
431         mutex_unlock(&devlink_mutex);
432         return err;
433 }
434
435 static void devlink_nl_post_doit(const struct genl_ops *ops,
436                                  struct sk_buff *skb, struct genl_info *info)
437 {
438         struct devlink *devlink;
439
440         devlink = devlink_get_from_info(info);
441         if (~ops->internal_flags & DEVLINK_NL_FLAG_NO_LOCK)
442                 mutex_unlock(&devlink->lock);
443         mutex_unlock(&devlink_mutex);
444 }
445
446 static struct genl_family devlink_nl_family;
447
448 enum devlink_multicast_groups {
449         DEVLINK_MCGRP_CONFIG,
450 };
451
452 static const struct genl_multicast_group devlink_nl_mcgrps[] = {
453         [DEVLINK_MCGRP_CONFIG] = { .name = DEVLINK_GENL_MCGRP_CONFIG_NAME },
454 };
455
456 static int devlink_nl_put_handle(struct sk_buff *msg, struct devlink *devlink)
457 {
458         if (nla_put_string(msg, DEVLINK_ATTR_BUS_NAME, devlink->dev->bus->name))
459                 return -EMSGSIZE;
460         if (nla_put_string(msg, DEVLINK_ATTR_DEV_NAME, dev_name(devlink->dev)))
461                 return -EMSGSIZE;
462         return 0;
463 }
464
465 static int devlink_nl_fill(struct sk_buff *msg, struct devlink *devlink,
466                            enum devlink_command cmd, u32 portid,
467                            u32 seq, int flags)
468 {
469         void *hdr;
470
471         hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
472         if (!hdr)
473                 return -EMSGSIZE;
474
475         if (devlink_nl_put_handle(msg, devlink))
476                 goto nla_put_failure;
477
478         genlmsg_end(msg, hdr);
479         return 0;
480
481 nla_put_failure:
482         genlmsg_cancel(msg, hdr);
483         return -EMSGSIZE;
484 }
485
486 static void devlink_notify(struct devlink *devlink, enum devlink_command cmd)
487 {
488         struct sk_buff *msg;
489         int err;
490
491         WARN_ON(cmd != DEVLINK_CMD_NEW && cmd != DEVLINK_CMD_DEL);
492
493         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
494         if (!msg)
495                 return;
496
497         err = devlink_nl_fill(msg, devlink, cmd, 0, 0, 0);
498         if (err) {
499                 nlmsg_free(msg);
500                 return;
501         }
502
503         genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
504                                 msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
505 }
506
507 static int devlink_nl_port_attrs_put(struct sk_buff *msg,
508                                      struct devlink_port *devlink_port)
509 {
510         struct devlink_port_attrs *attrs = &devlink_port->attrs;
511
512         if (!attrs->set)
513                 return 0;
514         if (nla_put_u16(msg, DEVLINK_ATTR_PORT_FLAVOUR, attrs->flavour))
515                 return -EMSGSIZE;
516         if (nla_put_u32(msg, DEVLINK_ATTR_PORT_NUMBER, attrs->port_number))
517                 return -EMSGSIZE;
518         if (!attrs->split)
519                 return 0;
520         if (nla_put_u32(msg, DEVLINK_ATTR_PORT_SPLIT_GROUP, attrs->port_number))
521                 return -EMSGSIZE;
522         if (nla_put_u32(msg, DEVLINK_ATTR_PORT_SPLIT_SUBPORT_NUMBER,
523                         attrs->split_subport_number))
524                 return -EMSGSIZE;
525         return 0;
526 }
527
528 static int devlink_nl_port_fill(struct sk_buff *msg, struct devlink *devlink,
529                                 struct devlink_port *devlink_port,
530                                 enum devlink_command cmd, u32 portid,
531                                 u32 seq, int flags)
532 {
533         void *hdr;
534
535         hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
536         if (!hdr)
537                 return -EMSGSIZE;
538
539         if (devlink_nl_put_handle(msg, devlink))
540                 goto nla_put_failure;
541         if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, devlink_port->index))
542                 goto nla_put_failure;
543         if (nla_put_u16(msg, DEVLINK_ATTR_PORT_TYPE, devlink_port->type))
544                 goto nla_put_failure;
545         if (devlink_port->desired_type != DEVLINK_PORT_TYPE_NOTSET &&
546             nla_put_u16(msg, DEVLINK_ATTR_PORT_DESIRED_TYPE,
547                         devlink_port->desired_type))
548                 goto nla_put_failure;
549         if (devlink_port->type == DEVLINK_PORT_TYPE_ETH) {
550                 struct net_device *netdev = devlink_port->type_dev;
551
552                 if (netdev &&
553                     (nla_put_u32(msg, DEVLINK_ATTR_PORT_NETDEV_IFINDEX,
554                                  netdev->ifindex) ||
555                      nla_put_string(msg, DEVLINK_ATTR_PORT_NETDEV_NAME,
556                                     netdev->name)))
557                         goto nla_put_failure;
558         }
559         if (devlink_port->type == DEVLINK_PORT_TYPE_IB) {
560                 struct ib_device *ibdev = devlink_port->type_dev;
561
562                 if (ibdev &&
563                     nla_put_string(msg, DEVLINK_ATTR_PORT_IBDEV_NAME,
564                                    ibdev->name))
565                         goto nla_put_failure;
566         }
567         if (devlink_nl_port_attrs_put(msg, devlink_port))
568                 goto nla_put_failure;
569
570         genlmsg_end(msg, hdr);
571         return 0;
572
573 nla_put_failure:
574         genlmsg_cancel(msg, hdr);
575         return -EMSGSIZE;
576 }
577
578 static void devlink_port_notify(struct devlink_port *devlink_port,
579                                 enum devlink_command cmd)
580 {
581         struct devlink *devlink = devlink_port->devlink;
582         struct sk_buff *msg;
583         int err;
584
585         if (!devlink_port->registered)
586                 return;
587
588         WARN_ON(cmd != DEVLINK_CMD_PORT_NEW && cmd != DEVLINK_CMD_PORT_DEL);
589
590         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
591         if (!msg)
592                 return;
593
594         err = devlink_nl_port_fill(msg, devlink, devlink_port, cmd, 0, 0, 0);
595         if (err) {
596                 nlmsg_free(msg);
597                 return;
598         }
599
600         genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
601                                 msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
602 }
603
604 static int devlink_nl_cmd_get_doit(struct sk_buff *skb, struct genl_info *info)
605 {
606         struct devlink *devlink = info->user_ptr[0];
607         struct sk_buff *msg;
608         int err;
609
610         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
611         if (!msg)
612                 return -ENOMEM;
613
614         err = devlink_nl_fill(msg, devlink, DEVLINK_CMD_NEW,
615                               info->snd_portid, info->snd_seq, 0);
616         if (err) {
617                 nlmsg_free(msg);
618                 return err;
619         }
620
621         return genlmsg_reply(msg, info);
622 }
623
624 static int devlink_nl_cmd_get_dumpit(struct sk_buff *msg,
625                                      struct netlink_callback *cb)
626 {
627         struct devlink *devlink;
628         int start = cb->args[0];
629         int idx = 0;
630         int err;
631
632         mutex_lock(&devlink_mutex);
633         list_for_each_entry(devlink, &devlink_list, list) {
634                 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
635                         continue;
636                 if (idx < start) {
637                         idx++;
638                         continue;
639                 }
640                 err = devlink_nl_fill(msg, devlink, DEVLINK_CMD_NEW,
641                                       NETLINK_CB(cb->skb).portid,
642                                       cb->nlh->nlmsg_seq, NLM_F_MULTI);
643                 if (err)
644                         goto out;
645                 idx++;
646         }
647 out:
648         mutex_unlock(&devlink_mutex);
649
650         cb->args[0] = idx;
651         return msg->len;
652 }
653
654 static int devlink_nl_cmd_port_get_doit(struct sk_buff *skb,
655                                         struct genl_info *info)
656 {
657         struct devlink_port *devlink_port = info->user_ptr[0];
658         struct devlink *devlink = devlink_port->devlink;
659         struct sk_buff *msg;
660         int err;
661
662         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
663         if (!msg)
664                 return -ENOMEM;
665
666         err = devlink_nl_port_fill(msg, devlink, devlink_port,
667                                    DEVLINK_CMD_PORT_NEW,
668                                    info->snd_portid, info->snd_seq, 0);
669         if (err) {
670                 nlmsg_free(msg);
671                 return err;
672         }
673
674         return genlmsg_reply(msg, info);
675 }
676
677 static int devlink_nl_cmd_port_get_dumpit(struct sk_buff *msg,
678                                           struct netlink_callback *cb)
679 {
680         struct devlink *devlink;
681         struct devlink_port *devlink_port;
682         int start = cb->args[0];
683         int idx = 0;
684         int err;
685
686         mutex_lock(&devlink_mutex);
687         list_for_each_entry(devlink, &devlink_list, list) {
688                 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
689                         continue;
690                 mutex_lock(&devlink->lock);
691                 list_for_each_entry(devlink_port, &devlink->port_list, list) {
692                         if (idx < start) {
693                                 idx++;
694                                 continue;
695                         }
696                         err = devlink_nl_port_fill(msg, devlink, devlink_port,
697                                                    DEVLINK_CMD_NEW,
698                                                    NETLINK_CB(cb->skb).portid,
699                                                    cb->nlh->nlmsg_seq,
700                                                    NLM_F_MULTI);
701                         if (err) {
702                                 mutex_unlock(&devlink->lock);
703                                 goto out;
704                         }
705                         idx++;
706                 }
707                 mutex_unlock(&devlink->lock);
708         }
709 out:
710         mutex_unlock(&devlink_mutex);
711
712         cb->args[0] = idx;
713         return msg->len;
714 }
715
716 static int devlink_port_type_set(struct devlink *devlink,
717                                  struct devlink_port *devlink_port,
718                                  enum devlink_port_type port_type)
719
720 {
721         int err;
722
723         if (devlink->ops && devlink->ops->port_type_set) {
724                 if (port_type == DEVLINK_PORT_TYPE_NOTSET)
725                         return -EINVAL;
726                 if (port_type == devlink_port->type)
727                         return 0;
728                 err = devlink->ops->port_type_set(devlink_port, port_type);
729                 if (err)
730                         return err;
731                 devlink_port->desired_type = port_type;
732                 devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW);
733                 return 0;
734         }
735         return -EOPNOTSUPP;
736 }
737
738 static int devlink_nl_cmd_port_set_doit(struct sk_buff *skb,
739                                         struct genl_info *info)
740 {
741         struct devlink_port *devlink_port = info->user_ptr[0];
742         struct devlink *devlink = devlink_port->devlink;
743         int err;
744
745         if (info->attrs[DEVLINK_ATTR_PORT_TYPE]) {
746                 enum devlink_port_type port_type;
747
748                 port_type = nla_get_u16(info->attrs[DEVLINK_ATTR_PORT_TYPE]);
749                 err = devlink_port_type_set(devlink, devlink_port, port_type);
750                 if (err)
751                         return err;
752         }
753         return 0;
754 }
755
756 static int devlink_port_split(struct devlink *devlink, u32 port_index,
757                               u32 count, struct netlink_ext_ack *extack)
758
759 {
760         if (devlink->ops && devlink->ops->port_split)
761                 return devlink->ops->port_split(devlink, port_index, count,
762                                                 extack);
763         return -EOPNOTSUPP;
764 }
765
766 static int devlink_nl_cmd_port_split_doit(struct sk_buff *skb,
767                                           struct genl_info *info)
768 {
769         struct devlink *devlink = info->user_ptr[0];
770         u32 port_index;
771         u32 count;
772
773         if (!info->attrs[DEVLINK_ATTR_PORT_INDEX] ||
774             !info->attrs[DEVLINK_ATTR_PORT_SPLIT_COUNT])
775                 return -EINVAL;
776
777         port_index = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_INDEX]);
778         count = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_SPLIT_COUNT]);
779         return devlink_port_split(devlink, port_index, count, info->extack);
780 }
781
782 static int devlink_port_unsplit(struct devlink *devlink, u32 port_index,
783                                 struct netlink_ext_ack *extack)
784
785 {
786         if (devlink->ops && devlink->ops->port_unsplit)
787                 return devlink->ops->port_unsplit(devlink, port_index, extack);
788         return -EOPNOTSUPP;
789 }
790
791 static int devlink_nl_cmd_port_unsplit_doit(struct sk_buff *skb,
792                                             struct genl_info *info)
793 {
794         struct devlink *devlink = info->user_ptr[0];
795         u32 port_index;
796
797         if (!info->attrs[DEVLINK_ATTR_PORT_INDEX])
798                 return -EINVAL;
799
800         port_index = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_INDEX]);
801         return devlink_port_unsplit(devlink, port_index, info->extack);
802 }
803
804 static int devlink_nl_sb_fill(struct sk_buff *msg, struct devlink *devlink,
805                               struct devlink_sb *devlink_sb,
806                               enum devlink_command cmd, u32 portid,
807                               u32 seq, int flags)
808 {
809         void *hdr;
810
811         hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
812         if (!hdr)
813                 return -EMSGSIZE;
814
815         if (devlink_nl_put_handle(msg, devlink))
816                 goto nla_put_failure;
817         if (nla_put_u32(msg, DEVLINK_ATTR_SB_INDEX, devlink_sb->index))
818                 goto nla_put_failure;
819         if (nla_put_u32(msg, DEVLINK_ATTR_SB_SIZE, devlink_sb->size))
820                 goto nla_put_failure;
821         if (nla_put_u16(msg, DEVLINK_ATTR_SB_INGRESS_POOL_COUNT,
822                         devlink_sb->ingress_pools_count))
823                 goto nla_put_failure;
824         if (nla_put_u16(msg, DEVLINK_ATTR_SB_EGRESS_POOL_COUNT,
825                         devlink_sb->egress_pools_count))
826                 goto nla_put_failure;
827         if (nla_put_u16(msg, DEVLINK_ATTR_SB_INGRESS_TC_COUNT,
828                         devlink_sb->ingress_tc_count))
829                 goto nla_put_failure;
830         if (nla_put_u16(msg, DEVLINK_ATTR_SB_EGRESS_TC_COUNT,
831                         devlink_sb->egress_tc_count))
832                 goto nla_put_failure;
833
834         genlmsg_end(msg, hdr);
835         return 0;
836
837 nla_put_failure:
838         genlmsg_cancel(msg, hdr);
839         return -EMSGSIZE;
840 }
841
842 static int devlink_nl_cmd_sb_get_doit(struct sk_buff *skb,
843                                       struct genl_info *info)
844 {
845         struct devlink *devlink = info->user_ptr[0];
846         struct devlink_sb *devlink_sb = info->user_ptr[1];
847         struct sk_buff *msg;
848         int err;
849
850         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
851         if (!msg)
852                 return -ENOMEM;
853
854         err = devlink_nl_sb_fill(msg, devlink, devlink_sb,
855                                  DEVLINK_CMD_SB_NEW,
856                                  info->snd_portid, info->snd_seq, 0);
857         if (err) {
858                 nlmsg_free(msg);
859                 return err;
860         }
861
862         return genlmsg_reply(msg, info);
863 }
864
865 static int devlink_nl_cmd_sb_get_dumpit(struct sk_buff *msg,
866                                         struct netlink_callback *cb)
867 {
868         struct devlink *devlink;
869         struct devlink_sb *devlink_sb;
870         int start = cb->args[0];
871         int idx = 0;
872         int err;
873
874         mutex_lock(&devlink_mutex);
875         list_for_each_entry(devlink, &devlink_list, list) {
876                 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
877                         continue;
878                 mutex_lock(&devlink->lock);
879                 list_for_each_entry(devlink_sb, &devlink->sb_list, list) {
880                         if (idx < start) {
881                                 idx++;
882                                 continue;
883                         }
884                         err = devlink_nl_sb_fill(msg, devlink, devlink_sb,
885                                                  DEVLINK_CMD_SB_NEW,
886                                                  NETLINK_CB(cb->skb).portid,
887                                                  cb->nlh->nlmsg_seq,
888                                                  NLM_F_MULTI);
889                         if (err) {
890                                 mutex_unlock(&devlink->lock);
891                                 goto out;
892                         }
893                         idx++;
894                 }
895                 mutex_unlock(&devlink->lock);
896         }
897 out:
898         mutex_unlock(&devlink_mutex);
899
900         cb->args[0] = idx;
901         return msg->len;
902 }
903
904 static int devlink_nl_sb_pool_fill(struct sk_buff *msg, struct devlink *devlink,
905                                    struct devlink_sb *devlink_sb,
906                                    u16 pool_index, enum devlink_command cmd,
907                                    u32 portid, u32 seq, int flags)
908 {
909         struct devlink_sb_pool_info pool_info;
910         void *hdr;
911         int err;
912
913         err = devlink->ops->sb_pool_get(devlink, devlink_sb->index,
914                                         pool_index, &pool_info);
915         if (err)
916                 return err;
917
918         hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
919         if (!hdr)
920                 return -EMSGSIZE;
921
922         if (devlink_nl_put_handle(msg, devlink))
923                 goto nla_put_failure;
924         if (nla_put_u32(msg, DEVLINK_ATTR_SB_INDEX, devlink_sb->index))
925                 goto nla_put_failure;
926         if (nla_put_u16(msg, DEVLINK_ATTR_SB_POOL_INDEX, pool_index))
927                 goto nla_put_failure;
928         if (nla_put_u8(msg, DEVLINK_ATTR_SB_POOL_TYPE, pool_info.pool_type))
929                 goto nla_put_failure;
930         if (nla_put_u32(msg, DEVLINK_ATTR_SB_POOL_SIZE, pool_info.size))
931                 goto nla_put_failure;
932         if (nla_put_u8(msg, DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE,
933                        pool_info.threshold_type))
934                 goto nla_put_failure;
935
936         genlmsg_end(msg, hdr);
937         return 0;
938
939 nla_put_failure:
940         genlmsg_cancel(msg, hdr);
941         return -EMSGSIZE;
942 }
943
944 static int devlink_nl_cmd_sb_pool_get_doit(struct sk_buff *skb,
945                                            struct genl_info *info)
946 {
947         struct devlink *devlink = info->user_ptr[0];
948         struct devlink_sb *devlink_sb = info->user_ptr[1];
949         struct sk_buff *msg;
950         u16 pool_index;
951         int err;
952
953         err = devlink_sb_pool_index_get_from_info(devlink_sb, info,
954                                                   &pool_index);
955         if (err)
956                 return err;
957
958         if (!devlink->ops || !devlink->ops->sb_pool_get)
959                 return -EOPNOTSUPP;
960
961         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
962         if (!msg)
963                 return -ENOMEM;
964
965         err = devlink_nl_sb_pool_fill(msg, devlink, devlink_sb, pool_index,
966                                       DEVLINK_CMD_SB_POOL_NEW,
967                                       info->snd_portid, info->snd_seq, 0);
968         if (err) {
969                 nlmsg_free(msg);
970                 return err;
971         }
972
973         return genlmsg_reply(msg, info);
974 }
975
976 static int __sb_pool_get_dumpit(struct sk_buff *msg, int start, int *p_idx,
977                                 struct devlink *devlink,
978                                 struct devlink_sb *devlink_sb,
979                                 u32 portid, u32 seq)
980 {
981         u16 pool_count = devlink_sb_pool_count(devlink_sb);
982         u16 pool_index;
983         int err;
984
985         for (pool_index = 0; pool_index < pool_count; pool_index++) {
986                 if (*p_idx < start) {
987                         (*p_idx)++;
988                         continue;
989                 }
990                 err = devlink_nl_sb_pool_fill(msg, devlink,
991                                               devlink_sb,
992                                               pool_index,
993                                               DEVLINK_CMD_SB_POOL_NEW,
994                                               portid, seq, NLM_F_MULTI);
995                 if (err)
996                         return err;
997                 (*p_idx)++;
998         }
999         return 0;
1000 }
1001
1002 static int devlink_nl_cmd_sb_pool_get_dumpit(struct sk_buff *msg,
1003                                              struct netlink_callback *cb)
1004 {
1005         struct devlink *devlink;
1006         struct devlink_sb *devlink_sb;
1007         int start = cb->args[0];
1008         int idx = 0;
1009         int err;
1010
1011         mutex_lock(&devlink_mutex);
1012         list_for_each_entry(devlink, &devlink_list, list) {
1013                 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)) ||
1014                     !devlink->ops || !devlink->ops->sb_pool_get)
1015                         continue;
1016                 mutex_lock(&devlink->lock);
1017                 list_for_each_entry(devlink_sb, &devlink->sb_list, list) {
1018                         err = __sb_pool_get_dumpit(msg, start, &idx, devlink,
1019                                                    devlink_sb,
1020                                                    NETLINK_CB(cb->skb).portid,
1021                                                    cb->nlh->nlmsg_seq);
1022                         if (err && err != -EOPNOTSUPP) {
1023                                 mutex_unlock(&devlink->lock);
1024                                 goto out;
1025                         }
1026                 }
1027                 mutex_unlock(&devlink->lock);
1028         }
1029 out:
1030         mutex_unlock(&devlink_mutex);
1031
1032         cb->args[0] = idx;
1033         return msg->len;
1034 }
1035
1036 static int devlink_sb_pool_set(struct devlink *devlink, unsigned int sb_index,
1037                                u16 pool_index, u32 size,
1038                                enum devlink_sb_threshold_type threshold_type)
1039
1040 {
1041         const struct devlink_ops *ops = devlink->ops;
1042
1043         if (ops && ops->sb_pool_set)
1044                 return ops->sb_pool_set(devlink, sb_index, pool_index,
1045                                         size, threshold_type);
1046         return -EOPNOTSUPP;
1047 }
1048
1049 static int devlink_nl_cmd_sb_pool_set_doit(struct sk_buff *skb,
1050                                            struct genl_info *info)
1051 {
1052         struct devlink *devlink = info->user_ptr[0];
1053         struct devlink_sb *devlink_sb = info->user_ptr[1];
1054         enum devlink_sb_threshold_type threshold_type;
1055         u16 pool_index;
1056         u32 size;
1057         int err;
1058
1059         err = devlink_sb_pool_index_get_from_info(devlink_sb, info,
1060                                                   &pool_index);
1061         if (err)
1062                 return err;
1063
1064         err = devlink_sb_th_type_get_from_info(info, &threshold_type);
1065         if (err)
1066                 return err;
1067
1068         if (!info->attrs[DEVLINK_ATTR_SB_POOL_SIZE])
1069                 return -EINVAL;
1070
1071         size = nla_get_u32(info->attrs[DEVLINK_ATTR_SB_POOL_SIZE]);
1072         return devlink_sb_pool_set(devlink, devlink_sb->index,
1073                                    pool_index, size, threshold_type);
1074 }
1075
1076 static int devlink_nl_sb_port_pool_fill(struct sk_buff *msg,
1077                                         struct devlink *devlink,
1078                                         struct devlink_port *devlink_port,
1079                                         struct devlink_sb *devlink_sb,
1080                                         u16 pool_index,
1081                                         enum devlink_command cmd,
1082                                         u32 portid, u32 seq, int flags)
1083 {
1084         const struct devlink_ops *ops = devlink->ops;
1085         u32 threshold;
1086         void *hdr;
1087         int err;
1088
1089         err = ops->sb_port_pool_get(devlink_port, devlink_sb->index,
1090                                     pool_index, &threshold);
1091         if (err)
1092                 return err;
1093
1094         hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
1095         if (!hdr)
1096                 return -EMSGSIZE;
1097
1098         if (devlink_nl_put_handle(msg, devlink))
1099                 goto nla_put_failure;
1100         if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, devlink_port->index))
1101                 goto nla_put_failure;
1102         if (nla_put_u32(msg, DEVLINK_ATTR_SB_INDEX, devlink_sb->index))
1103                 goto nla_put_failure;
1104         if (nla_put_u16(msg, DEVLINK_ATTR_SB_POOL_INDEX, pool_index))
1105                 goto nla_put_failure;
1106         if (nla_put_u32(msg, DEVLINK_ATTR_SB_THRESHOLD, threshold))
1107                 goto nla_put_failure;
1108
1109         if (ops->sb_occ_port_pool_get) {
1110                 u32 cur;
1111                 u32 max;
1112
1113                 err = ops->sb_occ_port_pool_get(devlink_port, devlink_sb->index,
1114                                                 pool_index, &cur, &max);
1115                 if (err && err != -EOPNOTSUPP)
1116                         goto sb_occ_get_failure;
1117                 if (!err) {
1118                         if (nla_put_u32(msg, DEVLINK_ATTR_SB_OCC_CUR, cur))
1119                                 goto nla_put_failure;
1120                         if (nla_put_u32(msg, DEVLINK_ATTR_SB_OCC_MAX, max))
1121                                 goto nla_put_failure;
1122                 }
1123         }
1124
1125         genlmsg_end(msg, hdr);
1126         return 0;
1127
1128 nla_put_failure:
1129         err = -EMSGSIZE;
1130 sb_occ_get_failure:
1131         genlmsg_cancel(msg, hdr);
1132         return err;
1133 }
1134
1135 static int devlink_nl_cmd_sb_port_pool_get_doit(struct sk_buff *skb,
1136                                                 struct genl_info *info)
1137 {
1138         struct devlink_port *devlink_port = info->user_ptr[0];
1139         struct devlink *devlink = devlink_port->devlink;
1140         struct devlink_sb *devlink_sb = info->user_ptr[1];
1141         struct sk_buff *msg;
1142         u16 pool_index;
1143         int err;
1144
1145         err = devlink_sb_pool_index_get_from_info(devlink_sb, info,
1146                                                   &pool_index);
1147         if (err)
1148                 return err;
1149
1150         if (!devlink->ops || !devlink->ops->sb_port_pool_get)
1151                 return -EOPNOTSUPP;
1152
1153         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1154         if (!msg)
1155                 return -ENOMEM;
1156
1157         err = devlink_nl_sb_port_pool_fill(msg, devlink, devlink_port,
1158                                            devlink_sb, pool_index,
1159                                            DEVLINK_CMD_SB_PORT_POOL_NEW,
1160                                            info->snd_portid, info->snd_seq, 0);
1161         if (err) {
1162                 nlmsg_free(msg);
1163                 return err;
1164         }
1165
1166         return genlmsg_reply(msg, info);
1167 }
1168
1169 static int __sb_port_pool_get_dumpit(struct sk_buff *msg, int start, int *p_idx,
1170                                      struct devlink *devlink,
1171                                      struct devlink_sb *devlink_sb,
1172                                      u32 portid, u32 seq)
1173 {
1174         struct devlink_port *devlink_port;
1175         u16 pool_count = devlink_sb_pool_count(devlink_sb);
1176         u16 pool_index;
1177         int err;
1178
1179         list_for_each_entry(devlink_port, &devlink->port_list, list) {
1180                 for (pool_index = 0; pool_index < pool_count; pool_index++) {
1181                         if (*p_idx < start) {
1182                                 (*p_idx)++;
1183                                 continue;
1184                         }
1185                         err = devlink_nl_sb_port_pool_fill(msg, devlink,
1186                                                            devlink_port,
1187                                                            devlink_sb,
1188                                                            pool_index,
1189                                                            DEVLINK_CMD_SB_PORT_POOL_NEW,
1190                                                            portid, seq,
1191                                                            NLM_F_MULTI);
1192                         if (err)
1193                                 return err;
1194                         (*p_idx)++;
1195                 }
1196         }
1197         return 0;
1198 }
1199
1200 static int devlink_nl_cmd_sb_port_pool_get_dumpit(struct sk_buff *msg,
1201                                                   struct netlink_callback *cb)
1202 {
1203         struct devlink *devlink;
1204         struct devlink_sb *devlink_sb;
1205         int start = cb->args[0];
1206         int idx = 0;
1207         int err;
1208
1209         mutex_lock(&devlink_mutex);
1210         list_for_each_entry(devlink, &devlink_list, list) {
1211                 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)) ||
1212                     !devlink->ops || !devlink->ops->sb_port_pool_get)
1213                         continue;
1214                 mutex_lock(&devlink->lock);
1215                 list_for_each_entry(devlink_sb, &devlink->sb_list, list) {
1216                         err = __sb_port_pool_get_dumpit(msg, start, &idx,
1217                                                         devlink, devlink_sb,
1218                                                         NETLINK_CB(cb->skb).portid,
1219                                                         cb->nlh->nlmsg_seq);
1220                         if (err && err != -EOPNOTSUPP) {
1221                                 mutex_unlock(&devlink->lock);
1222                                 goto out;
1223                         }
1224                 }
1225                 mutex_unlock(&devlink->lock);
1226         }
1227 out:
1228         mutex_unlock(&devlink_mutex);
1229
1230         cb->args[0] = idx;
1231         return msg->len;
1232 }
1233
1234 static int devlink_sb_port_pool_set(struct devlink_port *devlink_port,
1235                                     unsigned int sb_index, u16 pool_index,
1236                                     u32 threshold)
1237
1238 {
1239         const struct devlink_ops *ops = devlink_port->devlink->ops;
1240
1241         if (ops && ops->sb_port_pool_set)
1242                 return ops->sb_port_pool_set(devlink_port, sb_index,
1243                                              pool_index, threshold);
1244         return -EOPNOTSUPP;
1245 }
1246
1247 static int devlink_nl_cmd_sb_port_pool_set_doit(struct sk_buff *skb,
1248                                                 struct genl_info *info)
1249 {
1250         struct devlink_port *devlink_port = info->user_ptr[0];
1251         struct devlink_sb *devlink_sb = info->user_ptr[1];
1252         u16 pool_index;
1253         u32 threshold;
1254         int err;
1255
1256         err = devlink_sb_pool_index_get_from_info(devlink_sb, info,
1257                                                   &pool_index);
1258         if (err)
1259                 return err;
1260
1261         if (!info->attrs[DEVLINK_ATTR_SB_THRESHOLD])
1262                 return -EINVAL;
1263
1264         threshold = nla_get_u32(info->attrs[DEVLINK_ATTR_SB_THRESHOLD]);
1265         return devlink_sb_port_pool_set(devlink_port, devlink_sb->index,
1266                                         pool_index, threshold);
1267 }
1268
1269 static int
1270 devlink_nl_sb_tc_pool_bind_fill(struct sk_buff *msg, struct devlink *devlink,
1271                                 struct devlink_port *devlink_port,
1272                                 struct devlink_sb *devlink_sb, u16 tc_index,
1273                                 enum devlink_sb_pool_type pool_type,
1274                                 enum devlink_command cmd,
1275                                 u32 portid, u32 seq, int flags)
1276 {
1277         const struct devlink_ops *ops = devlink->ops;
1278         u16 pool_index;
1279         u32 threshold;
1280         void *hdr;
1281         int err;
1282
1283         err = ops->sb_tc_pool_bind_get(devlink_port, devlink_sb->index,
1284                                        tc_index, pool_type,
1285                                        &pool_index, &threshold);
1286         if (err)
1287                 return err;
1288
1289         hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
1290         if (!hdr)
1291                 return -EMSGSIZE;
1292
1293         if (devlink_nl_put_handle(msg, devlink))
1294                 goto nla_put_failure;
1295         if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, devlink_port->index))
1296                 goto nla_put_failure;
1297         if (nla_put_u32(msg, DEVLINK_ATTR_SB_INDEX, devlink_sb->index))
1298                 goto nla_put_failure;
1299         if (nla_put_u16(msg, DEVLINK_ATTR_SB_TC_INDEX, tc_index))
1300                 goto nla_put_failure;
1301         if (nla_put_u8(msg, DEVLINK_ATTR_SB_POOL_TYPE, pool_type))
1302                 goto nla_put_failure;
1303         if (nla_put_u16(msg, DEVLINK_ATTR_SB_POOL_INDEX, pool_index))
1304                 goto nla_put_failure;
1305         if (nla_put_u32(msg, DEVLINK_ATTR_SB_THRESHOLD, threshold))
1306                 goto nla_put_failure;
1307
1308         if (ops->sb_occ_tc_port_bind_get) {
1309                 u32 cur;
1310                 u32 max;
1311
1312                 err = ops->sb_occ_tc_port_bind_get(devlink_port,
1313                                                    devlink_sb->index,
1314                                                    tc_index, pool_type,
1315                                                    &cur, &max);
1316                 if (err && err != -EOPNOTSUPP)
1317                         return err;
1318                 if (!err) {
1319                         if (nla_put_u32(msg, DEVLINK_ATTR_SB_OCC_CUR, cur))
1320                                 goto nla_put_failure;
1321                         if (nla_put_u32(msg, DEVLINK_ATTR_SB_OCC_MAX, max))
1322                                 goto nla_put_failure;
1323                 }
1324         }
1325
1326         genlmsg_end(msg, hdr);
1327         return 0;
1328
1329 nla_put_failure:
1330         genlmsg_cancel(msg, hdr);
1331         return -EMSGSIZE;
1332 }
1333
1334 static int devlink_nl_cmd_sb_tc_pool_bind_get_doit(struct sk_buff *skb,
1335                                                    struct genl_info *info)
1336 {
1337         struct devlink_port *devlink_port = info->user_ptr[0];
1338         struct devlink *devlink = devlink_port->devlink;
1339         struct devlink_sb *devlink_sb = info->user_ptr[1];
1340         struct sk_buff *msg;
1341         enum devlink_sb_pool_type pool_type;
1342         u16 tc_index;
1343         int err;
1344
1345         err = devlink_sb_pool_type_get_from_info(info, &pool_type);
1346         if (err)
1347                 return err;
1348
1349         err = devlink_sb_tc_index_get_from_info(devlink_sb, info,
1350                                                 pool_type, &tc_index);
1351         if (err)
1352                 return err;
1353
1354         if (!devlink->ops || !devlink->ops->sb_tc_pool_bind_get)
1355                 return -EOPNOTSUPP;
1356
1357         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1358         if (!msg)
1359                 return -ENOMEM;
1360
1361         err = devlink_nl_sb_tc_pool_bind_fill(msg, devlink, devlink_port,
1362                                               devlink_sb, tc_index, pool_type,
1363                                               DEVLINK_CMD_SB_TC_POOL_BIND_NEW,
1364                                               info->snd_portid,
1365                                               info->snd_seq, 0);
1366         if (err) {
1367                 nlmsg_free(msg);
1368                 return err;
1369         }
1370
1371         return genlmsg_reply(msg, info);
1372 }
1373
1374 static int __sb_tc_pool_bind_get_dumpit(struct sk_buff *msg,
1375                                         int start, int *p_idx,
1376                                         struct devlink *devlink,
1377                                         struct devlink_sb *devlink_sb,
1378                                         u32 portid, u32 seq)
1379 {
1380         struct devlink_port *devlink_port;
1381         u16 tc_index;
1382         int err;
1383
1384         list_for_each_entry(devlink_port, &devlink->port_list, list) {
1385                 for (tc_index = 0;
1386                      tc_index < devlink_sb->ingress_tc_count; tc_index++) {
1387                         if (*p_idx < start) {
1388                                 (*p_idx)++;
1389                                 continue;
1390                         }
1391                         err = devlink_nl_sb_tc_pool_bind_fill(msg, devlink,
1392                                                               devlink_port,
1393                                                               devlink_sb,
1394                                                               tc_index,
1395                                                               DEVLINK_SB_POOL_TYPE_INGRESS,
1396                                                               DEVLINK_CMD_SB_TC_POOL_BIND_NEW,
1397                                                               portid, seq,
1398                                                               NLM_F_MULTI);
1399                         if (err)
1400                                 return err;
1401                         (*p_idx)++;
1402                 }
1403                 for (tc_index = 0;
1404                      tc_index < devlink_sb->egress_tc_count; tc_index++) {
1405                         if (*p_idx < start) {
1406                                 (*p_idx)++;
1407                                 continue;
1408                         }
1409                         err = devlink_nl_sb_tc_pool_bind_fill(msg, devlink,
1410                                                               devlink_port,
1411                                                               devlink_sb,
1412                                                               tc_index,
1413                                                               DEVLINK_SB_POOL_TYPE_EGRESS,
1414                                                               DEVLINK_CMD_SB_TC_POOL_BIND_NEW,
1415                                                               portid, seq,
1416                                                               NLM_F_MULTI);
1417                         if (err)
1418                                 return err;
1419                         (*p_idx)++;
1420                 }
1421         }
1422         return 0;
1423 }
1424
1425 static int
1426 devlink_nl_cmd_sb_tc_pool_bind_get_dumpit(struct sk_buff *msg,
1427                                           struct netlink_callback *cb)
1428 {
1429         struct devlink *devlink;
1430         struct devlink_sb *devlink_sb;
1431         int start = cb->args[0];
1432         int idx = 0;
1433         int err;
1434
1435         mutex_lock(&devlink_mutex);
1436         list_for_each_entry(devlink, &devlink_list, list) {
1437                 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)) ||
1438                     !devlink->ops || !devlink->ops->sb_tc_pool_bind_get)
1439                         continue;
1440
1441                 mutex_lock(&devlink->lock);
1442                 list_for_each_entry(devlink_sb, &devlink->sb_list, list) {
1443                         err = __sb_tc_pool_bind_get_dumpit(msg, start, &idx,
1444                                                            devlink,
1445                                                            devlink_sb,
1446                                                            NETLINK_CB(cb->skb).portid,
1447                                                            cb->nlh->nlmsg_seq);
1448                         if (err && err != -EOPNOTSUPP) {
1449                                 mutex_unlock(&devlink->lock);
1450                                 goto out;
1451                         }
1452                 }
1453                 mutex_unlock(&devlink->lock);
1454         }
1455 out:
1456         mutex_unlock(&devlink_mutex);
1457
1458         cb->args[0] = idx;
1459         return msg->len;
1460 }
1461
1462 static int devlink_sb_tc_pool_bind_set(struct devlink_port *devlink_port,
1463                                        unsigned int sb_index, u16 tc_index,
1464                                        enum devlink_sb_pool_type pool_type,
1465                                        u16 pool_index, u32 threshold)
1466
1467 {
1468         const struct devlink_ops *ops = devlink_port->devlink->ops;
1469
1470         if (ops && ops->sb_tc_pool_bind_set)
1471                 return ops->sb_tc_pool_bind_set(devlink_port, sb_index,
1472                                                 tc_index, pool_type,
1473                                                 pool_index, threshold);
1474         return -EOPNOTSUPP;
1475 }
1476
1477 static int devlink_nl_cmd_sb_tc_pool_bind_set_doit(struct sk_buff *skb,
1478                                                    struct genl_info *info)
1479 {
1480         struct devlink_port *devlink_port = info->user_ptr[0];
1481         struct devlink_sb *devlink_sb = info->user_ptr[1];
1482         enum devlink_sb_pool_type pool_type;
1483         u16 tc_index;
1484         u16 pool_index;
1485         u32 threshold;
1486         int err;
1487
1488         err = devlink_sb_pool_type_get_from_info(info, &pool_type);
1489         if (err)
1490                 return err;
1491
1492         err = devlink_sb_tc_index_get_from_info(devlink_sb, info,
1493                                                 pool_type, &tc_index);
1494         if (err)
1495                 return err;
1496
1497         err = devlink_sb_pool_index_get_from_info(devlink_sb, info,
1498                                                   &pool_index);
1499         if (err)
1500                 return err;
1501
1502         if (!info->attrs[DEVLINK_ATTR_SB_THRESHOLD])
1503                 return -EINVAL;
1504
1505         threshold = nla_get_u32(info->attrs[DEVLINK_ATTR_SB_THRESHOLD]);
1506         return devlink_sb_tc_pool_bind_set(devlink_port, devlink_sb->index,
1507                                            tc_index, pool_type,
1508                                            pool_index, threshold);
1509 }
1510
1511 static int devlink_nl_cmd_sb_occ_snapshot_doit(struct sk_buff *skb,
1512                                                struct genl_info *info)
1513 {
1514         struct devlink *devlink = info->user_ptr[0];
1515         struct devlink_sb *devlink_sb = info->user_ptr[1];
1516         const struct devlink_ops *ops = devlink->ops;
1517
1518         if (ops && ops->sb_occ_snapshot)
1519                 return ops->sb_occ_snapshot(devlink, devlink_sb->index);
1520         return -EOPNOTSUPP;
1521 }
1522
1523 static int devlink_nl_cmd_sb_occ_max_clear_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 && ops->sb_occ_max_clear)
1531                 return ops->sb_occ_max_clear(devlink, devlink_sb->index);
1532         return -EOPNOTSUPP;
1533 }
1534
1535 static int devlink_nl_eswitch_fill(struct sk_buff *msg, struct devlink *devlink,
1536                                    enum devlink_command cmd, u32 portid,
1537                                    u32 seq, int flags)
1538 {
1539         const struct devlink_ops *ops = devlink->ops;
1540         u8 inline_mode, encap_mode;
1541         void *hdr;
1542         int err = 0;
1543         u16 mode;
1544
1545         hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
1546         if (!hdr)
1547                 return -EMSGSIZE;
1548
1549         err = devlink_nl_put_handle(msg, devlink);
1550         if (err)
1551                 goto nla_put_failure;
1552
1553         if (ops->eswitch_mode_get) {
1554                 err = ops->eswitch_mode_get(devlink, &mode);
1555                 if (err)
1556                         goto nla_put_failure;
1557                 err = nla_put_u16(msg, DEVLINK_ATTR_ESWITCH_MODE, mode);
1558                 if (err)
1559                         goto nla_put_failure;
1560         }
1561
1562         if (ops->eswitch_inline_mode_get) {
1563                 err = ops->eswitch_inline_mode_get(devlink, &inline_mode);
1564                 if (err)
1565                         goto nla_put_failure;
1566                 err = nla_put_u8(msg, DEVLINK_ATTR_ESWITCH_INLINE_MODE,
1567                                  inline_mode);
1568                 if (err)
1569                         goto nla_put_failure;
1570         }
1571
1572         if (ops->eswitch_encap_mode_get) {
1573                 err = ops->eswitch_encap_mode_get(devlink, &encap_mode);
1574                 if (err)
1575                         goto nla_put_failure;
1576                 err = nla_put_u8(msg, DEVLINK_ATTR_ESWITCH_ENCAP_MODE, encap_mode);
1577                 if (err)
1578                         goto nla_put_failure;
1579         }
1580
1581         genlmsg_end(msg, hdr);
1582         return 0;
1583
1584 nla_put_failure:
1585         genlmsg_cancel(msg, hdr);
1586         return err;
1587 }
1588
1589 static int devlink_nl_cmd_eswitch_get_doit(struct sk_buff *skb,
1590                                            struct genl_info *info)
1591 {
1592         struct devlink *devlink = info->user_ptr[0];
1593         const struct devlink_ops *ops = devlink->ops;
1594         struct sk_buff *msg;
1595         int err;
1596
1597         if (!ops)
1598                 return -EOPNOTSUPP;
1599
1600         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1601         if (!msg)
1602                 return -ENOMEM;
1603
1604         err = devlink_nl_eswitch_fill(msg, devlink, DEVLINK_CMD_ESWITCH_GET,
1605                                       info->snd_portid, info->snd_seq, 0);
1606
1607         if (err) {
1608                 nlmsg_free(msg);
1609                 return err;
1610         }
1611
1612         return genlmsg_reply(msg, info);
1613 }
1614
1615 static int devlink_nl_cmd_eswitch_set_doit(struct sk_buff *skb,
1616                                            struct genl_info *info)
1617 {
1618         struct devlink *devlink = info->user_ptr[0];
1619         const struct devlink_ops *ops = devlink->ops;
1620         u8 inline_mode, encap_mode;
1621         int err = 0;
1622         u16 mode;
1623
1624         if (!ops)
1625                 return -EOPNOTSUPP;
1626
1627         if (info->attrs[DEVLINK_ATTR_ESWITCH_MODE]) {
1628                 if (!ops->eswitch_mode_set)
1629                         return -EOPNOTSUPP;
1630                 mode = nla_get_u16(info->attrs[DEVLINK_ATTR_ESWITCH_MODE]);
1631                 err = ops->eswitch_mode_set(devlink, mode);
1632                 if (err)
1633                         return err;
1634         }
1635
1636         if (info->attrs[DEVLINK_ATTR_ESWITCH_INLINE_MODE]) {
1637                 if (!ops->eswitch_inline_mode_set)
1638                         return -EOPNOTSUPP;
1639                 inline_mode = nla_get_u8(
1640                                 info->attrs[DEVLINK_ATTR_ESWITCH_INLINE_MODE]);
1641                 err = ops->eswitch_inline_mode_set(devlink, inline_mode);
1642                 if (err)
1643                         return err;
1644         }
1645
1646         if (info->attrs[DEVLINK_ATTR_ESWITCH_ENCAP_MODE]) {
1647                 if (!ops->eswitch_encap_mode_set)
1648                         return -EOPNOTSUPP;
1649                 encap_mode = nla_get_u8(info->attrs[DEVLINK_ATTR_ESWITCH_ENCAP_MODE]);
1650                 err = ops->eswitch_encap_mode_set(devlink, encap_mode);
1651                 if (err)
1652                         return err;
1653         }
1654
1655         return 0;
1656 }
1657
1658 int devlink_dpipe_match_put(struct sk_buff *skb,
1659                             struct devlink_dpipe_match *match)
1660 {
1661         struct devlink_dpipe_header *header = match->header;
1662         struct devlink_dpipe_field *field = &header->fields[match->field_id];
1663         struct nlattr *match_attr;
1664
1665         match_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_MATCH);
1666         if (!match_attr)
1667                 return -EMSGSIZE;
1668
1669         if (nla_put_u32(skb, DEVLINK_ATTR_DPIPE_MATCH_TYPE, match->type) ||
1670             nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_INDEX, match->header_index) ||
1671             nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_ID, header->id) ||
1672             nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_ID, field->id) ||
1673             nla_put_u8(skb, DEVLINK_ATTR_DPIPE_HEADER_GLOBAL, header->global))
1674                 goto nla_put_failure;
1675
1676         nla_nest_end(skb, match_attr);
1677         return 0;
1678
1679 nla_put_failure:
1680         nla_nest_cancel(skb, match_attr);
1681         return -EMSGSIZE;
1682 }
1683 EXPORT_SYMBOL_GPL(devlink_dpipe_match_put);
1684
1685 static int devlink_dpipe_matches_put(struct devlink_dpipe_table *table,
1686                                      struct sk_buff *skb)
1687 {
1688         struct nlattr *matches_attr;
1689
1690         matches_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_TABLE_MATCHES);
1691         if (!matches_attr)
1692                 return -EMSGSIZE;
1693
1694         if (table->table_ops->matches_dump(table->priv, skb))
1695                 goto nla_put_failure;
1696
1697         nla_nest_end(skb, matches_attr);
1698         return 0;
1699
1700 nla_put_failure:
1701         nla_nest_cancel(skb, matches_attr);
1702         return -EMSGSIZE;
1703 }
1704
1705 int devlink_dpipe_action_put(struct sk_buff *skb,
1706                              struct devlink_dpipe_action *action)
1707 {
1708         struct devlink_dpipe_header *header = action->header;
1709         struct devlink_dpipe_field *field = &header->fields[action->field_id];
1710         struct nlattr *action_attr;
1711
1712         action_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_ACTION);
1713         if (!action_attr)
1714                 return -EMSGSIZE;
1715
1716         if (nla_put_u32(skb, DEVLINK_ATTR_DPIPE_ACTION_TYPE, action->type) ||
1717             nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_INDEX, action->header_index) ||
1718             nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_ID, header->id) ||
1719             nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_ID, field->id) ||
1720             nla_put_u8(skb, DEVLINK_ATTR_DPIPE_HEADER_GLOBAL, header->global))
1721                 goto nla_put_failure;
1722
1723         nla_nest_end(skb, action_attr);
1724         return 0;
1725
1726 nla_put_failure:
1727         nla_nest_cancel(skb, action_attr);
1728         return -EMSGSIZE;
1729 }
1730 EXPORT_SYMBOL_GPL(devlink_dpipe_action_put);
1731
1732 static int devlink_dpipe_actions_put(struct devlink_dpipe_table *table,
1733                                      struct sk_buff *skb)
1734 {
1735         struct nlattr *actions_attr;
1736
1737         actions_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_TABLE_ACTIONS);
1738         if (!actions_attr)
1739                 return -EMSGSIZE;
1740
1741         if (table->table_ops->actions_dump(table->priv, skb))
1742                 goto nla_put_failure;
1743
1744         nla_nest_end(skb, actions_attr);
1745         return 0;
1746
1747 nla_put_failure:
1748         nla_nest_cancel(skb, actions_attr);
1749         return -EMSGSIZE;
1750 }
1751
1752 static int devlink_dpipe_table_put(struct sk_buff *skb,
1753                                    struct devlink_dpipe_table *table)
1754 {
1755         struct nlattr *table_attr;
1756         u64 table_size;
1757
1758         table_size = table->table_ops->size_get(table->priv);
1759         table_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_TABLE);
1760         if (!table_attr)
1761                 return -EMSGSIZE;
1762
1763         if (nla_put_string(skb, DEVLINK_ATTR_DPIPE_TABLE_NAME, table->name) ||
1764             nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_TABLE_SIZE, table_size,
1765                               DEVLINK_ATTR_PAD))
1766                 goto nla_put_failure;
1767         if (nla_put_u8(skb, DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED,
1768                        table->counters_enabled))
1769                 goto nla_put_failure;
1770
1771         if (table->resource_valid) {
1772                 if (nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_TABLE_RESOURCE_ID,
1773                                       table->resource_id, DEVLINK_ATTR_PAD) ||
1774                     nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_TABLE_RESOURCE_UNITS,
1775                                       table->resource_units, DEVLINK_ATTR_PAD))
1776                         goto nla_put_failure;
1777         }
1778         if (devlink_dpipe_matches_put(table, skb))
1779                 goto nla_put_failure;
1780
1781         if (devlink_dpipe_actions_put(table, skb))
1782                 goto nla_put_failure;
1783
1784         nla_nest_end(skb, table_attr);
1785         return 0;
1786
1787 nla_put_failure:
1788         nla_nest_cancel(skb, table_attr);
1789         return -EMSGSIZE;
1790 }
1791
1792 static int devlink_dpipe_send_and_alloc_skb(struct sk_buff **pskb,
1793                                             struct genl_info *info)
1794 {
1795         int err;
1796
1797         if (*pskb) {
1798                 err = genlmsg_reply(*pskb, info);
1799                 if (err)
1800                         return err;
1801         }
1802         *pskb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
1803         if (!*pskb)
1804                 return -ENOMEM;
1805         return 0;
1806 }
1807
1808 static int devlink_dpipe_tables_fill(struct genl_info *info,
1809                                      enum devlink_command cmd, int flags,
1810                                      struct list_head *dpipe_tables,
1811                                      const char *table_name)
1812 {
1813         struct devlink *devlink = info->user_ptr[0];
1814         struct devlink_dpipe_table *table;
1815         struct nlattr *tables_attr;
1816         struct sk_buff *skb = NULL;
1817         struct nlmsghdr *nlh;
1818         bool incomplete;
1819         void *hdr;
1820         int i;
1821         int err;
1822
1823         table = list_first_entry(dpipe_tables,
1824                                  struct devlink_dpipe_table, list);
1825 start_again:
1826         err = devlink_dpipe_send_and_alloc_skb(&skb, info);
1827         if (err)
1828                 return err;
1829
1830         hdr = genlmsg_put(skb, info->snd_portid, info->snd_seq,
1831                           &devlink_nl_family, NLM_F_MULTI, cmd);
1832         if (!hdr) {
1833                 nlmsg_free(skb);
1834                 return -EMSGSIZE;
1835         }
1836
1837         if (devlink_nl_put_handle(skb, devlink))
1838                 goto nla_put_failure;
1839         tables_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_TABLES);
1840         if (!tables_attr)
1841                 goto nla_put_failure;
1842
1843         i = 0;
1844         incomplete = false;
1845         list_for_each_entry_from(table, dpipe_tables, list) {
1846                 if (!table_name) {
1847                         err = devlink_dpipe_table_put(skb, table);
1848                         if (err) {
1849                                 if (!i)
1850                                         goto err_table_put;
1851                                 incomplete = true;
1852                                 break;
1853                         }
1854                 } else {
1855                         if (!strcmp(table->name, table_name)) {
1856                                 err = devlink_dpipe_table_put(skb, table);
1857                                 if (err)
1858                                         break;
1859                         }
1860                 }
1861                 i++;
1862         }
1863
1864         nla_nest_end(skb, tables_attr);
1865         genlmsg_end(skb, hdr);
1866         if (incomplete)
1867                 goto start_again;
1868
1869 send_done:
1870         nlh = nlmsg_put(skb, info->snd_portid, info->snd_seq,
1871                         NLMSG_DONE, 0, flags | NLM_F_MULTI);
1872         if (!nlh) {
1873                 err = devlink_dpipe_send_and_alloc_skb(&skb, info);
1874                 if (err)
1875                         return err;
1876                 goto send_done;
1877         }
1878
1879         return genlmsg_reply(skb, info);
1880
1881 nla_put_failure:
1882         err = -EMSGSIZE;
1883 err_table_put:
1884         nlmsg_free(skb);
1885         return err;
1886 }
1887
1888 static int devlink_nl_cmd_dpipe_table_get(struct sk_buff *skb,
1889                                           struct genl_info *info)
1890 {
1891         struct devlink *devlink = info->user_ptr[0];
1892         const char *table_name =  NULL;
1893
1894         if (info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME])
1895                 table_name = nla_data(info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME]);
1896
1897         return devlink_dpipe_tables_fill(info, DEVLINK_CMD_DPIPE_TABLE_GET, 0,
1898                                          &devlink->dpipe_table_list,
1899                                          table_name);
1900 }
1901
1902 static int devlink_dpipe_value_put(struct sk_buff *skb,
1903                                    struct devlink_dpipe_value *value)
1904 {
1905         if (nla_put(skb, DEVLINK_ATTR_DPIPE_VALUE,
1906                     value->value_size, value->value))
1907                 return -EMSGSIZE;
1908         if (value->mask)
1909                 if (nla_put(skb, DEVLINK_ATTR_DPIPE_VALUE_MASK,
1910                             value->value_size, value->mask))
1911                         return -EMSGSIZE;
1912         if (value->mapping_valid)
1913                 if (nla_put_u32(skb, DEVLINK_ATTR_DPIPE_VALUE_MAPPING,
1914                                 value->mapping_value))
1915                         return -EMSGSIZE;
1916         return 0;
1917 }
1918
1919 static int devlink_dpipe_action_value_put(struct sk_buff *skb,
1920                                           struct devlink_dpipe_value *value)
1921 {
1922         if (!value->action)
1923                 return -EINVAL;
1924         if (devlink_dpipe_action_put(skb, value->action))
1925                 return -EMSGSIZE;
1926         if (devlink_dpipe_value_put(skb, value))
1927                 return -EMSGSIZE;
1928         return 0;
1929 }
1930
1931 static int devlink_dpipe_action_values_put(struct sk_buff *skb,
1932                                            struct devlink_dpipe_value *values,
1933                                            unsigned int values_count)
1934 {
1935         struct nlattr *action_attr;
1936         int i;
1937         int err;
1938
1939         for (i = 0; i < values_count; i++) {
1940                 action_attr = nla_nest_start(skb,
1941                                              DEVLINK_ATTR_DPIPE_ACTION_VALUE);
1942                 if (!action_attr)
1943                         return -EMSGSIZE;
1944                 err = devlink_dpipe_action_value_put(skb, &values[i]);
1945                 if (err)
1946                         goto err_action_value_put;
1947                 nla_nest_end(skb, action_attr);
1948         }
1949         return 0;
1950
1951 err_action_value_put:
1952         nla_nest_cancel(skb, action_attr);
1953         return err;
1954 }
1955
1956 static int devlink_dpipe_match_value_put(struct sk_buff *skb,
1957                                          struct devlink_dpipe_value *value)
1958 {
1959         if (!value->match)
1960                 return -EINVAL;
1961         if (devlink_dpipe_match_put(skb, value->match))
1962                 return -EMSGSIZE;
1963         if (devlink_dpipe_value_put(skb, value))
1964                 return -EMSGSIZE;
1965         return 0;
1966 }
1967
1968 static int devlink_dpipe_match_values_put(struct sk_buff *skb,
1969                                           struct devlink_dpipe_value *values,
1970                                           unsigned int values_count)
1971 {
1972         struct nlattr *match_attr;
1973         int i;
1974         int err;
1975
1976         for (i = 0; i < values_count; i++) {
1977                 match_attr = nla_nest_start(skb,
1978                                             DEVLINK_ATTR_DPIPE_MATCH_VALUE);
1979                 if (!match_attr)
1980                         return -EMSGSIZE;
1981                 err = devlink_dpipe_match_value_put(skb, &values[i]);
1982                 if (err)
1983                         goto err_match_value_put;
1984                 nla_nest_end(skb, match_attr);
1985         }
1986         return 0;
1987
1988 err_match_value_put:
1989         nla_nest_cancel(skb, match_attr);
1990         return err;
1991 }
1992
1993 static int devlink_dpipe_entry_put(struct sk_buff *skb,
1994                                    struct devlink_dpipe_entry *entry)
1995 {
1996         struct nlattr *entry_attr, *matches_attr, *actions_attr;
1997         int err;
1998
1999         entry_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_ENTRY);
2000         if (!entry_attr)
2001                 return  -EMSGSIZE;
2002
2003         if (nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_ENTRY_INDEX, entry->index,
2004                               DEVLINK_ATTR_PAD))
2005                 goto nla_put_failure;
2006         if (entry->counter_valid)
2007                 if (nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_ENTRY_COUNTER,
2008                                       entry->counter, DEVLINK_ATTR_PAD))
2009                         goto nla_put_failure;
2010
2011         matches_attr = nla_nest_start(skb,
2012                                       DEVLINK_ATTR_DPIPE_ENTRY_MATCH_VALUES);
2013         if (!matches_attr)
2014                 goto nla_put_failure;
2015
2016         err = devlink_dpipe_match_values_put(skb, entry->match_values,
2017                                              entry->match_values_count);
2018         if (err) {
2019                 nla_nest_cancel(skb, matches_attr);
2020                 goto err_match_values_put;
2021         }
2022         nla_nest_end(skb, matches_attr);
2023
2024         actions_attr = nla_nest_start(skb,
2025                                       DEVLINK_ATTR_DPIPE_ENTRY_ACTION_VALUES);
2026         if (!actions_attr)
2027                 goto nla_put_failure;
2028
2029         err = devlink_dpipe_action_values_put(skb, entry->action_values,
2030                                               entry->action_values_count);
2031         if (err) {
2032                 nla_nest_cancel(skb, actions_attr);
2033                 goto err_action_values_put;
2034         }
2035         nla_nest_end(skb, actions_attr);
2036
2037         nla_nest_end(skb, entry_attr);
2038         return 0;
2039
2040 nla_put_failure:
2041         err = -EMSGSIZE;
2042 err_match_values_put:
2043 err_action_values_put:
2044         nla_nest_cancel(skb, entry_attr);
2045         return err;
2046 }
2047
2048 static struct devlink_dpipe_table *
2049 devlink_dpipe_table_find(struct list_head *dpipe_tables,
2050                          const char *table_name)
2051 {
2052         struct devlink_dpipe_table *table;
2053
2054         list_for_each_entry_rcu(table, dpipe_tables, list) {
2055                 if (!strcmp(table->name, table_name))
2056                         return table;
2057         }
2058         return NULL;
2059 }
2060
2061 int devlink_dpipe_entry_ctx_prepare(struct devlink_dpipe_dump_ctx *dump_ctx)
2062 {
2063         struct devlink *devlink;
2064         int err;
2065
2066         err = devlink_dpipe_send_and_alloc_skb(&dump_ctx->skb,
2067                                                dump_ctx->info);
2068         if (err)
2069                 return err;
2070
2071         dump_ctx->hdr = genlmsg_put(dump_ctx->skb,
2072                                     dump_ctx->info->snd_portid,
2073                                     dump_ctx->info->snd_seq,
2074                                     &devlink_nl_family, NLM_F_MULTI,
2075                                     dump_ctx->cmd);
2076         if (!dump_ctx->hdr)
2077                 goto nla_put_failure;
2078
2079         devlink = dump_ctx->info->user_ptr[0];
2080         if (devlink_nl_put_handle(dump_ctx->skb, devlink))
2081                 goto nla_put_failure;
2082         dump_ctx->nest = nla_nest_start(dump_ctx->skb,
2083                                         DEVLINK_ATTR_DPIPE_ENTRIES);
2084         if (!dump_ctx->nest)
2085                 goto nla_put_failure;
2086         return 0;
2087
2088 nla_put_failure:
2089         nlmsg_free(dump_ctx->skb);
2090         return -EMSGSIZE;
2091 }
2092 EXPORT_SYMBOL_GPL(devlink_dpipe_entry_ctx_prepare);
2093
2094 int devlink_dpipe_entry_ctx_append(struct devlink_dpipe_dump_ctx *dump_ctx,
2095                                    struct devlink_dpipe_entry *entry)
2096 {
2097         return devlink_dpipe_entry_put(dump_ctx->skb, entry);
2098 }
2099 EXPORT_SYMBOL_GPL(devlink_dpipe_entry_ctx_append);
2100
2101 int devlink_dpipe_entry_ctx_close(struct devlink_dpipe_dump_ctx *dump_ctx)
2102 {
2103         nla_nest_end(dump_ctx->skb, dump_ctx->nest);
2104         genlmsg_end(dump_ctx->skb, dump_ctx->hdr);
2105         return 0;
2106 }
2107 EXPORT_SYMBOL_GPL(devlink_dpipe_entry_ctx_close);
2108
2109 void devlink_dpipe_entry_clear(struct devlink_dpipe_entry *entry)
2110
2111 {
2112         unsigned int value_count, value_index;
2113         struct devlink_dpipe_value *value;
2114
2115         value = entry->action_values;
2116         value_count = entry->action_values_count;
2117         for (value_index = 0; value_index < value_count; value_index++) {
2118                 kfree(value[value_index].value);
2119                 kfree(value[value_index].mask);
2120         }
2121
2122         value = entry->match_values;
2123         value_count = entry->match_values_count;
2124         for (value_index = 0; value_index < value_count; value_index++) {
2125                 kfree(value[value_index].value);
2126                 kfree(value[value_index].mask);
2127         }
2128 }
2129 EXPORT_SYMBOL(devlink_dpipe_entry_clear);
2130
2131 static int devlink_dpipe_entries_fill(struct genl_info *info,
2132                                       enum devlink_command cmd, int flags,
2133                                       struct devlink_dpipe_table *table)
2134 {
2135         struct devlink_dpipe_dump_ctx dump_ctx;
2136         struct nlmsghdr *nlh;
2137         int err;
2138
2139         dump_ctx.skb = NULL;
2140         dump_ctx.cmd = cmd;
2141         dump_ctx.info = info;
2142
2143         err = table->table_ops->entries_dump(table->priv,
2144                                              table->counters_enabled,
2145                                              &dump_ctx);
2146         if (err)
2147                 return err;
2148
2149 send_done:
2150         nlh = nlmsg_put(dump_ctx.skb, info->snd_portid, info->snd_seq,
2151                         NLMSG_DONE, 0, flags | NLM_F_MULTI);
2152         if (!nlh) {
2153                 err = devlink_dpipe_send_and_alloc_skb(&dump_ctx.skb, info);
2154                 if (err)
2155                         return err;
2156                 goto send_done;
2157         }
2158         return genlmsg_reply(dump_ctx.skb, info);
2159 }
2160
2161 static int devlink_nl_cmd_dpipe_entries_get(struct sk_buff *skb,
2162                                             struct genl_info *info)
2163 {
2164         struct devlink *devlink = info->user_ptr[0];
2165         struct devlink_dpipe_table *table;
2166         const char *table_name;
2167
2168         if (!info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME])
2169                 return -EINVAL;
2170
2171         table_name = nla_data(info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME]);
2172         table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
2173                                          table_name);
2174         if (!table)
2175                 return -EINVAL;
2176
2177         if (!table->table_ops->entries_dump)
2178                 return -EINVAL;
2179
2180         return devlink_dpipe_entries_fill(info, DEVLINK_CMD_DPIPE_ENTRIES_GET,
2181                                           0, table);
2182 }
2183
2184 static int devlink_dpipe_fields_put(struct sk_buff *skb,
2185                                     const struct devlink_dpipe_header *header)
2186 {
2187         struct devlink_dpipe_field *field;
2188         struct nlattr *field_attr;
2189         int i;
2190
2191         for (i = 0; i < header->fields_count; i++) {
2192                 field = &header->fields[i];
2193                 field_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_FIELD);
2194                 if (!field_attr)
2195                         return -EMSGSIZE;
2196                 if (nla_put_string(skb, DEVLINK_ATTR_DPIPE_FIELD_NAME, field->name) ||
2197                     nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_ID, field->id) ||
2198                     nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_BITWIDTH, field->bitwidth) ||
2199                     nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_MAPPING_TYPE, field->mapping_type))
2200                         goto nla_put_failure;
2201                 nla_nest_end(skb, field_attr);
2202         }
2203         return 0;
2204
2205 nla_put_failure:
2206         nla_nest_cancel(skb, field_attr);
2207         return -EMSGSIZE;
2208 }
2209
2210 static int devlink_dpipe_header_put(struct sk_buff *skb,
2211                                     struct devlink_dpipe_header *header)
2212 {
2213         struct nlattr *fields_attr, *header_attr;
2214         int err;
2215
2216         header_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_HEADER);
2217         if (!header_attr)
2218                 return -EMSGSIZE;
2219
2220         if (nla_put_string(skb, DEVLINK_ATTR_DPIPE_HEADER_NAME, header->name) ||
2221             nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_ID, header->id) ||
2222             nla_put_u8(skb, DEVLINK_ATTR_DPIPE_HEADER_GLOBAL, header->global))
2223                 goto nla_put_failure;
2224
2225         fields_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_HEADER_FIELDS);
2226         if (!fields_attr)
2227                 goto nla_put_failure;
2228
2229         err = devlink_dpipe_fields_put(skb, header);
2230         if (err) {
2231                 nla_nest_cancel(skb, fields_attr);
2232                 goto nla_put_failure;
2233         }
2234         nla_nest_end(skb, fields_attr);
2235         nla_nest_end(skb, header_attr);
2236         return 0;
2237
2238 nla_put_failure:
2239         err = -EMSGSIZE;
2240         nla_nest_cancel(skb, header_attr);
2241         return err;
2242 }
2243
2244 static int devlink_dpipe_headers_fill(struct genl_info *info,
2245                                       enum devlink_command cmd, int flags,
2246                                       struct devlink_dpipe_headers *
2247                                       dpipe_headers)
2248 {
2249         struct devlink *devlink = info->user_ptr[0];
2250         struct nlattr *headers_attr;
2251         struct sk_buff *skb = NULL;
2252         struct nlmsghdr *nlh;
2253         void *hdr;
2254         int i, j;
2255         int err;
2256
2257         i = 0;
2258 start_again:
2259         err = devlink_dpipe_send_and_alloc_skb(&skb, info);
2260         if (err)
2261                 return err;
2262
2263         hdr = genlmsg_put(skb, info->snd_portid, info->snd_seq,
2264                           &devlink_nl_family, NLM_F_MULTI, cmd);
2265         if (!hdr) {
2266                 nlmsg_free(skb);
2267                 return -EMSGSIZE;
2268         }
2269
2270         if (devlink_nl_put_handle(skb, devlink))
2271                 goto nla_put_failure;
2272         headers_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_HEADERS);
2273         if (!headers_attr)
2274                 goto nla_put_failure;
2275
2276         j = 0;
2277         for (; i < dpipe_headers->headers_count; i++) {
2278                 err = devlink_dpipe_header_put(skb, dpipe_headers->headers[i]);
2279                 if (err) {
2280                         if (!j)
2281                                 goto err_table_put;
2282                         break;
2283                 }
2284                 j++;
2285         }
2286         nla_nest_end(skb, headers_attr);
2287         genlmsg_end(skb, hdr);
2288         if (i != dpipe_headers->headers_count)
2289                 goto start_again;
2290
2291 send_done:
2292         nlh = nlmsg_put(skb, info->snd_portid, info->snd_seq,
2293                         NLMSG_DONE, 0, flags | NLM_F_MULTI);
2294         if (!nlh) {
2295                 err = devlink_dpipe_send_and_alloc_skb(&skb, info);
2296                 if (err)
2297                         return err;
2298                 goto send_done;
2299         }
2300         return genlmsg_reply(skb, info);
2301
2302 nla_put_failure:
2303         err = -EMSGSIZE;
2304 err_table_put:
2305         nlmsg_free(skb);
2306         return err;
2307 }
2308
2309 static int devlink_nl_cmd_dpipe_headers_get(struct sk_buff *skb,
2310                                             struct genl_info *info)
2311 {
2312         struct devlink *devlink = info->user_ptr[0];
2313
2314         if (!devlink->dpipe_headers)
2315                 return -EOPNOTSUPP;
2316         return devlink_dpipe_headers_fill(info, DEVLINK_CMD_DPIPE_HEADERS_GET,
2317                                           0, devlink->dpipe_headers);
2318 }
2319
2320 static int devlink_dpipe_table_counters_set(struct devlink *devlink,
2321                                             const char *table_name,
2322                                             bool enable)
2323 {
2324         struct devlink_dpipe_table *table;
2325
2326         table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
2327                                          table_name);
2328         if (!table)
2329                 return -EINVAL;
2330
2331         if (table->counter_control_extern)
2332                 return -EOPNOTSUPP;
2333
2334         if (!(table->counters_enabled ^ enable))
2335                 return 0;
2336
2337         table->counters_enabled = enable;
2338         if (table->table_ops->counters_set_update)
2339                 table->table_ops->counters_set_update(table->priv, enable);
2340         return 0;
2341 }
2342
2343 static int devlink_nl_cmd_dpipe_table_counters_set(struct sk_buff *skb,
2344                                                    struct genl_info *info)
2345 {
2346         struct devlink *devlink = info->user_ptr[0];
2347         const char *table_name;
2348         bool counters_enable;
2349
2350         if (!info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME] ||
2351             !info->attrs[DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED])
2352                 return -EINVAL;
2353
2354         table_name = nla_data(info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME]);
2355         counters_enable = !!nla_get_u8(info->attrs[DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED]);
2356
2357         return devlink_dpipe_table_counters_set(devlink, table_name,
2358                                                 counters_enable);
2359 }
2360
2361 static struct devlink_resource *
2362 devlink_resource_find(struct devlink *devlink,
2363                       struct devlink_resource *resource, u64 resource_id)
2364 {
2365         struct list_head *resource_list;
2366
2367         if (resource)
2368                 resource_list = &resource->resource_list;
2369         else
2370                 resource_list = &devlink->resource_list;
2371
2372         list_for_each_entry(resource, resource_list, list) {
2373                 struct devlink_resource *child_resource;
2374
2375                 if (resource->id == resource_id)
2376                         return resource;
2377
2378                 child_resource = devlink_resource_find(devlink, resource,
2379                                                        resource_id);
2380                 if (child_resource)
2381                         return child_resource;
2382         }
2383         return NULL;
2384 }
2385
2386 static void
2387 devlink_resource_validate_children(struct devlink_resource *resource)
2388 {
2389         struct devlink_resource *child_resource;
2390         bool size_valid = true;
2391         u64 parts_size = 0;
2392
2393         if (list_empty(&resource->resource_list))
2394                 goto out;
2395
2396         list_for_each_entry(child_resource, &resource->resource_list, list)
2397                 parts_size += child_resource->size_new;
2398
2399         if (parts_size > resource->size_new)
2400                 size_valid = false;
2401 out:
2402         resource->size_valid = size_valid;
2403 }
2404
2405 static int
2406 devlink_resource_validate_size(struct devlink_resource *resource, u64 size,
2407                                struct netlink_ext_ack *extack)
2408 {
2409         u64 reminder;
2410         int err = 0;
2411
2412         if (size > resource->size_params.size_max) {
2413                 NL_SET_ERR_MSG_MOD(extack, "Size larger than maximum");
2414                 err = -EINVAL;
2415         }
2416
2417         if (size < resource->size_params.size_min) {
2418                 NL_SET_ERR_MSG_MOD(extack, "Size smaller than minimum");
2419                 err = -EINVAL;
2420         }
2421
2422         div64_u64_rem(size, resource->size_params.size_granularity, &reminder);
2423         if (reminder) {
2424                 NL_SET_ERR_MSG_MOD(extack, "Wrong granularity");
2425                 err = -EINVAL;
2426         }
2427
2428         return err;
2429 }
2430
2431 static int devlink_nl_cmd_resource_set(struct sk_buff *skb,
2432                                        struct genl_info *info)
2433 {
2434         struct devlink *devlink = info->user_ptr[0];
2435         struct devlink_resource *resource;
2436         u64 resource_id;
2437         u64 size;
2438         int err;
2439
2440         if (!info->attrs[DEVLINK_ATTR_RESOURCE_ID] ||
2441             !info->attrs[DEVLINK_ATTR_RESOURCE_SIZE])
2442                 return -EINVAL;
2443         resource_id = nla_get_u64(info->attrs[DEVLINK_ATTR_RESOURCE_ID]);
2444
2445         resource = devlink_resource_find(devlink, NULL, resource_id);
2446         if (!resource)
2447                 return -EINVAL;
2448
2449         size = nla_get_u64(info->attrs[DEVLINK_ATTR_RESOURCE_SIZE]);
2450         err = devlink_resource_validate_size(resource, size, info->extack);
2451         if (err)
2452                 return err;
2453
2454         resource->size_new = size;
2455         devlink_resource_validate_children(resource);
2456         if (resource->parent)
2457                 devlink_resource_validate_children(resource->parent);
2458         return 0;
2459 }
2460
2461 static int
2462 devlink_resource_size_params_put(struct devlink_resource *resource,
2463                                  struct sk_buff *skb)
2464 {
2465         struct devlink_resource_size_params *size_params;
2466
2467         size_params = &resource->size_params;
2468         if (nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_SIZE_GRAN,
2469                               size_params->size_granularity, DEVLINK_ATTR_PAD) ||
2470             nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_SIZE_MAX,
2471                               size_params->size_max, DEVLINK_ATTR_PAD) ||
2472             nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_SIZE_MIN,
2473                               size_params->size_min, DEVLINK_ATTR_PAD) ||
2474             nla_put_u8(skb, DEVLINK_ATTR_RESOURCE_UNIT, size_params->unit))
2475                 return -EMSGSIZE;
2476         return 0;
2477 }
2478
2479 static int devlink_resource_occ_put(struct devlink_resource *resource,
2480                                     struct sk_buff *skb)
2481 {
2482         if (!resource->occ_get)
2483                 return 0;
2484         return nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_OCC,
2485                                  resource->occ_get(resource->occ_get_priv),
2486                                  DEVLINK_ATTR_PAD);
2487 }
2488
2489 static int devlink_resource_put(struct devlink *devlink, struct sk_buff *skb,
2490                                 struct devlink_resource *resource)
2491 {
2492         struct devlink_resource *child_resource;
2493         struct nlattr *child_resource_attr;
2494         struct nlattr *resource_attr;
2495
2496         resource_attr = nla_nest_start(skb, DEVLINK_ATTR_RESOURCE);
2497         if (!resource_attr)
2498                 return -EMSGSIZE;
2499
2500         if (nla_put_string(skb, DEVLINK_ATTR_RESOURCE_NAME, resource->name) ||
2501             nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_SIZE, resource->size,
2502                               DEVLINK_ATTR_PAD) ||
2503             nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_ID, resource->id,
2504                               DEVLINK_ATTR_PAD))
2505                 goto nla_put_failure;
2506         if (resource->size != resource->size_new)
2507                 nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_SIZE_NEW,
2508                                   resource->size_new, DEVLINK_ATTR_PAD);
2509         if (devlink_resource_occ_put(resource, skb))
2510                 goto nla_put_failure;
2511         if (devlink_resource_size_params_put(resource, skb))
2512                 goto nla_put_failure;
2513         if (list_empty(&resource->resource_list))
2514                 goto out;
2515
2516         if (nla_put_u8(skb, DEVLINK_ATTR_RESOURCE_SIZE_VALID,
2517                        resource->size_valid))
2518                 goto nla_put_failure;
2519
2520         child_resource_attr = nla_nest_start(skb, DEVLINK_ATTR_RESOURCE_LIST);
2521         if (!child_resource_attr)
2522                 goto nla_put_failure;
2523
2524         list_for_each_entry(child_resource, &resource->resource_list, list) {
2525                 if (devlink_resource_put(devlink, skb, child_resource))
2526                         goto resource_put_failure;
2527         }
2528
2529         nla_nest_end(skb, child_resource_attr);
2530 out:
2531         nla_nest_end(skb, resource_attr);
2532         return 0;
2533
2534 resource_put_failure:
2535         nla_nest_cancel(skb, child_resource_attr);
2536 nla_put_failure:
2537         nla_nest_cancel(skb, resource_attr);
2538         return -EMSGSIZE;
2539 }
2540
2541 static int devlink_resource_fill(struct genl_info *info,
2542                                  enum devlink_command cmd, int flags)
2543 {
2544         struct devlink *devlink = info->user_ptr[0];
2545         struct devlink_resource *resource;
2546         struct nlattr *resources_attr;
2547         struct sk_buff *skb = NULL;
2548         struct nlmsghdr *nlh;
2549         bool incomplete;
2550         void *hdr;
2551         int i;
2552         int err;
2553
2554         resource = list_first_entry(&devlink->resource_list,
2555                                     struct devlink_resource, list);
2556 start_again:
2557         err = devlink_dpipe_send_and_alloc_skb(&skb, info);
2558         if (err)
2559                 return err;
2560
2561         hdr = genlmsg_put(skb, info->snd_portid, info->snd_seq,
2562                           &devlink_nl_family, NLM_F_MULTI, cmd);
2563         if (!hdr) {
2564                 nlmsg_free(skb);
2565                 return -EMSGSIZE;
2566         }
2567
2568         if (devlink_nl_put_handle(skb, devlink))
2569                 goto nla_put_failure;
2570
2571         resources_attr = nla_nest_start(skb, DEVLINK_ATTR_RESOURCE_LIST);
2572         if (!resources_attr)
2573                 goto nla_put_failure;
2574
2575         incomplete = false;
2576         i = 0;
2577         list_for_each_entry_from(resource, &devlink->resource_list, list) {
2578                 err = devlink_resource_put(devlink, skb, resource);
2579                 if (err) {
2580                         if (!i)
2581                                 goto err_resource_put;
2582                         incomplete = true;
2583                         break;
2584                 }
2585                 i++;
2586         }
2587         nla_nest_end(skb, resources_attr);
2588         genlmsg_end(skb, hdr);
2589         if (incomplete)
2590                 goto start_again;
2591 send_done:
2592         nlh = nlmsg_put(skb, info->snd_portid, info->snd_seq,
2593                         NLMSG_DONE, 0, flags | NLM_F_MULTI);
2594         if (!nlh) {
2595                 err = devlink_dpipe_send_and_alloc_skb(&skb, info);
2596                 if (err)
2597                         return err;
2598                 goto send_done;
2599         }
2600         return genlmsg_reply(skb, info);
2601
2602 nla_put_failure:
2603         err = -EMSGSIZE;
2604 err_resource_put:
2605         nlmsg_free(skb);
2606         return err;
2607 }
2608
2609 static int devlink_nl_cmd_resource_dump(struct sk_buff *skb,
2610                                         struct genl_info *info)
2611 {
2612         struct devlink *devlink = info->user_ptr[0];
2613
2614         if (list_empty(&devlink->resource_list))
2615                 return -EOPNOTSUPP;
2616
2617         return devlink_resource_fill(info, DEVLINK_CMD_RESOURCE_DUMP, 0);
2618 }
2619
2620 static int
2621 devlink_resources_validate(struct devlink *devlink,
2622                            struct devlink_resource *resource,
2623                            struct genl_info *info)
2624 {
2625         struct list_head *resource_list;
2626         int err = 0;
2627
2628         if (resource)
2629                 resource_list = &resource->resource_list;
2630         else
2631                 resource_list = &devlink->resource_list;
2632
2633         list_for_each_entry(resource, resource_list, list) {
2634                 if (!resource->size_valid)
2635                         return -EINVAL;
2636                 err = devlink_resources_validate(devlink, resource, info);
2637                 if (err)
2638                         return err;
2639         }
2640         return err;
2641 }
2642
2643 static int devlink_nl_cmd_reload(struct sk_buff *skb, struct genl_info *info)
2644 {
2645         struct devlink *devlink = info->user_ptr[0];
2646         int err;
2647
2648         if (!devlink->ops->reload)
2649                 return -EOPNOTSUPP;
2650
2651         err = devlink_resources_validate(devlink, NULL, info);
2652         if (err) {
2653                 NL_SET_ERR_MSG_MOD(info->extack, "resources size validation failed");
2654                 return err;
2655         }
2656         return devlink->ops->reload(devlink, info->extack);
2657 }
2658
2659 static const struct devlink_param devlink_param_generic[] = {
2660         {
2661                 .id = DEVLINK_PARAM_GENERIC_ID_INT_ERR_RESET,
2662                 .name = DEVLINK_PARAM_GENERIC_INT_ERR_RESET_NAME,
2663                 .type = DEVLINK_PARAM_GENERIC_INT_ERR_RESET_TYPE,
2664         },
2665         {
2666                 .id = DEVLINK_PARAM_GENERIC_ID_MAX_MACS,
2667                 .name = DEVLINK_PARAM_GENERIC_MAX_MACS_NAME,
2668                 .type = DEVLINK_PARAM_GENERIC_MAX_MACS_TYPE,
2669         },
2670         {
2671                 .id = DEVLINK_PARAM_GENERIC_ID_ENABLE_SRIOV,
2672                 .name = DEVLINK_PARAM_GENERIC_ENABLE_SRIOV_NAME,
2673                 .type = DEVLINK_PARAM_GENERIC_ENABLE_SRIOV_TYPE,
2674         },
2675         {
2676                 .id = DEVLINK_PARAM_GENERIC_ID_REGION_SNAPSHOT,
2677                 .name = DEVLINK_PARAM_GENERIC_REGION_SNAPSHOT_NAME,
2678                 .type = DEVLINK_PARAM_GENERIC_REGION_SNAPSHOT_TYPE,
2679         },
2680 };
2681
2682 static int devlink_param_generic_verify(const struct devlink_param *param)
2683 {
2684         /* verify it match generic parameter by id and name */
2685         if (param->id > DEVLINK_PARAM_GENERIC_ID_MAX)
2686                 return -EINVAL;
2687         if (strcmp(param->name, devlink_param_generic[param->id].name))
2688                 return -ENOENT;
2689
2690         WARN_ON(param->type != devlink_param_generic[param->id].type);
2691
2692         return 0;
2693 }
2694
2695 static int devlink_param_driver_verify(const struct devlink_param *param)
2696 {
2697         int i;
2698
2699         if (param->id <= DEVLINK_PARAM_GENERIC_ID_MAX)
2700                 return -EINVAL;
2701         /* verify no such name in generic params */
2702         for (i = 0; i <= DEVLINK_PARAM_GENERIC_ID_MAX; i++)
2703                 if (!strcmp(param->name, devlink_param_generic[i].name))
2704                         return -EEXIST;
2705
2706         return 0;
2707 }
2708
2709 static struct devlink_param_item *
2710 devlink_param_find_by_name(struct list_head *param_list,
2711                            const char *param_name)
2712 {
2713         struct devlink_param_item *param_item;
2714
2715         list_for_each_entry(param_item, param_list, list)
2716                 if (!strcmp(param_item->param->name, param_name))
2717                         return param_item;
2718         return NULL;
2719 }
2720
2721 static struct devlink_param_item *
2722 devlink_param_find_by_id(struct list_head *param_list, u32 param_id)
2723 {
2724         struct devlink_param_item *param_item;
2725
2726         list_for_each_entry(param_item, param_list, list)
2727                 if (param_item->param->id == param_id)
2728                         return param_item;
2729         return NULL;
2730 }
2731
2732 static bool
2733 devlink_param_cmode_is_supported(const struct devlink_param *param,
2734                                  enum devlink_param_cmode cmode)
2735 {
2736         return test_bit(cmode, &param->supported_cmodes);
2737 }
2738
2739 static int devlink_param_get(struct devlink *devlink,
2740                              const struct devlink_param *param,
2741                              struct devlink_param_gset_ctx *ctx)
2742 {
2743         if (!param->get)
2744                 return -EOPNOTSUPP;
2745         return param->get(devlink, param->id, ctx);
2746 }
2747
2748 static int devlink_param_set(struct devlink *devlink,
2749                              const struct devlink_param *param,
2750                              struct devlink_param_gset_ctx *ctx)
2751 {
2752         if (!param->set)
2753                 return -EOPNOTSUPP;
2754         return param->set(devlink, param->id, ctx);
2755 }
2756
2757 static int
2758 devlink_param_type_to_nla_type(enum devlink_param_type param_type)
2759 {
2760         switch (param_type) {
2761         case DEVLINK_PARAM_TYPE_U8:
2762                 return NLA_U8;
2763         case DEVLINK_PARAM_TYPE_U16:
2764                 return NLA_U16;
2765         case DEVLINK_PARAM_TYPE_U32:
2766                 return NLA_U32;
2767         case DEVLINK_PARAM_TYPE_STRING:
2768                 return NLA_STRING;
2769         case DEVLINK_PARAM_TYPE_BOOL:
2770                 return NLA_FLAG;
2771         default:
2772                 return -EINVAL;
2773         }
2774 }
2775
2776 static int
2777 devlink_nl_param_value_fill_one(struct sk_buff *msg,
2778                                 enum devlink_param_type type,
2779                                 enum devlink_param_cmode cmode,
2780                                 union devlink_param_value val)
2781 {
2782         struct nlattr *param_value_attr;
2783
2784         param_value_attr = nla_nest_start(msg, DEVLINK_ATTR_PARAM_VALUE);
2785         if (!param_value_attr)
2786                 goto nla_put_failure;
2787
2788         if (nla_put_u8(msg, DEVLINK_ATTR_PARAM_VALUE_CMODE, cmode))
2789                 goto value_nest_cancel;
2790
2791         switch (type) {
2792         case DEVLINK_PARAM_TYPE_U8:
2793                 if (nla_put_u8(msg, DEVLINK_ATTR_PARAM_VALUE_DATA, val.vu8))
2794                         goto value_nest_cancel;
2795                 break;
2796         case DEVLINK_PARAM_TYPE_U16:
2797                 if (nla_put_u16(msg, DEVLINK_ATTR_PARAM_VALUE_DATA, val.vu16))
2798                         goto value_nest_cancel;
2799                 break;
2800         case DEVLINK_PARAM_TYPE_U32:
2801                 if (nla_put_u32(msg, DEVLINK_ATTR_PARAM_VALUE_DATA, val.vu32))
2802                         goto value_nest_cancel;
2803                 break;
2804         case DEVLINK_PARAM_TYPE_STRING:
2805                 if (nla_put_string(msg, DEVLINK_ATTR_PARAM_VALUE_DATA,
2806                                    val.vstr))
2807                         goto value_nest_cancel;
2808                 break;
2809         case DEVLINK_PARAM_TYPE_BOOL:
2810                 if (val.vbool &&
2811                     nla_put_flag(msg, DEVLINK_ATTR_PARAM_VALUE_DATA))
2812                         goto value_nest_cancel;
2813                 break;
2814         }
2815
2816         nla_nest_end(msg, param_value_attr);
2817         return 0;
2818
2819 value_nest_cancel:
2820         nla_nest_cancel(msg, param_value_attr);
2821 nla_put_failure:
2822         return -EMSGSIZE;
2823 }
2824
2825 static int devlink_nl_param_fill(struct sk_buff *msg, struct devlink *devlink,
2826                                  struct devlink_param_item *param_item,
2827                                  enum devlink_command cmd,
2828                                  u32 portid, u32 seq, int flags)
2829 {
2830         union devlink_param_value param_value[DEVLINK_PARAM_CMODE_MAX + 1];
2831         const struct devlink_param *param = param_item->param;
2832         struct devlink_param_gset_ctx ctx;
2833         struct nlattr *param_values_list;
2834         struct nlattr *param_attr;
2835         int nla_type;
2836         void *hdr;
2837         int err;
2838         int i;
2839
2840         /* Get value from driver part to driverinit configuration mode */
2841         for (i = 0; i <= DEVLINK_PARAM_CMODE_MAX; i++) {
2842                 if (!devlink_param_cmode_is_supported(param, i))
2843                         continue;
2844                 if (i == DEVLINK_PARAM_CMODE_DRIVERINIT) {
2845                         if (!param_item->driverinit_value_valid)
2846                                 return -EOPNOTSUPP;
2847                         param_value[i] = param_item->driverinit_value;
2848                 } else {
2849                         ctx.cmode = i;
2850                         err = devlink_param_get(devlink, param, &ctx);
2851                         if (err)
2852                                 return err;
2853                         param_value[i] = ctx.val;
2854                 }
2855         }
2856
2857         hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
2858         if (!hdr)
2859                 return -EMSGSIZE;
2860
2861         if (devlink_nl_put_handle(msg, devlink))
2862                 goto genlmsg_cancel;
2863         param_attr = nla_nest_start(msg, DEVLINK_ATTR_PARAM);
2864         if (!param_attr)
2865                 goto genlmsg_cancel;
2866         if (nla_put_string(msg, DEVLINK_ATTR_PARAM_NAME, param->name))
2867                 goto param_nest_cancel;
2868         if (param->generic && nla_put_flag(msg, DEVLINK_ATTR_PARAM_GENERIC))
2869                 goto param_nest_cancel;
2870
2871         nla_type = devlink_param_type_to_nla_type(param->type);
2872         if (nla_type < 0)
2873                 goto param_nest_cancel;
2874         if (nla_put_u8(msg, DEVLINK_ATTR_PARAM_TYPE, nla_type))
2875                 goto param_nest_cancel;
2876
2877         param_values_list = nla_nest_start(msg, DEVLINK_ATTR_PARAM_VALUES_LIST);
2878         if (!param_values_list)
2879                 goto param_nest_cancel;
2880
2881         for (i = 0; i <= DEVLINK_PARAM_CMODE_MAX; i++) {
2882                 if (!devlink_param_cmode_is_supported(param, i))
2883                         continue;
2884                 err = devlink_nl_param_value_fill_one(msg, param->type,
2885                                                       i, param_value[i]);
2886                 if (err)
2887                         goto values_list_nest_cancel;
2888         }
2889
2890         nla_nest_end(msg, param_values_list);
2891         nla_nest_end(msg, param_attr);
2892         genlmsg_end(msg, hdr);
2893         return 0;
2894
2895 values_list_nest_cancel:
2896         nla_nest_end(msg, param_values_list);
2897 param_nest_cancel:
2898         nla_nest_cancel(msg, param_attr);
2899 genlmsg_cancel:
2900         genlmsg_cancel(msg, hdr);
2901         return -EMSGSIZE;
2902 }
2903
2904 static void devlink_param_notify(struct devlink *devlink,
2905                                  struct devlink_param_item *param_item,
2906                                  enum devlink_command cmd)
2907 {
2908         struct sk_buff *msg;
2909         int err;
2910
2911         WARN_ON(cmd != DEVLINK_CMD_PARAM_NEW && cmd != DEVLINK_CMD_PARAM_DEL);
2912
2913         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2914         if (!msg)
2915                 return;
2916         err = devlink_nl_param_fill(msg, devlink, param_item, cmd, 0, 0, 0);
2917         if (err) {
2918                 nlmsg_free(msg);
2919                 return;
2920         }
2921
2922         genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
2923                                 msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
2924 }
2925
2926 static int devlink_nl_cmd_param_get_dumpit(struct sk_buff *msg,
2927                                            struct netlink_callback *cb)
2928 {
2929         struct devlink_param_item *param_item;
2930         struct devlink *devlink;
2931         int start = cb->args[0];
2932         int idx = 0;
2933         int err;
2934
2935         mutex_lock(&devlink_mutex);
2936         list_for_each_entry(devlink, &devlink_list, list) {
2937                 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
2938                         continue;
2939                 mutex_lock(&devlink->lock);
2940                 list_for_each_entry(param_item, &devlink->param_list, list) {
2941                         if (idx < start) {
2942                                 idx++;
2943                                 continue;
2944                         }
2945                         err = devlink_nl_param_fill(msg, devlink, param_item,
2946                                                     DEVLINK_CMD_PARAM_GET,
2947                                                     NETLINK_CB(cb->skb).portid,
2948                                                     cb->nlh->nlmsg_seq,
2949                                                     NLM_F_MULTI);
2950                         if (err) {
2951                                 mutex_unlock(&devlink->lock);
2952                                 goto out;
2953                         }
2954                         idx++;
2955                 }
2956                 mutex_unlock(&devlink->lock);
2957         }
2958 out:
2959         mutex_unlock(&devlink_mutex);
2960
2961         cb->args[0] = idx;
2962         return msg->len;
2963 }
2964
2965 static int
2966 devlink_param_type_get_from_info(struct genl_info *info,
2967                                  enum devlink_param_type *param_type)
2968 {
2969         if (!info->attrs[DEVLINK_ATTR_PARAM_TYPE])
2970                 return -EINVAL;
2971
2972         switch (nla_get_u8(info->attrs[DEVLINK_ATTR_PARAM_TYPE])) {
2973         case NLA_U8:
2974                 *param_type = DEVLINK_PARAM_TYPE_U8;
2975                 break;
2976         case NLA_U16:
2977                 *param_type = DEVLINK_PARAM_TYPE_U16;
2978                 break;
2979         case NLA_U32:
2980                 *param_type = DEVLINK_PARAM_TYPE_U32;
2981                 break;
2982         case NLA_STRING:
2983                 *param_type = DEVLINK_PARAM_TYPE_STRING;
2984                 break;
2985         case NLA_FLAG:
2986                 *param_type = DEVLINK_PARAM_TYPE_BOOL;
2987                 break;
2988         default:
2989                 return -EINVAL;
2990         }
2991
2992         return 0;
2993 }
2994
2995 static int
2996 devlink_param_value_get_from_info(const struct devlink_param *param,
2997                                   struct genl_info *info,
2998                                   union devlink_param_value *value)
2999 {
3000         struct nlattr *param_data;
3001         int len;
3002
3003         param_data = info->attrs[DEVLINK_ATTR_PARAM_VALUE_DATA];
3004
3005         if (param->type != DEVLINK_PARAM_TYPE_BOOL && !param_data)
3006                 return -EINVAL;
3007
3008         switch (param->type) {
3009         case DEVLINK_PARAM_TYPE_U8:
3010                 if (nla_len(param_data) != sizeof(u8))
3011                         return -EINVAL;
3012                 value->vu8 = nla_get_u8(param_data);
3013                 break;
3014         case DEVLINK_PARAM_TYPE_U16:
3015                 if (nla_len(param_data) != sizeof(u16))
3016                         return -EINVAL;
3017                 value->vu16 = nla_get_u16(param_data);
3018                 break;
3019         case DEVLINK_PARAM_TYPE_U32:
3020                 if (nla_len(param_data) != sizeof(u32))
3021                         return -EINVAL;
3022                 value->vu32 = nla_get_u32(param_data);
3023                 break;
3024         case DEVLINK_PARAM_TYPE_STRING:
3025                 len = strnlen(nla_data(param_data), nla_len(param_data));
3026                 if (len == nla_len(param_data) ||
3027                     len >= __DEVLINK_PARAM_MAX_STRING_VALUE)
3028                         return -EINVAL;
3029                 strcpy(value->vstr, nla_data(param_data));
3030                 break;
3031         case DEVLINK_PARAM_TYPE_BOOL:
3032                 if (param_data && nla_len(param_data))
3033                         return -EINVAL;
3034                 value->vbool = nla_get_flag(param_data);
3035                 break;
3036         }
3037         return 0;
3038 }
3039
3040 static struct devlink_param_item *
3041 devlink_param_get_from_info(struct devlink *devlink,
3042                             struct genl_info *info)
3043 {
3044         char *param_name;
3045
3046         if (!info->attrs[DEVLINK_ATTR_PARAM_NAME])
3047                 return NULL;
3048
3049         param_name = nla_data(info->attrs[DEVLINK_ATTR_PARAM_NAME]);
3050         return devlink_param_find_by_name(&devlink->param_list, param_name);
3051 }
3052
3053 static int devlink_nl_cmd_param_get_doit(struct sk_buff *skb,
3054                                          struct genl_info *info)
3055 {
3056         struct devlink *devlink = info->user_ptr[0];
3057         struct devlink_param_item *param_item;
3058         struct sk_buff *msg;
3059         int err;
3060
3061         param_item = devlink_param_get_from_info(devlink, info);
3062         if (!param_item)
3063                 return -EINVAL;
3064
3065         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
3066         if (!msg)
3067                 return -ENOMEM;
3068
3069         err = devlink_nl_param_fill(msg, devlink, param_item,
3070                                     DEVLINK_CMD_PARAM_GET,
3071                                     info->snd_portid, info->snd_seq, 0);
3072         if (err) {
3073                 nlmsg_free(msg);
3074                 return err;
3075         }
3076
3077         return genlmsg_reply(msg, info);
3078 }
3079
3080 static int devlink_nl_cmd_param_set_doit(struct sk_buff *skb,
3081                                          struct genl_info *info)
3082 {
3083         struct devlink *devlink = info->user_ptr[0];
3084         enum devlink_param_type param_type;
3085         struct devlink_param_gset_ctx ctx;
3086         enum devlink_param_cmode cmode;
3087         struct devlink_param_item *param_item;
3088         const struct devlink_param *param;
3089         union devlink_param_value value;
3090         int err = 0;
3091
3092         param_item = devlink_param_get_from_info(devlink, info);
3093         if (!param_item)
3094                 return -EINVAL;
3095         param = param_item->param;
3096         err = devlink_param_type_get_from_info(info, &param_type);
3097         if (err)
3098                 return err;
3099         if (param_type != param->type)
3100                 return -EINVAL;
3101         err = devlink_param_value_get_from_info(param, info, &value);
3102         if (err)
3103                 return err;
3104         if (param->validate) {
3105                 err = param->validate(devlink, param->id, value, info->extack);
3106                 if (err)
3107                         return err;
3108         }
3109
3110         if (!info->attrs[DEVLINK_ATTR_PARAM_VALUE_CMODE])
3111                 return -EINVAL;
3112         cmode = nla_get_u8(info->attrs[DEVLINK_ATTR_PARAM_VALUE_CMODE]);
3113         if (!devlink_param_cmode_is_supported(param, cmode))
3114                 return -EOPNOTSUPP;
3115
3116         if (cmode == DEVLINK_PARAM_CMODE_DRIVERINIT) {
3117                 if (param->type == DEVLINK_PARAM_TYPE_STRING)
3118                         strcpy(param_item->driverinit_value.vstr, value.vstr);
3119                 else
3120                         param_item->driverinit_value = value;
3121                 param_item->driverinit_value_valid = true;
3122         } else {
3123                 if (!param->set)
3124                         return -EOPNOTSUPP;
3125                 ctx.val = value;
3126                 ctx.cmode = cmode;
3127                 err = devlink_param_set(devlink, param, &ctx);
3128                 if (err)
3129                         return err;
3130         }
3131
3132         devlink_param_notify(devlink, param_item, DEVLINK_CMD_PARAM_NEW);
3133         return 0;
3134 }
3135
3136 static int devlink_param_register_one(struct devlink *devlink,
3137                                       const struct devlink_param *param)
3138 {
3139         struct devlink_param_item *param_item;
3140
3141         if (devlink_param_find_by_name(&devlink->param_list,
3142                                        param->name))
3143                 return -EEXIST;
3144
3145         if (param->supported_cmodes == BIT(DEVLINK_PARAM_CMODE_DRIVERINIT))
3146                 WARN_ON(param->get || param->set);
3147         else
3148                 WARN_ON(!param->get || !param->set);
3149
3150         param_item = kzalloc(sizeof(*param_item), GFP_KERNEL);
3151         if (!param_item)
3152                 return -ENOMEM;
3153         param_item->param = param;
3154
3155         list_add_tail(&param_item->list, &devlink->param_list);
3156         devlink_param_notify(devlink, param_item, DEVLINK_CMD_PARAM_NEW);
3157         return 0;
3158 }
3159
3160 static void devlink_param_unregister_one(struct devlink *devlink,
3161                                          const struct devlink_param *param)
3162 {
3163         struct devlink_param_item *param_item;
3164
3165         param_item = devlink_param_find_by_name(&devlink->param_list,
3166                                                 param->name);
3167         WARN_ON(!param_item);
3168         devlink_param_notify(devlink, param_item, DEVLINK_CMD_PARAM_DEL);
3169         list_del(&param_item->list);
3170         kfree(param_item);
3171 }
3172
3173 static int devlink_nl_region_snapshot_id_put(struct sk_buff *msg,
3174                                              struct devlink *devlink,
3175                                              struct devlink_snapshot *snapshot)
3176 {
3177         struct nlattr *snap_attr;
3178         int err;
3179
3180         snap_attr = nla_nest_start(msg, DEVLINK_ATTR_REGION_SNAPSHOT);
3181         if (!snap_attr)
3182                 return -EINVAL;
3183
3184         err = nla_put_u32(msg, DEVLINK_ATTR_REGION_SNAPSHOT_ID, snapshot->id);
3185         if (err)
3186                 goto nla_put_failure;
3187
3188         nla_nest_end(msg, snap_attr);
3189         return 0;
3190
3191 nla_put_failure:
3192         nla_nest_cancel(msg, snap_attr);
3193         return err;
3194 }
3195
3196 static int devlink_nl_region_snapshots_id_put(struct sk_buff *msg,
3197                                               struct devlink *devlink,
3198                                               struct devlink_region *region)
3199 {
3200         struct devlink_snapshot *snapshot;
3201         struct nlattr *snapshots_attr;
3202         int err;
3203
3204         snapshots_attr = nla_nest_start(msg, DEVLINK_ATTR_REGION_SNAPSHOTS);
3205         if (!snapshots_attr)
3206                 return -EINVAL;
3207
3208         list_for_each_entry(snapshot, &region->snapshot_list, list) {
3209                 err = devlink_nl_region_snapshot_id_put(msg, devlink, snapshot);
3210                 if (err)
3211                         goto nla_put_failure;
3212         }
3213
3214         nla_nest_end(msg, snapshots_attr);
3215         return 0;
3216
3217 nla_put_failure:
3218         nla_nest_cancel(msg, snapshots_attr);
3219         return err;
3220 }
3221
3222 static int devlink_nl_region_fill(struct sk_buff *msg, struct devlink *devlink,
3223                                   enum devlink_command cmd, u32 portid,
3224                                   u32 seq, int flags,
3225                                   struct devlink_region *region)
3226 {
3227         void *hdr;
3228         int err;
3229
3230         hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
3231         if (!hdr)
3232                 return -EMSGSIZE;
3233
3234         err = devlink_nl_put_handle(msg, devlink);
3235         if (err)
3236                 goto nla_put_failure;
3237
3238         err = nla_put_string(msg, DEVLINK_ATTR_REGION_NAME, region->name);
3239         if (err)
3240                 goto nla_put_failure;
3241
3242         err = nla_put_u64_64bit(msg, DEVLINK_ATTR_REGION_SIZE,
3243                                 region->size,
3244                                 DEVLINK_ATTR_PAD);
3245         if (err)
3246                 goto nla_put_failure;
3247
3248         err = devlink_nl_region_snapshots_id_put(msg, devlink, region);
3249         if (err)
3250                 goto nla_put_failure;
3251
3252         genlmsg_end(msg, hdr);
3253         return 0;
3254
3255 nla_put_failure:
3256         genlmsg_cancel(msg, hdr);
3257         return err;
3258 }
3259
3260 static void devlink_nl_region_notify(struct devlink_region *region,
3261                                      struct devlink_snapshot *snapshot,
3262                                      enum devlink_command cmd)
3263 {
3264         struct devlink *devlink = region->devlink;
3265         struct sk_buff *msg;
3266         void *hdr;
3267         int err;
3268
3269         WARN_ON(cmd != DEVLINK_CMD_REGION_NEW && cmd != DEVLINK_CMD_REGION_DEL);
3270
3271         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
3272         if (!msg)
3273                 return;
3274
3275         hdr = genlmsg_put(msg, 0, 0, &devlink_nl_family, 0, cmd);
3276         if (!hdr)
3277                 goto out_free_msg;
3278
3279         err = devlink_nl_put_handle(msg, devlink);
3280         if (err)
3281                 goto out_cancel_msg;
3282
3283         err = nla_put_string(msg, DEVLINK_ATTR_REGION_NAME,
3284                              region->name);
3285         if (err)
3286                 goto out_cancel_msg;
3287
3288         if (snapshot) {
3289                 err = nla_put_u32(msg, DEVLINK_ATTR_REGION_SNAPSHOT_ID,
3290                                   snapshot->id);
3291                 if (err)
3292                         goto out_cancel_msg;
3293         } else {
3294                 err = nla_put_u64_64bit(msg, DEVLINK_ATTR_REGION_SIZE,
3295                                         region->size, DEVLINK_ATTR_PAD);
3296                 if (err)
3297                         goto out_cancel_msg;
3298         }
3299         genlmsg_end(msg, hdr);
3300
3301         genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
3302                                 msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
3303
3304         return;
3305
3306 out_cancel_msg:
3307         genlmsg_cancel(msg, hdr);
3308 out_free_msg:
3309         nlmsg_free(msg);
3310 }
3311
3312 static int devlink_nl_cmd_region_get_doit(struct sk_buff *skb,
3313                                           struct genl_info *info)
3314 {
3315         struct devlink *devlink = info->user_ptr[0];
3316         struct devlink_region *region;
3317         const char *region_name;
3318         struct sk_buff *msg;
3319         int err;
3320
3321         if (!info->attrs[DEVLINK_ATTR_REGION_NAME])
3322                 return -EINVAL;
3323
3324         region_name = nla_data(info->attrs[DEVLINK_ATTR_REGION_NAME]);
3325         region = devlink_region_get_by_name(devlink, region_name);
3326         if (!region)
3327                 return -EINVAL;
3328
3329         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
3330         if (!msg)
3331                 return -ENOMEM;
3332
3333         err = devlink_nl_region_fill(msg, devlink, DEVLINK_CMD_REGION_GET,
3334                                      info->snd_portid, info->snd_seq, 0,
3335                                      region);
3336         if (err) {
3337                 nlmsg_free(msg);
3338                 return err;
3339         }
3340
3341         return genlmsg_reply(msg, info);
3342 }
3343
3344 static int devlink_nl_cmd_region_get_dumpit(struct sk_buff *msg,
3345                                             struct netlink_callback *cb)
3346 {
3347         struct devlink_region *region;
3348         struct devlink *devlink;
3349         int start = cb->args[0];
3350         int idx = 0;
3351         int err;
3352
3353         mutex_lock(&devlink_mutex);
3354         list_for_each_entry(devlink, &devlink_list, list) {
3355                 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
3356                         continue;
3357
3358                 mutex_lock(&devlink->lock);
3359                 list_for_each_entry(region, &devlink->region_list, list) {
3360                         if (idx < start) {
3361                                 idx++;
3362                                 continue;
3363                         }
3364                         err = devlink_nl_region_fill(msg, devlink,
3365                                                      DEVLINK_CMD_REGION_GET,
3366                                                      NETLINK_CB(cb->skb).portid,
3367                                                      cb->nlh->nlmsg_seq,
3368                                                      NLM_F_MULTI, region);
3369                         if (err) {
3370                                 mutex_unlock(&devlink->lock);
3371                                 goto out;
3372                         }
3373                         idx++;
3374                 }
3375                 mutex_unlock(&devlink->lock);
3376         }
3377 out:
3378         mutex_unlock(&devlink_mutex);
3379         cb->args[0] = idx;
3380         return msg->len;
3381 }
3382
3383 static int devlink_nl_cmd_region_del(struct sk_buff *skb,
3384                                      struct genl_info *info)
3385 {
3386         struct devlink *devlink = info->user_ptr[0];
3387         struct devlink_snapshot *snapshot;
3388         struct devlink_region *region;
3389         const char *region_name;
3390         u32 snapshot_id;
3391
3392         if (!info->attrs[DEVLINK_ATTR_REGION_NAME] ||
3393             !info->attrs[DEVLINK_ATTR_REGION_SNAPSHOT_ID])
3394                 return -EINVAL;
3395
3396         region_name = nla_data(info->attrs[DEVLINK_ATTR_REGION_NAME]);
3397         snapshot_id = nla_get_u32(info->attrs[DEVLINK_ATTR_REGION_SNAPSHOT_ID]);
3398
3399         region = devlink_region_get_by_name(devlink, region_name);
3400         if (!region)
3401                 return -EINVAL;
3402
3403         snapshot = devlink_region_snapshot_get_by_id(region, snapshot_id);
3404         if (!snapshot)
3405                 return -EINVAL;
3406
3407         devlink_nl_region_notify(region, snapshot, DEVLINK_CMD_REGION_DEL);
3408         devlink_region_snapshot_del(snapshot);
3409         return 0;
3410 }
3411
3412 static int devlink_nl_cmd_region_read_chunk_fill(struct sk_buff *msg,
3413                                                  struct devlink *devlink,
3414                                                  u8 *chunk, u32 chunk_size,
3415                                                  u64 addr)
3416 {
3417         struct nlattr *chunk_attr;
3418         int err;
3419
3420         chunk_attr = nla_nest_start(msg, DEVLINK_ATTR_REGION_CHUNK);
3421         if (!chunk_attr)
3422                 return -EINVAL;
3423
3424         err = nla_put(msg, DEVLINK_ATTR_REGION_CHUNK_DATA, chunk_size, chunk);
3425         if (err)
3426                 goto nla_put_failure;
3427
3428         err = nla_put_u64_64bit(msg, DEVLINK_ATTR_REGION_CHUNK_ADDR, addr,
3429                                 DEVLINK_ATTR_PAD);
3430         if (err)
3431                 goto nla_put_failure;
3432
3433         nla_nest_end(msg, chunk_attr);
3434         return 0;
3435
3436 nla_put_failure:
3437         nla_nest_cancel(msg, chunk_attr);
3438         return err;
3439 }
3440
3441 #define DEVLINK_REGION_READ_CHUNK_SIZE 256
3442
3443 static int devlink_nl_region_read_snapshot_fill(struct sk_buff *skb,
3444                                                 struct devlink *devlink,
3445                                                 struct devlink_region *region,
3446                                                 struct nlattr **attrs,
3447                                                 u64 start_offset,
3448                                                 u64 end_offset,
3449                                                 bool dump,
3450                                                 u64 *new_offset)
3451 {
3452         struct devlink_snapshot *snapshot;
3453         u64 curr_offset = start_offset;
3454         u32 snapshot_id;
3455         int err = 0;
3456
3457         *new_offset = start_offset;
3458
3459         snapshot_id = nla_get_u32(attrs[DEVLINK_ATTR_REGION_SNAPSHOT_ID]);
3460         snapshot = devlink_region_snapshot_get_by_id(region, snapshot_id);
3461         if (!snapshot)
3462                 return -EINVAL;
3463
3464         if (end_offset > snapshot->data_len || dump)
3465                 end_offset = snapshot->data_len;
3466
3467         while (curr_offset < end_offset) {
3468                 u32 data_size;
3469                 u8 *data;
3470
3471                 if (end_offset - curr_offset < DEVLINK_REGION_READ_CHUNK_SIZE)
3472                         data_size = end_offset - curr_offset;
3473                 else
3474                         data_size = DEVLINK_REGION_READ_CHUNK_SIZE;
3475
3476                 data = &snapshot->data[curr_offset];
3477                 err = devlink_nl_cmd_region_read_chunk_fill(skb, devlink,
3478                                                             data, data_size,
3479                                                             curr_offset);
3480                 if (err)
3481                         break;
3482
3483                 curr_offset += data_size;
3484         }
3485         *new_offset = curr_offset;
3486
3487         return err;
3488 }
3489
3490 static int devlink_nl_cmd_region_read_dumpit(struct sk_buff *skb,
3491                                              struct netlink_callback *cb)
3492 {
3493         u64 ret_offset, start_offset, end_offset = 0;
3494         struct nlattr *attrs[DEVLINK_ATTR_MAX + 1];
3495         const struct genl_ops *ops = cb->data;
3496         struct devlink_region *region;
3497         struct nlattr *chunks_attr;
3498         const char *region_name;
3499         struct devlink *devlink;
3500         bool dump = true;
3501         void *hdr;
3502         int err;
3503
3504         start_offset = *((u64 *)&cb->args[0]);
3505
3506         err = nlmsg_parse(cb->nlh, GENL_HDRLEN + devlink_nl_family.hdrsize,
3507                           attrs, DEVLINK_ATTR_MAX, ops->policy, NULL);
3508         if (err)
3509                 goto out;
3510
3511         devlink = devlink_get_from_attrs(sock_net(cb->skb->sk), attrs);
3512         if (IS_ERR(devlink))
3513                 goto out;
3514
3515         mutex_lock(&devlink_mutex);
3516         mutex_lock(&devlink->lock);
3517
3518         if (!attrs[DEVLINK_ATTR_REGION_NAME] ||
3519             !attrs[DEVLINK_ATTR_REGION_SNAPSHOT_ID])
3520                 goto out_unlock;
3521
3522         region_name = nla_data(attrs[DEVLINK_ATTR_REGION_NAME]);
3523         region = devlink_region_get_by_name(devlink, region_name);
3524         if (!region)
3525                 goto out_unlock;
3526
3527         hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
3528                           &devlink_nl_family, NLM_F_ACK | NLM_F_MULTI,
3529                           DEVLINK_CMD_REGION_READ);
3530         if (!hdr)
3531                 goto out_unlock;
3532
3533         err = devlink_nl_put_handle(skb, devlink);
3534         if (err)
3535                 goto nla_put_failure;
3536
3537         err = nla_put_string(skb, DEVLINK_ATTR_REGION_NAME, region_name);
3538         if (err)
3539                 goto nla_put_failure;
3540
3541         chunks_attr = nla_nest_start(skb, DEVLINK_ATTR_REGION_CHUNKS);
3542         if (!chunks_attr)
3543                 goto nla_put_failure;
3544
3545         if (attrs[DEVLINK_ATTR_REGION_CHUNK_ADDR] &&
3546             attrs[DEVLINK_ATTR_REGION_CHUNK_LEN]) {
3547                 if (!start_offset)
3548                         start_offset =
3549                                 nla_get_u64(attrs[DEVLINK_ATTR_REGION_CHUNK_ADDR]);
3550
3551                 end_offset = nla_get_u64(attrs[DEVLINK_ATTR_REGION_CHUNK_ADDR]);
3552                 end_offset += nla_get_u64(attrs[DEVLINK_ATTR_REGION_CHUNK_LEN]);
3553                 dump = false;
3554         }
3555
3556         err = devlink_nl_region_read_snapshot_fill(skb, devlink,
3557                                                    region, attrs,
3558                                                    start_offset,
3559                                                    end_offset, dump,
3560                                                    &ret_offset);
3561
3562         if (err && err != -EMSGSIZE)
3563                 goto nla_put_failure;
3564
3565         /* Check if there was any progress done to prevent infinite loop */
3566         if (ret_offset == start_offset)
3567                 goto nla_put_failure;
3568
3569         *((u64 *)&cb->args[0]) = ret_offset;
3570
3571         nla_nest_end(skb, chunks_attr);
3572         genlmsg_end(skb, hdr);
3573         mutex_unlock(&devlink->lock);
3574         mutex_unlock(&devlink_mutex);
3575
3576         return skb->len;
3577
3578 nla_put_failure:
3579         genlmsg_cancel(skb, hdr);
3580 out_unlock:
3581         mutex_unlock(&devlink->lock);
3582         mutex_unlock(&devlink_mutex);
3583 out:
3584         return 0;
3585 }
3586
3587 static const struct nla_policy devlink_nl_policy[DEVLINK_ATTR_MAX + 1] = {
3588         [DEVLINK_ATTR_BUS_NAME] = { .type = NLA_NUL_STRING },
3589         [DEVLINK_ATTR_DEV_NAME] = { .type = NLA_NUL_STRING },
3590         [DEVLINK_ATTR_PORT_INDEX] = { .type = NLA_U32 },
3591         [DEVLINK_ATTR_PORT_TYPE] = { .type = NLA_U16 },
3592         [DEVLINK_ATTR_PORT_SPLIT_COUNT] = { .type = NLA_U32 },
3593         [DEVLINK_ATTR_SB_INDEX] = { .type = NLA_U32 },
3594         [DEVLINK_ATTR_SB_POOL_INDEX] = { .type = NLA_U16 },
3595         [DEVLINK_ATTR_SB_POOL_TYPE] = { .type = NLA_U8 },
3596         [DEVLINK_ATTR_SB_POOL_SIZE] = { .type = NLA_U32 },
3597         [DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE] = { .type = NLA_U8 },
3598         [DEVLINK_ATTR_SB_THRESHOLD] = { .type = NLA_U32 },
3599         [DEVLINK_ATTR_SB_TC_INDEX] = { .type = NLA_U16 },
3600         [DEVLINK_ATTR_ESWITCH_MODE] = { .type = NLA_U16 },
3601         [DEVLINK_ATTR_ESWITCH_INLINE_MODE] = { .type = NLA_U8 },
3602         [DEVLINK_ATTR_ESWITCH_ENCAP_MODE] = { .type = NLA_U8 },
3603         [DEVLINK_ATTR_DPIPE_TABLE_NAME] = { .type = NLA_NUL_STRING },
3604         [DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED] = { .type = NLA_U8 },
3605         [DEVLINK_ATTR_RESOURCE_ID] = { .type = NLA_U64},
3606         [DEVLINK_ATTR_RESOURCE_SIZE] = { .type = NLA_U64},
3607         [DEVLINK_ATTR_PARAM_NAME] = { .type = NLA_NUL_STRING },
3608         [DEVLINK_ATTR_PARAM_TYPE] = { .type = NLA_U8 },
3609         [DEVLINK_ATTR_PARAM_VALUE_CMODE] = { .type = NLA_U8 },
3610         [DEVLINK_ATTR_REGION_NAME] = { .type = NLA_NUL_STRING },
3611         [DEVLINK_ATTR_REGION_SNAPSHOT_ID] = { .type = NLA_U32 },
3612         [DEVLINK_ATTR_REGION_CHUNK_ADDR] = { .type = NLA_U64 },
3613         [DEVLINK_ATTR_REGION_CHUNK_LEN] = { .type = NLA_U64 },
3614 };
3615
3616 static const struct genl_ops devlink_nl_ops[] = {
3617         {
3618                 .cmd = DEVLINK_CMD_GET,
3619                 .doit = devlink_nl_cmd_get_doit,
3620                 .dumpit = devlink_nl_cmd_get_dumpit,
3621                 .policy = devlink_nl_policy,
3622                 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
3623                 /* can be retrieved by unprivileged users */
3624         },
3625         {
3626                 .cmd = DEVLINK_CMD_PORT_GET,
3627                 .doit = devlink_nl_cmd_port_get_doit,
3628                 .dumpit = devlink_nl_cmd_port_get_dumpit,
3629                 .policy = devlink_nl_policy,
3630                 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
3631                 /* can be retrieved by unprivileged users */
3632         },
3633         {
3634                 .cmd = DEVLINK_CMD_PORT_SET,
3635                 .doit = devlink_nl_cmd_port_set_doit,
3636                 .policy = devlink_nl_policy,
3637                 .flags = GENL_ADMIN_PERM,
3638                 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
3639         },
3640         {
3641                 .cmd = DEVLINK_CMD_PORT_SPLIT,
3642                 .doit = devlink_nl_cmd_port_split_doit,
3643                 .policy = devlink_nl_policy,
3644                 .flags = GENL_ADMIN_PERM,
3645                 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
3646                                   DEVLINK_NL_FLAG_NO_LOCK,
3647         },
3648         {
3649                 .cmd = DEVLINK_CMD_PORT_UNSPLIT,
3650                 .doit = devlink_nl_cmd_port_unsplit_doit,
3651                 .policy = devlink_nl_policy,
3652                 .flags = GENL_ADMIN_PERM,
3653                 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
3654                                   DEVLINK_NL_FLAG_NO_LOCK,
3655         },
3656         {
3657                 .cmd = DEVLINK_CMD_SB_GET,
3658                 .doit = devlink_nl_cmd_sb_get_doit,
3659                 .dumpit = devlink_nl_cmd_sb_get_dumpit,
3660                 .policy = devlink_nl_policy,
3661                 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
3662                                   DEVLINK_NL_FLAG_NEED_SB,
3663                 /* can be retrieved by unprivileged users */
3664         },
3665         {
3666                 .cmd = DEVLINK_CMD_SB_POOL_GET,
3667                 .doit = devlink_nl_cmd_sb_pool_get_doit,
3668                 .dumpit = devlink_nl_cmd_sb_pool_get_dumpit,
3669                 .policy = devlink_nl_policy,
3670                 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
3671                                   DEVLINK_NL_FLAG_NEED_SB,
3672                 /* can be retrieved by unprivileged users */
3673         },
3674         {
3675                 .cmd = DEVLINK_CMD_SB_POOL_SET,
3676                 .doit = devlink_nl_cmd_sb_pool_set_doit,
3677                 .policy = devlink_nl_policy,
3678                 .flags = GENL_ADMIN_PERM,
3679                 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
3680                                   DEVLINK_NL_FLAG_NEED_SB,
3681         },
3682         {
3683                 .cmd = DEVLINK_CMD_SB_PORT_POOL_GET,
3684                 .doit = devlink_nl_cmd_sb_port_pool_get_doit,
3685                 .dumpit = devlink_nl_cmd_sb_port_pool_get_dumpit,
3686                 .policy = devlink_nl_policy,
3687                 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT |
3688                                   DEVLINK_NL_FLAG_NEED_SB,
3689                 /* can be retrieved by unprivileged users */
3690         },
3691         {
3692                 .cmd = DEVLINK_CMD_SB_PORT_POOL_SET,
3693                 .doit = devlink_nl_cmd_sb_port_pool_set_doit,
3694                 .policy = devlink_nl_policy,
3695                 .flags = GENL_ADMIN_PERM,
3696                 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT |
3697                                   DEVLINK_NL_FLAG_NEED_SB,
3698         },
3699         {
3700                 .cmd = DEVLINK_CMD_SB_TC_POOL_BIND_GET,
3701                 .doit = devlink_nl_cmd_sb_tc_pool_bind_get_doit,
3702                 .dumpit = devlink_nl_cmd_sb_tc_pool_bind_get_dumpit,
3703                 .policy = devlink_nl_policy,
3704                 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT |
3705                                   DEVLINK_NL_FLAG_NEED_SB,
3706                 /* can be retrieved by unprivileged users */
3707         },
3708         {
3709                 .cmd = DEVLINK_CMD_SB_TC_POOL_BIND_SET,
3710                 .doit = devlink_nl_cmd_sb_tc_pool_bind_set_doit,
3711                 .policy = devlink_nl_policy,
3712                 .flags = GENL_ADMIN_PERM,
3713                 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT |
3714                                   DEVLINK_NL_FLAG_NEED_SB,
3715         },
3716         {
3717                 .cmd = DEVLINK_CMD_SB_OCC_SNAPSHOT,
3718                 .doit = devlink_nl_cmd_sb_occ_snapshot_doit,
3719                 .policy = devlink_nl_policy,
3720                 .flags = GENL_ADMIN_PERM,
3721                 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
3722                                   DEVLINK_NL_FLAG_NEED_SB,
3723         },
3724         {
3725                 .cmd = DEVLINK_CMD_SB_OCC_MAX_CLEAR,
3726                 .doit = devlink_nl_cmd_sb_occ_max_clear_doit,
3727                 .policy = devlink_nl_policy,
3728                 .flags = GENL_ADMIN_PERM,
3729                 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
3730                                   DEVLINK_NL_FLAG_NEED_SB,
3731         },
3732         {
3733                 .cmd = DEVLINK_CMD_ESWITCH_GET,
3734                 .doit = devlink_nl_cmd_eswitch_get_doit,
3735                 .policy = devlink_nl_policy,
3736                 .flags = GENL_ADMIN_PERM,
3737                 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
3738         },
3739         {
3740                 .cmd = DEVLINK_CMD_ESWITCH_SET,
3741                 .doit = devlink_nl_cmd_eswitch_set_doit,
3742                 .policy = devlink_nl_policy,
3743                 .flags = GENL_ADMIN_PERM,
3744                 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
3745                                   DEVLINK_NL_FLAG_NO_LOCK,
3746         },
3747         {
3748                 .cmd = DEVLINK_CMD_DPIPE_TABLE_GET,
3749                 .doit = devlink_nl_cmd_dpipe_table_get,
3750                 .policy = devlink_nl_policy,
3751                 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
3752                 /* can be retrieved by unprivileged users */
3753         },
3754         {
3755                 .cmd = DEVLINK_CMD_DPIPE_ENTRIES_GET,
3756                 .doit = devlink_nl_cmd_dpipe_entries_get,
3757                 .policy = devlink_nl_policy,
3758                 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
3759                 /* can be retrieved by unprivileged users */
3760         },
3761         {
3762                 .cmd = DEVLINK_CMD_DPIPE_HEADERS_GET,
3763                 .doit = devlink_nl_cmd_dpipe_headers_get,
3764                 .policy = devlink_nl_policy,
3765                 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
3766                 /* can be retrieved by unprivileged users */
3767         },
3768         {
3769                 .cmd = DEVLINK_CMD_DPIPE_TABLE_COUNTERS_SET,
3770                 .doit = devlink_nl_cmd_dpipe_table_counters_set,
3771                 .policy = devlink_nl_policy,
3772                 .flags = GENL_ADMIN_PERM,
3773                 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
3774         },
3775         {
3776                 .cmd = DEVLINK_CMD_RESOURCE_SET,
3777                 .doit = devlink_nl_cmd_resource_set,
3778                 .policy = devlink_nl_policy,
3779                 .flags = GENL_ADMIN_PERM,
3780                 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
3781         },
3782         {
3783                 .cmd = DEVLINK_CMD_RESOURCE_DUMP,
3784                 .doit = devlink_nl_cmd_resource_dump,
3785                 .policy = devlink_nl_policy,
3786                 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
3787                 /* can be retrieved by unprivileged users */
3788         },
3789         {
3790                 .cmd = DEVLINK_CMD_RELOAD,
3791                 .doit = devlink_nl_cmd_reload,
3792                 .policy = devlink_nl_policy,
3793                 .flags = GENL_ADMIN_PERM,
3794                 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
3795                                   DEVLINK_NL_FLAG_NO_LOCK,
3796         },
3797         {
3798                 .cmd = DEVLINK_CMD_PARAM_GET,
3799                 .doit = devlink_nl_cmd_param_get_doit,
3800                 .dumpit = devlink_nl_cmd_param_get_dumpit,
3801                 .policy = devlink_nl_policy,
3802                 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
3803                 /* can be retrieved by unprivileged users */
3804         },
3805         {
3806                 .cmd = DEVLINK_CMD_PARAM_SET,
3807                 .doit = devlink_nl_cmd_param_set_doit,
3808                 .policy = devlink_nl_policy,
3809                 .flags = GENL_ADMIN_PERM,
3810                 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
3811         },
3812         {
3813                 .cmd = DEVLINK_CMD_REGION_GET,
3814                 .doit = devlink_nl_cmd_region_get_doit,
3815                 .dumpit = devlink_nl_cmd_region_get_dumpit,
3816                 .policy = devlink_nl_policy,
3817                 .flags = GENL_ADMIN_PERM,
3818                 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
3819         },
3820         {
3821                 .cmd = DEVLINK_CMD_REGION_DEL,
3822                 .doit = devlink_nl_cmd_region_del,
3823                 .policy = devlink_nl_policy,
3824                 .flags = GENL_ADMIN_PERM,
3825                 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
3826         },
3827         {
3828                 .cmd = DEVLINK_CMD_REGION_READ,
3829                 .dumpit = devlink_nl_cmd_region_read_dumpit,
3830                 .policy = devlink_nl_policy,
3831                 .flags = GENL_ADMIN_PERM,
3832                 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
3833         },
3834 };
3835
3836 static struct genl_family devlink_nl_family __ro_after_init = {
3837         .name           = DEVLINK_GENL_NAME,
3838         .version        = DEVLINK_GENL_VERSION,
3839         .maxattr        = DEVLINK_ATTR_MAX,
3840         .netnsok        = true,
3841         .pre_doit       = devlink_nl_pre_doit,
3842         .post_doit      = devlink_nl_post_doit,
3843         .module         = THIS_MODULE,
3844         .ops            = devlink_nl_ops,
3845         .n_ops          = ARRAY_SIZE(devlink_nl_ops),
3846         .mcgrps         = devlink_nl_mcgrps,
3847         .n_mcgrps       = ARRAY_SIZE(devlink_nl_mcgrps),
3848 };
3849
3850 /**
3851  *      devlink_alloc - Allocate new devlink instance resources
3852  *
3853  *      @ops: ops
3854  *      @priv_size: size of user private data
3855  *
3856  *      Allocate new devlink instance resources, including devlink index
3857  *      and name.
3858  */
3859 struct devlink *devlink_alloc(const struct devlink_ops *ops, size_t priv_size)
3860 {
3861         struct devlink *devlink;
3862
3863         devlink = kzalloc(sizeof(*devlink) + priv_size, GFP_KERNEL);
3864         if (!devlink)
3865                 return NULL;
3866         devlink->ops = ops;
3867         devlink_net_set(devlink, &init_net);
3868         INIT_LIST_HEAD(&devlink->port_list);
3869         INIT_LIST_HEAD(&devlink->sb_list);
3870         INIT_LIST_HEAD_RCU(&devlink->dpipe_table_list);
3871         INIT_LIST_HEAD(&devlink->resource_list);
3872         INIT_LIST_HEAD(&devlink->param_list);
3873         INIT_LIST_HEAD(&devlink->region_list);
3874         mutex_init(&devlink->lock);
3875         return devlink;
3876 }
3877 EXPORT_SYMBOL_GPL(devlink_alloc);
3878
3879 /**
3880  *      devlink_register - Register devlink instance
3881  *
3882  *      @devlink: devlink
3883  */
3884 int devlink_register(struct devlink *devlink, struct device *dev)
3885 {
3886         mutex_lock(&devlink_mutex);
3887         devlink->dev = dev;
3888         list_add_tail(&devlink->list, &devlink_list);
3889         devlink_notify(devlink, DEVLINK_CMD_NEW);
3890         mutex_unlock(&devlink_mutex);
3891         return 0;
3892 }
3893 EXPORT_SYMBOL_GPL(devlink_register);
3894
3895 /**
3896  *      devlink_unregister - Unregister devlink instance
3897  *
3898  *      @devlink: devlink
3899  */
3900 void devlink_unregister(struct devlink *devlink)
3901 {
3902         mutex_lock(&devlink_mutex);
3903         devlink_notify(devlink, DEVLINK_CMD_DEL);
3904         list_del(&devlink->list);
3905         mutex_unlock(&devlink_mutex);
3906 }
3907 EXPORT_SYMBOL_GPL(devlink_unregister);
3908
3909 /**
3910  *      devlink_free - Free devlink instance resources
3911  *
3912  *      @devlink: devlink
3913  */
3914 void devlink_free(struct devlink *devlink)
3915 {
3916         kfree(devlink);
3917 }
3918 EXPORT_SYMBOL_GPL(devlink_free);
3919
3920 /**
3921  *      devlink_port_register - Register devlink port
3922  *
3923  *      @devlink: devlink
3924  *      @devlink_port: devlink port
3925  *      @port_index
3926  *
3927  *      Register devlink port with provided port index. User can use
3928  *      any indexing, even hw-related one. devlink_port structure
3929  *      is convenient to be embedded inside user driver private structure.
3930  *      Note that the caller should take care of zeroing the devlink_port
3931  *      structure.
3932  */
3933 int devlink_port_register(struct devlink *devlink,
3934                           struct devlink_port *devlink_port,
3935                           unsigned int port_index)
3936 {
3937         mutex_lock(&devlink->lock);
3938         if (devlink_port_index_exists(devlink, port_index)) {
3939                 mutex_unlock(&devlink->lock);
3940                 return -EEXIST;
3941         }
3942         devlink_port->devlink = devlink;
3943         devlink_port->index = port_index;
3944         devlink_port->registered = true;
3945         list_add_tail(&devlink_port->list, &devlink->port_list);
3946         mutex_unlock(&devlink->lock);
3947         devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW);
3948         return 0;
3949 }
3950 EXPORT_SYMBOL_GPL(devlink_port_register);
3951
3952 /**
3953  *      devlink_port_unregister - Unregister devlink port
3954  *
3955  *      @devlink_port: devlink port
3956  */
3957 void devlink_port_unregister(struct devlink_port *devlink_port)
3958 {
3959         struct devlink *devlink = devlink_port->devlink;
3960
3961         devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_DEL);
3962         mutex_lock(&devlink->lock);
3963         list_del(&devlink_port->list);
3964         mutex_unlock(&devlink->lock);
3965 }
3966 EXPORT_SYMBOL_GPL(devlink_port_unregister);
3967
3968 static void __devlink_port_type_set(struct devlink_port *devlink_port,
3969                                     enum devlink_port_type type,
3970                                     void *type_dev)
3971 {
3972         devlink_port->type = type;
3973         devlink_port->type_dev = type_dev;
3974         devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW);
3975 }
3976
3977 /**
3978  *      devlink_port_type_eth_set - Set port type to Ethernet
3979  *
3980  *      @devlink_port: devlink port
3981  *      @netdev: related netdevice
3982  */
3983 void devlink_port_type_eth_set(struct devlink_port *devlink_port,
3984                                struct net_device *netdev)
3985 {
3986         return __devlink_port_type_set(devlink_port,
3987                                        DEVLINK_PORT_TYPE_ETH, netdev);
3988 }
3989 EXPORT_SYMBOL_GPL(devlink_port_type_eth_set);
3990
3991 /**
3992  *      devlink_port_type_ib_set - Set port type to InfiniBand
3993  *
3994  *      @devlink_port: devlink port
3995  *      @ibdev: related IB device
3996  */
3997 void devlink_port_type_ib_set(struct devlink_port *devlink_port,
3998                               struct ib_device *ibdev)
3999 {
4000         return __devlink_port_type_set(devlink_port,
4001                                        DEVLINK_PORT_TYPE_IB, ibdev);
4002 }
4003 EXPORT_SYMBOL_GPL(devlink_port_type_ib_set);
4004
4005 /**
4006  *      devlink_port_type_clear - Clear port type
4007  *
4008  *      @devlink_port: devlink port
4009  */
4010 void devlink_port_type_clear(struct devlink_port *devlink_port)
4011 {
4012         return __devlink_port_type_set(devlink_port,
4013                                        DEVLINK_PORT_TYPE_NOTSET, NULL);
4014 }
4015 EXPORT_SYMBOL_GPL(devlink_port_type_clear);
4016
4017 /**
4018  *      devlink_port_attrs_set - Set port attributes
4019  *
4020  *      @devlink_port: devlink port
4021  *      @flavour: flavour of the port
4022  *      @port_number: number of the port that is facing user, for example
4023  *                    the front panel port number
4024  *      @split: indicates if this is split port
4025  *      @split_subport_number: if the port is split, this is the number
4026  *                             of subport.
4027  */
4028 void devlink_port_attrs_set(struct devlink_port *devlink_port,
4029                             enum devlink_port_flavour flavour,
4030                             u32 port_number, bool split,
4031                             u32 split_subport_number)
4032 {
4033         struct devlink_port_attrs *attrs = &devlink_port->attrs;
4034
4035         attrs->set = true;
4036         attrs->flavour = flavour;
4037         attrs->port_number = port_number;
4038         attrs->split = split;
4039         attrs->split_subport_number = split_subport_number;
4040         devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW);
4041 }
4042 EXPORT_SYMBOL_GPL(devlink_port_attrs_set);
4043
4044 int devlink_port_get_phys_port_name(struct devlink_port *devlink_port,
4045                                     char *name, size_t len)
4046 {
4047         struct devlink_port_attrs *attrs = &devlink_port->attrs;
4048         int n = 0;
4049
4050         if (!attrs->set)
4051                 return -EOPNOTSUPP;
4052
4053         switch (attrs->flavour) {
4054         case DEVLINK_PORT_FLAVOUR_PHYSICAL:
4055                 if (!attrs->split)
4056                         n = snprintf(name, len, "p%u", attrs->port_number);
4057                 else
4058                         n = snprintf(name, len, "p%us%u", attrs->port_number,
4059                                      attrs->split_subport_number);
4060                 break;
4061         case DEVLINK_PORT_FLAVOUR_CPU:
4062         case DEVLINK_PORT_FLAVOUR_DSA:
4063                 /* As CPU and DSA ports do not have a netdevice associated
4064                  * case should not ever happen.
4065                  */
4066                 WARN_ON(1);
4067                 return -EINVAL;
4068         }
4069
4070         if (n >= len)
4071                 return -EINVAL;
4072
4073         return 0;
4074 }
4075 EXPORT_SYMBOL_GPL(devlink_port_get_phys_port_name);
4076
4077 int devlink_sb_register(struct devlink *devlink, unsigned int sb_index,
4078                         u32 size, u16 ingress_pools_count,
4079                         u16 egress_pools_count, u16 ingress_tc_count,
4080                         u16 egress_tc_count)
4081 {
4082         struct devlink_sb *devlink_sb;
4083         int err = 0;
4084
4085         mutex_lock(&devlink->lock);
4086         if (devlink_sb_index_exists(devlink, sb_index)) {
4087                 err = -EEXIST;
4088                 goto unlock;
4089         }
4090
4091         devlink_sb = kzalloc(sizeof(*devlink_sb), GFP_KERNEL);
4092         if (!devlink_sb) {
4093                 err = -ENOMEM;
4094                 goto unlock;
4095         }
4096         devlink_sb->index = sb_index;
4097         devlink_sb->size = size;
4098         devlink_sb->ingress_pools_count = ingress_pools_count;
4099         devlink_sb->egress_pools_count = egress_pools_count;
4100         devlink_sb->ingress_tc_count = ingress_tc_count;
4101         devlink_sb->egress_tc_count = egress_tc_count;
4102         list_add_tail(&devlink_sb->list, &devlink->sb_list);
4103 unlock:
4104         mutex_unlock(&devlink->lock);
4105         return err;
4106 }
4107 EXPORT_SYMBOL_GPL(devlink_sb_register);
4108
4109 void devlink_sb_unregister(struct devlink *devlink, unsigned int sb_index)
4110 {
4111         struct devlink_sb *devlink_sb;
4112
4113         mutex_lock(&devlink->lock);
4114         devlink_sb = devlink_sb_get_by_index(devlink, sb_index);
4115         WARN_ON(!devlink_sb);
4116         list_del(&devlink_sb->list);
4117         mutex_unlock(&devlink->lock);
4118         kfree(devlink_sb);
4119 }
4120 EXPORT_SYMBOL_GPL(devlink_sb_unregister);
4121
4122 /**
4123  *      devlink_dpipe_headers_register - register dpipe headers
4124  *
4125  *      @devlink: devlink
4126  *      @dpipe_headers: dpipe header array
4127  *
4128  *      Register the headers supported by hardware.
4129  */
4130 int devlink_dpipe_headers_register(struct devlink *devlink,
4131                                    struct devlink_dpipe_headers *dpipe_headers)
4132 {
4133         mutex_lock(&devlink->lock);
4134         devlink->dpipe_headers = dpipe_headers;
4135         mutex_unlock(&devlink->lock);
4136         return 0;
4137 }
4138 EXPORT_SYMBOL_GPL(devlink_dpipe_headers_register);
4139
4140 /**
4141  *      devlink_dpipe_headers_unregister - unregister dpipe headers
4142  *
4143  *      @devlink: devlink
4144  *
4145  *      Unregister the headers supported by hardware.
4146  */
4147 void devlink_dpipe_headers_unregister(struct devlink *devlink)
4148 {
4149         mutex_lock(&devlink->lock);
4150         devlink->dpipe_headers = NULL;
4151         mutex_unlock(&devlink->lock);
4152 }
4153 EXPORT_SYMBOL_GPL(devlink_dpipe_headers_unregister);
4154
4155 /**
4156  *      devlink_dpipe_table_counter_enabled - check if counter allocation
4157  *                                            required
4158  *      @devlink: devlink
4159  *      @table_name: tables name
4160  *
4161  *      Used by driver to check if counter allocation is required.
4162  *      After counter allocation is turned on the table entries
4163  *      are updated to include counter statistics.
4164  *
4165  *      After that point on the driver must respect the counter
4166  *      state so that each entry added to the table is added
4167  *      with a counter.
4168  */
4169 bool devlink_dpipe_table_counter_enabled(struct devlink *devlink,
4170                                          const char *table_name)
4171 {
4172         struct devlink_dpipe_table *table;
4173         bool enabled;
4174
4175         rcu_read_lock();
4176         table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
4177                                          table_name);
4178         enabled = false;
4179         if (table)
4180                 enabled = table->counters_enabled;
4181         rcu_read_unlock();
4182         return enabled;
4183 }
4184 EXPORT_SYMBOL_GPL(devlink_dpipe_table_counter_enabled);
4185
4186 /**
4187  *      devlink_dpipe_table_register - register dpipe table
4188  *
4189  *      @devlink: devlink
4190  *      @table_name: table name
4191  *      @table_ops: table ops
4192  *      @priv: priv
4193  *      @counter_control_extern: external control for counters
4194  */
4195 int devlink_dpipe_table_register(struct devlink *devlink,
4196                                  const char *table_name,
4197                                  struct devlink_dpipe_table_ops *table_ops,
4198                                  void *priv, bool counter_control_extern)
4199 {
4200         struct devlink_dpipe_table *table;
4201
4202         if (devlink_dpipe_table_find(&devlink->dpipe_table_list, table_name))
4203                 return -EEXIST;
4204
4205         if (WARN_ON(!table_ops->size_get))
4206                 return -EINVAL;
4207
4208         table = kzalloc(sizeof(*table), GFP_KERNEL);
4209         if (!table)
4210                 return -ENOMEM;
4211
4212         table->name = table_name;
4213         table->table_ops = table_ops;
4214         table->priv = priv;
4215         table->counter_control_extern = counter_control_extern;
4216
4217         mutex_lock(&devlink->lock);
4218         list_add_tail_rcu(&table->list, &devlink->dpipe_table_list);
4219         mutex_unlock(&devlink->lock);
4220         return 0;
4221 }
4222 EXPORT_SYMBOL_GPL(devlink_dpipe_table_register);
4223
4224 /**
4225  *      devlink_dpipe_table_unregister - unregister dpipe table
4226  *
4227  *      @devlink: devlink
4228  *      @table_name: table name
4229  */
4230 void devlink_dpipe_table_unregister(struct devlink *devlink,
4231                                     const char *table_name)
4232 {
4233         struct devlink_dpipe_table *table;
4234
4235         mutex_lock(&devlink->lock);
4236         table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
4237                                          table_name);
4238         if (!table)
4239                 goto unlock;
4240         list_del_rcu(&table->list);
4241         mutex_unlock(&devlink->lock);
4242         kfree_rcu(table, rcu);
4243         return;
4244 unlock:
4245         mutex_unlock(&devlink->lock);
4246 }
4247 EXPORT_SYMBOL_GPL(devlink_dpipe_table_unregister);
4248
4249 /**
4250  *      devlink_resource_register - devlink resource register
4251  *
4252  *      @devlink: devlink
4253  *      @resource_name: resource's name
4254  *      @top_hierarchy: top hierarchy
4255  *      @reload_required: reload is required for new configuration to
4256  *                        apply
4257  *      @resource_size: resource's size
4258  *      @resource_id: resource's id
4259  *      @parent_reosurce_id: resource's parent id
4260  *      @size params: size parameters
4261  */
4262 int devlink_resource_register(struct devlink *devlink,
4263                               const char *resource_name,
4264                               u64 resource_size,
4265                               u64 resource_id,
4266                               u64 parent_resource_id,
4267                               const struct devlink_resource_size_params *size_params)
4268 {
4269         struct devlink_resource *resource;
4270         struct list_head *resource_list;
4271         bool top_hierarchy;
4272         int err = 0;
4273
4274         top_hierarchy = parent_resource_id == DEVLINK_RESOURCE_ID_PARENT_TOP;
4275
4276         mutex_lock(&devlink->lock);
4277         resource = devlink_resource_find(devlink, NULL, resource_id);
4278         if (resource) {
4279                 err = -EINVAL;
4280                 goto out;
4281         }
4282
4283         resource = kzalloc(sizeof(*resource), GFP_KERNEL);
4284         if (!resource) {
4285                 err = -ENOMEM;
4286                 goto out;
4287         }
4288
4289         if (top_hierarchy) {
4290                 resource_list = &devlink->resource_list;
4291         } else {
4292                 struct devlink_resource *parent_resource;
4293
4294                 parent_resource = devlink_resource_find(devlink, NULL,
4295                                                         parent_resource_id);
4296                 if (parent_resource) {
4297                         resource_list = &parent_resource->resource_list;
4298                         resource->parent = parent_resource;
4299                 } else {
4300                         kfree(resource);
4301                         err = -EINVAL;
4302                         goto out;
4303                 }
4304         }
4305
4306         resource->name = resource_name;
4307         resource->size = resource_size;
4308         resource->size_new = resource_size;
4309         resource->id = resource_id;
4310         resource->size_valid = true;
4311         memcpy(&resource->size_params, size_params,
4312                sizeof(resource->size_params));
4313         INIT_LIST_HEAD(&resource->resource_list);
4314         list_add_tail(&resource->list, resource_list);
4315 out:
4316         mutex_unlock(&devlink->lock);
4317         return err;
4318 }
4319 EXPORT_SYMBOL_GPL(devlink_resource_register);
4320
4321 /**
4322  *      devlink_resources_unregister - free all resources
4323  *
4324  *      @devlink: devlink
4325  *      @resource: resource
4326  */
4327 void devlink_resources_unregister(struct devlink *devlink,
4328                                   struct devlink_resource *resource)
4329 {
4330         struct devlink_resource *tmp, *child_resource;
4331         struct list_head *resource_list;
4332
4333         if (resource)
4334                 resource_list = &resource->resource_list;
4335         else
4336                 resource_list = &devlink->resource_list;
4337
4338         if (!resource)
4339                 mutex_lock(&devlink->lock);
4340
4341         list_for_each_entry_safe(child_resource, tmp, resource_list, list) {
4342                 devlink_resources_unregister(devlink, child_resource);
4343                 list_del(&child_resource->list);
4344                 kfree(child_resource);
4345         }
4346
4347         if (!resource)
4348                 mutex_unlock(&devlink->lock);
4349 }
4350 EXPORT_SYMBOL_GPL(devlink_resources_unregister);
4351
4352 /**
4353  *      devlink_resource_size_get - get and update size
4354  *
4355  *      @devlink: devlink
4356  *      @resource_id: the requested resource id
4357  *      @p_resource_size: ptr to update
4358  */
4359 int devlink_resource_size_get(struct devlink *devlink,
4360                               u64 resource_id,
4361                               u64 *p_resource_size)
4362 {
4363         struct devlink_resource *resource;
4364         int err = 0;
4365
4366         mutex_lock(&devlink->lock);
4367         resource = devlink_resource_find(devlink, NULL, resource_id);
4368         if (!resource) {
4369                 err = -EINVAL;
4370                 goto out;
4371         }
4372         *p_resource_size = resource->size_new;
4373         resource->size = resource->size_new;
4374 out:
4375         mutex_unlock(&devlink->lock);
4376         return err;
4377 }
4378 EXPORT_SYMBOL_GPL(devlink_resource_size_get);
4379
4380 /**
4381  *      devlink_dpipe_table_resource_set - set the resource id
4382  *
4383  *      @devlink: devlink
4384  *      @table_name: table name
4385  *      @resource_id: resource id
4386  *      @resource_units: number of resource's units consumed per table's entry
4387  */
4388 int devlink_dpipe_table_resource_set(struct devlink *devlink,
4389                                      const char *table_name, u64 resource_id,
4390                                      u64 resource_units)
4391 {
4392         struct devlink_dpipe_table *table;
4393         int err = 0;
4394
4395         mutex_lock(&devlink->lock);
4396         table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
4397                                          table_name);
4398         if (!table) {
4399                 err = -EINVAL;
4400                 goto out;
4401         }
4402         table->resource_id = resource_id;
4403         table->resource_units = resource_units;
4404         table->resource_valid = true;
4405 out:
4406         mutex_unlock(&devlink->lock);
4407         return err;
4408 }
4409 EXPORT_SYMBOL_GPL(devlink_dpipe_table_resource_set);
4410
4411 /**
4412  *      devlink_resource_occ_get_register - register occupancy getter
4413  *
4414  *      @devlink: devlink
4415  *      @resource_id: resource id
4416  *      @occ_get: occupancy getter callback
4417  *      @occ_get_priv: occupancy getter callback priv
4418  */
4419 void devlink_resource_occ_get_register(struct devlink *devlink,
4420                                        u64 resource_id,
4421                                        devlink_resource_occ_get_t *occ_get,
4422                                        void *occ_get_priv)
4423 {
4424         struct devlink_resource *resource;
4425
4426         mutex_lock(&devlink->lock);
4427         resource = devlink_resource_find(devlink, NULL, resource_id);
4428         if (WARN_ON(!resource))
4429                 goto out;
4430         WARN_ON(resource->occ_get);
4431
4432         resource->occ_get = occ_get;
4433         resource->occ_get_priv = occ_get_priv;
4434 out:
4435         mutex_unlock(&devlink->lock);
4436 }
4437 EXPORT_SYMBOL_GPL(devlink_resource_occ_get_register);
4438
4439 /**
4440  *      devlink_resource_occ_get_unregister - unregister occupancy getter
4441  *
4442  *      @devlink: devlink
4443  *      @resource_id: resource id
4444  */
4445 void devlink_resource_occ_get_unregister(struct devlink *devlink,
4446                                          u64 resource_id)
4447 {
4448         struct devlink_resource *resource;
4449
4450         mutex_lock(&devlink->lock);
4451         resource = devlink_resource_find(devlink, NULL, resource_id);
4452         if (WARN_ON(!resource))
4453                 goto out;
4454         WARN_ON(!resource->occ_get);
4455
4456         resource->occ_get = NULL;
4457         resource->occ_get_priv = NULL;
4458 out:
4459         mutex_unlock(&devlink->lock);
4460 }
4461 EXPORT_SYMBOL_GPL(devlink_resource_occ_get_unregister);
4462
4463 /**
4464  *      devlink_params_register - register configuration parameters
4465  *
4466  *      @devlink: devlink
4467  *      @params: configuration parameters array
4468  *      @params_count: number of parameters provided
4469  *
4470  *      Register the configuration parameters supported by the driver.
4471  */
4472 int devlink_params_register(struct devlink *devlink,
4473                             const struct devlink_param *params,
4474                             size_t params_count)
4475 {
4476         const struct devlink_param *param = params;
4477         int i;
4478         int err;
4479
4480         mutex_lock(&devlink->lock);
4481         for (i = 0; i < params_count; i++, param++) {
4482                 if (!param || !param->name || !param->supported_cmodes) {
4483                         err = -EINVAL;
4484                         goto rollback;
4485                 }
4486                 if (param->generic) {
4487                         err = devlink_param_generic_verify(param);
4488                         if (err)
4489                                 goto rollback;
4490                 } else {
4491                         err = devlink_param_driver_verify(param);
4492                         if (err)
4493                                 goto rollback;
4494                 }
4495                 err = devlink_param_register_one(devlink, param);
4496                 if (err)
4497                         goto rollback;
4498         }
4499
4500         mutex_unlock(&devlink->lock);
4501         return 0;
4502
4503 rollback:
4504         if (!i)
4505                 goto unlock;
4506         for (param--; i > 0; i--, param--)
4507                 devlink_param_unregister_one(devlink, param);
4508 unlock:
4509         mutex_unlock(&devlink->lock);
4510         return err;
4511 }
4512 EXPORT_SYMBOL_GPL(devlink_params_register);
4513
4514 /**
4515  *      devlink_params_unregister - unregister configuration parameters
4516  *      @devlink: devlink
4517  *      @params: configuration parameters to unregister
4518  *      @params_count: number of parameters provided
4519  */
4520 void devlink_params_unregister(struct devlink *devlink,
4521                                const struct devlink_param *params,
4522                                size_t params_count)
4523 {
4524         const struct devlink_param *param = params;
4525         int i;
4526
4527         mutex_lock(&devlink->lock);
4528         for (i = 0; i < params_count; i++, param++)
4529                 devlink_param_unregister_one(devlink, param);
4530         mutex_unlock(&devlink->lock);
4531 }
4532 EXPORT_SYMBOL_GPL(devlink_params_unregister);
4533
4534 /**
4535  *      devlink_param_driverinit_value_get - get configuration parameter
4536  *                                           value for driver initializing
4537  *
4538  *      @devlink: devlink
4539  *      @param_id: parameter ID
4540  *      @init_val: value of parameter in driverinit configuration mode
4541  *
4542  *      This function should be used by the driver to get driverinit
4543  *      configuration for initialization after reload command.
4544  */
4545 int devlink_param_driverinit_value_get(struct devlink *devlink, u32 param_id,
4546                                        union devlink_param_value *init_val)
4547 {
4548         struct devlink_param_item *param_item;
4549
4550         if (!devlink->ops || !devlink->ops->reload)
4551                 return -EOPNOTSUPP;
4552
4553         param_item = devlink_param_find_by_id(&devlink->param_list, param_id);
4554         if (!param_item)
4555                 return -EINVAL;
4556
4557         if (!param_item->driverinit_value_valid ||
4558             !devlink_param_cmode_is_supported(param_item->param,
4559                                               DEVLINK_PARAM_CMODE_DRIVERINIT))
4560                 return -EOPNOTSUPP;
4561
4562         if (param_item->param->type == DEVLINK_PARAM_TYPE_STRING)
4563                 strcpy(init_val->vstr, param_item->driverinit_value.vstr);
4564         else
4565                 *init_val = param_item->driverinit_value;
4566
4567         return 0;
4568 }
4569 EXPORT_SYMBOL_GPL(devlink_param_driverinit_value_get);
4570
4571 /**
4572  *      devlink_param_driverinit_value_set - set value of configuration
4573  *                                           parameter for driverinit
4574  *                                           configuration mode
4575  *
4576  *      @devlink: devlink
4577  *      @param_id: parameter ID
4578  *      @init_val: value of parameter to set for driverinit configuration mode
4579  *
4580  *      This function should be used by the driver to set driverinit
4581  *      configuration mode default value.
4582  */
4583 int devlink_param_driverinit_value_set(struct devlink *devlink, u32 param_id,
4584                                        union devlink_param_value init_val)
4585 {
4586         struct devlink_param_item *param_item;
4587
4588         param_item = devlink_param_find_by_id(&devlink->param_list, param_id);
4589         if (!param_item)
4590                 return -EINVAL;
4591
4592         if (!devlink_param_cmode_is_supported(param_item->param,
4593                                               DEVLINK_PARAM_CMODE_DRIVERINIT))
4594                 return -EOPNOTSUPP;
4595
4596         if (param_item->param->type == DEVLINK_PARAM_TYPE_STRING)
4597                 strcpy(param_item->driverinit_value.vstr, init_val.vstr);
4598         else
4599                 param_item->driverinit_value = init_val;
4600         param_item->driverinit_value_valid = true;
4601
4602         devlink_param_notify(devlink, param_item, DEVLINK_CMD_PARAM_NEW);
4603         return 0;
4604 }
4605 EXPORT_SYMBOL_GPL(devlink_param_driverinit_value_set);
4606
4607 /**
4608  *      devlink_param_value_changed - notify devlink on a parameter's value
4609  *                                    change. Should be called by the driver
4610  *                                    right after the change.
4611  *
4612  *      @devlink: devlink
4613  *      @param_id: parameter ID
4614  *
4615  *      This function should be used by the driver to notify devlink on value
4616  *      change, excluding driverinit configuration mode.
4617  *      For driverinit configuration mode driver should use the function
4618  *      devlink_param_driverinit_value_set() instead.
4619  */
4620 void devlink_param_value_changed(struct devlink *devlink, u32 param_id)
4621 {
4622         struct devlink_param_item *param_item;
4623
4624         param_item = devlink_param_find_by_id(&devlink->param_list, param_id);
4625         WARN_ON(!param_item);
4626
4627         devlink_param_notify(devlink, param_item, DEVLINK_CMD_PARAM_NEW);
4628 }
4629 EXPORT_SYMBOL_GPL(devlink_param_value_changed);
4630
4631 /**
4632  *      devlink_param_value_str_fill - Safely fill-up the string preventing
4633  *                                     from overflow of the preallocated buffer
4634  *
4635  *      @dst_val: destination devlink_param_value
4636  *      @src: source buffer
4637  */
4638 void devlink_param_value_str_fill(union devlink_param_value *dst_val,
4639                                   const char *src)
4640 {
4641         size_t len;
4642
4643         len = strlcpy(dst_val->vstr, src, __DEVLINK_PARAM_MAX_STRING_VALUE);
4644         WARN_ON(len >= __DEVLINK_PARAM_MAX_STRING_VALUE);
4645 }
4646 EXPORT_SYMBOL_GPL(devlink_param_value_str_fill);
4647
4648 /**
4649  *      devlink_region_create - create a new address region
4650  *
4651  *      @devlink: devlink
4652  *      @region_name: region name
4653  *      @region_max_snapshots: Maximum supported number of snapshots for region
4654  *      @region_size: size of region
4655  */
4656 struct devlink_region *devlink_region_create(struct devlink *devlink,
4657                                              const char *region_name,
4658                                              u32 region_max_snapshots,
4659                                              u64 region_size)
4660 {
4661         struct devlink_region *region;
4662         int err = 0;
4663
4664         mutex_lock(&devlink->lock);
4665
4666         if (devlink_region_get_by_name(devlink, region_name)) {
4667                 err = -EEXIST;
4668                 goto unlock;
4669         }
4670
4671         region = kzalloc(sizeof(*region), GFP_KERNEL);
4672         if (!region) {
4673                 err = -ENOMEM;
4674                 goto unlock;
4675         }
4676
4677         region->devlink = devlink;
4678         region->max_snapshots = region_max_snapshots;
4679         region->name = region_name;
4680         region->size = region_size;
4681         INIT_LIST_HEAD(&region->snapshot_list);
4682         list_add_tail(&region->list, &devlink->region_list);
4683         devlink_nl_region_notify(region, NULL, DEVLINK_CMD_REGION_NEW);
4684
4685         mutex_unlock(&devlink->lock);
4686         return region;
4687
4688 unlock:
4689         mutex_unlock(&devlink->lock);
4690         return ERR_PTR(err);
4691 }
4692 EXPORT_SYMBOL_GPL(devlink_region_create);
4693
4694 /**
4695  *      devlink_region_destroy - destroy address region
4696  *
4697  *      @region: devlink region to destroy
4698  */
4699 void devlink_region_destroy(struct devlink_region *region)
4700 {
4701         struct devlink *devlink = region->devlink;
4702         struct devlink_snapshot *snapshot, *ts;
4703
4704         mutex_lock(&devlink->lock);
4705
4706         /* Free all snapshots of region */
4707         list_for_each_entry_safe(snapshot, ts, &region->snapshot_list, list)
4708                 devlink_region_snapshot_del(snapshot);
4709
4710         list_del(&region->list);
4711
4712         devlink_nl_region_notify(region, NULL, DEVLINK_CMD_REGION_DEL);
4713         mutex_unlock(&devlink->lock);
4714         kfree(region);
4715 }
4716 EXPORT_SYMBOL_GPL(devlink_region_destroy);
4717
4718 /**
4719  *      devlink_region_shapshot_id_get - get snapshot ID
4720  *
4721  *      This callback should be called when adding a new snapshot,
4722  *      Driver should use the same id for multiple snapshots taken
4723  *      on multiple regions at the same time/by the same trigger.
4724  *
4725  *      @devlink: devlink
4726  */
4727 u32 devlink_region_shapshot_id_get(struct devlink *devlink)
4728 {
4729         u32 id;
4730
4731         mutex_lock(&devlink->lock);
4732         id = ++devlink->snapshot_id;
4733         mutex_unlock(&devlink->lock);
4734
4735         return id;
4736 }
4737 EXPORT_SYMBOL_GPL(devlink_region_shapshot_id_get);
4738
4739 /**
4740  *      devlink_region_snapshot_create - create a new snapshot
4741  *      This will add a new snapshot of a region. The snapshot
4742  *      will be stored on the region struct and can be accessed
4743  *      from devlink. This is useful for future analyses of snapshots.
4744  *      Multiple snapshots can be created on a region.
4745  *      The @snapshot_id should be obtained using the getter function.
4746  *
4747  *      @devlink_region: devlink region of the snapshot
4748  *      @data_len: size of snapshot data
4749  *      @data: snapshot data
4750  *      @snapshot_id: snapshot id to be created
4751  *      @data_destructor: pointer to destructor function to free data
4752  */
4753 int devlink_region_snapshot_create(struct devlink_region *region, u64 data_len,
4754                                    u8 *data, u32 snapshot_id,
4755                                    devlink_snapshot_data_dest_t *data_destructor)
4756 {
4757         struct devlink *devlink = region->devlink;
4758         struct devlink_snapshot *snapshot;
4759         int err;
4760
4761         mutex_lock(&devlink->lock);
4762
4763         /* check if region can hold one more snapshot */
4764         if (region->cur_snapshots == region->max_snapshots) {
4765                 err = -ENOMEM;
4766                 goto unlock;
4767         }
4768
4769         if (devlink_region_snapshot_get_by_id(region, snapshot_id)) {
4770                 err = -EEXIST;
4771                 goto unlock;
4772         }
4773
4774         snapshot = kzalloc(sizeof(*snapshot), GFP_KERNEL);
4775         if (!snapshot) {
4776                 err = -ENOMEM;
4777                 goto unlock;
4778         }
4779
4780         snapshot->id = snapshot_id;
4781         snapshot->region = region;
4782         snapshot->data = data;
4783         snapshot->data_len = data_len;
4784         snapshot->data_destructor = data_destructor;
4785
4786         list_add_tail(&snapshot->list, &region->snapshot_list);
4787
4788         region->cur_snapshots++;
4789
4790         devlink_nl_region_notify(region, snapshot, DEVLINK_CMD_REGION_NEW);
4791         mutex_unlock(&devlink->lock);
4792         return 0;
4793
4794 unlock:
4795         mutex_unlock(&devlink->lock);
4796         return err;
4797 }
4798 EXPORT_SYMBOL_GPL(devlink_region_snapshot_create);
4799
4800 static int __init devlink_module_init(void)
4801 {
4802         return genl_register_family(&devlink_nl_family);
4803 }
4804
4805 static void __exit devlink_module_exit(void)
4806 {
4807         genl_unregister_family(&devlink_nl_family);
4808 }
4809
4810 module_init(devlink_module_init);
4811 module_exit(devlink_module_exit);
4812
4813 MODULE_LICENSE("GPL v2");
4814 MODULE_AUTHOR("Jiri Pirko <jiri@mellanox.com>");
4815 MODULE_DESCRIPTION("Network physical device Netlink interface");
4816 MODULE_ALIAS_GENL_FAMILY(DEVLINK_GENL_NAME);