Loading...
1// SPDX-License-Identifier: GPL-2.0
2/* Copyright (C) 2019-2021, Intel Corporation. */
3
4#include "ice.h"
5#include "ice_lib.h"
6#include "ice_eswitch.h"
7#include "ice_eswitch_br.h"
8#include "ice_fltr.h"
9#include "ice_repr.h"
10#include "ice_devlink.h"
11#include "ice_tc_lib.h"
12
13/**
14 * ice_eswitch_del_sp_rules - delete adv rules added on PRs
15 * @pf: pointer to the PF struct
16 *
17 * Delete all advanced rules that were used to forward packets with the
18 * device's VSI index to the corresponding eswitch ctrl VSI queue.
19 */
20static void ice_eswitch_del_sp_rules(struct ice_pf *pf)
21{
22 struct ice_repr *repr;
23 unsigned long id;
24
25 xa_for_each(&pf->eswitch.reprs, id, repr) {
26 if (repr->sp_rule.rid)
27 ice_rem_adv_rule_by_id(&pf->hw, &repr->sp_rule);
28 }
29}
30
31/**
32 * ice_eswitch_add_sp_rule - add adv rule with device's VSI index
33 * @pf: pointer to PF struct
34 * @repr: pointer to the repr struct
35 *
36 * This function adds advanced rule that forwards packets with
37 * device's VSI index to the corresponding eswitch ctrl VSI queue.
38 */
39static int ice_eswitch_add_sp_rule(struct ice_pf *pf, struct ice_repr *repr)
40{
41 struct ice_vsi *ctrl_vsi = pf->eswitch.control_vsi;
42 struct ice_adv_rule_info rule_info = { 0 };
43 struct ice_adv_lkup_elem *list;
44 struct ice_hw *hw = &pf->hw;
45 const u16 lkups_cnt = 1;
46 int err;
47
48 list = kcalloc(lkups_cnt, sizeof(*list), GFP_ATOMIC);
49 if (!list)
50 return -ENOMEM;
51
52 ice_rule_add_src_vsi_metadata(list);
53
54 rule_info.sw_act.flag = ICE_FLTR_TX;
55 rule_info.sw_act.vsi_handle = ctrl_vsi->idx;
56 rule_info.sw_act.fltr_act = ICE_FWD_TO_Q;
57 rule_info.sw_act.fwd_id.q_id = hw->func_caps.common_cap.rxq_first_id +
58 ctrl_vsi->rxq_map[repr->q_id];
59 rule_info.flags_info.act |= ICE_SINGLE_ACT_LB_ENABLE;
60 rule_info.flags_info.act_valid = true;
61 rule_info.tun_type = ICE_SW_TUN_AND_NON_TUN;
62 rule_info.src_vsi = repr->src_vsi->idx;
63
64 err = ice_add_adv_rule(hw, list, lkups_cnt, &rule_info,
65 &repr->sp_rule);
66 if (err)
67 dev_err(ice_pf_to_dev(pf), "Unable to add slow-path rule for eswitch for PR %d",
68 repr->id);
69
70 kfree(list);
71 return err;
72}
73
74static int
75ice_eswitch_add_sp_rules(struct ice_pf *pf)
76{
77 struct ice_repr *repr;
78 unsigned long id;
79 int err;
80
81 xa_for_each(&pf->eswitch.reprs, id, repr) {
82 err = ice_eswitch_add_sp_rule(pf, repr);
83 if (err) {
84 ice_eswitch_del_sp_rules(pf);
85 return err;
86 }
87 }
88
89 return 0;
90}
91
92/**
93 * ice_eswitch_setup_env - configure eswitch HW filters
94 * @pf: pointer to PF struct
95 *
96 * This function adds HW filters configuration specific for switchdev
97 * mode.
98 */
99static int ice_eswitch_setup_env(struct ice_pf *pf)
100{
101 struct ice_vsi *uplink_vsi = pf->eswitch.uplink_vsi;
102 struct ice_vsi *ctrl_vsi = pf->eswitch.control_vsi;
103 struct net_device *netdev = uplink_vsi->netdev;
104 struct ice_vsi_vlan_ops *vlan_ops;
105 bool rule_added = false;
106
107 ice_remove_vsi_fltr(&pf->hw, uplink_vsi->idx);
108
109 netif_addr_lock_bh(netdev);
110 __dev_uc_unsync(netdev, NULL);
111 __dev_mc_unsync(netdev, NULL);
112 netif_addr_unlock_bh(netdev);
113
114 if (ice_vsi_add_vlan_zero(uplink_vsi))
115 goto err_def_rx;
116
117 if (!ice_is_dflt_vsi_in_use(uplink_vsi->port_info)) {
118 if (ice_set_dflt_vsi(uplink_vsi))
119 goto err_def_rx;
120 rule_added = true;
121 }
122
123 vlan_ops = ice_get_compat_vsi_vlan_ops(uplink_vsi);
124 if (vlan_ops->dis_rx_filtering(uplink_vsi))
125 goto err_dis_rx;
126
127 if (ice_vsi_update_security(uplink_vsi, ice_vsi_ctx_set_allow_override))
128 goto err_override_uplink;
129
130 if (ice_vsi_update_security(ctrl_vsi, ice_vsi_ctx_set_allow_override))
131 goto err_override_control;
132
133 if (ice_vsi_update_local_lb(uplink_vsi, true))
134 goto err_override_local_lb;
135
136 return 0;
137
138err_override_local_lb:
139 ice_vsi_update_security(ctrl_vsi, ice_vsi_ctx_clear_allow_override);
140err_override_control:
141 ice_vsi_update_security(uplink_vsi, ice_vsi_ctx_clear_allow_override);
142err_override_uplink:
143 vlan_ops->ena_rx_filtering(uplink_vsi);
144err_dis_rx:
145 if (rule_added)
146 ice_clear_dflt_vsi(uplink_vsi);
147err_def_rx:
148 ice_fltr_add_mac_and_broadcast(uplink_vsi,
149 uplink_vsi->port_info->mac.perm_addr,
150 ICE_FWD_TO_VSI);
151 return -ENODEV;
152}
153
154/**
155 * ice_eswitch_remap_rings_to_vectors - reconfigure rings of eswitch ctrl VSI
156 * @eswitch: pointer to eswitch struct
157 *
158 * In eswitch number of allocated Tx/Rx rings is equal.
159 *
160 * This function fills q_vectors structures associated with representor and
161 * move each ring pairs to port representor netdevs. Each port representor
162 * will have dedicated 1 Tx/Rx ring pair, so number of rings pair is equal to
163 * number of VFs.
164 */
165static void ice_eswitch_remap_rings_to_vectors(struct ice_eswitch *eswitch)
166{
167 struct ice_vsi *vsi = eswitch->control_vsi;
168 unsigned long repr_id = 0;
169 int q_id;
170
171 ice_for_each_txq(vsi, q_id) {
172 struct ice_q_vector *q_vector;
173 struct ice_tx_ring *tx_ring;
174 struct ice_rx_ring *rx_ring;
175 struct ice_repr *repr;
176
177 repr = xa_find(&eswitch->reprs, &repr_id, U32_MAX,
178 XA_PRESENT);
179 if (!repr)
180 break;
181
182 repr_id += 1;
183 repr->q_id = q_id;
184 q_vector = repr->q_vector;
185 tx_ring = vsi->tx_rings[q_id];
186 rx_ring = vsi->rx_rings[q_id];
187
188 q_vector->vsi = vsi;
189 q_vector->reg_idx = vsi->q_vectors[0]->reg_idx;
190
191 q_vector->num_ring_tx = 1;
192 q_vector->tx.tx_ring = tx_ring;
193 tx_ring->q_vector = q_vector;
194 tx_ring->next = NULL;
195 tx_ring->netdev = repr->netdev;
196 /* In switchdev mode, from OS stack perspective, there is only
197 * one queue for given netdev, so it needs to be indexed as 0.
198 */
199 tx_ring->q_index = 0;
200
201 q_vector->num_ring_rx = 1;
202 q_vector->rx.rx_ring = rx_ring;
203 rx_ring->q_vector = q_vector;
204 rx_ring->next = NULL;
205 rx_ring->netdev = repr->netdev;
206 }
207}
208
209/**
210 * ice_eswitch_release_repr - clear PR VSI configuration
211 * @pf: poiner to PF struct
212 * @repr: pointer to PR
213 */
214static void
215ice_eswitch_release_repr(struct ice_pf *pf, struct ice_repr *repr)
216{
217 struct ice_vsi *vsi = repr->src_vsi;
218
219 /* Skip representors that aren't configured */
220 if (!repr->dst)
221 return;
222
223 ice_vsi_update_security(vsi, ice_vsi_ctx_set_antispoof);
224 metadata_dst_free(repr->dst);
225 repr->dst = NULL;
226 ice_fltr_add_mac_and_broadcast(vsi, repr->parent_mac,
227 ICE_FWD_TO_VSI);
228
229 netif_napi_del(&repr->q_vector->napi);
230}
231
232/**
233 * ice_eswitch_setup_repr - configure PR to run in switchdev mode
234 * @pf: pointer to PF struct
235 * @repr: pointer to PR struct
236 */
237static int ice_eswitch_setup_repr(struct ice_pf *pf, struct ice_repr *repr)
238{
239 struct ice_vsi *ctrl_vsi = pf->eswitch.control_vsi;
240 struct ice_vsi *vsi = repr->src_vsi;
241 struct metadata_dst *dst;
242
243 ice_remove_vsi_fltr(&pf->hw, vsi->idx);
244 repr->dst = metadata_dst_alloc(0, METADATA_HW_PORT_MUX,
245 GFP_KERNEL);
246 if (!repr->dst)
247 goto err_add_mac_fltr;
248
249 if (ice_vsi_update_security(vsi, ice_vsi_ctx_clear_antispoof))
250 goto err_dst_free;
251
252 if (ice_vsi_add_vlan_zero(vsi))
253 goto err_update_security;
254
255 netif_napi_add(repr->netdev, &repr->q_vector->napi,
256 ice_napi_poll);
257
258 netif_keep_dst(repr->netdev);
259
260 dst = repr->dst;
261 dst->u.port_info.port_id = vsi->vsi_num;
262 dst->u.port_info.lower_dev = repr->netdev;
263 ice_repr_set_traffic_vsi(repr, ctrl_vsi);
264
265 return 0;
266
267err_update_security:
268 ice_vsi_update_security(vsi, ice_vsi_ctx_set_antispoof);
269err_dst_free:
270 metadata_dst_free(repr->dst);
271 repr->dst = NULL;
272err_add_mac_fltr:
273 ice_fltr_add_mac_and_broadcast(vsi, repr->parent_mac, ICE_FWD_TO_VSI);
274
275 return -ENODEV;
276}
277
278/**
279 * ice_eswitch_update_repr - reconfigure port representor
280 * @repr_id: representor ID
281 * @vsi: VSI for which port representor is configured
282 */
283void ice_eswitch_update_repr(unsigned long repr_id, struct ice_vsi *vsi)
284{
285 struct ice_pf *pf = vsi->back;
286 struct ice_repr *repr;
287 int ret;
288
289 if (!ice_is_switchdev_running(pf))
290 return;
291
292 repr = xa_load(&pf->eswitch.reprs, repr_id);
293 if (!repr)
294 return;
295
296 repr->src_vsi = vsi;
297 repr->dst->u.port_info.port_id = vsi->vsi_num;
298
299 if (repr->br_port)
300 repr->br_port->vsi = vsi;
301
302 ret = ice_vsi_update_security(vsi, ice_vsi_ctx_clear_antispoof);
303 if (ret) {
304 ice_fltr_add_mac_and_broadcast(vsi, repr->parent_mac,
305 ICE_FWD_TO_VSI);
306 dev_err(ice_pf_to_dev(pf), "Failed to update VSI of port representor %d",
307 repr->id);
308 }
309}
310
311/**
312 * ice_eswitch_port_start_xmit - callback for packets transmit
313 * @skb: send buffer
314 * @netdev: network interface device structure
315 *
316 * Returns NETDEV_TX_OK if sent, else an error code
317 */
318netdev_tx_t
319ice_eswitch_port_start_xmit(struct sk_buff *skb, struct net_device *netdev)
320{
321 struct ice_netdev_priv *np;
322 struct ice_repr *repr;
323 struct ice_vsi *vsi;
324
325 np = netdev_priv(netdev);
326 vsi = np->vsi;
327
328 if (!vsi || !ice_is_switchdev_running(vsi->back))
329 return NETDEV_TX_BUSY;
330
331 if (ice_is_reset_in_progress(vsi->back->state) ||
332 test_bit(ICE_VF_DIS, vsi->back->state))
333 return NETDEV_TX_BUSY;
334
335 repr = ice_netdev_to_repr(netdev);
336 skb_dst_drop(skb);
337 dst_hold((struct dst_entry *)repr->dst);
338 skb_dst_set(skb, (struct dst_entry *)repr->dst);
339 skb->queue_mapping = repr->q_id;
340
341 return ice_start_xmit(skb, netdev);
342}
343
344/**
345 * ice_eswitch_set_target_vsi - set eswitch context in Tx context descriptor
346 * @skb: pointer to send buffer
347 * @off: pointer to offload struct
348 */
349void
350ice_eswitch_set_target_vsi(struct sk_buff *skb,
351 struct ice_tx_offload_params *off)
352{
353 struct metadata_dst *dst = skb_metadata_dst(skb);
354 u64 cd_cmd, dst_vsi;
355
356 if (!dst) {
357 cd_cmd = ICE_TX_CTX_DESC_SWTCH_UPLINK << ICE_TXD_CTX_QW1_CMD_S;
358 off->cd_qw1 |= (cd_cmd | ICE_TX_DESC_DTYPE_CTX);
359 } else {
360 cd_cmd = ICE_TX_CTX_DESC_SWTCH_VSI << ICE_TXD_CTX_QW1_CMD_S;
361 dst_vsi = FIELD_PREP(ICE_TXD_CTX_QW1_VSI_M,
362 dst->u.port_info.port_id);
363 off->cd_qw1 = cd_cmd | dst_vsi | ICE_TX_DESC_DTYPE_CTX;
364 }
365}
366
367/**
368 * ice_eswitch_release_env - clear eswitch HW filters
369 * @pf: pointer to PF struct
370 *
371 * This function removes HW filters configuration specific for switchdev
372 * mode and restores default legacy mode settings.
373 */
374static void ice_eswitch_release_env(struct ice_pf *pf)
375{
376 struct ice_vsi *uplink_vsi = pf->eswitch.uplink_vsi;
377 struct ice_vsi *ctrl_vsi = pf->eswitch.control_vsi;
378 struct ice_vsi_vlan_ops *vlan_ops;
379
380 vlan_ops = ice_get_compat_vsi_vlan_ops(uplink_vsi);
381
382 ice_vsi_update_local_lb(uplink_vsi, false);
383 ice_vsi_update_security(ctrl_vsi, ice_vsi_ctx_clear_allow_override);
384 ice_vsi_update_security(uplink_vsi, ice_vsi_ctx_clear_allow_override);
385 vlan_ops->ena_rx_filtering(uplink_vsi);
386 ice_clear_dflt_vsi(uplink_vsi);
387 ice_fltr_add_mac_and_broadcast(uplink_vsi,
388 uplink_vsi->port_info->mac.perm_addr,
389 ICE_FWD_TO_VSI);
390}
391
392/**
393 * ice_eswitch_vsi_setup - configure eswitch control VSI
394 * @pf: pointer to PF structure
395 * @pi: pointer to port_info structure
396 */
397static struct ice_vsi *
398ice_eswitch_vsi_setup(struct ice_pf *pf, struct ice_port_info *pi)
399{
400 struct ice_vsi_cfg_params params = {};
401
402 params.type = ICE_VSI_SWITCHDEV_CTRL;
403 params.pi = pi;
404 params.flags = ICE_VSI_FLAG_INIT;
405
406 return ice_vsi_setup(pf, ¶ms);
407}
408
409/**
410 * ice_eswitch_napi_enable - enable NAPI for all port representors
411 * @reprs: xarray of reprs
412 */
413static void ice_eswitch_napi_enable(struct xarray *reprs)
414{
415 struct ice_repr *repr;
416 unsigned long id;
417
418 xa_for_each(reprs, id, repr)
419 napi_enable(&repr->q_vector->napi);
420}
421
422/**
423 * ice_eswitch_napi_disable - disable NAPI for all port representors
424 * @reprs: xarray of reprs
425 */
426static void ice_eswitch_napi_disable(struct xarray *reprs)
427{
428 struct ice_repr *repr;
429 unsigned long id;
430
431 xa_for_each(reprs, id, repr)
432 napi_disable(&repr->q_vector->napi);
433}
434
435/**
436 * ice_eswitch_enable_switchdev - configure eswitch in switchdev mode
437 * @pf: pointer to PF structure
438 */
439static int ice_eswitch_enable_switchdev(struct ice_pf *pf)
440{
441 struct ice_vsi *ctrl_vsi, *uplink_vsi;
442
443 uplink_vsi = ice_get_main_vsi(pf);
444 if (!uplink_vsi)
445 return -ENODEV;
446
447 if (netif_is_any_bridge_port(uplink_vsi->netdev)) {
448 dev_err(ice_pf_to_dev(pf),
449 "Uplink port cannot be a bridge port\n");
450 return -EINVAL;
451 }
452
453 pf->eswitch.control_vsi = ice_eswitch_vsi_setup(pf, pf->hw.port_info);
454 if (!pf->eswitch.control_vsi)
455 return -ENODEV;
456
457 ctrl_vsi = pf->eswitch.control_vsi;
458 /* cp VSI is createad with 1 queue as default */
459 pf->eswitch.qs.value = 1;
460 pf->eswitch.uplink_vsi = uplink_vsi;
461
462 if (ice_eswitch_setup_env(pf))
463 goto err_vsi;
464
465 if (ice_eswitch_br_offloads_init(pf))
466 goto err_br_offloads;
467
468 pf->eswitch.is_running = true;
469
470 return 0;
471
472err_br_offloads:
473 ice_eswitch_release_env(pf);
474err_vsi:
475 ice_vsi_release(ctrl_vsi);
476 return -ENODEV;
477}
478
479/**
480 * ice_eswitch_disable_switchdev - disable eswitch resources
481 * @pf: pointer to PF structure
482 */
483static void ice_eswitch_disable_switchdev(struct ice_pf *pf)
484{
485 struct ice_vsi *ctrl_vsi = pf->eswitch.control_vsi;
486
487 ice_eswitch_br_offloads_deinit(pf);
488 ice_eswitch_release_env(pf);
489 ice_vsi_release(ctrl_vsi);
490
491 pf->eswitch.is_running = false;
492 pf->eswitch.qs.is_reaching = false;
493}
494
495/**
496 * ice_eswitch_mode_set - set new eswitch mode
497 * @devlink: pointer to devlink structure
498 * @mode: eswitch mode to switch to
499 * @extack: pointer to extack structure
500 */
501int
502ice_eswitch_mode_set(struct devlink *devlink, u16 mode,
503 struct netlink_ext_ack *extack)
504{
505 struct ice_pf *pf = devlink_priv(devlink);
506
507 if (pf->eswitch_mode == mode)
508 return 0;
509
510 if (ice_has_vfs(pf)) {
511 dev_info(ice_pf_to_dev(pf), "Changing eswitch mode is allowed only if there is no VFs created");
512 NL_SET_ERR_MSG_MOD(extack, "Changing eswitch mode is allowed only if there is no VFs created");
513 return -EOPNOTSUPP;
514 }
515
516 switch (mode) {
517 case DEVLINK_ESWITCH_MODE_LEGACY:
518 dev_info(ice_pf_to_dev(pf), "PF %d changed eswitch mode to legacy",
519 pf->hw.pf_id);
520 xa_destroy(&pf->eswitch.reprs);
521 NL_SET_ERR_MSG_MOD(extack, "Changed eswitch mode to legacy");
522 break;
523 case DEVLINK_ESWITCH_MODE_SWITCHDEV:
524 {
525 if (ice_is_adq_active(pf)) {
526 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");
527 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");
528 return -EOPNOTSUPP;
529 }
530
531 dev_info(ice_pf_to_dev(pf), "PF %d changed eswitch mode to switchdev",
532 pf->hw.pf_id);
533 xa_init_flags(&pf->eswitch.reprs, XA_FLAGS_ALLOC);
534 NL_SET_ERR_MSG_MOD(extack, "Changed eswitch mode to switchdev");
535 break;
536 }
537 default:
538 NL_SET_ERR_MSG_MOD(extack, "Unknown eswitch mode");
539 return -EINVAL;
540 }
541
542 pf->eswitch_mode = mode;
543 return 0;
544}
545
546/**
547 * ice_eswitch_mode_get - get current eswitch mode
548 * @devlink: pointer to devlink structure
549 * @mode: output parameter for current eswitch mode
550 */
551int ice_eswitch_mode_get(struct devlink *devlink, u16 *mode)
552{
553 struct ice_pf *pf = devlink_priv(devlink);
554
555 *mode = pf->eswitch_mode;
556 return 0;
557}
558
559/**
560 * ice_is_eswitch_mode_switchdev - check if eswitch mode is set to switchdev
561 * @pf: pointer to PF structure
562 *
563 * Returns true if eswitch mode is set to DEVLINK_ESWITCH_MODE_SWITCHDEV,
564 * false otherwise.
565 */
566bool ice_is_eswitch_mode_switchdev(struct ice_pf *pf)
567{
568 return pf->eswitch_mode == DEVLINK_ESWITCH_MODE_SWITCHDEV;
569}
570
571/**
572 * ice_eswitch_start_all_tx_queues - start Tx queues of all port representors
573 * @pf: pointer to PF structure
574 */
575static void ice_eswitch_start_all_tx_queues(struct ice_pf *pf)
576{
577 struct ice_repr *repr;
578 unsigned long id;
579
580 if (test_bit(ICE_DOWN, pf->state))
581 return;
582
583 xa_for_each(&pf->eswitch.reprs, id, repr)
584 ice_repr_start_tx_queues(repr);
585}
586
587/**
588 * ice_eswitch_stop_all_tx_queues - stop Tx queues of all port representors
589 * @pf: pointer to PF structure
590 */
591void ice_eswitch_stop_all_tx_queues(struct ice_pf *pf)
592{
593 struct ice_repr *repr;
594 unsigned long id;
595
596 if (test_bit(ICE_DOWN, pf->state))
597 return;
598
599 xa_for_each(&pf->eswitch.reprs, id, repr)
600 ice_repr_stop_tx_queues(repr);
601}
602
603static void ice_eswitch_stop_reprs(struct ice_pf *pf)
604{
605 ice_eswitch_del_sp_rules(pf);
606 ice_eswitch_stop_all_tx_queues(pf);
607 ice_eswitch_napi_disable(&pf->eswitch.reprs);
608}
609
610static void ice_eswitch_start_reprs(struct ice_pf *pf)
611{
612 ice_eswitch_napi_enable(&pf->eswitch.reprs);
613 ice_eswitch_start_all_tx_queues(pf);
614 ice_eswitch_add_sp_rules(pf);
615}
616
617static void
618ice_eswitch_cp_change_queues(struct ice_eswitch *eswitch, int change)
619{
620 struct ice_vsi *cp = eswitch->control_vsi;
621 int queues = 0;
622
623 if (eswitch->qs.is_reaching) {
624 if (eswitch->qs.to_reach >= eswitch->qs.value + change) {
625 queues = eswitch->qs.to_reach;
626 eswitch->qs.is_reaching = false;
627 } else {
628 queues = 0;
629 }
630 } else if ((change > 0 && cp->alloc_txq <= eswitch->qs.value) ||
631 change < 0) {
632 queues = cp->alloc_txq + change;
633 }
634
635 if (queues) {
636 cp->req_txq = queues;
637 cp->req_rxq = queues;
638 ice_vsi_close(cp);
639 ice_vsi_rebuild(cp, ICE_VSI_FLAG_NO_INIT);
640 ice_vsi_open(cp);
641 } else if (!change) {
642 /* change == 0 means that VSI wasn't open, open it here */
643 ice_vsi_open(cp);
644 }
645
646 eswitch->qs.value += change;
647 ice_eswitch_remap_rings_to_vectors(eswitch);
648}
649
650int
651ice_eswitch_attach(struct ice_pf *pf, struct ice_vf *vf)
652{
653 struct ice_repr *repr;
654 int change = 1;
655 int err;
656
657 if (pf->eswitch_mode == DEVLINK_ESWITCH_MODE_LEGACY)
658 return 0;
659
660 if (xa_empty(&pf->eswitch.reprs)) {
661 err = ice_eswitch_enable_switchdev(pf);
662 if (err)
663 return err;
664 /* Control plane VSI is created with 1 queue as default */
665 pf->eswitch.qs.to_reach -= 1;
666 change = 0;
667 }
668
669 ice_eswitch_stop_reprs(pf);
670
671 repr = ice_repr_add_vf(vf);
672 if (IS_ERR(repr)) {
673 err = PTR_ERR(repr);
674 goto err_create_repr;
675 }
676
677 err = ice_eswitch_setup_repr(pf, repr);
678 if (err)
679 goto err_setup_repr;
680
681 err = xa_alloc(&pf->eswitch.reprs, &repr->id, repr,
682 XA_LIMIT(1, INT_MAX), GFP_KERNEL);
683 if (err)
684 goto err_xa_alloc;
685
686 vf->repr_id = repr->id;
687
688 ice_eswitch_cp_change_queues(&pf->eswitch, change);
689 ice_eswitch_start_reprs(pf);
690
691 return 0;
692
693err_xa_alloc:
694 ice_eswitch_release_repr(pf, repr);
695err_setup_repr:
696 ice_repr_rem_vf(repr);
697err_create_repr:
698 if (xa_empty(&pf->eswitch.reprs))
699 ice_eswitch_disable_switchdev(pf);
700 ice_eswitch_start_reprs(pf);
701
702 return err;
703}
704
705void ice_eswitch_detach(struct ice_pf *pf, struct ice_vf *vf)
706{
707 struct ice_repr *repr = xa_load(&pf->eswitch.reprs, vf->repr_id);
708 struct devlink *devlink = priv_to_devlink(pf);
709
710 if (!repr)
711 return;
712
713 ice_eswitch_stop_reprs(pf);
714 xa_erase(&pf->eswitch.reprs, repr->id);
715
716 if (xa_empty(&pf->eswitch.reprs))
717 ice_eswitch_disable_switchdev(pf);
718 else
719 ice_eswitch_cp_change_queues(&pf->eswitch, -1);
720
721 ice_eswitch_release_repr(pf, repr);
722 ice_repr_rem_vf(repr);
723
724 if (xa_empty(&pf->eswitch.reprs)) {
725 /* since all port representors are destroyed, there is
726 * no point in keeping the nodes
727 */
728 ice_devlink_rate_clear_tx_topology(ice_get_main_vsi(pf));
729 devl_lock(devlink);
730 devl_rate_nodes_destroy(devlink);
731 devl_unlock(devlink);
732 } else {
733 ice_eswitch_start_reprs(pf);
734 }
735}
736
737/**
738 * ice_eswitch_rebuild - rebuild eswitch
739 * @pf: pointer to PF structure
740 */
741int ice_eswitch_rebuild(struct ice_pf *pf)
742{
743 struct ice_repr *repr;
744 unsigned long id;
745 int err;
746
747 if (!ice_is_switchdev_running(pf))
748 return 0;
749
750 err = ice_vsi_rebuild(pf->eswitch.control_vsi, ICE_VSI_FLAG_INIT);
751 if (err)
752 return err;
753
754 xa_for_each(&pf->eswitch.reprs, id, repr)
755 ice_eswitch_detach(pf, repr->vf);
756
757 return 0;
758}
759
760/**
761 * ice_eswitch_reserve_cp_queues - reserve control plane VSI queues
762 * @pf: pointer to PF structure
763 * @change: how many more (or less) queues is needed
764 *
765 * Remember to call ice_eswitch_attach/detach() the "change" times.
766 */
767void ice_eswitch_reserve_cp_queues(struct ice_pf *pf, int change)
768{
769 if (pf->eswitch.qs.value + change < 0)
770 return;
771
772 pf->eswitch.qs.to_reach = pf->eswitch.qs.value + change;
773 pf->eswitch.qs.is_reaching = true;
774}
1// SPDX-License-Identifier: GPL-2.0
2/* Copyright (C) 2019-2021, Intel Corporation. */
3
4#include "ice.h"
5#include "ice_lib.h"
6#include "ice_eswitch.h"
7#include "ice_fltr.h"
8#include "ice_repr.h"
9#include "ice_devlink.h"
10#include "ice_tc_lib.h"
11
12/**
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
17 *
18 * This function adds advanced rule that forwards packets with
19 * VF's MAC address (src MAC) to the corresponding switchdev ctrl VSI queue.
20 */
21int
22ice_eswitch_add_vf_mac_rule(struct ice_pf *pf, struct ice_vf *vf, const u8 *mac)
23{
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;
29 int err;
30
31 list = kcalloc(lkups_cnt, sizeof(*list), GFP_ATOMIC);
32 if (!list)
33 return -ENOMEM;
34
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);
38
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;
42 rule_info.rx = false;
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;
48
49 err = ice_add_adv_rule(hw, list, lkups_cnt, &rule_info,
50 vf->repr->mac_rule);
51 if (err)
52 dev_err(ice_pf_to_dev(pf), "Unable to add VF mac rule in switchdev mode for VF %d",
53 vf->vf_id);
54 else
55 vf->repr->rule_added = true;
56
57 kfree(list);
58 return err;
59}
60
61/**
62 * ice_eswitch_replay_vf_mac_rule - replay adv rule with VF's MAC
63 * @vf: pointer to vF struct
64 *
65 * This function replays VF's MAC rule after reset.
66 */
67void ice_eswitch_replay_vf_mac_rule(struct ice_vf *vf)
68{
69 int err;
70
71 if (!ice_is_switchdev_running(vf->pf))
72 return;
73
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);
77 if (err) {
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);
80 return;
81 }
82 vf->num_mac++;
83
84 ether_addr_copy(vf->dev_lan_addr.addr, vf->hw_lan_addr.addr);
85 }
86}
87
88/**
89 * ice_eswitch_del_vf_mac_rule - delete adv rule with VF's MAC
90 * @vf: pointer to the VF struct
91 *
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.
94 */
95void ice_eswitch_del_vf_mac_rule(struct ice_vf *vf)
96{
97 if (!ice_is_switchdev_running(vf->pf))
98 return;
99
100 if (!vf->repr->rule_added)
101 return;
102
103 ice_rem_adv_rule_by_id(&vf->pf->hw, vf->repr->mac_rule);
104 vf->repr->rule_added = false;
105}
106
107/**
108 * ice_eswitch_setup_env - configure switchdev HW filters
109 * @pf: pointer to PF struct
110 *
111 * This function adds HW filters configuration specific for switchdev
112 * mode.
113 */
114static int ice_eswitch_setup_env(struct ice_pf *pf)
115{
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;
121
122 vlan_ops = ice_get_compat_vsi_vlan_ops(ctrl_vsi);
123 if (vlan_ops->dis_stripping(ctrl_vsi))
124 return -ENODEV;
125
126 ice_remove_vsi_fltr(&pf->hw, uplink_vsi->idx);
127
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);
132
133 if (ice_vsi_add_vlan_zero(uplink_vsi))
134 goto err_def_rx;
135
136 if (!ice_is_dflt_vsi_in_use(uplink_vsi->port_info)) {
137 if (ice_set_dflt_vsi(uplink_vsi))
138 goto err_def_rx;
139 rule_added = true;
140 }
141
142 if (ice_vsi_update_security(uplink_vsi, ice_vsi_ctx_set_allow_override))
143 goto err_override_uplink;
144
145 if (ice_vsi_update_security(ctrl_vsi, ice_vsi_ctx_set_allow_override))
146 goto err_override_control;
147
148 return 0;
149
150err_override_control:
151 ice_vsi_update_security(uplink_vsi, ice_vsi_ctx_clear_allow_override);
152err_override_uplink:
153 if (rule_added)
154 ice_clear_dflt_vsi(uplink_vsi);
155err_def_rx:
156 ice_fltr_add_mac_and_broadcast(uplink_vsi,
157 uplink_vsi->port_info->mac.perm_addr,
158 ICE_FWD_TO_VSI);
159 return -ENODEV;
160}
161
162/**
163 * ice_eswitch_remap_rings_to_vectors - reconfigure rings of switchdev ctrl VSI
164 * @pf: pointer to PF struct
165 *
166 * In switchdev number of allocated Tx/Rx rings is equal.
167 *
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
171 * number of VFs.
172 */
173static void ice_eswitch_remap_rings_to_vectors(struct ice_pf *pf)
174{
175 struct ice_vsi *vsi = pf->switchdev.control_vsi;
176 int q_id;
177
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;
183 struct ice_vf *vf;
184
185 vf = ice_get_vf_by_id(pf, q_id);
186 if (WARN_ON(!vf))
187 continue;
188
189 repr = vf->repr;
190 q_vector = repr->q_vector;
191 tx_ring = vsi->tx_rings[q_id];
192 rx_ring = vsi->rx_rings[q_id];
193
194 q_vector->vsi = vsi;
195 q_vector->reg_idx = vsi->q_vectors[0]->reg_idx;
196
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.
204 */
205 tx_ring->q_index = 0;
206
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;
212
213 ice_put_vf(vf);
214 }
215}
216
217/**
218 * ice_eswitch_release_reprs - clear PR VSIs configuration
219 * @pf: poiner to PF struct
220 * @ctrl_vsi: pointer to switchdev control VSI
221 */
222static void
223ice_eswitch_release_reprs(struct ice_pf *pf, struct ice_vsi *ctrl_vsi)
224{
225 struct ice_vf *vf;
226 unsigned int bkt;
227
228 lockdep_assert_held(&pf->vfs.table_lock);
229
230 ice_for_each_vf(pf, bkt, vf) {
231 struct ice_vsi *vsi = vf->repr->src_vsi;
232
233 /* Skip VFs that aren't configured */
234 if (!vf->repr->dst)
235 continue;
236
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,
241 ICE_FWD_TO_VSI);
242
243 netif_napi_del(&vf->repr->q_vector->napi);
244 }
245}
246
247/**
248 * ice_eswitch_setup_reprs - configure port reprs to run in switchdev mode
249 * @pf: pointer to PF struct
250 */
251static int ice_eswitch_setup_reprs(struct ice_pf *pf)
252{
253 struct ice_vsi *ctrl_vsi = pf->switchdev.control_vsi;
254 int max_vsi_num = 0;
255 struct ice_vf *vf;
256 unsigned int bkt;
257
258 lockdep_assert_held(&pf->vfs.table_lock);
259
260 ice_for_each_vf(pf, bkt, vf) {
261 struct ice_vsi *vsi = vf->repr->src_vsi;
262
263 ice_remove_vsi_fltr(&pf->hw, vsi->idx);
264 vf->repr->dst = metadata_dst_alloc(0, METADATA_HW_PORT_MUX,
265 GFP_KERNEL);
266 if (!vf->repr->dst) {
267 ice_fltr_add_mac_and_broadcast(vsi,
268 vf->hw_lan_addr.addr,
269 ICE_FWD_TO_VSI);
270 goto err;
271 }
272
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,
276 ICE_FWD_TO_VSI);
277 metadata_dst_free(vf->repr->dst);
278 vf->repr->dst = NULL;
279 goto err;
280 }
281
282 if (ice_vsi_add_vlan_zero(vsi)) {
283 ice_fltr_add_mac_and_broadcast(vsi,
284 vf->hw_lan_addr.addr,
285 ICE_FWD_TO_VSI);
286 metadata_dst_free(vf->repr->dst);
287 vf->repr->dst = NULL;
288 ice_vsi_update_security(vsi, ice_vsi_ctx_set_antispoof);
289 goto err;
290 }
291
292 if (max_vsi_num < vsi->vsi_num)
293 max_vsi_num = vsi->vsi_num;
294
295 netif_napi_add(vf->repr->netdev, &vf->repr->q_vector->napi,
296 ice_napi_poll);
297
298 netif_keep_dst(vf->repr->netdev);
299 }
300
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;
305
306 dst = repr->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);
310 }
311
312 return 0;
313
314err:
315 ice_eswitch_release_reprs(pf, ctrl_vsi);
316
317 return -ENODEV;
318}
319
320/**
321 * ice_eswitch_update_repr - reconfigure VF port representor
322 * @vsi: VF VSI for which port representor is configured
323 */
324void ice_eswitch_update_repr(struct ice_vsi *vsi)
325{
326 struct ice_pf *pf = vsi->back;
327 struct ice_repr *repr;
328 struct ice_vf *vf;
329 int ret;
330
331 if (!ice_is_switchdev_running(pf))
332 return;
333
334 vf = vsi->vf;
335 repr = vf->repr;
336 repr->src_vsi = vsi;
337 repr->dst->u.port_info.port_id = vsi->vsi_num;
338
339 ret = ice_vsi_update_security(vsi, ice_vsi_ctx_clear_antispoof);
340 if (ret) {
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",
343 vsi->vf->vf_id);
344 }
345}
346
347/**
348 * ice_eswitch_port_start_xmit - callback for packets transmit
349 * @skb: send buffer
350 * @netdev: network interface device structure
351 *
352 * Returns NETDEV_TX_OK if sent, else an error code
353 */
354netdev_tx_t
355ice_eswitch_port_start_xmit(struct sk_buff *skb, struct net_device *netdev)
356{
357 struct ice_netdev_priv *np;
358 struct ice_repr *repr;
359 struct ice_vsi *vsi;
360
361 np = netdev_priv(netdev);
362 vsi = np->vsi;
363
364 if (ice_is_reset_in_progress(vsi->back->state) ||
365 test_bit(ICE_VF_DIS, vsi->back->state))
366 return NETDEV_TX_BUSY;
367
368 repr = ice_netdev_to_repr(netdev);
369 skb_dst_drop(skb);
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;
373
374 return ice_start_xmit(skb, netdev);
375}
376
377/**
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
381 */
382void
383ice_eswitch_set_target_vsi(struct sk_buff *skb,
384 struct ice_tx_offload_params *off)
385{
386 struct metadata_dst *dst = skb_metadata_dst(skb);
387 u64 cd_cmd, dst_vsi;
388
389 if (!dst) {
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);
392 } else {
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;
397 }
398}
399
400/**
401 * ice_eswitch_release_env - clear switchdev HW filters
402 * @pf: pointer to PF struct
403 *
404 * This function removes HW filters configuration specific for switchdev
405 * mode and restores default legacy mode settings.
406 */
407static void ice_eswitch_release_env(struct ice_pf *pf)
408{
409 struct ice_vsi *uplink_vsi = pf->switchdev.uplink_vsi;
410 struct ice_vsi *ctrl_vsi = pf->switchdev.control_vsi;
411
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);
415 ice_fltr_add_mac_and_broadcast(uplink_vsi,
416 uplink_vsi->port_info->mac.perm_addr,
417 ICE_FWD_TO_VSI);
418}
419
420/**
421 * ice_eswitch_vsi_setup - configure switchdev control VSI
422 * @pf: pointer to PF structure
423 * @pi: pointer to port_info structure
424 */
425static struct ice_vsi *
426ice_eswitch_vsi_setup(struct ice_pf *pf, struct ice_port_info *pi)
427{
428 return ice_vsi_setup(pf, pi, ICE_VSI_SWITCHDEV_CTRL, NULL, NULL);
429}
430
431/**
432 * ice_eswitch_napi_del - remove NAPI handle for all port representors
433 * @pf: pointer to PF structure
434 */
435static void ice_eswitch_napi_del(struct ice_pf *pf)
436{
437 struct ice_vf *vf;
438 unsigned int bkt;
439
440 lockdep_assert_held(&pf->vfs.table_lock);
441
442 ice_for_each_vf(pf, bkt, vf)
443 netif_napi_del(&vf->repr->q_vector->napi);
444}
445
446/**
447 * ice_eswitch_napi_enable - enable NAPI for all port representors
448 * @pf: pointer to PF structure
449 */
450static void ice_eswitch_napi_enable(struct ice_pf *pf)
451{
452 struct ice_vf *vf;
453 unsigned int bkt;
454
455 lockdep_assert_held(&pf->vfs.table_lock);
456
457 ice_for_each_vf(pf, bkt, vf)
458 napi_enable(&vf->repr->q_vector->napi);
459}
460
461/**
462 * ice_eswitch_napi_disable - disable NAPI for all port representors
463 * @pf: pointer to PF structure
464 */
465static void ice_eswitch_napi_disable(struct ice_pf *pf)
466{
467 struct ice_vf *vf;
468 unsigned int bkt;
469
470 lockdep_assert_held(&pf->vfs.table_lock);
471
472 ice_for_each_vf(pf, bkt, vf)
473 napi_disable(&vf->repr->q_vector->napi);
474}
475
476/**
477 * ice_eswitch_enable_switchdev - configure eswitch in switchdev mode
478 * @pf: pointer to PF structure
479 */
480static int ice_eswitch_enable_switchdev(struct ice_pf *pf)
481{
482 struct ice_vsi *ctrl_vsi;
483
484 pf->switchdev.control_vsi = ice_eswitch_vsi_setup(pf, pf->hw.port_info);
485 if (!pf->switchdev.control_vsi)
486 return -ENODEV;
487
488 ctrl_vsi = pf->switchdev.control_vsi;
489 pf->switchdev.uplink_vsi = ice_get_main_vsi(pf);
490 if (!pf->switchdev.uplink_vsi)
491 goto err_vsi;
492
493 if (ice_eswitch_setup_env(pf))
494 goto err_vsi;
495
496 if (ice_repr_add_for_all_vfs(pf))
497 goto err_repr_add;
498
499 if (ice_eswitch_setup_reprs(pf))
500 goto err_setup_reprs;
501
502 ice_eswitch_remap_rings_to_vectors(pf);
503
504 if (ice_vsi_open(ctrl_vsi))
505 goto err_setup_reprs;
506
507 ice_eswitch_napi_enable(pf);
508
509 return 0;
510
511err_setup_reprs:
512 ice_repr_rem_from_all_vfs(pf);
513err_repr_add:
514 ice_eswitch_release_env(pf);
515err_vsi:
516 ice_vsi_release(ctrl_vsi);
517 return -ENODEV;
518}
519
520/**
521 * ice_eswitch_disable_switchdev - disable switchdev resources
522 * @pf: pointer to PF structure
523 */
524static void ice_eswitch_disable_switchdev(struct ice_pf *pf)
525{
526 struct ice_vsi *ctrl_vsi = pf->switchdev.control_vsi;
527
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);
534}
535
536/**
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
541 */
542int
543ice_eswitch_mode_set(struct devlink *devlink, u16 mode,
544 struct netlink_ext_ack *extack)
545{
546 struct ice_pf *pf = devlink_priv(devlink);
547
548 if (pf->eswitch_mode == mode)
549 return 0;
550
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");
554 return -EOPNOTSUPP;
555 }
556
557 switch (mode) {
558 case DEVLINK_ESWITCH_MODE_LEGACY:
559 dev_info(ice_pf_to_dev(pf), "PF %d changed eswitch mode to legacy",
560 pf->hw.pf_id);
561 NL_SET_ERR_MSG_MOD(extack, "Changed eswitch mode to legacy");
562 break;
563 case DEVLINK_ESWITCH_MODE_SWITCHDEV:
564 {
565 dev_info(ice_pf_to_dev(pf), "PF %d changed eswitch mode to switchdev",
566 pf->hw.pf_id);
567 NL_SET_ERR_MSG_MOD(extack, "Changed eswitch mode to switchdev");
568 break;
569 }
570 default:
571 NL_SET_ERR_MSG_MOD(extack, "Unknown eswitch mode");
572 return -EINVAL;
573 }
574
575 pf->eswitch_mode = mode;
576 return 0;
577}
578
579/**
580 * ice_eswitch_mode_get - get current eswitch mode
581 * @devlink: pointer to devlink structure
582 * @mode: output parameter for current eswitch mode
583 */
584int ice_eswitch_mode_get(struct devlink *devlink, u16 *mode)
585{
586 struct ice_pf *pf = devlink_priv(devlink);
587
588 *mode = pf->eswitch_mode;
589 return 0;
590}
591
592/**
593 * ice_is_eswitch_mode_switchdev - check if eswitch mode is set to switchdev
594 * @pf: pointer to PF structure
595 *
596 * Returns true if eswitch mode is set to DEVLINK_ESWITCH_MODE_SWITCHDEV,
597 * false otherwise.
598 */
599bool ice_is_eswitch_mode_switchdev(struct ice_pf *pf)
600{
601 return pf->eswitch_mode == DEVLINK_ESWITCH_MODE_SWITCHDEV;
602}
603
604/**
605 * ice_eswitch_release - cleanup eswitch
606 * @pf: pointer to PF structure
607 */
608void ice_eswitch_release(struct ice_pf *pf)
609{
610 if (pf->eswitch_mode == DEVLINK_ESWITCH_MODE_LEGACY)
611 return;
612
613 ice_eswitch_disable_switchdev(pf);
614 pf->switchdev.is_running = false;
615}
616
617/**
618 * ice_eswitch_configure - configure eswitch
619 * @pf: pointer to PF structure
620 */
621int ice_eswitch_configure(struct ice_pf *pf)
622{
623 int status;
624
625 if (pf->eswitch_mode == DEVLINK_ESWITCH_MODE_LEGACY || pf->switchdev.is_running)
626 return 0;
627
628 status = ice_eswitch_enable_switchdev(pf);
629 if (status)
630 return status;
631
632 pf->switchdev.is_running = true;
633 return 0;
634}
635
636/**
637 * ice_eswitch_start_all_tx_queues - start Tx queues of all port representors
638 * @pf: pointer to PF structure
639 */
640static void ice_eswitch_start_all_tx_queues(struct ice_pf *pf)
641{
642 struct ice_vf *vf;
643 unsigned int bkt;
644
645 lockdep_assert_held(&pf->vfs.table_lock);
646
647 if (test_bit(ICE_DOWN, pf->state))
648 return;
649
650 ice_for_each_vf(pf, bkt, vf) {
651 if (vf->repr)
652 ice_repr_start_tx_queues(vf->repr);
653 }
654}
655
656/**
657 * ice_eswitch_stop_all_tx_queues - stop Tx queues of all port representors
658 * @pf: pointer to PF structure
659 */
660void ice_eswitch_stop_all_tx_queues(struct ice_pf *pf)
661{
662 struct ice_vf *vf;
663 unsigned int bkt;
664
665 lockdep_assert_held(&pf->vfs.table_lock);
666
667 if (test_bit(ICE_DOWN, pf->state))
668 return;
669
670 ice_for_each_vf(pf, bkt, vf) {
671 if (vf->repr)
672 ice_repr_stop_tx_queues(vf->repr);
673 }
674}
675
676/**
677 * ice_eswitch_rebuild - rebuild eswitch
678 * @pf: pointer to PF structure
679 */
680int ice_eswitch_rebuild(struct ice_pf *pf)
681{
682 struct ice_vsi *ctrl_vsi = pf->switchdev.control_vsi;
683 int status;
684
685 ice_eswitch_napi_disable(pf);
686 ice_eswitch_napi_del(pf);
687
688 status = ice_eswitch_setup_env(pf);
689 if (status)
690 return status;
691
692 status = ice_eswitch_setup_reprs(pf);
693 if (status)
694 return status;
695
696 ice_eswitch_remap_rings_to_vectors(pf);
697
698 ice_replay_tc_fltrs(pf);
699
700 status = ice_vsi_open(ctrl_vsi);
701 if (status)
702 return status;
703
704 ice_eswitch_napi_enable(pf);
705 ice_eswitch_start_all_tx_queues(pf);
706
707 return 0;
708}