devlink: Introduce controller number
[linux-2.6-microblaze.git] / include / net / devlink.h
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3  * include/net/devlink.h - Network physical device Netlink interface
4  * Copyright (c) 2016 Mellanox Technologies. All rights reserved.
5  * Copyright (c) 2016 Jiri Pirko <jiri@mellanox.com>
6  */
7 #ifndef _NET_DEVLINK_H_
8 #define _NET_DEVLINK_H_
9
10 #include <linux/device.h>
11 #include <linux/slab.h>
12 #include <linux/gfp.h>
13 #include <linux/list.h>
14 #include <linux/netdevice.h>
15 #include <linux/spinlock.h>
16 #include <linux/workqueue.h>
17 #include <linux/refcount.h>
18 #include <net/net_namespace.h>
19 #include <net/flow_offload.h>
20 #include <uapi/linux/devlink.h>
21 #include <linux/xarray.h>
22
23 struct devlink_ops;
24
25 struct devlink {
26         struct list_head list;
27         struct list_head port_list;
28         struct list_head sb_list;
29         struct list_head dpipe_table_list;
30         struct list_head resource_list;
31         struct list_head param_list;
32         struct list_head region_list;
33         struct list_head reporter_list;
34         struct mutex reporters_lock; /* protects reporter_list */
35         struct devlink_dpipe_headers *dpipe_headers;
36         struct list_head trap_list;
37         struct list_head trap_group_list;
38         struct list_head trap_policer_list;
39         const struct devlink_ops *ops;
40         struct xarray snapshot_ids;
41         struct device *dev;
42         possible_net_t _net;
43         struct mutex lock; /* Serializes access to devlink instance specific objects such as
44                             * port, sb, dpipe, resource, params, region, traps and more.
45                             */
46         u8 reload_failed:1,
47            reload_enabled:1,
48            registered:1;
49         char priv[0] __aligned(NETDEV_ALIGN);
50 };
51
52 struct devlink_port_phys_attrs {
53         u32 port_number; /* Same value as "split group".
54                           * A physical port which is visible to the user
55                           * for a given port flavour.
56                           */
57         u32 split_subport_number; /* If the port is split, this is the number of subport. */
58 };
59
60 /**
61  * struct devlink_port_pci_pf_attrs - devlink port's PCI PF attributes
62  * @controller: Associated controller number
63  * @pf: Associated PCI PF number for this port.
64  * @external: when set, indicates if a port is for an external controller
65  */
66 struct devlink_port_pci_pf_attrs {
67         u32 controller;
68         u16 pf;
69         u8 external:1;
70 };
71
72 /**
73  * struct devlink_port_pci_vf_attrs - devlink port's PCI VF attributes
74  * @controller: Associated controller number
75  * @pf: Associated PCI PF number for this port.
76  * @vf: Associated PCI VF for of the PCI PF for this port.
77  * @external: when set, indicates if a port is for an external controller
78  */
79 struct devlink_port_pci_vf_attrs {
80         u32 controller;
81         u16 pf;
82         u16 vf;
83         u8 external:1;
84 };
85
86 /**
87  * struct devlink_port_attrs - devlink port object
88  * @flavour: flavour of the port
89  * @split: indicates if this is split port
90  * @splittable: indicates if the port can be split.
91  * @lanes: maximum number of lanes the port supports. 0 value is not passed to netlink.
92  * @switch_id: if the port is part of switch, this is buffer with ID, otherwise this is NULL
93  * @phys: physical port attributes
94  * @pci_pf: PCI PF port attributes
95  * @pci_vf: PCI VF port attributes
96  */
97 struct devlink_port_attrs {
98         u8 split:1,
99            splittable:1;
100         u32 lanes;
101         enum devlink_port_flavour flavour;
102         struct netdev_phys_item_id switch_id;
103         union {
104                 struct devlink_port_phys_attrs phys;
105                 struct devlink_port_pci_pf_attrs pci_pf;
106                 struct devlink_port_pci_vf_attrs pci_vf;
107         };
108 };
109
110 struct devlink_port {
111         struct list_head list;
112         struct list_head param_list;
113         struct devlink *devlink;
114         unsigned int index;
115         bool registered;
116         spinlock_t type_lock; /* Protects type and type_dev
117                                * pointer consistency.
118                                */
119         enum devlink_port_type type;
120         enum devlink_port_type desired_type;
121         void *type_dev;
122         struct devlink_port_attrs attrs;
123         u8 attrs_set:1,
124            switch_port:1;
125         struct delayed_work type_warn_dw;
126         struct list_head reporter_list;
127         struct mutex reporters_lock; /* Protects reporter_list */
128 };
129
130 struct devlink_sb_pool_info {
131         enum devlink_sb_pool_type pool_type;
132         u32 size;
133         enum devlink_sb_threshold_type threshold_type;
134         u32 cell_size;
135 };
136
137 /**
138  * struct devlink_dpipe_field - dpipe field object
139  * @name: field name
140  * @id: index inside the headers field array
141  * @bitwidth: bitwidth
142  * @mapping_type: mapping type
143  */
144 struct devlink_dpipe_field {
145         const char *name;
146         unsigned int id;
147         unsigned int bitwidth;
148         enum devlink_dpipe_field_mapping_type mapping_type;
149 };
150
151 /**
152  * struct devlink_dpipe_header - dpipe header object
153  * @name: header name
154  * @id: index, global/local detrmined by global bit
155  * @fields: fields
156  * @fields_count: number of fields
157  * @global: indicates if header is shared like most protocol header
158  *          or driver specific
159  */
160 struct devlink_dpipe_header {
161         const char *name;
162         unsigned int id;
163         struct devlink_dpipe_field *fields;
164         unsigned int fields_count;
165         bool global;
166 };
167
168 /**
169  * struct devlink_dpipe_match - represents match operation
170  * @type: type of match
171  * @header_index: header index (packets can have several headers of same
172  *                type like in case of tunnels)
173  * @header: header
174  * @fieled_id: field index
175  */
176 struct devlink_dpipe_match {
177         enum devlink_dpipe_match_type type;
178         unsigned int header_index;
179         struct devlink_dpipe_header *header;
180         unsigned int field_id;
181 };
182
183 /**
184  * struct devlink_dpipe_action - represents action operation
185  * @type: type of action
186  * @header_index: header index (packets can have several headers of same
187  *                type like in case of tunnels)
188  * @header: header
189  * @fieled_id: field index
190  */
191 struct devlink_dpipe_action {
192         enum devlink_dpipe_action_type type;
193         unsigned int header_index;
194         struct devlink_dpipe_header *header;
195         unsigned int field_id;
196 };
197
198 /**
199  * struct devlink_dpipe_value - represents value of match/action
200  * @action: action
201  * @match: match
202  * @mapping_value: in case the field has some mapping this value
203  *                 specified the mapping value
204  * @mapping_valid: specify if mapping value is valid
205  * @value_size: value size
206  * @value: value
207  * @mask: bit mask
208  */
209 struct devlink_dpipe_value {
210         union {
211                 struct devlink_dpipe_action *action;
212                 struct devlink_dpipe_match *match;
213         };
214         unsigned int mapping_value;
215         bool mapping_valid;
216         unsigned int value_size;
217         void *value;
218         void *mask;
219 };
220
221 /**
222  * struct devlink_dpipe_entry - table entry object
223  * @index: index of the entry in the table
224  * @match_values: match values
225  * @matche_values_count: count of matches tuples
226  * @action_values: actions values
227  * @action_values_count: count of actions values
228  * @counter: value of counter
229  * @counter_valid: Specify if value is valid from hardware
230  */
231 struct devlink_dpipe_entry {
232         u64 index;
233         struct devlink_dpipe_value *match_values;
234         unsigned int match_values_count;
235         struct devlink_dpipe_value *action_values;
236         unsigned int action_values_count;
237         u64 counter;
238         bool counter_valid;
239 };
240
241 /**
242  * struct devlink_dpipe_dump_ctx - context provided to driver in order
243  *                                 to dump
244  * @info: info
245  * @cmd: devlink command
246  * @skb: skb
247  * @nest: top attribute
248  * @hdr: hdr
249  */
250 struct devlink_dpipe_dump_ctx {
251         struct genl_info *info;
252         enum devlink_command cmd;
253         struct sk_buff *skb;
254         struct nlattr *nest;
255         void *hdr;
256 };
257
258 struct devlink_dpipe_table_ops;
259
260 /**
261  * struct devlink_dpipe_table - table object
262  * @priv: private
263  * @name: table name
264  * @counters_enabled: indicates if counters are active
265  * @counter_control_extern: indicates if counter control is in dpipe or
266  *                          external tool
267  * @resource_valid: Indicate that the resource id is valid
268  * @resource_id: relative resource this table is related to
269  * @resource_units: number of resource's unit consumed per table's entry
270  * @table_ops: table operations
271  * @rcu: rcu
272  */
273 struct devlink_dpipe_table {
274         void *priv;
275         struct list_head list;
276         const char *name;
277         bool counters_enabled;
278         bool counter_control_extern;
279         bool resource_valid;
280         u64 resource_id;
281         u64 resource_units;
282         struct devlink_dpipe_table_ops *table_ops;
283         struct rcu_head rcu;
284 };
285
286 /**
287  * struct devlink_dpipe_table_ops - dpipe_table ops
288  * @actions_dump - dumps all tables actions
289  * @matches_dump - dumps all tables matches
290  * @entries_dump - dumps all active entries in the table
291  * @counters_set_update - when changing the counter status hardware sync
292  *                        maybe needed to allocate/free counter related
293  *                        resources
294  * @size_get - get size
295  */
296 struct devlink_dpipe_table_ops {
297         int (*actions_dump)(void *priv, struct sk_buff *skb);
298         int (*matches_dump)(void *priv, struct sk_buff *skb);
299         int (*entries_dump)(void *priv, bool counters_enabled,
300                             struct devlink_dpipe_dump_ctx *dump_ctx);
301         int (*counters_set_update)(void *priv, bool enable);
302         u64 (*size_get)(void *priv);
303 };
304
305 /**
306  * struct devlink_dpipe_headers - dpipe headers
307  * @headers - header array can be shared (global bit) or driver specific
308  * @headers_count - count of headers
309  */
310 struct devlink_dpipe_headers {
311         struct devlink_dpipe_header **headers;
312         unsigned int headers_count;
313 };
314
315 /**
316  * struct devlink_resource_size_params - resource's size parameters
317  * @size_min: minimum size which can be set
318  * @size_max: maximum size which can be set
319  * @size_granularity: size granularity
320  * @size_unit: resource's basic unit
321  */
322 struct devlink_resource_size_params {
323         u64 size_min;
324         u64 size_max;
325         u64 size_granularity;
326         enum devlink_resource_unit unit;
327 };
328
329 static inline void
330 devlink_resource_size_params_init(struct devlink_resource_size_params *size_params,
331                                   u64 size_min, u64 size_max,
332                                   u64 size_granularity,
333                                   enum devlink_resource_unit unit)
334 {
335         size_params->size_min = size_min;
336         size_params->size_max = size_max;
337         size_params->size_granularity = size_granularity;
338         size_params->unit = unit;
339 }
340
341 typedef u64 devlink_resource_occ_get_t(void *priv);
342
343 /**
344  * struct devlink_resource - devlink resource
345  * @name: name of the resource
346  * @id: id, per devlink instance
347  * @size: size of the resource
348  * @size_new: updated size of the resource, reload is needed
349  * @size_valid: valid in case the total size of the resource is valid
350  *              including its children
351  * @parent: parent resource
352  * @size_params: size parameters
353  * @list: parent list
354  * @resource_list: list of child resources
355  */
356 struct devlink_resource {
357         const char *name;
358         u64 id;
359         u64 size;
360         u64 size_new;
361         bool size_valid;
362         struct devlink_resource *parent;
363         struct devlink_resource_size_params size_params;
364         struct list_head list;
365         struct list_head resource_list;
366         devlink_resource_occ_get_t *occ_get;
367         void *occ_get_priv;
368 };
369
370 #define DEVLINK_RESOURCE_ID_PARENT_TOP 0
371
372 #define __DEVLINK_PARAM_MAX_STRING_VALUE 32
373 enum devlink_param_type {
374         DEVLINK_PARAM_TYPE_U8,
375         DEVLINK_PARAM_TYPE_U16,
376         DEVLINK_PARAM_TYPE_U32,
377         DEVLINK_PARAM_TYPE_STRING,
378         DEVLINK_PARAM_TYPE_BOOL,
379 };
380
381 union devlink_param_value {
382         u8 vu8;
383         u16 vu16;
384         u32 vu32;
385         char vstr[__DEVLINK_PARAM_MAX_STRING_VALUE];
386         bool vbool;
387 };
388
389 struct devlink_param_gset_ctx {
390         union devlink_param_value val;
391         enum devlink_param_cmode cmode;
392 };
393
394 /**
395  * struct devlink_param - devlink configuration parameter data
396  * @name: name of the parameter
397  * @generic: indicates if the parameter is generic or driver specific
398  * @type: parameter type
399  * @supported_cmodes: bitmap of supported configuration modes
400  * @get: get parameter value, used for runtime and permanent
401  *       configuration modes
402  * @set: set parameter value, used for runtime and permanent
403  *       configuration modes
404  * @validate: validate input value is applicable (within value range, etc.)
405  *
406  * This struct should be used by the driver to fill the data for
407  * a parameter it registers.
408  */
409 struct devlink_param {
410         u32 id;
411         const char *name;
412         bool generic;
413         enum devlink_param_type type;
414         unsigned long supported_cmodes;
415         int (*get)(struct devlink *devlink, u32 id,
416                    struct devlink_param_gset_ctx *ctx);
417         int (*set)(struct devlink *devlink, u32 id,
418                    struct devlink_param_gset_ctx *ctx);
419         int (*validate)(struct devlink *devlink, u32 id,
420                         union devlink_param_value val,
421                         struct netlink_ext_ack *extack);
422 };
423
424 struct devlink_param_item {
425         struct list_head list;
426         const struct devlink_param *param;
427         union devlink_param_value driverinit_value;
428         bool driverinit_value_valid;
429         bool published;
430 };
431
432 enum devlink_param_generic_id {
433         DEVLINK_PARAM_GENERIC_ID_INT_ERR_RESET,
434         DEVLINK_PARAM_GENERIC_ID_MAX_MACS,
435         DEVLINK_PARAM_GENERIC_ID_ENABLE_SRIOV,
436         DEVLINK_PARAM_GENERIC_ID_REGION_SNAPSHOT,
437         DEVLINK_PARAM_GENERIC_ID_IGNORE_ARI,
438         DEVLINK_PARAM_GENERIC_ID_MSIX_VEC_PER_PF_MAX,
439         DEVLINK_PARAM_GENERIC_ID_MSIX_VEC_PER_PF_MIN,
440         DEVLINK_PARAM_GENERIC_ID_FW_LOAD_POLICY,
441         DEVLINK_PARAM_GENERIC_ID_RESET_DEV_ON_DRV_PROBE,
442         DEVLINK_PARAM_GENERIC_ID_ENABLE_ROCE,
443
444         /* add new param generic ids above here*/
445         __DEVLINK_PARAM_GENERIC_ID_MAX,
446         DEVLINK_PARAM_GENERIC_ID_MAX = __DEVLINK_PARAM_GENERIC_ID_MAX - 1,
447 };
448
449 #define DEVLINK_PARAM_GENERIC_INT_ERR_RESET_NAME "internal_error_reset"
450 #define DEVLINK_PARAM_GENERIC_INT_ERR_RESET_TYPE DEVLINK_PARAM_TYPE_BOOL
451
452 #define DEVLINK_PARAM_GENERIC_MAX_MACS_NAME "max_macs"
453 #define DEVLINK_PARAM_GENERIC_MAX_MACS_TYPE DEVLINK_PARAM_TYPE_U32
454
455 #define DEVLINK_PARAM_GENERIC_ENABLE_SRIOV_NAME "enable_sriov"
456 #define DEVLINK_PARAM_GENERIC_ENABLE_SRIOV_TYPE DEVLINK_PARAM_TYPE_BOOL
457
458 #define DEVLINK_PARAM_GENERIC_REGION_SNAPSHOT_NAME "region_snapshot_enable"
459 #define DEVLINK_PARAM_GENERIC_REGION_SNAPSHOT_TYPE DEVLINK_PARAM_TYPE_BOOL
460
461 #define DEVLINK_PARAM_GENERIC_IGNORE_ARI_NAME "ignore_ari"
462 #define DEVLINK_PARAM_GENERIC_IGNORE_ARI_TYPE DEVLINK_PARAM_TYPE_BOOL
463
464 #define DEVLINK_PARAM_GENERIC_MSIX_VEC_PER_PF_MAX_NAME "msix_vec_per_pf_max"
465 #define DEVLINK_PARAM_GENERIC_MSIX_VEC_PER_PF_MAX_TYPE DEVLINK_PARAM_TYPE_U32
466
467 #define DEVLINK_PARAM_GENERIC_MSIX_VEC_PER_PF_MIN_NAME "msix_vec_per_pf_min"
468 #define DEVLINK_PARAM_GENERIC_MSIX_VEC_PER_PF_MIN_TYPE DEVLINK_PARAM_TYPE_U32
469
470 #define DEVLINK_PARAM_GENERIC_FW_LOAD_POLICY_NAME "fw_load_policy"
471 #define DEVLINK_PARAM_GENERIC_FW_LOAD_POLICY_TYPE DEVLINK_PARAM_TYPE_U8
472
473 #define DEVLINK_PARAM_GENERIC_RESET_DEV_ON_DRV_PROBE_NAME \
474         "reset_dev_on_drv_probe"
475 #define DEVLINK_PARAM_GENERIC_RESET_DEV_ON_DRV_PROBE_TYPE DEVLINK_PARAM_TYPE_U8
476
477 #define DEVLINK_PARAM_GENERIC_ENABLE_ROCE_NAME "enable_roce"
478 #define DEVLINK_PARAM_GENERIC_ENABLE_ROCE_TYPE DEVLINK_PARAM_TYPE_BOOL
479
480 #define DEVLINK_PARAM_GENERIC(_id, _cmodes, _get, _set, _validate)      \
481 {                                                                       \
482         .id = DEVLINK_PARAM_GENERIC_ID_##_id,                           \
483         .name = DEVLINK_PARAM_GENERIC_##_id##_NAME,                     \
484         .type = DEVLINK_PARAM_GENERIC_##_id##_TYPE,                     \
485         .generic = true,                                                \
486         .supported_cmodes = _cmodes,                                    \
487         .get = _get,                                                    \
488         .set = _set,                                                    \
489         .validate = _validate,                                          \
490 }
491
492 #define DEVLINK_PARAM_DRIVER(_id, _name, _type, _cmodes, _get, _set, _validate) \
493 {                                                                       \
494         .id = _id,                                                      \
495         .name = _name,                                                  \
496         .type = _type,                                                  \
497         .supported_cmodes = _cmodes,                                    \
498         .get = _get,                                                    \
499         .set = _set,                                                    \
500         .validate = _validate,                                          \
501 }
502
503 /* Part number, identifier of board design */
504 #define DEVLINK_INFO_VERSION_GENERIC_BOARD_ID   "board.id"
505 /* Revision of board design */
506 #define DEVLINK_INFO_VERSION_GENERIC_BOARD_REV  "board.rev"
507 /* Maker of the board */
508 #define DEVLINK_INFO_VERSION_GENERIC_BOARD_MANUFACTURE  "board.manufacture"
509
510 /* Part number, identifier of asic design */
511 #define DEVLINK_INFO_VERSION_GENERIC_ASIC_ID    "asic.id"
512 /* Revision of asic design */
513 #define DEVLINK_INFO_VERSION_GENERIC_ASIC_REV   "asic.rev"
514
515 /* Overall FW version */
516 #define DEVLINK_INFO_VERSION_GENERIC_FW         "fw"
517 /* Control processor FW version */
518 #define DEVLINK_INFO_VERSION_GENERIC_FW_MGMT    "fw.mgmt"
519 /* FW interface specification version */
520 #define DEVLINK_INFO_VERSION_GENERIC_FW_MGMT_API        "fw.mgmt.api"
521 /* Data path microcode controlling high-speed packet processing */
522 #define DEVLINK_INFO_VERSION_GENERIC_FW_APP     "fw.app"
523 /* UNDI software version */
524 #define DEVLINK_INFO_VERSION_GENERIC_FW_UNDI    "fw.undi"
525 /* NCSI support/handler version */
526 #define DEVLINK_INFO_VERSION_GENERIC_FW_NCSI    "fw.ncsi"
527 /* FW parameter set id */
528 #define DEVLINK_INFO_VERSION_GENERIC_FW_PSID    "fw.psid"
529 /* RoCE FW version */
530 #define DEVLINK_INFO_VERSION_GENERIC_FW_ROCE    "fw.roce"
531 /* Firmware bundle identifier */
532 #define DEVLINK_INFO_VERSION_GENERIC_FW_BUNDLE_ID       "fw.bundle_id"
533
534 struct devlink_region;
535 struct devlink_info_req;
536
537 /**
538  * struct devlink_region_ops - Region operations
539  * @name: region name
540  * @destructor: callback used to free snapshot memory when deleting
541  * @snapshot: callback to request an immediate snapshot. On success,
542  *            the data variable must be updated to point to the snapshot data.
543  *            The function will be called while the devlink instance lock is
544  *            held.
545  */
546 struct devlink_region_ops {
547         const char *name;
548         void (*destructor)(const void *data);
549         int (*snapshot)(struct devlink *devlink, struct netlink_ext_ack *extack,
550                         u8 **data);
551 };
552
553 struct devlink_fmsg;
554 struct devlink_health_reporter;
555
556 enum devlink_health_reporter_state {
557         DEVLINK_HEALTH_REPORTER_STATE_HEALTHY,
558         DEVLINK_HEALTH_REPORTER_STATE_ERROR,
559 };
560
561 /**
562  * struct devlink_health_reporter_ops - Reporter operations
563  * @name: reporter name
564  * @recover: callback to recover from reported error
565  *           if priv_ctx is NULL, run a full recover
566  * @dump: callback to dump an object
567  *        if priv_ctx is NULL, run a full dump
568  * @diagnose: callback to diagnose the current status
569  */
570
571 struct devlink_health_reporter_ops {
572         char *name;
573         int (*recover)(struct devlink_health_reporter *reporter,
574                        void *priv_ctx, struct netlink_ext_ack *extack);
575         int (*dump)(struct devlink_health_reporter *reporter,
576                     struct devlink_fmsg *fmsg, void *priv_ctx,
577                     struct netlink_ext_ack *extack);
578         int (*diagnose)(struct devlink_health_reporter *reporter,
579                         struct devlink_fmsg *fmsg,
580                         struct netlink_ext_ack *extack);
581 };
582
583 /**
584  * struct devlink_trap_policer - Immutable packet trap policer attributes.
585  * @id: Policer identifier.
586  * @init_rate: Initial rate in packets / sec.
587  * @init_burst: Initial burst size in packets.
588  * @max_rate: Maximum rate.
589  * @min_rate: Minimum rate.
590  * @max_burst: Maximum burst size.
591  * @min_burst: Minimum burst size.
592  *
593  * Describes immutable attributes of packet trap policers that drivers register
594  * with devlink.
595  */
596 struct devlink_trap_policer {
597         u32 id;
598         u64 init_rate;
599         u64 init_burst;
600         u64 max_rate;
601         u64 min_rate;
602         u64 max_burst;
603         u64 min_burst;
604 };
605
606 /**
607  * struct devlink_trap_group - Immutable packet trap group attributes.
608  * @name: Trap group name.
609  * @id: Trap group identifier.
610  * @generic: Whether the trap group is generic or not.
611  * @init_policer_id: Initial policer identifier.
612  *
613  * Describes immutable attributes of packet trap groups that drivers register
614  * with devlink.
615  */
616 struct devlink_trap_group {
617         const char *name;
618         u16 id;
619         bool generic;
620         u32 init_policer_id;
621 };
622
623 #define DEVLINK_TRAP_METADATA_TYPE_F_IN_PORT    BIT(0)
624 #define DEVLINK_TRAP_METADATA_TYPE_F_FA_COOKIE  BIT(1)
625
626 /**
627  * struct devlink_trap - Immutable packet trap attributes.
628  * @type: Trap type.
629  * @init_action: Initial trap action.
630  * @generic: Whether the trap is generic or not.
631  * @id: Trap identifier.
632  * @name: Trap name.
633  * @init_group_id: Initial group identifier.
634  * @metadata_cap: Metadata types that can be provided by the trap.
635  *
636  * Describes immutable attributes of packet traps that drivers register with
637  * devlink.
638  */
639 struct devlink_trap {
640         enum devlink_trap_type type;
641         enum devlink_trap_action init_action;
642         bool generic;
643         u16 id;
644         const char *name;
645         u16 init_group_id;
646         u32 metadata_cap;
647 };
648
649 /* All traps must be documented in
650  * Documentation/networking/devlink/devlink-trap.rst
651  */
652 enum devlink_trap_generic_id {
653         DEVLINK_TRAP_GENERIC_ID_SMAC_MC,
654         DEVLINK_TRAP_GENERIC_ID_VLAN_TAG_MISMATCH,
655         DEVLINK_TRAP_GENERIC_ID_INGRESS_VLAN_FILTER,
656         DEVLINK_TRAP_GENERIC_ID_INGRESS_STP_FILTER,
657         DEVLINK_TRAP_GENERIC_ID_EMPTY_TX_LIST,
658         DEVLINK_TRAP_GENERIC_ID_PORT_LOOPBACK_FILTER,
659         DEVLINK_TRAP_GENERIC_ID_BLACKHOLE_ROUTE,
660         DEVLINK_TRAP_GENERIC_ID_TTL_ERROR,
661         DEVLINK_TRAP_GENERIC_ID_TAIL_DROP,
662         DEVLINK_TRAP_GENERIC_ID_NON_IP_PACKET,
663         DEVLINK_TRAP_GENERIC_ID_UC_DIP_MC_DMAC,
664         DEVLINK_TRAP_GENERIC_ID_DIP_LB,
665         DEVLINK_TRAP_GENERIC_ID_SIP_MC,
666         DEVLINK_TRAP_GENERIC_ID_SIP_LB,
667         DEVLINK_TRAP_GENERIC_ID_CORRUPTED_IP_HDR,
668         DEVLINK_TRAP_GENERIC_ID_IPV4_SIP_BC,
669         DEVLINK_TRAP_GENERIC_ID_IPV6_MC_DIP_RESERVED_SCOPE,
670         DEVLINK_TRAP_GENERIC_ID_IPV6_MC_DIP_INTERFACE_LOCAL_SCOPE,
671         DEVLINK_TRAP_GENERIC_ID_MTU_ERROR,
672         DEVLINK_TRAP_GENERIC_ID_UNRESOLVED_NEIGH,
673         DEVLINK_TRAP_GENERIC_ID_RPF,
674         DEVLINK_TRAP_GENERIC_ID_REJECT_ROUTE,
675         DEVLINK_TRAP_GENERIC_ID_IPV4_LPM_UNICAST_MISS,
676         DEVLINK_TRAP_GENERIC_ID_IPV6_LPM_UNICAST_MISS,
677         DEVLINK_TRAP_GENERIC_ID_NON_ROUTABLE,
678         DEVLINK_TRAP_GENERIC_ID_DECAP_ERROR,
679         DEVLINK_TRAP_GENERIC_ID_OVERLAY_SMAC_MC,
680         DEVLINK_TRAP_GENERIC_ID_INGRESS_FLOW_ACTION_DROP,
681         DEVLINK_TRAP_GENERIC_ID_EGRESS_FLOW_ACTION_DROP,
682         DEVLINK_TRAP_GENERIC_ID_STP,
683         DEVLINK_TRAP_GENERIC_ID_LACP,
684         DEVLINK_TRAP_GENERIC_ID_LLDP,
685         DEVLINK_TRAP_GENERIC_ID_IGMP_QUERY,
686         DEVLINK_TRAP_GENERIC_ID_IGMP_V1_REPORT,
687         DEVLINK_TRAP_GENERIC_ID_IGMP_V2_REPORT,
688         DEVLINK_TRAP_GENERIC_ID_IGMP_V3_REPORT,
689         DEVLINK_TRAP_GENERIC_ID_IGMP_V2_LEAVE,
690         DEVLINK_TRAP_GENERIC_ID_MLD_QUERY,
691         DEVLINK_TRAP_GENERIC_ID_MLD_V1_REPORT,
692         DEVLINK_TRAP_GENERIC_ID_MLD_V2_REPORT,
693         DEVLINK_TRAP_GENERIC_ID_MLD_V1_DONE,
694         DEVLINK_TRAP_GENERIC_ID_IPV4_DHCP,
695         DEVLINK_TRAP_GENERIC_ID_IPV6_DHCP,
696         DEVLINK_TRAP_GENERIC_ID_ARP_REQUEST,
697         DEVLINK_TRAP_GENERIC_ID_ARP_RESPONSE,
698         DEVLINK_TRAP_GENERIC_ID_ARP_OVERLAY,
699         DEVLINK_TRAP_GENERIC_ID_IPV6_NEIGH_SOLICIT,
700         DEVLINK_TRAP_GENERIC_ID_IPV6_NEIGH_ADVERT,
701         DEVLINK_TRAP_GENERIC_ID_IPV4_BFD,
702         DEVLINK_TRAP_GENERIC_ID_IPV6_BFD,
703         DEVLINK_TRAP_GENERIC_ID_IPV4_OSPF,
704         DEVLINK_TRAP_GENERIC_ID_IPV6_OSPF,
705         DEVLINK_TRAP_GENERIC_ID_IPV4_BGP,
706         DEVLINK_TRAP_GENERIC_ID_IPV6_BGP,
707         DEVLINK_TRAP_GENERIC_ID_IPV4_VRRP,
708         DEVLINK_TRAP_GENERIC_ID_IPV6_VRRP,
709         DEVLINK_TRAP_GENERIC_ID_IPV4_PIM,
710         DEVLINK_TRAP_GENERIC_ID_IPV6_PIM,
711         DEVLINK_TRAP_GENERIC_ID_UC_LB,
712         DEVLINK_TRAP_GENERIC_ID_LOCAL_ROUTE,
713         DEVLINK_TRAP_GENERIC_ID_EXTERNAL_ROUTE,
714         DEVLINK_TRAP_GENERIC_ID_IPV6_UC_DIP_LINK_LOCAL_SCOPE,
715         DEVLINK_TRAP_GENERIC_ID_IPV6_DIP_ALL_NODES,
716         DEVLINK_TRAP_GENERIC_ID_IPV6_DIP_ALL_ROUTERS,
717         DEVLINK_TRAP_GENERIC_ID_IPV6_ROUTER_SOLICIT,
718         DEVLINK_TRAP_GENERIC_ID_IPV6_ROUTER_ADVERT,
719         DEVLINK_TRAP_GENERIC_ID_IPV6_REDIRECT,
720         DEVLINK_TRAP_GENERIC_ID_IPV4_ROUTER_ALERT,
721         DEVLINK_TRAP_GENERIC_ID_IPV6_ROUTER_ALERT,
722         DEVLINK_TRAP_GENERIC_ID_PTP_EVENT,
723         DEVLINK_TRAP_GENERIC_ID_PTP_GENERAL,
724         DEVLINK_TRAP_GENERIC_ID_FLOW_ACTION_SAMPLE,
725         DEVLINK_TRAP_GENERIC_ID_FLOW_ACTION_TRAP,
726         DEVLINK_TRAP_GENERIC_ID_EARLY_DROP,
727
728         /* Add new generic trap IDs above */
729         __DEVLINK_TRAP_GENERIC_ID_MAX,
730         DEVLINK_TRAP_GENERIC_ID_MAX = __DEVLINK_TRAP_GENERIC_ID_MAX - 1,
731 };
732
733 /* All trap groups must be documented in
734  * Documentation/networking/devlink/devlink-trap.rst
735  */
736 enum devlink_trap_group_generic_id {
737         DEVLINK_TRAP_GROUP_GENERIC_ID_L2_DROPS,
738         DEVLINK_TRAP_GROUP_GENERIC_ID_L3_DROPS,
739         DEVLINK_TRAP_GROUP_GENERIC_ID_L3_EXCEPTIONS,
740         DEVLINK_TRAP_GROUP_GENERIC_ID_BUFFER_DROPS,
741         DEVLINK_TRAP_GROUP_GENERIC_ID_TUNNEL_DROPS,
742         DEVLINK_TRAP_GROUP_GENERIC_ID_ACL_DROPS,
743         DEVLINK_TRAP_GROUP_GENERIC_ID_STP,
744         DEVLINK_TRAP_GROUP_GENERIC_ID_LACP,
745         DEVLINK_TRAP_GROUP_GENERIC_ID_LLDP,
746         DEVLINK_TRAP_GROUP_GENERIC_ID_MC_SNOOPING,
747         DEVLINK_TRAP_GROUP_GENERIC_ID_DHCP,
748         DEVLINK_TRAP_GROUP_GENERIC_ID_NEIGH_DISCOVERY,
749         DEVLINK_TRAP_GROUP_GENERIC_ID_BFD,
750         DEVLINK_TRAP_GROUP_GENERIC_ID_OSPF,
751         DEVLINK_TRAP_GROUP_GENERIC_ID_BGP,
752         DEVLINK_TRAP_GROUP_GENERIC_ID_VRRP,
753         DEVLINK_TRAP_GROUP_GENERIC_ID_PIM,
754         DEVLINK_TRAP_GROUP_GENERIC_ID_UC_LB,
755         DEVLINK_TRAP_GROUP_GENERIC_ID_LOCAL_DELIVERY,
756         DEVLINK_TRAP_GROUP_GENERIC_ID_EXTERNAL_DELIVERY,
757         DEVLINK_TRAP_GROUP_GENERIC_ID_IPV6,
758         DEVLINK_TRAP_GROUP_GENERIC_ID_PTP_EVENT,
759         DEVLINK_TRAP_GROUP_GENERIC_ID_PTP_GENERAL,
760         DEVLINK_TRAP_GROUP_GENERIC_ID_ACL_SAMPLE,
761         DEVLINK_TRAP_GROUP_GENERIC_ID_ACL_TRAP,
762
763         /* Add new generic trap group IDs above */
764         __DEVLINK_TRAP_GROUP_GENERIC_ID_MAX,
765         DEVLINK_TRAP_GROUP_GENERIC_ID_MAX =
766                 __DEVLINK_TRAP_GROUP_GENERIC_ID_MAX - 1,
767 };
768
769 #define DEVLINK_TRAP_GENERIC_NAME_SMAC_MC \
770         "source_mac_is_multicast"
771 #define DEVLINK_TRAP_GENERIC_NAME_VLAN_TAG_MISMATCH \
772         "vlan_tag_mismatch"
773 #define DEVLINK_TRAP_GENERIC_NAME_INGRESS_VLAN_FILTER \
774         "ingress_vlan_filter"
775 #define DEVLINK_TRAP_GENERIC_NAME_INGRESS_STP_FILTER \
776         "ingress_spanning_tree_filter"
777 #define DEVLINK_TRAP_GENERIC_NAME_EMPTY_TX_LIST \
778         "port_list_is_empty"
779 #define DEVLINK_TRAP_GENERIC_NAME_PORT_LOOPBACK_FILTER \
780         "port_loopback_filter"
781 #define DEVLINK_TRAP_GENERIC_NAME_BLACKHOLE_ROUTE \
782         "blackhole_route"
783 #define DEVLINK_TRAP_GENERIC_NAME_TTL_ERROR \
784         "ttl_value_is_too_small"
785 #define DEVLINK_TRAP_GENERIC_NAME_TAIL_DROP \
786         "tail_drop"
787 #define DEVLINK_TRAP_GENERIC_NAME_NON_IP_PACKET \
788         "non_ip"
789 #define DEVLINK_TRAP_GENERIC_NAME_UC_DIP_MC_DMAC \
790         "uc_dip_over_mc_dmac"
791 #define DEVLINK_TRAP_GENERIC_NAME_DIP_LB \
792         "dip_is_loopback_address"
793 #define DEVLINK_TRAP_GENERIC_NAME_SIP_MC \
794         "sip_is_mc"
795 #define DEVLINK_TRAP_GENERIC_NAME_SIP_LB \
796         "sip_is_loopback_address"
797 #define DEVLINK_TRAP_GENERIC_NAME_CORRUPTED_IP_HDR \
798         "ip_header_corrupted"
799 #define DEVLINK_TRAP_GENERIC_NAME_IPV4_SIP_BC \
800         "ipv4_sip_is_limited_bc"
801 #define DEVLINK_TRAP_GENERIC_NAME_IPV6_MC_DIP_RESERVED_SCOPE \
802         "ipv6_mc_dip_reserved_scope"
803 #define DEVLINK_TRAP_GENERIC_NAME_IPV6_MC_DIP_INTERFACE_LOCAL_SCOPE \
804         "ipv6_mc_dip_interface_local_scope"
805 #define DEVLINK_TRAP_GENERIC_NAME_MTU_ERROR \
806         "mtu_value_is_too_small"
807 #define DEVLINK_TRAP_GENERIC_NAME_UNRESOLVED_NEIGH \
808         "unresolved_neigh"
809 #define DEVLINK_TRAP_GENERIC_NAME_RPF \
810         "mc_reverse_path_forwarding"
811 #define DEVLINK_TRAP_GENERIC_NAME_REJECT_ROUTE \
812         "reject_route"
813 #define DEVLINK_TRAP_GENERIC_NAME_IPV4_LPM_UNICAST_MISS \
814         "ipv4_lpm_miss"
815 #define DEVLINK_TRAP_GENERIC_NAME_IPV6_LPM_UNICAST_MISS \
816         "ipv6_lpm_miss"
817 #define DEVLINK_TRAP_GENERIC_NAME_NON_ROUTABLE \
818         "non_routable_packet"
819 #define DEVLINK_TRAP_GENERIC_NAME_DECAP_ERROR \
820         "decap_error"
821 #define DEVLINK_TRAP_GENERIC_NAME_OVERLAY_SMAC_MC \
822         "overlay_smac_is_mc"
823 #define DEVLINK_TRAP_GENERIC_NAME_INGRESS_FLOW_ACTION_DROP \
824         "ingress_flow_action_drop"
825 #define DEVLINK_TRAP_GENERIC_NAME_EGRESS_FLOW_ACTION_DROP \
826         "egress_flow_action_drop"
827 #define DEVLINK_TRAP_GENERIC_NAME_STP \
828         "stp"
829 #define DEVLINK_TRAP_GENERIC_NAME_LACP \
830         "lacp"
831 #define DEVLINK_TRAP_GENERIC_NAME_LLDP \
832         "lldp"
833 #define DEVLINK_TRAP_GENERIC_NAME_IGMP_QUERY \
834         "igmp_query"
835 #define DEVLINK_TRAP_GENERIC_NAME_IGMP_V1_REPORT \
836         "igmp_v1_report"
837 #define DEVLINK_TRAP_GENERIC_NAME_IGMP_V2_REPORT \
838         "igmp_v2_report"
839 #define DEVLINK_TRAP_GENERIC_NAME_IGMP_V3_REPORT \
840         "igmp_v3_report"
841 #define DEVLINK_TRAP_GENERIC_NAME_IGMP_V2_LEAVE \
842         "igmp_v2_leave"
843 #define DEVLINK_TRAP_GENERIC_NAME_MLD_QUERY \
844         "mld_query"
845 #define DEVLINK_TRAP_GENERIC_NAME_MLD_V1_REPORT \
846         "mld_v1_report"
847 #define DEVLINK_TRAP_GENERIC_NAME_MLD_V2_REPORT \
848         "mld_v2_report"
849 #define DEVLINK_TRAP_GENERIC_NAME_MLD_V1_DONE \
850         "mld_v1_done"
851 #define DEVLINK_TRAP_GENERIC_NAME_IPV4_DHCP \
852         "ipv4_dhcp"
853 #define DEVLINK_TRAP_GENERIC_NAME_IPV6_DHCP \
854         "ipv6_dhcp"
855 #define DEVLINK_TRAP_GENERIC_NAME_ARP_REQUEST \
856         "arp_request"
857 #define DEVLINK_TRAP_GENERIC_NAME_ARP_RESPONSE \
858         "arp_response"
859 #define DEVLINK_TRAP_GENERIC_NAME_ARP_OVERLAY \
860         "arp_overlay"
861 #define DEVLINK_TRAP_GENERIC_NAME_IPV6_NEIGH_SOLICIT \
862         "ipv6_neigh_solicit"
863 #define DEVLINK_TRAP_GENERIC_NAME_IPV6_NEIGH_ADVERT \
864         "ipv6_neigh_advert"
865 #define DEVLINK_TRAP_GENERIC_NAME_IPV4_BFD \
866         "ipv4_bfd"
867 #define DEVLINK_TRAP_GENERIC_NAME_IPV6_BFD \
868         "ipv6_bfd"
869 #define DEVLINK_TRAP_GENERIC_NAME_IPV4_OSPF \
870         "ipv4_ospf"
871 #define DEVLINK_TRAP_GENERIC_NAME_IPV6_OSPF \
872         "ipv6_ospf"
873 #define DEVLINK_TRAP_GENERIC_NAME_IPV4_BGP \
874         "ipv4_bgp"
875 #define DEVLINK_TRAP_GENERIC_NAME_IPV6_BGP \
876         "ipv6_bgp"
877 #define DEVLINK_TRAP_GENERIC_NAME_IPV4_VRRP \
878         "ipv4_vrrp"
879 #define DEVLINK_TRAP_GENERIC_NAME_IPV6_VRRP \
880         "ipv6_vrrp"
881 #define DEVLINK_TRAP_GENERIC_NAME_IPV4_PIM \
882         "ipv4_pim"
883 #define DEVLINK_TRAP_GENERIC_NAME_IPV6_PIM \
884         "ipv6_pim"
885 #define DEVLINK_TRAP_GENERIC_NAME_UC_LB \
886         "uc_loopback"
887 #define DEVLINK_TRAP_GENERIC_NAME_LOCAL_ROUTE \
888         "local_route"
889 #define DEVLINK_TRAP_GENERIC_NAME_EXTERNAL_ROUTE \
890         "external_route"
891 #define DEVLINK_TRAP_GENERIC_NAME_IPV6_UC_DIP_LINK_LOCAL_SCOPE \
892         "ipv6_uc_dip_link_local_scope"
893 #define DEVLINK_TRAP_GENERIC_NAME_IPV6_DIP_ALL_NODES \
894         "ipv6_dip_all_nodes"
895 #define DEVLINK_TRAP_GENERIC_NAME_IPV6_DIP_ALL_ROUTERS \
896         "ipv6_dip_all_routers"
897 #define DEVLINK_TRAP_GENERIC_NAME_IPV6_ROUTER_SOLICIT \
898         "ipv6_router_solicit"
899 #define DEVLINK_TRAP_GENERIC_NAME_IPV6_ROUTER_ADVERT \
900         "ipv6_router_advert"
901 #define DEVLINK_TRAP_GENERIC_NAME_IPV6_REDIRECT \
902         "ipv6_redirect"
903 #define DEVLINK_TRAP_GENERIC_NAME_IPV4_ROUTER_ALERT \
904         "ipv4_router_alert"
905 #define DEVLINK_TRAP_GENERIC_NAME_IPV6_ROUTER_ALERT \
906         "ipv6_router_alert"
907 #define DEVLINK_TRAP_GENERIC_NAME_PTP_EVENT \
908         "ptp_event"
909 #define DEVLINK_TRAP_GENERIC_NAME_PTP_GENERAL \
910         "ptp_general"
911 #define DEVLINK_TRAP_GENERIC_NAME_FLOW_ACTION_SAMPLE \
912         "flow_action_sample"
913 #define DEVLINK_TRAP_GENERIC_NAME_FLOW_ACTION_TRAP \
914         "flow_action_trap"
915 #define DEVLINK_TRAP_GENERIC_NAME_EARLY_DROP \
916         "early_drop"
917
918 #define DEVLINK_TRAP_GROUP_GENERIC_NAME_L2_DROPS \
919         "l2_drops"
920 #define DEVLINK_TRAP_GROUP_GENERIC_NAME_L3_DROPS \
921         "l3_drops"
922 #define DEVLINK_TRAP_GROUP_GENERIC_NAME_L3_EXCEPTIONS \
923         "l3_exceptions"
924 #define DEVLINK_TRAP_GROUP_GENERIC_NAME_BUFFER_DROPS \
925         "buffer_drops"
926 #define DEVLINK_TRAP_GROUP_GENERIC_NAME_TUNNEL_DROPS \
927         "tunnel_drops"
928 #define DEVLINK_TRAP_GROUP_GENERIC_NAME_ACL_DROPS \
929         "acl_drops"
930 #define DEVLINK_TRAP_GROUP_GENERIC_NAME_STP \
931         "stp"
932 #define DEVLINK_TRAP_GROUP_GENERIC_NAME_LACP \
933         "lacp"
934 #define DEVLINK_TRAP_GROUP_GENERIC_NAME_LLDP \
935         "lldp"
936 #define DEVLINK_TRAP_GROUP_GENERIC_NAME_MC_SNOOPING  \
937         "mc_snooping"
938 #define DEVLINK_TRAP_GROUP_GENERIC_NAME_DHCP \
939         "dhcp"
940 #define DEVLINK_TRAP_GROUP_GENERIC_NAME_NEIGH_DISCOVERY \
941         "neigh_discovery"
942 #define DEVLINK_TRAP_GROUP_GENERIC_NAME_BFD \
943         "bfd"
944 #define DEVLINK_TRAP_GROUP_GENERIC_NAME_OSPF \
945         "ospf"
946 #define DEVLINK_TRAP_GROUP_GENERIC_NAME_BGP \
947         "bgp"
948 #define DEVLINK_TRAP_GROUP_GENERIC_NAME_VRRP \
949         "vrrp"
950 #define DEVLINK_TRAP_GROUP_GENERIC_NAME_PIM \
951         "pim"
952 #define DEVLINK_TRAP_GROUP_GENERIC_NAME_UC_LB \
953         "uc_loopback"
954 #define DEVLINK_TRAP_GROUP_GENERIC_NAME_LOCAL_DELIVERY \
955         "local_delivery"
956 #define DEVLINK_TRAP_GROUP_GENERIC_NAME_EXTERNAL_DELIVERY \
957         "external_delivery"
958 #define DEVLINK_TRAP_GROUP_GENERIC_NAME_IPV6 \
959         "ipv6"
960 #define DEVLINK_TRAP_GROUP_GENERIC_NAME_PTP_EVENT \
961         "ptp_event"
962 #define DEVLINK_TRAP_GROUP_GENERIC_NAME_PTP_GENERAL \
963         "ptp_general"
964 #define DEVLINK_TRAP_GROUP_GENERIC_NAME_ACL_SAMPLE \
965         "acl_sample"
966 #define DEVLINK_TRAP_GROUP_GENERIC_NAME_ACL_TRAP \
967         "acl_trap"
968
969 #define DEVLINK_TRAP_GENERIC(_type, _init_action, _id, _group_id,             \
970                              _metadata_cap)                                   \
971         {                                                                     \
972                 .type = DEVLINK_TRAP_TYPE_##_type,                            \
973                 .init_action = DEVLINK_TRAP_ACTION_##_init_action,            \
974                 .generic = true,                                              \
975                 .id = DEVLINK_TRAP_GENERIC_ID_##_id,                          \
976                 .name = DEVLINK_TRAP_GENERIC_NAME_##_id,                      \
977                 .init_group_id = _group_id,                                   \
978                 .metadata_cap = _metadata_cap,                                \
979         }
980
981 #define DEVLINK_TRAP_DRIVER(_type, _init_action, _id, _name, _group_id,       \
982                             _metadata_cap)                                    \
983         {                                                                     \
984                 .type = DEVLINK_TRAP_TYPE_##_type,                            \
985                 .init_action = DEVLINK_TRAP_ACTION_##_init_action,            \
986                 .generic = false,                                             \
987                 .id = _id,                                                    \
988                 .name = _name,                                                \
989                 .init_group_id = _group_id,                                   \
990                 .metadata_cap = _metadata_cap,                                \
991         }
992
993 #define DEVLINK_TRAP_GROUP_GENERIC(_id, _policer_id)                          \
994         {                                                                     \
995                 .name = DEVLINK_TRAP_GROUP_GENERIC_NAME_##_id,                \
996                 .id = DEVLINK_TRAP_GROUP_GENERIC_ID_##_id,                    \
997                 .generic = true,                                              \
998                 .init_policer_id = _policer_id,                               \
999         }
1000
1001 #define DEVLINK_TRAP_POLICER(_id, _rate, _burst, _max_rate, _min_rate,        \
1002                              _max_burst, _min_burst)                          \
1003         {                                                                     \
1004                 .id = _id,                                                    \
1005                 .init_rate = _rate,                                           \
1006                 .init_burst = _burst,                                         \
1007                 .max_rate = _max_rate,                                        \
1008                 .min_rate = _min_rate,                                        \
1009                 .max_burst = _max_burst,                                      \
1010                 .min_burst = _min_burst,                                      \
1011         }
1012
1013 struct devlink_ops {
1014         int (*reload_down)(struct devlink *devlink, bool netns_change,
1015                            struct netlink_ext_ack *extack);
1016         int (*reload_up)(struct devlink *devlink,
1017                          struct netlink_ext_ack *extack);
1018         int (*port_type_set)(struct devlink_port *devlink_port,
1019                              enum devlink_port_type port_type);
1020         int (*port_split)(struct devlink *devlink, unsigned int port_index,
1021                           unsigned int count, struct netlink_ext_ack *extack);
1022         int (*port_unsplit)(struct devlink *devlink, unsigned int port_index,
1023                             struct netlink_ext_ack *extack);
1024         int (*sb_pool_get)(struct devlink *devlink, unsigned int sb_index,
1025                            u16 pool_index,
1026                            struct devlink_sb_pool_info *pool_info);
1027         int (*sb_pool_set)(struct devlink *devlink, unsigned int sb_index,
1028                            u16 pool_index, u32 size,
1029                            enum devlink_sb_threshold_type threshold_type,
1030                            struct netlink_ext_ack *extack);
1031         int (*sb_port_pool_get)(struct devlink_port *devlink_port,
1032                                 unsigned int sb_index, u16 pool_index,
1033                                 u32 *p_threshold);
1034         int (*sb_port_pool_set)(struct devlink_port *devlink_port,
1035                                 unsigned int sb_index, u16 pool_index,
1036                                 u32 threshold, struct netlink_ext_ack *extack);
1037         int (*sb_tc_pool_bind_get)(struct devlink_port *devlink_port,
1038                                    unsigned int sb_index,
1039                                    u16 tc_index,
1040                                    enum devlink_sb_pool_type pool_type,
1041                                    u16 *p_pool_index, u32 *p_threshold);
1042         int (*sb_tc_pool_bind_set)(struct devlink_port *devlink_port,
1043                                    unsigned int sb_index,
1044                                    u16 tc_index,
1045                                    enum devlink_sb_pool_type pool_type,
1046                                    u16 pool_index, u32 threshold,
1047                                    struct netlink_ext_ack *extack);
1048         int (*sb_occ_snapshot)(struct devlink *devlink,
1049                                unsigned int sb_index);
1050         int (*sb_occ_max_clear)(struct devlink *devlink,
1051                                 unsigned int sb_index);
1052         int (*sb_occ_port_pool_get)(struct devlink_port *devlink_port,
1053                                     unsigned int sb_index, u16 pool_index,
1054                                     u32 *p_cur, u32 *p_max);
1055         int (*sb_occ_tc_port_bind_get)(struct devlink_port *devlink_port,
1056                                        unsigned int sb_index,
1057                                        u16 tc_index,
1058                                        enum devlink_sb_pool_type pool_type,
1059                                        u32 *p_cur, u32 *p_max);
1060
1061         int (*eswitch_mode_get)(struct devlink *devlink, u16 *p_mode);
1062         int (*eswitch_mode_set)(struct devlink *devlink, u16 mode,
1063                                 struct netlink_ext_ack *extack);
1064         int (*eswitch_inline_mode_get)(struct devlink *devlink, u8 *p_inline_mode);
1065         int (*eswitch_inline_mode_set)(struct devlink *devlink, u8 inline_mode,
1066                                        struct netlink_ext_ack *extack);
1067         int (*eswitch_encap_mode_get)(struct devlink *devlink,
1068                                       enum devlink_eswitch_encap_mode *p_encap_mode);
1069         int (*eswitch_encap_mode_set)(struct devlink *devlink,
1070                                       enum devlink_eswitch_encap_mode encap_mode,
1071                                       struct netlink_ext_ack *extack);
1072         int (*info_get)(struct devlink *devlink, struct devlink_info_req *req,
1073                         struct netlink_ext_ack *extack);
1074         int (*flash_update)(struct devlink *devlink, const char *file_name,
1075                             const char *component,
1076                             struct netlink_ext_ack *extack);
1077         /**
1078          * @trap_init: Trap initialization function.
1079          *
1080          * Should be used by device drivers to initialize the trap in the
1081          * underlying device. Drivers should also store the provided trap
1082          * context, so that they could efficiently pass it to
1083          * devlink_trap_report() when the trap is triggered.
1084          */
1085         int (*trap_init)(struct devlink *devlink,
1086                          const struct devlink_trap *trap, void *trap_ctx);
1087         /**
1088          * @trap_fini: Trap de-initialization function.
1089          *
1090          * Should be used by device drivers to de-initialize the trap in the
1091          * underlying device.
1092          */
1093         void (*trap_fini)(struct devlink *devlink,
1094                           const struct devlink_trap *trap, void *trap_ctx);
1095         /**
1096          * @trap_action_set: Trap action set function.
1097          */
1098         int (*trap_action_set)(struct devlink *devlink,
1099                                const struct devlink_trap *trap,
1100                                enum devlink_trap_action action,
1101                                struct netlink_ext_ack *extack);
1102         /**
1103          * @trap_group_init: Trap group initialization function.
1104          *
1105          * Should be used by device drivers to initialize the trap group in the
1106          * underlying device.
1107          */
1108         int (*trap_group_init)(struct devlink *devlink,
1109                                const struct devlink_trap_group *group);
1110         /**
1111          * @trap_group_set: Trap group parameters set function.
1112          *
1113          * Note: @policer can be NULL when a policer is being unbound from
1114          * @group.
1115          */
1116         int (*trap_group_set)(struct devlink *devlink,
1117                               const struct devlink_trap_group *group,
1118                               const struct devlink_trap_policer *policer,
1119                               struct netlink_ext_ack *extack);
1120         /**
1121          * @trap_policer_init: Trap policer initialization function.
1122          *
1123          * Should be used by device drivers to initialize the trap policer in
1124          * the underlying device.
1125          */
1126         int (*trap_policer_init)(struct devlink *devlink,
1127                                  const struct devlink_trap_policer *policer);
1128         /**
1129          * @trap_policer_fini: Trap policer de-initialization function.
1130          *
1131          * Should be used by device drivers to de-initialize the trap policer
1132          * in the underlying device.
1133          */
1134         void (*trap_policer_fini)(struct devlink *devlink,
1135                                   const struct devlink_trap_policer *policer);
1136         /**
1137          * @trap_policer_set: Trap policer parameters set function.
1138          */
1139         int (*trap_policer_set)(struct devlink *devlink,
1140                                 const struct devlink_trap_policer *policer,
1141                                 u64 rate, u64 burst,
1142                                 struct netlink_ext_ack *extack);
1143         /**
1144          * @trap_policer_counter_get: Trap policer counter get function.
1145          *
1146          * Should be used by device drivers to report number of packets dropped
1147          * by the policer.
1148          */
1149         int (*trap_policer_counter_get)(struct devlink *devlink,
1150                                         const struct devlink_trap_policer *policer,
1151                                         u64 *p_drops);
1152         /**
1153          * @port_function_hw_addr_get: Port function's hardware address get function.
1154          *
1155          * Should be used by device drivers to report the hardware address of a function managed
1156          * by the devlink port. Driver should return -EOPNOTSUPP if it doesn't support port
1157          * function handling for a particular port.
1158          *
1159          * Note: @extack can be NULL when port notifier queries the port function.
1160          */
1161         int (*port_function_hw_addr_get)(struct devlink *devlink, struct devlink_port *port,
1162                                          u8 *hw_addr, int *hw_addr_len,
1163                                          struct netlink_ext_ack *extack);
1164         /**
1165          * @port_function_hw_addr_set: Port function's hardware address set function.
1166          *
1167          * Should be used by device drivers to set the hardware address of a function managed
1168          * by the devlink port. Driver should return -EOPNOTSUPP if it doesn't support port
1169          * function handling for a particular port.
1170          */
1171         int (*port_function_hw_addr_set)(struct devlink *devlink, struct devlink_port *port,
1172                                          const u8 *hw_addr, int hw_addr_len,
1173                                          struct netlink_ext_ack *extack);
1174 };
1175
1176 static inline void *devlink_priv(struct devlink *devlink)
1177 {
1178         BUG_ON(!devlink);
1179         return &devlink->priv;
1180 }
1181
1182 static inline struct devlink *priv_to_devlink(void *priv)
1183 {
1184         BUG_ON(!priv);
1185         return container_of(priv, struct devlink, priv);
1186 }
1187
1188 static inline struct devlink_port *
1189 netdev_to_devlink_port(struct net_device *dev)
1190 {
1191         if (dev->netdev_ops->ndo_get_devlink_port)
1192                 return dev->netdev_ops->ndo_get_devlink_port(dev);
1193         return NULL;
1194 }
1195
1196 static inline struct devlink *netdev_to_devlink(struct net_device *dev)
1197 {
1198         struct devlink_port *devlink_port = netdev_to_devlink_port(dev);
1199
1200         if (devlink_port)
1201                 return devlink_port->devlink;
1202         return NULL;
1203 }
1204
1205 struct ib_device;
1206
1207 struct net *devlink_net(const struct devlink *devlink);
1208 void devlink_net_set(struct devlink *devlink, struct net *net);
1209 struct devlink *devlink_alloc(const struct devlink_ops *ops, size_t priv_size);
1210 int devlink_register(struct devlink *devlink, struct device *dev);
1211 void devlink_unregister(struct devlink *devlink);
1212 void devlink_reload_enable(struct devlink *devlink);
1213 void devlink_reload_disable(struct devlink *devlink);
1214 void devlink_free(struct devlink *devlink);
1215 int devlink_port_register(struct devlink *devlink,
1216                           struct devlink_port *devlink_port,
1217                           unsigned int port_index);
1218 void devlink_port_unregister(struct devlink_port *devlink_port);
1219 void devlink_port_type_eth_set(struct devlink_port *devlink_port,
1220                                struct net_device *netdev);
1221 void devlink_port_type_ib_set(struct devlink_port *devlink_port,
1222                               struct ib_device *ibdev);
1223 void devlink_port_type_clear(struct devlink_port *devlink_port);
1224 void devlink_port_attrs_set(struct devlink_port *devlink_port,
1225                             struct devlink_port_attrs *devlink_port_attrs);
1226 void devlink_port_attrs_pci_pf_set(struct devlink_port *devlink_port, u32 controller,
1227                                    u16 pf, bool external);
1228 void devlink_port_attrs_pci_vf_set(struct devlink_port *devlink_port, u32 controller,
1229                                    u16 pf, u16 vf, bool external);
1230 int devlink_sb_register(struct devlink *devlink, unsigned int sb_index,
1231                         u32 size, u16 ingress_pools_count,
1232                         u16 egress_pools_count, u16 ingress_tc_count,
1233                         u16 egress_tc_count);
1234 void devlink_sb_unregister(struct devlink *devlink, unsigned int sb_index);
1235 int devlink_dpipe_table_register(struct devlink *devlink,
1236                                  const char *table_name,
1237                                  struct devlink_dpipe_table_ops *table_ops,
1238                                  void *priv, bool counter_control_extern);
1239 void devlink_dpipe_table_unregister(struct devlink *devlink,
1240                                     const char *table_name);
1241 int devlink_dpipe_headers_register(struct devlink *devlink,
1242                                    struct devlink_dpipe_headers *dpipe_headers);
1243 void devlink_dpipe_headers_unregister(struct devlink *devlink);
1244 bool devlink_dpipe_table_counter_enabled(struct devlink *devlink,
1245                                          const char *table_name);
1246 int devlink_dpipe_entry_ctx_prepare(struct devlink_dpipe_dump_ctx *dump_ctx);
1247 int devlink_dpipe_entry_ctx_append(struct devlink_dpipe_dump_ctx *dump_ctx,
1248                                    struct devlink_dpipe_entry *entry);
1249 int devlink_dpipe_entry_ctx_close(struct devlink_dpipe_dump_ctx *dump_ctx);
1250 void devlink_dpipe_entry_clear(struct devlink_dpipe_entry *entry);
1251 int devlink_dpipe_action_put(struct sk_buff *skb,
1252                              struct devlink_dpipe_action *action);
1253 int devlink_dpipe_match_put(struct sk_buff *skb,
1254                             struct devlink_dpipe_match *match);
1255 extern struct devlink_dpipe_header devlink_dpipe_header_ethernet;
1256 extern struct devlink_dpipe_header devlink_dpipe_header_ipv4;
1257 extern struct devlink_dpipe_header devlink_dpipe_header_ipv6;
1258
1259 int devlink_resource_register(struct devlink *devlink,
1260                               const char *resource_name,
1261                               u64 resource_size,
1262                               u64 resource_id,
1263                               u64 parent_resource_id,
1264                               const struct devlink_resource_size_params *size_params);
1265 void devlink_resources_unregister(struct devlink *devlink,
1266                                   struct devlink_resource *resource);
1267 int devlink_resource_size_get(struct devlink *devlink,
1268                               u64 resource_id,
1269                               u64 *p_resource_size);
1270 int devlink_dpipe_table_resource_set(struct devlink *devlink,
1271                                      const char *table_name, u64 resource_id,
1272                                      u64 resource_units);
1273 void devlink_resource_occ_get_register(struct devlink *devlink,
1274                                        u64 resource_id,
1275                                        devlink_resource_occ_get_t *occ_get,
1276                                        void *occ_get_priv);
1277 void devlink_resource_occ_get_unregister(struct devlink *devlink,
1278                                          u64 resource_id);
1279 int devlink_params_register(struct devlink *devlink,
1280                             const struct devlink_param *params,
1281                             size_t params_count);
1282 void devlink_params_unregister(struct devlink *devlink,
1283                                const struct devlink_param *params,
1284                                size_t params_count);
1285 void devlink_params_publish(struct devlink *devlink);
1286 void devlink_params_unpublish(struct devlink *devlink);
1287 int devlink_port_params_register(struct devlink_port *devlink_port,
1288                                  const struct devlink_param *params,
1289                                  size_t params_count);
1290 void devlink_port_params_unregister(struct devlink_port *devlink_port,
1291                                     const struct devlink_param *params,
1292                                     size_t params_count);
1293 int devlink_param_driverinit_value_get(struct devlink *devlink, u32 param_id,
1294                                        union devlink_param_value *init_val);
1295 int devlink_param_driverinit_value_set(struct devlink *devlink, u32 param_id,
1296                                        union devlink_param_value init_val);
1297 int
1298 devlink_port_param_driverinit_value_get(struct devlink_port *devlink_port,
1299                                         u32 param_id,
1300                                         union devlink_param_value *init_val);
1301 int devlink_port_param_driverinit_value_set(struct devlink_port *devlink_port,
1302                                             u32 param_id,
1303                                             union devlink_param_value init_val);
1304 void devlink_param_value_changed(struct devlink *devlink, u32 param_id);
1305 void devlink_port_param_value_changed(struct devlink_port *devlink_port,
1306                                       u32 param_id);
1307 void devlink_param_value_str_fill(union devlink_param_value *dst_val,
1308                                   const char *src);
1309 struct devlink_region *
1310 devlink_region_create(struct devlink *devlink,
1311                       const struct devlink_region_ops *ops,
1312                       u32 region_max_snapshots, u64 region_size);
1313 void devlink_region_destroy(struct devlink_region *region);
1314 int devlink_region_snapshot_id_get(struct devlink *devlink, u32 *id);
1315 void devlink_region_snapshot_id_put(struct devlink *devlink, u32 id);
1316 int devlink_region_snapshot_create(struct devlink_region *region,
1317                                    u8 *data, u32 snapshot_id);
1318 int devlink_info_serial_number_put(struct devlink_info_req *req,
1319                                    const char *sn);
1320 int devlink_info_driver_name_put(struct devlink_info_req *req,
1321                                  const char *name);
1322 int devlink_info_board_serial_number_put(struct devlink_info_req *req,
1323                                          const char *bsn);
1324 int devlink_info_version_fixed_put(struct devlink_info_req *req,
1325                                    const char *version_name,
1326                                    const char *version_value);
1327 int devlink_info_version_stored_put(struct devlink_info_req *req,
1328                                     const char *version_name,
1329                                     const char *version_value);
1330 int devlink_info_version_running_put(struct devlink_info_req *req,
1331                                      const char *version_name,
1332                                      const char *version_value);
1333
1334 int devlink_fmsg_obj_nest_start(struct devlink_fmsg *fmsg);
1335 int devlink_fmsg_obj_nest_end(struct devlink_fmsg *fmsg);
1336
1337 int devlink_fmsg_pair_nest_start(struct devlink_fmsg *fmsg, const char *name);
1338 int devlink_fmsg_pair_nest_end(struct devlink_fmsg *fmsg);
1339
1340 int devlink_fmsg_arr_pair_nest_start(struct devlink_fmsg *fmsg,
1341                                      const char *name);
1342 int devlink_fmsg_arr_pair_nest_end(struct devlink_fmsg *fmsg);
1343 int devlink_fmsg_binary_pair_nest_start(struct devlink_fmsg *fmsg,
1344                                         const char *name);
1345 int devlink_fmsg_binary_pair_nest_end(struct devlink_fmsg *fmsg);
1346
1347 int devlink_fmsg_bool_put(struct devlink_fmsg *fmsg, bool value);
1348 int devlink_fmsg_u8_put(struct devlink_fmsg *fmsg, u8 value);
1349 int devlink_fmsg_u32_put(struct devlink_fmsg *fmsg, u32 value);
1350 int devlink_fmsg_u64_put(struct devlink_fmsg *fmsg, u64 value);
1351 int devlink_fmsg_string_put(struct devlink_fmsg *fmsg, const char *value);
1352 int devlink_fmsg_binary_put(struct devlink_fmsg *fmsg, const void *value,
1353                             u16 value_len);
1354
1355 int devlink_fmsg_bool_pair_put(struct devlink_fmsg *fmsg, const char *name,
1356                                bool value);
1357 int devlink_fmsg_u8_pair_put(struct devlink_fmsg *fmsg, const char *name,
1358                              u8 value);
1359 int devlink_fmsg_u32_pair_put(struct devlink_fmsg *fmsg, const char *name,
1360                               u32 value);
1361 int devlink_fmsg_u64_pair_put(struct devlink_fmsg *fmsg, const char *name,
1362                               u64 value);
1363 int devlink_fmsg_string_pair_put(struct devlink_fmsg *fmsg, const char *name,
1364                                  const char *value);
1365 int devlink_fmsg_binary_pair_put(struct devlink_fmsg *fmsg, const char *name,
1366                                  const void *value, u32 value_len);
1367
1368 struct devlink_health_reporter *
1369 devlink_health_reporter_create(struct devlink *devlink,
1370                                const struct devlink_health_reporter_ops *ops,
1371                                u64 graceful_period, void *priv);
1372
1373 struct devlink_health_reporter *
1374 devlink_port_health_reporter_create(struct devlink_port *port,
1375                                     const struct devlink_health_reporter_ops *ops,
1376                                     u64 graceful_period, void *priv);
1377
1378 void
1379 devlink_health_reporter_destroy(struct devlink_health_reporter *reporter);
1380
1381 void
1382 devlink_port_health_reporter_destroy(struct devlink_health_reporter *reporter);
1383
1384 void *
1385 devlink_health_reporter_priv(struct devlink_health_reporter *reporter);
1386 int devlink_health_report(struct devlink_health_reporter *reporter,
1387                           const char *msg, void *priv_ctx);
1388 void
1389 devlink_health_reporter_state_update(struct devlink_health_reporter *reporter,
1390                                      enum devlink_health_reporter_state state);
1391 void
1392 devlink_health_reporter_recovery_done(struct devlink_health_reporter *reporter);
1393
1394 bool devlink_is_reload_failed(const struct devlink *devlink);
1395
1396 void devlink_flash_update_begin_notify(struct devlink *devlink);
1397 void devlink_flash_update_end_notify(struct devlink *devlink);
1398 void devlink_flash_update_status_notify(struct devlink *devlink,
1399                                         const char *status_msg,
1400                                         const char *component,
1401                                         unsigned long done,
1402                                         unsigned long total);
1403
1404 int devlink_traps_register(struct devlink *devlink,
1405                            const struct devlink_trap *traps,
1406                            size_t traps_count, void *priv);
1407 void devlink_traps_unregister(struct devlink *devlink,
1408                               const struct devlink_trap *traps,
1409                               size_t traps_count);
1410 void devlink_trap_report(struct devlink *devlink, struct sk_buff *skb,
1411                          void *trap_ctx, struct devlink_port *in_devlink_port,
1412                          const struct flow_action_cookie *fa_cookie);
1413 void *devlink_trap_ctx_priv(void *trap_ctx);
1414 int devlink_trap_groups_register(struct devlink *devlink,
1415                                  const struct devlink_trap_group *groups,
1416                                  size_t groups_count);
1417 void devlink_trap_groups_unregister(struct devlink *devlink,
1418                                     const struct devlink_trap_group *groups,
1419                                     size_t groups_count);
1420 int
1421 devlink_trap_policers_register(struct devlink *devlink,
1422                                const struct devlink_trap_policer *policers,
1423                                size_t policers_count);
1424 void
1425 devlink_trap_policers_unregister(struct devlink *devlink,
1426                                  const struct devlink_trap_policer *policers,
1427                                  size_t policers_count);
1428
1429 #if IS_ENABLED(CONFIG_NET_DEVLINK)
1430
1431 void devlink_compat_running_version(struct net_device *dev,
1432                                     char *buf, size_t len);
1433 int devlink_compat_flash_update(struct net_device *dev, const char *file_name);
1434 int devlink_compat_phys_port_name_get(struct net_device *dev,
1435                                       char *name, size_t len);
1436 int devlink_compat_switch_id_get(struct net_device *dev,
1437                                  struct netdev_phys_item_id *ppid);
1438
1439 #else
1440
1441 static inline void
1442 devlink_compat_running_version(struct net_device *dev, char *buf, size_t len)
1443 {
1444 }
1445
1446 static inline int
1447 devlink_compat_flash_update(struct net_device *dev, const char *file_name)
1448 {
1449         return -EOPNOTSUPP;
1450 }
1451
1452 static inline int
1453 devlink_compat_phys_port_name_get(struct net_device *dev,
1454                                   char *name, size_t len)
1455 {
1456         return -EOPNOTSUPP;
1457 }
1458
1459 static inline int
1460 devlink_compat_switch_id_get(struct net_device *dev,
1461                              struct netdev_phys_item_id *ppid)
1462 {
1463         return -EOPNOTSUPP;
1464 }
1465
1466 #endif
1467
1468 #endif /* _NET_DEVLINK_H_ */