1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (C) 2019-2021, Intel Corporation. */
6 #include "ice_eswitch.h"
7 #include "ice_eswitch_br.h"
10 #include "ice_devlink.h"
11 #include "ice_tc_lib.h"
14 * ice_eswitch_add_vf_sp_rule - add adv rule with VF's VSI index
15 * @pf: pointer to PF struct
16 * @vf: pointer to VF struct
18 * This function adds advanced rule that forwards packets with
19 * VF's VSI index to the corresponding switchdev ctrl VSI queue.
22 ice_eswitch_add_vf_sp_rule(struct ice_pf *pf, struct ice_vf *vf)
24 struct ice_vsi *ctrl_vsi = pf->switchdev.control_vsi;
25 struct ice_adv_rule_info rule_info = { 0 };
26 struct ice_adv_lkup_elem *list;
27 struct ice_hw *hw = &pf->hw;
28 const u16 lkups_cnt = 1;
31 list = kcalloc(lkups_cnt, sizeof(*list), GFP_ATOMIC);
35 ice_rule_add_src_vsi_metadata(list);
37 rule_info.sw_act.flag = ICE_FLTR_TX;
38 rule_info.sw_act.vsi_handle = ctrl_vsi->idx;
39 rule_info.sw_act.fltr_act = ICE_FWD_TO_Q;
40 rule_info.sw_act.fwd_id.q_id = hw->func_caps.common_cap.rxq_first_id +
41 ctrl_vsi->rxq_map[vf->vf_id];
42 rule_info.flags_info.act |= ICE_SINGLE_ACT_LB_ENABLE;
43 rule_info.flags_info.act_valid = true;
44 rule_info.tun_type = ICE_SW_TUN_AND_NON_TUN;
45 rule_info.src_vsi = vf->lan_vsi_idx;
47 err = ice_add_adv_rule(hw, list, lkups_cnt, &rule_info,
50 dev_err(ice_pf_to_dev(pf), "Unable to add VF slow-path rule in switchdev mode for VF %d",
58 * ice_eswitch_del_vf_sp_rule - delete adv rule with VF's VSI index
59 * @vf: pointer to the VF struct
61 * Delete the advanced rule that was used to forward packets with the VF's VSI
62 * index to the corresponding switchdev ctrl VSI queue.
64 static void ice_eswitch_del_vf_sp_rule(struct ice_vf *vf)
69 ice_rem_adv_rule_by_id(&vf->pf->hw, &vf->repr->sp_rule);
73 * ice_eswitch_setup_env - configure switchdev HW filters
74 * @pf: pointer to PF struct
76 * This function adds HW filters configuration specific for switchdev
79 static int ice_eswitch_setup_env(struct ice_pf *pf)
81 struct ice_vsi *uplink_vsi = pf->switchdev.uplink_vsi;
82 struct net_device *uplink_netdev = uplink_vsi->netdev;
83 struct ice_vsi *ctrl_vsi = pf->switchdev.control_vsi;
84 struct ice_vsi_vlan_ops *vlan_ops;
85 bool rule_added = false;
87 vlan_ops = ice_get_compat_vsi_vlan_ops(ctrl_vsi);
88 if (vlan_ops->dis_stripping(ctrl_vsi))
91 ice_remove_vsi_fltr(&pf->hw, uplink_vsi->idx);
93 netif_addr_lock_bh(uplink_netdev);
94 __dev_uc_unsync(uplink_netdev, NULL);
95 __dev_mc_unsync(uplink_netdev, NULL);
96 netif_addr_unlock_bh(uplink_netdev);
98 if (ice_vsi_add_vlan_zero(uplink_vsi))
101 if (!ice_is_dflt_vsi_in_use(uplink_vsi->port_info)) {
102 if (ice_set_dflt_vsi(uplink_vsi))
107 vlan_ops = ice_get_compat_vsi_vlan_ops(uplink_vsi);
108 if (vlan_ops->dis_rx_filtering(uplink_vsi))
111 if (ice_vsi_update_security(uplink_vsi, ice_vsi_ctx_set_allow_override))
112 goto err_override_uplink;
114 if (ice_vsi_update_security(ctrl_vsi, ice_vsi_ctx_set_allow_override))
115 goto err_override_control;
117 if (ice_vsi_update_local_lb(uplink_vsi, true))
118 goto err_override_local_lb;
122 err_override_local_lb:
123 ice_vsi_update_security(ctrl_vsi, ice_vsi_ctx_clear_allow_override);
124 err_override_control:
125 ice_vsi_update_security(uplink_vsi, ice_vsi_ctx_clear_allow_override);
127 vlan_ops->ena_rx_filtering(uplink_vsi);
130 ice_clear_dflt_vsi(uplink_vsi);
132 ice_fltr_add_mac_and_broadcast(uplink_vsi,
133 uplink_vsi->port_info->mac.perm_addr,
139 * ice_eswitch_remap_rings_to_vectors - reconfigure rings of switchdev ctrl VSI
140 * @pf: pointer to PF struct
142 * In switchdev number of allocated Tx/Rx rings is equal.
144 * This function fills q_vectors structures associated with representor and
145 * move each ring pairs to port representor netdevs. Each port representor
146 * will have dedicated 1 Tx/Rx ring pair, so number of rings pair is equal to
149 static void ice_eswitch_remap_rings_to_vectors(struct ice_pf *pf)
151 struct ice_vsi *vsi = pf->switchdev.control_vsi;
154 ice_for_each_txq(vsi, q_id) {
155 struct ice_q_vector *q_vector;
156 struct ice_tx_ring *tx_ring;
157 struct ice_rx_ring *rx_ring;
158 struct ice_repr *repr;
161 vf = ice_get_vf_by_id(pf, q_id);
166 q_vector = repr->q_vector;
167 tx_ring = vsi->tx_rings[q_id];
168 rx_ring = vsi->rx_rings[q_id];
171 q_vector->reg_idx = vsi->q_vectors[0]->reg_idx;
173 q_vector->num_ring_tx = 1;
174 q_vector->tx.tx_ring = tx_ring;
175 tx_ring->q_vector = q_vector;
176 tx_ring->next = NULL;
177 tx_ring->netdev = repr->netdev;
178 /* In switchdev mode, from OS stack perspective, there is only
179 * one queue for given netdev, so it needs to be indexed as 0.
181 tx_ring->q_index = 0;
183 q_vector->num_ring_rx = 1;
184 q_vector->rx.rx_ring = rx_ring;
185 rx_ring->q_vector = q_vector;
186 rx_ring->next = NULL;
187 rx_ring->netdev = repr->netdev;
194 * ice_eswitch_release_reprs - clear PR VSIs configuration
195 * @pf: poiner to PF struct
196 * @ctrl_vsi: pointer to switchdev control VSI
199 ice_eswitch_release_reprs(struct ice_pf *pf, struct ice_vsi *ctrl_vsi)
204 lockdep_assert_held(&pf->vfs.table_lock);
206 ice_for_each_vf(pf, bkt, vf) {
207 struct ice_vsi *vsi = vf->repr->src_vsi;
209 /* Skip VFs that aren't configured */
213 ice_vsi_update_security(vsi, ice_vsi_ctx_set_antispoof);
214 metadata_dst_free(vf->repr->dst);
215 vf->repr->dst = NULL;
216 ice_eswitch_del_vf_sp_rule(vf);
217 ice_fltr_add_mac_and_broadcast(vsi, vf->hw_lan_addr,
220 netif_napi_del(&vf->repr->q_vector->napi);
225 * ice_eswitch_setup_reprs - configure port reprs to run in switchdev mode
226 * @pf: pointer to PF struct
228 static int ice_eswitch_setup_reprs(struct ice_pf *pf)
230 struct ice_vsi *ctrl_vsi = pf->switchdev.control_vsi;
235 lockdep_assert_held(&pf->vfs.table_lock);
237 ice_for_each_vf(pf, bkt, vf) {
238 struct ice_vsi *vsi = vf->repr->src_vsi;
240 ice_remove_vsi_fltr(&pf->hw, vsi->idx);
241 vf->repr->dst = metadata_dst_alloc(0, METADATA_HW_PORT_MUX,
243 if (!vf->repr->dst) {
244 ice_fltr_add_mac_and_broadcast(vsi, vf->hw_lan_addr,
249 if (ice_eswitch_add_vf_sp_rule(pf, vf)) {
250 ice_fltr_add_mac_and_broadcast(vsi, vf->hw_lan_addr,
255 if (ice_vsi_update_security(vsi, ice_vsi_ctx_clear_antispoof)) {
256 ice_fltr_add_mac_and_broadcast(vsi, vf->hw_lan_addr,
258 ice_eswitch_del_vf_sp_rule(vf);
259 metadata_dst_free(vf->repr->dst);
260 vf->repr->dst = NULL;
264 if (ice_vsi_add_vlan_zero(vsi)) {
265 ice_fltr_add_mac_and_broadcast(vsi, vf->hw_lan_addr,
267 ice_eswitch_del_vf_sp_rule(vf);
268 metadata_dst_free(vf->repr->dst);
269 vf->repr->dst = NULL;
270 ice_vsi_update_security(vsi, ice_vsi_ctx_set_antispoof);
274 if (max_vsi_num < vsi->vsi_num)
275 max_vsi_num = vsi->vsi_num;
277 netif_napi_add(vf->repr->netdev, &vf->repr->q_vector->napi,
280 netif_keep_dst(vf->repr->netdev);
283 ice_for_each_vf(pf, bkt, vf) {
284 struct ice_repr *repr = vf->repr;
285 struct ice_vsi *vsi = repr->src_vsi;
286 struct metadata_dst *dst;
289 dst->u.port_info.port_id = vsi->vsi_num;
290 dst->u.port_info.lower_dev = repr->netdev;
291 ice_repr_set_traffic_vsi(repr, ctrl_vsi);
297 ice_eswitch_release_reprs(pf, ctrl_vsi);
303 * ice_eswitch_update_repr - reconfigure VF port representor
304 * @vsi: VF VSI for which port representor is configured
306 void ice_eswitch_update_repr(struct ice_vsi *vsi)
308 struct ice_pf *pf = vsi->back;
309 struct ice_repr *repr;
313 if (!ice_is_switchdev_running(pf))
319 repr->dst->u.port_info.port_id = vsi->vsi_num;
322 repr->br_port->vsi = vsi;
324 ret = ice_vsi_update_security(vsi, ice_vsi_ctx_clear_antispoof);
326 ice_fltr_add_mac_and_broadcast(vsi, vf->hw_lan_addr, ICE_FWD_TO_VSI);
327 dev_err(ice_pf_to_dev(pf), "Failed to update VF %d port representor",
333 * ice_eswitch_port_start_xmit - callback for packets transmit
335 * @netdev: network interface device structure
337 * Returns NETDEV_TX_OK if sent, else an error code
340 ice_eswitch_port_start_xmit(struct sk_buff *skb, struct net_device *netdev)
342 struct ice_netdev_priv *np;
343 struct ice_repr *repr;
346 np = netdev_priv(netdev);
349 if (!vsi || !ice_is_switchdev_running(vsi->back))
350 return NETDEV_TX_BUSY;
352 if (ice_is_reset_in_progress(vsi->back->state) ||
353 test_bit(ICE_VF_DIS, vsi->back->state))
354 return NETDEV_TX_BUSY;
356 repr = ice_netdev_to_repr(netdev);
358 dst_hold((struct dst_entry *)repr->dst);
359 skb_dst_set(skb, (struct dst_entry *)repr->dst);
360 skb->queue_mapping = repr->vf->vf_id;
362 return ice_start_xmit(skb, netdev);
366 * ice_eswitch_set_target_vsi - set switchdev context in Tx context descriptor
367 * @skb: pointer to send buffer
368 * @off: pointer to offload struct
371 ice_eswitch_set_target_vsi(struct sk_buff *skb,
372 struct ice_tx_offload_params *off)
374 struct metadata_dst *dst = skb_metadata_dst(skb);
378 cd_cmd = ICE_TX_CTX_DESC_SWTCH_UPLINK << ICE_TXD_CTX_QW1_CMD_S;
379 off->cd_qw1 |= (cd_cmd | ICE_TX_DESC_DTYPE_CTX);
381 cd_cmd = ICE_TX_CTX_DESC_SWTCH_VSI << ICE_TXD_CTX_QW1_CMD_S;
382 dst_vsi = ((u64)dst->u.port_info.port_id <<
383 ICE_TXD_CTX_QW1_VSI_S) & ICE_TXD_CTX_QW1_VSI_M;
384 off->cd_qw1 = cd_cmd | dst_vsi | ICE_TX_DESC_DTYPE_CTX;
389 * ice_eswitch_release_env - clear switchdev HW filters
390 * @pf: pointer to PF struct
392 * This function removes HW filters configuration specific for switchdev
393 * mode and restores default legacy mode settings.
395 static void ice_eswitch_release_env(struct ice_pf *pf)
397 struct ice_vsi *uplink_vsi = pf->switchdev.uplink_vsi;
398 struct ice_vsi *ctrl_vsi = pf->switchdev.control_vsi;
399 struct ice_vsi_vlan_ops *vlan_ops;
401 vlan_ops = ice_get_compat_vsi_vlan_ops(uplink_vsi);
403 ice_vsi_update_local_lb(uplink_vsi, false);
404 ice_vsi_update_security(ctrl_vsi, ice_vsi_ctx_clear_allow_override);
405 ice_vsi_update_security(uplink_vsi, ice_vsi_ctx_clear_allow_override);
406 vlan_ops->ena_rx_filtering(uplink_vsi);
407 ice_clear_dflt_vsi(uplink_vsi);
408 ice_fltr_add_mac_and_broadcast(uplink_vsi,
409 uplink_vsi->port_info->mac.perm_addr,
414 * ice_eswitch_vsi_setup - configure switchdev control VSI
415 * @pf: pointer to PF structure
416 * @pi: pointer to port_info structure
418 static struct ice_vsi *
419 ice_eswitch_vsi_setup(struct ice_pf *pf, struct ice_port_info *pi)
421 struct ice_vsi_cfg_params params = {};
423 params.type = ICE_VSI_SWITCHDEV_CTRL;
425 params.flags = ICE_VSI_FLAG_INIT;
427 return ice_vsi_setup(pf, ¶ms);
431 * ice_eswitch_napi_del - remove NAPI handle for all port representors
432 * @pf: pointer to PF structure
434 static void ice_eswitch_napi_del(struct ice_pf *pf)
439 lockdep_assert_held(&pf->vfs.table_lock);
441 ice_for_each_vf(pf, bkt, vf)
442 netif_napi_del(&vf->repr->q_vector->napi);
446 * ice_eswitch_napi_enable - enable NAPI for all port representors
447 * @pf: pointer to PF structure
449 static void ice_eswitch_napi_enable(struct ice_pf *pf)
454 lockdep_assert_held(&pf->vfs.table_lock);
456 ice_for_each_vf(pf, bkt, vf)
457 napi_enable(&vf->repr->q_vector->napi);
461 * ice_eswitch_napi_disable - disable NAPI for all port representors
462 * @pf: pointer to PF structure
464 static void ice_eswitch_napi_disable(struct ice_pf *pf)
469 lockdep_assert_held(&pf->vfs.table_lock);
471 ice_for_each_vf(pf, bkt, vf)
472 napi_disable(&vf->repr->q_vector->napi);
476 * ice_eswitch_enable_switchdev - configure eswitch in switchdev mode
477 * @pf: pointer to PF structure
479 static int ice_eswitch_enable_switchdev(struct ice_pf *pf)
481 struct ice_vsi *ctrl_vsi, *uplink_vsi;
483 uplink_vsi = ice_get_main_vsi(pf);
487 if (netif_is_any_bridge_port(uplink_vsi->netdev)) {
488 dev_err(ice_pf_to_dev(pf),
489 "Uplink port cannot be a bridge port\n");
493 pf->switchdev.control_vsi = ice_eswitch_vsi_setup(pf, pf->hw.port_info);
494 if (!pf->switchdev.control_vsi)
497 ctrl_vsi = pf->switchdev.control_vsi;
498 pf->switchdev.uplink_vsi = uplink_vsi;
500 if (ice_eswitch_setup_env(pf))
503 if (ice_repr_add_for_all_vfs(pf))
506 if (ice_eswitch_setup_reprs(pf))
507 goto err_setup_reprs;
509 ice_eswitch_remap_rings_to_vectors(pf);
511 if (ice_vsi_open(ctrl_vsi))
512 goto err_setup_reprs;
514 if (ice_eswitch_br_offloads_init(pf))
515 goto err_br_offloads;
517 ice_eswitch_napi_enable(pf);
522 ice_vsi_close(ctrl_vsi);
524 ice_repr_rem_from_all_vfs(pf);
526 ice_eswitch_release_env(pf);
528 ice_vsi_release(ctrl_vsi);
533 * ice_eswitch_disable_switchdev - disable switchdev resources
534 * @pf: pointer to PF structure
536 static void ice_eswitch_disable_switchdev(struct ice_pf *pf)
538 struct ice_vsi *ctrl_vsi = pf->switchdev.control_vsi;
540 ice_eswitch_napi_disable(pf);
541 ice_eswitch_br_offloads_deinit(pf);
542 ice_eswitch_release_env(pf);
543 ice_eswitch_release_reprs(pf, ctrl_vsi);
544 ice_vsi_release(ctrl_vsi);
545 ice_repr_rem_from_all_vfs(pf);
549 * ice_eswitch_mode_set - set new eswitch mode
550 * @devlink: pointer to devlink structure
551 * @mode: eswitch mode to switch to
552 * @extack: pointer to extack structure
555 ice_eswitch_mode_set(struct devlink *devlink, u16 mode,
556 struct netlink_ext_ack *extack)
558 struct ice_pf *pf = devlink_priv(devlink);
560 if (pf->eswitch_mode == mode)
563 if (ice_has_vfs(pf)) {
564 dev_info(ice_pf_to_dev(pf), "Changing eswitch mode is allowed only if there is no VFs created");
565 NL_SET_ERR_MSG_MOD(extack, "Changing eswitch mode is allowed only if there is no VFs created");
570 case DEVLINK_ESWITCH_MODE_LEGACY:
571 dev_info(ice_pf_to_dev(pf), "PF %d changed eswitch mode to legacy",
573 NL_SET_ERR_MSG_MOD(extack, "Changed eswitch mode to legacy");
575 case DEVLINK_ESWITCH_MODE_SWITCHDEV:
577 if (ice_is_adq_active(pf)) {
578 dev_err(ice_pf_to_dev(pf), "Couldn't change eswitch mode to switchdev - ADQ is active. Delete ADQ configs and try again, e.g. tc qdisc del dev $PF root");
579 NL_SET_ERR_MSG_MOD(extack, "Couldn't change eswitch mode to switchdev - ADQ is active. Delete ADQ configs and try again, e.g. tc qdisc del dev $PF root");
583 dev_info(ice_pf_to_dev(pf), "PF %d changed eswitch mode to switchdev",
585 NL_SET_ERR_MSG_MOD(extack, "Changed eswitch mode to switchdev");
589 NL_SET_ERR_MSG_MOD(extack, "Unknown eswitch mode");
593 pf->eswitch_mode = mode;
598 * ice_eswitch_mode_get - get current eswitch mode
599 * @devlink: pointer to devlink structure
600 * @mode: output parameter for current eswitch mode
602 int ice_eswitch_mode_get(struct devlink *devlink, u16 *mode)
604 struct ice_pf *pf = devlink_priv(devlink);
606 *mode = pf->eswitch_mode;
611 * ice_is_eswitch_mode_switchdev - check if eswitch mode is set to switchdev
612 * @pf: pointer to PF structure
614 * Returns true if eswitch mode is set to DEVLINK_ESWITCH_MODE_SWITCHDEV,
617 bool ice_is_eswitch_mode_switchdev(struct ice_pf *pf)
619 return pf->eswitch_mode == DEVLINK_ESWITCH_MODE_SWITCHDEV;
623 * ice_eswitch_release - cleanup eswitch
624 * @pf: pointer to PF structure
626 void ice_eswitch_release(struct ice_pf *pf)
628 if (pf->eswitch_mode == DEVLINK_ESWITCH_MODE_LEGACY)
631 ice_eswitch_disable_switchdev(pf);
632 pf->switchdev.is_running = false;
636 * ice_eswitch_configure - configure eswitch
637 * @pf: pointer to PF structure
639 int ice_eswitch_configure(struct ice_pf *pf)
643 if (pf->eswitch_mode == DEVLINK_ESWITCH_MODE_LEGACY || pf->switchdev.is_running)
646 status = ice_eswitch_enable_switchdev(pf);
650 pf->switchdev.is_running = true;
655 * ice_eswitch_start_all_tx_queues - start Tx queues of all port representors
656 * @pf: pointer to PF structure
658 static void ice_eswitch_start_all_tx_queues(struct ice_pf *pf)
663 lockdep_assert_held(&pf->vfs.table_lock);
665 if (test_bit(ICE_DOWN, pf->state))
668 ice_for_each_vf(pf, bkt, vf) {
670 ice_repr_start_tx_queues(vf->repr);
675 * ice_eswitch_stop_all_tx_queues - stop Tx queues of all port representors
676 * @pf: pointer to PF structure
678 void ice_eswitch_stop_all_tx_queues(struct ice_pf *pf)
683 lockdep_assert_held(&pf->vfs.table_lock);
685 if (test_bit(ICE_DOWN, pf->state))
688 ice_for_each_vf(pf, bkt, vf) {
690 ice_repr_stop_tx_queues(vf->repr);
695 * ice_eswitch_rebuild - rebuild eswitch
696 * @pf: pointer to PF structure
698 int ice_eswitch_rebuild(struct ice_pf *pf)
700 struct ice_vsi *ctrl_vsi = pf->switchdev.control_vsi;
703 ice_eswitch_napi_disable(pf);
704 ice_eswitch_napi_del(pf);
706 status = ice_eswitch_setup_env(pf);
710 status = ice_eswitch_setup_reprs(pf);
714 ice_eswitch_remap_rings_to_vectors(pf);
716 ice_replay_tc_fltrs(pf);
718 status = ice_vsi_open(ctrl_vsi);
722 ice_eswitch_napi_enable(pf);
723 ice_eswitch_start_all_tx_queues(pf);