1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (C) 2019-2021, Intel Corporation. */
3 
4 #include "ice_vsi_vlan_ops.h"
5 #include "ice_vsi_vlan_lib.h"
6 #include "ice_vlan_mode.h"
7 #include "ice.h"
8 #include "ice_vf_vsi_vlan_ops.h"
9 #include "ice_sriov.h"
10 
11 static int
noop_vlan_arg(struct ice_vsi __always_unused * vsi,struct ice_vlan __always_unused * vlan)12 noop_vlan_arg(struct ice_vsi __always_unused *vsi,
13 	      struct ice_vlan __always_unused *vlan)
14 {
15 	return 0;
16 }
17 
18 static int
noop_vlan(struct ice_vsi __always_unused * vsi)19 noop_vlan(struct ice_vsi __always_unused *vsi)
20 {
21 	return 0;
22 }
23 
ice_port_vlan_on(struct ice_vsi * vsi)24 static void ice_port_vlan_on(struct ice_vsi *vsi)
25 {
26 	struct ice_vsi_vlan_ops *vlan_ops;
27 	struct ice_pf *pf = vsi->back;
28 
29 	/* setup inner VLAN ops */
30 	vlan_ops = &vsi->inner_vlan_ops;
31 
32 	if (ice_is_dvm_ena(&pf->hw)) {
33 		vlan_ops->add_vlan = noop_vlan_arg;
34 		vlan_ops->del_vlan = noop_vlan_arg;
35 		vlan_ops->ena_stripping = ice_vsi_ena_inner_stripping;
36 		vlan_ops->dis_stripping = ice_vsi_dis_inner_stripping;
37 		vlan_ops->ena_insertion = ice_vsi_ena_inner_insertion;
38 		vlan_ops->dis_insertion = ice_vsi_dis_inner_insertion;
39 
40 		/* setup outer VLAN ops */
41 		vlan_ops = &vsi->outer_vlan_ops;
42 		vlan_ops->set_port_vlan = ice_vsi_set_outer_port_vlan;
43 		vlan_ops->clear_port_vlan = ice_vsi_clear_outer_port_vlan;
44 	} else {
45 		vlan_ops->set_port_vlan = ice_vsi_set_inner_port_vlan;
46 		vlan_ops->clear_port_vlan = ice_vsi_clear_inner_port_vlan;
47 	}
48 
49 	/* all Rx traffic should be in the domain of the assigned port VLAN,
50 	 * so prevent disabling Rx VLAN filtering
51 	 */
52 	vlan_ops->dis_rx_filtering = noop_vlan;
53 
54 	vlan_ops->ena_rx_filtering = ice_vsi_ena_rx_vlan_filtering;
55 }
56 
ice_port_vlan_off(struct ice_vsi * vsi)57 static void ice_port_vlan_off(struct ice_vsi *vsi)
58 {
59 	struct ice_vsi_vlan_ops *vlan_ops;
60 	struct ice_pf *pf = vsi->back;
61 
62 	/* setup inner VLAN ops */
63 	vlan_ops = &vsi->inner_vlan_ops;
64 
65 	vlan_ops->ena_stripping = ice_vsi_ena_inner_stripping;
66 	vlan_ops->dis_stripping = ice_vsi_dis_inner_stripping;
67 	vlan_ops->ena_insertion = ice_vsi_ena_inner_insertion;
68 	vlan_ops->dis_insertion = ice_vsi_dis_inner_insertion;
69 
70 	if (ice_is_dvm_ena(&pf->hw)) {
71 		vlan_ops = &vsi->outer_vlan_ops;
72 
73 		vlan_ops->del_vlan = ice_vsi_del_vlan;
74 		vlan_ops->ena_stripping = ice_vsi_ena_outer_stripping;
75 		vlan_ops->dis_stripping = ice_vsi_dis_outer_stripping;
76 		vlan_ops->ena_insertion = ice_vsi_ena_outer_insertion;
77 		vlan_ops->dis_insertion = ice_vsi_dis_outer_insertion;
78 	} else {
79 		vlan_ops->del_vlan = ice_vsi_del_vlan;
80 	}
81 
82 	vlan_ops->dis_rx_filtering = ice_vsi_dis_rx_vlan_filtering;
83 
84 	if (!test_bit(ICE_FLAG_VF_VLAN_PRUNING, pf->flags))
85 		vlan_ops->ena_rx_filtering = noop_vlan;
86 	else
87 		vlan_ops->ena_rx_filtering =
88 			ice_vsi_ena_rx_vlan_filtering;
89 }
90 
91 /**
92  * ice_vf_vsi_enable_port_vlan - Set VSI VLAN ops to support port VLAN
93  * @vsi: VF's VSI being configured
94  *
95  * The function won't create port VLAN, it only allows to create port VLAN
96  * using VLAN ops on the VF VSI.
97  */
ice_vf_vsi_enable_port_vlan(struct ice_vsi * vsi)98 void ice_vf_vsi_enable_port_vlan(struct ice_vsi *vsi)
99 {
100 	if (WARN_ON_ONCE(!vsi->vf))
101 		return;
102 
103 	ice_port_vlan_on(vsi);
104 }
105 
106 /**
107  * ice_vf_vsi_disable_port_vlan - Clear VSI support for creating port VLAN
108  * @vsi: VF's VSI being configured
109  *
110  * The function should be called after removing port VLAN on VSI
111  * (using VLAN ops)
112  */
ice_vf_vsi_disable_port_vlan(struct ice_vsi * vsi)113 void ice_vf_vsi_disable_port_vlan(struct ice_vsi *vsi)
114 {
115 	if (WARN_ON_ONCE(!vsi->vf))
116 		return;
117 
118 	ice_port_vlan_off(vsi);
119 }
120 
121 /**
122  * ice_vf_vsi_init_vlan_ops - Initialize default VSI VLAN ops for VF VSI
123  * @vsi: VF's VSI being configured
124  *
125  * If Double VLAN Mode (DVM) is enabled, assume that the VF supports the new
126  * VIRTCHNL_VF_VLAN_OFFLOAD_V2 capability and set up the VLAN ops accordingly.
127  * If SVM is enabled maintain the same level of VLAN support previous to
128  * VIRTCHNL_VF_VLAN_OFFLOAD_V2.
129  */
ice_vf_vsi_init_vlan_ops(struct ice_vsi * vsi)130 void ice_vf_vsi_init_vlan_ops(struct ice_vsi *vsi)
131 {
132 	struct ice_vsi_vlan_ops *vlan_ops;
133 	struct ice_pf *pf = vsi->back;
134 	struct ice_vf *vf = vsi->vf;
135 
136 	if (WARN_ON(!vf))
137 		return;
138 
139 	if (ice_vf_is_port_vlan_ena(vf))
140 		ice_port_vlan_on(vsi);
141 	else
142 		ice_port_vlan_off(vsi);
143 
144 	vlan_ops = ice_is_dvm_ena(&pf->hw) ?
145 		&vsi->outer_vlan_ops : &vsi->inner_vlan_ops;
146 
147 	vlan_ops->add_vlan = ice_vsi_add_vlan;
148 	vlan_ops->ena_tx_filtering = ice_vsi_ena_tx_vlan_filtering;
149 	vlan_ops->dis_tx_filtering = ice_vsi_dis_tx_vlan_filtering;
150 }
151 
152 /**
153  * ice_vf_vsi_cfg_dvm_legacy_vlan_mode - Config VLAN mode for old VFs in DVM
154  * @vsi: VF's VSI being configured
155  *
156  * This should only be called when Double VLAN Mode (DVM) is enabled, there
157  * is not a port VLAN enabled on this VF, and the VF negotiates
158  * VIRTCHNL_VF_OFFLOAD_VLAN.
159  *
160  * This function sets up the VF VSI's inner and outer ice_vsi_vlan_ops and also
161  * initializes software only VLAN mode (i.e. allow all VLANs). Also, use no-op
162  * implementations for any functions that may be called during the lifetime of
163  * the VF so these methods do nothing and succeed.
164  */
ice_vf_vsi_cfg_dvm_legacy_vlan_mode(struct ice_vsi * vsi)165 void ice_vf_vsi_cfg_dvm_legacy_vlan_mode(struct ice_vsi *vsi)
166 {
167 	struct ice_vsi_vlan_ops *vlan_ops;
168 	struct ice_vf *vf = vsi->vf;
169 	struct device *dev;
170 
171 	if (WARN_ON(!vf))
172 		return;
173 
174 	dev = ice_pf_to_dev(vf->pf);
175 
176 	if (!ice_is_dvm_ena(&vsi->back->hw) || ice_vf_is_port_vlan_ena(vf))
177 		return;
178 
179 	vlan_ops = &vsi->outer_vlan_ops;
180 
181 	/* Rx VLAN filtering always disabled to allow software offloaded VLANs
182 	 * for VFs that only support VIRTCHNL_VF_OFFLOAD_VLAN and don't have a
183 	 * port VLAN configured
184 	 */
185 	vlan_ops->dis_rx_filtering = ice_vsi_dis_rx_vlan_filtering;
186 	/* Don't fail when attempting to enable Rx VLAN filtering */
187 	vlan_ops->ena_rx_filtering = noop_vlan;
188 
189 	/* Tx VLAN filtering always disabled to allow software offloaded VLANs
190 	 * for VFs that only support VIRTCHNL_VF_OFFLOAD_VLAN and don't have a
191 	 * port VLAN configured
192 	 */
193 	vlan_ops->dis_tx_filtering = ice_vsi_dis_tx_vlan_filtering;
194 	/* Don't fail when attempting to enable Tx VLAN filtering */
195 	vlan_ops->ena_tx_filtering = noop_vlan;
196 
197 	if (vlan_ops->dis_rx_filtering(vsi))
198 		dev_dbg(dev, "Failed to disable Rx VLAN filtering for old VF without VIRTCHNL_VF_OFFLOAD_VLAN_V2 support\n");
199 	if (vlan_ops->dis_tx_filtering(vsi))
200 		dev_dbg(dev, "Failed to disable Tx VLAN filtering for old VF without VIRTHCNL_VF_OFFLOAD_VLAN_V2 support\n");
201 
202 	/* All outer VLAN offloads must be disabled */
203 	vlan_ops->dis_stripping = ice_vsi_dis_outer_stripping;
204 	vlan_ops->dis_insertion = ice_vsi_dis_outer_insertion;
205 
206 	if (vlan_ops->dis_stripping(vsi))
207 		dev_dbg(dev, "Failed to disable outer VLAN stripping for old VF without VIRTCHNL_VF_OFFLOAD_VLAN_V2 support\n");
208 
209 	if (vlan_ops->dis_insertion(vsi))
210 		dev_dbg(dev, "Failed to disable outer VLAN insertion for old VF without VIRTCHNL_VF_OFFLOAD_VLAN_V2 support\n");
211 
212 	/* All inner VLAN offloads must be disabled */
213 	vlan_ops = &vsi->inner_vlan_ops;
214 
215 	vlan_ops->dis_stripping = ice_vsi_dis_outer_stripping;
216 	vlan_ops->dis_insertion = ice_vsi_dis_outer_insertion;
217 
218 	if (vlan_ops->dis_stripping(vsi))
219 		dev_dbg(dev, "Failed to disable inner VLAN stripping for old VF without VIRTCHNL_VF_OFFLOAD_VLAN_V2 support\n");
220 
221 	if (vlan_ops->dis_insertion(vsi))
222 		dev_dbg(dev, "Failed to disable inner VLAN insertion for old VF without VIRTCHNL_VF_OFFLOAD_VLAN_V2 support\n");
223 }
224 
225 /**
226  * ice_vf_vsi_cfg_svm_legacy_vlan_mode - Config VLAN mode for old VFs in SVM
227  * @vsi: VF's VSI being configured
228  *
229  * This should only be called when Single VLAN Mode (SVM) is enabled, there is
230  * not a port VLAN enabled on this VF, and the VF negotiates
231  * VIRTCHNL_VF_OFFLOAD_VLAN.
232  *
233  * All of the normal SVM VLAN ops are identical for this case. However, by
234  * default Rx VLAN filtering should be turned off by default in this case.
235  */
ice_vf_vsi_cfg_svm_legacy_vlan_mode(struct ice_vsi * vsi)236 void ice_vf_vsi_cfg_svm_legacy_vlan_mode(struct ice_vsi *vsi)
237 {
238 	struct ice_vf *vf = vsi->vf;
239 
240 	if (WARN_ON(!vf))
241 		return;
242 
243 	if (ice_is_dvm_ena(&vsi->back->hw) || ice_vf_is_port_vlan_ena(vf))
244 		return;
245 
246 	if (vsi->inner_vlan_ops.dis_rx_filtering(vsi))
247 		dev_dbg(ice_pf_to_dev(vf->pf), "Failed to disable Rx VLAN filtering for old VF with VIRTCHNL_VF_OFFLOAD_VLAN support\n");
248 }
249