1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (C) 2019-2021, Intel Corporation. */
6 #include "ice_eswitch.h"
9 #include "ice_devlink.h"
10 #include "ice_tc_lib.h"
13 * ice_eswitch_add_vf_sp_rule - add adv rule with VF's VSI index
14 * @pf: pointer to PF struct
15 * @vf: pointer to VF struct
17 * This function adds advanced rule that forwards packets with
18 * VF's VSI index to the corresponding switchdev ctrl VSI queue.
21 ice_eswitch_add_vf_sp_rule(struct ice_pf *pf, struct ice_vf *vf)
23 struct ice_vsi *ctrl_vsi = pf->switchdev.control_vsi;
24 struct ice_adv_rule_info rule_info = { 0 };
25 struct ice_adv_lkup_elem *list;
26 struct ice_hw *hw = &pf->hw;
27 const u16 lkups_cnt = 1;
30 list = kcalloc(lkups_cnt, sizeof(*list), GFP_ATOMIC);
34 ice_rule_add_src_vsi_metadata(list);
36 rule_info.sw_act.flag = ICE_FLTR_TX;
37 rule_info.sw_act.vsi_handle = ctrl_vsi->idx;
38 rule_info.sw_act.fltr_act = ICE_FWD_TO_Q;
39 rule_info.sw_act.fwd_id.q_id = hw->func_caps.common_cap.rxq_first_id +
40 ctrl_vsi->rxq_map[vf->vf_id];
41 rule_info.flags_info.act |= ICE_SINGLE_ACT_LB_ENABLE;
42 rule_info.flags_info.act_valid = true;
43 rule_info.tun_type = ICE_SW_TUN_AND_NON_TUN;
44 rule_info.src_vsi = vf->lan_vsi_idx;
46 err = ice_add_adv_rule(hw, list, lkups_cnt, &rule_info,
49 dev_err(ice_pf_to_dev(pf), "Unable to add VF slow-path rule in switchdev mode for VF %d",
57 * ice_eswitch_del_vf_sp_rule - delete adv rule with VF's VSI index
58 * @vf: pointer to the VF struct
60 * Delete the advanced rule that was used to forward packets with the VF's VSI
61 * index to the corresponding switchdev ctrl VSI queue.
63 static void ice_eswitch_del_vf_sp_rule(struct ice_vf *vf)
68 ice_rem_adv_rule_by_id(&vf->pf->hw, &vf->repr->sp_rule);
72 * ice_eswitch_setup_env - configure switchdev HW filters
73 * @pf: pointer to PF struct
75 * This function adds HW filters configuration specific for switchdev
78 static int ice_eswitch_setup_env(struct ice_pf *pf)
80 struct ice_vsi *uplink_vsi = pf->switchdev.uplink_vsi;
81 struct net_device *uplink_netdev = uplink_vsi->netdev;
82 struct ice_vsi *ctrl_vsi = pf->switchdev.control_vsi;
83 struct ice_vsi_vlan_ops *vlan_ops;
84 bool rule_added = false;
86 vlan_ops = ice_get_compat_vsi_vlan_ops(ctrl_vsi);
87 if (vlan_ops->dis_stripping(ctrl_vsi))
90 ice_remove_vsi_fltr(&pf->hw, uplink_vsi->idx);
92 netif_addr_lock_bh(uplink_netdev);
93 __dev_uc_unsync(uplink_netdev, NULL);
94 __dev_mc_unsync(uplink_netdev, NULL);
95 netif_addr_unlock_bh(uplink_netdev);
97 if (ice_vsi_add_vlan_zero(uplink_vsi))
100 if (!ice_is_dflt_vsi_in_use(uplink_vsi->port_info)) {
101 if (ice_set_dflt_vsi(uplink_vsi))
106 if (ice_vsi_update_security(uplink_vsi, ice_vsi_ctx_set_allow_override))
107 goto err_override_uplink;
109 if (ice_vsi_update_security(ctrl_vsi, ice_vsi_ctx_set_allow_override))
110 goto err_override_control;
114 err_override_control:
115 ice_vsi_update_security(uplink_vsi, ice_vsi_ctx_clear_allow_override);
118 ice_clear_dflt_vsi(uplink_vsi);
120 ice_fltr_add_mac_and_broadcast(uplink_vsi,
121 uplink_vsi->port_info->mac.perm_addr,
127 * ice_eswitch_remap_rings_to_vectors - reconfigure rings of switchdev ctrl VSI
128 * @pf: pointer to PF struct
130 * In switchdev number of allocated Tx/Rx rings is equal.
132 * This function fills q_vectors structures associated with representor and
133 * move each ring pairs to port representor netdevs. Each port representor
134 * will have dedicated 1 Tx/Rx ring pair, so number of rings pair is equal to
137 static void ice_eswitch_remap_rings_to_vectors(struct ice_pf *pf)
139 struct ice_vsi *vsi = pf->switchdev.control_vsi;
142 ice_for_each_txq(vsi, q_id) {
143 struct ice_q_vector *q_vector;
144 struct ice_tx_ring *tx_ring;
145 struct ice_rx_ring *rx_ring;
146 struct ice_repr *repr;
149 vf = ice_get_vf_by_id(pf, q_id);
154 q_vector = repr->q_vector;
155 tx_ring = vsi->tx_rings[q_id];
156 rx_ring = vsi->rx_rings[q_id];
159 q_vector->reg_idx = vsi->q_vectors[0]->reg_idx;
161 q_vector->num_ring_tx = 1;
162 q_vector->tx.tx_ring = tx_ring;
163 tx_ring->q_vector = q_vector;
164 tx_ring->next = NULL;
165 tx_ring->netdev = repr->netdev;
166 /* In switchdev mode, from OS stack perspective, there is only
167 * one queue for given netdev, so it needs to be indexed as 0.
169 tx_ring->q_index = 0;
171 q_vector->num_ring_rx = 1;
172 q_vector->rx.rx_ring = rx_ring;
173 rx_ring->q_vector = q_vector;
174 rx_ring->next = NULL;
175 rx_ring->netdev = repr->netdev;
182 * ice_eswitch_release_reprs - clear PR VSIs configuration
183 * @pf: poiner to PF struct
184 * @ctrl_vsi: pointer to switchdev control VSI
187 ice_eswitch_release_reprs(struct ice_pf *pf, struct ice_vsi *ctrl_vsi)
192 lockdep_assert_held(&pf->vfs.table_lock);
194 ice_for_each_vf(pf, bkt, vf) {
195 struct ice_vsi *vsi = vf->repr->src_vsi;
197 /* Skip VFs that aren't configured */
201 ice_vsi_update_security(vsi, ice_vsi_ctx_set_antispoof);
202 metadata_dst_free(vf->repr->dst);
203 vf->repr->dst = NULL;
204 ice_eswitch_del_vf_sp_rule(vf);
205 ice_fltr_add_mac_and_broadcast(vsi, vf->hw_lan_addr,
208 netif_napi_del(&vf->repr->q_vector->napi);
213 * ice_eswitch_setup_reprs - configure port reprs to run in switchdev mode
214 * @pf: pointer to PF struct
216 static int ice_eswitch_setup_reprs(struct ice_pf *pf)
218 struct ice_vsi *ctrl_vsi = pf->switchdev.control_vsi;
223 lockdep_assert_held(&pf->vfs.table_lock);
225 ice_for_each_vf(pf, bkt, vf) {
226 struct ice_vsi *vsi = vf->repr->src_vsi;
228 ice_remove_vsi_fltr(&pf->hw, vsi->idx);
229 vf->repr->dst = metadata_dst_alloc(0, METADATA_HW_PORT_MUX,
231 if (!vf->repr->dst) {
232 ice_fltr_add_mac_and_broadcast(vsi, vf->hw_lan_addr,
237 if (ice_eswitch_add_vf_sp_rule(pf, vf)) {
238 ice_fltr_add_mac_and_broadcast(vsi, vf->hw_lan_addr,
243 if (ice_vsi_update_security(vsi, ice_vsi_ctx_clear_antispoof)) {
244 ice_fltr_add_mac_and_broadcast(vsi, vf->hw_lan_addr,
246 ice_eswitch_del_vf_sp_rule(vf);
247 metadata_dst_free(vf->repr->dst);
248 vf->repr->dst = NULL;
252 if (ice_vsi_add_vlan_zero(vsi)) {
253 ice_fltr_add_mac_and_broadcast(vsi, vf->hw_lan_addr,
255 ice_eswitch_del_vf_sp_rule(vf);
256 metadata_dst_free(vf->repr->dst);
257 vf->repr->dst = NULL;
258 ice_vsi_update_security(vsi, ice_vsi_ctx_set_antispoof);
262 if (max_vsi_num < vsi->vsi_num)
263 max_vsi_num = vsi->vsi_num;
265 netif_napi_add(vf->repr->netdev, &vf->repr->q_vector->napi,
268 netif_keep_dst(vf->repr->netdev);
271 ice_for_each_vf(pf, bkt, vf) {
272 struct ice_repr *repr = vf->repr;
273 struct ice_vsi *vsi = repr->src_vsi;
274 struct metadata_dst *dst;
277 dst->u.port_info.port_id = vsi->vsi_num;
278 dst->u.port_info.lower_dev = repr->netdev;
279 ice_repr_set_traffic_vsi(repr, ctrl_vsi);
285 ice_eswitch_release_reprs(pf, ctrl_vsi);
291 * ice_eswitch_update_repr - reconfigure VF port representor
292 * @vsi: VF VSI for which port representor is configured
294 void ice_eswitch_update_repr(struct ice_vsi *vsi)
296 struct ice_pf *pf = vsi->back;
297 struct ice_repr *repr;
301 if (!ice_is_switchdev_running(pf))
307 repr->dst->u.port_info.port_id = vsi->vsi_num;
309 ret = ice_vsi_update_security(vsi, ice_vsi_ctx_clear_antispoof);
311 ice_fltr_add_mac_and_broadcast(vsi, vf->hw_lan_addr, ICE_FWD_TO_VSI);
312 dev_err(ice_pf_to_dev(pf), "Failed to update VF %d port representor",
318 * ice_eswitch_port_start_xmit - callback for packets transmit
320 * @netdev: network interface device structure
322 * Returns NETDEV_TX_OK if sent, else an error code
325 ice_eswitch_port_start_xmit(struct sk_buff *skb, struct net_device *netdev)
327 struct ice_netdev_priv *np;
328 struct ice_repr *repr;
331 np = netdev_priv(netdev);
334 if (ice_is_reset_in_progress(vsi->back->state) ||
335 test_bit(ICE_VF_DIS, vsi->back->state))
336 return NETDEV_TX_BUSY;
338 repr = ice_netdev_to_repr(netdev);
340 dst_hold((struct dst_entry *)repr->dst);
341 skb_dst_set(skb, (struct dst_entry *)repr->dst);
342 skb->queue_mapping = repr->vf->vf_id;
344 return ice_start_xmit(skb, netdev);
348 * ice_eswitch_set_target_vsi - set switchdev context in Tx context descriptor
349 * @skb: pointer to send buffer
350 * @off: pointer to offload struct
353 ice_eswitch_set_target_vsi(struct sk_buff *skb,
354 struct ice_tx_offload_params *off)
356 struct metadata_dst *dst = skb_metadata_dst(skb);
360 cd_cmd = ICE_TX_CTX_DESC_SWTCH_UPLINK << ICE_TXD_CTX_QW1_CMD_S;
361 off->cd_qw1 |= (cd_cmd | ICE_TX_DESC_DTYPE_CTX);
363 cd_cmd = ICE_TX_CTX_DESC_SWTCH_VSI << ICE_TXD_CTX_QW1_CMD_S;
364 dst_vsi = ((u64)dst->u.port_info.port_id <<
365 ICE_TXD_CTX_QW1_VSI_S) & ICE_TXD_CTX_QW1_VSI_M;
366 off->cd_qw1 = cd_cmd | dst_vsi | ICE_TX_DESC_DTYPE_CTX;
371 * ice_eswitch_release_env - clear switchdev HW filters
372 * @pf: pointer to PF struct
374 * This function removes HW filters configuration specific for switchdev
375 * mode and restores default legacy mode settings.
377 static void ice_eswitch_release_env(struct ice_pf *pf)
379 struct ice_vsi *uplink_vsi = pf->switchdev.uplink_vsi;
380 struct ice_vsi *ctrl_vsi = pf->switchdev.control_vsi;
382 ice_vsi_update_security(ctrl_vsi, ice_vsi_ctx_clear_allow_override);
383 ice_vsi_update_security(uplink_vsi, ice_vsi_ctx_clear_allow_override);
384 ice_clear_dflt_vsi(uplink_vsi);
385 ice_fltr_add_mac_and_broadcast(uplink_vsi,
386 uplink_vsi->port_info->mac.perm_addr,
391 * ice_eswitch_vsi_setup - configure switchdev control VSI
392 * @pf: pointer to PF structure
393 * @pi: pointer to port_info structure
395 static struct ice_vsi *
396 ice_eswitch_vsi_setup(struct ice_pf *pf, struct ice_port_info *pi)
398 struct ice_vsi_cfg_params params = {};
400 params.type = ICE_VSI_SWITCHDEV_CTRL;
402 params.flags = ICE_VSI_FLAG_INIT;
404 return ice_vsi_setup(pf, ¶ms);
408 * ice_eswitch_napi_del - remove NAPI handle for all port representors
409 * @pf: pointer to PF structure
411 static void ice_eswitch_napi_del(struct ice_pf *pf)
416 lockdep_assert_held(&pf->vfs.table_lock);
418 ice_for_each_vf(pf, bkt, vf)
419 netif_napi_del(&vf->repr->q_vector->napi);
423 * ice_eswitch_napi_enable - enable NAPI for all port representors
424 * @pf: pointer to PF structure
426 static void ice_eswitch_napi_enable(struct ice_pf *pf)
431 lockdep_assert_held(&pf->vfs.table_lock);
433 ice_for_each_vf(pf, bkt, vf)
434 napi_enable(&vf->repr->q_vector->napi);
438 * ice_eswitch_napi_disable - disable NAPI for all port representors
439 * @pf: pointer to PF structure
441 static void ice_eswitch_napi_disable(struct ice_pf *pf)
446 lockdep_assert_held(&pf->vfs.table_lock);
448 ice_for_each_vf(pf, bkt, vf)
449 napi_disable(&vf->repr->q_vector->napi);
453 * ice_eswitch_enable_switchdev - configure eswitch in switchdev mode
454 * @pf: pointer to PF structure
456 static int ice_eswitch_enable_switchdev(struct ice_pf *pf)
458 struct ice_vsi *ctrl_vsi;
460 pf->switchdev.control_vsi = ice_eswitch_vsi_setup(pf, pf->hw.port_info);
461 if (!pf->switchdev.control_vsi)
464 ctrl_vsi = pf->switchdev.control_vsi;
465 pf->switchdev.uplink_vsi = ice_get_main_vsi(pf);
466 if (!pf->switchdev.uplink_vsi)
469 if (ice_eswitch_setup_env(pf))
472 if (ice_repr_add_for_all_vfs(pf))
475 if (ice_eswitch_setup_reprs(pf))
476 goto err_setup_reprs;
478 ice_eswitch_remap_rings_to_vectors(pf);
480 if (ice_vsi_open(ctrl_vsi))
481 goto err_setup_reprs;
483 ice_eswitch_napi_enable(pf);
488 ice_repr_rem_from_all_vfs(pf);
490 ice_eswitch_release_env(pf);
492 ice_vsi_release(ctrl_vsi);
497 * ice_eswitch_disable_switchdev - disable switchdev resources
498 * @pf: pointer to PF structure
500 static void ice_eswitch_disable_switchdev(struct ice_pf *pf)
502 struct ice_vsi *ctrl_vsi = pf->switchdev.control_vsi;
504 ice_eswitch_napi_disable(pf);
505 ice_eswitch_release_env(pf);
506 ice_rem_adv_rule_for_vsi(&pf->hw, ctrl_vsi->idx);
507 ice_eswitch_release_reprs(pf, ctrl_vsi);
508 ice_vsi_release(ctrl_vsi);
509 ice_repr_rem_from_all_vfs(pf);
513 * ice_eswitch_mode_set - set new eswitch mode
514 * @devlink: pointer to devlink structure
515 * @mode: eswitch mode to switch to
516 * @extack: pointer to extack structure
519 ice_eswitch_mode_set(struct devlink *devlink, u16 mode,
520 struct netlink_ext_ack *extack)
522 struct ice_pf *pf = devlink_priv(devlink);
524 if (pf->eswitch_mode == mode)
527 if (ice_has_vfs(pf)) {
528 dev_info(ice_pf_to_dev(pf), "Changing eswitch mode is allowed only if there is no VFs created");
529 NL_SET_ERR_MSG_MOD(extack, "Changing eswitch mode is allowed only if there is no VFs created");
534 case DEVLINK_ESWITCH_MODE_LEGACY:
535 dev_info(ice_pf_to_dev(pf), "PF %d changed eswitch mode to legacy",
537 NL_SET_ERR_MSG_MOD(extack, "Changed eswitch mode to legacy");
539 case DEVLINK_ESWITCH_MODE_SWITCHDEV:
541 dev_info(ice_pf_to_dev(pf), "PF %d changed eswitch mode to switchdev",
543 NL_SET_ERR_MSG_MOD(extack, "Changed eswitch mode to switchdev");
547 NL_SET_ERR_MSG_MOD(extack, "Unknown eswitch mode");
551 pf->eswitch_mode = mode;
556 * ice_eswitch_mode_get - get current eswitch mode
557 * @devlink: pointer to devlink structure
558 * @mode: output parameter for current eswitch mode
560 int ice_eswitch_mode_get(struct devlink *devlink, u16 *mode)
562 struct ice_pf *pf = devlink_priv(devlink);
564 *mode = pf->eswitch_mode;
569 * ice_is_eswitch_mode_switchdev - check if eswitch mode is set to switchdev
570 * @pf: pointer to PF structure
572 * Returns true if eswitch mode is set to DEVLINK_ESWITCH_MODE_SWITCHDEV,
575 bool ice_is_eswitch_mode_switchdev(struct ice_pf *pf)
577 return pf->eswitch_mode == DEVLINK_ESWITCH_MODE_SWITCHDEV;
581 * ice_eswitch_release - cleanup eswitch
582 * @pf: pointer to PF structure
584 void ice_eswitch_release(struct ice_pf *pf)
586 if (pf->eswitch_mode == DEVLINK_ESWITCH_MODE_LEGACY)
589 ice_eswitch_disable_switchdev(pf);
590 pf->switchdev.is_running = false;
594 * ice_eswitch_configure - configure eswitch
595 * @pf: pointer to PF structure
597 int ice_eswitch_configure(struct ice_pf *pf)
601 if (pf->eswitch_mode == DEVLINK_ESWITCH_MODE_LEGACY || pf->switchdev.is_running)
604 status = ice_eswitch_enable_switchdev(pf);
608 pf->switchdev.is_running = true;
613 * ice_eswitch_start_all_tx_queues - start Tx queues of all port representors
614 * @pf: pointer to PF structure
616 static void ice_eswitch_start_all_tx_queues(struct ice_pf *pf)
621 lockdep_assert_held(&pf->vfs.table_lock);
623 if (test_bit(ICE_DOWN, pf->state))
626 ice_for_each_vf(pf, bkt, vf) {
628 ice_repr_start_tx_queues(vf->repr);
633 * ice_eswitch_stop_all_tx_queues - stop Tx queues of all port representors
634 * @pf: pointer to PF structure
636 void ice_eswitch_stop_all_tx_queues(struct ice_pf *pf)
641 lockdep_assert_held(&pf->vfs.table_lock);
643 if (test_bit(ICE_DOWN, pf->state))
646 ice_for_each_vf(pf, bkt, vf) {
648 ice_repr_stop_tx_queues(vf->repr);
653 * ice_eswitch_rebuild - rebuild eswitch
654 * @pf: pointer to PF structure
656 int ice_eswitch_rebuild(struct ice_pf *pf)
658 struct ice_vsi *ctrl_vsi = pf->switchdev.control_vsi;
661 ice_eswitch_napi_disable(pf);
662 ice_eswitch_napi_del(pf);
664 status = ice_eswitch_setup_env(pf);
668 status = ice_eswitch_setup_reprs(pf);
672 ice_eswitch_remap_rings_to_vectors(pf);
674 ice_replay_tc_fltrs(pf);
676 status = ice_vsi_open(ctrl_vsi);
680 ice_eswitch_napi_enable(pf);
681 ice_eswitch_start_all_tx_queues(pf);