xref: /linux-6.15/include/net/devlink.h (revision 3aac1ead)
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 <[email protected]>
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_flash_notify - devlink dev flash notify data
396  * @status_msg: current status string
397  * @component: firmware component being updated
398  * @done: amount of work completed of total amount
399  * @total: amount of work expected to be done
400  * @timeout: expected max timeout in seconds
401  *
402  * These are values to be given to userland to be displayed in order
403  * to show current activity in a firmware update process.
404  */
405 struct devlink_flash_notify {
406 	const char *status_msg;
407 	const char *component;
408 	unsigned long done;
409 	unsigned long total;
410 	unsigned long timeout;
411 };
412 
413 /**
414  * struct devlink_param - devlink configuration parameter data
415  * @name: name of the parameter
416  * @generic: indicates if the parameter is generic or driver specific
417  * @type: parameter type
418  * @supported_cmodes: bitmap of supported configuration modes
419  * @get: get parameter value, used for runtime and permanent
420  *       configuration modes
421  * @set: set parameter value, used for runtime and permanent
422  *       configuration modes
423  * @validate: validate input value is applicable (within value range, etc.)
424  *
425  * This struct should be used by the driver to fill the data for
426  * a parameter it registers.
427  */
428 struct devlink_param {
429 	u32 id;
430 	const char *name;
431 	bool generic;
432 	enum devlink_param_type type;
433 	unsigned long supported_cmodes;
434 	int (*get)(struct devlink *devlink, u32 id,
435 		   struct devlink_param_gset_ctx *ctx);
436 	int (*set)(struct devlink *devlink, u32 id,
437 		   struct devlink_param_gset_ctx *ctx);
438 	int (*validate)(struct devlink *devlink, u32 id,
439 			union devlink_param_value val,
440 			struct netlink_ext_ack *extack);
441 };
442 
443 struct devlink_param_item {
444 	struct list_head list;
445 	const struct devlink_param *param;
446 	union devlink_param_value driverinit_value;
447 	bool driverinit_value_valid;
448 	bool published;
449 };
450 
451 enum devlink_param_generic_id {
452 	DEVLINK_PARAM_GENERIC_ID_INT_ERR_RESET,
453 	DEVLINK_PARAM_GENERIC_ID_MAX_MACS,
454 	DEVLINK_PARAM_GENERIC_ID_ENABLE_SRIOV,
455 	DEVLINK_PARAM_GENERIC_ID_REGION_SNAPSHOT,
456 	DEVLINK_PARAM_GENERIC_ID_IGNORE_ARI,
457 	DEVLINK_PARAM_GENERIC_ID_MSIX_VEC_PER_PF_MAX,
458 	DEVLINK_PARAM_GENERIC_ID_MSIX_VEC_PER_PF_MIN,
459 	DEVLINK_PARAM_GENERIC_ID_FW_LOAD_POLICY,
460 	DEVLINK_PARAM_GENERIC_ID_RESET_DEV_ON_DRV_PROBE,
461 	DEVLINK_PARAM_GENERIC_ID_ENABLE_ROCE,
462 
463 	/* add new param generic ids above here*/
464 	__DEVLINK_PARAM_GENERIC_ID_MAX,
465 	DEVLINK_PARAM_GENERIC_ID_MAX = __DEVLINK_PARAM_GENERIC_ID_MAX - 1,
466 };
467 
468 #define DEVLINK_PARAM_GENERIC_INT_ERR_RESET_NAME "internal_error_reset"
469 #define DEVLINK_PARAM_GENERIC_INT_ERR_RESET_TYPE DEVLINK_PARAM_TYPE_BOOL
470 
471 #define DEVLINK_PARAM_GENERIC_MAX_MACS_NAME "max_macs"
472 #define DEVLINK_PARAM_GENERIC_MAX_MACS_TYPE DEVLINK_PARAM_TYPE_U32
473 
474 #define DEVLINK_PARAM_GENERIC_ENABLE_SRIOV_NAME "enable_sriov"
475 #define DEVLINK_PARAM_GENERIC_ENABLE_SRIOV_TYPE DEVLINK_PARAM_TYPE_BOOL
476 
477 #define DEVLINK_PARAM_GENERIC_REGION_SNAPSHOT_NAME "region_snapshot_enable"
478 #define DEVLINK_PARAM_GENERIC_REGION_SNAPSHOT_TYPE DEVLINK_PARAM_TYPE_BOOL
479 
480 #define DEVLINK_PARAM_GENERIC_IGNORE_ARI_NAME "ignore_ari"
481 #define DEVLINK_PARAM_GENERIC_IGNORE_ARI_TYPE DEVLINK_PARAM_TYPE_BOOL
482 
483 #define DEVLINK_PARAM_GENERIC_MSIX_VEC_PER_PF_MAX_NAME "msix_vec_per_pf_max"
484 #define DEVLINK_PARAM_GENERIC_MSIX_VEC_PER_PF_MAX_TYPE DEVLINK_PARAM_TYPE_U32
485 
486 #define DEVLINK_PARAM_GENERIC_MSIX_VEC_PER_PF_MIN_NAME "msix_vec_per_pf_min"
487 #define DEVLINK_PARAM_GENERIC_MSIX_VEC_PER_PF_MIN_TYPE DEVLINK_PARAM_TYPE_U32
488 
489 #define DEVLINK_PARAM_GENERIC_FW_LOAD_POLICY_NAME "fw_load_policy"
490 #define DEVLINK_PARAM_GENERIC_FW_LOAD_POLICY_TYPE DEVLINK_PARAM_TYPE_U8
491 
492 #define DEVLINK_PARAM_GENERIC_RESET_DEV_ON_DRV_PROBE_NAME \
493 	"reset_dev_on_drv_probe"
494 #define DEVLINK_PARAM_GENERIC_RESET_DEV_ON_DRV_PROBE_TYPE DEVLINK_PARAM_TYPE_U8
495 
496 #define DEVLINK_PARAM_GENERIC_ENABLE_ROCE_NAME "enable_roce"
497 #define DEVLINK_PARAM_GENERIC_ENABLE_ROCE_TYPE DEVLINK_PARAM_TYPE_BOOL
498 
499 #define DEVLINK_PARAM_GENERIC(_id, _cmodes, _get, _set, _validate)	\
500 {									\
501 	.id = DEVLINK_PARAM_GENERIC_ID_##_id,				\
502 	.name = DEVLINK_PARAM_GENERIC_##_id##_NAME,			\
503 	.type = DEVLINK_PARAM_GENERIC_##_id##_TYPE,			\
504 	.generic = true,						\
505 	.supported_cmodes = _cmodes,					\
506 	.get = _get,							\
507 	.set = _set,							\
508 	.validate = _validate,						\
509 }
510 
511 #define DEVLINK_PARAM_DRIVER(_id, _name, _type, _cmodes, _get, _set, _validate)	\
512 {									\
513 	.id = _id,							\
514 	.name = _name,							\
515 	.type = _type,							\
516 	.supported_cmodes = _cmodes,					\
517 	.get = _get,							\
518 	.set = _set,							\
519 	.validate = _validate,						\
520 }
521 
522 /* Part number, identifier of board design */
523 #define DEVLINK_INFO_VERSION_GENERIC_BOARD_ID	"board.id"
524 /* Revision of board design */
525 #define DEVLINK_INFO_VERSION_GENERIC_BOARD_REV	"board.rev"
526 /* Maker of the board */
527 #define DEVLINK_INFO_VERSION_GENERIC_BOARD_MANUFACTURE	"board.manufacture"
528 
529 /* Part number, identifier of asic design */
530 #define DEVLINK_INFO_VERSION_GENERIC_ASIC_ID	"asic.id"
531 /* Revision of asic design */
532 #define DEVLINK_INFO_VERSION_GENERIC_ASIC_REV	"asic.rev"
533 
534 /* Overall FW version */
535 #define DEVLINK_INFO_VERSION_GENERIC_FW		"fw"
536 /* Control processor FW version */
537 #define DEVLINK_INFO_VERSION_GENERIC_FW_MGMT	"fw.mgmt"
538 /* FW interface specification version */
539 #define DEVLINK_INFO_VERSION_GENERIC_FW_MGMT_API	"fw.mgmt.api"
540 /* Data path microcode controlling high-speed packet processing */
541 #define DEVLINK_INFO_VERSION_GENERIC_FW_APP	"fw.app"
542 /* UNDI software version */
543 #define DEVLINK_INFO_VERSION_GENERIC_FW_UNDI	"fw.undi"
544 /* NCSI support/handler version */
545 #define DEVLINK_INFO_VERSION_GENERIC_FW_NCSI	"fw.ncsi"
546 /* FW parameter set id */
547 #define DEVLINK_INFO_VERSION_GENERIC_FW_PSID	"fw.psid"
548 /* RoCE FW version */
549 #define DEVLINK_INFO_VERSION_GENERIC_FW_ROCE	"fw.roce"
550 /* Firmware bundle identifier */
551 #define DEVLINK_INFO_VERSION_GENERIC_FW_BUNDLE_ID	"fw.bundle_id"
552 
553 struct devlink_region;
554 struct devlink_info_req;
555 
556 /**
557  * struct devlink_region_ops - Region operations
558  * @name: region name
559  * @destructor: callback used to free snapshot memory when deleting
560  * @snapshot: callback to request an immediate snapshot. On success,
561  *            the data variable must be updated to point to the snapshot data.
562  *            The function will be called while the devlink instance lock is
563  *            held.
564  * @priv: Pointer to driver private data for the region operation
565  */
566 struct devlink_region_ops {
567 	const char *name;
568 	void (*destructor)(const void *data);
569 	int (*snapshot)(struct devlink *devlink,
570 			const struct devlink_region_ops *ops,
571 			struct netlink_ext_ack *extack,
572 			u8 **data);
573 	void *priv;
574 };
575 
576 struct devlink_fmsg;
577 struct devlink_health_reporter;
578 
579 enum devlink_health_reporter_state {
580 	DEVLINK_HEALTH_REPORTER_STATE_HEALTHY,
581 	DEVLINK_HEALTH_REPORTER_STATE_ERROR,
582 };
583 
584 /**
585  * struct devlink_health_reporter_ops - Reporter operations
586  * @name: reporter name
587  * @recover: callback to recover from reported error
588  *           if priv_ctx is NULL, run a full recover
589  * @dump: callback to dump an object
590  *        if priv_ctx is NULL, run a full dump
591  * @diagnose: callback to diagnose the current status
592  * @test: callback to trigger a test event
593  */
594 
595 struct devlink_health_reporter_ops {
596 	char *name;
597 	int (*recover)(struct devlink_health_reporter *reporter,
598 		       void *priv_ctx, struct netlink_ext_ack *extack);
599 	int (*dump)(struct devlink_health_reporter *reporter,
600 		    struct devlink_fmsg *fmsg, void *priv_ctx,
601 		    struct netlink_ext_ack *extack);
602 	int (*diagnose)(struct devlink_health_reporter *reporter,
603 			struct devlink_fmsg *fmsg,
604 			struct netlink_ext_ack *extack);
605 	int (*test)(struct devlink_health_reporter *reporter,
606 		    struct netlink_ext_ack *extack);
607 };
608 
609 /**
610  * struct devlink_trap_policer - Immutable packet trap policer attributes.
611  * @id: Policer identifier.
612  * @init_rate: Initial rate in packets / sec.
613  * @init_burst: Initial burst size in packets.
614  * @max_rate: Maximum rate.
615  * @min_rate: Minimum rate.
616  * @max_burst: Maximum burst size.
617  * @min_burst: Minimum burst size.
618  *
619  * Describes immutable attributes of packet trap policers that drivers register
620  * with devlink.
621  */
622 struct devlink_trap_policer {
623 	u32 id;
624 	u64 init_rate;
625 	u64 init_burst;
626 	u64 max_rate;
627 	u64 min_rate;
628 	u64 max_burst;
629 	u64 min_burst;
630 };
631 
632 /**
633  * struct devlink_trap_group - Immutable packet trap group attributes.
634  * @name: Trap group name.
635  * @id: Trap group identifier.
636  * @generic: Whether the trap group is generic or not.
637  * @init_policer_id: Initial policer identifier.
638  *
639  * Describes immutable attributes of packet trap groups that drivers register
640  * with devlink.
641  */
642 struct devlink_trap_group {
643 	const char *name;
644 	u16 id;
645 	bool generic;
646 	u32 init_policer_id;
647 };
648 
649 #define DEVLINK_TRAP_METADATA_TYPE_F_IN_PORT	BIT(0)
650 #define DEVLINK_TRAP_METADATA_TYPE_F_FA_COOKIE	BIT(1)
651 
652 /**
653  * struct devlink_trap - Immutable packet trap attributes.
654  * @type: Trap type.
655  * @init_action: Initial trap action.
656  * @generic: Whether the trap is generic or not.
657  * @id: Trap identifier.
658  * @name: Trap name.
659  * @init_group_id: Initial group identifier.
660  * @metadata_cap: Metadata types that can be provided by the trap.
661  *
662  * Describes immutable attributes of packet traps that drivers register with
663  * devlink.
664  */
665 struct devlink_trap {
666 	enum devlink_trap_type type;
667 	enum devlink_trap_action init_action;
668 	bool generic;
669 	u16 id;
670 	const char *name;
671 	u16 init_group_id;
672 	u32 metadata_cap;
673 };
674 
675 /* All traps must be documented in
676  * Documentation/networking/devlink/devlink-trap.rst
677  */
678 enum devlink_trap_generic_id {
679 	DEVLINK_TRAP_GENERIC_ID_SMAC_MC,
680 	DEVLINK_TRAP_GENERIC_ID_VLAN_TAG_MISMATCH,
681 	DEVLINK_TRAP_GENERIC_ID_INGRESS_VLAN_FILTER,
682 	DEVLINK_TRAP_GENERIC_ID_INGRESS_STP_FILTER,
683 	DEVLINK_TRAP_GENERIC_ID_EMPTY_TX_LIST,
684 	DEVLINK_TRAP_GENERIC_ID_PORT_LOOPBACK_FILTER,
685 	DEVLINK_TRAP_GENERIC_ID_BLACKHOLE_ROUTE,
686 	DEVLINK_TRAP_GENERIC_ID_TTL_ERROR,
687 	DEVLINK_TRAP_GENERIC_ID_TAIL_DROP,
688 	DEVLINK_TRAP_GENERIC_ID_NON_IP_PACKET,
689 	DEVLINK_TRAP_GENERIC_ID_UC_DIP_MC_DMAC,
690 	DEVLINK_TRAP_GENERIC_ID_DIP_LB,
691 	DEVLINK_TRAP_GENERIC_ID_SIP_MC,
692 	DEVLINK_TRAP_GENERIC_ID_SIP_LB,
693 	DEVLINK_TRAP_GENERIC_ID_CORRUPTED_IP_HDR,
694 	DEVLINK_TRAP_GENERIC_ID_IPV4_SIP_BC,
695 	DEVLINK_TRAP_GENERIC_ID_IPV6_MC_DIP_RESERVED_SCOPE,
696 	DEVLINK_TRAP_GENERIC_ID_IPV6_MC_DIP_INTERFACE_LOCAL_SCOPE,
697 	DEVLINK_TRAP_GENERIC_ID_MTU_ERROR,
698 	DEVLINK_TRAP_GENERIC_ID_UNRESOLVED_NEIGH,
699 	DEVLINK_TRAP_GENERIC_ID_RPF,
700 	DEVLINK_TRAP_GENERIC_ID_REJECT_ROUTE,
701 	DEVLINK_TRAP_GENERIC_ID_IPV4_LPM_UNICAST_MISS,
702 	DEVLINK_TRAP_GENERIC_ID_IPV6_LPM_UNICAST_MISS,
703 	DEVLINK_TRAP_GENERIC_ID_NON_ROUTABLE,
704 	DEVLINK_TRAP_GENERIC_ID_DECAP_ERROR,
705 	DEVLINK_TRAP_GENERIC_ID_OVERLAY_SMAC_MC,
706 	DEVLINK_TRAP_GENERIC_ID_INGRESS_FLOW_ACTION_DROP,
707 	DEVLINK_TRAP_GENERIC_ID_EGRESS_FLOW_ACTION_DROP,
708 	DEVLINK_TRAP_GENERIC_ID_STP,
709 	DEVLINK_TRAP_GENERIC_ID_LACP,
710 	DEVLINK_TRAP_GENERIC_ID_LLDP,
711 	DEVLINK_TRAP_GENERIC_ID_IGMP_QUERY,
712 	DEVLINK_TRAP_GENERIC_ID_IGMP_V1_REPORT,
713 	DEVLINK_TRAP_GENERIC_ID_IGMP_V2_REPORT,
714 	DEVLINK_TRAP_GENERIC_ID_IGMP_V3_REPORT,
715 	DEVLINK_TRAP_GENERIC_ID_IGMP_V2_LEAVE,
716 	DEVLINK_TRAP_GENERIC_ID_MLD_QUERY,
717 	DEVLINK_TRAP_GENERIC_ID_MLD_V1_REPORT,
718 	DEVLINK_TRAP_GENERIC_ID_MLD_V2_REPORT,
719 	DEVLINK_TRAP_GENERIC_ID_MLD_V1_DONE,
720 	DEVLINK_TRAP_GENERIC_ID_IPV4_DHCP,
721 	DEVLINK_TRAP_GENERIC_ID_IPV6_DHCP,
722 	DEVLINK_TRAP_GENERIC_ID_ARP_REQUEST,
723 	DEVLINK_TRAP_GENERIC_ID_ARP_RESPONSE,
724 	DEVLINK_TRAP_GENERIC_ID_ARP_OVERLAY,
725 	DEVLINK_TRAP_GENERIC_ID_IPV6_NEIGH_SOLICIT,
726 	DEVLINK_TRAP_GENERIC_ID_IPV6_NEIGH_ADVERT,
727 	DEVLINK_TRAP_GENERIC_ID_IPV4_BFD,
728 	DEVLINK_TRAP_GENERIC_ID_IPV6_BFD,
729 	DEVLINK_TRAP_GENERIC_ID_IPV4_OSPF,
730 	DEVLINK_TRAP_GENERIC_ID_IPV6_OSPF,
731 	DEVLINK_TRAP_GENERIC_ID_IPV4_BGP,
732 	DEVLINK_TRAP_GENERIC_ID_IPV6_BGP,
733 	DEVLINK_TRAP_GENERIC_ID_IPV4_VRRP,
734 	DEVLINK_TRAP_GENERIC_ID_IPV6_VRRP,
735 	DEVLINK_TRAP_GENERIC_ID_IPV4_PIM,
736 	DEVLINK_TRAP_GENERIC_ID_IPV6_PIM,
737 	DEVLINK_TRAP_GENERIC_ID_UC_LB,
738 	DEVLINK_TRAP_GENERIC_ID_LOCAL_ROUTE,
739 	DEVLINK_TRAP_GENERIC_ID_EXTERNAL_ROUTE,
740 	DEVLINK_TRAP_GENERIC_ID_IPV6_UC_DIP_LINK_LOCAL_SCOPE,
741 	DEVLINK_TRAP_GENERIC_ID_IPV6_DIP_ALL_NODES,
742 	DEVLINK_TRAP_GENERIC_ID_IPV6_DIP_ALL_ROUTERS,
743 	DEVLINK_TRAP_GENERIC_ID_IPV6_ROUTER_SOLICIT,
744 	DEVLINK_TRAP_GENERIC_ID_IPV6_ROUTER_ADVERT,
745 	DEVLINK_TRAP_GENERIC_ID_IPV6_REDIRECT,
746 	DEVLINK_TRAP_GENERIC_ID_IPV4_ROUTER_ALERT,
747 	DEVLINK_TRAP_GENERIC_ID_IPV6_ROUTER_ALERT,
748 	DEVLINK_TRAP_GENERIC_ID_PTP_EVENT,
749 	DEVLINK_TRAP_GENERIC_ID_PTP_GENERAL,
750 	DEVLINK_TRAP_GENERIC_ID_FLOW_ACTION_SAMPLE,
751 	DEVLINK_TRAP_GENERIC_ID_FLOW_ACTION_TRAP,
752 	DEVLINK_TRAP_GENERIC_ID_EARLY_DROP,
753 
754 	/* Add new generic trap IDs above */
755 	__DEVLINK_TRAP_GENERIC_ID_MAX,
756 	DEVLINK_TRAP_GENERIC_ID_MAX = __DEVLINK_TRAP_GENERIC_ID_MAX - 1,
757 };
758 
759 /* All trap groups must be documented in
760  * Documentation/networking/devlink/devlink-trap.rst
761  */
762 enum devlink_trap_group_generic_id {
763 	DEVLINK_TRAP_GROUP_GENERIC_ID_L2_DROPS,
764 	DEVLINK_TRAP_GROUP_GENERIC_ID_L3_DROPS,
765 	DEVLINK_TRAP_GROUP_GENERIC_ID_L3_EXCEPTIONS,
766 	DEVLINK_TRAP_GROUP_GENERIC_ID_BUFFER_DROPS,
767 	DEVLINK_TRAP_GROUP_GENERIC_ID_TUNNEL_DROPS,
768 	DEVLINK_TRAP_GROUP_GENERIC_ID_ACL_DROPS,
769 	DEVLINK_TRAP_GROUP_GENERIC_ID_STP,
770 	DEVLINK_TRAP_GROUP_GENERIC_ID_LACP,
771 	DEVLINK_TRAP_GROUP_GENERIC_ID_LLDP,
772 	DEVLINK_TRAP_GROUP_GENERIC_ID_MC_SNOOPING,
773 	DEVLINK_TRAP_GROUP_GENERIC_ID_DHCP,
774 	DEVLINK_TRAP_GROUP_GENERIC_ID_NEIGH_DISCOVERY,
775 	DEVLINK_TRAP_GROUP_GENERIC_ID_BFD,
776 	DEVLINK_TRAP_GROUP_GENERIC_ID_OSPF,
777 	DEVLINK_TRAP_GROUP_GENERIC_ID_BGP,
778 	DEVLINK_TRAP_GROUP_GENERIC_ID_VRRP,
779 	DEVLINK_TRAP_GROUP_GENERIC_ID_PIM,
780 	DEVLINK_TRAP_GROUP_GENERIC_ID_UC_LB,
781 	DEVLINK_TRAP_GROUP_GENERIC_ID_LOCAL_DELIVERY,
782 	DEVLINK_TRAP_GROUP_GENERIC_ID_EXTERNAL_DELIVERY,
783 	DEVLINK_TRAP_GROUP_GENERIC_ID_IPV6,
784 	DEVLINK_TRAP_GROUP_GENERIC_ID_PTP_EVENT,
785 	DEVLINK_TRAP_GROUP_GENERIC_ID_PTP_GENERAL,
786 	DEVLINK_TRAP_GROUP_GENERIC_ID_ACL_SAMPLE,
787 	DEVLINK_TRAP_GROUP_GENERIC_ID_ACL_TRAP,
788 
789 	/* Add new generic trap group IDs above */
790 	__DEVLINK_TRAP_GROUP_GENERIC_ID_MAX,
791 	DEVLINK_TRAP_GROUP_GENERIC_ID_MAX =
792 		__DEVLINK_TRAP_GROUP_GENERIC_ID_MAX - 1,
793 };
794 
795 #define DEVLINK_TRAP_GENERIC_NAME_SMAC_MC \
796 	"source_mac_is_multicast"
797 #define DEVLINK_TRAP_GENERIC_NAME_VLAN_TAG_MISMATCH \
798 	"vlan_tag_mismatch"
799 #define DEVLINK_TRAP_GENERIC_NAME_INGRESS_VLAN_FILTER \
800 	"ingress_vlan_filter"
801 #define DEVLINK_TRAP_GENERIC_NAME_INGRESS_STP_FILTER \
802 	"ingress_spanning_tree_filter"
803 #define DEVLINK_TRAP_GENERIC_NAME_EMPTY_TX_LIST \
804 	"port_list_is_empty"
805 #define DEVLINK_TRAP_GENERIC_NAME_PORT_LOOPBACK_FILTER \
806 	"port_loopback_filter"
807 #define DEVLINK_TRAP_GENERIC_NAME_BLACKHOLE_ROUTE \
808 	"blackhole_route"
809 #define DEVLINK_TRAP_GENERIC_NAME_TTL_ERROR \
810 	"ttl_value_is_too_small"
811 #define DEVLINK_TRAP_GENERIC_NAME_TAIL_DROP \
812 	"tail_drop"
813 #define DEVLINK_TRAP_GENERIC_NAME_NON_IP_PACKET \
814 	"non_ip"
815 #define DEVLINK_TRAP_GENERIC_NAME_UC_DIP_MC_DMAC \
816 	"uc_dip_over_mc_dmac"
817 #define DEVLINK_TRAP_GENERIC_NAME_DIP_LB \
818 	"dip_is_loopback_address"
819 #define DEVLINK_TRAP_GENERIC_NAME_SIP_MC \
820 	"sip_is_mc"
821 #define DEVLINK_TRAP_GENERIC_NAME_SIP_LB \
822 	"sip_is_loopback_address"
823 #define DEVLINK_TRAP_GENERIC_NAME_CORRUPTED_IP_HDR \
824 	"ip_header_corrupted"
825 #define DEVLINK_TRAP_GENERIC_NAME_IPV4_SIP_BC \
826 	"ipv4_sip_is_limited_bc"
827 #define DEVLINK_TRAP_GENERIC_NAME_IPV6_MC_DIP_RESERVED_SCOPE \
828 	"ipv6_mc_dip_reserved_scope"
829 #define DEVLINK_TRAP_GENERIC_NAME_IPV6_MC_DIP_INTERFACE_LOCAL_SCOPE \
830 	"ipv6_mc_dip_interface_local_scope"
831 #define DEVLINK_TRAP_GENERIC_NAME_MTU_ERROR \
832 	"mtu_value_is_too_small"
833 #define DEVLINK_TRAP_GENERIC_NAME_UNRESOLVED_NEIGH \
834 	"unresolved_neigh"
835 #define DEVLINK_TRAP_GENERIC_NAME_RPF \
836 	"mc_reverse_path_forwarding"
837 #define DEVLINK_TRAP_GENERIC_NAME_REJECT_ROUTE \
838 	"reject_route"
839 #define DEVLINK_TRAP_GENERIC_NAME_IPV4_LPM_UNICAST_MISS \
840 	"ipv4_lpm_miss"
841 #define DEVLINK_TRAP_GENERIC_NAME_IPV6_LPM_UNICAST_MISS \
842 	"ipv6_lpm_miss"
843 #define DEVLINK_TRAP_GENERIC_NAME_NON_ROUTABLE \
844 	"non_routable_packet"
845 #define DEVLINK_TRAP_GENERIC_NAME_DECAP_ERROR \
846 	"decap_error"
847 #define DEVLINK_TRAP_GENERIC_NAME_OVERLAY_SMAC_MC \
848 	"overlay_smac_is_mc"
849 #define DEVLINK_TRAP_GENERIC_NAME_INGRESS_FLOW_ACTION_DROP \
850 	"ingress_flow_action_drop"
851 #define DEVLINK_TRAP_GENERIC_NAME_EGRESS_FLOW_ACTION_DROP \
852 	"egress_flow_action_drop"
853 #define DEVLINK_TRAP_GENERIC_NAME_STP \
854 	"stp"
855 #define DEVLINK_TRAP_GENERIC_NAME_LACP \
856 	"lacp"
857 #define DEVLINK_TRAP_GENERIC_NAME_LLDP \
858 	"lldp"
859 #define DEVLINK_TRAP_GENERIC_NAME_IGMP_QUERY \
860 	"igmp_query"
861 #define DEVLINK_TRAP_GENERIC_NAME_IGMP_V1_REPORT \
862 	"igmp_v1_report"
863 #define DEVLINK_TRAP_GENERIC_NAME_IGMP_V2_REPORT \
864 	"igmp_v2_report"
865 #define DEVLINK_TRAP_GENERIC_NAME_IGMP_V3_REPORT \
866 	"igmp_v3_report"
867 #define DEVLINK_TRAP_GENERIC_NAME_IGMP_V2_LEAVE \
868 	"igmp_v2_leave"
869 #define DEVLINK_TRAP_GENERIC_NAME_MLD_QUERY \
870 	"mld_query"
871 #define DEVLINK_TRAP_GENERIC_NAME_MLD_V1_REPORT \
872 	"mld_v1_report"
873 #define DEVLINK_TRAP_GENERIC_NAME_MLD_V2_REPORT \
874 	"mld_v2_report"
875 #define DEVLINK_TRAP_GENERIC_NAME_MLD_V1_DONE \
876 	"mld_v1_done"
877 #define DEVLINK_TRAP_GENERIC_NAME_IPV4_DHCP \
878 	"ipv4_dhcp"
879 #define DEVLINK_TRAP_GENERIC_NAME_IPV6_DHCP \
880 	"ipv6_dhcp"
881 #define DEVLINK_TRAP_GENERIC_NAME_ARP_REQUEST \
882 	"arp_request"
883 #define DEVLINK_TRAP_GENERIC_NAME_ARP_RESPONSE \
884 	"arp_response"
885 #define DEVLINK_TRAP_GENERIC_NAME_ARP_OVERLAY \
886 	"arp_overlay"
887 #define DEVLINK_TRAP_GENERIC_NAME_IPV6_NEIGH_SOLICIT \
888 	"ipv6_neigh_solicit"
889 #define DEVLINK_TRAP_GENERIC_NAME_IPV6_NEIGH_ADVERT \
890 	"ipv6_neigh_advert"
891 #define DEVLINK_TRAP_GENERIC_NAME_IPV4_BFD \
892 	"ipv4_bfd"
893 #define DEVLINK_TRAP_GENERIC_NAME_IPV6_BFD \
894 	"ipv6_bfd"
895 #define DEVLINK_TRAP_GENERIC_NAME_IPV4_OSPF \
896 	"ipv4_ospf"
897 #define DEVLINK_TRAP_GENERIC_NAME_IPV6_OSPF \
898 	"ipv6_ospf"
899 #define DEVLINK_TRAP_GENERIC_NAME_IPV4_BGP \
900 	"ipv4_bgp"
901 #define DEVLINK_TRAP_GENERIC_NAME_IPV6_BGP \
902 	"ipv6_bgp"
903 #define DEVLINK_TRAP_GENERIC_NAME_IPV4_VRRP \
904 	"ipv4_vrrp"
905 #define DEVLINK_TRAP_GENERIC_NAME_IPV6_VRRP \
906 	"ipv6_vrrp"
907 #define DEVLINK_TRAP_GENERIC_NAME_IPV4_PIM \
908 	"ipv4_pim"
909 #define DEVLINK_TRAP_GENERIC_NAME_IPV6_PIM \
910 	"ipv6_pim"
911 #define DEVLINK_TRAP_GENERIC_NAME_UC_LB \
912 	"uc_loopback"
913 #define DEVLINK_TRAP_GENERIC_NAME_LOCAL_ROUTE \
914 	"local_route"
915 #define DEVLINK_TRAP_GENERIC_NAME_EXTERNAL_ROUTE \
916 	"external_route"
917 #define DEVLINK_TRAP_GENERIC_NAME_IPV6_UC_DIP_LINK_LOCAL_SCOPE \
918 	"ipv6_uc_dip_link_local_scope"
919 #define DEVLINK_TRAP_GENERIC_NAME_IPV6_DIP_ALL_NODES \
920 	"ipv6_dip_all_nodes"
921 #define DEVLINK_TRAP_GENERIC_NAME_IPV6_DIP_ALL_ROUTERS \
922 	"ipv6_dip_all_routers"
923 #define DEVLINK_TRAP_GENERIC_NAME_IPV6_ROUTER_SOLICIT \
924 	"ipv6_router_solicit"
925 #define DEVLINK_TRAP_GENERIC_NAME_IPV6_ROUTER_ADVERT \
926 	"ipv6_router_advert"
927 #define DEVLINK_TRAP_GENERIC_NAME_IPV6_REDIRECT \
928 	"ipv6_redirect"
929 #define DEVLINK_TRAP_GENERIC_NAME_IPV4_ROUTER_ALERT \
930 	"ipv4_router_alert"
931 #define DEVLINK_TRAP_GENERIC_NAME_IPV6_ROUTER_ALERT \
932 	"ipv6_router_alert"
933 #define DEVLINK_TRAP_GENERIC_NAME_PTP_EVENT \
934 	"ptp_event"
935 #define DEVLINK_TRAP_GENERIC_NAME_PTP_GENERAL \
936 	"ptp_general"
937 #define DEVLINK_TRAP_GENERIC_NAME_FLOW_ACTION_SAMPLE \
938 	"flow_action_sample"
939 #define DEVLINK_TRAP_GENERIC_NAME_FLOW_ACTION_TRAP \
940 	"flow_action_trap"
941 #define DEVLINK_TRAP_GENERIC_NAME_EARLY_DROP \
942 	"early_drop"
943 
944 #define DEVLINK_TRAP_GROUP_GENERIC_NAME_L2_DROPS \
945 	"l2_drops"
946 #define DEVLINK_TRAP_GROUP_GENERIC_NAME_L3_DROPS \
947 	"l3_drops"
948 #define DEVLINK_TRAP_GROUP_GENERIC_NAME_L3_EXCEPTIONS \
949 	"l3_exceptions"
950 #define DEVLINK_TRAP_GROUP_GENERIC_NAME_BUFFER_DROPS \
951 	"buffer_drops"
952 #define DEVLINK_TRAP_GROUP_GENERIC_NAME_TUNNEL_DROPS \
953 	"tunnel_drops"
954 #define DEVLINK_TRAP_GROUP_GENERIC_NAME_ACL_DROPS \
955 	"acl_drops"
956 #define DEVLINK_TRAP_GROUP_GENERIC_NAME_STP \
957 	"stp"
958 #define DEVLINK_TRAP_GROUP_GENERIC_NAME_LACP \
959 	"lacp"
960 #define DEVLINK_TRAP_GROUP_GENERIC_NAME_LLDP \
961 	"lldp"
962 #define DEVLINK_TRAP_GROUP_GENERIC_NAME_MC_SNOOPING  \
963 	"mc_snooping"
964 #define DEVLINK_TRAP_GROUP_GENERIC_NAME_DHCP \
965 	"dhcp"
966 #define DEVLINK_TRAP_GROUP_GENERIC_NAME_NEIGH_DISCOVERY \
967 	"neigh_discovery"
968 #define DEVLINK_TRAP_GROUP_GENERIC_NAME_BFD \
969 	"bfd"
970 #define DEVLINK_TRAP_GROUP_GENERIC_NAME_OSPF \
971 	"ospf"
972 #define DEVLINK_TRAP_GROUP_GENERIC_NAME_BGP \
973 	"bgp"
974 #define DEVLINK_TRAP_GROUP_GENERIC_NAME_VRRP \
975 	"vrrp"
976 #define DEVLINK_TRAP_GROUP_GENERIC_NAME_PIM \
977 	"pim"
978 #define DEVLINK_TRAP_GROUP_GENERIC_NAME_UC_LB \
979 	"uc_loopback"
980 #define DEVLINK_TRAP_GROUP_GENERIC_NAME_LOCAL_DELIVERY \
981 	"local_delivery"
982 #define DEVLINK_TRAP_GROUP_GENERIC_NAME_EXTERNAL_DELIVERY \
983 	"external_delivery"
984 #define DEVLINK_TRAP_GROUP_GENERIC_NAME_IPV6 \
985 	"ipv6"
986 #define DEVLINK_TRAP_GROUP_GENERIC_NAME_PTP_EVENT \
987 	"ptp_event"
988 #define DEVLINK_TRAP_GROUP_GENERIC_NAME_PTP_GENERAL \
989 	"ptp_general"
990 #define DEVLINK_TRAP_GROUP_GENERIC_NAME_ACL_SAMPLE \
991 	"acl_sample"
992 #define DEVLINK_TRAP_GROUP_GENERIC_NAME_ACL_TRAP \
993 	"acl_trap"
994 
995 #define DEVLINK_TRAP_GENERIC(_type, _init_action, _id, _group_id,	      \
996 			     _metadata_cap)				      \
997 	{								      \
998 		.type = DEVLINK_TRAP_TYPE_##_type,			      \
999 		.init_action = DEVLINK_TRAP_ACTION_##_init_action,	      \
1000 		.generic = true,					      \
1001 		.id = DEVLINK_TRAP_GENERIC_ID_##_id,			      \
1002 		.name = DEVLINK_TRAP_GENERIC_NAME_##_id,		      \
1003 		.init_group_id = _group_id,				      \
1004 		.metadata_cap = _metadata_cap,				      \
1005 	}
1006 
1007 #define DEVLINK_TRAP_DRIVER(_type, _init_action, _id, _name, _group_id,	      \
1008 			    _metadata_cap)				      \
1009 	{								      \
1010 		.type = DEVLINK_TRAP_TYPE_##_type,			      \
1011 		.init_action = DEVLINK_TRAP_ACTION_##_init_action,	      \
1012 		.generic = false,					      \
1013 		.id = _id,						      \
1014 		.name = _name,						      \
1015 		.init_group_id = _group_id,				      \
1016 		.metadata_cap = _metadata_cap,				      \
1017 	}
1018 
1019 #define DEVLINK_TRAP_GROUP_GENERIC(_id, _policer_id)			      \
1020 	{								      \
1021 		.name = DEVLINK_TRAP_GROUP_GENERIC_NAME_##_id,		      \
1022 		.id = DEVLINK_TRAP_GROUP_GENERIC_ID_##_id,		      \
1023 		.generic = true,					      \
1024 		.init_policer_id = _policer_id,				      \
1025 	}
1026 
1027 #define DEVLINK_TRAP_POLICER(_id, _rate, _burst, _max_rate, _min_rate,	      \
1028 			     _max_burst, _min_burst)			      \
1029 	{								      \
1030 		.id = _id,						      \
1031 		.init_rate = _rate,					      \
1032 		.init_burst = _burst,					      \
1033 		.max_rate = _max_rate,					      \
1034 		.min_rate = _min_rate,					      \
1035 		.max_burst = _max_burst,				      \
1036 		.min_burst = _min_burst,				      \
1037 	}
1038 
1039 struct devlink_ops {
1040 	int (*reload_down)(struct devlink *devlink, bool netns_change,
1041 			   struct netlink_ext_ack *extack);
1042 	int (*reload_up)(struct devlink *devlink,
1043 			 struct netlink_ext_ack *extack);
1044 	int (*port_type_set)(struct devlink_port *devlink_port,
1045 			     enum devlink_port_type port_type);
1046 	int (*port_split)(struct devlink *devlink, unsigned int port_index,
1047 			  unsigned int count, struct netlink_ext_ack *extack);
1048 	int (*port_unsplit)(struct devlink *devlink, unsigned int port_index,
1049 			    struct netlink_ext_ack *extack);
1050 	int (*sb_pool_get)(struct devlink *devlink, unsigned int sb_index,
1051 			   u16 pool_index,
1052 			   struct devlink_sb_pool_info *pool_info);
1053 	int (*sb_pool_set)(struct devlink *devlink, unsigned int sb_index,
1054 			   u16 pool_index, u32 size,
1055 			   enum devlink_sb_threshold_type threshold_type,
1056 			   struct netlink_ext_ack *extack);
1057 	int (*sb_port_pool_get)(struct devlink_port *devlink_port,
1058 				unsigned int sb_index, u16 pool_index,
1059 				u32 *p_threshold);
1060 	int (*sb_port_pool_set)(struct devlink_port *devlink_port,
1061 				unsigned int sb_index, u16 pool_index,
1062 				u32 threshold, struct netlink_ext_ack *extack);
1063 	int (*sb_tc_pool_bind_get)(struct devlink_port *devlink_port,
1064 				   unsigned int sb_index,
1065 				   u16 tc_index,
1066 				   enum devlink_sb_pool_type pool_type,
1067 				   u16 *p_pool_index, u32 *p_threshold);
1068 	int (*sb_tc_pool_bind_set)(struct devlink_port *devlink_port,
1069 				   unsigned int sb_index,
1070 				   u16 tc_index,
1071 				   enum devlink_sb_pool_type pool_type,
1072 				   u16 pool_index, u32 threshold,
1073 				   struct netlink_ext_ack *extack);
1074 	int (*sb_occ_snapshot)(struct devlink *devlink,
1075 			       unsigned int sb_index);
1076 	int (*sb_occ_max_clear)(struct devlink *devlink,
1077 				unsigned int sb_index);
1078 	int (*sb_occ_port_pool_get)(struct devlink_port *devlink_port,
1079 				    unsigned int sb_index, u16 pool_index,
1080 				    u32 *p_cur, u32 *p_max);
1081 	int (*sb_occ_tc_port_bind_get)(struct devlink_port *devlink_port,
1082 				       unsigned int sb_index,
1083 				       u16 tc_index,
1084 				       enum devlink_sb_pool_type pool_type,
1085 				       u32 *p_cur, u32 *p_max);
1086 
1087 	int (*eswitch_mode_get)(struct devlink *devlink, u16 *p_mode);
1088 	int (*eswitch_mode_set)(struct devlink *devlink, u16 mode,
1089 				struct netlink_ext_ack *extack);
1090 	int (*eswitch_inline_mode_get)(struct devlink *devlink, u8 *p_inline_mode);
1091 	int (*eswitch_inline_mode_set)(struct devlink *devlink, u8 inline_mode,
1092 				       struct netlink_ext_ack *extack);
1093 	int (*eswitch_encap_mode_get)(struct devlink *devlink,
1094 				      enum devlink_eswitch_encap_mode *p_encap_mode);
1095 	int (*eswitch_encap_mode_set)(struct devlink *devlink,
1096 				      enum devlink_eswitch_encap_mode encap_mode,
1097 				      struct netlink_ext_ack *extack);
1098 	int (*info_get)(struct devlink *devlink, struct devlink_info_req *req,
1099 			struct netlink_ext_ack *extack);
1100 	int (*flash_update)(struct devlink *devlink, const char *file_name,
1101 			    const char *component,
1102 			    struct netlink_ext_ack *extack);
1103 	/**
1104 	 * @trap_init: Trap initialization function.
1105 	 *
1106 	 * Should be used by device drivers to initialize the trap in the
1107 	 * underlying device. Drivers should also store the provided trap
1108 	 * context, so that they could efficiently pass it to
1109 	 * devlink_trap_report() when the trap is triggered.
1110 	 */
1111 	int (*trap_init)(struct devlink *devlink,
1112 			 const struct devlink_trap *trap, void *trap_ctx);
1113 	/**
1114 	 * @trap_fini: Trap de-initialization function.
1115 	 *
1116 	 * Should be used by device drivers to de-initialize the trap in the
1117 	 * underlying device.
1118 	 */
1119 	void (*trap_fini)(struct devlink *devlink,
1120 			  const struct devlink_trap *trap, void *trap_ctx);
1121 	/**
1122 	 * @trap_action_set: Trap action set function.
1123 	 */
1124 	int (*trap_action_set)(struct devlink *devlink,
1125 			       const struct devlink_trap *trap,
1126 			       enum devlink_trap_action action,
1127 			       struct netlink_ext_ack *extack);
1128 	/**
1129 	 * @trap_group_init: Trap group initialization function.
1130 	 *
1131 	 * Should be used by device drivers to initialize the trap group in the
1132 	 * underlying device.
1133 	 */
1134 	int (*trap_group_init)(struct devlink *devlink,
1135 			       const struct devlink_trap_group *group);
1136 	/**
1137 	 * @trap_group_set: Trap group parameters set function.
1138 	 *
1139 	 * Note: @policer can be NULL when a policer is being unbound from
1140 	 * @group.
1141 	 */
1142 	int (*trap_group_set)(struct devlink *devlink,
1143 			      const struct devlink_trap_group *group,
1144 			      const struct devlink_trap_policer *policer,
1145 			      struct netlink_ext_ack *extack);
1146 	/**
1147 	 * @trap_policer_init: Trap policer initialization function.
1148 	 *
1149 	 * Should be used by device drivers to initialize the trap policer in
1150 	 * the underlying device.
1151 	 */
1152 	int (*trap_policer_init)(struct devlink *devlink,
1153 				 const struct devlink_trap_policer *policer);
1154 	/**
1155 	 * @trap_policer_fini: Trap policer de-initialization function.
1156 	 *
1157 	 * Should be used by device drivers to de-initialize the trap policer
1158 	 * in the underlying device.
1159 	 */
1160 	void (*trap_policer_fini)(struct devlink *devlink,
1161 				  const struct devlink_trap_policer *policer);
1162 	/**
1163 	 * @trap_policer_set: Trap policer parameters set function.
1164 	 */
1165 	int (*trap_policer_set)(struct devlink *devlink,
1166 				const struct devlink_trap_policer *policer,
1167 				u64 rate, u64 burst,
1168 				struct netlink_ext_ack *extack);
1169 	/**
1170 	 * @trap_policer_counter_get: Trap policer counter get function.
1171 	 *
1172 	 * Should be used by device drivers to report number of packets dropped
1173 	 * by the policer.
1174 	 */
1175 	int (*trap_policer_counter_get)(struct devlink *devlink,
1176 					const struct devlink_trap_policer *policer,
1177 					u64 *p_drops);
1178 	/**
1179 	 * @port_function_hw_addr_get: Port function's hardware address get function.
1180 	 *
1181 	 * Should be used by device drivers to report the hardware address of a function managed
1182 	 * by the devlink port. Driver should return -EOPNOTSUPP if it doesn't support port
1183 	 * function handling for a particular port.
1184 	 *
1185 	 * Note: @extack can be NULL when port notifier queries the port function.
1186 	 */
1187 	int (*port_function_hw_addr_get)(struct devlink *devlink, struct devlink_port *port,
1188 					 u8 *hw_addr, int *hw_addr_len,
1189 					 struct netlink_ext_ack *extack);
1190 	/**
1191 	 * @port_function_hw_addr_set: Port function's hardware address set function.
1192 	 *
1193 	 * Should be used by device drivers to set the hardware address of a function managed
1194 	 * by the devlink port. Driver should return -EOPNOTSUPP if it doesn't support port
1195 	 * function handling for a particular port.
1196 	 */
1197 	int (*port_function_hw_addr_set)(struct devlink *devlink, struct devlink_port *port,
1198 					 const u8 *hw_addr, int hw_addr_len,
1199 					 struct netlink_ext_ack *extack);
1200 };
1201 
1202 static inline void *devlink_priv(struct devlink *devlink)
1203 {
1204 	BUG_ON(!devlink);
1205 	return &devlink->priv;
1206 }
1207 
1208 static inline struct devlink *priv_to_devlink(void *priv)
1209 {
1210 	BUG_ON(!priv);
1211 	return container_of(priv, struct devlink, priv);
1212 }
1213 
1214 static inline struct devlink_port *
1215 netdev_to_devlink_port(struct net_device *dev)
1216 {
1217 	if (dev->netdev_ops->ndo_get_devlink_port)
1218 		return dev->netdev_ops->ndo_get_devlink_port(dev);
1219 	return NULL;
1220 }
1221 
1222 static inline struct devlink *netdev_to_devlink(struct net_device *dev)
1223 {
1224 	struct devlink_port *devlink_port = netdev_to_devlink_port(dev);
1225 
1226 	if (devlink_port)
1227 		return devlink_port->devlink;
1228 	return NULL;
1229 }
1230 
1231 struct ib_device;
1232 
1233 struct net *devlink_net(const struct devlink *devlink);
1234 void devlink_net_set(struct devlink *devlink, struct net *net);
1235 struct devlink *devlink_alloc(const struct devlink_ops *ops, size_t priv_size);
1236 int devlink_register(struct devlink *devlink, struct device *dev);
1237 void devlink_unregister(struct devlink *devlink);
1238 void devlink_reload_enable(struct devlink *devlink);
1239 void devlink_reload_disable(struct devlink *devlink);
1240 void devlink_free(struct devlink *devlink);
1241 int devlink_port_register(struct devlink *devlink,
1242 			  struct devlink_port *devlink_port,
1243 			  unsigned int port_index);
1244 void devlink_port_unregister(struct devlink_port *devlink_port);
1245 void devlink_port_type_eth_set(struct devlink_port *devlink_port,
1246 			       struct net_device *netdev);
1247 void devlink_port_type_ib_set(struct devlink_port *devlink_port,
1248 			      struct ib_device *ibdev);
1249 void devlink_port_type_clear(struct devlink_port *devlink_port);
1250 void devlink_port_attrs_set(struct devlink_port *devlink_port,
1251 			    struct devlink_port_attrs *devlink_port_attrs);
1252 void devlink_port_attrs_pci_pf_set(struct devlink_port *devlink_port, u32 controller,
1253 				   u16 pf, bool external);
1254 void devlink_port_attrs_pci_vf_set(struct devlink_port *devlink_port, u32 controller,
1255 				   u16 pf, u16 vf, bool external);
1256 int devlink_sb_register(struct devlink *devlink, unsigned int sb_index,
1257 			u32 size, u16 ingress_pools_count,
1258 			u16 egress_pools_count, u16 ingress_tc_count,
1259 			u16 egress_tc_count);
1260 void devlink_sb_unregister(struct devlink *devlink, unsigned int sb_index);
1261 int devlink_dpipe_table_register(struct devlink *devlink,
1262 				 const char *table_name,
1263 				 struct devlink_dpipe_table_ops *table_ops,
1264 				 void *priv, bool counter_control_extern);
1265 void devlink_dpipe_table_unregister(struct devlink *devlink,
1266 				    const char *table_name);
1267 int devlink_dpipe_headers_register(struct devlink *devlink,
1268 				   struct devlink_dpipe_headers *dpipe_headers);
1269 void devlink_dpipe_headers_unregister(struct devlink *devlink);
1270 bool devlink_dpipe_table_counter_enabled(struct devlink *devlink,
1271 					 const char *table_name);
1272 int devlink_dpipe_entry_ctx_prepare(struct devlink_dpipe_dump_ctx *dump_ctx);
1273 int devlink_dpipe_entry_ctx_append(struct devlink_dpipe_dump_ctx *dump_ctx,
1274 				   struct devlink_dpipe_entry *entry);
1275 int devlink_dpipe_entry_ctx_close(struct devlink_dpipe_dump_ctx *dump_ctx);
1276 void devlink_dpipe_entry_clear(struct devlink_dpipe_entry *entry);
1277 int devlink_dpipe_action_put(struct sk_buff *skb,
1278 			     struct devlink_dpipe_action *action);
1279 int devlink_dpipe_match_put(struct sk_buff *skb,
1280 			    struct devlink_dpipe_match *match);
1281 extern struct devlink_dpipe_header devlink_dpipe_header_ethernet;
1282 extern struct devlink_dpipe_header devlink_dpipe_header_ipv4;
1283 extern struct devlink_dpipe_header devlink_dpipe_header_ipv6;
1284 
1285 int devlink_resource_register(struct devlink *devlink,
1286 			      const char *resource_name,
1287 			      u64 resource_size,
1288 			      u64 resource_id,
1289 			      u64 parent_resource_id,
1290 			      const struct devlink_resource_size_params *size_params);
1291 void devlink_resources_unregister(struct devlink *devlink,
1292 				  struct devlink_resource *resource);
1293 int devlink_resource_size_get(struct devlink *devlink,
1294 			      u64 resource_id,
1295 			      u64 *p_resource_size);
1296 int devlink_dpipe_table_resource_set(struct devlink *devlink,
1297 				     const char *table_name, u64 resource_id,
1298 				     u64 resource_units);
1299 void devlink_resource_occ_get_register(struct devlink *devlink,
1300 				       u64 resource_id,
1301 				       devlink_resource_occ_get_t *occ_get,
1302 				       void *occ_get_priv);
1303 void devlink_resource_occ_get_unregister(struct devlink *devlink,
1304 					 u64 resource_id);
1305 int devlink_params_register(struct devlink *devlink,
1306 			    const struct devlink_param *params,
1307 			    size_t params_count);
1308 void devlink_params_unregister(struct devlink *devlink,
1309 			       const struct devlink_param *params,
1310 			       size_t params_count);
1311 void devlink_params_publish(struct devlink *devlink);
1312 void devlink_params_unpublish(struct devlink *devlink);
1313 int devlink_port_params_register(struct devlink_port *devlink_port,
1314 				 const struct devlink_param *params,
1315 				 size_t params_count);
1316 void devlink_port_params_unregister(struct devlink_port *devlink_port,
1317 				    const struct devlink_param *params,
1318 				    size_t params_count);
1319 int devlink_param_driverinit_value_get(struct devlink *devlink, u32 param_id,
1320 				       union devlink_param_value *init_val);
1321 int devlink_param_driverinit_value_set(struct devlink *devlink, u32 param_id,
1322 				       union devlink_param_value init_val);
1323 int
1324 devlink_port_param_driverinit_value_get(struct devlink_port *devlink_port,
1325 					u32 param_id,
1326 					union devlink_param_value *init_val);
1327 int devlink_port_param_driverinit_value_set(struct devlink_port *devlink_port,
1328 					    u32 param_id,
1329 					    union devlink_param_value init_val);
1330 void devlink_param_value_changed(struct devlink *devlink, u32 param_id);
1331 void devlink_port_param_value_changed(struct devlink_port *devlink_port,
1332 				      u32 param_id);
1333 void devlink_param_value_str_fill(union devlink_param_value *dst_val,
1334 				  const char *src);
1335 struct devlink_region *
1336 devlink_region_create(struct devlink *devlink,
1337 		      const struct devlink_region_ops *ops,
1338 		      u32 region_max_snapshots, u64 region_size);
1339 void devlink_region_destroy(struct devlink_region *region);
1340 int devlink_region_snapshot_id_get(struct devlink *devlink, u32 *id);
1341 void devlink_region_snapshot_id_put(struct devlink *devlink, u32 id);
1342 int devlink_region_snapshot_create(struct devlink_region *region,
1343 				   u8 *data, u32 snapshot_id);
1344 int devlink_info_serial_number_put(struct devlink_info_req *req,
1345 				   const char *sn);
1346 int devlink_info_driver_name_put(struct devlink_info_req *req,
1347 				 const char *name);
1348 int devlink_info_board_serial_number_put(struct devlink_info_req *req,
1349 					 const char *bsn);
1350 int devlink_info_version_fixed_put(struct devlink_info_req *req,
1351 				   const char *version_name,
1352 				   const char *version_value);
1353 int devlink_info_version_stored_put(struct devlink_info_req *req,
1354 				    const char *version_name,
1355 				    const char *version_value);
1356 int devlink_info_version_running_put(struct devlink_info_req *req,
1357 				     const char *version_name,
1358 				     const char *version_value);
1359 
1360 int devlink_fmsg_obj_nest_start(struct devlink_fmsg *fmsg);
1361 int devlink_fmsg_obj_nest_end(struct devlink_fmsg *fmsg);
1362 
1363 int devlink_fmsg_pair_nest_start(struct devlink_fmsg *fmsg, const char *name);
1364 int devlink_fmsg_pair_nest_end(struct devlink_fmsg *fmsg);
1365 
1366 int devlink_fmsg_arr_pair_nest_start(struct devlink_fmsg *fmsg,
1367 				     const char *name);
1368 int devlink_fmsg_arr_pair_nest_end(struct devlink_fmsg *fmsg);
1369 int devlink_fmsg_binary_pair_nest_start(struct devlink_fmsg *fmsg,
1370 					const char *name);
1371 int devlink_fmsg_binary_pair_nest_end(struct devlink_fmsg *fmsg);
1372 
1373 int devlink_fmsg_bool_put(struct devlink_fmsg *fmsg, bool value);
1374 int devlink_fmsg_u8_put(struct devlink_fmsg *fmsg, u8 value);
1375 int devlink_fmsg_u32_put(struct devlink_fmsg *fmsg, u32 value);
1376 int devlink_fmsg_u64_put(struct devlink_fmsg *fmsg, u64 value);
1377 int devlink_fmsg_string_put(struct devlink_fmsg *fmsg, const char *value);
1378 int devlink_fmsg_binary_put(struct devlink_fmsg *fmsg, const void *value,
1379 			    u16 value_len);
1380 
1381 int devlink_fmsg_bool_pair_put(struct devlink_fmsg *fmsg, const char *name,
1382 			       bool value);
1383 int devlink_fmsg_u8_pair_put(struct devlink_fmsg *fmsg, const char *name,
1384 			     u8 value);
1385 int devlink_fmsg_u32_pair_put(struct devlink_fmsg *fmsg, const char *name,
1386 			      u32 value);
1387 int devlink_fmsg_u64_pair_put(struct devlink_fmsg *fmsg, const char *name,
1388 			      u64 value);
1389 int devlink_fmsg_string_pair_put(struct devlink_fmsg *fmsg, const char *name,
1390 				 const char *value);
1391 int devlink_fmsg_binary_pair_put(struct devlink_fmsg *fmsg, const char *name,
1392 				 const void *value, u32 value_len);
1393 
1394 struct devlink_health_reporter *
1395 devlink_health_reporter_create(struct devlink *devlink,
1396 			       const struct devlink_health_reporter_ops *ops,
1397 			       u64 graceful_period, void *priv);
1398 
1399 struct devlink_health_reporter *
1400 devlink_port_health_reporter_create(struct devlink_port *port,
1401 				    const struct devlink_health_reporter_ops *ops,
1402 				    u64 graceful_period, void *priv);
1403 
1404 void
1405 devlink_health_reporter_destroy(struct devlink_health_reporter *reporter);
1406 
1407 void
1408 devlink_port_health_reporter_destroy(struct devlink_health_reporter *reporter);
1409 
1410 void *
1411 devlink_health_reporter_priv(struct devlink_health_reporter *reporter);
1412 int devlink_health_report(struct devlink_health_reporter *reporter,
1413 			  const char *msg, void *priv_ctx);
1414 void
1415 devlink_health_reporter_state_update(struct devlink_health_reporter *reporter,
1416 				     enum devlink_health_reporter_state state);
1417 void
1418 devlink_health_reporter_recovery_done(struct devlink_health_reporter *reporter);
1419 
1420 bool devlink_is_reload_failed(const struct devlink *devlink);
1421 
1422 void devlink_flash_update_begin_notify(struct devlink *devlink);
1423 void devlink_flash_update_end_notify(struct devlink *devlink);
1424 void devlink_flash_update_status_notify(struct devlink *devlink,
1425 					const char *status_msg,
1426 					const char *component,
1427 					unsigned long done,
1428 					unsigned long total);
1429 void devlink_flash_update_timeout_notify(struct devlink *devlink,
1430 					 const char *status_msg,
1431 					 const char *component,
1432 					 unsigned long timeout);
1433 
1434 int devlink_traps_register(struct devlink *devlink,
1435 			   const struct devlink_trap *traps,
1436 			   size_t traps_count, void *priv);
1437 void devlink_traps_unregister(struct devlink *devlink,
1438 			      const struct devlink_trap *traps,
1439 			      size_t traps_count);
1440 void devlink_trap_report(struct devlink *devlink, struct sk_buff *skb,
1441 			 void *trap_ctx, struct devlink_port *in_devlink_port,
1442 			 const struct flow_action_cookie *fa_cookie);
1443 void *devlink_trap_ctx_priv(void *trap_ctx);
1444 int devlink_trap_groups_register(struct devlink *devlink,
1445 				 const struct devlink_trap_group *groups,
1446 				 size_t groups_count);
1447 void devlink_trap_groups_unregister(struct devlink *devlink,
1448 				    const struct devlink_trap_group *groups,
1449 				    size_t groups_count);
1450 int
1451 devlink_trap_policers_register(struct devlink *devlink,
1452 			       const struct devlink_trap_policer *policers,
1453 			       size_t policers_count);
1454 void
1455 devlink_trap_policers_unregister(struct devlink *devlink,
1456 				 const struct devlink_trap_policer *policers,
1457 				 size_t policers_count);
1458 
1459 #if IS_ENABLED(CONFIG_NET_DEVLINK)
1460 
1461 void devlink_compat_running_version(struct net_device *dev,
1462 				    char *buf, size_t len);
1463 int devlink_compat_flash_update(struct net_device *dev, const char *file_name);
1464 int devlink_compat_phys_port_name_get(struct net_device *dev,
1465 				      char *name, size_t len);
1466 int devlink_compat_switch_id_get(struct net_device *dev,
1467 				 struct netdev_phys_item_id *ppid);
1468 
1469 #else
1470 
1471 static inline void
1472 devlink_compat_running_version(struct net_device *dev, char *buf, size_t len)
1473 {
1474 }
1475 
1476 static inline int
1477 devlink_compat_flash_update(struct net_device *dev, const char *file_name)
1478 {
1479 	return -EOPNOTSUPP;
1480 }
1481 
1482 static inline int
1483 devlink_compat_phys_port_name_get(struct net_device *dev,
1484 				  char *name, size_t len)
1485 {
1486 	return -EOPNOTSUPP;
1487 }
1488 
1489 static inline int
1490 devlink_compat_switch_id_get(struct net_device *dev,
1491 			     struct netdev_phys_item_id *ppid)
1492 {
1493 	return -EOPNOTSUPP;
1494 }
1495 
1496 #endif
1497 
1498 #endif /* _NET_DEVLINK_H_ */
1499