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_mac_rule - add adv rule with VF's MAC
14 * @pf: pointer to PF struct
15 * @vf: pointer to VF struct
16 * @mac: VF's MAC address
18 * This function adds advanced rule that forwards packets with
19 * VF's MAC address (src MAC) to the corresponding switchdev ctrl VSI queue.
22 ice_eswitch_add_vf_mac_rule(struct ice_pf *pf, struct ice_vf *vf, const u8 *mac)
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 list[0].type = ICE_MAC_OFOS;
36 ether_addr_copy(list[0].h_u.eth_hdr.src_addr, mac);
37 eth_broadcast_addr(list[0].m_u.eth_hdr.src_addr);
39 rule_info.sw_act.flag |= ICE_FLTR_TX;
40 rule_info.sw_act.vsi_handle = ctrl_vsi->idx;
41 rule_info.sw_act.fltr_act = ICE_FWD_TO_Q;
43 rule_info.sw_act.fwd_id.q_id = hw->func_caps.common_cap.rxq_first_id +
44 ctrl_vsi->rxq_map[vf->vf_id];
45 rule_info.flags_info.act |= ICE_SINGLE_ACT_LB_ENABLE;
46 rule_info.flags_info.act_valid = true;
47 rule_info.tun_type = ICE_SW_TUN_AND_NON_TUN;
49 err = ice_add_adv_rule(hw, list, lkups_cnt, &rule_info,
52 dev_err(ice_pf_to_dev(pf), "Unable to add VF mac rule in switchdev mode for VF %d",
55 vf->repr->rule_added = true;
62 * ice_eswitch_replay_vf_mac_rule - replay adv rule with VF's MAC
63 * @vf: pointer to vF struct
65 * This function replays VF's MAC rule after reset.
67 void ice_eswitch_replay_vf_mac_rule(struct ice_vf *vf)
71 if (!ice_is_switchdev_running(vf->pf))
74 if (is_valid_ether_addr(vf->hw_lan_addr.addr)) {
75 err = ice_eswitch_add_vf_mac_rule(vf->pf, vf,
76 vf->hw_lan_addr.addr);
78 dev_err(ice_pf_to_dev(vf->pf), "Failed to add MAC %pM for VF %d\n, error %d\n",
79 vf->hw_lan_addr.addr, vf->vf_id, err);
84 ether_addr_copy(vf->dev_lan_addr.addr, vf->hw_lan_addr.addr);
89 * ice_eswitch_del_vf_mac_rule - delete adv rule with VF's MAC
90 * @vf: pointer to the VF struct
92 * Delete the advanced rule that was used to forward packets with the VF's MAC
93 * address (src MAC) to the corresponding switchdev ctrl VSI queue.
95 void ice_eswitch_del_vf_mac_rule(struct ice_vf *vf)
97 if (!ice_is_switchdev_running(vf->pf))
100 if (!vf->repr->rule_added)
103 ice_rem_adv_rule_by_id(&vf->pf->hw, vf->repr->mac_rule);
104 vf->repr->rule_added = false;
108 * ice_eswitch_setup_env - configure switchdev HW filters
109 * @pf: pointer to PF struct
111 * This function adds HW filters configuration specific for switchdev
114 static int ice_eswitch_setup_env(struct ice_pf *pf)
116 struct ice_vsi *uplink_vsi = pf->switchdev.uplink_vsi;
117 struct net_device *uplink_netdev = uplink_vsi->netdev;
118 struct ice_vsi *ctrl_vsi = pf->switchdev.control_vsi;
119 struct ice_vsi_vlan_ops *vlan_ops;
120 bool rule_added = false;
122 vlan_ops = ice_get_compat_vsi_vlan_ops(ctrl_vsi);
123 if (vlan_ops->dis_stripping(ctrl_vsi))
126 ice_remove_vsi_fltr(&pf->hw, uplink_vsi->idx);
128 netif_addr_lock_bh(uplink_netdev);
129 __dev_uc_unsync(uplink_netdev, NULL);
130 __dev_mc_unsync(uplink_netdev, NULL);
131 netif_addr_unlock_bh(uplink_netdev);
133 if (ice_vsi_add_vlan_zero(uplink_vsi))
136 if (!ice_is_dflt_vsi_in_use(uplink_vsi->vsw)) {
137 if (ice_set_dflt_vsi(uplink_vsi->vsw, uplink_vsi))
142 if (ice_vsi_update_security(uplink_vsi, ice_vsi_ctx_set_allow_override))
143 goto err_override_uplink;
145 if (ice_vsi_update_security(ctrl_vsi, ice_vsi_ctx_set_allow_override))
146 goto err_override_control;
150 err_override_control:
151 ice_vsi_update_security(uplink_vsi, ice_vsi_ctx_clear_allow_override);
154 ice_clear_dflt_vsi(uplink_vsi->vsw);
156 ice_fltr_add_mac_and_broadcast(uplink_vsi,
157 uplink_vsi->port_info->mac.perm_addr,
163 * ice_eswitch_remap_rings_to_vectors - reconfigure rings of switchdev ctrl VSI
164 * @pf: pointer to PF struct
166 * In switchdev number of allocated Tx/Rx rings is equal.
168 * This function fills q_vectors structures associated with representor and
169 * move each ring pairs to port representor netdevs. Each port representor
170 * will have dedicated 1 Tx/Rx ring pair, so number of rings pair is equal to
173 static void ice_eswitch_remap_rings_to_vectors(struct ice_pf *pf)
175 struct ice_vsi *vsi = pf->switchdev.control_vsi;
178 ice_for_each_txq(vsi, q_id) {
179 struct ice_q_vector *q_vector;
180 struct ice_tx_ring *tx_ring;
181 struct ice_rx_ring *rx_ring;
182 struct ice_repr *repr;
185 vf = ice_get_vf_by_id(pf, q_id);
190 q_vector = repr->q_vector;
191 tx_ring = vsi->tx_rings[q_id];
192 rx_ring = vsi->rx_rings[q_id];
195 q_vector->reg_idx = vsi->q_vectors[0]->reg_idx;
197 q_vector->num_ring_tx = 1;
198 q_vector->tx.tx_ring = tx_ring;
199 tx_ring->q_vector = q_vector;
200 tx_ring->next = NULL;
201 tx_ring->netdev = repr->netdev;
202 /* In switchdev mode, from OS stack perspective, there is only
203 * one queue for given netdev, so it needs to be indexed as 0.
205 tx_ring->q_index = 0;
207 q_vector->num_ring_rx = 1;
208 q_vector->rx.rx_ring = rx_ring;
209 rx_ring->q_vector = q_vector;
210 rx_ring->next = NULL;
211 rx_ring->netdev = repr->netdev;
218 * ice_eswitch_release_reprs - clear PR VSIs configuration
219 * @pf: poiner to PF struct
220 * @ctrl_vsi: pointer to switchdev control VSI
223 ice_eswitch_release_reprs(struct ice_pf *pf, struct ice_vsi *ctrl_vsi)
228 lockdep_assert_held(&pf->vfs.table_lock);
230 ice_for_each_vf(pf, bkt, vf) {
231 struct ice_vsi *vsi = vf->repr->src_vsi;
233 /* Skip VFs that aren't configured */
237 ice_vsi_update_security(vsi, ice_vsi_ctx_set_antispoof);
238 metadata_dst_free(vf->repr->dst);
239 vf->repr->dst = NULL;
240 ice_fltr_add_mac_and_broadcast(vsi, vf->hw_lan_addr.addr,
243 netif_napi_del(&vf->repr->q_vector->napi);
248 * ice_eswitch_setup_reprs - configure port reprs to run in switchdev mode
249 * @pf: pointer to PF struct
251 static int ice_eswitch_setup_reprs(struct ice_pf *pf)
253 struct ice_vsi *ctrl_vsi = pf->switchdev.control_vsi;
258 lockdep_assert_held(&pf->vfs.table_lock);
260 ice_for_each_vf(pf, bkt, vf) {
261 struct ice_vsi *vsi = vf->repr->src_vsi;
263 ice_remove_vsi_fltr(&pf->hw, vsi->idx);
264 vf->repr->dst = metadata_dst_alloc(0, METADATA_HW_PORT_MUX,
266 if (!vf->repr->dst) {
267 ice_fltr_add_mac_and_broadcast(vsi,
268 vf->hw_lan_addr.addr,
273 if (ice_vsi_update_security(vsi, ice_vsi_ctx_clear_antispoof)) {
274 ice_fltr_add_mac_and_broadcast(vsi,
275 vf->hw_lan_addr.addr,
277 metadata_dst_free(vf->repr->dst);
278 vf->repr->dst = NULL;
282 if (ice_vsi_add_vlan_zero(vsi)) {
283 ice_fltr_add_mac_and_broadcast(vsi,
284 vf->hw_lan_addr.addr,
286 metadata_dst_free(vf->repr->dst);
287 vf->repr->dst = NULL;
288 ice_vsi_update_security(vsi, ice_vsi_ctx_set_antispoof);
292 if (max_vsi_num < vsi->vsi_num)
293 max_vsi_num = vsi->vsi_num;
295 netif_napi_add(vf->repr->netdev, &vf->repr->q_vector->napi, ice_napi_poll,
298 netif_keep_dst(vf->repr->netdev);
301 ice_for_each_vf(pf, bkt, vf) {
302 struct ice_repr *repr = vf->repr;
303 struct ice_vsi *vsi = repr->src_vsi;
304 struct metadata_dst *dst;
307 dst->u.port_info.port_id = vsi->vsi_num;
308 dst->u.port_info.lower_dev = repr->netdev;
309 ice_repr_set_traffic_vsi(repr, ctrl_vsi);
315 ice_eswitch_release_reprs(pf, ctrl_vsi);
321 * ice_eswitch_update_repr - reconfigure VF port representor
322 * @vsi: VF VSI for which port representor is configured
324 void ice_eswitch_update_repr(struct ice_vsi *vsi)
326 struct ice_pf *pf = vsi->back;
327 struct ice_repr *repr;
331 if (!ice_is_switchdev_running(pf))
337 repr->dst->u.port_info.port_id = vsi->vsi_num;
339 ret = ice_vsi_update_security(vsi, ice_vsi_ctx_clear_antispoof);
341 ice_fltr_add_mac_and_broadcast(vsi, vf->hw_lan_addr.addr, ICE_FWD_TO_VSI);
342 dev_err(ice_pf_to_dev(pf), "Failed to update VF %d port representor",
348 * ice_eswitch_port_start_xmit - callback for packets transmit
350 * @netdev: network interface device structure
352 * Returns NETDEV_TX_OK if sent, else an error code
355 ice_eswitch_port_start_xmit(struct sk_buff *skb, struct net_device *netdev)
357 struct ice_netdev_priv *np;
358 struct ice_repr *repr;
361 np = netdev_priv(netdev);
364 if (ice_is_reset_in_progress(vsi->back->state) ||
365 test_bit(ICE_VF_DIS, vsi->back->state))
366 return NETDEV_TX_BUSY;
368 repr = ice_netdev_to_repr(netdev);
370 dst_hold((struct dst_entry *)repr->dst);
371 skb_dst_set(skb, (struct dst_entry *)repr->dst);
372 skb->queue_mapping = repr->vf->vf_id;
374 return ice_start_xmit(skb, netdev);
378 * ice_eswitch_set_target_vsi - set switchdev context in Tx context descriptor
379 * @skb: pointer to send buffer
380 * @off: pointer to offload struct
383 ice_eswitch_set_target_vsi(struct sk_buff *skb,
384 struct ice_tx_offload_params *off)
386 struct metadata_dst *dst = skb_metadata_dst(skb);
390 cd_cmd = ICE_TX_CTX_DESC_SWTCH_UPLINK << ICE_TXD_CTX_QW1_CMD_S;
391 off->cd_qw1 |= (cd_cmd | ICE_TX_DESC_DTYPE_CTX);
393 cd_cmd = ICE_TX_CTX_DESC_SWTCH_VSI << ICE_TXD_CTX_QW1_CMD_S;
394 dst_vsi = ((u64)dst->u.port_info.port_id <<
395 ICE_TXD_CTX_QW1_VSI_S) & ICE_TXD_CTX_QW1_VSI_M;
396 off->cd_qw1 = cd_cmd | dst_vsi | ICE_TX_DESC_DTYPE_CTX;
401 * ice_eswitch_release_env - clear switchdev HW filters
402 * @pf: pointer to PF struct
404 * This function removes HW filters configuration specific for switchdev
405 * mode and restores default legacy mode settings.
407 static void ice_eswitch_release_env(struct ice_pf *pf)
409 struct ice_vsi *uplink_vsi = pf->switchdev.uplink_vsi;
410 struct ice_vsi *ctrl_vsi = pf->switchdev.control_vsi;
412 ice_vsi_update_security(ctrl_vsi, ice_vsi_ctx_clear_allow_override);
413 ice_vsi_update_security(uplink_vsi, ice_vsi_ctx_clear_allow_override);
414 ice_clear_dflt_vsi(uplink_vsi->vsw);
415 ice_fltr_add_mac_and_broadcast(uplink_vsi,
416 uplink_vsi->port_info->mac.perm_addr,
421 * ice_eswitch_vsi_setup - configure switchdev control VSI
422 * @pf: pointer to PF structure
423 * @pi: pointer to port_info structure
425 static struct ice_vsi *
426 ice_eswitch_vsi_setup(struct ice_pf *pf, struct ice_port_info *pi)
428 return ice_vsi_setup(pf, pi, ICE_VSI_SWITCHDEV_CTRL, NULL, NULL);
432 * ice_eswitch_napi_del - remove NAPI handle for all port representors
433 * @pf: pointer to PF structure
435 static void ice_eswitch_napi_del(struct ice_pf *pf)
440 lockdep_assert_held(&pf->vfs.table_lock);
442 ice_for_each_vf(pf, bkt, vf)
443 netif_napi_del(&vf->repr->q_vector->napi);
447 * ice_eswitch_napi_enable - enable NAPI for all port representors
448 * @pf: pointer to PF structure
450 static void ice_eswitch_napi_enable(struct ice_pf *pf)
455 lockdep_assert_held(&pf->vfs.table_lock);
457 ice_for_each_vf(pf, bkt, vf)
458 napi_enable(&vf->repr->q_vector->napi);
462 * ice_eswitch_napi_disable - disable NAPI for all port representors
463 * @pf: pointer to PF structure
465 static void ice_eswitch_napi_disable(struct ice_pf *pf)
470 lockdep_assert_held(&pf->vfs.table_lock);
472 ice_for_each_vf(pf, bkt, vf)
473 napi_disable(&vf->repr->q_vector->napi);
477 * ice_eswitch_enable_switchdev - configure eswitch in switchdev mode
478 * @pf: pointer to PF structure
480 static int ice_eswitch_enable_switchdev(struct ice_pf *pf)
482 struct ice_vsi *ctrl_vsi;
484 pf->switchdev.control_vsi = ice_eswitch_vsi_setup(pf, pf->hw.port_info);
485 if (!pf->switchdev.control_vsi)
488 ctrl_vsi = pf->switchdev.control_vsi;
489 pf->switchdev.uplink_vsi = ice_get_main_vsi(pf);
490 if (!pf->switchdev.uplink_vsi)
493 if (ice_eswitch_setup_env(pf))
496 if (ice_repr_add_for_all_vfs(pf))
499 if (ice_eswitch_setup_reprs(pf))
500 goto err_setup_reprs;
502 ice_eswitch_remap_rings_to_vectors(pf);
504 if (ice_vsi_open(ctrl_vsi))
505 goto err_setup_reprs;
507 ice_eswitch_napi_enable(pf);
512 ice_repr_rem_from_all_vfs(pf);
514 ice_eswitch_release_env(pf);
516 ice_vsi_release(ctrl_vsi);
521 * ice_eswitch_disable_switchdev - disable switchdev resources
522 * @pf: pointer to PF structure
524 static void ice_eswitch_disable_switchdev(struct ice_pf *pf)
526 struct ice_vsi *ctrl_vsi = pf->switchdev.control_vsi;
528 ice_eswitch_napi_disable(pf);
529 ice_eswitch_release_env(pf);
530 ice_rem_adv_rule_for_vsi(&pf->hw, ctrl_vsi->idx);
531 ice_eswitch_release_reprs(pf, ctrl_vsi);
532 ice_vsi_release(ctrl_vsi);
533 ice_repr_rem_from_all_vfs(pf);
537 * ice_eswitch_mode_set - set new eswitch mode
538 * @devlink: pointer to devlink structure
539 * @mode: eswitch mode to switch to
540 * @extack: pointer to extack structure
543 ice_eswitch_mode_set(struct devlink *devlink, u16 mode,
544 struct netlink_ext_ack *extack)
546 struct ice_pf *pf = devlink_priv(devlink);
548 if (pf->eswitch_mode == mode)
551 if (ice_has_vfs(pf)) {
552 dev_info(ice_pf_to_dev(pf), "Changing eswitch mode is allowed only if there is no VFs created");
553 NL_SET_ERR_MSG_MOD(extack, "Changing eswitch mode is allowed only if there is no VFs created");
558 case DEVLINK_ESWITCH_MODE_LEGACY:
559 dev_info(ice_pf_to_dev(pf), "PF %d changed eswitch mode to legacy",
561 NL_SET_ERR_MSG_MOD(extack, "Changed eswitch mode to legacy");
563 case DEVLINK_ESWITCH_MODE_SWITCHDEV:
565 dev_info(ice_pf_to_dev(pf), "PF %d changed eswitch mode to switchdev",
567 NL_SET_ERR_MSG_MOD(extack, "Changed eswitch mode to switchdev");
571 NL_SET_ERR_MSG_MOD(extack, "Unknown eswitch mode");
575 pf->eswitch_mode = mode;
580 * ice_eswitch_mode_get - get current eswitch mode
581 * @devlink: pointer to devlink structure
582 * @mode: output parameter for current eswitch mode
584 int ice_eswitch_mode_get(struct devlink *devlink, u16 *mode)
586 struct ice_pf *pf = devlink_priv(devlink);
588 *mode = pf->eswitch_mode;
593 * ice_is_eswitch_mode_switchdev - check if eswitch mode is set to switchdev
594 * @pf: pointer to PF structure
596 * Returns true if eswitch mode is set to DEVLINK_ESWITCH_MODE_SWITCHDEV,
599 bool ice_is_eswitch_mode_switchdev(struct ice_pf *pf)
601 return pf->eswitch_mode == DEVLINK_ESWITCH_MODE_SWITCHDEV;
605 * ice_eswitch_release - cleanup eswitch
606 * @pf: pointer to PF structure
608 void ice_eswitch_release(struct ice_pf *pf)
610 if (pf->eswitch_mode == DEVLINK_ESWITCH_MODE_LEGACY)
613 ice_eswitch_disable_switchdev(pf);
614 pf->switchdev.is_running = false;
618 * ice_eswitch_configure - configure eswitch
619 * @pf: pointer to PF structure
621 int ice_eswitch_configure(struct ice_pf *pf)
625 if (pf->eswitch_mode == DEVLINK_ESWITCH_MODE_LEGACY || pf->switchdev.is_running)
628 status = ice_eswitch_enable_switchdev(pf);
632 pf->switchdev.is_running = true;
637 * ice_eswitch_start_all_tx_queues - start Tx queues of all port representors
638 * @pf: pointer to PF structure
640 static void ice_eswitch_start_all_tx_queues(struct ice_pf *pf)
645 lockdep_assert_held(&pf->vfs.table_lock);
647 if (test_bit(ICE_DOWN, pf->state))
650 ice_for_each_vf(pf, bkt, vf) {
652 ice_repr_start_tx_queues(vf->repr);
657 * ice_eswitch_stop_all_tx_queues - stop Tx queues of all port representors
658 * @pf: pointer to PF structure
660 void ice_eswitch_stop_all_tx_queues(struct ice_pf *pf)
665 lockdep_assert_held(&pf->vfs.table_lock);
667 if (test_bit(ICE_DOWN, pf->state))
670 ice_for_each_vf(pf, bkt, vf) {
672 ice_repr_stop_tx_queues(vf->repr);
677 * ice_eswitch_rebuild - rebuild eswitch
678 * @pf: pointer to PF structure
680 int ice_eswitch_rebuild(struct ice_pf *pf)
682 struct ice_vsi *ctrl_vsi = pf->switchdev.control_vsi;
685 ice_eswitch_napi_disable(pf);
686 ice_eswitch_napi_del(pf);
688 status = ice_eswitch_setup_env(pf);
692 status = ice_eswitch_setup_reprs(pf);
696 ice_eswitch_remap_rings_to_vectors(pf);
698 ice_replay_tc_fltrs(pf);
700 status = ice_vsi_open(ctrl_vsi);
704 ice_eswitch_napi_enable(pf);
705 ice_eswitch_start_all_tx_queues(pf);