devlink: add version reporting to devlink info API
[sfrench/cifs-2.6.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                         return err;
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         genlmsg_cancel(msg, hdr);
1130         return -EMSGSIZE;
1131 }
1132
1133 static int devlink_nl_cmd_sb_port_pool_get_doit(struct sk_buff *skb,
1134                                                 struct genl_info *info)
1135 {
1136         struct devlink_port *devlink_port = info->user_ptr[0];
1137         struct devlink *devlink = devlink_port->devlink;
1138         struct devlink_sb *devlink_sb = info->user_ptr[1];
1139         struct sk_buff *msg;
1140         u16 pool_index;
1141         int err;
1142
1143         err = devlink_sb_pool_index_get_from_info(devlink_sb, info,
1144                                                   &pool_index);
1145         if (err)
1146                 return err;
1147
1148         if (!devlink->ops || !devlink->ops->sb_port_pool_get)
1149                 return -EOPNOTSUPP;
1150
1151         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1152         if (!msg)
1153                 return -ENOMEM;
1154
1155         err = devlink_nl_sb_port_pool_fill(msg, devlink, devlink_port,
1156                                            devlink_sb, pool_index,
1157                                            DEVLINK_CMD_SB_PORT_POOL_NEW,
1158                                            info->snd_portid, info->snd_seq, 0);
1159         if (err) {
1160                 nlmsg_free(msg);
1161                 return err;
1162         }
1163
1164         return genlmsg_reply(msg, info);
1165 }
1166
1167 static int __sb_port_pool_get_dumpit(struct sk_buff *msg, int start, int *p_idx,
1168                                      struct devlink *devlink,
1169                                      struct devlink_sb *devlink_sb,
1170                                      u32 portid, u32 seq)
1171 {
1172         struct devlink_port *devlink_port;
1173         u16 pool_count = devlink_sb_pool_count(devlink_sb);
1174         u16 pool_index;
1175         int err;
1176
1177         list_for_each_entry(devlink_port, &devlink->port_list, list) {
1178                 for (pool_index = 0; pool_index < pool_count; pool_index++) {
1179                         if (*p_idx < start) {
1180                                 (*p_idx)++;
1181                                 continue;
1182                         }
1183                         err = devlink_nl_sb_port_pool_fill(msg, devlink,
1184                                                            devlink_port,
1185                                                            devlink_sb,
1186                                                            pool_index,
1187                                                            DEVLINK_CMD_SB_PORT_POOL_NEW,
1188                                                            portid, seq,
1189                                                            NLM_F_MULTI);
1190                         if (err)
1191                                 return err;
1192                         (*p_idx)++;
1193                 }
1194         }
1195         return 0;
1196 }
1197
1198 static int devlink_nl_cmd_sb_port_pool_get_dumpit(struct sk_buff *msg,
1199                                                   struct netlink_callback *cb)
1200 {
1201         struct devlink *devlink;
1202         struct devlink_sb *devlink_sb;
1203         int start = cb->args[0];
1204         int idx = 0;
1205         int err;
1206
1207         mutex_lock(&devlink_mutex);
1208         list_for_each_entry(devlink, &devlink_list, list) {
1209                 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)) ||
1210                     !devlink->ops || !devlink->ops->sb_port_pool_get)
1211                         continue;
1212                 mutex_lock(&devlink->lock);
1213                 list_for_each_entry(devlink_sb, &devlink->sb_list, list) {
1214                         err = __sb_port_pool_get_dumpit(msg, start, &idx,
1215                                                         devlink, devlink_sb,
1216                                                         NETLINK_CB(cb->skb).portid,
1217                                                         cb->nlh->nlmsg_seq);
1218                         if (err && err != -EOPNOTSUPP) {
1219                                 mutex_unlock(&devlink->lock);
1220                                 goto out;
1221                         }
1222                 }
1223                 mutex_unlock(&devlink->lock);
1224         }
1225 out:
1226         mutex_unlock(&devlink_mutex);
1227
1228         cb->args[0] = idx;
1229         return msg->len;
1230 }
1231
1232 static int devlink_sb_port_pool_set(struct devlink_port *devlink_port,
1233                                     unsigned int sb_index, u16 pool_index,
1234                                     u32 threshold)
1235
1236 {
1237         const struct devlink_ops *ops = devlink_port->devlink->ops;
1238
1239         if (ops && ops->sb_port_pool_set)
1240                 return ops->sb_port_pool_set(devlink_port, sb_index,
1241                                              pool_index, threshold);
1242         return -EOPNOTSUPP;
1243 }
1244
1245 static int devlink_nl_cmd_sb_port_pool_set_doit(struct sk_buff *skb,
1246                                                 struct genl_info *info)
1247 {
1248         struct devlink_port *devlink_port = info->user_ptr[0];
1249         struct devlink_sb *devlink_sb = info->user_ptr[1];
1250         u16 pool_index;
1251         u32 threshold;
1252         int err;
1253
1254         err = devlink_sb_pool_index_get_from_info(devlink_sb, info,
1255                                                   &pool_index);
1256         if (err)
1257                 return err;
1258
1259         if (!info->attrs[DEVLINK_ATTR_SB_THRESHOLD])
1260                 return -EINVAL;
1261
1262         threshold = nla_get_u32(info->attrs[DEVLINK_ATTR_SB_THRESHOLD]);
1263         return devlink_sb_port_pool_set(devlink_port, devlink_sb->index,
1264                                         pool_index, threshold);
1265 }
1266
1267 static int
1268 devlink_nl_sb_tc_pool_bind_fill(struct sk_buff *msg, struct devlink *devlink,
1269                                 struct devlink_port *devlink_port,
1270                                 struct devlink_sb *devlink_sb, u16 tc_index,
1271                                 enum devlink_sb_pool_type pool_type,
1272                                 enum devlink_command cmd,
1273                                 u32 portid, u32 seq, int flags)
1274 {
1275         const struct devlink_ops *ops = devlink->ops;
1276         u16 pool_index;
1277         u32 threshold;
1278         void *hdr;
1279         int err;
1280
1281         err = ops->sb_tc_pool_bind_get(devlink_port, devlink_sb->index,
1282                                        tc_index, pool_type,
1283                                        &pool_index, &threshold);
1284         if (err)
1285                 return err;
1286
1287         hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
1288         if (!hdr)
1289                 return -EMSGSIZE;
1290
1291         if (devlink_nl_put_handle(msg, devlink))
1292                 goto nla_put_failure;
1293         if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, devlink_port->index))
1294                 goto nla_put_failure;
1295         if (nla_put_u32(msg, DEVLINK_ATTR_SB_INDEX, devlink_sb->index))
1296                 goto nla_put_failure;
1297         if (nla_put_u16(msg, DEVLINK_ATTR_SB_TC_INDEX, tc_index))
1298                 goto nla_put_failure;
1299         if (nla_put_u8(msg, DEVLINK_ATTR_SB_POOL_TYPE, pool_type))
1300                 goto nla_put_failure;
1301         if (nla_put_u16(msg, DEVLINK_ATTR_SB_POOL_INDEX, pool_index))
1302                 goto nla_put_failure;
1303         if (nla_put_u32(msg, DEVLINK_ATTR_SB_THRESHOLD, threshold))
1304                 goto nla_put_failure;
1305
1306         if (ops->sb_occ_tc_port_bind_get) {
1307                 u32 cur;
1308                 u32 max;
1309
1310                 err = ops->sb_occ_tc_port_bind_get(devlink_port,
1311                                                    devlink_sb->index,
1312                                                    tc_index, pool_type,
1313                                                    &cur, &max);
1314                 if (err && err != -EOPNOTSUPP)
1315                         return err;
1316                 if (!err) {
1317                         if (nla_put_u32(msg, DEVLINK_ATTR_SB_OCC_CUR, cur))
1318                                 goto nla_put_failure;
1319                         if (nla_put_u32(msg, DEVLINK_ATTR_SB_OCC_MAX, max))
1320                                 goto nla_put_failure;
1321                 }
1322         }
1323
1324         genlmsg_end(msg, hdr);
1325         return 0;
1326
1327 nla_put_failure:
1328         genlmsg_cancel(msg, hdr);
1329         return -EMSGSIZE;
1330 }
1331
1332 static int devlink_nl_cmd_sb_tc_pool_bind_get_doit(struct sk_buff *skb,
1333                                                    struct genl_info *info)
1334 {
1335         struct devlink_port *devlink_port = info->user_ptr[0];
1336         struct devlink *devlink = devlink_port->devlink;
1337         struct devlink_sb *devlink_sb = info->user_ptr[1];
1338         struct sk_buff *msg;
1339         enum devlink_sb_pool_type pool_type;
1340         u16 tc_index;
1341         int err;
1342
1343         err = devlink_sb_pool_type_get_from_info(info, &pool_type);
1344         if (err)
1345                 return err;
1346
1347         err = devlink_sb_tc_index_get_from_info(devlink_sb, info,
1348                                                 pool_type, &tc_index);
1349         if (err)
1350                 return err;
1351
1352         if (!devlink->ops || !devlink->ops->sb_tc_pool_bind_get)
1353                 return -EOPNOTSUPP;
1354
1355         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1356         if (!msg)
1357                 return -ENOMEM;
1358
1359         err = devlink_nl_sb_tc_pool_bind_fill(msg, devlink, devlink_port,
1360                                               devlink_sb, tc_index, pool_type,
1361                                               DEVLINK_CMD_SB_TC_POOL_BIND_NEW,
1362                                               info->snd_portid,
1363                                               info->snd_seq, 0);
1364         if (err) {
1365                 nlmsg_free(msg);
1366                 return err;
1367         }
1368
1369         return genlmsg_reply(msg, info);
1370 }
1371
1372 static int __sb_tc_pool_bind_get_dumpit(struct sk_buff *msg,
1373                                         int start, int *p_idx,
1374                                         struct devlink *devlink,
1375                                         struct devlink_sb *devlink_sb,
1376                                         u32 portid, u32 seq)
1377 {
1378         struct devlink_port *devlink_port;
1379         u16 tc_index;
1380         int err;
1381
1382         list_for_each_entry(devlink_port, &devlink->port_list, list) {
1383                 for (tc_index = 0;
1384                      tc_index < devlink_sb->ingress_tc_count; tc_index++) {
1385                         if (*p_idx < start) {
1386                                 (*p_idx)++;
1387                                 continue;
1388                         }
1389                         err = devlink_nl_sb_tc_pool_bind_fill(msg, devlink,
1390                                                               devlink_port,
1391                                                               devlink_sb,
1392                                                               tc_index,
1393                                                               DEVLINK_SB_POOL_TYPE_INGRESS,
1394                                                               DEVLINK_CMD_SB_TC_POOL_BIND_NEW,
1395                                                               portid, seq,
1396                                                               NLM_F_MULTI);
1397                         if (err)
1398                                 return err;
1399                         (*p_idx)++;
1400                 }
1401                 for (tc_index = 0;
1402                      tc_index < devlink_sb->egress_tc_count; tc_index++) {
1403                         if (*p_idx < start) {
1404                                 (*p_idx)++;
1405                                 continue;
1406                         }
1407                         err = devlink_nl_sb_tc_pool_bind_fill(msg, devlink,
1408                                                               devlink_port,
1409                                                               devlink_sb,
1410                                                               tc_index,
1411                                                               DEVLINK_SB_POOL_TYPE_EGRESS,
1412                                                               DEVLINK_CMD_SB_TC_POOL_BIND_NEW,
1413                                                               portid, seq,
1414                                                               NLM_F_MULTI);
1415                         if (err)
1416                                 return err;
1417                         (*p_idx)++;
1418                 }
1419         }
1420         return 0;
1421 }
1422
1423 static int
1424 devlink_nl_cmd_sb_tc_pool_bind_get_dumpit(struct sk_buff *msg,
1425                                           struct netlink_callback *cb)
1426 {
1427         struct devlink *devlink;
1428         struct devlink_sb *devlink_sb;
1429         int start = cb->args[0];
1430         int idx = 0;
1431         int err;
1432
1433         mutex_lock(&devlink_mutex);
1434         list_for_each_entry(devlink, &devlink_list, list) {
1435                 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)) ||
1436                     !devlink->ops || !devlink->ops->sb_tc_pool_bind_get)
1437                         continue;
1438
1439                 mutex_lock(&devlink->lock);
1440                 list_for_each_entry(devlink_sb, &devlink->sb_list, list) {
1441                         err = __sb_tc_pool_bind_get_dumpit(msg, start, &idx,
1442                                                            devlink,
1443                                                            devlink_sb,
1444                                                            NETLINK_CB(cb->skb).portid,
1445                                                            cb->nlh->nlmsg_seq);
1446                         if (err && err != -EOPNOTSUPP) {
1447                                 mutex_unlock(&devlink->lock);
1448                                 goto out;
1449                         }
1450                 }
1451                 mutex_unlock(&devlink->lock);
1452         }
1453 out:
1454         mutex_unlock(&devlink_mutex);
1455
1456         cb->args[0] = idx;
1457         return msg->len;
1458 }
1459
1460 static int devlink_sb_tc_pool_bind_set(struct devlink_port *devlink_port,
1461                                        unsigned int sb_index, u16 tc_index,
1462                                        enum devlink_sb_pool_type pool_type,
1463                                        u16 pool_index, u32 threshold)
1464
1465 {
1466         const struct devlink_ops *ops = devlink_port->devlink->ops;
1467
1468         if (ops && ops->sb_tc_pool_bind_set)
1469                 return ops->sb_tc_pool_bind_set(devlink_port, sb_index,
1470                                                 tc_index, pool_type,
1471                                                 pool_index, threshold);
1472         return -EOPNOTSUPP;
1473 }
1474
1475 static int devlink_nl_cmd_sb_tc_pool_bind_set_doit(struct sk_buff *skb,
1476                                                    struct genl_info *info)
1477 {
1478         struct devlink_port *devlink_port = info->user_ptr[0];
1479         struct devlink_sb *devlink_sb = info->user_ptr[1];
1480         enum devlink_sb_pool_type pool_type;
1481         u16 tc_index;
1482         u16 pool_index;
1483         u32 threshold;
1484         int err;
1485
1486         err = devlink_sb_pool_type_get_from_info(info, &pool_type);
1487         if (err)
1488                 return err;
1489
1490         err = devlink_sb_tc_index_get_from_info(devlink_sb, info,
1491                                                 pool_type, &tc_index);
1492         if (err)
1493                 return err;
1494
1495         err = devlink_sb_pool_index_get_from_info(devlink_sb, info,
1496                                                   &pool_index);
1497         if (err)
1498                 return err;
1499
1500         if (!info->attrs[DEVLINK_ATTR_SB_THRESHOLD])
1501                 return -EINVAL;
1502
1503         threshold = nla_get_u32(info->attrs[DEVLINK_ATTR_SB_THRESHOLD]);
1504         return devlink_sb_tc_pool_bind_set(devlink_port, devlink_sb->index,
1505                                            tc_index, pool_type,
1506                                            pool_index, threshold);
1507 }
1508
1509 static int devlink_nl_cmd_sb_occ_snapshot_doit(struct sk_buff *skb,
1510                                                struct genl_info *info)
1511 {
1512         struct devlink *devlink = info->user_ptr[0];
1513         struct devlink_sb *devlink_sb = info->user_ptr[1];
1514         const struct devlink_ops *ops = devlink->ops;
1515
1516         if (ops && ops->sb_occ_snapshot)
1517                 return ops->sb_occ_snapshot(devlink, devlink_sb->index);
1518         return -EOPNOTSUPP;
1519 }
1520
1521 static int devlink_nl_cmd_sb_occ_max_clear_doit(struct sk_buff *skb,
1522                                                 struct genl_info *info)
1523 {
1524         struct devlink *devlink = info->user_ptr[0];
1525         struct devlink_sb *devlink_sb = info->user_ptr[1];
1526         const struct devlink_ops *ops = devlink->ops;
1527
1528         if (ops && ops->sb_occ_max_clear)
1529                 return ops->sb_occ_max_clear(devlink, devlink_sb->index);
1530         return -EOPNOTSUPP;
1531 }
1532
1533 static int devlink_nl_eswitch_fill(struct sk_buff *msg, struct devlink *devlink,
1534                                    enum devlink_command cmd, u32 portid,
1535                                    u32 seq, int flags)
1536 {
1537         const struct devlink_ops *ops = devlink->ops;
1538         u8 inline_mode, encap_mode;
1539         void *hdr;
1540         int err = 0;
1541         u16 mode;
1542
1543         hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
1544         if (!hdr)
1545                 return -EMSGSIZE;
1546
1547         err = devlink_nl_put_handle(msg, devlink);
1548         if (err)
1549                 goto nla_put_failure;
1550
1551         if (ops->eswitch_mode_get) {
1552                 err = ops->eswitch_mode_get(devlink, &mode);
1553                 if (err)
1554                         goto nla_put_failure;
1555                 err = nla_put_u16(msg, DEVLINK_ATTR_ESWITCH_MODE, mode);
1556                 if (err)
1557                         goto nla_put_failure;
1558         }
1559
1560         if (ops->eswitch_inline_mode_get) {
1561                 err = ops->eswitch_inline_mode_get(devlink, &inline_mode);
1562                 if (err)
1563                         goto nla_put_failure;
1564                 err = nla_put_u8(msg, DEVLINK_ATTR_ESWITCH_INLINE_MODE,
1565                                  inline_mode);
1566                 if (err)
1567                         goto nla_put_failure;
1568         }
1569
1570         if (ops->eswitch_encap_mode_get) {
1571                 err = ops->eswitch_encap_mode_get(devlink, &encap_mode);
1572                 if (err)
1573                         goto nla_put_failure;
1574                 err = nla_put_u8(msg, DEVLINK_ATTR_ESWITCH_ENCAP_MODE, encap_mode);
1575                 if (err)
1576                         goto nla_put_failure;
1577         }
1578
1579         genlmsg_end(msg, hdr);
1580         return 0;
1581
1582 nla_put_failure:
1583         genlmsg_cancel(msg, hdr);
1584         return err;
1585 }
1586
1587 static int devlink_nl_cmd_eswitch_get_doit(struct sk_buff *skb,
1588                                            struct genl_info *info)
1589 {
1590         struct devlink *devlink = info->user_ptr[0];
1591         const struct devlink_ops *ops = devlink->ops;
1592         struct sk_buff *msg;
1593         int err;
1594
1595         if (!ops)
1596                 return -EOPNOTSUPP;
1597
1598         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1599         if (!msg)
1600                 return -ENOMEM;
1601
1602         err = devlink_nl_eswitch_fill(msg, devlink, DEVLINK_CMD_ESWITCH_GET,
1603                                       info->snd_portid, info->snd_seq, 0);
1604
1605         if (err) {
1606                 nlmsg_free(msg);
1607                 return err;
1608         }
1609
1610         return genlmsg_reply(msg, info);
1611 }
1612
1613 static int devlink_nl_cmd_eswitch_set_doit(struct sk_buff *skb,
1614                                            struct genl_info *info)
1615 {
1616         struct devlink *devlink = info->user_ptr[0];
1617         const struct devlink_ops *ops = devlink->ops;
1618         u8 inline_mode, encap_mode;
1619         int err = 0;
1620         u16 mode;
1621
1622         if (!ops)
1623                 return -EOPNOTSUPP;
1624
1625         if (info->attrs[DEVLINK_ATTR_ESWITCH_MODE]) {
1626                 if (!ops->eswitch_mode_set)
1627                         return -EOPNOTSUPP;
1628                 mode = nla_get_u16(info->attrs[DEVLINK_ATTR_ESWITCH_MODE]);
1629                 err = ops->eswitch_mode_set(devlink, mode, info->extack);
1630                 if (err)
1631                         return err;
1632         }
1633
1634         if (info->attrs[DEVLINK_ATTR_ESWITCH_INLINE_MODE]) {
1635                 if (!ops->eswitch_inline_mode_set)
1636                         return -EOPNOTSUPP;
1637                 inline_mode = nla_get_u8(
1638                                 info->attrs[DEVLINK_ATTR_ESWITCH_INLINE_MODE]);
1639                 err = ops->eswitch_inline_mode_set(devlink, inline_mode,
1640                                                    info->extack);
1641                 if (err)
1642                         return err;
1643         }
1644
1645         if (info->attrs[DEVLINK_ATTR_ESWITCH_ENCAP_MODE]) {
1646                 if (!ops->eswitch_encap_mode_set)
1647                         return -EOPNOTSUPP;
1648                 encap_mode = nla_get_u8(info->attrs[DEVLINK_ATTR_ESWITCH_ENCAP_MODE]);
1649                 err = ops->eswitch_encap_mode_set(devlink, encap_mode,
1650                                                   info->extack);
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                 .id = DEVLINK_PARAM_GENERIC_ID_IGNORE_ARI,
2682                 .name = DEVLINK_PARAM_GENERIC_IGNORE_ARI_NAME,
2683                 .type = DEVLINK_PARAM_GENERIC_IGNORE_ARI_TYPE,
2684         },
2685         {
2686                 .id = DEVLINK_PARAM_GENERIC_ID_MSIX_VEC_PER_PF_MAX,
2687                 .name = DEVLINK_PARAM_GENERIC_MSIX_VEC_PER_PF_MAX_NAME,
2688                 .type = DEVLINK_PARAM_GENERIC_MSIX_VEC_PER_PF_MAX_TYPE,
2689         },
2690         {
2691                 .id = DEVLINK_PARAM_GENERIC_ID_MSIX_VEC_PER_PF_MIN,
2692                 .name = DEVLINK_PARAM_GENERIC_MSIX_VEC_PER_PF_MIN_NAME,
2693                 .type = DEVLINK_PARAM_GENERIC_MSIX_VEC_PER_PF_MIN_TYPE,
2694         },
2695         {
2696                 .id = DEVLINK_PARAM_GENERIC_ID_FW_LOAD_POLICY,
2697                 .name = DEVLINK_PARAM_GENERIC_FW_LOAD_POLICY_NAME,
2698                 .type = DEVLINK_PARAM_GENERIC_FW_LOAD_POLICY_TYPE,
2699         },
2700         {
2701                 .id = DEVLINK_PARAM_GENERIC_ID_WOL,
2702                 .name = DEVLINK_PARAM_GENERIC_WOL_NAME,
2703                 .type = DEVLINK_PARAM_GENERIC_WOL_TYPE,
2704         },
2705 };
2706
2707 static int devlink_param_generic_verify(const struct devlink_param *param)
2708 {
2709         /* verify it match generic parameter by id and name */
2710         if (param->id > DEVLINK_PARAM_GENERIC_ID_MAX)
2711                 return -EINVAL;
2712         if (strcmp(param->name, devlink_param_generic[param->id].name))
2713                 return -ENOENT;
2714
2715         WARN_ON(param->type != devlink_param_generic[param->id].type);
2716
2717         return 0;
2718 }
2719
2720 static int devlink_param_driver_verify(const struct devlink_param *param)
2721 {
2722         int i;
2723
2724         if (param->id <= DEVLINK_PARAM_GENERIC_ID_MAX)
2725                 return -EINVAL;
2726         /* verify no such name in generic params */
2727         for (i = 0; i <= DEVLINK_PARAM_GENERIC_ID_MAX; i++)
2728                 if (!strcmp(param->name, devlink_param_generic[i].name))
2729                         return -EEXIST;
2730
2731         return 0;
2732 }
2733
2734 static struct devlink_param_item *
2735 devlink_param_find_by_name(struct list_head *param_list,
2736                            const char *param_name)
2737 {
2738         struct devlink_param_item *param_item;
2739
2740         list_for_each_entry(param_item, param_list, list)
2741                 if (!strcmp(param_item->param->name, param_name))
2742                         return param_item;
2743         return NULL;
2744 }
2745
2746 static struct devlink_param_item *
2747 devlink_param_find_by_id(struct list_head *param_list, u32 param_id)
2748 {
2749         struct devlink_param_item *param_item;
2750
2751         list_for_each_entry(param_item, param_list, list)
2752                 if (param_item->param->id == param_id)
2753                         return param_item;
2754         return NULL;
2755 }
2756
2757 static bool
2758 devlink_param_cmode_is_supported(const struct devlink_param *param,
2759                                  enum devlink_param_cmode cmode)
2760 {
2761         return test_bit(cmode, &param->supported_cmodes);
2762 }
2763
2764 static int devlink_param_get(struct devlink *devlink,
2765                              const struct devlink_param *param,
2766                              struct devlink_param_gset_ctx *ctx)
2767 {
2768         if (!param->get)
2769                 return -EOPNOTSUPP;
2770         return param->get(devlink, param->id, ctx);
2771 }
2772
2773 static int devlink_param_set(struct devlink *devlink,
2774                              const struct devlink_param *param,
2775                              struct devlink_param_gset_ctx *ctx)
2776 {
2777         if (!param->set)
2778                 return -EOPNOTSUPP;
2779         return param->set(devlink, param->id, ctx);
2780 }
2781
2782 static int
2783 devlink_param_type_to_nla_type(enum devlink_param_type param_type)
2784 {
2785         switch (param_type) {
2786         case DEVLINK_PARAM_TYPE_U8:
2787                 return NLA_U8;
2788         case DEVLINK_PARAM_TYPE_U16:
2789                 return NLA_U16;
2790         case DEVLINK_PARAM_TYPE_U32:
2791                 return NLA_U32;
2792         case DEVLINK_PARAM_TYPE_STRING:
2793                 return NLA_STRING;
2794         case DEVLINK_PARAM_TYPE_BOOL:
2795                 return NLA_FLAG;
2796         default:
2797                 return -EINVAL;
2798         }
2799 }
2800
2801 static int
2802 devlink_nl_param_value_fill_one(struct sk_buff *msg,
2803                                 enum devlink_param_type type,
2804                                 enum devlink_param_cmode cmode,
2805                                 union devlink_param_value val)
2806 {
2807         struct nlattr *param_value_attr;
2808
2809         param_value_attr = nla_nest_start(msg, DEVLINK_ATTR_PARAM_VALUE);
2810         if (!param_value_attr)
2811                 goto nla_put_failure;
2812
2813         if (nla_put_u8(msg, DEVLINK_ATTR_PARAM_VALUE_CMODE, cmode))
2814                 goto value_nest_cancel;
2815
2816         switch (type) {
2817         case DEVLINK_PARAM_TYPE_U8:
2818                 if (nla_put_u8(msg, DEVLINK_ATTR_PARAM_VALUE_DATA, val.vu8))
2819                         goto value_nest_cancel;
2820                 break;
2821         case DEVLINK_PARAM_TYPE_U16:
2822                 if (nla_put_u16(msg, DEVLINK_ATTR_PARAM_VALUE_DATA, val.vu16))
2823                         goto value_nest_cancel;
2824                 break;
2825         case DEVLINK_PARAM_TYPE_U32:
2826                 if (nla_put_u32(msg, DEVLINK_ATTR_PARAM_VALUE_DATA, val.vu32))
2827                         goto value_nest_cancel;
2828                 break;
2829         case DEVLINK_PARAM_TYPE_STRING:
2830                 if (nla_put_string(msg, DEVLINK_ATTR_PARAM_VALUE_DATA,
2831                                    val.vstr))
2832                         goto value_nest_cancel;
2833                 break;
2834         case DEVLINK_PARAM_TYPE_BOOL:
2835                 if (val.vbool &&
2836                     nla_put_flag(msg, DEVLINK_ATTR_PARAM_VALUE_DATA))
2837                         goto value_nest_cancel;
2838                 break;
2839         }
2840
2841         nla_nest_end(msg, param_value_attr);
2842         return 0;
2843
2844 value_nest_cancel:
2845         nla_nest_cancel(msg, param_value_attr);
2846 nla_put_failure:
2847         return -EMSGSIZE;
2848 }
2849
2850 static int devlink_nl_param_fill(struct sk_buff *msg, struct devlink *devlink,
2851                                  unsigned int port_index,
2852                                  struct devlink_param_item *param_item,
2853                                  enum devlink_command cmd,
2854                                  u32 portid, u32 seq, int flags)
2855 {
2856         union devlink_param_value param_value[DEVLINK_PARAM_CMODE_MAX + 1];
2857         const struct devlink_param *param = param_item->param;
2858         struct devlink_param_gset_ctx ctx;
2859         struct nlattr *param_values_list;
2860         struct nlattr *param_attr;
2861         int nla_type;
2862         void *hdr;
2863         int err;
2864         int i;
2865
2866         /* Get value from driver part to driverinit configuration mode */
2867         for (i = 0; i <= DEVLINK_PARAM_CMODE_MAX; i++) {
2868                 if (!devlink_param_cmode_is_supported(param, i))
2869                         continue;
2870                 if (i == DEVLINK_PARAM_CMODE_DRIVERINIT) {
2871                         if (!param_item->driverinit_value_valid)
2872                                 return -EOPNOTSUPP;
2873                         param_value[i] = param_item->driverinit_value;
2874                 } else {
2875                         ctx.cmode = i;
2876                         err = devlink_param_get(devlink, param, &ctx);
2877                         if (err)
2878                                 return err;
2879                         param_value[i] = ctx.val;
2880                 }
2881         }
2882
2883         hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
2884         if (!hdr)
2885                 return -EMSGSIZE;
2886
2887         if (devlink_nl_put_handle(msg, devlink))
2888                 goto genlmsg_cancel;
2889
2890         if (cmd == DEVLINK_CMD_PORT_PARAM_GET ||
2891             cmd == DEVLINK_CMD_PORT_PARAM_NEW ||
2892             cmd == DEVLINK_CMD_PORT_PARAM_DEL)
2893                 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, port_index))
2894                         goto genlmsg_cancel;
2895
2896         param_attr = nla_nest_start(msg, DEVLINK_ATTR_PARAM);
2897         if (!param_attr)
2898                 goto genlmsg_cancel;
2899         if (nla_put_string(msg, DEVLINK_ATTR_PARAM_NAME, param->name))
2900                 goto param_nest_cancel;
2901         if (param->generic && nla_put_flag(msg, DEVLINK_ATTR_PARAM_GENERIC))
2902                 goto param_nest_cancel;
2903
2904         nla_type = devlink_param_type_to_nla_type(param->type);
2905         if (nla_type < 0)
2906                 goto param_nest_cancel;
2907         if (nla_put_u8(msg, DEVLINK_ATTR_PARAM_TYPE, nla_type))
2908                 goto param_nest_cancel;
2909
2910         param_values_list = nla_nest_start(msg, DEVLINK_ATTR_PARAM_VALUES_LIST);
2911         if (!param_values_list)
2912                 goto param_nest_cancel;
2913
2914         for (i = 0; i <= DEVLINK_PARAM_CMODE_MAX; i++) {
2915                 if (!devlink_param_cmode_is_supported(param, i))
2916                         continue;
2917                 err = devlink_nl_param_value_fill_one(msg, param->type,
2918                                                       i, param_value[i]);
2919                 if (err)
2920                         goto values_list_nest_cancel;
2921         }
2922
2923         nla_nest_end(msg, param_values_list);
2924         nla_nest_end(msg, param_attr);
2925         genlmsg_end(msg, hdr);
2926         return 0;
2927
2928 values_list_nest_cancel:
2929         nla_nest_end(msg, param_values_list);
2930 param_nest_cancel:
2931         nla_nest_cancel(msg, param_attr);
2932 genlmsg_cancel:
2933         genlmsg_cancel(msg, hdr);
2934         return -EMSGSIZE;
2935 }
2936
2937 static void devlink_param_notify(struct devlink *devlink,
2938                                  unsigned int port_index,
2939                                  struct devlink_param_item *param_item,
2940                                  enum devlink_command cmd)
2941 {
2942         struct sk_buff *msg;
2943         int err;
2944
2945         WARN_ON(cmd != DEVLINK_CMD_PARAM_NEW && cmd != DEVLINK_CMD_PARAM_DEL &&
2946                 cmd != DEVLINK_CMD_PORT_PARAM_NEW &&
2947                 cmd != DEVLINK_CMD_PORT_PARAM_DEL);
2948
2949         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2950         if (!msg)
2951                 return;
2952         err = devlink_nl_param_fill(msg, devlink, port_index, param_item, cmd,
2953                                     0, 0, 0);
2954         if (err) {
2955                 nlmsg_free(msg);
2956                 return;
2957         }
2958
2959         genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
2960                                 msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
2961 }
2962
2963 static int devlink_nl_cmd_param_get_dumpit(struct sk_buff *msg,
2964                                            struct netlink_callback *cb)
2965 {
2966         struct devlink_param_item *param_item;
2967         struct devlink *devlink;
2968         int start = cb->args[0];
2969         int idx = 0;
2970         int err;
2971
2972         mutex_lock(&devlink_mutex);
2973         list_for_each_entry(devlink, &devlink_list, list) {
2974                 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
2975                         continue;
2976                 mutex_lock(&devlink->lock);
2977                 list_for_each_entry(param_item, &devlink->param_list, list) {
2978                         if (idx < start) {
2979                                 idx++;
2980                                 continue;
2981                         }
2982                         err = devlink_nl_param_fill(msg, devlink, 0, param_item,
2983                                                     DEVLINK_CMD_PARAM_GET,
2984                                                     NETLINK_CB(cb->skb).portid,
2985                                                     cb->nlh->nlmsg_seq,
2986                                                     NLM_F_MULTI);
2987                         if (err) {
2988                                 mutex_unlock(&devlink->lock);
2989                                 goto out;
2990                         }
2991                         idx++;
2992                 }
2993                 mutex_unlock(&devlink->lock);
2994         }
2995 out:
2996         mutex_unlock(&devlink_mutex);
2997
2998         cb->args[0] = idx;
2999         return msg->len;
3000 }
3001
3002 static int
3003 devlink_param_type_get_from_info(struct genl_info *info,
3004                                  enum devlink_param_type *param_type)
3005 {
3006         if (!info->attrs[DEVLINK_ATTR_PARAM_TYPE])
3007                 return -EINVAL;
3008
3009         switch (nla_get_u8(info->attrs[DEVLINK_ATTR_PARAM_TYPE])) {
3010         case NLA_U8:
3011                 *param_type = DEVLINK_PARAM_TYPE_U8;
3012                 break;
3013         case NLA_U16:
3014                 *param_type = DEVLINK_PARAM_TYPE_U16;
3015                 break;
3016         case NLA_U32:
3017                 *param_type = DEVLINK_PARAM_TYPE_U32;
3018                 break;
3019         case NLA_STRING:
3020                 *param_type = DEVLINK_PARAM_TYPE_STRING;
3021                 break;
3022         case NLA_FLAG:
3023                 *param_type = DEVLINK_PARAM_TYPE_BOOL;
3024                 break;
3025         default:
3026                 return -EINVAL;
3027         }
3028
3029         return 0;
3030 }
3031
3032 static int
3033 devlink_param_value_get_from_info(const struct devlink_param *param,
3034                                   struct genl_info *info,
3035                                   union devlink_param_value *value)
3036 {
3037         int len;
3038
3039         if (param->type != DEVLINK_PARAM_TYPE_BOOL &&
3040             !info->attrs[DEVLINK_ATTR_PARAM_VALUE_DATA])
3041                 return -EINVAL;
3042
3043         switch (param->type) {
3044         case DEVLINK_PARAM_TYPE_U8:
3045                 value->vu8 = nla_get_u8(info->attrs[DEVLINK_ATTR_PARAM_VALUE_DATA]);
3046                 break;
3047         case DEVLINK_PARAM_TYPE_U16:
3048                 value->vu16 = nla_get_u16(info->attrs[DEVLINK_ATTR_PARAM_VALUE_DATA]);
3049                 break;
3050         case DEVLINK_PARAM_TYPE_U32:
3051                 value->vu32 = nla_get_u32(info->attrs[DEVLINK_ATTR_PARAM_VALUE_DATA]);
3052                 break;
3053         case DEVLINK_PARAM_TYPE_STRING:
3054                 len = strnlen(nla_data(info->attrs[DEVLINK_ATTR_PARAM_VALUE_DATA]),
3055                               nla_len(info->attrs[DEVLINK_ATTR_PARAM_VALUE_DATA]));
3056                 if (len == nla_len(info->attrs[DEVLINK_ATTR_PARAM_VALUE_DATA]) ||
3057                     len >= __DEVLINK_PARAM_MAX_STRING_VALUE)
3058                         return -EINVAL;
3059                 strcpy(value->vstr,
3060                        nla_data(info->attrs[DEVLINK_ATTR_PARAM_VALUE_DATA]));
3061                 break;
3062         case DEVLINK_PARAM_TYPE_BOOL:
3063                 value->vbool = info->attrs[DEVLINK_ATTR_PARAM_VALUE_DATA] ?
3064                                true : false;
3065                 break;
3066         }
3067         return 0;
3068 }
3069
3070 static struct devlink_param_item *
3071 devlink_param_get_from_info(struct list_head *param_list,
3072                             struct genl_info *info)
3073 {
3074         char *param_name;
3075
3076         if (!info->attrs[DEVLINK_ATTR_PARAM_NAME])
3077                 return NULL;
3078
3079         param_name = nla_data(info->attrs[DEVLINK_ATTR_PARAM_NAME]);
3080         return devlink_param_find_by_name(param_list, param_name);
3081 }
3082
3083 static int devlink_nl_cmd_param_get_doit(struct sk_buff *skb,
3084                                          struct genl_info *info)
3085 {
3086         struct devlink *devlink = info->user_ptr[0];
3087         struct devlink_param_item *param_item;
3088         struct sk_buff *msg;
3089         int err;
3090
3091         param_item = devlink_param_get_from_info(&devlink->param_list, info);
3092         if (!param_item)
3093                 return -EINVAL;
3094
3095         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
3096         if (!msg)
3097                 return -ENOMEM;
3098
3099         err = devlink_nl_param_fill(msg, devlink, 0, param_item,
3100                                     DEVLINK_CMD_PARAM_GET,
3101                                     info->snd_portid, info->snd_seq, 0);
3102         if (err) {
3103                 nlmsg_free(msg);
3104                 return err;
3105         }
3106
3107         return genlmsg_reply(msg, info);
3108 }
3109
3110 static int __devlink_nl_cmd_param_set_doit(struct devlink *devlink,
3111                                            unsigned int port_index,
3112                                            struct list_head *param_list,
3113                                            struct genl_info *info,
3114                                            enum devlink_command cmd)
3115 {
3116         enum devlink_param_type param_type;
3117         struct devlink_param_gset_ctx ctx;
3118         enum devlink_param_cmode cmode;
3119         struct devlink_param_item *param_item;
3120         const struct devlink_param *param;
3121         union devlink_param_value value;
3122         int err = 0;
3123
3124         param_item = devlink_param_get_from_info(param_list, info);
3125         if (!param_item)
3126                 return -EINVAL;
3127         param = param_item->param;
3128         err = devlink_param_type_get_from_info(info, &param_type);
3129         if (err)
3130                 return err;
3131         if (param_type != param->type)
3132                 return -EINVAL;
3133         err = devlink_param_value_get_from_info(param, info, &value);
3134         if (err)
3135                 return err;
3136         if (param->validate) {
3137                 err = param->validate(devlink, param->id, value, info->extack);
3138                 if (err)
3139                         return err;
3140         }
3141
3142         if (!info->attrs[DEVLINK_ATTR_PARAM_VALUE_CMODE])
3143                 return -EINVAL;
3144         cmode = nla_get_u8(info->attrs[DEVLINK_ATTR_PARAM_VALUE_CMODE]);
3145         if (!devlink_param_cmode_is_supported(param, cmode))
3146                 return -EOPNOTSUPP;
3147
3148         if (cmode == DEVLINK_PARAM_CMODE_DRIVERINIT) {
3149                 if (param->type == DEVLINK_PARAM_TYPE_STRING)
3150                         strcpy(param_item->driverinit_value.vstr, value.vstr);
3151                 else
3152                         param_item->driverinit_value = value;
3153                 param_item->driverinit_value_valid = true;
3154         } else {
3155                 if (!param->set)
3156                         return -EOPNOTSUPP;
3157                 ctx.val = value;
3158                 ctx.cmode = cmode;
3159                 err = devlink_param_set(devlink, param, &ctx);
3160                 if (err)
3161                         return err;
3162         }
3163
3164         devlink_param_notify(devlink, port_index, param_item, cmd);
3165         return 0;
3166 }
3167
3168 static int devlink_nl_cmd_param_set_doit(struct sk_buff *skb,
3169                                          struct genl_info *info)
3170 {
3171         struct devlink *devlink = info->user_ptr[0];
3172
3173         return __devlink_nl_cmd_param_set_doit(devlink, 0, &devlink->param_list,
3174                                                info, DEVLINK_CMD_PARAM_NEW);
3175 }
3176
3177 static int devlink_param_register_one(struct devlink *devlink,
3178                                       unsigned int port_index,
3179                                       struct list_head *param_list,
3180                                       const struct devlink_param *param,
3181                                       enum devlink_command cmd)
3182 {
3183         struct devlink_param_item *param_item;
3184
3185         if (devlink_param_find_by_name(param_list, param->name))
3186                 return -EEXIST;
3187
3188         if (param->supported_cmodes == BIT(DEVLINK_PARAM_CMODE_DRIVERINIT))
3189                 WARN_ON(param->get || param->set);
3190         else
3191                 WARN_ON(!param->get || !param->set);
3192
3193         param_item = kzalloc(sizeof(*param_item), GFP_KERNEL);
3194         if (!param_item)
3195                 return -ENOMEM;
3196         param_item->param = param;
3197
3198         list_add_tail(&param_item->list, param_list);
3199         devlink_param_notify(devlink, port_index, param_item, cmd);
3200         return 0;
3201 }
3202
3203 static void devlink_param_unregister_one(struct devlink *devlink,
3204                                          unsigned int port_index,
3205                                          struct list_head *param_list,
3206                                          const struct devlink_param *param,
3207                                          enum devlink_command cmd)
3208 {
3209         struct devlink_param_item *param_item;
3210
3211         param_item = devlink_param_find_by_name(param_list, param->name);
3212         WARN_ON(!param_item);
3213         devlink_param_notify(devlink, port_index, param_item, cmd);
3214         list_del(&param_item->list);
3215         kfree(param_item);
3216 }
3217
3218 static int devlink_nl_cmd_port_param_get_dumpit(struct sk_buff *msg,
3219                                                 struct netlink_callback *cb)
3220 {
3221         struct devlink_param_item *param_item;
3222         struct devlink_port *devlink_port;
3223         struct devlink *devlink;
3224         int start = cb->args[0];
3225         int idx = 0;
3226         int err;
3227
3228         mutex_lock(&devlink_mutex);
3229         list_for_each_entry(devlink, &devlink_list, list) {
3230                 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
3231                         continue;
3232                 mutex_lock(&devlink->lock);
3233                 list_for_each_entry(devlink_port, &devlink->port_list, list) {
3234                         list_for_each_entry(param_item,
3235                                             &devlink_port->param_list, list) {
3236                                 if (idx < start) {
3237                                         idx++;
3238                                         continue;
3239                                 }
3240                                 err = devlink_nl_param_fill(msg,
3241                                                 devlink_port->devlink,
3242                                                 devlink_port->index, param_item,
3243                                                 DEVLINK_CMD_PORT_PARAM_GET,
3244                                                 NETLINK_CB(cb->skb).portid,
3245                                                 cb->nlh->nlmsg_seq,
3246                                                 NLM_F_MULTI);
3247                                 if (err) {
3248                                         mutex_unlock(&devlink->lock);
3249                                         goto out;
3250                                 }
3251                                 idx++;
3252                         }
3253                 }
3254                 mutex_unlock(&devlink->lock);
3255         }
3256 out:
3257         mutex_unlock(&devlink_mutex);
3258
3259         cb->args[0] = idx;
3260         return msg->len;
3261 }
3262
3263 static int devlink_nl_cmd_port_param_get_doit(struct sk_buff *skb,
3264                                               struct genl_info *info)
3265 {
3266         struct devlink_port *devlink_port = info->user_ptr[0];
3267         struct devlink_param_item *param_item;
3268         struct sk_buff *msg;
3269         int err;
3270
3271         param_item = devlink_param_get_from_info(&devlink_port->param_list,
3272                                                  info);
3273         if (!param_item)
3274                 return -EINVAL;
3275
3276         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
3277         if (!msg)
3278                 return -ENOMEM;
3279
3280         err = devlink_nl_param_fill(msg, devlink_port->devlink,
3281                                     devlink_port->index, param_item,
3282                                     DEVLINK_CMD_PORT_PARAM_GET,
3283                                     info->snd_portid, info->snd_seq, 0);
3284         if (err) {
3285                 nlmsg_free(msg);
3286                 return err;
3287         }
3288
3289         return genlmsg_reply(msg, info);
3290 }
3291
3292 static int devlink_nl_cmd_port_param_set_doit(struct sk_buff *skb,
3293                                               struct genl_info *info)
3294 {
3295         struct devlink_port *devlink_port = info->user_ptr[0];
3296
3297         return __devlink_nl_cmd_param_set_doit(devlink_port->devlink,
3298                                                devlink_port->index,
3299                                                &devlink_port->param_list, info,
3300                                                DEVLINK_CMD_PORT_PARAM_NEW);
3301 }
3302
3303 static int devlink_nl_region_snapshot_id_put(struct sk_buff *msg,
3304                                              struct devlink *devlink,
3305                                              struct devlink_snapshot *snapshot)
3306 {
3307         struct nlattr *snap_attr;
3308         int err;
3309
3310         snap_attr = nla_nest_start(msg, DEVLINK_ATTR_REGION_SNAPSHOT);
3311         if (!snap_attr)
3312                 return -EINVAL;
3313
3314         err = nla_put_u32(msg, DEVLINK_ATTR_REGION_SNAPSHOT_ID, snapshot->id);
3315         if (err)
3316                 goto nla_put_failure;
3317
3318         nla_nest_end(msg, snap_attr);
3319         return 0;
3320
3321 nla_put_failure:
3322         nla_nest_cancel(msg, snap_attr);
3323         return err;
3324 }
3325
3326 static int devlink_nl_region_snapshots_id_put(struct sk_buff *msg,
3327                                               struct devlink *devlink,
3328                                               struct devlink_region *region)
3329 {
3330         struct devlink_snapshot *snapshot;
3331         struct nlattr *snapshots_attr;
3332         int err;
3333
3334         snapshots_attr = nla_nest_start(msg, DEVLINK_ATTR_REGION_SNAPSHOTS);
3335         if (!snapshots_attr)
3336                 return -EINVAL;
3337
3338         list_for_each_entry(snapshot, &region->snapshot_list, list) {
3339                 err = devlink_nl_region_snapshot_id_put(msg, devlink, snapshot);
3340                 if (err)
3341                         goto nla_put_failure;
3342         }
3343
3344         nla_nest_end(msg, snapshots_attr);
3345         return 0;
3346
3347 nla_put_failure:
3348         nla_nest_cancel(msg, snapshots_attr);
3349         return err;
3350 }
3351
3352 static int devlink_nl_region_fill(struct sk_buff *msg, struct devlink *devlink,
3353                                   enum devlink_command cmd, u32 portid,
3354                                   u32 seq, int flags,
3355                                   struct devlink_region *region)
3356 {
3357         void *hdr;
3358         int err;
3359
3360         hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
3361         if (!hdr)
3362                 return -EMSGSIZE;
3363
3364         err = devlink_nl_put_handle(msg, devlink);
3365         if (err)
3366                 goto nla_put_failure;
3367
3368         err = nla_put_string(msg, DEVLINK_ATTR_REGION_NAME, region->name);
3369         if (err)
3370                 goto nla_put_failure;
3371
3372         err = nla_put_u64_64bit(msg, DEVLINK_ATTR_REGION_SIZE,
3373                                 region->size,
3374                                 DEVLINK_ATTR_PAD);
3375         if (err)
3376                 goto nla_put_failure;
3377
3378         err = devlink_nl_region_snapshots_id_put(msg, devlink, region);
3379         if (err)
3380                 goto nla_put_failure;
3381
3382         genlmsg_end(msg, hdr);
3383         return 0;
3384
3385 nla_put_failure:
3386         genlmsg_cancel(msg, hdr);
3387         return err;
3388 }
3389
3390 static void devlink_nl_region_notify(struct devlink_region *region,
3391                                      struct devlink_snapshot *snapshot,
3392                                      enum devlink_command cmd)
3393 {
3394         struct devlink *devlink = region->devlink;
3395         struct sk_buff *msg;
3396         void *hdr;
3397         int err;
3398
3399         WARN_ON(cmd != DEVLINK_CMD_REGION_NEW && cmd != DEVLINK_CMD_REGION_DEL);
3400
3401         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
3402         if (!msg)
3403                 return;
3404
3405         hdr = genlmsg_put(msg, 0, 0, &devlink_nl_family, 0, cmd);
3406         if (!hdr)
3407                 goto out_free_msg;
3408
3409         err = devlink_nl_put_handle(msg, devlink);
3410         if (err)
3411                 goto out_cancel_msg;
3412
3413         err = nla_put_string(msg, DEVLINK_ATTR_REGION_NAME,
3414                              region->name);
3415         if (err)
3416                 goto out_cancel_msg;
3417
3418         if (snapshot) {
3419                 err = nla_put_u32(msg, DEVLINK_ATTR_REGION_SNAPSHOT_ID,
3420                                   snapshot->id);
3421                 if (err)
3422                         goto out_cancel_msg;
3423         } else {
3424                 err = nla_put_u64_64bit(msg, DEVLINK_ATTR_REGION_SIZE,
3425                                         region->size, DEVLINK_ATTR_PAD);
3426                 if (err)
3427                         goto out_cancel_msg;
3428         }
3429         genlmsg_end(msg, hdr);
3430
3431         genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
3432                                 msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
3433
3434         return;
3435
3436 out_cancel_msg:
3437         genlmsg_cancel(msg, hdr);
3438 out_free_msg:
3439         nlmsg_free(msg);
3440 }
3441
3442 static int devlink_nl_cmd_region_get_doit(struct sk_buff *skb,
3443                                           struct genl_info *info)
3444 {
3445         struct devlink *devlink = info->user_ptr[0];
3446         struct devlink_region *region;
3447         const char *region_name;
3448         struct sk_buff *msg;
3449         int err;
3450
3451         if (!info->attrs[DEVLINK_ATTR_REGION_NAME])
3452                 return -EINVAL;
3453
3454         region_name = nla_data(info->attrs[DEVLINK_ATTR_REGION_NAME]);
3455         region = devlink_region_get_by_name(devlink, region_name);
3456         if (!region)
3457                 return -EINVAL;
3458
3459         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
3460         if (!msg)
3461                 return -ENOMEM;
3462
3463         err = devlink_nl_region_fill(msg, devlink, DEVLINK_CMD_REGION_GET,
3464                                      info->snd_portid, info->snd_seq, 0,
3465                                      region);
3466         if (err) {
3467                 nlmsg_free(msg);
3468                 return err;
3469         }
3470
3471         return genlmsg_reply(msg, info);
3472 }
3473
3474 static int devlink_nl_cmd_region_get_dumpit(struct sk_buff *msg,
3475                                             struct netlink_callback *cb)
3476 {
3477         struct devlink_region *region;
3478         struct devlink *devlink;
3479         int start = cb->args[0];
3480         int idx = 0;
3481         int err;
3482
3483         mutex_lock(&devlink_mutex);
3484         list_for_each_entry(devlink, &devlink_list, list) {
3485                 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
3486                         continue;
3487
3488                 mutex_lock(&devlink->lock);
3489                 list_for_each_entry(region, &devlink->region_list, list) {
3490                         if (idx < start) {
3491                                 idx++;
3492                                 continue;
3493                         }
3494                         err = devlink_nl_region_fill(msg, devlink,
3495                                                      DEVLINK_CMD_REGION_GET,
3496                                                      NETLINK_CB(cb->skb).portid,
3497                                                      cb->nlh->nlmsg_seq,
3498                                                      NLM_F_MULTI, region);
3499                         if (err) {
3500                                 mutex_unlock(&devlink->lock);
3501                                 goto out;
3502                         }
3503                         idx++;
3504                 }
3505                 mutex_unlock(&devlink->lock);
3506         }
3507 out:
3508         mutex_unlock(&devlink_mutex);
3509         cb->args[0] = idx;
3510         return msg->len;
3511 }
3512
3513 static int devlink_nl_cmd_region_del(struct sk_buff *skb,
3514                                      struct genl_info *info)
3515 {
3516         struct devlink *devlink = info->user_ptr[0];
3517         struct devlink_snapshot *snapshot;
3518         struct devlink_region *region;
3519         const char *region_name;
3520         u32 snapshot_id;
3521
3522         if (!info->attrs[DEVLINK_ATTR_REGION_NAME] ||
3523             !info->attrs[DEVLINK_ATTR_REGION_SNAPSHOT_ID])
3524                 return -EINVAL;
3525
3526         region_name = nla_data(info->attrs[DEVLINK_ATTR_REGION_NAME]);
3527         snapshot_id = nla_get_u32(info->attrs[DEVLINK_ATTR_REGION_SNAPSHOT_ID]);
3528
3529         region = devlink_region_get_by_name(devlink, region_name);
3530         if (!region)
3531                 return -EINVAL;
3532
3533         snapshot = devlink_region_snapshot_get_by_id(region, snapshot_id);
3534         if (!snapshot)
3535                 return -EINVAL;
3536
3537         devlink_nl_region_notify(region, snapshot, DEVLINK_CMD_REGION_DEL);
3538         devlink_region_snapshot_del(snapshot);
3539         return 0;
3540 }
3541
3542 static int devlink_nl_cmd_region_read_chunk_fill(struct sk_buff *msg,
3543                                                  struct devlink *devlink,
3544                                                  u8 *chunk, u32 chunk_size,
3545                                                  u64 addr)
3546 {
3547         struct nlattr *chunk_attr;
3548         int err;
3549
3550         chunk_attr = nla_nest_start(msg, DEVLINK_ATTR_REGION_CHUNK);
3551         if (!chunk_attr)
3552                 return -EINVAL;
3553
3554         err = nla_put(msg, DEVLINK_ATTR_REGION_CHUNK_DATA, chunk_size, chunk);
3555         if (err)
3556                 goto nla_put_failure;
3557
3558         err = nla_put_u64_64bit(msg, DEVLINK_ATTR_REGION_CHUNK_ADDR, addr,
3559                                 DEVLINK_ATTR_PAD);
3560         if (err)
3561                 goto nla_put_failure;
3562
3563         nla_nest_end(msg, chunk_attr);
3564         return 0;
3565
3566 nla_put_failure:
3567         nla_nest_cancel(msg, chunk_attr);
3568         return err;
3569 }
3570
3571 #define DEVLINK_REGION_READ_CHUNK_SIZE 256
3572
3573 static int devlink_nl_region_read_snapshot_fill(struct sk_buff *skb,
3574                                                 struct devlink *devlink,
3575                                                 struct devlink_region *region,
3576                                                 struct nlattr **attrs,
3577                                                 u64 start_offset,
3578                                                 u64 end_offset,
3579                                                 bool dump,
3580                                                 u64 *new_offset)
3581 {
3582         struct devlink_snapshot *snapshot;
3583         u64 curr_offset = start_offset;
3584         u32 snapshot_id;
3585         int err = 0;
3586
3587         *new_offset = start_offset;
3588
3589         snapshot_id = nla_get_u32(attrs[DEVLINK_ATTR_REGION_SNAPSHOT_ID]);
3590         snapshot = devlink_region_snapshot_get_by_id(region, snapshot_id);
3591         if (!snapshot)
3592                 return -EINVAL;
3593
3594         if (end_offset > snapshot->data_len || dump)
3595                 end_offset = snapshot->data_len;
3596
3597         while (curr_offset < end_offset) {
3598                 u32 data_size;
3599                 u8 *data;
3600
3601                 if (end_offset - curr_offset < DEVLINK_REGION_READ_CHUNK_SIZE)
3602                         data_size = end_offset - curr_offset;
3603                 else
3604                         data_size = DEVLINK_REGION_READ_CHUNK_SIZE;
3605
3606                 data = &snapshot->data[curr_offset];
3607                 err = devlink_nl_cmd_region_read_chunk_fill(skb, devlink,
3608                                                             data, data_size,
3609                                                             curr_offset);
3610                 if (err)
3611                         break;
3612
3613                 curr_offset += data_size;
3614         }
3615         *new_offset = curr_offset;
3616
3617         return err;
3618 }
3619
3620 static int devlink_nl_cmd_region_read_dumpit(struct sk_buff *skb,
3621                                              struct netlink_callback *cb)
3622 {
3623         u64 ret_offset, start_offset, end_offset = 0;
3624         struct nlattr *attrs[DEVLINK_ATTR_MAX + 1];
3625         const struct genl_ops *ops = cb->data;
3626         struct devlink_region *region;
3627         struct nlattr *chunks_attr;
3628         const char *region_name;
3629         struct devlink *devlink;
3630         bool dump = true;
3631         void *hdr;
3632         int err;
3633
3634         start_offset = *((u64 *)&cb->args[0]);
3635
3636         err = nlmsg_parse(cb->nlh, GENL_HDRLEN + devlink_nl_family.hdrsize,
3637                           attrs, DEVLINK_ATTR_MAX, ops->policy, cb->extack);
3638         if (err)
3639                 goto out;
3640
3641         devlink = devlink_get_from_attrs(sock_net(cb->skb->sk), attrs);
3642         if (IS_ERR(devlink))
3643                 goto out;
3644
3645         mutex_lock(&devlink_mutex);
3646         mutex_lock(&devlink->lock);
3647
3648         if (!attrs[DEVLINK_ATTR_REGION_NAME] ||
3649             !attrs[DEVLINK_ATTR_REGION_SNAPSHOT_ID])
3650                 goto out_unlock;
3651
3652         region_name = nla_data(attrs[DEVLINK_ATTR_REGION_NAME]);
3653         region = devlink_region_get_by_name(devlink, region_name);
3654         if (!region)
3655                 goto out_unlock;
3656
3657         hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
3658                           &devlink_nl_family, NLM_F_ACK | NLM_F_MULTI,
3659                           DEVLINK_CMD_REGION_READ);
3660         if (!hdr)
3661                 goto out_unlock;
3662
3663         err = devlink_nl_put_handle(skb, devlink);
3664         if (err)
3665                 goto nla_put_failure;
3666
3667         err = nla_put_string(skb, DEVLINK_ATTR_REGION_NAME, region_name);
3668         if (err)
3669                 goto nla_put_failure;
3670
3671         chunks_attr = nla_nest_start(skb, DEVLINK_ATTR_REGION_CHUNKS);
3672         if (!chunks_attr)
3673                 goto nla_put_failure;
3674
3675         if (attrs[DEVLINK_ATTR_REGION_CHUNK_ADDR] &&
3676             attrs[DEVLINK_ATTR_REGION_CHUNK_LEN]) {
3677                 if (!start_offset)
3678                         start_offset =
3679                                 nla_get_u64(attrs[DEVLINK_ATTR_REGION_CHUNK_ADDR]);
3680
3681                 end_offset = nla_get_u64(attrs[DEVLINK_ATTR_REGION_CHUNK_ADDR]);
3682                 end_offset += nla_get_u64(attrs[DEVLINK_ATTR_REGION_CHUNK_LEN]);
3683                 dump = false;
3684         }
3685
3686         err = devlink_nl_region_read_snapshot_fill(skb, devlink,
3687                                                    region, attrs,
3688                                                    start_offset,
3689                                                    end_offset, dump,
3690                                                    &ret_offset);
3691
3692         if (err && err != -EMSGSIZE)
3693                 goto nla_put_failure;
3694
3695         /* Check if there was any progress done to prevent infinite loop */
3696         if (ret_offset == start_offset)
3697                 goto nla_put_failure;
3698
3699         *((u64 *)&cb->args[0]) = ret_offset;
3700
3701         nla_nest_end(skb, chunks_attr);
3702         genlmsg_end(skb, hdr);
3703         mutex_unlock(&devlink->lock);
3704         mutex_unlock(&devlink_mutex);
3705
3706         return skb->len;
3707
3708 nla_put_failure:
3709         genlmsg_cancel(skb, hdr);
3710 out_unlock:
3711         mutex_unlock(&devlink->lock);
3712         mutex_unlock(&devlink_mutex);
3713 out:
3714         return 0;
3715 }
3716
3717 struct devlink_info_req {
3718         struct sk_buff *msg;
3719 };
3720
3721 int devlink_info_driver_name_put(struct devlink_info_req *req, const char *name)
3722 {
3723         return nla_put_string(req->msg, DEVLINK_ATTR_INFO_DRIVER_NAME, name);
3724 }
3725 EXPORT_SYMBOL_GPL(devlink_info_driver_name_put);
3726
3727 int devlink_info_serial_number_put(struct devlink_info_req *req, const char *sn)
3728 {
3729         return nla_put_string(req->msg, DEVLINK_ATTR_INFO_SERIAL_NUMBER, sn);
3730 }
3731 EXPORT_SYMBOL_GPL(devlink_info_serial_number_put);
3732
3733 static int devlink_info_version_put(struct devlink_info_req *req, int attr,
3734                                     const char *version_name,
3735                                     const char *version_value)
3736 {
3737         struct nlattr *nest;
3738         int err;
3739
3740         nest = nla_nest_start(req->msg, attr);
3741         if (!nest)
3742                 return -EMSGSIZE;
3743
3744         err = nla_put_string(req->msg, DEVLINK_ATTR_INFO_VERSION_NAME,
3745                              version_name);
3746         if (err)
3747                 goto nla_put_failure;
3748
3749         err = nla_put_string(req->msg, DEVLINK_ATTR_INFO_VERSION_VALUE,
3750                              version_value);
3751         if (err)
3752                 goto nla_put_failure;
3753
3754         nla_nest_end(req->msg, nest);
3755
3756         return 0;
3757
3758 nla_put_failure:
3759         nla_nest_cancel(req->msg, nest);
3760         return err;
3761 }
3762
3763 int devlink_info_version_fixed_put(struct devlink_info_req *req,
3764                                    const char *version_name,
3765                                    const char *version_value)
3766 {
3767         return devlink_info_version_put(req, DEVLINK_ATTR_INFO_VERSION_FIXED,
3768                                         version_name, version_value);
3769 }
3770 EXPORT_SYMBOL_GPL(devlink_info_version_fixed_put);
3771
3772 int devlink_info_version_stored_put(struct devlink_info_req *req,
3773                                     const char *version_name,
3774                                     const char *version_value)
3775 {
3776         return devlink_info_version_put(req, DEVLINK_ATTR_INFO_VERSION_STORED,
3777                                         version_name, version_value);
3778 }
3779 EXPORT_SYMBOL_GPL(devlink_info_version_stored_put);
3780
3781 int devlink_info_version_running_put(struct devlink_info_req *req,
3782                                      const char *version_name,
3783                                      const char *version_value)
3784 {
3785         return devlink_info_version_put(req, DEVLINK_ATTR_INFO_VERSION_RUNNING,
3786                                         version_name, version_value);
3787 }
3788 EXPORT_SYMBOL_GPL(devlink_info_version_running_put);
3789
3790 static int
3791 devlink_nl_info_fill(struct sk_buff *msg, struct devlink *devlink,
3792                      enum devlink_command cmd, u32 portid,
3793                      u32 seq, int flags, struct netlink_ext_ack *extack)
3794 {
3795         struct devlink_info_req req;
3796         void *hdr;
3797         int err;
3798
3799         hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
3800         if (!hdr)
3801                 return -EMSGSIZE;
3802
3803         err = -EMSGSIZE;
3804         if (devlink_nl_put_handle(msg, devlink))
3805                 goto err_cancel_msg;
3806
3807         req.msg = msg;
3808         err = devlink->ops->info_get(devlink, &req, extack);
3809         if (err)
3810                 goto err_cancel_msg;
3811
3812         genlmsg_end(msg, hdr);
3813         return 0;
3814
3815 err_cancel_msg:
3816         genlmsg_cancel(msg, hdr);
3817         return err;
3818 }
3819
3820 static int devlink_nl_cmd_info_get_doit(struct sk_buff *skb,
3821                                         struct genl_info *info)
3822 {
3823         struct devlink *devlink = info->user_ptr[0];
3824         struct sk_buff *msg;
3825         int err;
3826
3827         if (!devlink->ops || !devlink->ops->info_get)
3828                 return -EOPNOTSUPP;
3829
3830         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
3831         if (!msg)
3832                 return -ENOMEM;
3833
3834         err = devlink_nl_info_fill(msg, devlink, DEVLINK_CMD_INFO_GET,
3835                                    info->snd_portid, info->snd_seq, 0,
3836                                    info->extack);
3837         if (err) {
3838                 nlmsg_free(msg);
3839                 return err;
3840         }
3841
3842         return genlmsg_reply(msg, info);
3843 }
3844
3845 static int devlink_nl_cmd_info_get_dumpit(struct sk_buff *msg,
3846                                           struct netlink_callback *cb)
3847 {
3848         struct devlink *devlink;
3849         int start = cb->args[0];
3850         int idx = 0;
3851         int err;
3852
3853         mutex_lock(&devlink_mutex);
3854         list_for_each_entry(devlink, &devlink_list, list) {
3855                 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
3856                         continue;
3857                 if (idx < start) {
3858                         idx++;
3859                         continue;
3860                 }
3861
3862                 mutex_lock(&devlink->lock);
3863                 err = devlink_nl_info_fill(msg, devlink, DEVLINK_CMD_INFO_GET,
3864                                            NETLINK_CB(cb->skb).portid,
3865                                            cb->nlh->nlmsg_seq, NLM_F_MULTI,
3866                                            cb->extack);
3867                 mutex_unlock(&devlink->lock);
3868                 if (err)
3869                         break;
3870                 idx++;
3871         }
3872         mutex_unlock(&devlink_mutex);
3873
3874         cb->args[0] = idx;
3875         return msg->len;
3876 }
3877
3878 static const struct nla_policy devlink_nl_policy[DEVLINK_ATTR_MAX + 1] = {
3879         [DEVLINK_ATTR_BUS_NAME] = { .type = NLA_NUL_STRING },
3880         [DEVLINK_ATTR_DEV_NAME] = { .type = NLA_NUL_STRING },
3881         [DEVLINK_ATTR_PORT_INDEX] = { .type = NLA_U32 },
3882         [DEVLINK_ATTR_PORT_TYPE] = { .type = NLA_U16 },
3883         [DEVLINK_ATTR_PORT_SPLIT_COUNT] = { .type = NLA_U32 },
3884         [DEVLINK_ATTR_SB_INDEX] = { .type = NLA_U32 },
3885         [DEVLINK_ATTR_SB_POOL_INDEX] = { .type = NLA_U16 },
3886         [DEVLINK_ATTR_SB_POOL_TYPE] = { .type = NLA_U8 },
3887         [DEVLINK_ATTR_SB_POOL_SIZE] = { .type = NLA_U32 },
3888         [DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE] = { .type = NLA_U8 },
3889         [DEVLINK_ATTR_SB_THRESHOLD] = { .type = NLA_U32 },
3890         [DEVLINK_ATTR_SB_TC_INDEX] = { .type = NLA_U16 },
3891         [DEVLINK_ATTR_ESWITCH_MODE] = { .type = NLA_U16 },
3892         [DEVLINK_ATTR_ESWITCH_INLINE_MODE] = { .type = NLA_U8 },
3893         [DEVLINK_ATTR_ESWITCH_ENCAP_MODE] = { .type = NLA_U8 },
3894         [DEVLINK_ATTR_DPIPE_TABLE_NAME] = { .type = NLA_NUL_STRING },
3895         [DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED] = { .type = NLA_U8 },
3896         [DEVLINK_ATTR_RESOURCE_ID] = { .type = NLA_U64},
3897         [DEVLINK_ATTR_RESOURCE_SIZE] = { .type = NLA_U64},
3898         [DEVLINK_ATTR_PARAM_NAME] = { .type = NLA_NUL_STRING },
3899         [DEVLINK_ATTR_PARAM_TYPE] = { .type = NLA_U8 },
3900         [DEVLINK_ATTR_PARAM_VALUE_CMODE] = { .type = NLA_U8 },
3901         [DEVLINK_ATTR_REGION_NAME] = { .type = NLA_NUL_STRING },
3902         [DEVLINK_ATTR_REGION_SNAPSHOT_ID] = { .type = NLA_U32 },
3903 };
3904
3905 static const struct genl_ops devlink_nl_ops[] = {
3906         {
3907                 .cmd = DEVLINK_CMD_GET,
3908                 .doit = devlink_nl_cmd_get_doit,
3909                 .dumpit = devlink_nl_cmd_get_dumpit,
3910                 .policy = devlink_nl_policy,
3911                 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
3912                 /* can be retrieved by unprivileged users */
3913         },
3914         {
3915                 .cmd = DEVLINK_CMD_PORT_GET,
3916                 .doit = devlink_nl_cmd_port_get_doit,
3917                 .dumpit = devlink_nl_cmd_port_get_dumpit,
3918                 .policy = devlink_nl_policy,
3919                 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
3920                 /* can be retrieved by unprivileged users */
3921         },
3922         {
3923                 .cmd = DEVLINK_CMD_PORT_SET,
3924                 .doit = devlink_nl_cmd_port_set_doit,
3925                 .policy = devlink_nl_policy,
3926                 .flags = GENL_ADMIN_PERM,
3927                 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
3928         },
3929         {
3930                 .cmd = DEVLINK_CMD_PORT_SPLIT,
3931                 .doit = devlink_nl_cmd_port_split_doit,
3932                 .policy = devlink_nl_policy,
3933                 .flags = GENL_ADMIN_PERM,
3934                 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
3935                                   DEVLINK_NL_FLAG_NO_LOCK,
3936         },
3937         {
3938                 .cmd = DEVLINK_CMD_PORT_UNSPLIT,
3939                 .doit = devlink_nl_cmd_port_unsplit_doit,
3940                 .policy = devlink_nl_policy,
3941                 .flags = GENL_ADMIN_PERM,
3942                 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
3943                                   DEVLINK_NL_FLAG_NO_LOCK,
3944         },
3945         {
3946                 .cmd = DEVLINK_CMD_SB_GET,
3947                 .doit = devlink_nl_cmd_sb_get_doit,
3948                 .dumpit = devlink_nl_cmd_sb_get_dumpit,
3949                 .policy = devlink_nl_policy,
3950                 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
3951                                   DEVLINK_NL_FLAG_NEED_SB,
3952                 /* can be retrieved by unprivileged users */
3953         },
3954         {
3955                 .cmd = DEVLINK_CMD_SB_POOL_GET,
3956                 .doit = devlink_nl_cmd_sb_pool_get_doit,
3957                 .dumpit = devlink_nl_cmd_sb_pool_get_dumpit,
3958                 .policy = devlink_nl_policy,
3959                 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
3960                                   DEVLINK_NL_FLAG_NEED_SB,
3961                 /* can be retrieved by unprivileged users */
3962         },
3963         {
3964                 .cmd = DEVLINK_CMD_SB_POOL_SET,
3965                 .doit = devlink_nl_cmd_sb_pool_set_doit,
3966                 .policy = devlink_nl_policy,
3967                 .flags = GENL_ADMIN_PERM,
3968                 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
3969                                   DEVLINK_NL_FLAG_NEED_SB,
3970         },
3971         {
3972                 .cmd = DEVLINK_CMD_SB_PORT_POOL_GET,
3973                 .doit = devlink_nl_cmd_sb_port_pool_get_doit,
3974                 .dumpit = devlink_nl_cmd_sb_port_pool_get_dumpit,
3975                 .policy = devlink_nl_policy,
3976                 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT |
3977                                   DEVLINK_NL_FLAG_NEED_SB,
3978                 /* can be retrieved by unprivileged users */
3979         },
3980         {
3981                 .cmd = DEVLINK_CMD_SB_PORT_POOL_SET,
3982                 .doit = devlink_nl_cmd_sb_port_pool_set_doit,
3983                 .policy = devlink_nl_policy,
3984                 .flags = GENL_ADMIN_PERM,
3985                 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT |
3986                                   DEVLINK_NL_FLAG_NEED_SB,
3987         },
3988         {
3989                 .cmd = DEVLINK_CMD_SB_TC_POOL_BIND_GET,
3990                 .doit = devlink_nl_cmd_sb_tc_pool_bind_get_doit,
3991                 .dumpit = devlink_nl_cmd_sb_tc_pool_bind_get_dumpit,
3992                 .policy = devlink_nl_policy,
3993                 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT |
3994                                   DEVLINK_NL_FLAG_NEED_SB,
3995                 /* can be retrieved by unprivileged users */
3996         },
3997         {
3998                 .cmd = DEVLINK_CMD_SB_TC_POOL_BIND_SET,
3999                 .doit = devlink_nl_cmd_sb_tc_pool_bind_set_doit,
4000                 .policy = devlink_nl_policy,
4001                 .flags = GENL_ADMIN_PERM,
4002                 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT |
4003                                   DEVLINK_NL_FLAG_NEED_SB,
4004         },
4005         {
4006                 .cmd = DEVLINK_CMD_SB_OCC_SNAPSHOT,
4007                 .doit = devlink_nl_cmd_sb_occ_snapshot_doit,
4008                 .policy = devlink_nl_policy,
4009                 .flags = GENL_ADMIN_PERM,
4010                 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
4011                                   DEVLINK_NL_FLAG_NEED_SB,
4012         },
4013         {
4014                 .cmd = DEVLINK_CMD_SB_OCC_MAX_CLEAR,
4015                 .doit = devlink_nl_cmd_sb_occ_max_clear_doit,
4016                 .policy = devlink_nl_policy,
4017                 .flags = GENL_ADMIN_PERM,
4018                 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
4019                                   DEVLINK_NL_FLAG_NEED_SB,
4020         },
4021         {
4022                 .cmd = DEVLINK_CMD_ESWITCH_GET,
4023                 .doit = devlink_nl_cmd_eswitch_get_doit,
4024                 .policy = devlink_nl_policy,
4025                 .flags = GENL_ADMIN_PERM,
4026                 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
4027         },
4028         {
4029                 .cmd = DEVLINK_CMD_ESWITCH_SET,
4030                 .doit = devlink_nl_cmd_eswitch_set_doit,
4031                 .policy = devlink_nl_policy,
4032                 .flags = GENL_ADMIN_PERM,
4033                 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
4034                                   DEVLINK_NL_FLAG_NO_LOCK,
4035         },
4036         {
4037                 .cmd = DEVLINK_CMD_DPIPE_TABLE_GET,
4038                 .doit = devlink_nl_cmd_dpipe_table_get,
4039                 .policy = devlink_nl_policy,
4040                 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
4041                 /* can be retrieved by unprivileged users */
4042         },
4043         {
4044                 .cmd = DEVLINK_CMD_DPIPE_ENTRIES_GET,
4045                 .doit = devlink_nl_cmd_dpipe_entries_get,
4046                 .policy = devlink_nl_policy,
4047                 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
4048                 /* can be retrieved by unprivileged users */
4049         },
4050         {
4051                 .cmd = DEVLINK_CMD_DPIPE_HEADERS_GET,
4052                 .doit = devlink_nl_cmd_dpipe_headers_get,
4053                 .policy = devlink_nl_policy,
4054                 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
4055                 /* can be retrieved by unprivileged users */
4056         },
4057         {
4058                 .cmd = DEVLINK_CMD_DPIPE_TABLE_COUNTERS_SET,
4059                 .doit = devlink_nl_cmd_dpipe_table_counters_set,
4060                 .policy = devlink_nl_policy,
4061                 .flags = GENL_ADMIN_PERM,
4062                 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
4063         },
4064         {
4065                 .cmd = DEVLINK_CMD_RESOURCE_SET,
4066                 .doit = devlink_nl_cmd_resource_set,
4067                 .policy = devlink_nl_policy,
4068                 .flags = GENL_ADMIN_PERM,
4069                 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
4070         },
4071         {
4072                 .cmd = DEVLINK_CMD_RESOURCE_DUMP,
4073                 .doit = devlink_nl_cmd_resource_dump,
4074                 .policy = devlink_nl_policy,
4075                 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
4076                 /* can be retrieved by unprivileged users */
4077         },
4078         {
4079                 .cmd = DEVLINK_CMD_RELOAD,
4080                 .doit = devlink_nl_cmd_reload,
4081                 .policy = devlink_nl_policy,
4082                 .flags = GENL_ADMIN_PERM,
4083                 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
4084                                   DEVLINK_NL_FLAG_NO_LOCK,
4085         },
4086         {
4087                 .cmd = DEVLINK_CMD_PARAM_GET,
4088                 .doit = devlink_nl_cmd_param_get_doit,
4089                 .dumpit = devlink_nl_cmd_param_get_dumpit,
4090                 .policy = devlink_nl_policy,
4091                 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
4092                 /* can be retrieved by unprivileged users */
4093         },
4094         {
4095                 .cmd = DEVLINK_CMD_PARAM_SET,
4096                 .doit = devlink_nl_cmd_param_set_doit,
4097                 .policy = devlink_nl_policy,
4098                 .flags = GENL_ADMIN_PERM,
4099                 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
4100         },
4101         {
4102                 .cmd = DEVLINK_CMD_PORT_PARAM_GET,
4103                 .doit = devlink_nl_cmd_port_param_get_doit,
4104                 .dumpit = devlink_nl_cmd_port_param_get_dumpit,
4105                 .policy = devlink_nl_policy,
4106                 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
4107                 /* can be retrieved by unprivileged users */
4108         },
4109         {
4110                 .cmd = DEVLINK_CMD_PORT_PARAM_SET,
4111                 .doit = devlink_nl_cmd_port_param_set_doit,
4112                 .policy = devlink_nl_policy,
4113                 .flags = GENL_ADMIN_PERM,
4114                 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
4115         },
4116         {
4117                 .cmd = DEVLINK_CMD_REGION_GET,
4118                 .doit = devlink_nl_cmd_region_get_doit,
4119                 .dumpit = devlink_nl_cmd_region_get_dumpit,
4120                 .policy = devlink_nl_policy,
4121                 .flags = GENL_ADMIN_PERM,
4122                 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
4123         },
4124         {
4125                 .cmd = DEVLINK_CMD_REGION_DEL,
4126                 .doit = devlink_nl_cmd_region_del,
4127                 .policy = devlink_nl_policy,
4128                 .flags = GENL_ADMIN_PERM,
4129                 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
4130         },
4131         {
4132                 .cmd = DEVLINK_CMD_REGION_READ,
4133                 .dumpit = devlink_nl_cmd_region_read_dumpit,
4134                 .policy = devlink_nl_policy,
4135                 .flags = GENL_ADMIN_PERM,
4136                 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
4137         },
4138         {
4139                 .cmd = DEVLINK_CMD_INFO_GET,
4140                 .doit = devlink_nl_cmd_info_get_doit,
4141                 .dumpit = devlink_nl_cmd_info_get_dumpit,
4142                 .policy = devlink_nl_policy,
4143                 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
4144                 /* can be retrieved by unprivileged users */
4145         },
4146 };
4147
4148 static struct genl_family devlink_nl_family __ro_after_init = {
4149         .name           = DEVLINK_GENL_NAME,
4150         .version        = DEVLINK_GENL_VERSION,
4151         .maxattr        = DEVLINK_ATTR_MAX,
4152         .netnsok        = true,
4153         .pre_doit       = devlink_nl_pre_doit,
4154         .post_doit      = devlink_nl_post_doit,
4155         .module         = THIS_MODULE,
4156         .ops            = devlink_nl_ops,
4157         .n_ops          = ARRAY_SIZE(devlink_nl_ops),
4158         .mcgrps         = devlink_nl_mcgrps,
4159         .n_mcgrps       = ARRAY_SIZE(devlink_nl_mcgrps),
4160 };
4161
4162 /**
4163  *      devlink_alloc - Allocate new devlink instance resources
4164  *
4165  *      @ops: ops
4166  *      @priv_size: size of user private data
4167  *
4168  *      Allocate new devlink instance resources, including devlink index
4169  *      and name.
4170  */
4171 struct devlink *devlink_alloc(const struct devlink_ops *ops, size_t priv_size)
4172 {
4173         struct devlink *devlink;
4174
4175         devlink = kzalloc(sizeof(*devlink) + priv_size, GFP_KERNEL);
4176         if (!devlink)
4177                 return NULL;
4178         devlink->ops = ops;
4179         devlink_net_set(devlink, &init_net);
4180         INIT_LIST_HEAD(&devlink->port_list);
4181         INIT_LIST_HEAD(&devlink->sb_list);
4182         INIT_LIST_HEAD_RCU(&devlink->dpipe_table_list);
4183         INIT_LIST_HEAD(&devlink->resource_list);
4184         INIT_LIST_HEAD(&devlink->param_list);
4185         INIT_LIST_HEAD(&devlink->region_list);
4186         mutex_init(&devlink->lock);
4187         return devlink;
4188 }
4189 EXPORT_SYMBOL_GPL(devlink_alloc);
4190
4191 /**
4192  *      devlink_register - Register devlink instance
4193  *
4194  *      @devlink: devlink
4195  */
4196 int devlink_register(struct devlink *devlink, struct device *dev)
4197 {
4198         mutex_lock(&devlink_mutex);
4199         devlink->dev = dev;
4200         list_add_tail(&devlink->list, &devlink_list);
4201         devlink_notify(devlink, DEVLINK_CMD_NEW);
4202         mutex_unlock(&devlink_mutex);
4203         return 0;
4204 }
4205 EXPORT_SYMBOL_GPL(devlink_register);
4206
4207 /**
4208  *      devlink_unregister - Unregister devlink instance
4209  *
4210  *      @devlink: devlink
4211  */
4212 void devlink_unregister(struct devlink *devlink)
4213 {
4214         mutex_lock(&devlink_mutex);
4215         devlink_notify(devlink, DEVLINK_CMD_DEL);
4216         list_del(&devlink->list);
4217         mutex_unlock(&devlink_mutex);
4218 }
4219 EXPORT_SYMBOL_GPL(devlink_unregister);
4220
4221 /**
4222  *      devlink_free - Free devlink instance resources
4223  *
4224  *      @devlink: devlink
4225  */
4226 void devlink_free(struct devlink *devlink)
4227 {
4228         kfree(devlink);
4229 }
4230 EXPORT_SYMBOL_GPL(devlink_free);
4231
4232 /**
4233  *      devlink_port_register - Register devlink port
4234  *
4235  *      @devlink: devlink
4236  *      @devlink_port: devlink port
4237  *      @port_index
4238  *
4239  *      Register devlink port with provided port index. User can use
4240  *      any indexing, even hw-related one. devlink_port structure
4241  *      is convenient to be embedded inside user driver private structure.
4242  *      Note that the caller should take care of zeroing the devlink_port
4243  *      structure.
4244  */
4245 int devlink_port_register(struct devlink *devlink,
4246                           struct devlink_port *devlink_port,
4247                           unsigned int port_index)
4248 {
4249         mutex_lock(&devlink->lock);
4250         if (devlink_port_index_exists(devlink, port_index)) {
4251                 mutex_unlock(&devlink->lock);
4252                 return -EEXIST;
4253         }
4254         devlink_port->devlink = devlink;
4255         devlink_port->index = port_index;
4256         devlink_port->registered = true;
4257         list_add_tail(&devlink_port->list, &devlink->port_list);
4258         INIT_LIST_HEAD(&devlink_port->param_list);
4259         mutex_unlock(&devlink->lock);
4260         devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW);
4261         return 0;
4262 }
4263 EXPORT_SYMBOL_GPL(devlink_port_register);
4264
4265 /**
4266  *      devlink_port_unregister - Unregister devlink port
4267  *
4268  *      @devlink_port: devlink port
4269  */
4270 void devlink_port_unregister(struct devlink_port *devlink_port)
4271 {
4272         struct devlink *devlink = devlink_port->devlink;
4273
4274         devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_DEL);
4275         mutex_lock(&devlink->lock);
4276         list_del(&devlink_port->list);
4277         mutex_unlock(&devlink->lock);
4278 }
4279 EXPORT_SYMBOL_GPL(devlink_port_unregister);
4280
4281 static void __devlink_port_type_set(struct devlink_port *devlink_port,
4282                                     enum devlink_port_type type,
4283                                     void *type_dev)
4284 {
4285         devlink_port->type = type;
4286         devlink_port->type_dev = type_dev;
4287         devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW);
4288 }
4289
4290 /**
4291  *      devlink_port_type_eth_set - Set port type to Ethernet
4292  *
4293  *      @devlink_port: devlink port
4294  *      @netdev: related netdevice
4295  */
4296 void devlink_port_type_eth_set(struct devlink_port *devlink_port,
4297                                struct net_device *netdev)
4298 {
4299         return __devlink_port_type_set(devlink_port,
4300                                        DEVLINK_PORT_TYPE_ETH, netdev);
4301 }
4302 EXPORT_SYMBOL_GPL(devlink_port_type_eth_set);
4303
4304 /**
4305  *      devlink_port_type_ib_set - Set port type to InfiniBand
4306  *
4307  *      @devlink_port: devlink port
4308  *      @ibdev: related IB device
4309  */
4310 void devlink_port_type_ib_set(struct devlink_port *devlink_port,
4311                               struct ib_device *ibdev)
4312 {
4313         return __devlink_port_type_set(devlink_port,
4314                                        DEVLINK_PORT_TYPE_IB, ibdev);
4315 }
4316 EXPORT_SYMBOL_GPL(devlink_port_type_ib_set);
4317
4318 /**
4319  *      devlink_port_type_clear - Clear port type
4320  *
4321  *      @devlink_port: devlink port
4322  */
4323 void devlink_port_type_clear(struct devlink_port *devlink_port)
4324 {
4325         return __devlink_port_type_set(devlink_port,
4326                                        DEVLINK_PORT_TYPE_NOTSET, NULL);
4327 }
4328 EXPORT_SYMBOL_GPL(devlink_port_type_clear);
4329
4330 /**
4331  *      devlink_port_attrs_set - Set port attributes
4332  *
4333  *      @devlink_port: devlink port
4334  *      @flavour: flavour of the port
4335  *      @port_number: number of the port that is facing user, for example
4336  *                    the front panel port number
4337  *      @split: indicates if this is split port
4338  *      @split_subport_number: if the port is split, this is the number
4339  *                             of subport.
4340  */
4341 void devlink_port_attrs_set(struct devlink_port *devlink_port,
4342                             enum devlink_port_flavour flavour,
4343                             u32 port_number, bool split,
4344                             u32 split_subport_number)
4345 {
4346         struct devlink_port_attrs *attrs = &devlink_port->attrs;
4347
4348         attrs->set = true;
4349         attrs->flavour = flavour;
4350         attrs->port_number = port_number;
4351         attrs->split = split;
4352         attrs->split_subport_number = split_subport_number;
4353         devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW);
4354 }
4355 EXPORT_SYMBOL_GPL(devlink_port_attrs_set);
4356
4357 int devlink_port_get_phys_port_name(struct devlink_port *devlink_port,
4358                                     char *name, size_t len)
4359 {
4360         struct devlink_port_attrs *attrs = &devlink_port->attrs;
4361         int n = 0;
4362
4363         if (!attrs->set)
4364                 return -EOPNOTSUPP;
4365
4366         switch (attrs->flavour) {
4367         case DEVLINK_PORT_FLAVOUR_PHYSICAL:
4368                 if (!attrs->split)
4369                         n = snprintf(name, len, "p%u", attrs->port_number);
4370                 else
4371                         n = snprintf(name, len, "p%us%u", attrs->port_number,
4372                                      attrs->split_subport_number);
4373                 break;
4374         case DEVLINK_PORT_FLAVOUR_CPU:
4375         case DEVLINK_PORT_FLAVOUR_DSA:
4376                 /* As CPU and DSA ports do not have a netdevice associated
4377                  * case should not ever happen.
4378                  */
4379                 WARN_ON(1);
4380                 return -EINVAL;
4381         }
4382
4383         if (n >= len)
4384                 return -EINVAL;
4385
4386         return 0;
4387 }
4388 EXPORT_SYMBOL_GPL(devlink_port_get_phys_port_name);
4389
4390 int devlink_sb_register(struct devlink *devlink, unsigned int sb_index,
4391                         u32 size, u16 ingress_pools_count,
4392                         u16 egress_pools_count, u16 ingress_tc_count,
4393                         u16 egress_tc_count)
4394 {
4395         struct devlink_sb *devlink_sb;
4396         int err = 0;
4397
4398         mutex_lock(&devlink->lock);
4399         if (devlink_sb_index_exists(devlink, sb_index)) {
4400                 err = -EEXIST;
4401                 goto unlock;
4402         }
4403
4404         devlink_sb = kzalloc(sizeof(*devlink_sb), GFP_KERNEL);
4405         if (!devlink_sb) {
4406                 err = -ENOMEM;
4407                 goto unlock;
4408         }
4409         devlink_sb->index = sb_index;
4410         devlink_sb->size = size;
4411         devlink_sb->ingress_pools_count = ingress_pools_count;
4412         devlink_sb->egress_pools_count = egress_pools_count;
4413         devlink_sb->ingress_tc_count = ingress_tc_count;
4414         devlink_sb->egress_tc_count = egress_tc_count;
4415         list_add_tail(&devlink_sb->list, &devlink->sb_list);
4416 unlock:
4417         mutex_unlock(&devlink->lock);
4418         return err;
4419 }
4420 EXPORT_SYMBOL_GPL(devlink_sb_register);
4421
4422 void devlink_sb_unregister(struct devlink *devlink, unsigned int sb_index)
4423 {
4424         struct devlink_sb *devlink_sb;
4425
4426         mutex_lock(&devlink->lock);
4427         devlink_sb = devlink_sb_get_by_index(devlink, sb_index);
4428         WARN_ON(!devlink_sb);
4429         list_del(&devlink_sb->list);
4430         mutex_unlock(&devlink->lock);
4431         kfree(devlink_sb);
4432 }
4433 EXPORT_SYMBOL_GPL(devlink_sb_unregister);
4434
4435 /**
4436  *      devlink_dpipe_headers_register - register dpipe headers
4437  *
4438  *      @devlink: devlink
4439  *      @dpipe_headers: dpipe header array
4440  *
4441  *      Register the headers supported by hardware.
4442  */
4443 int devlink_dpipe_headers_register(struct devlink *devlink,
4444                                    struct devlink_dpipe_headers *dpipe_headers)
4445 {
4446         mutex_lock(&devlink->lock);
4447         devlink->dpipe_headers = dpipe_headers;
4448         mutex_unlock(&devlink->lock);
4449         return 0;
4450 }
4451 EXPORT_SYMBOL_GPL(devlink_dpipe_headers_register);
4452
4453 /**
4454  *      devlink_dpipe_headers_unregister - unregister dpipe headers
4455  *
4456  *      @devlink: devlink
4457  *
4458  *      Unregister the headers supported by hardware.
4459  */
4460 void devlink_dpipe_headers_unregister(struct devlink *devlink)
4461 {
4462         mutex_lock(&devlink->lock);
4463         devlink->dpipe_headers = NULL;
4464         mutex_unlock(&devlink->lock);
4465 }
4466 EXPORT_SYMBOL_GPL(devlink_dpipe_headers_unregister);
4467
4468 /**
4469  *      devlink_dpipe_table_counter_enabled - check if counter allocation
4470  *                                            required
4471  *      @devlink: devlink
4472  *      @table_name: tables name
4473  *
4474  *      Used by driver to check if counter allocation is required.
4475  *      After counter allocation is turned on the table entries
4476  *      are updated to include counter statistics.
4477  *
4478  *      After that point on the driver must respect the counter
4479  *      state so that each entry added to the table is added
4480  *      with a counter.
4481  */
4482 bool devlink_dpipe_table_counter_enabled(struct devlink *devlink,
4483                                          const char *table_name)
4484 {
4485         struct devlink_dpipe_table *table;
4486         bool enabled;
4487
4488         rcu_read_lock();
4489         table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
4490                                          table_name);
4491         enabled = false;
4492         if (table)
4493                 enabled = table->counters_enabled;
4494         rcu_read_unlock();
4495         return enabled;
4496 }
4497 EXPORT_SYMBOL_GPL(devlink_dpipe_table_counter_enabled);
4498
4499 /**
4500  *      devlink_dpipe_table_register - register dpipe table
4501  *
4502  *      @devlink: devlink
4503  *      @table_name: table name
4504  *      @table_ops: table ops
4505  *      @priv: priv
4506  *      @counter_control_extern: external control for counters
4507  */
4508 int devlink_dpipe_table_register(struct devlink *devlink,
4509                                  const char *table_name,
4510                                  struct devlink_dpipe_table_ops *table_ops,
4511                                  void *priv, bool counter_control_extern)
4512 {
4513         struct devlink_dpipe_table *table;
4514
4515         if (devlink_dpipe_table_find(&devlink->dpipe_table_list, table_name))
4516                 return -EEXIST;
4517
4518         if (WARN_ON(!table_ops->size_get))
4519                 return -EINVAL;
4520
4521         table = kzalloc(sizeof(*table), GFP_KERNEL);
4522         if (!table)
4523                 return -ENOMEM;
4524
4525         table->name = table_name;
4526         table->table_ops = table_ops;
4527         table->priv = priv;
4528         table->counter_control_extern = counter_control_extern;
4529
4530         mutex_lock(&devlink->lock);
4531         list_add_tail_rcu(&table->list, &devlink->dpipe_table_list);
4532         mutex_unlock(&devlink->lock);
4533         return 0;
4534 }
4535 EXPORT_SYMBOL_GPL(devlink_dpipe_table_register);
4536
4537 /**
4538  *      devlink_dpipe_table_unregister - unregister dpipe table
4539  *
4540  *      @devlink: devlink
4541  *      @table_name: table name
4542  */
4543 void devlink_dpipe_table_unregister(struct devlink *devlink,
4544                                     const char *table_name)
4545 {
4546         struct devlink_dpipe_table *table;
4547
4548         mutex_lock(&devlink->lock);
4549         table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
4550                                          table_name);
4551         if (!table)
4552                 goto unlock;
4553         list_del_rcu(&table->list);
4554         mutex_unlock(&devlink->lock);
4555         kfree_rcu(table, rcu);
4556         return;
4557 unlock:
4558         mutex_unlock(&devlink->lock);
4559 }
4560 EXPORT_SYMBOL_GPL(devlink_dpipe_table_unregister);
4561
4562 /**
4563  *      devlink_resource_register - devlink resource register
4564  *
4565  *      @devlink: devlink
4566  *      @resource_name: resource's name
4567  *      @top_hierarchy: top hierarchy
4568  *      @reload_required: reload is required for new configuration to
4569  *                        apply
4570  *      @resource_size: resource's size
4571  *      @resource_id: resource's id
4572  *      @parent_reosurce_id: resource's parent id
4573  *      @size params: size parameters
4574  */
4575 int devlink_resource_register(struct devlink *devlink,
4576                               const char *resource_name,
4577                               u64 resource_size,
4578                               u64 resource_id,
4579                               u64 parent_resource_id,
4580                               const struct devlink_resource_size_params *size_params)
4581 {
4582         struct devlink_resource *resource;
4583         struct list_head *resource_list;
4584         bool top_hierarchy;
4585         int err = 0;
4586
4587         top_hierarchy = parent_resource_id == DEVLINK_RESOURCE_ID_PARENT_TOP;
4588
4589         mutex_lock(&devlink->lock);
4590         resource = devlink_resource_find(devlink, NULL, resource_id);
4591         if (resource) {
4592                 err = -EINVAL;
4593                 goto out;
4594         }
4595
4596         resource = kzalloc(sizeof(*resource), GFP_KERNEL);
4597         if (!resource) {
4598                 err = -ENOMEM;
4599                 goto out;
4600         }
4601
4602         if (top_hierarchy) {
4603                 resource_list = &devlink->resource_list;
4604         } else {
4605                 struct devlink_resource *parent_resource;
4606
4607                 parent_resource = devlink_resource_find(devlink, NULL,
4608                                                         parent_resource_id);
4609                 if (parent_resource) {
4610                         resource_list = &parent_resource->resource_list;
4611                         resource->parent = parent_resource;
4612                 } else {
4613                         kfree(resource);
4614                         err = -EINVAL;
4615                         goto out;
4616                 }
4617         }
4618
4619         resource->name = resource_name;
4620         resource->size = resource_size;
4621         resource->size_new = resource_size;
4622         resource->id = resource_id;
4623         resource->size_valid = true;
4624         memcpy(&resource->size_params, size_params,
4625                sizeof(resource->size_params));
4626         INIT_LIST_HEAD(&resource->resource_list);
4627         list_add_tail(&resource->list, resource_list);
4628 out:
4629         mutex_unlock(&devlink->lock);
4630         return err;
4631 }
4632 EXPORT_SYMBOL_GPL(devlink_resource_register);
4633
4634 /**
4635  *      devlink_resources_unregister - free all resources
4636  *
4637  *      @devlink: devlink
4638  *      @resource: resource
4639  */
4640 void devlink_resources_unregister(struct devlink *devlink,
4641                                   struct devlink_resource *resource)
4642 {
4643         struct devlink_resource *tmp, *child_resource;
4644         struct list_head *resource_list;
4645
4646         if (resource)
4647                 resource_list = &resource->resource_list;
4648         else
4649                 resource_list = &devlink->resource_list;
4650
4651         if (!resource)
4652                 mutex_lock(&devlink->lock);
4653
4654         list_for_each_entry_safe(child_resource, tmp, resource_list, list) {
4655                 devlink_resources_unregister(devlink, child_resource);
4656                 list_del(&child_resource->list);
4657                 kfree(child_resource);
4658         }
4659
4660         if (!resource)
4661                 mutex_unlock(&devlink->lock);
4662 }
4663 EXPORT_SYMBOL_GPL(devlink_resources_unregister);
4664
4665 /**
4666  *      devlink_resource_size_get - get and update size
4667  *
4668  *      @devlink: devlink
4669  *      @resource_id: the requested resource id
4670  *      @p_resource_size: ptr to update
4671  */
4672 int devlink_resource_size_get(struct devlink *devlink,
4673                               u64 resource_id,
4674                               u64 *p_resource_size)
4675 {
4676         struct devlink_resource *resource;
4677         int err = 0;
4678
4679         mutex_lock(&devlink->lock);
4680         resource = devlink_resource_find(devlink, NULL, resource_id);
4681         if (!resource) {
4682                 err = -EINVAL;
4683                 goto out;
4684         }
4685         *p_resource_size = resource->size_new;
4686         resource->size = resource->size_new;
4687 out:
4688         mutex_unlock(&devlink->lock);
4689         return err;
4690 }
4691 EXPORT_SYMBOL_GPL(devlink_resource_size_get);
4692
4693 /**
4694  *      devlink_dpipe_table_resource_set - set the resource id
4695  *
4696  *      @devlink: devlink
4697  *      @table_name: table name
4698  *      @resource_id: resource id
4699  *      @resource_units: number of resource's units consumed per table's entry
4700  */
4701 int devlink_dpipe_table_resource_set(struct devlink *devlink,
4702                                      const char *table_name, u64 resource_id,
4703                                      u64 resource_units)
4704 {
4705         struct devlink_dpipe_table *table;
4706         int err = 0;
4707
4708         mutex_lock(&devlink->lock);
4709         table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
4710                                          table_name);
4711         if (!table) {
4712                 err = -EINVAL;
4713                 goto out;
4714         }
4715         table->resource_id = resource_id;
4716         table->resource_units = resource_units;
4717         table->resource_valid = true;
4718 out:
4719         mutex_unlock(&devlink->lock);
4720         return err;
4721 }
4722 EXPORT_SYMBOL_GPL(devlink_dpipe_table_resource_set);
4723
4724 /**
4725  *      devlink_resource_occ_get_register - register occupancy getter
4726  *
4727  *      @devlink: devlink
4728  *      @resource_id: resource id
4729  *      @occ_get: occupancy getter callback
4730  *      @occ_get_priv: occupancy getter callback priv
4731  */
4732 void devlink_resource_occ_get_register(struct devlink *devlink,
4733                                        u64 resource_id,
4734                                        devlink_resource_occ_get_t *occ_get,
4735                                        void *occ_get_priv)
4736 {
4737         struct devlink_resource *resource;
4738
4739         mutex_lock(&devlink->lock);
4740         resource = devlink_resource_find(devlink, NULL, resource_id);
4741         if (WARN_ON(!resource))
4742                 goto out;
4743         WARN_ON(resource->occ_get);
4744
4745         resource->occ_get = occ_get;
4746         resource->occ_get_priv = occ_get_priv;
4747 out:
4748         mutex_unlock(&devlink->lock);
4749 }
4750 EXPORT_SYMBOL_GPL(devlink_resource_occ_get_register);
4751
4752 /**
4753  *      devlink_resource_occ_get_unregister - unregister occupancy getter
4754  *
4755  *      @devlink: devlink
4756  *      @resource_id: resource id
4757  */
4758 void devlink_resource_occ_get_unregister(struct devlink *devlink,
4759                                          u64 resource_id)
4760 {
4761         struct devlink_resource *resource;
4762
4763         mutex_lock(&devlink->lock);
4764         resource = devlink_resource_find(devlink, NULL, resource_id);
4765         if (WARN_ON(!resource))
4766                 goto out;
4767         WARN_ON(!resource->occ_get);
4768
4769         resource->occ_get = NULL;
4770         resource->occ_get_priv = NULL;
4771 out:
4772         mutex_unlock(&devlink->lock);
4773 }
4774 EXPORT_SYMBOL_GPL(devlink_resource_occ_get_unregister);
4775
4776 static int devlink_param_verify(const struct devlink_param *param)
4777 {
4778         if (!param || !param->name || !param->supported_cmodes)
4779                 return -EINVAL;
4780         if (param->generic)
4781                 return devlink_param_generic_verify(param);
4782         else
4783                 return devlink_param_driver_verify(param);
4784 }
4785
4786 static int __devlink_params_register(struct devlink *devlink,
4787                                      unsigned int port_index,
4788                                      struct list_head *param_list,
4789                                      const struct devlink_param *params,
4790                                      size_t params_count,
4791                                      enum devlink_command reg_cmd,
4792                                      enum devlink_command unreg_cmd)
4793 {
4794         const struct devlink_param *param = params;
4795         int i;
4796         int err;
4797
4798         mutex_lock(&devlink->lock);
4799         for (i = 0; i < params_count; i++, param++) {
4800                 err = devlink_param_verify(param);
4801                 if (err)
4802                         goto rollback;
4803
4804                 err = devlink_param_register_one(devlink, port_index,
4805                                                  param_list, param, reg_cmd);
4806                 if (err)
4807                         goto rollback;
4808         }
4809
4810         mutex_unlock(&devlink->lock);
4811         return 0;
4812
4813 rollback:
4814         if (!i)
4815                 goto unlock;
4816         for (param--; i > 0; i--, param--)
4817                 devlink_param_unregister_one(devlink, port_index, param_list,
4818                                              param, unreg_cmd);
4819 unlock:
4820         mutex_unlock(&devlink->lock);
4821         return err;
4822 }
4823
4824 static void __devlink_params_unregister(struct devlink *devlink,
4825                                         unsigned int port_index,
4826                                         struct list_head *param_list,
4827                                         const struct devlink_param *params,
4828                                         size_t params_count,
4829                                         enum devlink_command cmd)
4830 {
4831         const struct devlink_param *param = params;
4832         int i;
4833
4834         mutex_lock(&devlink->lock);
4835         for (i = 0; i < params_count; i++, param++)
4836                 devlink_param_unregister_one(devlink, 0, param_list, param,
4837                                              cmd);
4838         mutex_unlock(&devlink->lock);
4839 }
4840
4841 /**
4842  *      devlink_params_register - register configuration parameters
4843  *
4844  *      @devlink: devlink
4845  *      @params: configuration parameters array
4846  *      @params_count: number of parameters provided
4847  *
4848  *      Register the configuration parameters supported by the driver.
4849  */
4850 int devlink_params_register(struct devlink *devlink,
4851                             const struct devlink_param *params,
4852                             size_t params_count)
4853 {
4854         return __devlink_params_register(devlink, 0, &devlink->param_list,
4855                                          params, params_count,
4856                                          DEVLINK_CMD_PARAM_NEW,
4857                                          DEVLINK_CMD_PARAM_DEL);
4858 }
4859 EXPORT_SYMBOL_GPL(devlink_params_register);
4860
4861 /**
4862  *      devlink_params_unregister - unregister configuration parameters
4863  *      @devlink: devlink
4864  *      @params: configuration parameters to unregister
4865  *      @params_count: number of parameters provided
4866  */
4867 void devlink_params_unregister(struct devlink *devlink,
4868                                const struct devlink_param *params,
4869                                size_t params_count)
4870 {
4871         return __devlink_params_unregister(devlink, 0, &devlink->param_list,
4872                                            params, params_count,
4873                                            DEVLINK_CMD_PARAM_DEL);
4874 }
4875 EXPORT_SYMBOL_GPL(devlink_params_unregister);
4876
4877 /**
4878  *      devlink_port_params_register - register port configuration parameters
4879  *
4880  *      @devlink_port: devlink port
4881  *      @params: configuration parameters array
4882  *      @params_count: number of parameters provided
4883  *
4884  *      Register the configuration parameters supported by the port.
4885  */
4886 int devlink_port_params_register(struct devlink_port *devlink_port,
4887                                  const struct devlink_param *params,
4888                                  size_t params_count)
4889 {
4890         return __devlink_params_register(devlink_port->devlink,
4891                                          devlink_port->index,
4892                                          &devlink_port->param_list, params,
4893                                          params_count,
4894                                          DEVLINK_CMD_PORT_PARAM_NEW,
4895                                          DEVLINK_CMD_PORT_PARAM_DEL);
4896 }
4897 EXPORT_SYMBOL_GPL(devlink_port_params_register);
4898
4899 /**
4900  *      devlink_port_params_unregister - unregister port configuration
4901  *      parameters
4902  *
4903  *      @devlink_port: devlink port
4904  *      @params: configuration parameters array
4905  *      @params_count: number of parameters provided
4906  */
4907 void devlink_port_params_unregister(struct devlink_port *devlink_port,
4908                                     const struct devlink_param *params,
4909                                     size_t params_count)
4910 {
4911         return __devlink_params_unregister(devlink_port->devlink,
4912                                            devlink_port->index,
4913                                            &devlink_port->param_list,
4914                                            params, params_count,
4915                                            DEVLINK_CMD_PORT_PARAM_DEL);
4916 }
4917 EXPORT_SYMBOL_GPL(devlink_port_params_unregister);
4918
4919 static int
4920 __devlink_param_driverinit_value_get(struct list_head *param_list, u32 param_id,
4921                                      union devlink_param_value *init_val)
4922 {
4923         struct devlink_param_item *param_item;
4924
4925         param_item = devlink_param_find_by_id(param_list, param_id);
4926         if (!param_item)
4927                 return -EINVAL;
4928
4929         if (!param_item->driverinit_value_valid ||
4930             !devlink_param_cmode_is_supported(param_item->param,
4931                                               DEVLINK_PARAM_CMODE_DRIVERINIT))
4932                 return -EOPNOTSUPP;
4933
4934         if (param_item->param->type == DEVLINK_PARAM_TYPE_STRING)
4935                 strcpy(init_val->vstr, param_item->driverinit_value.vstr);
4936         else
4937                 *init_val = param_item->driverinit_value;
4938
4939         return 0;
4940 }
4941
4942 static int
4943 __devlink_param_driverinit_value_set(struct devlink *devlink,
4944                                      unsigned int port_index,
4945                                      struct list_head *param_list, u32 param_id,
4946                                      union devlink_param_value init_val,
4947                                      enum devlink_command cmd)
4948 {
4949         struct devlink_param_item *param_item;
4950
4951         param_item = devlink_param_find_by_id(param_list, param_id);
4952         if (!param_item)
4953                 return -EINVAL;
4954
4955         if (!devlink_param_cmode_is_supported(param_item->param,
4956                                               DEVLINK_PARAM_CMODE_DRIVERINIT))
4957                 return -EOPNOTSUPP;
4958
4959         if (param_item->param->type == DEVLINK_PARAM_TYPE_STRING)
4960                 strcpy(param_item->driverinit_value.vstr, init_val.vstr);
4961         else
4962                 param_item->driverinit_value = init_val;
4963         param_item->driverinit_value_valid = true;
4964
4965         devlink_param_notify(devlink, port_index, param_item, cmd);
4966         return 0;
4967 }
4968
4969 /**
4970  *      devlink_param_driverinit_value_get - get configuration parameter
4971  *                                           value for driver initializing
4972  *
4973  *      @devlink: devlink
4974  *      @param_id: parameter ID
4975  *      @init_val: value of parameter in driverinit configuration mode
4976  *
4977  *      This function should be used by the driver to get driverinit
4978  *      configuration for initialization after reload command.
4979  */
4980 int devlink_param_driverinit_value_get(struct devlink *devlink, u32 param_id,
4981                                        union devlink_param_value *init_val)
4982 {
4983         if (!devlink->ops || !devlink->ops->reload)
4984                 return -EOPNOTSUPP;
4985
4986         return __devlink_param_driverinit_value_get(&devlink->param_list,
4987                                                     param_id, init_val);
4988 }
4989 EXPORT_SYMBOL_GPL(devlink_param_driverinit_value_get);
4990
4991 /**
4992  *      devlink_param_driverinit_value_set - set value of configuration
4993  *                                           parameter for driverinit
4994  *                                           configuration mode
4995  *
4996  *      @devlink: devlink
4997  *      @param_id: parameter ID
4998  *      @init_val: value of parameter to set for driverinit configuration mode
4999  *
5000  *      This function should be used by the driver to set driverinit
5001  *      configuration mode default value.
5002  */
5003 int devlink_param_driverinit_value_set(struct devlink *devlink, u32 param_id,
5004                                        union devlink_param_value init_val)
5005 {
5006         return __devlink_param_driverinit_value_set(devlink, 0,
5007                                                     &devlink->param_list,
5008                                                     param_id, init_val,
5009                                                     DEVLINK_CMD_PARAM_NEW);
5010 }
5011 EXPORT_SYMBOL_GPL(devlink_param_driverinit_value_set);
5012
5013 /**
5014  *      devlink_port_param_driverinit_value_get - get configuration parameter
5015  *                                              value for driver initializing
5016  *
5017  *      @devlink_port: devlink_port
5018  *      @param_id: parameter ID
5019  *      @init_val: value of parameter in driverinit configuration mode
5020  *
5021  *      This function should be used by the driver to get driverinit
5022  *      configuration for initialization after reload command.
5023  */
5024 int devlink_port_param_driverinit_value_get(struct devlink_port *devlink_port,
5025                                             u32 param_id,
5026                                             union devlink_param_value *init_val)
5027 {
5028         struct devlink *devlink = devlink_port->devlink;
5029
5030         if (!devlink->ops || !devlink->ops->reload)
5031                 return -EOPNOTSUPP;
5032
5033         return __devlink_param_driverinit_value_get(&devlink_port->param_list,
5034                                                     param_id, init_val);
5035 }
5036 EXPORT_SYMBOL_GPL(devlink_port_param_driverinit_value_get);
5037
5038 /**
5039  *     devlink_port_param_driverinit_value_set - set value of configuration
5040  *                                               parameter for driverinit
5041  *                                               configuration mode
5042  *
5043  *     @devlink_port: devlink_port
5044  *     @param_id: parameter ID
5045  *     @init_val: value of parameter to set for driverinit configuration mode
5046  *
5047  *     This function should be used by the driver to set driverinit
5048  *     configuration mode default value.
5049  */
5050 int devlink_port_param_driverinit_value_set(struct devlink_port *devlink_port,
5051                                             u32 param_id,
5052                                             union devlink_param_value init_val)
5053 {
5054         return __devlink_param_driverinit_value_set(devlink_port->devlink,
5055                                                     devlink_port->index,
5056                                                     &devlink_port->param_list,
5057                                                     param_id, init_val,
5058                                                     DEVLINK_CMD_PORT_PARAM_NEW);
5059 }
5060 EXPORT_SYMBOL_GPL(devlink_port_param_driverinit_value_set);
5061
5062 /**
5063  *      devlink_param_value_changed - notify devlink on a parameter's value
5064  *                                    change. Should be called by the driver
5065  *                                    right after the change.
5066  *
5067  *      @devlink: devlink
5068  *      @param_id: parameter ID
5069  *
5070  *      This function should be used by the driver to notify devlink on value
5071  *      change, excluding driverinit configuration mode.
5072  *      For driverinit configuration mode driver should use the function
5073  */
5074 void devlink_param_value_changed(struct devlink *devlink, u32 param_id)
5075 {
5076         struct devlink_param_item *param_item;
5077
5078         param_item = devlink_param_find_by_id(&devlink->param_list, param_id);
5079         WARN_ON(!param_item);
5080
5081         devlink_param_notify(devlink, 0, param_item, DEVLINK_CMD_PARAM_NEW);
5082 }
5083 EXPORT_SYMBOL_GPL(devlink_param_value_changed);
5084
5085 /**
5086  *     devlink_port_param_value_changed - notify devlink on a parameter's value
5087  *                                      change. Should be called by the driver
5088  *                                      right after the change.
5089  *
5090  *     @devlink_port: devlink_port
5091  *     @param_id: parameter ID
5092  *
5093  *     This function should be used by the driver to notify devlink on value
5094  *     change, excluding driverinit configuration mode.
5095  *     For driverinit configuration mode driver should use the function
5096  *     devlink_port_param_driverinit_value_set() instead.
5097  */
5098 void devlink_port_param_value_changed(struct devlink_port *devlink_port,
5099                                       u32 param_id)
5100 {
5101         struct devlink_param_item *param_item;
5102
5103         param_item = devlink_param_find_by_id(&devlink_port->param_list,
5104                                               param_id);
5105         WARN_ON(!param_item);
5106
5107         devlink_param_notify(devlink_port->devlink, devlink_port->index,
5108                              param_item, DEVLINK_CMD_PORT_PARAM_NEW);
5109 }
5110 EXPORT_SYMBOL_GPL(devlink_port_param_value_changed);
5111
5112 /**
5113  *      devlink_param_value_str_fill - Safely fill-up the string preventing
5114  *                                     from overflow of the preallocated buffer
5115  *
5116  *      @dst_val: destination devlink_param_value
5117  *      @src: source buffer
5118  */
5119 void devlink_param_value_str_fill(union devlink_param_value *dst_val,
5120                                   const char *src)
5121 {
5122         size_t len;
5123
5124         len = strlcpy(dst_val->vstr, src, __DEVLINK_PARAM_MAX_STRING_VALUE);
5125         WARN_ON(len >= __DEVLINK_PARAM_MAX_STRING_VALUE);
5126 }
5127 EXPORT_SYMBOL_GPL(devlink_param_value_str_fill);
5128
5129 /**
5130  *      devlink_region_create - create a new address region
5131  *
5132  *      @devlink: devlink
5133  *      @region_name: region name
5134  *      @region_max_snapshots: Maximum supported number of snapshots for region
5135  *      @region_size: size of region
5136  */
5137 struct devlink_region *devlink_region_create(struct devlink *devlink,
5138                                              const char *region_name,
5139                                              u32 region_max_snapshots,
5140                                              u64 region_size)
5141 {
5142         struct devlink_region *region;
5143         int err = 0;
5144
5145         mutex_lock(&devlink->lock);
5146
5147         if (devlink_region_get_by_name(devlink, region_name)) {
5148                 err = -EEXIST;
5149                 goto unlock;
5150         }
5151
5152         region = kzalloc(sizeof(*region), GFP_KERNEL);
5153         if (!region) {
5154                 err = -ENOMEM;
5155                 goto unlock;
5156         }
5157
5158         region->devlink = devlink;
5159         region->max_snapshots = region_max_snapshots;
5160         region->name = region_name;
5161         region->size = region_size;
5162         INIT_LIST_HEAD(&region->snapshot_list);
5163         list_add_tail(&region->list, &devlink->region_list);
5164         devlink_nl_region_notify(region, NULL, DEVLINK_CMD_REGION_NEW);
5165
5166         mutex_unlock(&devlink->lock);
5167         return region;
5168
5169 unlock:
5170         mutex_unlock(&devlink->lock);
5171         return ERR_PTR(err);
5172 }
5173 EXPORT_SYMBOL_GPL(devlink_region_create);
5174
5175 /**
5176  *      devlink_region_destroy - destroy address region
5177  *
5178  *      @region: devlink region to destroy
5179  */
5180 void devlink_region_destroy(struct devlink_region *region)
5181 {
5182         struct devlink *devlink = region->devlink;
5183         struct devlink_snapshot *snapshot, *ts;
5184
5185         mutex_lock(&devlink->lock);
5186
5187         /* Free all snapshots of region */
5188         list_for_each_entry_safe(snapshot, ts, &region->snapshot_list, list)
5189                 devlink_region_snapshot_del(snapshot);
5190
5191         list_del(&region->list);
5192
5193         devlink_nl_region_notify(region, NULL, DEVLINK_CMD_REGION_DEL);
5194         mutex_unlock(&devlink->lock);
5195         kfree(region);
5196 }
5197 EXPORT_SYMBOL_GPL(devlink_region_destroy);
5198
5199 /**
5200  *      devlink_region_shapshot_id_get - get snapshot ID
5201  *
5202  *      This callback should be called when adding a new snapshot,
5203  *      Driver should use the same id for multiple snapshots taken
5204  *      on multiple regions at the same time/by the same trigger.
5205  *
5206  *      @devlink: devlink
5207  */
5208 u32 devlink_region_shapshot_id_get(struct devlink *devlink)
5209 {
5210         u32 id;
5211
5212         mutex_lock(&devlink->lock);
5213         id = ++devlink->snapshot_id;
5214         mutex_unlock(&devlink->lock);
5215
5216         return id;
5217 }
5218 EXPORT_SYMBOL_GPL(devlink_region_shapshot_id_get);
5219
5220 /**
5221  *      devlink_region_snapshot_create - create a new snapshot
5222  *      This will add a new snapshot of a region. The snapshot
5223  *      will be stored on the region struct and can be accessed
5224  *      from devlink. This is useful for future analyses of snapshots.
5225  *      Multiple snapshots can be created on a region.
5226  *      The @snapshot_id should be obtained using the getter function.
5227  *
5228  *      @devlink_region: devlink region of the snapshot
5229  *      @data_len: size of snapshot data
5230  *      @data: snapshot data
5231  *      @snapshot_id: snapshot id to be created
5232  *      @data_destructor: pointer to destructor function to free data
5233  */
5234 int devlink_region_snapshot_create(struct devlink_region *region, u64 data_len,
5235                                    u8 *data, u32 snapshot_id,
5236                                    devlink_snapshot_data_dest_t *data_destructor)
5237 {
5238         struct devlink *devlink = region->devlink;
5239         struct devlink_snapshot *snapshot;
5240         int err;
5241
5242         mutex_lock(&devlink->lock);
5243
5244         /* check if region can hold one more snapshot */
5245         if (region->cur_snapshots == region->max_snapshots) {
5246                 err = -ENOMEM;
5247                 goto unlock;
5248         }
5249
5250         if (devlink_region_snapshot_get_by_id(region, snapshot_id)) {
5251                 err = -EEXIST;
5252                 goto unlock;
5253         }
5254
5255         snapshot = kzalloc(sizeof(*snapshot), GFP_KERNEL);
5256         if (!snapshot) {
5257                 err = -ENOMEM;
5258                 goto unlock;
5259         }
5260
5261         snapshot->id = snapshot_id;
5262         snapshot->region = region;
5263         snapshot->data = data;
5264         snapshot->data_len = data_len;
5265         snapshot->data_destructor = data_destructor;
5266
5267         list_add_tail(&snapshot->list, &region->snapshot_list);
5268
5269         region->cur_snapshots++;
5270
5271         devlink_nl_region_notify(region, snapshot, DEVLINK_CMD_REGION_NEW);
5272         mutex_unlock(&devlink->lock);
5273         return 0;
5274
5275 unlock:
5276         mutex_unlock(&devlink->lock);
5277         return err;
5278 }
5279 EXPORT_SYMBOL_GPL(devlink_region_snapshot_create);
5280
5281 static int __init devlink_module_init(void)
5282 {
5283         return genl_register_family(&devlink_nl_family);
5284 }
5285
5286 static void __exit devlink_module_exit(void)
5287 {
5288         genl_unregister_family(&devlink_nl_family);
5289 }
5290
5291 module_init(devlink_module_init);
5292 module_exit(devlink_module_exit);
5293
5294 MODULE_LICENSE("GPL v2");
5295 MODULE_AUTHOR("Jiri Pirko <jiri@mellanox.com>");
5296 MODULE_DESCRIPTION("Network physical device Netlink interface");
5297 MODULE_ALIAS_GENL_FAMILY(DEVLINK_GENL_NAME);