1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (C) 2021, Intel Corporation. */
3 
4 #include "ice_virtchnl_allowlist.h"
5 
6 /* Purpose of this file is to share functionality to allowlist or denylist
7  * opcodes used in PF <-> VF communication. Group of opcodes:
8  * - default -> should be always allowed after creating VF,
9  *   default_allowlist_opcodes
10  * - opcodes needed by VF to work correctly, but not associated with caps ->
11  *   should be allowed after successful VF resources allocation,
12  *   working_allowlist_opcodes
13  * - opcodes needed by VF when caps are activated
14  *
15  * Caps that don't use new opcodes (no opcodes should be allowed):
16  * - VIRTCHNL_VF_OFFLOAD_WB_ON_ITR
17  * - VIRTCHNL_VF_OFFLOAD_CRC
18  * - VIRTCHNL_VF_OFFLOAD_RX_POLLING
19  * - VIRTCHNL_VF_OFFLOAD_RSS_PCTYPE_V2
20  * - VIRTCHNL_VF_OFFLOAD_ENCAP
21  * - VIRTCHNL_VF_OFFLOAD_ENCAP_CSUM
22  * - VIRTCHNL_VF_OFFLOAD_RX_ENCAP_CSUM
23  * - VIRTCHNL_VF_OFFLOAD_USO
24  */
25 
26 /* default opcodes to communicate with VF */
27 static const u32 default_allowlist_opcodes[] = {
28 	VIRTCHNL_OP_GET_VF_RESOURCES, VIRTCHNL_OP_VERSION, VIRTCHNL_OP_RESET_VF,
29 };
30 
31 /* opcodes supported after successful VIRTCHNL_OP_GET_VF_RESOURCES */
32 static const u32 working_allowlist_opcodes[] = {
33 	VIRTCHNL_OP_CONFIG_TX_QUEUE, VIRTCHNL_OP_CONFIG_RX_QUEUE,
34 	VIRTCHNL_OP_CONFIG_VSI_QUEUES, VIRTCHNL_OP_CONFIG_IRQ_MAP,
35 	VIRTCHNL_OP_ENABLE_QUEUES, VIRTCHNL_OP_DISABLE_QUEUES,
36 	VIRTCHNL_OP_GET_STATS, VIRTCHNL_OP_EVENT,
37 };
38 
39 /* VIRTCHNL_VF_OFFLOAD_L2 */
40 static const u32 l2_allowlist_opcodes[] = {
41 	VIRTCHNL_OP_ADD_ETH_ADDR, VIRTCHNL_OP_DEL_ETH_ADDR,
42 	VIRTCHNL_OP_CONFIG_PROMISCUOUS_MODE,
43 };
44 
45 /* VIRTCHNL_VF_OFFLOAD_REQ_QUEUES */
46 static const u32 req_queues_allowlist_opcodes[] = {
47 	VIRTCHNL_OP_REQUEST_QUEUES,
48 };
49 
50 /* VIRTCHNL_VF_OFFLOAD_VLAN */
51 static const u32 vlan_allowlist_opcodes[] = {
52 	VIRTCHNL_OP_ADD_VLAN, VIRTCHNL_OP_DEL_VLAN,
53 	VIRTCHNL_OP_ENABLE_VLAN_STRIPPING, VIRTCHNL_OP_DISABLE_VLAN_STRIPPING,
54 };
55 
56 /* VIRTCHNL_VF_OFFLOAD_VLAN_V2 */
57 static const u32 vlan_v2_allowlist_opcodes[] = {
58 	VIRTCHNL_OP_GET_OFFLOAD_VLAN_V2_CAPS, VIRTCHNL_OP_ADD_VLAN_V2,
59 	VIRTCHNL_OP_DEL_VLAN_V2, VIRTCHNL_OP_ENABLE_VLAN_STRIPPING_V2,
60 	VIRTCHNL_OP_DISABLE_VLAN_STRIPPING_V2,
61 	VIRTCHNL_OP_ENABLE_VLAN_INSERTION_V2,
62 	VIRTCHNL_OP_DISABLE_VLAN_INSERTION_V2,
63 };
64 
65 /* VIRTCHNL_VF_OFFLOAD_RSS_PF */
66 static const u32 rss_pf_allowlist_opcodes[] = {
67 	VIRTCHNL_OP_CONFIG_RSS_KEY, VIRTCHNL_OP_CONFIG_RSS_LUT,
68 	VIRTCHNL_OP_GET_RSS_HENA_CAPS, VIRTCHNL_OP_SET_RSS_HENA,
69 };
70 
71 /* VIRTCHNL_VF_OFFLOAD_RX_FLEX_DESC */
72 static const u32 rx_flex_desc_allowlist_opcodes[] = {
73 	VIRTCHNL_OP_GET_SUPPORTED_RXDIDS,
74 };
75 
76 /* VIRTCHNL_VF_OFFLOAD_ADV_RSS_PF */
77 static const u32 adv_rss_pf_allowlist_opcodes[] = {
78 	VIRTCHNL_OP_ADD_RSS_CFG, VIRTCHNL_OP_DEL_RSS_CFG,
79 };
80 
81 /* VIRTCHNL_VF_OFFLOAD_FDIR_PF */
82 static const u32 fdir_pf_allowlist_opcodes[] = {
83 	VIRTCHNL_OP_ADD_FDIR_FILTER, VIRTCHNL_OP_DEL_FDIR_FILTER,
84 };
85 
86 struct allowlist_opcode_info {
87 	const u32 *opcodes;
88 	size_t size;
89 };
90 
91 #define BIT_INDEX(caps) (HWEIGHT((caps) - 1))
92 #define ALLOW_ITEM(caps, list) \
93 	[BIT_INDEX(caps)] = { \
94 		.opcodes = list, \
95 		.size = ARRAY_SIZE(list) \
96 	}
97 static const struct allowlist_opcode_info allowlist_opcodes[] = {
98 	ALLOW_ITEM(VIRTCHNL_VF_OFFLOAD_L2, l2_allowlist_opcodes),
99 	ALLOW_ITEM(VIRTCHNL_VF_OFFLOAD_REQ_QUEUES, req_queues_allowlist_opcodes),
100 	ALLOW_ITEM(VIRTCHNL_VF_OFFLOAD_VLAN, vlan_allowlist_opcodes),
101 	ALLOW_ITEM(VIRTCHNL_VF_OFFLOAD_RSS_PF, rss_pf_allowlist_opcodes),
102 	ALLOW_ITEM(VIRTCHNL_VF_OFFLOAD_RX_FLEX_DESC, rx_flex_desc_allowlist_opcodes),
103 	ALLOW_ITEM(VIRTCHNL_VF_OFFLOAD_ADV_RSS_PF, adv_rss_pf_allowlist_opcodes),
104 	ALLOW_ITEM(VIRTCHNL_VF_OFFLOAD_FDIR_PF, fdir_pf_allowlist_opcodes),
105 	ALLOW_ITEM(VIRTCHNL_VF_OFFLOAD_VLAN_V2, vlan_v2_allowlist_opcodes),
106 };
107 
108 /**
109  * ice_vc_is_opcode_allowed - check if this opcode is allowed on this VF
110  * @vf: pointer to VF structure
111  * @opcode: virtchnl opcode
112  *
113  * Return true if message is allowed on this VF
114  */
ice_vc_is_opcode_allowed(struct ice_vf * vf,u32 opcode)115 bool ice_vc_is_opcode_allowed(struct ice_vf *vf, u32 opcode)
116 {
117 	if (opcode >= VIRTCHNL_OP_MAX)
118 		return false;
119 
120 	return test_bit(opcode, vf->opcodes_allowlist);
121 }
122 
123 /**
124  * ice_vc_allowlist_opcodes - allowlist selected opcodes
125  * @vf: pointer to VF structure
126  * @opcodes: array of opocodes to allowlist
127  * @size: size of opcodes array
128  *
129  * Function should be called to allowlist opcodes on VF.
130  */
131 static void
ice_vc_allowlist_opcodes(struct ice_vf * vf,const u32 * opcodes,size_t size)132 ice_vc_allowlist_opcodes(struct ice_vf *vf, const u32 *opcodes, size_t size)
133 {
134 	unsigned int i;
135 
136 	for (i = 0; i < size; i++)
137 		set_bit(opcodes[i], vf->opcodes_allowlist);
138 }
139 
140 /**
141  * ice_vc_clear_allowlist - clear all allowlist opcodes
142  * @vf: pointer to VF structure
143  */
ice_vc_clear_allowlist(struct ice_vf * vf)144 static void ice_vc_clear_allowlist(struct ice_vf *vf)
145 {
146 	bitmap_zero(vf->opcodes_allowlist, VIRTCHNL_OP_MAX);
147 }
148 
149 /**
150  * ice_vc_set_default_allowlist - allowlist default opcodes for VF
151  * @vf: pointer to VF structure
152  */
ice_vc_set_default_allowlist(struct ice_vf * vf)153 void ice_vc_set_default_allowlist(struct ice_vf *vf)
154 {
155 	ice_vc_clear_allowlist(vf);
156 	ice_vc_allowlist_opcodes(vf, default_allowlist_opcodes,
157 				 ARRAY_SIZE(default_allowlist_opcodes));
158 }
159 
160 /**
161  * ice_vc_set_working_allowlist - allowlist opcodes needed to by VF to work
162  * @vf: pointer to VF structure
163  *
164  * allowlist opcodes that aren't associated with specific caps, but
165  * are needed by VF to work.
166  */
ice_vc_set_working_allowlist(struct ice_vf * vf)167 void ice_vc_set_working_allowlist(struct ice_vf *vf)
168 {
169 	ice_vc_allowlist_opcodes(vf, working_allowlist_opcodes,
170 				 ARRAY_SIZE(working_allowlist_opcodes));
171 }
172 
173 /**
174  * ice_vc_set_caps_allowlist - allowlist VF opcodes according caps
175  * @vf: pointer to VF structure
176  */
ice_vc_set_caps_allowlist(struct ice_vf * vf)177 void ice_vc_set_caps_allowlist(struct ice_vf *vf)
178 {
179 	unsigned long caps = vf->driver_caps;
180 	unsigned int i;
181 
182 	for_each_set_bit(i, &caps, ARRAY_SIZE(allowlist_opcodes))
183 		ice_vc_allowlist_opcodes(vf, allowlist_opcodes[i].opcodes,
184 					 allowlist_opcodes[i].size);
185 }
186