1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (C) 2019-2021, Intel Corporation. */
3 
4 #include "ice_vsi_vlan_lib.h"
5 #include "ice_lib.h"
6 #include "ice_fltr.h"
7 #include "ice.h"
8 
9 static void print_invalid_tpid(struct ice_vsi *vsi, u16 tpid)
10 {
11 	dev_err(ice_pf_to_dev(vsi->back), "%s %d specified invalid VLAN tpid 0x%04x\n",
12 		ice_vsi_type_str(vsi->type), vsi->idx, tpid);
13 }
14 
15 /**
16  * validate_vlan - check if the ice_vlan passed in is valid
17  * @vsi: VSI used for printing error message
18  * @vlan: ice_vlan structure to validate
19  *
20  * Return true if the VLAN TPID is valid or if the VLAN TPID is 0 and the VLAN
21  * VID is 0, which allows for non-zero VLAN filters with the specified VLAN TPID
22  * and untagged VLAN 0 filters to be added to the prune list respectively.
23  */
24 static bool validate_vlan(struct ice_vsi *vsi, struct ice_vlan *vlan)
25 {
26 	if (vlan->tpid != ETH_P_8021Q && vlan->tpid != ETH_P_8021AD &&
27 	    vlan->tpid != ETH_P_QINQ1 && (vlan->tpid || vlan->vid)) {
28 		print_invalid_tpid(vsi, vlan->tpid);
29 		return false;
30 	}
31 
32 	return true;
33 }
34 
35 /**
36  * ice_vsi_add_vlan - default add VLAN implementation for all VSI types
37  * @vsi: VSI being configured
38  * @vlan: VLAN filter to add
39  */
40 int ice_vsi_add_vlan(struct ice_vsi *vsi, struct ice_vlan *vlan)
41 {
42 	int err;
43 
44 	if (!validate_vlan(vsi, vlan))
45 		return -EINVAL;
46 
47 	err = ice_fltr_add_vlan(vsi, vlan);
48 	if (err && err != -EEXIST) {
49 		dev_err(ice_pf_to_dev(vsi->back), "Failure Adding VLAN %d on VSI %i, status %d\n",
50 			vlan->vid, vsi->vsi_num, err);
51 		return err;
52 	}
53 
54 	vsi->num_vlan++;
55 	return 0;
56 }
57 
58 /**
59  * ice_vsi_del_vlan - default del VLAN implementation for all VSI types
60  * @vsi: VSI being configured
61  * @vlan: VLAN filter to delete
62  */
63 int ice_vsi_del_vlan(struct ice_vsi *vsi, struct ice_vlan *vlan)
64 {
65 	struct ice_pf *pf = vsi->back;
66 	struct device *dev;
67 	int err;
68 
69 	if (!validate_vlan(vsi, vlan))
70 		return -EINVAL;
71 
72 	dev = ice_pf_to_dev(pf);
73 
74 	err = ice_fltr_remove_vlan(vsi, vlan);
75 	if (!err)
76 		vsi->num_vlan--;
77 	else if (err == -ENOENT || err == -EBUSY)
78 		err = 0;
79 	else
80 		dev_err(dev, "Error removing VLAN %d on VSI %i error: %d\n",
81 			vlan->vid, vsi->vsi_num, err);
82 
83 	return err;
84 }
85 
86 /**
87  * ice_vsi_manage_vlan_insertion - Manage VLAN insertion for the VSI for Tx
88  * @vsi: the VSI being changed
89  */
90 static int ice_vsi_manage_vlan_insertion(struct ice_vsi *vsi)
91 {
92 	struct ice_hw *hw = &vsi->back->hw;
93 	struct ice_vsi_ctx *ctxt;
94 	int err;
95 
96 	ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
97 	if (!ctxt)
98 		return -ENOMEM;
99 
100 	/* Here we are configuring the VSI to let the driver add VLAN tags by
101 	 * setting inner_vlan_flags to ICE_AQ_VSI_INNER_VLAN_TX_MODE_ALL. The actual VLAN tag
102 	 * insertion happens in the Tx hot path, in ice_tx_map.
103 	 */
104 	ctxt->info.inner_vlan_flags = ICE_AQ_VSI_INNER_VLAN_TX_MODE_ALL;
105 
106 	/* Preserve existing VLAN strip setting */
107 	ctxt->info.inner_vlan_flags |= (vsi->info.inner_vlan_flags &
108 					ICE_AQ_VSI_INNER_VLAN_EMODE_M);
109 
110 	ctxt->info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_VLAN_VALID);
111 
112 	err = ice_update_vsi(hw, vsi->idx, ctxt, NULL);
113 	if (err) {
114 		dev_err(ice_pf_to_dev(vsi->back), "update VSI for VLAN insert failed, err %d aq_err %s\n",
115 			err, ice_aq_str(hw->adminq.sq_last_status));
116 		goto out;
117 	}
118 
119 	vsi->info.inner_vlan_flags = ctxt->info.inner_vlan_flags;
120 out:
121 	kfree(ctxt);
122 	return err;
123 }
124 
125 /**
126  * ice_vsi_manage_vlan_stripping - Manage VLAN stripping for the VSI for Rx
127  * @vsi: the VSI being changed
128  * @ena: boolean value indicating if this is a enable or disable request
129  */
130 static int ice_vsi_manage_vlan_stripping(struct ice_vsi *vsi, bool ena)
131 {
132 	struct ice_hw *hw = &vsi->back->hw;
133 	struct ice_vsi_ctx *ctxt;
134 	u8 *ivf;
135 	int err;
136 
137 	/* do not allow modifying VLAN stripping when a port VLAN is configured
138 	 * on this VSI
139 	 */
140 	if (vsi->info.port_based_inner_vlan)
141 		return 0;
142 
143 	ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
144 	if (!ctxt)
145 		return -ENOMEM;
146 
147 	ivf = &ctxt->info.inner_vlan_flags;
148 
149 	/* Here we are configuring what the VSI should do with the VLAN tag in
150 	 * the Rx packet. We can either leave the tag in the packet or put it in
151 	 * the Rx descriptor.
152 	 */
153 	if (ena) {
154 		/* Strip VLAN tag from Rx packet and put it in the desc */
155 		*ivf = FIELD_PREP(ICE_AQ_VSI_INNER_VLAN_EMODE_M,
156 				  ICE_AQ_VSI_INNER_VLAN_EMODE_STR_BOTH);
157 	} else {
158 		/* Disable stripping. Leave tag in packet */
159 		*ivf = FIELD_PREP(ICE_AQ_VSI_INNER_VLAN_EMODE_M,
160 				  ICE_AQ_VSI_INNER_VLAN_EMODE_NOTHING);
161 	}
162 
163 	/* Allow all packets untagged/tagged */
164 	*ivf |= ICE_AQ_VSI_INNER_VLAN_TX_MODE_ALL;
165 
166 	ctxt->info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_VLAN_VALID);
167 
168 	err = ice_update_vsi(hw, vsi->idx, ctxt, NULL);
169 	if (err) {
170 		dev_err(ice_pf_to_dev(vsi->back), "update VSI for VLAN strip failed, ena = %d err %d aq_err %s\n",
171 			ena, err, ice_aq_str(hw->adminq.sq_last_status));
172 		goto out;
173 	}
174 
175 	vsi->info.inner_vlan_flags = ctxt->info.inner_vlan_flags;
176 out:
177 	kfree(ctxt);
178 	return err;
179 }
180 
181 int ice_vsi_ena_inner_stripping(struct ice_vsi *vsi, const u16 tpid)
182 {
183 	if (tpid != ETH_P_8021Q) {
184 		print_invalid_tpid(vsi, tpid);
185 		return -EINVAL;
186 	}
187 
188 	return ice_vsi_manage_vlan_stripping(vsi, true);
189 }
190 
191 int ice_vsi_dis_inner_stripping(struct ice_vsi *vsi)
192 {
193 	return ice_vsi_manage_vlan_stripping(vsi, false);
194 }
195 
196 int ice_vsi_ena_inner_insertion(struct ice_vsi *vsi, const u16 tpid)
197 {
198 	if (tpid != ETH_P_8021Q) {
199 		print_invalid_tpid(vsi, tpid);
200 		return -EINVAL;
201 	}
202 
203 	return ice_vsi_manage_vlan_insertion(vsi);
204 }
205 
206 int ice_vsi_dis_inner_insertion(struct ice_vsi *vsi)
207 {
208 	return ice_vsi_manage_vlan_insertion(vsi);
209 }
210 
211 static void
212 ice_save_vlan_info(struct ice_aqc_vsi_props *info,
213 		   struct ice_vsi_vlan_info *vlan)
214 {
215 	vlan->sw_flags2 = info->sw_flags2;
216 	vlan->inner_vlan_flags = info->inner_vlan_flags;
217 	vlan->outer_vlan_flags = info->outer_vlan_flags;
218 }
219 
220 static void
221 ice_restore_vlan_info(struct ice_aqc_vsi_props *info,
222 		      struct ice_vsi_vlan_info *vlan)
223 {
224 	info->sw_flags2 = vlan->sw_flags2;
225 	info->inner_vlan_flags = vlan->inner_vlan_flags;
226 	info->outer_vlan_flags = vlan->outer_vlan_flags;
227 }
228 
229 /**
230  * __ice_vsi_set_inner_port_vlan - set port VLAN VSI context settings to enable a port VLAN
231  * @vsi: the VSI to update
232  * @pvid_info: VLAN ID and QoS used to set the PVID VSI context field
233  */
234 static int __ice_vsi_set_inner_port_vlan(struct ice_vsi *vsi, u16 pvid_info)
235 {
236 	struct ice_hw *hw = &vsi->back->hw;
237 	struct ice_aqc_vsi_props *info;
238 	struct ice_vsi_ctx *ctxt;
239 	int ret;
240 
241 	ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
242 	if (!ctxt)
243 		return -ENOMEM;
244 
245 	ice_save_vlan_info(&vsi->info, &vsi->vlan_info);
246 	ctxt->info = vsi->info;
247 	info = &ctxt->info;
248 	info->inner_vlan_flags = ICE_AQ_VSI_INNER_VLAN_TX_MODE_ACCEPTUNTAGGED |
249 		ICE_AQ_VSI_INNER_VLAN_INSERT_PVID |
250 		ICE_AQ_VSI_INNER_VLAN_EMODE_STR;
251 	info->sw_flags2 |= ICE_AQ_VSI_SW_FLAG_RX_VLAN_PRUNE_ENA;
252 
253 	info->port_based_inner_vlan = cpu_to_le16(pvid_info);
254 	info->valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_VLAN_VALID |
255 					   ICE_AQ_VSI_PROP_SW_VALID);
256 
257 	ret = ice_update_vsi(hw, vsi->idx, ctxt, NULL);
258 	if (ret) {
259 		dev_info(ice_hw_to_dev(hw), "update VSI for port VLAN failed, err %d aq_err %s\n",
260 			 ret, ice_aq_str(hw->adminq.sq_last_status));
261 		goto out;
262 	}
263 
264 	vsi->info.inner_vlan_flags = info->inner_vlan_flags;
265 	vsi->info.sw_flags2 = info->sw_flags2;
266 	vsi->info.port_based_inner_vlan = info->port_based_inner_vlan;
267 out:
268 	kfree(ctxt);
269 	return ret;
270 }
271 
272 int ice_vsi_set_inner_port_vlan(struct ice_vsi *vsi, struct ice_vlan *vlan)
273 {
274 	u16 port_vlan_info;
275 
276 	if (vlan->tpid != ETH_P_8021Q)
277 		return -EINVAL;
278 
279 	if (vlan->prio > 7)
280 		return -EINVAL;
281 
282 	port_vlan_info = vlan->vid | (vlan->prio << VLAN_PRIO_SHIFT);
283 
284 	return __ice_vsi_set_inner_port_vlan(vsi, port_vlan_info);
285 }
286 
287 int ice_vsi_clear_inner_port_vlan(struct ice_vsi *vsi)
288 {
289 	struct ice_hw *hw = &vsi->back->hw;
290 	struct ice_aqc_vsi_props *info;
291 	struct ice_vsi_ctx *ctxt;
292 	int ret;
293 
294 	ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
295 	if (!ctxt)
296 		return -ENOMEM;
297 
298 	ice_restore_vlan_info(&vsi->info, &vsi->vlan_info);
299 	vsi->info.port_based_inner_vlan = 0;
300 	ctxt->info = vsi->info;
301 	info = &ctxt->info;
302 	info->valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_VLAN_VALID |
303 					   ICE_AQ_VSI_PROP_SW_VALID);
304 
305 	ret = ice_update_vsi(hw, vsi->idx, ctxt, NULL);
306 	if (ret)
307 		dev_err(ice_hw_to_dev(hw), "update VSI for port VLAN failed, err %d aq_err %s\n",
308 			ret, ice_aq_str(hw->adminq.sq_last_status));
309 
310 	kfree(ctxt);
311 	return ret;
312 }
313 
314 /**
315  * ice_cfg_vlan_pruning - enable or disable VLAN pruning on the VSI
316  * @vsi: VSI to enable or disable VLAN pruning on
317  * @ena: set to true to enable VLAN pruning and false to disable it
318  *
319  * returns 0 if VSI is updated, negative otherwise
320  */
321 static int ice_cfg_vlan_pruning(struct ice_vsi *vsi, bool ena)
322 {
323 	struct ice_vsi_ctx *ctxt;
324 	struct ice_pf *pf;
325 	int status;
326 
327 	if (!vsi)
328 		return -EINVAL;
329 
330 	/* Don't enable VLAN pruning if the netdev is currently in promiscuous
331 	 * mode. VLAN pruning will be enabled when the interface exits
332 	 * promiscuous mode if any VLAN filters are active.
333 	 */
334 	if (vsi->netdev && vsi->netdev->flags & IFF_PROMISC && ena)
335 		return 0;
336 
337 	pf = vsi->back;
338 	ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
339 	if (!ctxt)
340 		return -ENOMEM;
341 
342 	ctxt->info = vsi->info;
343 
344 	if (ena)
345 		ctxt->info.sw_flags2 |= ICE_AQ_VSI_SW_FLAG_RX_VLAN_PRUNE_ENA;
346 	else
347 		ctxt->info.sw_flags2 &= ~ICE_AQ_VSI_SW_FLAG_RX_VLAN_PRUNE_ENA;
348 
349 	ctxt->info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_SW_VALID);
350 
351 	status = ice_update_vsi(&pf->hw, vsi->idx, ctxt, NULL);
352 	if (status) {
353 		netdev_err(vsi->netdev, "%sabling VLAN pruning on VSI handle: %d, VSI HW ID: %d failed, err = %d, aq_err = %s\n",
354 			   ena ? "En" : "Dis", vsi->idx, vsi->vsi_num, status,
355 			   ice_aq_str(pf->hw.adminq.sq_last_status));
356 		goto err_out;
357 	}
358 
359 	vsi->info.sw_flags2 = ctxt->info.sw_flags2;
360 
361 	kfree(ctxt);
362 	return 0;
363 
364 err_out:
365 	kfree(ctxt);
366 	return status;
367 }
368 
369 int ice_vsi_ena_rx_vlan_filtering(struct ice_vsi *vsi)
370 {
371 	return ice_cfg_vlan_pruning(vsi, true);
372 }
373 
374 int ice_vsi_dis_rx_vlan_filtering(struct ice_vsi *vsi)
375 {
376 	return ice_cfg_vlan_pruning(vsi, false);
377 }
378 
379 static int ice_cfg_vlan_antispoof(struct ice_vsi *vsi, bool enable)
380 {
381 	struct ice_vsi_ctx *ctx;
382 	int err;
383 
384 	ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
385 	if (!ctx)
386 		return -ENOMEM;
387 
388 	ctx->info.sec_flags = vsi->info.sec_flags;
389 	ctx->info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_SECURITY_VALID);
390 
391 	if (enable)
392 		ctx->info.sec_flags |= ICE_AQ_VSI_SEC_TX_VLAN_PRUNE_ENA <<
393 			ICE_AQ_VSI_SEC_TX_PRUNE_ENA_S;
394 	else
395 		ctx->info.sec_flags &= ~(ICE_AQ_VSI_SEC_TX_VLAN_PRUNE_ENA <<
396 					 ICE_AQ_VSI_SEC_TX_PRUNE_ENA_S);
397 
398 	err = ice_update_vsi(&vsi->back->hw, vsi->idx, ctx, NULL);
399 	if (err)
400 		dev_err(ice_pf_to_dev(vsi->back), "Failed to configure Tx VLAN anti-spoof %s for VSI %d, error %d\n",
401 			enable ? "ON" : "OFF", vsi->vsi_num, err);
402 	else
403 		vsi->info.sec_flags = ctx->info.sec_flags;
404 
405 	kfree(ctx);
406 
407 	return err;
408 }
409 
410 int ice_vsi_ena_tx_vlan_filtering(struct ice_vsi *vsi)
411 {
412 	return ice_cfg_vlan_antispoof(vsi, true);
413 }
414 
415 int ice_vsi_dis_tx_vlan_filtering(struct ice_vsi *vsi)
416 {
417 	return ice_cfg_vlan_antispoof(vsi, false);
418 }
419 
420 /**
421  * tpid_to_vsi_outer_vlan_type - convert from TPID to VSI context based tag_type
422  * @tpid: tpid used to translate into VSI context based tag_type
423  * @tag_type: output variable to hold the VSI context based tag type
424  */
425 static int tpid_to_vsi_outer_vlan_type(u16 tpid, u8 *tag_type)
426 {
427 	switch (tpid) {
428 	case ETH_P_8021Q:
429 		*tag_type = ICE_AQ_VSI_OUTER_TAG_VLAN_8100;
430 		break;
431 	case ETH_P_8021AD:
432 		*tag_type = ICE_AQ_VSI_OUTER_TAG_STAG;
433 		break;
434 	case ETH_P_QINQ1:
435 		*tag_type = ICE_AQ_VSI_OUTER_TAG_VLAN_9100;
436 		break;
437 	default:
438 		*tag_type = 0;
439 		return -EINVAL;
440 	}
441 
442 	return 0;
443 }
444 
445 /**
446  * ice_vsi_ena_outer_stripping - enable outer VLAN stripping
447  * @vsi: VSI to configure
448  * @tpid: TPID to enable outer VLAN stripping for
449  *
450  * Enable outer VLAN stripping via VSI context. This function should only be
451  * used if DVM is supported. Also, this function should never be called directly
452  * as it should be part of ice_vsi_vlan_ops if it's needed.
453  *
454  * Since the VSI context only supports a single TPID for insertion and
455  * stripping, setting the TPID for stripping will affect the TPID for insertion.
456  * Callers need to be aware of this limitation.
457  *
458  * Only modify outer VLAN stripping settings and the VLAN TPID. Outer VLAN
459  * insertion settings are unmodified.
460  *
461  * This enables hardware to strip a VLAN tag with the specified TPID to be
462  * stripped from the packet and placed in the receive descriptor.
463  */
464 int ice_vsi_ena_outer_stripping(struct ice_vsi *vsi, u16 tpid)
465 {
466 	struct ice_hw *hw = &vsi->back->hw;
467 	struct ice_vsi_ctx *ctxt;
468 	u8 tag_type;
469 	int err;
470 
471 	/* do not allow modifying VLAN stripping when a port VLAN is configured
472 	 * on this VSI
473 	 */
474 	if (vsi->info.port_based_outer_vlan)
475 		return 0;
476 
477 	if (tpid_to_vsi_outer_vlan_type(tpid, &tag_type))
478 		return -EINVAL;
479 
480 	ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
481 	if (!ctxt)
482 		return -ENOMEM;
483 
484 	ctxt->info.valid_sections =
485 		cpu_to_le16(ICE_AQ_VSI_PROP_OUTER_TAG_VALID);
486 	/* clear current outer VLAN strip settings */
487 	ctxt->info.outer_vlan_flags = vsi->info.outer_vlan_flags &
488 		~(ICE_AQ_VSI_OUTER_VLAN_EMODE_M | ICE_AQ_VSI_OUTER_TAG_TYPE_M);
489 	ctxt->info.outer_vlan_flags |=
490 		((ICE_AQ_VSI_OUTER_VLAN_EMODE_SHOW_BOTH <<
491 		  ICE_AQ_VSI_OUTER_VLAN_EMODE_S) |
492 		 ((tag_type << ICE_AQ_VSI_OUTER_TAG_TYPE_S) &
493 		  ICE_AQ_VSI_OUTER_TAG_TYPE_M));
494 
495 	err = ice_update_vsi(hw, vsi->idx, ctxt, NULL);
496 	if (err)
497 		dev_err(ice_pf_to_dev(vsi->back), "update VSI for enabling outer VLAN stripping failed, err %d aq_err %s\n",
498 			err, ice_aq_str(hw->adminq.sq_last_status));
499 	else
500 		vsi->info.outer_vlan_flags = ctxt->info.outer_vlan_flags;
501 
502 	kfree(ctxt);
503 	return err;
504 }
505 
506 /**
507  * ice_vsi_dis_outer_stripping - disable outer VLAN stripping
508  * @vsi: VSI to configure
509  *
510  * Disable outer VLAN stripping via VSI context. This function should only be
511  * used if DVM is supported. Also, this function should never be called directly
512  * as it should be part of ice_vsi_vlan_ops if it's needed.
513  *
514  * Only modify the outer VLAN stripping settings. The VLAN TPID and outer VLAN
515  * insertion settings are unmodified.
516  *
517  * This tells the hardware to not strip any VLAN tagged packets, thus leaving
518  * them in the packet. This enables software offloaded VLAN stripping and
519  * disables hardware offloaded VLAN stripping.
520  */
521 int ice_vsi_dis_outer_stripping(struct ice_vsi *vsi)
522 {
523 	struct ice_hw *hw = &vsi->back->hw;
524 	struct ice_vsi_ctx *ctxt;
525 	int err;
526 
527 	if (vsi->info.port_based_outer_vlan)
528 		return 0;
529 
530 	ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
531 	if (!ctxt)
532 		return -ENOMEM;
533 
534 	ctxt->info.valid_sections =
535 		cpu_to_le16(ICE_AQ_VSI_PROP_OUTER_TAG_VALID);
536 	/* clear current outer VLAN strip settings */
537 	ctxt->info.outer_vlan_flags = vsi->info.outer_vlan_flags &
538 		~ICE_AQ_VSI_OUTER_VLAN_EMODE_M;
539 	ctxt->info.outer_vlan_flags |= ICE_AQ_VSI_OUTER_VLAN_EMODE_NOTHING <<
540 		ICE_AQ_VSI_OUTER_VLAN_EMODE_S;
541 
542 	err = ice_update_vsi(hw, vsi->idx, ctxt, NULL);
543 	if (err)
544 		dev_err(ice_pf_to_dev(vsi->back), "update VSI for disabling outer VLAN stripping failed, err %d aq_err %s\n",
545 			err, ice_aq_str(hw->adminq.sq_last_status));
546 	else
547 		vsi->info.outer_vlan_flags = ctxt->info.outer_vlan_flags;
548 
549 	kfree(ctxt);
550 	return err;
551 }
552 
553 /**
554  * ice_vsi_ena_outer_insertion - enable outer VLAN insertion
555  * @vsi: VSI to configure
556  * @tpid: TPID to enable outer VLAN insertion for
557  *
558  * Enable outer VLAN insertion via VSI context. This function should only be
559  * used if DVM is supported. Also, this function should never be called directly
560  * as it should be part of ice_vsi_vlan_ops if it's needed.
561  *
562  * Since the VSI context only supports a single TPID for insertion and
563  * stripping, setting the TPID for insertion will affect the TPID for stripping.
564  * Callers need to be aware of this limitation.
565  *
566  * Only modify outer VLAN insertion settings and the VLAN TPID. Outer VLAN
567  * stripping settings are unmodified.
568  *
569  * This allows a VLAN tag with the specified TPID to be inserted in the transmit
570  * descriptor.
571  */
572 int ice_vsi_ena_outer_insertion(struct ice_vsi *vsi, u16 tpid)
573 {
574 	struct ice_hw *hw = &vsi->back->hw;
575 	struct ice_vsi_ctx *ctxt;
576 	u8 tag_type;
577 	int err;
578 
579 	if (vsi->info.port_based_outer_vlan)
580 		return 0;
581 
582 	if (tpid_to_vsi_outer_vlan_type(tpid, &tag_type))
583 		return -EINVAL;
584 
585 	ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
586 	if (!ctxt)
587 		return -ENOMEM;
588 
589 	ctxt->info.valid_sections =
590 		cpu_to_le16(ICE_AQ_VSI_PROP_OUTER_TAG_VALID);
591 	/* clear current outer VLAN insertion settings */
592 	ctxt->info.outer_vlan_flags = vsi->info.outer_vlan_flags &
593 		~(ICE_AQ_VSI_OUTER_VLAN_PORT_BASED_INSERT |
594 		  ICE_AQ_VSI_OUTER_VLAN_BLOCK_TX_DESC |
595 		  ICE_AQ_VSI_OUTER_VLAN_TX_MODE_M |
596 		  ICE_AQ_VSI_OUTER_TAG_TYPE_M);
597 	ctxt->info.outer_vlan_flags |=
598 		((ICE_AQ_VSI_OUTER_VLAN_TX_MODE_ALL <<
599 		  ICE_AQ_VSI_OUTER_VLAN_TX_MODE_S) &
600 		 ICE_AQ_VSI_OUTER_VLAN_TX_MODE_M) |
601 		((tag_type << ICE_AQ_VSI_OUTER_TAG_TYPE_S) &
602 		 ICE_AQ_VSI_OUTER_TAG_TYPE_M);
603 
604 	err = ice_update_vsi(hw, vsi->idx, ctxt, NULL);
605 	if (err)
606 		dev_err(ice_pf_to_dev(vsi->back), "update VSI for enabling outer VLAN insertion failed, err %d aq_err %s\n",
607 			err, ice_aq_str(hw->adminq.sq_last_status));
608 	else
609 		vsi->info.outer_vlan_flags = ctxt->info.outer_vlan_flags;
610 
611 	kfree(ctxt);
612 	return err;
613 }
614 
615 /**
616  * ice_vsi_dis_outer_insertion - disable outer VLAN insertion
617  * @vsi: VSI to configure
618  *
619  * Disable outer VLAN insertion via VSI context. This function should only be
620  * used if DVM is supported. Also, this function should never be called directly
621  * as it should be part of ice_vsi_vlan_ops if it's needed.
622  *
623  * Only modify the outer VLAN insertion settings. The VLAN TPID and outer VLAN
624  * settings are unmodified.
625  *
626  * This tells the hardware to not allow any VLAN tagged packets in the transmit
627  * descriptor. This enables software offloaded VLAN insertion and disables
628  * hardware offloaded VLAN insertion.
629  */
630 int ice_vsi_dis_outer_insertion(struct ice_vsi *vsi)
631 {
632 	struct ice_hw *hw = &vsi->back->hw;
633 	struct ice_vsi_ctx *ctxt;
634 	int err;
635 
636 	if (vsi->info.port_based_outer_vlan)
637 		return 0;
638 
639 	ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
640 	if (!ctxt)
641 		return -ENOMEM;
642 
643 	ctxt->info.valid_sections =
644 		cpu_to_le16(ICE_AQ_VSI_PROP_OUTER_TAG_VALID);
645 	/* clear current outer VLAN insertion settings */
646 	ctxt->info.outer_vlan_flags = vsi->info.outer_vlan_flags &
647 		~(ICE_AQ_VSI_OUTER_VLAN_PORT_BASED_INSERT |
648 		  ICE_AQ_VSI_OUTER_VLAN_TX_MODE_M);
649 	ctxt->info.outer_vlan_flags |=
650 		ICE_AQ_VSI_OUTER_VLAN_BLOCK_TX_DESC |
651 		((ICE_AQ_VSI_OUTER_VLAN_TX_MODE_ALL <<
652 		  ICE_AQ_VSI_OUTER_VLAN_TX_MODE_S) &
653 		 ICE_AQ_VSI_OUTER_VLAN_TX_MODE_M);
654 
655 	err = ice_update_vsi(hw, vsi->idx, ctxt, NULL);
656 	if (err)
657 		dev_err(ice_pf_to_dev(vsi->back), "update VSI for disabling outer VLAN insertion failed, err %d aq_err %s\n",
658 			err, ice_aq_str(hw->adminq.sq_last_status));
659 	else
660 		vsi->info.outer_vlan_flags = ctxt->info.outer_vlan_flags;
661 
662 	kfree(ctxt);
663 	return err;
664 }
665 
666 /**
667  * __ice_vsi_set_outer_port_vlan - set the outer port VLAN and related settings
668  * @vsi: VSI to configure
669  * @vlan_info: packed u16 that contains the VLAN prio and ID
670  * @tpid: TPID of the port VLAN
671  *
672  * Set the port VLAN prio, ID, and TPID.
673  *
674  * Enable VLAN pruning so the VSI doesn't receive any traffic that doesn't match
675  * a VLAN prune rule. The caller should take care to add a VLAN prune rule that
676  * matches the port VLAN ID and TPID.
677  *
678  * Tell hardware to strip outer VLAN tagged packets on receive and don't put
679  * them in the receive descriptor. VSI(s) in port VLANs should not be aware of
680  * the port VLAN ID or TPID they are assigned to.
681  *
682  * Tell hardware to prevent outer VLAN tag insertion on transmit and only allow
683  * untagged outer packets from the transmit descriptor.
684  *
685  * Also, tell the hardware to insert the port VLAN on transmit.
686  */
687 static int
688 __ice_vsi_set_outer_port_vlan(struct ice_vsi *vsi, u16 vlan_info, u16 tpid)
689 {
690 	struct ice_hw *hw = &vsi->back->hw;
691 	struct ice_vsi_ctx *ctxt;
692 	u8 tag_type;
693 	int err;
694 
695 	if (tpid_to_vsi_outer_vlan_type(tpid, &tag_type))
696 		return -EINVAL;
697 
698 	ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
699 	if (!ctxt)
700 		return -ENOMEM;
701 
702 	ice_save_vlan_info(&vsi->info, &vsi->vlan_info);
703 	ctxt->info = vsi->info;
704 
705 	ctxt->info.sw_flags2 |= ICE_AQ_VSI_SW_FLAG_RX_VLAN_PRUNE_ENA;
706 
707 	ctxt->info.port_based_outer_vlan = cpu_to_le16(vlan_info);
708 	ctxt->info.outer_vlan_flags =
709 		(ICE_AQ_VSI_OUTER_VLAN_EMODE_SHOW <<
710 		 ICE_AQ_VSI_OUTER_VLAN_EMODE_S) |
711 		((tag_type << ICE_AQ_VSI_OUTER_TAG_TYPE_S) &
712 		 ICE_AQ_VSI_OUTER_TAG_TYPE_M) |
713 		ICE_AQ_VSI_OUTER_VLAN_BLOCK_TX_DESC |
714 		(ICE_AQ_VSI_OUTER_VLAN_TX_MODE_ACCEPTUNTAGGED <<
715 		 ICE_AQ_VSI_OUTER_VLAN_TX_MODE_S) |
716 		ICE_AQ_VSI_OUTER_VLAN_PORT_BASED_INSERT;
717 
718 	ctxt->info.valid_sections =
719 		cpu_to_le16(ICE_AQ_VSI_PROP_OUTER_TAG_VALID |
720 			    ICE_AQ_VSI_PROP_SW_VALID);
721 
722 	err = ice_update_vsi(hw, vsi->idx, ctxt, NULL);
723 	if (err) {
724 		dev_err(ice_pf_to_dev(vsi->back), "update VSI for setting outer port based VLAN failed, err %d aq_err %s\n",
725 			err, ice_aq_str(hw->adminq.sq_last_status));
726 	} else {
727 		vsi->info.port_based_outer_vlan = ctxt->info.port_based_outer_vlan;
728 		vsi->info.outer_vlan_flags = ctxt->info.outer_vlan_flags;
729 		vsi->info.sw_flags2 = ctxt->info.sw_flags2;
730 	}
731 
732 	kfree(ctxt);
733 	return err;
734 }
735 
736 /**
737  * ice_vsi_set_outer_port_vlan - public version of __ice_vsi_set_outer_port_vlan
738  * @vsi: VSI to configure
739  * @vlan: ice_vlan structure used to set the port VLAN
740  *
741  * Set the outer port VLAN via VSI context. This function should only be
742  * used if DVM is supported. Also, this function should never be called directly
743  * as it should be part of ice_vsi_vlan_ops if it's needed.
744  *
745  * Use the ice_vlan structure passed in to set this VSI in a port VLAN.
746  */
747 int ice_vsi_set_outer_port_vlan(struct ice_vsi *vsi, struct ice_vlan *vlan)
748 {
749 	u16 port_vlan_info;
750 
751 	if (vlan->prio > (VLAN_PRIO_MASK >> VLAN_PRIO_SHIFT))
752 		return -EINVAL;
753 
754 	port_vlan_info = vlan->vid | (vlan->prio << VLAN_PRIO_SHIFT);
755 
756 	return __ice_vsi_set_outer_port_vlan(vsi, port_vlan_info, vlan->tpid);
757 }
758 
759 /**
760  * ice_vsi_clear_outer_port_vlan - clear outer port vlan
761  * @vsi: VSI to configure
762  *
763  * The function is restoring previously set vlan config (saved in
764  * vsi->vlan_info). Setting happens in port vlan configuration.
765  */
766 int ice_vsi_clear_outer_port_vlan(struct ice_vsi *vsi)
767 {
768 	struct ice_hw *hw = &vsi->back->hw;
769 	struct ice_vsi_ctx *ctxt;
770 	int err;
771 
772 	ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
773 	if (!ctxt)
774 		return -ENOMEM;
775 
776 	ice_restore_vlan_info(&vsi->info, &vsi->vlan_info);
777 	vsi->info.port_based_outer_vlan = 0;
778 	ctxt->info = vsi->info;
779 
780 	ctxt->info.valid_sections =
781 		cpu_to_le16(ICE_AQ_VSI_PROP_OUTER_TAG_VALID |
782 			    ICE_AQ_VSI_PROP_SW_VALID);
783 
784 	err = ice_update_vsi(hw, vsi->idx, ctxt, NULL);
785 	if (err)
786 		dev_err(ice_pf_to_dev(vsi->back), "update VSI for clearing outer port based VLAN failed, err %d aq_err %s\n",
787 			err, ice_aq_str(hw->adminq.sq_last_status));
788 
789 	kfree(ctxt);
790 	return err;
791 }
792