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