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