xref: /linux/drivers/net/ethernet/microchip/vcap/vcap_api.h (revision e0305cc1d125d76e6614a675b1126f00152d6789)
18beef08fSSteen Hegelund /* SPDX-License-Identifier: BSD-3-Clause */
28beef08fSSteen Hegelund /* Copyright (C) 2022 Microchip Technology Inc. and its subsidiaries.
38beef08fSSteen Hegelund  * Microchip VCAP API
48beef08fSSteen Hegelund  */
58beef08fSSteen Hegelund 
68beef08fSSteen Hegelund #ifndef __VCAP_API__
78beef08fSSteen Hegelund #define __VCAP_API__
88beef08fSSteen Hegelund 
98beef08fSSteen Hegelund #include <linux/types.h>
108beef08fSSteen Hegelund #include <linux/list.h>
118beef08fSSteen Hegelund #include <linux/netdevice.h>
128beef08fSSteen Hegelund 
138beef08fSSteen Hegelund /* Use the generated API model */
1467d63751SSteen Hegelund #ifdef CONFIG_VCAP_KUNIT_TEST
1567d63751SSteen Hegelund #include "vcap_ag_api_kunit.h"
1667d63751SSteen Hegelund #endif
178beef08fSSteen Hegelund #include "vcap_ag_api.h"
188beef08fSSteen Hegelund 
198beef08fSSteen Hegelund #define VCAP_CID_LOOKUP_SIZE          100000 /* Chains in a lookup */
208beef08fSSteen Hegelund #define VCAP_CID_INGRESS_L0          1000000 /* Ingress Stage 1 Lookup 0 */
218beef08fSSteen Hegelund #define VCAP_CID_INGRESS_L1          1100000 /* Ingress Stage 1 Lookup 1 */
228beef08fSSteen Hegelund #define VCAP_CID_INGRESS_L2          1200000 /* Ingress Stage 1 Lookup 2 */
238beef08fSSteen Hegelund #define VCAP_CID_INGRESS_L3          1300000 /* Ingress Stage 1 Lookup 3 */
248beef08fSSteen Hegelund #define VCAP_CID_INGRESS_L4          1400000 /* Ingress Stage 1 Lookup 4 */
258beef08fSSteen Hegelund #define VCAP_CID_INGRESS_L5          1500000 /* Ingress Stage 1 Lookup 5 */
268beef08fSSteen Hegelund 
278beef08fSSteen Hegelund #define VCAP_CID_PREROUTING_IPV6     3000000 /* Prerouting Stage */
288beef08fSSteen Hegelund #define VCAP_CID_PREROUTING          6000000 /* Prerouting Stage */
298beef08fSSteen Hegelund 
308beef08fSSteen Hegelund #define VCAP_CID_INGRESS_STAGE2_L0   8000000 /* Ingress Stage 2 Lookup 0 */
318beef08fSSteen Hegelund #define VCAP_CID_INGRESS_STAGE2_L1   8100000 /* Ingress Stage 2 Lookup 1 */
328beef08fSSteen Hegelund #define VCAP_CID_INGRESS_STAGE2_L2   8200000 /* Ingress Stage 2 Lookup 2 */
338beef08fSSteen Hegelund #define VCAP_CID_INGRESS_STAGE2_L3   8300000 /* Ingress Stage 2 Lookup 3 */
348beef08fSSteen Hegelund 
358beef08fSSteen Hegelund #define VCAP_CID_EGRESS_L0           10000000 /* Egress Lookup 0 */
368beef08fSSteen Hegelund #define VCAP_CID_EGRESS_L1           10100000 /* Egress Lookup 1 */
378beef08fSSteen Hegelund 
388beef08fSSteen Hegelund #define VCAP_CID_EGRESS_STAGE2_L0    20000000 /* Egress Stage 2 Lookup 0 */
398beef08fSSteen Hegelund #define VCAP_CID_EGRESS_STAGE2_L1    20100000 /* Egress Stage 2 Lookup 1 */
408beef08fSSteen Hegelund 
418beef08fSSteen Hegelund /* Known users of the VCAP API */
428beef08fSSteen Hegelund enum vcap_user {
438beef08fSSteen Hegelund 	VCAP_USER_PTP,
448beef08fSSteen Hegelund 	VCAP_USER_MRP,
458beef08fSSteen Hegelund 	VCAP_USER_CFM,
468beef08fSSteen Hegelund 	VCAP_USER_VLAN,
478beef08fSSteen Hegelund 	VCAP_USER_QOS,
488beef08fSSteen Hegelund 	VCAP_USER_VCAP_UTIL,
498beef08fSSteen Hegelund 	VCAP_USER_TC,
508beef08fSSteen Hegelund 	VCAP_USER_TC_EXTRA,
518beef08fSSteen Hegelund 
528beef08fSSteen Hegelund 	/* add new users above here */
538beef08fSSteen Hegelund 
548beef08fSSteen Hegelund 	/* used to define VCAP_USER_MAX below */
558beef08fSSteen Hegelund 	__VCAP_USER_AFTER_LAST,
568beef08fSSteen Hegelund 	VCAP_USER_MAX = __VCAP_USER_AFTER_LAST - 1,
578beef08fSSteen Hegelund };
588beef08fSSteen Hegelund 
598beef08fSSteen Hegelund /* VCAP information used for displaying data */
608beef08fSSteen Hegelund struct vcap_statistics {
618beef08fSSteen Hegelund 	char *name;
628beef08fSSteen Hegelund 	int count;
638beef08fSSteen Hegelund 	const char * const *keyfield_set_names;
648beef08fSSteen Hegelund 	const char * const *actionfield_set_names;
658beef08fSSteen Hegelund 	const char * const *keyfield_names;
668beef08fSSteen Hegelund 	const char * const *actionfield_names;
678beef08fSSteen Hegelund };
688beef08fSSteen Hegelund 
698beef08fSSteen Hegelund /* VCAP key/action field type, position and width */
708beef08fSSteen Hegelund struct vcap_field {
718beef08fSSteen Hegelund 	u16 type;
728beef08fSSteen Hegelund 	u16 width;
738beef08fSSteen Hegelund 	u16 offset;
748beef08fSSteen Hegelund };
758beef08fSSteen Hegelund 
768beef08fSSteen Hegelund /* VCAP keyset or actionset type and width */
778beef08fSSteen Hegelund struct vcap_set {
788beef08fSSteen Hegelund 	u8 type_id;
798beef08fSSteen Hegelund 	u8 sw_per_item;
808beef08fSSteen Hegelund 	u8 sw_cnt;
818beef08fSSteen Hegelund };
828beef08fSSteen Hegelund 
838beef08fSSteen Hegelund /* VCAP typegroup position and bitvalue */
848beef08fSSteen Hegelund struct vcap_typegroup {
858beef08fSSteen Hegelund 	u16 offset;
868beef08fSSteen Hegelund 	u16 width;
878beef08fSSteen Hegelund 	u16 value;
888beef08fSSteen Hegelund };
898beef08fSSteen Hegelund 
908beef08fSSteen Hegelund /* VCAP model data */
918beef08fSSteen Hegelund struct vcap_info {
928beef08fSSteen Hegelund 	char *name; /* user-friendly name */
938beef08fSSteen Hegelund 	u16 rows; /* number of row in instance */
948beef08fSSteen Hegelund 	u16 sw_count; /* maximum subwords used per rule */
958beef08fSSteen Hegelund 	u16 sw_width; /* bits per subword in a keyset */
968beef08fSSteen Hegelund 	u16 sticky_width; /* sticky bits per rule */
978beef08fSSteen Hegelund 	u16 act_width;  /* bits per subword in an actionset */
988beef08fSSteen Hegelund 	u16 default_cnt; /* number of default rules */
998beef08fSSteen Hegelund 	u16 require_cnt_dis; /* not used */
1008beef08fSSteen Hegelund 	u16 version; /* vcap rtl version */
1018beef08fSSteen Hegelund 	const struct vcap_set *keyfield_set; /* keysets */
1028beef08fSSteen Hegelund 	int keyfield_set_size; /* number of keysets */
1038beef08fSSteen Hegelund 	const struct vcap_set *actionfield_set; /* actionsets */
1048beef08fSSteen Hegelund 	int actionfield_set_size; /* number of actionsets */
1058beef08fSSteen Hegelund 	/* map of keys per keyset */
1068beef08fSSteen Hegelund 	const struct vcap_field **keyfield_set_map;
1078beef08fSSteen Hegelund 	/* number of entries in the above map */
1088beef08fSSteen Hegelund 	int *keyfield_set_map_size;
1098beef08fSSteen Hegelund 	/* map of actions per actionset */
1108beef08fSSteen Hegelund 	const struct vcap_field **actionfield_set_map;
1118beef08fSSteen Hegelund 	/* number of entries in the above map */
1128beef08fSSteen Hegelund 	int *actionfield_set_map_size;
1138beef08fSSteen Hegelund 	/* map of keyset typegroups per subword size */
1148beef08fSSteen Hegelund 	const struct vcap_typegroup **keyfield_set_typegroups;
1158beef08fSSteen Hegelund 	/* map of actionset typegroups per subword size */
1168beef08fSSteen Hegelund 	const struct vcap_typegroup **actionfield_set_typegroups;
1178beef08fSSteen Hegelund };
1188beef08fSSteen Hegelund 
1198beef08fSSteen Hegelund enum vcap_field_type {
1208beef08fSSteen Hegelund 	VCAP_FIELD_BIT,
1218beef08fSSteen Hegelund 	VCAP_FIELD_U32,
1228beef08fSSteen Hegelund 	VCAP_FIELD_U48,
1238beef08fSSteen Hegelund 	VCAP_FIELD_U56,
1248beef08fSSteen Hegelund 	VCAP_FIELD_U64,
1258beef08fSSteen Hegelund 	VCAP_FIELD_U72,
1268beef08fSSteen Hegelund 	VCAP_FIELD_U112,
1278beef08fSSteen Hegelund 	VCAP_FIELD_U128,
1288beef08fSSteen Hegelund };
1298beef08fSSteen Hegelund 
1308beef08fSSteen Hegelund /* VCAP rule data towards the VCAP cache */
1318beef08fSSteen Hegelund struct vcap_cache_data {
1328beef08fSSteen Hegelund 	u32 *keystream;
1338beef08fSSteen Hegelund 	u32 *maskstream;
1348beef08fSSteen Hegelund 	u32 *actionstream;
1358beef08fSSteen Hegelund 	u32 counter;
1368beef08fSSteen Hegelund 	bool sticky;
1378beef08fSSteen Hegelund };
1388beef08fSSteen Hegelund 
1398beef08fSSteen Hegelund /* Selects which part of the rule must be updated */
1408beef08fSSteen Hegelund enum vcap_selection {
1418beef08fSSteen Hegelund 	VCAP_SEL_ENTRY = 0x01,
1428beef08fSSteen Hegelund 	VCAP_SEL_ACTION = 0x02,
1438beef08fSSteen Hegelund 	VCAP_SEL_COUNTER = 0x04,
1448beef08fSSteen Hegelund 	VCAP_SEL_ALL = 0xff,
1458beef08fSSteen Hegelund };
1468beef08fSSteen Hegelund 
1478beef08fSSteen Hegelund /* Commands towards the VCAP cache */
1488beef08fSSteen Hegelund enum vcap_command {
1498beef08fSSteen Hegelund 	VCAP_CMD_WRITE = 0,
1508beef08fSSteen Hegelund 	VCAP_CMD_READ = 1,
1518beef08fSSteen Hegelund 	VCAP_CMD_MOVE_DOWN = 2,
1528beef08fSSteen Hegelund 	VCAP_CMD_MOVE_UP = 3,
1538beef08fSSteen Hegelund 	VCAP_CMD_INITIALIZE = 4,
1548beef08fSSteen Hegelund };
1558beef08fSSteen Hegelund 
1568beef08fSSteen Hegelund enum vcap_rule_error {
1578beef08fSSteen Hegelund 	VCAP_ERR_NONE = 0,  /* No known error */
1588beef08fSSteen Hegelund 	VCAP_ERR_NO_ADMIN,  /* No admin instance */
1598beef08fSSteen Hegelund 	VCAP_ERR_NO_NETDEV,  /* No netdev instance */
1608beef08fSSteen Hegelund 	VCAP_ERR_NO_KEYSET_MATCH, /* No keyset matched the rule keys */
1618beef08fSSteen Hegelund 	VCAP_ERR_NO_ACTIONSET_MATCH, /* No actionset matched the rule actions */
1628beef08fSSteen Hegelund 	VCAP_ERR_NO_PORT_KEYSET_MATCH, /* No port keyset matched the rule keys */
1638beef08fSSteen Hegelund };
1648beef08fSSteen Hegelund 
1658beef08fSSteen Hegelund /* Administration of each VCAP instance */
1668beef08fSSteen Hegelund struct vcap_admin {
1678beef08fSSteen Hegelund 	struct list_head list; /* for insertion in vcap_control */
1688beef08fSSteen Hegelund 	struct list_head rules; /* list of rules */
16967456717SSteen Hegelund 	struct list_head enabled; /* list of enabled ports */
1708beef08fSSteen Hegelund 	enum vcap_type vtype;  /* type of vcap */
1718beef08fSSteen Hegelund 	int vinst; /* instance number within the same type */
1728beef08fSSteen Hegelund 	int first_cid; /* first chain id in this vcap */
1738beef08fSSteen Hegelund 	int last_cid; /* last chain id in this vcap */
1748beef08fSSteen Hegelund 	int tgt_inst; /* hardware instance number */
1758beef08fSSteen Hegelund 	int lookups; /* number of lookups in this vcap type */
1768beef08fSSteen Hegelund 	int lookups_per_instance; /* number of lookups in this instance */
1778beef08fSSteen Hegelund 	int last_valid_addr; /* top of address range to be used */
1788beef08fSSteen Hegelund 	int first_valid_addr; /* bottom of address range to be used */
1798beef08fSSteen Hegelund 	int last_used_addr;  /* address of lowest added rule */
1808beef08fSSteen Hegelund 	bool w32be; /* vcap uses "32bit-word big-endian" encoding */
1818beef08fSSteen Hegelund 	struct vcap_cache_data cache; /* encoded rule data */
1828beef08fSSteen Hegelund };
1838beef08fSSteen Hegelund 
1848beef08fSSteen Hegelund /* Client supplied VCAP rule data */
1858beef08fSSteen Hegelund struct vcap_rule {
1868beef08fSSteen Hegelund 	int vcap_chain_id; /* chain used for this rule */
1878beef08fSSteen Hegelund 	enum vcap_user user; /* rule owner */
1888beef08fSSteen Hegelund 	u16 priority;
1898beef08fSSteen Hegelund 	u32 id;  /* vcap rule id, must be unique, 0 will auto-generate a value */
1908beef08fSSteen Hegelund 	u64 cookie;  /* used by the client to identify the rule */
1918beef08fSSteen Hegelund 	struct list_head keyfields;  /* list of vcap_client_keyfield */
1928beef08fSSteen Hegelund 	struct list_head actionfields;  /* list of vcap_client_actionfield */
1938beef08fSSteen Hegelund 	enum vcap_keyfield_set keyset; /* keyset used: may be derived from fields */
1948beef08fSSteen Hegelund 	enum vcap_actionfield_set actionset; /* actionset used: may be derived from fields */
1958beef08fSSteen Hegelund 	enum vcap_rule_error exterr; /* extended error - used by TC */
1968beef08fSSteen Hegelund 	u64 client; /* space for client defined data */
1978beef08fSSteen Hegelund };
1988beef08fSSteen Hegelund 
1998beef08fSSteen Hegelund /* List of keysets */
2008beef08fSSteen Hegelund struct vcap_keyset_list {
2018beef08fSSteen Hegelund 	int max; /* size of the keyset list */
2028beef08fSSteen Hegelund 	int cnt; /* count of keysets actually in the list */
2038beef08fSSteen Hegelund 	enum vcap_keyfield_set *keysets; /* the list of keysets */
2048beef08fSSteen Hegelund };
2058beef08fSSteen Hegelund 
206*e0305cc1SSteen Hegelund /* Client output printf-like function with destination */
207*e0305cc1SSteen Hegelund struct vcap_output_print {
208*e0305cc1SSteen Hegelund 	__printf(2, 3)
209*e0305cc1SSteen Hegelund 	void (*prf)(void *out, const char *fmt, ...);
210*e0305cc1SSteen Hegelund 	void *dst;
211*e0305cc1SSteen Hegelund };
212*e0305cc1SSteen Hegelund 
2138beef08fSSteen Hegelund /* Client supplied VCAP callback operations */
2148beef08fSSteen Hegelund struct vcap_operations {
2158beef08fSSteen Hegelund 	/* validate port keyset operation */
2168beef08fSSteen Hegelund 	enum vcap_keyfield_set (*validate_keyset)
2178beef08fSSteen Hegelund 		(struct net_device *ndev,
2188beef08fSSteen Hegelund 		 struct vcap_admin *admin,
2198beef08fSSteen Hegelund 		 struct vcap_rule *rule,
2208beef08fSSteen Hegelund 		 struct vcap_keyset_list *kslist,
2218beef08fSSteen Hegelund 		 u16 l3_proto);
2228beef08fSSteen Hegelund 	/* add default rule fields for the selected keyset operations */
2238beef08fSSteen Hegelund 	void (*add_default_fields)
2248beef08fSSteen Hegelund 		(struct net_device *ndev,
2258beef08fSSteen Hegelund 		 struct vcap_admin *admin,
2268beef08fSSteen Hegelund 		 struct vcap_rule *rule);
2278beef08fSSteen Hegelund 	/* cache operations */
2288beef08fSSteen Hegelund 	void (*cache_erase)
2298beef08fSSteen Hegelund 		(struct vcap_admin *admin);
2308beef08fSSteen Hegelund 	void (*cache_write)
2318beef08fSSteen Hegelund 		(struct net_device *ndev,
2328beef08fSSteen Hegelund 		 struct vcap_admin *admin,
2338beef08fSSteen Hegelund 		 enum vcap_selection sel,
2348beef08fSSteen Hegelund 		 u32 idx, u32 count);
2358beef08fSSteen Hegelund 	void (*cache_read)
2368beef08fSSteen Hegelund 		(struct net_device *ndev,
2378beef08fSSteen Hegelund 		 struct vcap_admin *admin,
2388beef08fSSteen Hegelund 		 enum vcap_selection sel,
2398beef08fSSteen Hegelund 		 u32 idx,
2408beef08fSSteen Hegelund 		 u32 count);
2418beef08fSSteen Hegelund 	/* block operations */
2428beef08fSSteen Hegelund 	void (*init)
2438beef08fSSteen Hegelund 		(struct net_device *ndev,
2448beef08fSSteen Hegelund 		 struct vcap_admin *admin,
2458beef08fSSteen Hegelund 		 u32 addr,
2468beef08fSSteen Hegelund 		 u32 count);
2478beef08fSSteen Hegelund 	void (*update)
2488beef08fSSteen Hegelund 		(struct net_device *ndev,
2498beef08fSSteen Hegelund 		 struct vcap_admin *admin,
2508beef08fSSteen Hegelund 		 enum vcap_command cmd,
2518beef08fSSteen Hegelund 		 enum vcap_selection sel,
2528beef08fSSteen Hegelund 		 u32 addr);
2538beef08fSSteen Hegelund 	void (*move)
2548beef08fSSteen Hegelund 		(struct net_device *ndev,
2558beef08fSSteen Hegelund 		 struct vcap_admin *admin,
2568beef08fSSteen Hegelund 		 u32 addr,
2578beef08fSSteen Hegelund 		 int offset,
2588beef08fSSteen Hegelund 		 int count);
2598beef08fSSteen Hegelund 	/* informational */
2608beef08fSSteen Hegelund 	int (*port_info)
2618beef08fSSteen Hegelund 		(struct net_device *ndev,
262*e0305cc1SSteen Hegelund 		 struct vcap_admin *admin,
263*e0305cc1SSteen Hegelund 		 struct vcap_output_print *out);
26467456717SSteen Hegelund 	/* enable/disable the lookups in a vcap instance */
26567456717SSteen Hegelund 	int (*enable)
26667456717SSteen Hegelund 		(struct net_device *ndev,
26767456717SSteen Hegelund 		 struct vcap_admin *admin,
26867456717SSteen Hegelund 		 bool enable);
2698beef08fSSteen Hegelund };
2708beef08fSSteen Hegelund 
2718beef08fSSteen Hegelund /* VCAP API Client control interface */
2728beef08fSSteen Hegelund struct vcap_control {
2738beef08fSSteen Hegelund 	u32 rule_id; /* last used rule id (unique across VCAP instances) */
2748beef08fSSteen Hegelund 	struct vcap_operations *ops;  /* client supplied operations */
2758beef08fSSteen Hegelund 	const struct vcap_info *vcaps; /* client supplied vcap models */
2768beef08fSSteen Hegelund 	const struct vcap_statistics *stats; /* client supplied vcap stats */
2778beef08fSSteen Hegelund 	struct list_head list; /* list of vcap instances */
2788beef08fSSteen Hegelund };
2798beef08fSSteen Hegelund 
2808beef08fSSteen Hegelund /* Set client control interface on the API */
2818beef08fSSteen Hegelund int vcap_api_set_client(struct vcap_control *vctrl);
2828beef08fSSteen Hegelund 
2838beef08fSSteen Hegelund #endif /* __VCAP_API__ */
284