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