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 "devlink/devlink.h"
11 #include "ice_tc_lib.h"
14 * ice_eswitch_setup_env - configure eswitch HW filters
15 * @pf: pointer to PF struct
17 * This function adds HW filters configuration specific for switchdev
20 static int ice_eswitch_setup_env(struct ice_pf *pf)
22 struct ice_vsi *uplink_vsi = pf->eswitch.uplink_vsi;
23 struct net_device *netdev = uplink_vsi->netdev;
24 bool if_running = netif_running(netdev);
25 struct ice_vsi_vlan_ops *vlan_ops;
27 if (if_running && !test_and_set_bit(ICE_VSI_DOWN, uplink_vsi->state))
28 if (ice_down(uplink_vsi))
31 ice_remove_vsi_fltr(&pf->hw, uplink_vsi->idx);
33 netif_addr_lock_bh(netdev);
34 __dev_uc_unsync(netdev, NULL);
35 __dev_mc_unsync(netdev, NULL);
36 netif_addr_unlock_bh(netdev);
38 if (ice_vsi_add_vlan_zero(uplink_vsi))
41 if (ice_cfg_dflt_vsi(uplink_vsi->port_info, uplink_vsi->idx, true,
45 if (ice_cfg_dflt_vsi(uplink_vsi->port_info, uplink_vsi->idx, true,
49 vlan_ops = ice_get_compat_vsi_vlan_ops(uplink_vsi);
50 if (vlan_ops->dis_rx_filtering(uplink_vsi))
51 goto err_vlan_filtering;
53 if (ice_vsi_update_security(uplink_vsi, ice_vsi_ctx_set_allow_override))
54 goto err_override_uplink;
56 if (ice_vsi_update_local_lb(uplink_vsi, true))
57 goto err_override_local_lb;
59 if (if_running && ice_up(uplink_vsi))
65 ice_vsi_update_local_lb(uplink_vsi, false);
66 err_override_local_lb:
67 ice_vsi_update_security(uplink_vsi, ice_vsi_ctx_clear_allow_override);
69 vlan_ops->ena_rx_filtering(uplink_vsi);
71 ice_cfg_dflt_vsi(uplink_vsi->port_info, uplink_vsi->idx, false,
74 ice_cfg_dflt_vsi(uplink_vsi->port_info, uplink_vsi->idx, false,
77 ice_vsi_del_vlan_zero(uplink_vsi);
79 ice_fltr_add_mac_and_broadcast(uplink_vsi,
80 uplink_vsi->port_info->mac.perm_addr,
89 * ice_eswitch_release_repr - clear PR VSI configuration
90 * @pf: poiner to PF struct
91 * @repr: pointer to PR
94 ice_eswitch_release_repr(struct ice_pf *pf, struct ice_repr *repr)
96 struct ice_vsi *vsi = repr->src_vsi;
98 /* Skip representors that aren't configured */
102 ice_vsi_update_security(vsi, ice_vsi_ctx_set_antispoof);
103 metadata_dst_free(repr->dst);
105 ice_fltr_add_mac_and_broadcast(vsi, repr->parent_mac,
110 * ice_eswitch_setup_repr - configure PR to run in switchdev mode
111 * @pf: pointer to PF struct
112 * @repr: pointer to PR struct
114 static int ice_eswitch_setup_repr(struct ice_pf *pf, struct ice_repr *repr)
116 struct ice_vsi *uplink_vsi = pf->eswitch.uplink_vsi;
117 struct ice_vsi *vsi = repr->src_vsi;
118 struct metadata_dst *dst;
120 repr->dst = metadata_dst_alloc(0, METADATA_HW_PORT_MUX,
125 netif_keep_dst(uplink_vsi->netdev);
128 dst->u.port_info.port_id = vsi->vsi_num;
129 dst->u.port_info.lower_dev = uplink_vsi->netdev;
135 * ice_eswitch_cfg_vsi - configure VSI to work in slow-path
136 * @vsi: VSI structure of representee
137 * @mac: representee MAC
139 * Return: 0 on success, non-zero on error.
141 int ice_eswitch_cfg_vsi(struct ice_vsi *vsi, const u8 *mac)
145 ice_remove_vsi_fltr(&vsi->back->hw, vsi->idx);
147 err = ice_vsi_update_security(vsi, ice_vsi_ctx_clear_antispoof);
149 goto err_update_security;
151 err = ice_vsi_add_vlan_zero(vsi);
158 ice_vsi_update_security(vsi, ice_vsi_ctx_set_antispoof);
160 ice_fltr_add_mac_and_broadcast(vsi, mac, ICE_FWD_TO_VSI);
166 * ice_eswitch_decfg_vsi - unroll changes done to VSI for switchdev
167 * @vsi: VSI structure of representee
168 * @mac: representee MAC
170 void ice_eswitch_decfg_vsi(struct ice_vsi *vsi, const u8 *mac)
172 ice_vsi_update_security(vsi, ice_vsi_ctx_set_antispoof);
173 ice_fltr_add_mac_and_broadcast(vsi, mac, ICE_FWD_TO_VSI);
177 * ice_eswitch_update_repr - reconfigure port representor
178 * @repr_id: representor ID
179 * @vsi: VSI for which port representor is configured
181 void ice_eswitch_update_repr(unsigned long *repr_id, struct ice_vsi *vsi)
183 struct ice_pf *pf = vsi->back;
184 struct ice_repr *repr;
187 if (!ice_is_switchdev_running(pf))
190 repr = xa_load(&pf->eswitch.reprs, *repr_id);
195 repr->dst->u.port_info.port_id = vsi->vsi_num;
198 repr->br_port->vsi = vsi;
200 err = ice_eswitch_cfg_vsi(vsi, repr->parent_mac);
202 dev_err(ice_pf_to_dev(pf), "Failed to update VSI of port representor %d",
205 /* The VSI number is different, reload the PR with new id */
206 if (repr->id != vsi->vsi_num) {
207 xa_erase(&pf->eswitch.reprs, repr->id);
208 repr->id = vsi->vsi_num;
209 if (xa_insert(&pf->eswitch.reprs, repr->id, repr, GFP_KERNEL))
210 dev_err(ice_pf_to_dev(pf), "Failed to reload port representor %d",
217 * ice_eswitch_port_start_xmit - callback for packets transmit
219 * @netdev: network interface device structure
221 * Returns NETDEV_TX_OK if sent, else an error code
224 ice_eswitch_port_start_xmit(struct sk_buff *skb, struct net_device *netdev)
226 struct ice_repr *repr = ice_netdev_to_repr(netdev);
227 unsigned int len = skb->len;
231 dst_hold((struct dst_entry *)repr->dst);
232 skb_dst_set(skb, (struct dst_entry *)repr->dst);
233 skb->dev = repr->dst->u.port_info.lower_dev;
235 ret = dev_queue_xmit(skb);
236 ice_repr_inc_tx_stats(repr, len, ret);
242 * ice_eswitch_set_target_vsi - set eswitch context in Tx context descriptor
243 * @skb: pointer to send buffer
244 * @off: pointer to offload struct
247 ice_eswitch_set_target_vsi(struct sk_buff *skb,
248 struct ice_tx_offload_params *off)
250 struct metadata_dst *dst = skb_metadata_dst(skb);
254 cd_cmd = ICE_TX_CTX_DESC_SWTCH_UPLINK << ICE_TXD_CTX_QW1_CMD_S;
255 off->cd_qw1 |= (cd_cmd | ICE_TX_DESC_DTYPE_CTX);
257 cd_cmd = ICE_TX_CTX_DESC_SWTCH_VSI << ICE_TXD_CTX_QW1_CMD_S;
258 dst_vsi = FIELD_PREP(ICE_TXD_CTX_QW1_VSI_M,
259 dst->u.port_info.port_id);
260 off->cd_qw1 = cd_cmd | dst_vsi | ICE_TX_DESC_DTYPE_CTX;
265 * ice_eswitch_release_env - clear eswitch HW filters
266 * @pf: pointer to PF struct
268 * This function removes HW filters configuration specific for switchdev
269 * mode and restores default legacy mode settings.
271 static void ice_eswitch_release_env(struct ice_pf *pf)
273 struct ice_vsi *uplink_vsi = pf->eswitch.uplink_vsi;
274 struct ice_vsi_vlan_ops *vlan_ops;
276 vlan_ops = ice_get_compat_vsi_vlan_ops(uplink_vsi);
278 ice_vsi_update_local_lb(uplink_vsi, false);
279 ice_vsi_update_security(uplink_vsi, ice_vsi_ctx_clear_allow_override);
280 vlan_ops->ena_rx_filtering(uplink_vsi);
281 ice_cfg_dflt_vsi(uplink_vsi->port_info, uplink_vsi->idx, false,
283 ice_cfg_dflt_vsi(uplink_vsi->port_info, uplink_vsi->idx, false,
285 ice_fltr_add_mac_and_broadcast(uplink_vsi,
286 uplink_vsi->port_info->mac.perm_addr,
291 * ice_eswitch_enable_switchdev - configure eswitch in switchdev mode
292 * @pf: pointer to PF structure
294 static int ice_eswitch_enable_switchdev(struct ice_pf *pf)
296 struct ice_vsi *uplink_vsi;
298 uplink_vsi = ice_get_main_vsi(pf);
302 if (netif_is_any_bridge_port(uplink_vsi->netdev)) {
303 dev_err(ice_pf_to_dev(pf),
304 "Uplink port cannot be a bridge port\n");
308 pf->eswitch.uplink_vsi = uplink_vsi;
310 if (ice_eswitch_setup_env(pf))
313 if (ice_eswitch_br_offloads_init(pf))
314 goto err_br_offloads;
316 pf->eswitch.is_running = true;
321 ice_eswitch_release_env(pf);
326 * ice_eswitch_disable_switchdev - disable eswitch resources
327 * @pf: pointer to PF structure
329 static void ice_eswitch_disable_switchdev(struct ice_pf *pf)
331 ice_eswitch_br_offloads_deinit(pf);
332 ice_eswitch_release_env(pf);
334 pf->eswitch.is_running = false;
338 * ice_eswitch_mode_set - set new eswitch mode
339 * @devlink: pointer to devlink structure
340 * @mode: eswitch mode to switch to
341 * @extack: pointer to extack structure
344 ice_eswitch_mode_set(struct devlink *devlink, u16 mode,
345 struct netlink_ext_ack *extack)
347 struct ice_pf *pf = devlink_priv(devlink);
349 if (pf->eswitch_mode == mode)
352 if (ice_has_vfs(pf)) {
353 dev_info(ice_pf_to_dev(pf), "Changing eswitch mode is allowed only if there is no VFs created");
354 NL_SET_ERR_MSG_MOD(extack, "Changing eswitch mode is allowed only if there is no VFs created");
359 case DEVLINK_ESWITCH_MODE_LEGACY:
360 dev_info(ice_pf_to_dev(pf), "PF %d changed eswitch mode to legacy",
362 xa_destroy(&pf->eswitch.reprs);
363 NL_SET_ERR_MSG_MOD(extack, "Changed eswitch mode to legacy");
365 case DEVLINK_ESWITCH_MODE_SWITCHDEV:
367 if (ice_is_adq_active(pf)) {
368 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");
369 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");
373 dev_info(ice_pf_to_dev(pf), "PF %d changed eswitch mode to switchdev",
375 xa_init(&pf->eswitch.reprs);
376 NL_SET_ERR_MSG_MOD(extack, "Changed eswitch mode to switchdev");
380 NL_SET_ERR_MSG_MOD(extack, "Unknown eswitch mode");
384 pf->eswitch_mode = mode;
389 * ice_eswitch_mode_get - get current eswitch mode
390 * @devlink: pointer to devlink structure
391 * @mode: output parameter for current eswitch mode
393 int ice_eswitch_mode_get(struct devlink *devlink, u16 *mode)
395 struct ice_pf *pf = devlink_priv(devlink);
397 *mode = pf->eswitch_mode;
402 * ice_is_eswitch_mode_switchdev - check if eswitch mode is set to switchdev
403 * @pf: pointer to PF structure
405 * Returns true if eswitch mode is set to DEVLINK_ESWITCH_MODE_SWITCHDEV,
408 bool ice_is_eswitch_mode_switchdev(struct ice_pf *pf)
410 return pf->eswitch_mode == DEVLINK_ESWITCH_MODE_SWITCHDEV;
414 * ice_eswitch_start_all_tx_queues - start Tx queues of all port representors
415 * @pf: pointer to PF structure
417 static void ice_eswitch_start_all_tx_queues(struct ice_pf *pf)
419 struct ice_repr *repr;
422 if (test_bit(ICE_DOWN, pf->state))
425 xa_for_each(&pf->eswitch.reprs, id, repr)
426 ice_repr_start_tx_queues(repr);
430 * ice_eswitch_stop_all_tx_queues - stop Tx queues of all port representors
431 * @pf: pointer to PF structure
433 void ice_eswitch_stop_all_tx_queues(struct ice_pf *pf)
435 struct ice_repr *repr;
438 if (test_bit(ICE_DOWN, pf->state))
441 xa_for_each(&pf->eswitch.reprs, id, repr)
442 ice_repr_stop_tx_queues(repr);
445 static void ice_eswitch_stop_reprs(struct ice_pf *pf)
447 ice_eswitch_stop_all_tx_queues(pf);
450 static void ice_eswitch_start_reprs(struct ice_pf *pf)
452 ice_eswitch_start_all_tx_queues(pf);
456 ice_eswitch_attach(struct ice_pf *pf, struct ice_repr *repr, unsigned long *id)
460 if (pf->eswitch_mode == DEVLINK_ESWITCH_MODE_LEGACY)
463 if (xa_empty(&pf->eswitch.reprs)) {
464 err = ice_eswitch_enable_switchdev(pf);
469 ice_eswitch_stop_reprs(pf);
471 err = repr->ops.add(repr);
473 goto err_create_repr;
475 err = ice_eswitch_setup_repr(pf, repr);
479 err = xa_insert(&pf->eswitch.reprs, repr->id, repr, GFP_KERNEL);
485 ice_eswitch_start_reprs(pf);
490 ice_eswitch_release_repr(pf, repr);
494 if (xa_empty(&pf->eswitch.reprs))
495 ice_eswitch_disable_switchdev(pf);
496 ice_eswitch_start_reprs(pf);
502 * ice_eswitch_attach_vf - attach VF to a eswitch
503 * @pf: pointer to PF structure
504 * @vf: pointer to VF structure to be attached
506 * During attaching port representor for VF is created.
508 * Return: zero on success or an error code on failure.
510 int ice_eswitch_attach_vf(struct ice_pf *pf, struct ice_vf *vf)
512 struct ice_repr *repr = ice_repr_create_vf(vf);
513 struct devlink *devlink = priv_to_devlink(pf);
517 return PTR_ERR(repr);
520 err = ice_eswitch_attach(pf, repr, &vf->repr_id);
522 ice_repr_destroy(repr);
523 devl_unlock(devlink);
529 * ice_eswitch_attach_sf - attach SF to a eswitch
530 * @pf: pointer to PF structure
531 * @sf: pointer to SF structure to be attached
533 * During attaching port representor for SF is created.
535 * Return: zero on success or an error code on failure.
537 int ice_eswitch_attach_sf(struct ice_pf *pf, struct ice_dynamic_port *sf)
539 struct ice_repr *repr = ice_repr_create_sf(sf);
543 return PTR_ERR(repr);
545 err = ice_eswitch_attach(pf, repr, &sf->repr_id);
547 ice_repr_destroy(repr);
552 static void ice_eswitch_detach(struct ice_pf *pf, struct ice_repr *repr)
554 ice_eswitch_stop_reprs(pf);
557 xa_erase(&pf->eswitch.reprs, repr->id);
559 if (xa_empty(&pf->eswitch.reprs))
560 ice_eswitch_disable_switchdev(pf);
562 ice_eswitch_release_repr(pf, repr);
563 ice_repr_destroy(repr);
565 if (xa_empty(&pf->eswitch.reprs)) {
566 struct devlink *devlink = priv_to_devlink(pf);
568 /* since all port representors are destroyed, there is
569 * no point in keeping the nodes
571 ice_devlink_rate_clear_tx_topology(ice_get_main_vsi(pf));
572 devl_rate_nodes_destroy(devlink);
574 ice_eswitch_start_reprs(pf);
579 * ice_eswitch_detach_vf - detach VF from a eswitch
580 * @pf: pointer to PF structure
581 * @vf: pointer to VF structure to be detached
583 void ice_eswitch_detach_vf(struct ice_pf *pf, struct ice_vf *vf)
585 struct ice_repr *repr = xa_load(&pf->eswitch.reprs, vf->repr_id);
586 struct devlink *devlink = priv_to_devlink(pf);
592 ice_eswitch_detach(pf, repr);
593 devl_unlock(devlink);
597 * ice_eswitch_detach_sf - detach SF from a eswitch
598 * @pf: pointer to PF structure
599 * @sf: pointer to SF structure to be detached
601 void ice_eswitch_detach_sf(struct ice_pf *pf, struct ice_dynamic_port *sf)
603 struct ice_repr *repr = xa_load(&pf->eswitch.reprs, sf->repr_id);
608 ice_eswitch_detach(pf, repr);
612 * ice_eswitch_get_target - get netdev based on src_vsi from descriptor
613 * @rx_ring: ring used to receive the packet
614 * @rx_desc: descriptor used to get src_vsi value
616 * Get src_vsi value from descriptor and load correct representor. If it isn't
617 * found return rx_ring->netdev.
619 struct net_device *ice_eswitch_get_target(struct ice_rx_ring *rx_ring,
620 union ice_32b_rx_flex_desc *rx_desc)
622 struct ice_eswitch *eswitch = &rx_ring->vsi->back->eswitch;
623 struct ice_32b_rx_flex_desc_nic_2 *desc;
624 struct ice_repr *repr;
626 desc = (struct ice_32b_rx_flex_desc_nic_2 *)rx_desc;
627 repr = xa_load(&eswitch->reprs, le16_to_cpu(desc->src_vsi));
629 return rx_ring->netdev;