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