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 "devlink/devlink.h" 11 #include "ice_tc_lib.h" 12 13 /** 14 * ice_eswitch_setup_env - configure eswitch HW filters 15 * @pf: pointer to PF struct 16 * 17 * This function adds HW filters configuration specific for switchdev 18 * mode. 19 */ 20 static int ice_eswitch_setup_env(struct ice_pf *pf) 21 { 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; 26 27 if (if_running && !test_and_set_bit(ICE_VSI_DOWN, uplink_vsi->state)) 28 if (ice_down(uplink_vsi)) 29 return -ENODEV; 30 31 ice_remove_vsi_fltr(&pf->hw, uplink_vsi->idx); 32 ice_vsi_cfg_sw_lldp(uplink_vsi, true, false); 33 34 netif_addr_lock_bh(netdev); 35 __dev_uc_unsync(netdev, NULL); 36 __dev_mc_unsync(netdev, NULL); 37 netif_addr_unlock_bh(netdev); 38 39 if (ice_vsi_add_vlan_zero(uplink_vsi)) 40 goto err_vlan_zero; 41 42 if (ice_set_dflt_vsi(uplink_vsi)) 43 goto err_def_rx; 44 45 if (ice_cfg_dflt_vsi(uplink_vsi->port_info, uplink_vsi->idx, true, 46 ICE_FLTR_TX)) 47 goto err_def_tx; 48 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; 52 53 if (ice_vsi_update_local_lb(uplink_vsi, true)) 54 goto err_override_local_lb; 55 56 if (if_running && ice_up(uplink_vsi)) 57 goto err_up; 58 59 return 0; 60 61 err_up: 62 ice_vsi_update_local_lb(uplink_vsi, false); 63 err_override_local_lb: 64 vlan_ops->ena_rx_filtering(uplink_vsi); 65 err_vlan_filtering: 66 ice_cfg_dflt_vsi(uplink_vsi->port_info, uplink_vsi->idx, false, 67 ICE_FLTR_TX); 68 err_def_tx: 69 ice_cfg_dflt_vsi(uplink_vsi->port_info, uplink_vsi->idx, false, 70 ICE_FLTR_RX); 71 err_def_rx: 72 ice_vsi_del_vlan_zero(uplink_vsi); 73 err_vlan_zero: 74 ice_fltr_add_mac_and_broadcast(uplink_vsi, 75 uplink_vsi->port_info->mac.perm_addr, 76 ICE_FWD_TO_VSI); 77 if (if_running) 78 ice_up(uplink_vsi); 79 80 return -ENODEV; 81 } 82 83 /** 84 * ice_eswitch_release_repr - clear PR VSI configuration 85 * @pf: poiner to PF struct 86 * @repr: pointer to PR 87 */ 88 static void 89 ice_eswitch_release_repr(struct ice_pf *pf, struct ice_repr *repr) 90 { 91 struct ice_vsi *vsi = repr->src_vsi; 92 93 /* Skip representors that aren't configured */ 94 if (!repr->dst) 95 return; 96 97 ice_vsi_update_security(vsi, ice_vsi_ctx_set_antispoof); 98 metadata_dst_free(repr->dst); 99 repr->dst = NULL; 100 ice_fltr_add_mac_and_broadcast(vsi, repr->parent_mac, 101 ICE_FWD_TO_VSI); 102 } 103 104 /** 105 * ice_eswitch_setup_repr - configure PR to run in switchdev mode 106 * @pf: pointer to PF struct 107 * @repr: pointer to PR struct 108 */ 109 static int ice_eswitch_setup_repr(struct ice_pf *pf, struct ice_repr *repr) 110 { 111 struct ice_vsi *uplink_vsi = pf->eswitch.uplink_vsi; 112 struct ice_vsi *vsi = repr->src_vsi; 113 struct metadata_dst *dst; 114 115 repr->dst = metadata_dst_alloc(0, METADATA_HW_PORT_MUX, 116 GFP_KERNEL); 117 if (!repr->dst) 118 return -ENOMEM; 119 120 dst = repr->dst; 121 dst->u.port_info.port_id = vsi->vsi_num; 122 dst->u.port_info.lower_dev = uplink_vsi->netdev; 123 124 return 0; 125 } 126 127 /** 128 * ice_eswitch_cfg_vsi - configure VSI to work in slow-path 129 * @vsi: VSI structure of representee 130 * @mac: representee MAC 131 * 132 * Return: 0 on success, non-zero on error. 133 */ 134 int ice_eswitch_cfg_vsi(struct ice_vsi *vsi, const u8 *mac) 135 { 136 int err; 137 138 ice_remove_vsi_fltr(&vsi->back->hw, vsi->idx); 139 140 err = ice_vsi_update_security(vsi, ice_vsi_ctx_clear_antispoof); 141 if (err) 142 goto err_update_security; 143 144 err = ice_vsi_add_vlan_zero(vsi); 145 if (err) 146 goto err_vlan_zero; 147 148 return 0; 149 150 err_vlan_zero: 151 ice_vsi_update_security(vsi, ice_vsi_ctx_set_antispoof); 152 err_update_security: 153 ice_fltr_add_mac_and_broadcast(vsi, mac, ICE_FWD_TO_VSI); 154 155 return err; 156 } 157 158 /** 159 * ice_eswitch_decfg_vsi - unroll changes done to VSI for switchdev 160 * @vsi: VSI structure of representee 161 * @mac: representee MAC 162 */ 163 void ice_eswitch_decfg_vsi(struct ice_vsi *vsi, const u8 *mac) 164 { 165 ice_vsi_update_security(vsi, ice_vsi_ctx_set_antispoof); 166 ice_fltr_add_mac_and_broadcast(vsi, mac, ICE_FWD_TO_VSI); 167 } 168 169 /** 170 * ice_eswitch_update_repr - reconfigure port representor 171 * @repr_id: representor ID 172 * @vsi: VSI for which port representor is configured 173 */ 174 void ice_eswitch_update_repr(unsigned long *repr_id, struct ice_vsi *vsi) 175 { 176 struct ice_pf *pf = vsi->back; 177 struct ice_repr *repr; 178 int err; 179 180 if (!ice_is_switchdev_running(pf)) 181 return; 182 183 repr = xa_load(&pf->eswitch.reprs, *repr_id); 184 if (!repr) 185 return; 186 187 repr->src_vsi = vsi; 188 repr->dst->u.port_info.port_id = vsi->vsi_num; 189 190 if (repr->br_port) 191 repr->br_port->vsi = vsi; 192 193 err = ice_eswitch_cfg_vsi(vsi, repr->parent_mac); 194 if (err) 195 dev_err(ice_pf_to_dev(pf), "Failed to update VSI of port representor %d", 196 repr->id); 197 198 /* The VSI number is different, reload the PR with new id */ 199 if (repr->id != vsi->vsi_num) { 200 xa_erase(&pf->eswitch.reprs, repr->id); 201 repr->id = vsi->vsi_num; 202 if (xa_insert(&pf->eswitch.reprs, repr->id, repr, GFP_KERNEL)) 203 dev_err(ice_pf_to_dev(pf), "Failed to reload port representor %d", 204 repr->id); 205 *repr_id = repr->id; 206 } 207 } 208 209 /** 210 * ice_eswitch_port_start_xmit - callback for packets transmit 211 * @skb: send buffer 212 * @netdev: network interface device structure 213 * 214 * Returns NETDEV_TX_OK if sent, else an error code 215 */ 216 netdev_tx_t 217 ice_eswitch_port_start_xmit(struct sk_buff *skb, struct net_device *netdev) 218 { 219 struct ice_repr *repr = ice_netdev_to_repr(netdev); 220 unsigned int len = skb->len; 221 int ret; 222 223 skb_dst_drop(skb); 224 dst_hold((struct dst_entry *)repr->dst); 225 skb_dst_set(skb, (struct dst_entry *)repr->dst); 226 skb->dev = repr->dst->u.port_info.lower_dev; 227 228 ret = dev_queue_xmit(skb); 229 ice_repr_inc_tx_stats(repr, len, ret); 230 231 return ret; 232 } 233 234 /** 235 * ice_eswitch_set_target_vsi - set eswitch context in Tx context descriptor 236 * @skb: pointer to send buffer 237 * @off: pointer to offload struct 238 */ 239 void 240 ice_eswitch_set_target_vsi(struct sk_buff *skb, 241 struct ice_tx_offload_params *off) 242 { 243 struct metadata_dst *dst = skb_metadata_dst(skb); 244 u64 cd_cmd, dst_vsi; 245 246 if (!dst) { 247 struct ethhdr *eth = (struct ethhdr *)skb_mac_header(skb); 248 249 if (unlikely(eth->h_proto == htons(ETH_P_LLDP))) 250 return; 251 cd_cmd = ICE_TX_CTX_DESC_SWTCH_UPLINK << ICE_TXD_CTX_QW1_CMD_S; 252 off->cd_qw1 |= (cd_cmd | ICE_TX_DESC_DTYPE_CTX); 253 } else { 254 cd_cmd = ICE_TX_CTX_DESC_SWTCH_VSI << ICE_TXD_CTX_QW1_CMD_S; 255 dst_vsi = FIELD_PREP(ICE_TXD_CTX_QW1_VSI_M, 256 dst->u.port_info.port_id); 257 off->cd_qw1 = cd_cmd | dst_vsi | ICE_TX_DESC_DTYPE_CTX; 258 } 259 } 260 261 /** 262 * ice_eswitch_release_env - clear eswitch HW filters 263 * @pf: pointer to PF struct 264 * 265 * This function removes HW filters configuration specific for switchdev 266 * mode and restores default legacy mode settings. 267 */ 268 static void ice_eswitch_release_env(struct ice_pf *pf) 269 { 270 struct ice_vsi *uplink_vsi = pf->eswitch.uplink_vsi; 271 struct ice_vsi_vlan_ops *vlan_ops; 272 273 vlan_ops = ice_get_compat_vsi_vlan_ops(uplink_vsi); 274 275 ice_vsi_update_local_lb(uplink_vsi, false); 276 vlan_ops->ena_rx_filtering(uplink_vsi); 277 ice_cfg_dflt_vsi(uplink_vsi->port_info, uplink_vsi->idx, false, 278 ICE_FLTR_TX); 279 ice_cfg_dflt_vsi(uplink_vsi->port_info, uplink_vsi->idx, false, 280 ICE_FLTR_RX); 281 ice_fltr_add_mac_and_broadcast(uplink_vsi, 282 uplink_vsi->port_info->mac.perm_addr, 283 ICE_FWD_TO_VSI); 284 ice_vsi_cfg_sw_lldp(uplink_vsi, true, true); 285 } 286 287 /** 288 * ice_eswitch_enable_switchdev - configure eswitch in switchdev mode 289 * @pf: pointer to PF structure 290 */ 291 static int ice_eswitch_enable_switchdev(struct ice_pf *pf) 292 { 293 struct ice_vsi *uplink_vsi; 294 295 uplink_vsi = ice_get_main_vsi(pf); 296 if (!uplink_vsi) 297 return -ENODEV; 298 299 if (netif_is_any_bridge_port(uplink_vsi->netdev)) { 300 dev_err(ice_pf_to_dev(pf), 301 "Uplink port cannot be a bridge port\n"); 302 return -EINVAL; 303 } 304 305 pf->eswitch.uplink_vsi = uplink_vsi; 306 307 if (ice_eswitch_setup_env(pf)) 308 return -ENODEV; 309 310 if (ice_eswitch_br_offloads_init(pf)) 311 goto err_br_offloads; 312 313 netif_keep_dst(uplink_vsi->netdev); 314 315 pf->eswitch.is_running = true; 316 317 return 0; 318 319 err_br_offloads: 320 ice_eswitch_release_env(pf); 321 return -ENODEV; 322 } 323 324 /** 325 * ice_eswitch_disable_switchdev - disable eswitch resources 326 * @pf: pointer to PF structure 327 */ 328 static void ice_eswitch_disable_switchdev(struct ice_pf *pf) 329 { 330 ice_eswitch_br_offloads_deinit(pf); 331 ice_eswitch_release_env(pf); 332 333 pf->eswitch.is_running = false; 334 } 335 336 /** 337 * ice_eswitch_mode_set - set new eswitch mode 338 * @devlink: pointer to devlink structure 339 * @mode: eswitch mode to switch to 340 * @extack: pointer to extack structure 341 */ 342 int 343 ice_eswitch_mode_set(struct devlink *devlink, u16 mode, 344 struct netlink_ext_ack *extack) 345 { 346 struct ice_pf *pf = devlink_priv(devlink); 347 348 if (pf->eswitch_mode == mode) 349 return 0; 350 351 if (ice_has_vfs(pf)) { 352 dev_info(ice_pf_to_dev(pf), "Changing eswitch mode is allowed only if there is no VFs created"); 353 NL_SET_ERR_MSG_MOD(extack, "Changing eswitch mode is allowed only if there is no VFs created"); 354 return -EOPNOTSUPP; 355 } 356 357 switch (mode) { 358 case DEVLINK_ESWITCH_MODE_LEGACY: 359 dev_info(ice_pf_to_dev(pf), "PF %d changed eswitch mode to legacy", 360 pf->hw.pf_id); 361 xa_destroy(&pf->eswitch.reprs); 362 NL_SET_ERR_MSG_MOD(extack, "Changed eswitch mode to legacy"); 363 break; 364 case DEVLINK_ESWITCH_MODE_SWITCHDEV: 365 { 366 if (ice_is_adq_active(pf)) { 367 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"); 368 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"); 369 return -EOPNOTSUPP; 370 } 371 372 dev_info(ice_pf_to_dev(pf), "PF %d changed eswitch mode to switchdev", 373 pf->hw.pf_id); 374 xa_init(&pf->eswitch.reprs); 375 NL_SET_ERR_MSG_MOD(extack, "Changed eswitch mode to switchdev"); 376 break; 377 } 378 default: 379 NL_SET_ERR_MSG_MOD(extack, "Unknown eswitch mode"); 380 return -EINVAL; 381 } 382 383 pf->eswitch_mode = mode; 384 return 0; 385 } 386 387 /** 388 * ice_eswitch_mode_get - get current eswitch mode 389 * @devlink: pointer to devlink structure 390 * @mode: output parameter for current eswitch mode 391 */ 392 int ice_eswitch_mode_get(struct devlink *devlink, u16 *mode) 393 { 394 struct ice_pf *pf = devlink_priv(devlink); 395 396 *mode = pf->eswitch_mode; 397 return 0; 398 } 399 400 /** 401 * ice_is_eswitch_mode_switchdev - check if eswitch mode is set to switchdev 402 * @pf: pointer to PF structure 403 * 404 * Returns true if eswitch mode is set to DEVLINK_ESWITCH_MODE_SWITCHDEV, 405 * false otherwise. 406 */ 407 bool ice_is_eswitch_mode_switchdev(struct ice_pf *pf) 408 { 409 return pf->eswitch_mode == DEVLINK_ESWITCH_MODE_SWITCHDEV; 410 } 411 412 /** 413 * ice_eswitch_start_all_tx_queues - start Tx queues of all port representors 414 * @pf: pointer to PF structure 415 */ 416 static void ice_eswitch_start_all_tx_queues(struct ice_pf *pf) 417 { 418 struct ice_repr *repr; 419 unsigned long id; 420 421 if (test_bit(ICE_DOWN, pf->state)) 422 return; 423 424 xa_for_each(&pf->eswitch.reprs, id, repr) 425 ice_repr_start_tx_queues(repr); 426 } 427 428 /** 429 * ice_eswitch_stop_all_tx_queues - stop Tx queues of all port representors 430 * @pf: pointer to PF structure 431 */ 432 void ice_eswitch_stop_all_tx_queues(struct ice_pf *pf) 433 { 434 struct ice_repr *repr; 435 unsigned long id; 436 437 if (test_bit(ICE_DOWN, pf->state)) 438 return; 439 440 xa_for_each(&pf->eswitch.reprs, id, repr) 441 ice_repr_stop_tx_queues(repr); 442 } 443 444 static void ice_eswitch_stop_reprs(struct ice_pf *pf) 445 { 446 ice_eswitch_stop_all_tx_queues(pf); 447 } 448 449 static void ice_eswitch_start_reprs(struct ice_pf *pf) 450 { 451 ice_eswitch_start_all_tx_queues(pf); 452 } 453 454 static int 455 ice_eswitch_attach(struct ice_pf *pf, struct ice_repr *repr, unsigned long *id) 456 { 457 int err; 458 459 if (pf->eswitch_mode == DEVLINK_ESWITCH_MODE_LEGACY) 460 return 0; 461 462 if (xa_empty(&pf->eswitch.reprs)) { 463 err = ice_eswitch_enable_switchdev(pf); 464 if (err) 465 return err; 466 } 467 468 ice_eswitch_stop_reprs(pf); 469 470 err = repr->ops.add(repr); 471 if (err) 472 goto err_create_repr; 473 474 err = ice_eswitch_setup_repr(pf, repr); 475 if (err) 476 goto err_setup_repr; 477 478 err = xa_insert(&pf->eswitch.reprs, repr->id, repr, GFP_KERNEL); 479 if (err) 480 goto err_xa_alloc; 481 482 *id = repr->id; 483 484 ice_eswitch_start_reprs(pf); 485 486 return 0; 487 488 err_xa_alloc: 489 ice_eswitch_release_repr(pf, repr); 490 err_setup_repr: 491 repr->ops.rem(repr); 492 err_create_repr: 493 if (xa_empty(&pf->eswitch.reprs)) 494 ice_eswitch_disable_switchdev(pf); 495 ice_eswitch_start_reprs(pf); 496 497 return err; 498 } 499 500 /** 501 * ice_eswitch_attach_vf - attach VF to a eswitch 502 * @pf: pointer to PF structure 503 * @vf: pointer to VF structure to be attached 504 * 505 * During attaching port representor for VF is created. 506 * 507 * Return: zero on success or an error code on failure. 508 */ 509 int ice_eswitch_attach_vf(struct ice_pf *pf, struct ice_vf *vf) 510 { 511 struct devlink *devlink = priv_to_devlink(pf); 512 struct ice_repr *repr; 513 int err; 514 515 repr = ice_repr_create_vf(vf); 516 if (IS_ERR(repr)) 517 return PTR_ERR(repr); 518 519 devl_lock(devlink); 520 err = ice_eswitch_attach(pf, repr, &vf->repr_id); 521 if (err) 522 ice_repr_destroy(repr); 523 devl_unlock(devlink); 524 525 return err; 526 } 527 528 /** 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 532 * 533 * During attaching port representor for SF is created. 534 * 535 * Return: zero on success or an error code on failure. 536 */ 537 int ice_eswitch_attach_sf(struct ice_pf *pf, struct ice_dynamic_port *sf) 538 { 539 struct ice_repr *repr = ice_repr_create_sf(sf); 540 int err; 541 542 if (IS_ERR(repr)) 543 return PTR_ERR(repr); 544 545 err = ice_eswitch_attach(pf, repr, &sf->repr_id); 546 if (err) 547 ice_repr_destroy(repr); 548 549 return err; 550 } 551 552 static void ice_eswitch_detach(struct ice_pf *pf, struct ice_repr *repr) 553 { 554 ice_eswitch_stop_reprs(pf); 555 repr->ops.rem(repr); 556 557 xa_erase(&pf->eswitch.reprs, repr->id); 558 559 if (xa_empty(&pf->eswitch.reprs)) 560 ice_eswitch_disable_switchdev(pf); 561 562 ice_eswitch_release_repr(pf, repr); 563 ice_repr_destroy(repr); 564 565 if (xa_empty(&pf->eswitch.reprs)) { 566 struct devlink *devlink = priv_to_devlink(pf); 567 568 /* since all port representors are destroyed, there is 569 * no point in keeping the nodes 570 */ 571 ice_devlink_rate_clear_tx_topology(ice_get_main_vsi(pf)); 572 devl_rate_nodes_destroy(devlink); 573 } else { 574 ice_eswitch_start_reprs(pf); 575 } 576 } 577 578 /** 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 582 */ 583 void ice_eswitch_detach_vf(struct ice_pf *pf, struct ice_vf *vf) 584 { 585 struct ice_repr *repr = xa_load(&pf->eswitch.reprs, vf->repr_id); 586 struct devlink *devlink = priv_to_devlink(pf); 587 588 if (!repr) 589 return; 590 591 devl_lock(devlink); 592 ice_eswitch_detach(pf, repr); 593 devl_unlock(devlink); 594 } 595 596 /** 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 600 */ 601 void ice_eswitch_detach_sf(struct ice_pf *pf, struct ice_dynamic_port *sf) 602 { 603 struct ice_repr *repr = xa_load(&pf->eswitch.reprs, sf->repr_id); 604 605 if (!repr) 606 return; 607 608 ice_eswitch_detach(pf, repr); 609 } 610 611 /** 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 615 * 616 * Get src_vsi value from descriptor and load correct representor. If it isn't 617 * found return rx_ring->netdev. 618 */ 619 struct net_device *ice_eswitch_get_target(struct ice_rx_ring *rx_ring, 620 union ice_32b_rx_flex_desc *rx_desc) 621 { 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; 625 626 desc = (struct ice_32b_rx_flex_desc_nic_2 *)rx_desc; 627 repr = xa_load(&eswitch->reprs, le16_to_cpu(desc->src_vsi)); 628 if (!repr) 629 return rx_ring->netdev; 630 631 return repr->netdev; 632 } 633