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;
48 err = ice_add_adv_rule(hw, list, lkups_cnt, &rule_info,
51 dev_err(ice_pf_to_dev(pf), "Unable to add VF mac rule in switchdev mode for VF %d",
54 vf->repr->rule_added = true;
61 * ice_eswitch_replay_vf_mac_rule - replay adv rule with VF's MAC
62 * @vf: pointer to vF struct
64 * This function replays VF's MAC rule after reset.
66 void ice_eswitch_replay_vf_mac_rule(struct ice_vf *vf)
70 if (!ice_is_switchdev_running(vf->pf))
73 if (is_valid_ether_addr(vf->hw_lan_addr.addr)) {
74 err = ice_eswitch_add_vf_mac_rule(vf->pf, vf,
75 vf->hw_lan_addr.addr);
77 dev_err(ice_pf_to_dev(vf->pf), "Failed to add MAC %pM for VF %d\n, error %d\n",
78 vf->hw_lan_addr.addr, vf->vf_id, err);
83 ether_addr_copy(vf->dev_lan_addr.addr, vf->hw_lan_addr.addr);
88 * ice_eswitch_del_vf_mac_rule - delete adv rule with VF's MAC
89 * @vf: pointer to the VF struct
91 * Delete the advanced rule that was used to forward packets with the VF's MAC
92 * address (src MAC) to the corresponding switchdev ctrl VSI queue.
94 void ice_eswitch_del_vf_mac_rule(struct ice_vf *vf)
96 if (!ice_is_switchdev_running(vf->pf))
99 if (!vf->repr->rule_added)
102 ice_rem_adv_rule_by_id(&vf->pf->hw, vf->repr->mac_rule);
103 vf->repr->rule_added = false;
107 * ice_eswitch_setup_env - configure switchdev HW filters
108 * @pf: pointer to PF struct
110 * This function adds HW filters configuration specific for switchdev
113 static int ice_eswitch_setup_env(struct ice_pf *pf)
115 struct ice_vsi *uplink_vsi = pf->switchdev.uplink_vsi;
116 struct net_device *uplink_netdev = uplink_vsi->netdev;
117 struct ice_vsi *ctrl_vsi = pf->switchdev.control_vsi;
118 bool rule_added = false;
120 ice_vsi_manage_vlan_stripping(ctrl_vsi, false);
122 ice_remove_vsi_fltr(&pf->hw, uplink_vsi->idx);
124 netif_addr_lock_bh(uplink_netdev);
125 __dev_uc_unsync(uplink_netdev, NULL);
126 __dev_mc_unsync(uplink_netdev, NULL);
127 netif_addr_unlock_bh(uplink_netdev);
129 if (ice_vsi_add_vlan(uplink_vsi, 0, ICE_FWD_TO_VSI))
132 if (!ice_is_dflt_vsi_in_use(uplink_vsi->vsw)) {
133 if (ice_set_dflt_vsi(uplink_vsi->vsw, uplink_vsi))
138 if (ice_vsi_update_security(uplink_vsi, ice_vsi_ctx_set_allow_override))
139 goto err_override_uplink;
141 if (ice_vsi_update_security(ctrl_vsi, ice_vsi_ctx_set_allow_override))
142 goto err_override_control;
146 err_override_control:
147 ice_vsi_update_security(uplink_vsi, ice_vsi_ctx_clear_allow_override);
150 ice_clear_dflt_vsi(uplink_vsi->vsw);
152 ice_fltr_add_mac_and_broadcast(uplink_vsi,
153 uplink_vsi->port_info->mac.perm_addr,
159 * ice_eswitch_remap_rings_to_vectors - reconfigure rings of switchdev ctrl VSI
160 * @pf: pointer to PF struct
162 * In switchdev number of allocated Tx/Rx rings is equal.
164 * This function fills q_vectors structures associated with representor and
165 * move each ring pairs to port representor netdevs. Each port representor
166 * will have dedicated 1 Tx/Rx ring pair, so number of rings pair is equal to
169 static void ice_eswitch_remap_rings_to_vectors(struct ice_pf *pf)
171 struct ice_vsi *vsi = pf->switchdev.control_vsi;
174 ice_for_each_txq(vsi, q_id) {
175 struct ice_repr *repr = pf->vf[q_id].repr;
176 struct ice_q_vector *q_vector = repr->q_vector;
177 struct ice_tx_ring *tx_ring = vsi->tx_rings[q_id];
178 struct ice_rx_ring *rx_ring = vsi->rx_rings[q_id];
181 q_vector->reg_idx = vsi->q_vectors[0]->reg_idx;
183 q_vector->num_ring_tx = 1;
184 q_vector->tx.tx_ring = tx_ring;
185 tx_ring->q_vector = q_vector;
186 tx_ring->next = NULL;
187 tx_ring->netdev = repr->netdev;
188 /* In switchdev mode, from OS stack perspective, there is only
189 * one queue for given netdev, so it needs to be indexed as 0.
191 tx_ring->q_index = 0;
193 q_vector->num_ring_rx = 1;
194 q_vector->rx.rx_ring = rx_ring;
195 rx_ring->q_vector = q_vector;
196 rx_ring->next = NULL;
197 rx_ring->netdev = repr->netdev;
202 * ice_eswitch_setup_reprs - configure port reprs to run in switchdev mode
203 * @pf: pointer to PF struct
205 static int ice_eswitch_setup_reprs(struct ice_pf *pf)
207 struct ice_vsi *ctrl_vsi = pf->switchdev.control_vsi;
211 ice_for_each_vf(pf, i) {
212 struct ice_vsi *vsi = pf->vf[i].repr->src_vsi;
213 struct ice_vf *vf = &pf->vf[i];
215 ice_remove_vsi_fltr(&pf->hw, vsi->idx);
216 vf->repr->dst = metadata_dst_alloc(0, METADATA_HW_PORT_MUX,
218 if (!vf->repr->dst) {
219 ice_fltr_add_mac_and_broadcast(vsi,
220 vf->hw_lan_addr.addr,
225 if (ice_vsi_update_security(vsi, ice_vsi_ctx_clear_antispoof)) {
226 ice_fltr_add_mac_and_broadcast(vsi,
227 vf->hw_lan_addr.addr,
229 metadata_dst_free(vf->repr->dst);
233 if (ice_vsi_add_vlan(vsi, 0, ICE_FWD_TO_VSI)) {
234 ice_fltr_add_mac_and_broadcast(vsi,
235 vf->hw_lan_addr.addr,
237 metadata_dst_free(vf->repr->dst);
238 ice_vsi_update_security(vsi, ice_vsi_ctx_set_antispoof);
242 if (max_vsi_num < vsi->vsi_num)
243 max_vsi_num = vsi->vsi_num;
245 netif_napi_add(vf->repr->netdev, &vf->repr->q_vector->napi, ice_napi_poll,
248 netif_keep_dst(vf->repr->netdev);
251 ice_for_each_vf(pf, i) {
252 struct ice_repr *repr = pf->vf[i].repr;
253 struct ice_vsi *vsi = repr->src_vsi;
254 struct metadata_dst *dst;
257 dst->u.port_info.port_id = vsi->vsi_num;
258 dst->u.port_info.lower_dev = repr->netdev;
259 ice_repr_set_traffic_vsi(repr, ctrl_vsi);
265 for (i = i - 1; i >= 0; i--) {
266 struct ice_vsi *vsi = pf->vf[i].repr->src_vsi;
267 struct ice_vf *vf = &pf->vf[i];
269 ice_vsi_update_security(vsi, ice_vsi_ctx_set_antispoof);
270 metadata_dst_free(vf->repr->dst);
271 ice_fltr_add_mac_and_broadcast(vsi, vf->hw_lan_addr.addr,
279 * ice_eswitch_release_reprs - clear PR VSIs configuration
280 * @pf: poiner to PF struct
281 * @ctrl_vsi: pointer to switchdev control VSI
284 ice_eswitch_release_reprs(struct ice_pf *pf, struct ice_vsi *ctrl_vsi)
288 ice_for_each_vf(pf, i) {
289 struct ice_vsi *vsi = pf->vf[i].repr->src_vsi;
290 struct ice_vf *vf = &pf->vf[i];
292 ice_vsi_update_security(vsi, ice_vsi_ctx_set_antispoof);
293 metadata_dst_free(vf->repr->dst);
294 ice_fltr_add_mac_and_broadcast(vsi, vf->hw_lan_addr.addr,
297 netif_napi_del(&vf->repr->q_vector->napi);
302 * ice_eswitch_update_repr - reconfigure VF port representor
303 * @vsi: VF VSI for which port representor is configured
305 void ice_eswitch_update_repr(struct ice_vsi *vsi)
307 struct ice_pf *pf = vsi->back;
308 struct ice_repr *repr;
312 if (!ice_is_switchdev_running(pf))
315 vf = &pf->vf[vsi->vf_id];
318 repr->dst->u.port_info.port_id = vsi->vsi_num;
320 ret = ice_vsi_update_security(vsi, ice_vsi_ctx_clear_antispoof);
322 ice_fltr_add_mac_and_broadcast(vsi, vf->hw_lan_addr.addr, ICE_FWD_TO_VSI);
323 dev_err(ice_pf_to_dev(pf), "Failed to update VF %d port representor", vsi->vf_id);
328 * ice_eswitch_port_start_xmit - callback for packets transmit
330 * @netdev: network interface device structure
332 * Returns NETDEV_TX_OK if sent, else an error code
335 ice_eswitch_port_start_xmit(struct sk_buff *skb, struct net_device *netdev)
337 struct ice_netdev_priv *np;
338 struct ice_repr *repr;
341 np = netdev_priv(netdev);
344 if (ice_is_reset_in_progress(vsi->back->state))
345 return NETDEV_TX_BUSY;
347 repr = ice_netdev_to_repr(netdev);
349 dst_hold((struct dst_entry *)repr->dst);
350 skb_dst_set(skb, (struct dst_entry *)repr->dst);
351 skb->queue_mapping = repr->vf->vf_id;
353 return ice_start_xmit(skb, netdev);
357 * ice_eswitch_set_target_vsi - set switchdev context in Tx context descriptor
358 * @skb: pointer to send buffer
359 * @off: pointer to offload struct
362 ice_eswitch_set_target_vsi(struct sk_buff *skb,
363 struct ice_tx_offload_params *off)
365 struct metadata_dst *dst = skb_metadata_dst(skb);
369 cd_cmd = ICE_TX_CTX_DESC_SWTCH_UPLINK << ICE_TXD_CTX_QW1_CMD_S;
370 off->cd_qw1 |= (cd_cmd | ICE_TX_DESC_DTYPE_CTX);
372 cd_cmd = ICE_TX_CTX_DESC_SWTCH_VSI << ICE_TXD_CTX_QW1_CMD_S;
373 dst_vsi = ((u64)dst->u.port_info.port_id <<
374 ICE_TXD_CTX_QW1_VSI_S) & ICE_TXD_CTX_QW1_VSI_M;
375 off->cd_qw1 = cd_cmd | dst_vsi | ICE_TX_DESC_DTYPE_CTX;
380 * ice_eswitch_release_env - clear switchdev HW filters
381 * @pf: pointer to PF struct
383 * This function removes HW filters configuration specific for switchdev
384 * mode and restores default legacy mode settings.
386 static void ice_eswitch_release_env(struct ice_pf *pf)
388 struct ice_vsi *uplink_vsi = pf->switchdev.uplink_vsi;
389 struct ice_vsi *ctrl_vsi = pf->switchdev.control_vsi;
391 ice_vsi_update_security(ctrl_vsi, ice_vsi_ctx_clear_allow_override);
392 ice_vsi_update_security(uplink_vsi, ice_vsi_ctx_clear_allow_override);
393 ice_clear_dflt_vsi(uplink_vsi->vsw);
394 ice_fltr_add_mac_and_broadcast(uplink_vsi,
395 uplink_vsi->port_info->mac.perm_addr,
400 * ice_eswitch_vsi_setup - configure switchdev control VSI
401 * @pf: pointer to PF structure
402 * @pi: pointer to port_info structure
404 static struct ice_vsi *
405 ice_eswitch_vsi_setup(struct ice_pf *pf, struct ice_port_info *pi)
407 return ice_vsi_setup(pf, pi, ICE_VSI_SWITCHDEV_CTRL, ICE_INVAL_VFID, NULL);
411 * ice_eswitch_napi_del - remove NAPI handle for all port representors
412 * @pf: pointer to PF structure
414 static void ice_eswitch_napi_del(struct ice_pf *pf)
418 ice_for_each_vf(pf, i)
419 netif_napi_del(&pf->vf[i].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)
430 ice_for_each_vf(pf, i)
431 napi_enable(&pf->vf[i].repr->q_vector->napi);
435 * ice_eswitch_napi_disable - disable NAPI for all port representors
436 * @pf: pointer to PF structure
438 static void ice_eswitch_napi_disable(struct ice_pf *pf)
442 ice_for_each_vf(pf, i)
443 napi_disable(&pf->vf[i].repr->q_vector->napi);
447 * ice_eswitch_enable_switchdev - configure eswitch in switchdev mode
448 * @pf: pointer to PF structure
450 static int ice_eswitch_enable_switchdev(struct ice_pf *pf)
452 struct ice_vsi *ctrl_vsi;
454 pf->switchdev.control_vsi = ice_eswitch_vsi_setup(pf, pf->hw.port_info);
455 if (!pf->switchdev.control_vsi)
458 ctrl_vsi = pf->switchdev.control_vsi;
459 pf->switchdev.uplink_vsi = ice_get_main_vsi(pf);
460 if (!pf->switchdev.uplink_vsi)
463 if (ice_eswitch_setup_env(pf))
466 if (ice_repr_add_for_all_vfs(pf))
469 if (ice_eswitch_setup_reprs(pf))
470 goto err_setup_reprs;
472 ice_eswitch_remap_rings_to_vectors(pf);
474 if (ice_vsi_open(ctrl_vsi))
475 goto err_setup_reprs;
477 ice_eswitch_napi_enable(pf);
482 ice_repr_rem_from_all_vfs(pf);
484 ice_eswitch_release_env(pf);
486 ice_vsi_release(ctrl_vsi);
491 * ice_eswitch_disable_switchdev - disable switchdev resources
492 * @pf: pointer to PF structure
494 static void ice_eswitch_disable_switchdev(struct ice_pf *pf)
496 struct ice_vsi *ctrl_vsi = pf->switchdev.control_vsi;
498 ice_eswitch_napi_disable(pf);
499 ice_eswitch_release_env(pf);
500 ice_rem_adv_rule_for_vsi(&pf->hw, ctrl_vsi->idx);
501 ice_eswitch_release_reprs(pf, ctrl_vsi);
502 ice_vsi_release(ctrl_vsi);
503 ice_repr_rem_from_all_vfs(pf);
507 * ice_eswitch_mode_set - set new eswitch mode
508 * @devlink: pointer to devlink structure
509 * @mode: eswitch mode to switch to
510 * @extack: pointer to extack structure
513 ice_eswitch_mode_set(struct devlink *devlink, u16 mode,
514 struct netlink_ext_ack *extack)
516 struct ice_pf *pf = devlink_priv(devlink);
518 if (pf->eswitch_mode == mode)
521 if (pf->num_alloc_vfs) {
522 dev_info(ice_pf_to_dev(pf), "Changing eswitch mode is allowed only if there is no VFs created");
523 NL_SET_ERR_MSG_MOD(extack, "Changing eswitch mode is allowed only if there is no VFs created");
528 case DEVLINK_ESWITCH_MODE_LEGACY:
529 dev_info(ice_pf_to_dev(pf), "PF %d changed eswitch mode to legacy",
531 NL_SET_ERR_MSG_MOD(extack, "Changed eswitch mode to legacy");
533 case DEVLINK_ESWITCH_MODE_SWITCHDEV:
535 dev_info(ice_pf_to_dev(pf), "PF %d changed eswitch mode to switchdev",
537 NL_SET_ERR_MSG_MOD(extack, "Changed eswitch mode to switchdev");
541 NL_SET_ERR_MSG_MOD(extack, "Unknown eswitch mode");
545 pf->eswitch_mode = mode;
550 * ice_eswitch_mode_get - get current eswitch mode
551 * @devlink: pointer to devlink structure
552 * @mode: output parameter for current eswitch mode
554 int ice_eswitch_mode_get(struct devlink *devlink, u16 *mode)
556 struct ice_pf *pf = devlink_priv(devlink);
558 *mode = pf->eswitch_mode;
563 * ice_is_eswitch_mode_switchdev - check if eswitch mode is set to switchdev
564 * @pf: pointer to PF structure
566 * Returns true if eswitch mode is set to DEVLINK_ESWITCH_MODE_SWITCHDEV,
569 bool ice_is_eswitch_mode_switchdev(struct ice_pf *pf)
571 return pf->eswitch_mode == DEVLINK_ESWITCH_MODE_SWITCHDEV;
575 * ice_eswitch_release - cleanup eswitch
576 * @pf: pointer to PF structure
578 void ice_eswitch_release(struct ice_pf *pf)
580 if (pf->eswitch_mode == DEVLINK_ESWITCH_MODE_LEGACY)
583 ice_eswitch_disable_switchdev(pf);
584 pf->switchdev.is_running = false;
588 * ice_eswitch_configure - configure eswitch
589 * @pf: pointer to PF structure
591 int ice_eswitch_configure(struct ice_pf *pf)
595 if (pf->eswitch_mode == DEVLINK_ESWITCH_MODE_LEGACY || pf->switchdev.is_running)
598 status = ice_eswitch_enable_switchdev(pf);
602 pf->switchdev.is_running = true;
607 * ice_eswitch_start_all_tx_queues - start Tx queues of all port representors
608 * @pf: pointer to PF structure
610 static void ice_eswitch_start_all_tx_queues(struct ice_pf *pf)
612 struct ice_repr *repr;
615 if (test_bit(ICE_DOWN, pf->state))
618 ice_for_each_vf(pf, i) {
619 repr = pf->vf[i].repr;
621 ice_repr_start_tx_queues(repr);
626 * ice_eswitch_stop_all_tx_queues - stop Tx queues of all port representors
627 * @pf: pointer to PF structure
629 void ice_eswitch_stop_all_tx_queues(struct ice_pf *pf)
631 struct ice_repr *repr;
634 if (test_bit(ICE_DOWN, pf->state))
637 ice_for_each_vf(pf, i) {
638 repr = pf->vf[i].repr;
640 ice_repr_stop_tx_queues(repr);
645 * ice_eswitch_rebuild - rebuild eswitch
646 * @pf: pointer to PF structure
648 int ice_eswitch_rebuild(struct ice_pf *pf)
650 struct ice_vsi *ctrl_vsi = pf->switchdev.control_vsi;
653 ice_eswitch_napi_disable(pf);
654 ice_eswitch_napi_del(pf);
656 status = ice_eswitch_setup_env(pf);
660 status = ice_eswitch_setup_reprs(pf);
664 ice_eswitch_remap_rings_to_vectors(pf);
666 ice_replay_tc_fltrs(pf);
668 status = ice_vsi_open(ctrl_vsi);
672 ice_eswitch_napi_enable(pf);
673 ice_eswitch_start_all_tx_queues(pf);