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