Merge tag 'microblaze-v5.4-rc1' of git://git.monstr.eu/linux-2.6-microblaze
[sfrench/cifs-2.6.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 <uapi/linux/devlink.h>
20
21 struct devlink_ops;
22
23 struct devlink {
24         struct list_head list;
25         struct list_head port_list;
26         struct list_head sb_list;
27         struct list_head dpipe_table_list;
28         struct list_head resource_list;
29         struct list_head param_list;
30         struct list_head region_list;
31         u32 snapshot_id;
32         struct list_head reporter_list;
33         struct mutex reporters_lock; /* protects reporter_list */
34         struct devlink_dpipe_headers *dpipe_headers;
35         struct list_head trap_list;
36         struct list_head trap_group_list;
37         const struct devlink_ops *ops;
38         struct device *dev;
39         possible_net_t _net;
40         struct mutex lock;
41         bool reload_failed;
42         char priv[0] __aligned(NETDEV_ALIGN);
43 };
44
45 struct devlink_port_phys_attrs {
46         u32 port_number; /* Same value as "split group".
47                           * A physical port which is visible to the user
48                           * for a given port flavour.
49                           */
50         u32 split_subport_number;
51 };
52
53 struct devlink_port_pci_pf_attrs {
54         u16 pf; /* Associated PCI PF for this port. */
55 };
56
57 struct devlink_port_pci_vf_attrs {
58         u16 pf; /* Associated PCI PF for this port. */
59         u16 vf; /* Associated PCI VF for of the PCI PF for this port. */
60 };
61
62 struct devlink_port_attrs {
63         u8 set:1,
64            split:1,
65            switch_port:1;
66         enum devlink_port_flavour flavour;
67         struct netdev_phys_item_id switch_id;
68         union {
69                 struct devlink_port_phys_attrs phys;
70                 struct devlink_port_pci_pf_attrs pci_pf;
71                 struct devlink_port_pci_vf_attrs pci_vf;
72         };
73 };
74
75 struct devlink_port {
76         struct list_head list;
77         struct list_head param_list;
78         struct devlink *devlink;
79         unsigned int index;
80         bool registered;
81         spinlock_t type_lock; /* Protects type and type_dev
82                                * pointer consistency.
83                                */
84         enum devlink_port_type type;
85         enum devlink_port_type desired_type;
86         void *type_dev;
87         struct devlink_port_attrs attrs;
88         struct delayed_work type_warn_dw;
89 };
90
91 struct devlink_sb_pool_info {
92         enum devlink_sb_pool_type pool_type;
93         u32 size;
94         enum devlink_sb_threshold_type threshold_type;
95         u32 cell_size;
96 };
97
98 /**
99  * struct devlink_dpipe_field - dpipe field object
100  * @name: field name
101  * @id: index inside the headers field array
102  * @bitwidth: bitwidth
103  * @mapping_type: mapping type
104  */
105 struct devlink_dpipe_field {
106         const char *name;
107         unsigned int id;
108         unsigned int bitwidth;
109         enum devlink_dpipe_field_mapping_type mapping_type;
110 };
111
112 /**
113  * struct devlink_dpipe_header - dpipe header object
114  * @name: header name
115  * @id: index, global/local detrmined by global bit
116  * @fields: fields
117  * @fields_count: number of fields
118  * @global: indicates if header is shared like most protocol header
119  *          or driver specific
120  */
121 struct devlink_dpipe_header {
122         const char *name;
123         unsigned int id;
124         struct devlink_dpipe_field *fields;
125         unsigned int fields_count;
126         bool global;
127 };
128
129 /**
130  * struct devlink_dpipe_match - represents match operation
131  * @type: type of match
132  * @header_index: header index (packets can have several headers of same
133  *                type like in case of tunnels)
134  * @header: header
135  * @fieled_id: field index
136  */
137 struct devlink_dpipe_match {
138         enum devlink_dpipe_match_type type;
139         unsigned int header_index;
140         struct devlink_dpipe_header *header;
141         unsigned int field_id;
142 };
143
144 /**
145  * struct devlink_dpipe_action - represents action operation
146  * @type: type of action
147  * @header_index: header index (packets can have several headers of same
148  *                type like in case of tunnels)
149  * @header: header
150  * @fieled_id: field index
151  */
152 struct devlink_dpipe_action {
153         enum devlink_dpipe_action_type type;
154         unsigned int header_index;
155         struct devlink_dpipe_header *header;
156         unsigned int field_id;
157 };
158
159 /**
160  * struct devlink_dpipe_value - represents value of match/action
161  * @action: action
162  * @match: match
163  * @mapping_value: in case the field has some mapping this value
164  *                 specified the mapping value
165  * @mapping_valid: specify if mapping value is valid
166  * @value_size: value size
167  * @value: value
168  * @mask: bit mask
169  */
170 struct devlink_dpipe_value {
171         union {
172                 struct devlink_dpipe_action *action;
173                 struct devlink_dpipe_match *match;
174         };
175         unsigned int mapping_value;
176         bool mapping_valid;
177         unsigned int value_size;
178         void *value;
179         void *mask;
180 };
181
182 /**
183  * struct devlink_dpipe_entry - table entry object
184  * @index: index of the entry in the table
185  * @match_values: match values
186  * @matche_values_count: count of matches tuples
187  * @action_values: actions values
188  * @action_values_count: count of actions values
189  * @counter: value of counter
190  * @counter_valid: Specify if value is valid from hardware
191  */
192 struct devlink_dpipe_entry {
193         u64 index;
194         struct devlink_dpipe_value *match_values;
195         unsigned int match_values_count;
196         struct devlink_dpipe_value *action_values;
197         unsigned int action_values_count;
198         u64 counter;
199         bool counter_valid;
200 };
201
202 /**
203  * struct devlink_dpipe_dump_ctx - context provided to driver in order
204  *                                 to dump
205  * @info: info
206  * @cmd: devlink command
207  * @skb: skb
208  * @nest: top attribute
209  * @hdr: hdr
210  */
211 struct devlink_dpipe_dump_ctx {
212         struct genl_info *info;
213         enum devlink_command cmd;
214         struct sk_buff *skb;
215         struct nlattr *nest;
216         void *hdr;
217 };
218
219 struct devlink_dpipe_table_ops;
220
221 /**
222  * struct devlink_dpipe_table - table object
223  * @priv: private
224  * @name: table name
225  * @counters_enabled: indicates if counters are active
226  * @counter_control_extern: indicates if counter control is in dpipe or
227  *                          external tool
228  * @resource_valid: Indicate that the resource id is valid
229  * @resource_id: relative resource this table is related to
230  * @resource_units: number of resource's unit consumed per table's entry
231  * @table_ops: table operations
232  * @rcu: rcu
233  */
234 struct devlink_dpipe_table {
235         void *priv;
236         struct list_head list;
237         const char *name;
238         bool counters_enabled;
239         bool counter_control_extern;
240         bool resource_valid;
241         u64 resource_id;
242         u64 resource_units;
243         struct devlink_dpipe_table_ops *table_ops;
244         struct rcu_head rcu;
245 };
246
247 /**
248  * struct devlink_dpipe_table_ops - dpipe_table ops
249  * @actions_dump - dumps all tables actions
250  * @matches_dump - dumps all tables matches
251  * @entries_dump - dumps all active entries in the table
252  * @counters_set_update - when changing the counter status hardware sync
253  *                        maybe needed to allocate/free counter related
254  *                        resources
255  * @size_get - get size
256  */
257 struct devlink_dpipe_table_ops {
258         int (*actions_dump)(void *priv, struct sk_buff *skb);
259         int (*matches_dump)(void *priv, struct sk_buff *skb);
260         int (*entries_dump)(void *priv, bool counters_enabled,
261                             struct devlink_dpipe_dump_ctx *dump_ctx);
262         int (*counters_set_update)(void *priv, bool enable);
263         u64 (*size_get)(void *priv);
264 };
265
266 /**
267  * struct devlink_dpipe_headers - dpipe headers
268  * @headers - header array can be shared (global bit) or driver specific
269  * @headers_count - count of headers
270  */
271 struct devlink_dpipe_headers {
272         struct devlink_dpipe_header **headers;
273         unsigned int headers_count;
274 };
275
276 /**
277  * struct devlink_resource_size_params - resource's size parameters
278  * @size_min: minimum size which can be set
279  * @size_max: maximum size which can be set
280  * @size_granularity: size granularity
281  * @size_unit: resource's basic unit
282  */
283 struct devlink_resource_size_params {
284         u64 size_min;
285         u64 size_max;
286         u64 size_granularity;
287         enum devlink_resource_unit unit;
288 };
289
290 static inline void
291 devlink_resource_size_params_init(struct devlink_resource_size_params *size_params,
292                                   u64 size_min, u64 size_max,
293                                   u64 size_granularity,
294                                   enum devlink_resource_unit unit)
295 {
296         size_params->size_min = size_min;
297         size_params->size_max = size_max;
298         size_params->size_granularity = size_granularity;
299         size_params->unit = unit;
300 }
301
302 typedef u64 devlink_resource_occ_get_t(void *priv);
303
304 /**
305  * struct devlink_resource - devlink resource
306  * @name: name of the resource
307  * @id: id, per devlink instance
308  * @size: size of the resource
309  * @size_new: updated size of the resource, reload is needed
310  * @size_valid: valid in case the total size of the resource is valid
311  *              including its children
312  * @parent: parent resource
313  * @size_params: size parameters
314  * @list: parent list
315  * @resource_list: list of child resources
316  */
317 struct devlink_resource {
318         const char *name;
319         u64 id;
320         u64 size;
321         u64 size_new;
322         bool size_valid;
323         struct devlink_resource *parent;
324         struct devlink_resource_size_params size_params;
325         struct list_head list;
326         struct list_head resource_list;
327         devlink_resource_occ_get_t *occ_get;
328         void *occ_get_priv;
329 };
330
331 #define DEVLINK_RESOURCE_ID_PARENT_TOP 0
332
333 #define __DEVLINK_PARAM_MAX_STRING_VALUE 32
334 enum devlink_param_type {
335         DEVLINK_PARAM_TYPE_U8,
336         DEVLINK_PARAM_TYPE_U16,
337         DEVLINK_PARAM_TYPE_U32,
338         DEVLINK_PARAM_TYPE_STRING,
339         DEVLINK_PARAM_TYPE_BOOL,
340 };
341
342 union devlink_param_value {
343         u8 vu8;
344         u16 vu16;
345         u32 vu32;
346         char vstr[__DEVLINK_PARAM_MAX_STRING_VALUE];
347         bool vbool;
348 };
349
350 struct devlink_param_gset_ctx {
351         union devlink_param_value val;
352         enum devlink_param_cmode cmode;
353 };
354
355 /**
356  * struct devlink_param - devlink configuration parameter data
357  * @name: name of the parameter
358  * @generic: indicates if the parameter is generic or driver specific
359  * @type: parameter type
360  * @supported_cmodes: bitmap of supported configuration modes
361  * @get: get parameter value, used for runtime and permanent
362  *       configuration modes
363  * @set: set parameter value, used for runtime and permanent
364  *       configuration modes
365  * @validate: validate input value is applicable (within value range, etc.)
366  *
367  * This struct should be used by the driver to fill the data for
368  * a parameter it registers.
369  */
370 struct devlink_param {
371         u32 id;
372         const char *name;
373         bool generic;
374         enum devlink_param_type type;
375         unsigned long supported_cmodes;
376         int (*get)(struct devlink *devlink, u32 id,
377                    struct devlink_param_gset_ctx *ctx);
378         int (*set)(struct devlink *devlink, u32 id,
379                    struct devlink_param_gset_ctx *ctx);
380         int (*validate)(struct devlink *devlink, u32 id,
381                         union devlink_param_value val,
382                         struct netlink_ext_ack *extack);
383 };
384
385 struct devlink_param_item {
386         struct list_head list;
387         const struct devlink_param *param;
388         union devlink_param_value driverinit_value;
389         bool driverinit_value_valid;
390         bool published;
391 };
392
393 enum devlink_param_generic_id {
394         DEVLINK_PARAM_GENERIC_ID_INT_ERR_RESET,
395         DEVLINK_PARAM_GENERIC_ID_MAX_MACS,
396         DEVLINK_PARAM_GENERIC_ID_ENABLE_SRIOV,
397         DEVLINK_PARAM_GENERIC_ID_REGION_SNAPSHOT,
398         DEVLINK_PARAM_GENERIC_ID_IGNORE_ARI,
399         DEVLINK_PARAM_GENERIC_ID_MSIX_VEC_PER_PF_MAX,
400         DEVLINK_PARAM_GENERIC_ID_MSIX_VEC_PER_PF_MIN,
401         DEVLINK_PARAM_GENERIC_ID_FW_LOAD_POLICY,
402         DEVLINK_PARAM_GENERIC_ID_RESET_DEV_ON_DRV_PROBE,
403
404         /* add new param generic ids above here*/
405         __DEVLINK_PARAM_GENERIC_ID_MAX,
406         DEVLINK_PARAM_GENERIC_ID_MAX = __DEVLINK_PARAM_GENERIC_ID_MAX - 1,
407 };
408
409 #define DEVLINK_PARAM_GENERIC_INT_ERR_RESET_NAME "internal_error_reset"
410 #define DEVLINK_PARAM_GENERIC_INT_ERR_RESET_TYPE DEVLINK_PARAM_TYPE_BOOL
411
412 #define DEVLINK_PARAM_GENERIC_MAX_MACS_NAME "max_macs"
413 #define DEVLINK_PARAM_GENERIC_MAX_MACS_TYPE DEVLINK_PARAM_TYPE_U32
414
415 #define DEVLINK_PARAM_GENERIC_ENABLE_SRIOV_NAME "enable_sriov"
416 #define DEVLINK_PARAM_GENERIC_ENABLE_SRIOV_TYPE DEVLINK_PARAM_TYPE_BOOL
417
418 #define DEVLINK_PARAM_GENERIC_REGION_SNAPSHOT_NAME "region_snapshot_enable"
419 #define DEVLINK_PARAM_GENERIC_REGION_SNAPSHOT_TYPE DEVLINK_PARAM_TYPE_BOOL
420
421 #define DEVLINK_PARAM_GENERIC_IGNORE_ARI_NAME "ignore_ari"
422 #define DEVLINK_PARAM_GENERIC_IGNORE_ARI_TYPE DEVLINK_PARAM_TYPE_BOOL
423
424 #define DEVLINK_PARAM_GENERIC_MSIX_VEC_PER_PF_MAX_NAME "msix_vec_per_pf_max"
425 #define DEVLINK_PARAM_GENERIC_MSIX_VEC_PER_PF_MAX_TYPE DEVLINK_PARAM_TYPE_U32
426
427 #define DEVLINK_PARAM_GENERIC_MSIX_VEC_PER_PF_MIN_NAME "msix_vec_per_pf_min"
428 #define DEVLINK_PARAM_GENERIC_MSIX_VEC_PER_PF_MIN_TYPE DEVLINK_PARAM_TYPE_U32
429
430 #define DEVLINK_PARAM_GENERIC_FW_LOAD_POLICY_NAME "fw_load_policy"
431 #define DEVLINK_PARAM_GENERIC_FW_LOAD_POLICY_TYPE DEVLINK_PARAM_TYPE_U8
432
433 #define DEVLINK_PARAM_GENERIC_RESET_DEV_ON_DRV_PROBE_NAME \
434         "reset_dev_on_drv_probe"
435 #define DEVLINK_PARAM_GENERIC_RESET_DEV_ON_DRV_PROBE_TYPE DEVLINK_PARAM_TYPE_U8
436
437 #define DEVLINK_PARAM_GENERIC(_id, _cmodes, _get, _set, _validate)      \
438 {                                                                       \
439         .id = DEVLINK_PARAM_GENERIC_ID_##_id,                           \
440         .name = DEVLINK_PARAM_GENERIC_##_id##_NAME,                     \
441         .type = DEVLINK_PARAM_GENERIC_##_id##_TYPE,                     \
442         .generic = true,                                                \
443         .supported_cmodes = _cmodes,                                    \
444         .get = _get,                                                    \
445         .set = _set,                                                    \
446         .validate = _validate,                                          \
447 }
448
449 #define DEVLINK_PARAM_DRIVER(_id, _name, _type, _cmodes, _get, _set, _validate) \
450 {                                                                       \
451         .id = _id,                                                      \
452         .name = _name,                                                  \
453         .type = _type,                                                  \
454         .supported_cmodes = _cmodes,                                    \
455         .get = _get,                                                    \
456         .set = _set,                                                    \
457         .validate = _validate,                                          \
458 }
459
460 /* Part number, identifier of board design */
461 #define DEVLINK_INFO_VERSION_GENERIC_BOARD_ID   "board.id"
462 /* Revision of board design */
463 #define DEVLINK_INFO_VERSION_GENERIC_BOARD_REV  "board.rev"
464 /* Maker of the board */
465 #define DEVLINK_INFO_VERSION_GENERIC_BOARD_MANUFACTURE  "board.manufacture"
466
467 /* Part number, identifier of asic design */
468 #define DEVLINK_INFO_VERSION_GENERIC_ASIC_ID    "asic.id"
469 /* Revision of asic design */
470 #define DEVLINK_INFO_VERSION_GENERIC_ASIC_REV   "asic.rev"
471
472 /* Overall FW version */
473 #define DEVLINK_INFO_VERSION_GENERIC_FW         "fw"
474 /* Control processor FW version */
475 #define DEVLINK_INFO_VERSION_GENERIC_FW_MGMT    "fw.mgmt"
476 /* Data path microcode controlling high-speed packet processing */
477 #define DEVLINK_INFO_VERSION_GENERIC_FW_APP     "fw.app"
478 /* UNDI software version */
479 #define DEVLINK_INFO_VERSION_GENERIC_FW_UNDI    "fw.undi"
480 /* NCSI support/handler version */
481 #define DEVLINK_INFO_VERSION_GENERIC_FW_NCSI    "fw.ncsi"
482
483 struct devlink_region;
484 struct devlink_info_req;
485
486 typedef void devlink_snapshot_data_dest_t(const void *data);
487
488 struct devlink_fmsg;
489 struct devlink_health_reporter;
490
491 enum devlink_health_reporter_state {
492         DEVLINK_HEALTH_REPORTER_STATE_HEALTHY,
493         DEVLINK_HEALTH_REPORTER_STATE_ERROR,
494 };
495
496 /**
497  * struct devlink_health_reporter_ops - Reporter operations
498  * @name: reporter name
499  * @recover: callback to recover from reported error
500  *           if priv_ctx is NULL, run a full recover
501  * @dump: callback to dump an object
502  *        if priv_ctx is NULL, run a full dump
503  * @diagnose: callback to diagnose the current status
504  */
505
506 struct devlink_health_reporter_ops {
507         char *name;
508         int (*recover)(struct devlink_health_reporter *reporter,
509                        void *priv_ctx);
510         int (*dump)(struct devlink_health_reporter *reporter,
511                     struct devlink_fmsg *fmsg, void *priv_ctx);
512         int (*diagnose)(struct devlink_health_reporter *reporter,
513                         struct devlink_fmsg *fmsg);
514 };
515
516 /**
517  * struct devlink_trap_group - Immutable packet trap group attributes.
518  * @name: Trap group name.
519  * @id: Trap group identifier.
520  * @generic: Whether the trap group is generic or not.
521  *
522  * Describes immutable attributes of packet trap groups that drivers register
523  * with devlink.
524  */
525 struct devlink_trap_group {
526         const char *name;
527         u16 id;
528         bool generic;
529 };
530
531 #define DEVLINK_TRAP_METADATA_TYPE_F_IN_PORT    BIT(0)
532
533 /**
534  * struct devlink_trap - Immutable packet trap attributes.
535  * @type: Trap type.
536  * @init_action: Initial trap action.
537  * @generic: Whether the trap is generic or not.
538  * @id: Trap identifier.
539  * @name: Trap name.
540  * @group: Immutable packet trap group attributes.
541  * @metadata_cap: Metadata types that can be provided by the trap.
542  *
543  * Describes immutable attributes of packet traps that drivers register with
544  * devlink.
545  */
546 struct devlink_trap {
547         enum devlink_trap_type type;
548         enum devlink_trap_action init_action;
549         bool generic;
550         u16 id;
551         const char *name;
552         struct devlink_trap_group group;
553         u32 metadata_cap;
554 };
555
556 /* All traps must be documented in
557  * Documentation/networking/devlink-trap.rst
558  */
559 enum devlink_trap_generic_id {
560         DEVLINK_TRAP_GENERIC_ID_SMAC_MC,
561         DEVLINK_TRAP_GENERIC_ID_VLAN_TAG_MISMATCH,
562         DEVLINK_TRAP_GENERIC_ID_INGRESS_VLAN_FILTER,
563         DEVLINK_TRAP_GENERIC_ID_INGRESS_STP_FILTER,
564         DEVLINK_TRAP_GENERIC_ID_EMPTY_TX_LIST,
565         DEVLINK_TRAP_GENERIC_ID_PORT_LOOPBACK_FILTER,
566         DEVLINK_TRAP_GENERIC_ID_BLACKHOLE_ROUTE,
567         DEVLINK_TRAP_GENERIC_ID_TTL_ERROR,
568         DEVLINK_TRAP_GENERIC_ID_TAIL_DROP,
569
570         /* Add new generic trap IDs above */
571         __DEVLINK_TRAP_GENERIC_ID_MAX,
572         DEVLINK_TRAP_GENERIC_ID_MAX = __DEVLINK_TRAP_GENERIC_ID_MAX - 1,
573 };
574
575 /* All trap groups must be documented in
576  * Documentation/networking/devlink-trap.rst
577  */
578 enum devlink_trap_group_generic_id {
579         DEVLINK_TRAP_GROUP_GENERIC_ID_L2_DROPS,
580         DEVLINK_TRAP_GROUP_GENERIC_ID_L3_DROPS,
581         DEVLINK_TRAP_GROUP_GENERIC_ID_BUFFER_DROPS,
582
583         /* Add new generic trap group IDs above */
584         __DEVLINK_TRAP_GROUP_GENERIC_ID_MAX,
585         DEVLINK_TRAP_GROUP_GENERIC_ID_MAX =
586                 __DEVLINK_TRAP_GROUP_GENERIC_ID_MAX - 1,
587 };
588
589 #define DEVLINK_TRAP_GENERIC_NAME_SMAC_MC \
590         "source_mac_is_multicast"
591 #define DEVLINK_TRAP_GENERIC_NAME_VLAN_TAG_MISMATCH \
592         "vlan_tag_mismatch"
593 #define DEVLINK_TRAP_GENERIC_NAME_INGRESS_VLAN_FILTER \
594         "ingress_vlan_filter"
595 #define DEVLINK_TRAP_GENERIC_NAME_INGRESS_STP_FILTER \
596         "ingress_spanning_tree_filter"
597 #define DEVLINK_TRAP_GENERIC_NAME_EMPTY_TX_LIST \
598         "port_list_is_empty"
599 #define DEVLINK_TRAP_GENERIC_NAME_PORT_LOOPBACK_FILTER \
600         "port_loopback_filter"
601 #define DEVLINK_TRAP_GENERIC_NAME_BLACKHOLE_ROUTE \
602         "blackhole_route"
603 #define DEVLINK_TRAP_GENERIC_NAME_TTL_ERROR \
604         "ttl_value_is_too_small"
605 #define DEVLINK_TRAP_GENERIC_NAME_TAIL_DROP \
606         "tail_drop"
607
608 #define DEVLINK_TRAP_GROUP_GENERIC_NAME_L2_DROPS \
609         "l2_drops"
610 #define DEVLINK_TRAP_GROUP_GENERIC_NAME_L3_DROPS \
611         "l3_drops"
612 #define DEVLINK_TRAP_GROUP_GENERIC_NAME_BUFFER_DROPS \
613         "buffer_drops"
614
615 #define DEVLINK_TRAP_GENERIC(_type, _init_action, _id, _group, _metadata_cap) \
616         {                                                                     \
617                 .type = DEVLINK_TRAP_TYPE_##_type,                            \
618                 .init_action = DEVLINK_TRAP_ACTION_##_init_action,            \
619                 .generic = true,                                              \
620                 .id = DEVLINK_TRAP_GENERIC_ID_##_id,                          \
621                 .name = DEVLINK_TRAP_GENERIC_NAME_##_id,                      \
622                 .group = _group,                                              \
623                 .metadata_cap = _metadata_cap,                                \
624         }
625
626 #define DEVLINK_TRAP_DRIVER(_type, _init_action, _id, _name, _group,          \
627                             _metadata_cap)                                    \
628         {                                                                     \
629                 .type = DEVLINK_TRAP_TYPE_##_type,                            \
630                 .init_action = DEVLINK_TRAP_ACTION_##_init_action,            \
631                 .generic = false,                                             \
632                 .id = _id,                                                    \
633                 .name = _name,                                                \
634                 .group = _group,                                              \
635                 .metadata_cap = _metadata_cap,                                \
636         }
637
638 #define DEVLINK_TRAP_GROUP_GENERIC(_id)                                       \
639         {                                                                     \
640                 .name = DEVLINK_TRAP_GROUP_GENERIC_NAME_##_id,                \
641                 .id = DEVLINK_TRAP_GROUP_GENERIC_ID_##_id,                    \
642                 .generic = true,                                              \
643         }
644
645 struct devlink_ops {
646         int (*reload_down)(struct devlink *devlink,
647                            struct netlink_ext_ack *extack);
648         int (*reload_up)(struct devlink *devlink,
649                          struct netlink_ext_ack *extack);
650         int (*port_type_set)(struct devlink_port *devlink_port,
651                              enum devlink_port_type port_type);
652         int (*port_split)(struct devlink *devlink, unsigned int port_index,
653                           unsigned int count, struct netlink_ext_ack *extack);
654         int (*port_unsplit)(struct devlink *devlink, unsigned int port_index,
655                             struct netlink_ext_ack *extack);
656         int (*sb_pool_get)(struct devlink *devlink, unsigned int sb_index,
657                            u16 pool_index,
658                            struct devlink_sb_pool_info *pool_info);
659         int (*sb_pool_set)(struct devlink *devlink, unsigned int sb_index,
660                            u16 pool_index, u32 size,
661                            enum devlink_sb_threshold_type threshold_type,
662                            struct netlink_ext_ack *extack);
663         int (*sb_port_pool_get)(struct devlink_port *devlink_port,
664                                 unsigned int sb_index, u16 pool_index,
665                                 u32 *p_threshold);
666         int (*sb_port_pool_set)(struct devlink_port *devlink_port,
667                                 unsigned int sb_index, u16 pool_index,
668                                 u32 threshold, struct netlink_ext_ack *extack);
669         int (*sb_tc_pool_bind_get)(struct devlink_port *devlink_port,
670                                    unsigned int sb_index,
671                                    u16 tc_index,
672                                    enum devlink_sb_pool_type pool_type,
673                                    u16 *p_pool_index, u32 *p_threshold);
674         int (*sb_tc_pool_bind_set)(struct devlink_port *devlink_port,
675                                    unsigned int sb_index,
676                                    u16 tc_index,
677                                    enum devlink_sb_pool_type pool_type,
678                                    u16 pool_index, u32 threshold,
679                                    struct netlink_ext_ack *extack);
680         int (*sb_occ_snapshot)(struct devlink *devlink,
681                                unsigned int sb_index);
682         int (*sb_occ_max_clear)(struct devlink *devlink,
683                                 unsigned int sb_index);
684         int (*sb_occ_port_pool_get)(struct devlink_port *devlink_port,
685                                     unsigned int sb_index, u16 pool_index,
686                                     u32 *p_cur, u32 *p_max);
687         int (*sb_occ_tc_port_bind_get)(struct devlink_port *devlink_port,
688                                        unsigned int sb_index,
689                                        u16 tc_index,
690                                        enum devlink_sb_pool_type pool_type,
691                                        u32 *p_cur, u32 *p_max);
692
693         int (*eswitch_mode_get)(struct devlink *devlink, u16 *p_mode);
694         int (*eswitch_mode_set)(struct devlink *devlink, u16 mode,
695                                 struct netlink_ext_ack *extack);
696         int (*eswitch_inline_mode_get)(struct devlink *devlink, u8 *p_inline_mode);
697         int (*eswitch_inline_mode_set)(struct devlink *devlink, u8 inline_mode,
698                                        struct netlink_ext_ack *extack);
699         int (*eswitch_encap_mode_get)(struct devlink *devlink,
700                                       enum devlink_eswitch_encap_mode *p_encap_mode);
701         int (*eswitch_encap_mode_set)(struct devlink *devlink,
702                                       enum devlink_eswitch_encap_mode encap_mode,
703                                       struct netlink_ext_ack *extack);
704         int (*info_get)(struct devlink *devlink, struct devlink_info_req *req,
705                         struct netlink_ext_ack *extack);
706         int (*flash_update)(struct devlink *devlink, const char *file_name,
707                             const char *component,
708                             struct netlink_ext_ack *extack);
709         /**
710          * @trap_init: Trap initialization function.
711          *
712          * Should be used by device drivers to initialize the trap in the
713          * underlying device. Drivers should also store the provided trap
714          * context, so that they could efficiently pass it to
715          * devlink_trap_report() when the trap is triggered.
716          */
717         int (*trap_init)(struct devlink *devlink,
718                          const struct devlink_trap *trap, void *trap_ctx);
719         /**
720          * @trap_fini: Trap de-initialization function.
721          *
722          * Should be used by device drivers to de-initialize the trap in the
723          * underlying device.
724          */
725         void (*trap_fini)(struct devlink *devlink,
726                           const struct devlink_trap *trap, void *trap_ctx);
727         /**
728          * @trap_action_set: Trap action set function.
729          */
730         int (*trap_action_set)(struct devlink *devlink,
731                                const struct devlink_trap *trap,
732                                enum devlink_trap_action action);
733         /**
734          * @trap_group_init: Trap group initialization function.
735          *
736          * Should be used by device drivers to initialize the trap group in the
737          * underlying device.
738          */
739         int (*trap_group_init)(struct devlink *devlink,
740                                const struct devlink_trap_group *group);
741 };
742
743 static inline void *devlink_priv(struct devlink *devlink)
744 {
745         BUG_ON(!devlink);
746         return &devlink->priv;
747 }
748
749 static inline struct devlink *priv_to_devlink(void *priv)
750 {
751         BUG_ON(!priv);
752         return container_of(priv, struct devlink, priv);
753 }
754
755 static inline struct devlink_port *
756 netdev_to_devlink_port(struct net_device *dev)
757 {
758         if (dev->netdev_ops->ndo_get_devlink_port)
759                 return dev->netdev_ops->ndo_get_devlink_port(dev);
760         return NULL;
761 }
762
763 static inline struct devlink *netdev_to_devlink(struct net_device *dev)
764 {
765         struct devlink_port *devlink_port = netdev_to_devlink_port(dev);
766
767         if (devlink_port)
768                 return devlink_port->devlink;
769         return NULL;
770 }
771
772 struct ib_device;
773
774 struct devlink *devlink_alloc(const struct devlink_ops *ops, size_t priv_size);
775 int devlink_register(struct devlink *devlink, struct device *dev);
776 void devlink_unregister(struct devlink *devlink);
777 void devlink_free(struct devlink *devlink);
778 int devlink_port_register(struct devlink *devlink,
779                           struct devlink_port *devlink_port,
780                           unsigned int port_index);
781 void devlink_port_unregister(struct devlink_port *devlink_port);
782 void devlink_port_type_eth_set(struct devlink_port *devlink_port,
783                                struct net_device *netdev);
784 void devlink_port_type_ib_set(struct devlink_port *devlink_port,
785                               struct ib_device *ibdev);
786 void devlink_port_type_clear(struct devlink_port *devlink_port);
787 void devlink_port_attrs_set(struct devlink_port *devlink_port,
788                             enum devlink_port_flavour flavour,
789                             u32 port_number, bool split,
790                             u32 split_subport_number,
791                             const unsigned char *switch_id,
792                             unsigned char switch_id_len);
793 void devlink_port_attrs_pci_pf_set(struct devlink_port *devlink_port,
794                                    const unsigned char *switch_id,
795                                    unsigned char switch_id_len, u16 pf);
796 void devlink_port_attrs_pci_vf_set(struct devlink_port *devlink_port,
797                                    const unsigned char *switch_id,
798                                    unsigned char switch_id_len,
799                                    u16 pf, u16 vf);
800 int devlink_sb_register(struct devlink *devlink, unsigned int sb_index,
801                         u32 size, u16 ingress_pools_count,
802                         u16 egress_pools_count, u16 ingress_tc_count,
803                         u16 egress_tc_count);
804 void devlink_sb_unregister(struct devlink *devlink, unsigned int sb_index);
805 int devlink_dpipe_table_register(struct devlink *devlink,
806                                  const char *table_name,
807                                  struct devlink_dpipe_table_ops *table_ops,
808                                  void *priv, bool counter_control_extern);
809 void devlink_dpipe_table_unregister(struct devlink *devlink,
810                                     const char *table_name);
811 int devlink_dpipe_headers_register(struct devlink *devlink,
812                                    struct devlink_dpipe_headers *dpipe_headers);
813 void devlink_dpipe_headers_unregister(struct devlink *devlink);
814 bool devlink_dpipe_table_counter_enabled(struct devlink *devlink,
815                                          const char *table_name);
816 int devlink_dpipe_entry_ctx_prepare(struct devlink_dpipe_dump_ctx *dump_ctx);
817 int devlink_dpipe_entry_ctx_append(struct devlink_dpipe_dump_ctx *dump_ctx,
818                                    struct devlink_dpipe_entry *entry);
819 int devlink_dpipe_entry_ctx_close(struct devlink_dpipe_dump_ctx *dump_ctx);
820 void devlink_dpipe_entry_clear(struct devlink_dpipe_entry *entry);
821 int devlink_dpipe_action_put(struct sk_buff *skb,
822                              struct devlink_dpipe_action *action);
823 int devlink_dpipe_match_put(struct sk_buff *skb,
824                             struct devlink_dpipe_match *match);
825 extern struct devlink_dpipe_header devlink_dpipe_header_ethernet;
826 extern struct devlink_dpipe_header devlink_dpipe_header_ipv4;
827 extern struct devlink_dpipe_header devlink_dpipe_header_ipv6;
828
829 int devlink_resource_register(struct devlink *devlink,
830                               const char *resource_name,
831                               u64 resource_size,
832                               u64 resource_id,
833                               u64 parent_resource_id,
834                               const struct devlink_resource_size_params *size_params);
835 void devlink_resources_unregister(struct devlink *devlink,
836                                   struct devlink_resource *resource);
837 int devlink_resource_size_get(struct devlink *devlink,
838                               u64 resource_id,
839                               u64 *p_resource_size);
840 int devlink_dpipe_table_resource_set(struct devlink *devlink,
841                                      const char *table_name, u64 resource_id,
842                                      u64 resource_units);
843 void devlink_resource_occ_get_register(struct devlink *devlink,
844                                        u64 resource_id,
845                                        devlink_resource_occ_get_t *occ_get,
846                                        void *occ_get_priv);
847 void devlink_resource_occ_get_unregister(struct devlink *devlink,
848                                          u64 resource_id);
849 int devlink_params_register(struct devlink *devlink,
850                             const struct devlink_param *params,
851                             size_t params_count);
852 void devlink_params_unregister(struct devlink *devlink,
853                                const struct devlink_param *params,
854                                size_t params_count);
855 void devlink_params_publish(struct devlink *devlink);
856 void devlink_params_unpublish(struct devlink *devlink);
857 int devlink_port_params_register(struct devlink_port *devlink_port,
858                                  const struct devlink_param *params,
859                                  size_t params_count);
860 void devlink_port_params_unregister(struct devlink_port *devlink_port,
861                                     const struct devlink_param *params,
862                                     size_t params_count);
863 int devlink_param_driverinit_value_get(struct devlink *devlink, u32 param_id,
864                                        union devlink_param_value *init_val);
865 int devlink_param_driverinit_value_set(struct devlink *devlink, u32 param_id,
866                                        union devlink_param_value init_val);
867 int
868 devlink_port_param_driverinit_value_get(struct devlink_port *devlink_port,
869                                         u32 param_id,
870                                         union devlink_param_value *init_val);
871 int devlink_port_param_driverinit_value_set(struct devlink_port *devlink_port,
872                                             u32 param_id,
873                                             union devlink_param_value init_val);
874 void devlink_param_value_changed(struct devlink *devlink, u32 param_id);
875 void devlink_port_param_value_changed(struct devlink_port *devlink_port,
876                                       u32 param_id);
877 void devlink_param_value_str_fill(union devlink_param_value *dst_val,
878                                   const char *src);
879 struct devlink_region *devlink_region_create(struct devlink *devlink,
880                                              const char *region_name,
881                                              u32 region_max_snapshots,
882                                              u64 region_size);
883 void devlink_region_destroy(struct devlink_region *region);
884 u32 devlink_region_shapshot_id_get(struct devlink *devlink);
885 int devlink_region_snapshot_create(struct devlink_region *region,
886                                    u8 *data, u32 snapshot_id,
887                                    devlink_snapshot_data_dest_t *data_destructor);
888 int devlink_info_serial_number_put(struct devlink_info_req *req,
889                                    const char *sn);
890 int devlink_info_driver_name_put(struct devlink_info_req *req,
891                                  const char *name);
892 int devlink_info_version_fixed_put(struct devlink_info_req *req,
893                                    const char *version_name,
894                                    const char *version_value);
895 int devlink_info_version_stored_put(struct devlink_info_req *req,
896                                     const char *version_name,
897                                     const char *version_value);
898 int devlink_info_version_running_put(struct devlink_info_req *req,
899                                      const char *version_name,
900                                      const char *version_value);
901
902 int devlink_fmsg_obj_nest_start(struct devlink_fmsg *fmsg);
903 int devlink_fmsg_obj_nest_end(struct devlink_fmsg *fmsg);
904
905 int devlink_fmsg_pair_nest_start(struct devlink_fmsg *fmsg, const char *name);
906 int devlink_fmsg_pair_nest_end(struct devlink_fmsg *fmsg);
907
908 int devlink_fmsg_arr_pair_nest_start(struct devlink_fmsg *fmsg,
909                                      const char *name);
910 int devlink_fmsg_arr_pair_nest_end(struct devlink_fmsg *fmsg);
911
912 int devlink_fmsg_bool_put(struct devlink_fmsg *fmsg, bool value);
913 int devlink_fmsg_u8_put(struct devlink_fmsg *fmsg, u8 value);
914 int devlink_fmsg_u32_put(struct devlink_fmsg *fmsg, u32 value);
915 int devlink_fmsg_u64_put(struct devlink_fmsg *fmsg, u64 value);
916 int devlink_fmsg_string_put(struct devlink_fmsg *fmsg, const char *value);
917 int devlink_fmsg_binary_put(struct devlink_fmsg *fmsg, const void *value,
918                             u16 value_len);
919
920 int devlink_fmsg_bool_pair_put(struct devlink_fmsg *fmsg, const char *name,
921                                bool value);
922 int devlink_fmsg_u8_pair_put(struct devlink_fmsg *fmsg, const char *name,
923                              u8 value);
924 int devlink_fmsg_u32_pair_put(struct devlink_fmsg *fmsg, const char *name,
925                               u32 value);
926 int devlink_fmsg_u64_pair_put(struct devlink_fmsg *fmsg, const char *name,
927                               u64 value);
928 int devlink_fmsg_string_pair_put(struct devlink_fmsg *fmsg, const char *name,
929                                  const char *value);
930 int devlink_fmsg_binary_pair_put(struct devlink_fmsg *fmsg, const char *name,
931                                  const void *value, u16 value_len);
932
933 struct devlink_health_reporter *
934 devlink_health_reporter_create(struct devlink *devlink,
935                                const struct devlink_health_reporter_ops *ops,
936                                u64 graceful_period, bool auto_recover,
937                                void *priv);
938 void
939 devlink_health_reporter_destroy(struct devlink_health_reporter *reporter);
940
941 void *
942 devlink_health_reporter_priv(struct devlink_health_reporter *reporter);
943 int devlink_health_report(struct devlink_health_reporter *reporter,
944                           const char *msg, void *priv_ctx);
945 void
946 devlink_health_reporter_state_update(struct devlink_health_reporter *reporter,
947                                      enum devlink_health_reporter_state state);
948
949 bool devlink_is_reload_failed(const struct devlink *devlink);
950
951 void devlink_flash_update_begin_notify(struct devlink *devlink);
952 void devlink_flash_update_end_notify(struct devlink *devlink);
953 void devlink_flash_update_status_notify(struct devlink *devlink,
954                                         const char *status_msg,
955                                         const char *component,
956                                         unsigned long done,
957                                         unsigned long total);
958
959 int devlink_traps_register(struct devlink *devlink,
960                            const struct devlink_trap *traps,
961                            size_t traps_count, void *priv);
962 void devlink_traps_unregister(struct devlink *devlink,
963                               const struct devlink_trap *traps,
964                               size_t traps_count);
965 void devlink_trap_report(struct devlink *devlink,
966                          struct sk_buff *skb, void *trap_ctx,
967                          struct devlink_port *in_devlink_port);
968 void *devlink_trap_ctx_priv(void *trap_ctx);
969
970 #if IS_ENABLED(CONFIG_NET_DEVLINK)
971
972 void devlink_compat_running_version(struct net_device *dev,
973                                     char *buf, size_t len);
974 int devlink_compat_flash_update(struct net_device *dev, const char *file_name);
975 int devlink_compat_phys_port_name_get(struct net_device *dev,
976                                       char *name, size_t len);
977 int devlink_compat_switch_id_get(struct net_device *dev,
978                                  struct netdev_phys_item_id *ppid);
979
980 #else
981
982 static inline void
983 devlink_compat_running_version(struct net_device *dev, char *buf, size_t len)
984 {
985 }
986
987 static inline int
988 devlink_compat_flash_update(struct net_device *dev, const char *file_name)
989 {
990         return -EOPNOTSUPP;
991 }
992
993 static inline int
994 devlink_compat_phys_port_name_get(struct net_device *dev,
995                                   char *name, size_t len)
996 {
997         return -EOPNOTSUPP;
998 }
999
1000 static inline int
1001 devlink_compat_switch_id_get(struct net_device *dev,
1002                              struct netdev_phys_item_id *ppid)
1003 {
1004         return -EOPNOTSUPP;
1005 }
1006
1007 #endif
1008
1009 #endif /* _NET_DEVLINK_H_ */