1 // SPDX-License-Identifier: GPL-2.0 2 /* Copyright (C) 2019-2021, Intel Corporation. */ 3 4 #include "ice.h" 5 #include "ice_eswitch.h" 6 #include "ice_devlink.h" 7 #include "ice_sriov.h" 8 #include "ice_tc_lib.h" 9 #include "ice_dcb_lib.h" 10 11 /** 12 * ice_repr_get_sw_port_id - get port ID associated with representor 13 * @repr: pointer to port representor 14 */ 15 static int ice_repr_get_sw_port_id(struct ice_repr *repr) 16 { 17 return repr->vf->pf->hw.port_info->lport; 18 } 19 20 /** 21 * ice_repr_get_phys_port_name - get phys port name 22 * @netdev: pointer to port representor netdev 23 * @buf: write here port name 24 * @len: max length of buf 25 */ 26 static int 27 ice_repr_get_phys_port_name(struct net_device *netdev, char *buf, size_t len) 28 { 29 struct ice_netdev_priv *np = netdev_priv(netdev); 30 struct ice_repr *repr = np->repr; 31 int res; 32 33 /* Devlink port is registered and devlink core is taking care of name formatting. */ 34 if (repr->vf->devlink_port.devlink) 35 return -EOPNOTSUPP; 36 37 res = snprintf(buf, len, "pf%dvfr%d", ice_repr_get_sw_port_id(repr), 38 repr->vf->vf_id); 39 if (res <= 0) 40 return -EOPNOTSUPP; 41 return 0; 42 } 43 44 /** 45 * ice_repr_get_stats64 - get VF stats for VFPR use 46 * @netdev: pointer to port representor netdev 47 * @stats: pointer to struct where stats can be stored 48 */ 49 static void 50 ice_repr_get_stats64(struct net_device *netdev, struct rtnl_link_stats64 *stats) 51 { 52 struct ice_netdev_priv *np = netdev_priv(netdev); 53 struct ice_eth_stats *eth_stats; 54 struct ice_vsi *vsi; 55 56 if (ice_is_vf_disabled(np->repr->vf)) 57 return; 58 vsi = np->repr->src_vsi; 59 60 ice_update_vsi_stats(vsi); 61 eth_stats = &vsi->eth_stats; 62 63 stats->tx_packets = eth_stats->tx_unicast + eth_stats->tx_broadcast + 64 eth_stats->tx_multicast; 65 stats->rx_packets = eth_stats->rx_unicast + eth_stats->rx_broadcast + 66 eth_stats->rx_multicast; 67 stats->tx_bytes = eth_stats->tx_bytes; 68 stats->rx_bytes = eth_stats->rx_bytes; 69 stats->multicast = eth_stats->rx_multicast; 70 stats->tx_errors = eth_stats->tx_errors; 71 stats->tx_dropped = eth_stats->tx_discards; 72 stats->rx_dropped = eth_stats->rx_discards; 73 } 74 75 /** 76 * ice_netdev_to_repr - Get port representor for given netdevice 77 * @netdev: pointer to port representor netdev 78 */ 79 struct ice_repr *ice_netdev_to_repr(struct net_device *netdev) 80 { 81 struct ice_netdev_priv *np = netdev_priv(netdev); 82 83 return np->repr; 84 } 85 86 /** 87 * ice_repr_open - Enable port representor's network interface 88 * @netdev: network interface device structure 89 * 90 * The open entry point is called when a port representor's network 91 * interface is made active by the system (IFF_UP). Corresponding 92 * VF is notified about link status change. 93 * 94 * Returns 0 on success 95 */ 96 static int ice_repr_open(struct net_device *netdev) 97 { 98 struct ice_repr *repr = ice_netdev_to_repr(netdev); 99 struct ice_vf *vf; 100 101 vf = repr->vf; 102 vf->link_forced = true; 103 vf->link_up = true; 104 ice_vc_notify_vf_link_state(vf); 105 106 netif_carrier_on(netdev); 107 netif_tx_start_all_queues(netdev); 108 109 return 0; 110 } 111 112 /** 113 * ice_repr_stop - Disable port representor's network interface 114 * @netdev: network interface device structure 115 * 116 * The stop entry point is called when a port representor's network 117 * interface is de-activated by the system. Corresponding 118 * VF is notified about link status change. 119 * 120 * Returns 0 on success 121 */ 122 static int ice_repr_stop(struct net_device *netdev) 123 { 124 struct ice_repr *repr = ice_netdev_to_repr(netdev); 125 struct ice_vf *vf; 126 127 vf = repr->vf; 128 vf->link_forced = true; 129 vf->link_up = false; 130 ice_vc_notify_vf_link_state(vf); 131 132 netif_carrier_off(netdev); 133 netif_tx_stop_all_queues(netdev); 134 135 return 0; 136 } 137 138 /** 139 * ice_repr_sp_stats64 - get slow path stats for port representor 140 * @dev: network interface device structure 141 * @stats: netlink stats structure 142 * 143 * RX/TX stats are being swapped here to be consistent with VF stats. In slow 144 * path, port representor receives data when the corresponding VF is sending it 145 * (and vice versa), TX and RX bytes/packets are effectively swapped on port 146 * representor. 147 */ 148 static int 149 ice_repr_sp_stats64(const struct net_device *dev, 150 struct rtnl_link_stats64 *stats) 151 { 152 struct ice_netdev_priv *np = netdev_priv(dev); 153 int vf_id = np->repr->vf->vf_id; 154 struct ice_tx_ring *tx_ring; 155 struct ice_rx_ring *rx_ring; 156 u64 pkts, bytes; 157 158 tx_ring = np->vsi->tx_rings[vf_id]; 159 ice_fetch_u64_stats_per_ring(&tx_ring->syncp, tx_ring->stats, 160 &pkts, &bytes); 161 stats->rx_packets = pkts; 162 stats->rx_bytes = bytes; 163 164 rx_ring = np->vsi->rx_rings[vf_id]; 165 ice_fetch_u64_stats_per_ring(&rx_ring->syncp, rx_ring->stats, 166 &pkts, &bytes); 167 stats->tx_packets = pkts; 168 stats->tx_bytes = bytes; 169 stats->tx_dropped = rx_ring->rx_stats.alloc_page_failed + 170 rx_ring->rx_stats.alloc_buf_failed; 171 172 return 0; 173 } 174 175 static bool 176 ice_repr_ndo_has_offload_stats(const struct net_device *dev, int attr_id) 177 { 178 return attr_id == IFLA_OFFLOAD_XSTATS_CPU_HIT; 179 } 180 181 static int 182 ice_repr_ndo_get_offload_stats(int attr_id, const struct net_device *dev, 183 void *sp) 184 { 185 if (attr_id == IFLA_OFFLOAD_XSTATS_CPU_HIT) 186 return ice_repr_sp_stats64(dev, (struct rtnl_link_stats64 *)sp); 187 188 return -EINVAL; 189 } 190 191 static int 192 ice_repr_setup_tc_cls_flower(struct ice_repr *repr, 193 struct flow_cls_offload *flower) 194 { 195 switch (flower->command) { 196 case FLOW_CLS_REPLACE: 197 return ice_add_cls_flower(repr->netdev, repr->src_vsi, flower); 198 case FLOW_CLS_DESTROY: 199 return ice_del_cls_flower(repr->src_vsi, flower); 200 default: 201 return -EINVAL; 202 } 203 } 204 205 static int 206 ice_repr_setup_tc_block_cb(enum tc_setup_type type, void *type_data, 207 void *cb_priv) 208 { 209 struct flow_cls_offload *flower = (struct flow_cls_offload *)type_data; 210 struct ice_netdev_priv *np = (struct ice_netdev_priv *)cb_priv; 211 212 switch (type) { 213 case TC_SETUP_CLSFLOWER: 214 return ice_repr_setup_tc_cls_flower(np->repr, flower); 215 default: 216 return -EOPNOTSUPP; 217 } 218 } 219 220 static LIST_HEAD(ice_repr_block_cb_list); 221 222 static int 223 ice_repr_setup_tc(struct net_device *netdev, enum tc_setup_type type, 224 void *type_data) 225 { 226 struct ice_netdev_priv *np = netdev_priv(netdev); 227 228 switch (type) { 229 case TC_SETUP_BLOCK: 230 return flow_block_cb_setup_simple((struct flow_block_offload *) 231 type_data, 232 &ice_repr_block_cb_list, 233 ice_repr_setup_tc_block_cb, 234 np, np, true); 235 default: 236 return -EOPNOTSUPP; 237 } 238 } 239 240 static const struct net_device_ops ice_repr_netdev_ops = { 241 .ndo_get_phys_port_name = ice_repr_get_phys_port_name, 242 .ndo_get_stats64 = ice_repr_get_stats64, 243 .ndo_open = ice_repr_open, 244 .ndo_stop = ice_repr_stop, 245 .ndo_start_xmit = ice_eswitch_port_start_xmit, 246 .ndo_setup_tc = ice_repr_setup_tc, 247 .ndo_has_offload_stats = ice_repr_ndo_has_offload_stats, 248 .ndo_get_offload_stats = ice_repr_ndo_get_offload_stats, 249 }; 250 251 /** 252 * ice_is_port_repr_netdev - Check if a given netdevice is a port representor netdev 253 * @netdev: pointer to netdev 254 */ 255 bool ice_is_port_repr_netdev(struct net_device *netdev) 256 { 257 return netdev && (netdev->netdev_ops == &ice_repr_netdev_ops); 258 } 259 260 /** 261 * ice_repr_reg_netdev - register port representor netdev 262 * @netdev: pointer to port representor netdev 263 */ 264 static int 265 ice_repr_reg_netdev(struct net_device *netdev) 266 { 267 eth_hw_addr_random(netdev); 268 netdev->netdev_ops = &ice_repr_netdev_ops; 269 ice_set_ethtool_repr_ops(netdev); 270 271 netdev->hw_features |= NETIF_F_HW_TC; 272 273 netif_carrier_off(netdev); 274 netif_tx_stop_all_queues(netdev); 275 276 return register_netdev(netdev); 277 } 278 279 /** 280 * ice_repr_add - add representor for VF 281 * @vf: pointer to VF structure 282 */ 283 static int ice_repr_add(struct ice_vf *vf) 284 { 285 struct ice_q_vector *q_vector; 286 struct ice_netdev_priv *np; 287 struct ice_repr *repr; 288 struct ice_vsi *vsi; 289 int err; 290 291 vsi = ice_get_vf_vsi(vf); 292 if (!vsi) 293 return -EINVAL; 294 295 repr = kzalloc(sizeof(*repr), GFP_KERNEL); 296 if (!repr) 297 return -ENOMEM; 298 299 #ifdef CONFIG_ICE_SWITCHDEV 300 repr->mac_rule = kzalloc(sizeof(*repr->mac_rule), GFP_KERNEL); 301 if (!repr->mac_rule) { 302 err = -ENOMEM; 303 goto err_alloc_rule; 304 } 305 #endif 306 307 repr->netdev = alloc_etherdev(sizeof(struct ice_netdev_priv)); 308 if (!repr->netdev) { 309 err = -ENOMEM; 310 goto err_alloc; 311 } 312 313 repr->src_vsi = vsi; 314 repr->vf = vf; 315 vf->repr = repr; 316 np = netdev_priv(repr->netdev); 317 np->repr = repr; 318 319 q_vector = kzalloc(sizeof(*q_vector), GFP_KERNEL); 320 if (!q_vector) { 321 err = -ENOMEM; 322 goto err_alloc_q_vector; 323 } 324 repr->q_vector = q_vector; 325 326 err = ice_devlink_create_vf_port(vf); 327 if (err) 328 goto err_devlink; 329 330 repr->netdev->min_mtu = ETH_MIN_MTU; 331 repr->netdev->max_mtu = ICE_MAX_MTU; 332 333 SET_NETDEV_DEV(repr->netdev, ice_pf_to_dev(vf->pf)); 334 SET_NETDEV_DEVLINK_PORT(repr->netdev, &vf->devlink_port); 335 err = ice_repr_reg_netdev(repr->netdev); 336 if (err) 337 goto err_netdev; 338 339 ice_virtchnl_set_repr_ops(vf); 340 341 return 0; 342 343 err_netdev: 344 ice_devlink_destroy_vf_port(vf); 345 err_devlink: 346 kfree(repr->q_vector); 347 vf->repr->q_vector = NULL; 348 err_alloc_q_vector: 349 free_netdev(repr->netdev); 350 repr->netdev = NULL; 351 err_alloc: 352 #ifdef CONFIG_ICE_SWITCHDEV 353 kfree(repr->mac_rule); 354 repr->mac_rule = NULL; 355 err_alloc_rule: 356 #endif 357 kfree(repr); 358 vf->repr = NULL; 359 return err; 360 } 361 362 /** 363 * ice_repr_rem - remove representor from VF 364 * @vf: pointer to VF structure 365 */ 366 static void ice_repr_rem(struct ice_vf *vf) 367 { 368 if (!vf->repr) 369 return; 370 371 kfree(vf->repr->q_vector); 372 vf->repr->q_vector = NULL; 373 unregister_netdev(vf->repr->netdev); 374 ice_devlink_destroy_vf_port(vf); 375 free_netdev(vf->repr->netdev); 376 vf->repr->netdev = NULL; 377 #ifdef CONFIG_ICE_SWITCHDEV 378 kfree(vf->repr->mac_rule); 379 vf->repr->mac_rule = NULL; 380 #endif 381 kfree(vf->repr); 382 vf->repr = NULL; 383 384 ice_virtchnl_set_dflt_ops(vf); 385 } 386 387 /** 388 * ice_repr_rem_from_all_vfs - remove port representor for all VFs 389 * @pf: pointer to PF structure 390 */ 391 void ice_repr_rem_from_all_vfs(struct ice_pf *pf) 392 { 393 struct devlink *devlink; 394 struct ice_vf *vf; 395 unsigned int bkt; 396 397 lockdep_assert_held(&pf->vfs.table_lock); 398 399 ice_for_each_vf(pf, bkt, vf) 400 ice_repr_rem(vf); 401 402 /* since all port representors are destroyed, there is 403 * no point in keeping the nodes 404 */ 405 devlink = priv_to_devlink(pf); 406 devl_lock(devlink); 407 devl_rate_nodes_destroy(devlink); 408 devl_unlock(devlink); 409 } 410 411 /** 412 * ice_repr_add_for_all_vfs - add port representor for all VFs 413 * @pf: pointer to PF structure 414 */ 415 int ice_repr_add_for_all_vfs(struct ice_pf *pf) 416 { 417 struct devlink *devlink; 418 struct ice_vf *vf; 419 unsigned int bkt; 420 int err; 421 422 lockdep_assert_held(&pf->vfs.table_lock); 423 424 ice_for_each_vf(pf, bkt, vf) { 425 err = ice_repr_add(vf); 426 if (err) 427 goto err; 428 } 429 430 /* only export if ADQ and DCB disabled */ 431 if (ice_is_adq_active(pf) || ice_is_dcb_active(pf)) 432 return 0; 433 434 devlink = priv_to_devlink(pf); 435 ice_devlink_rate_init_tx_topology(devlink, ice_get_main_vsi(pf)); 436 437 return 0; 438 439 err: 440 ice_repr_rem_from_all_vfs(pf); 441 442 return err; 443 } 444 445 /** 446 * ice_repr_start_tx_queues - start Tx queues of port representor 447 * @repr: pointer to repr structure 448 */ 449 void ice_repr_start_tx_queues(struct ice_repr *repr) 450 { 451 netif_carrier_on(repr->netdev); 452 netif_tx_start_all_queues(repr->netdev); 453 } 454 455 /** 456 * ice_repr_stop_tx_queues - stop Tx queues of port representor 457 * @repr: pointer to repr structure 458 */ 459 void ice_repr_stop_tx_queues(struct ice_repr *repr) 460 { 461 netif_carrier_off(repr->netdev); 462 netif_tx_stop_all_queues(repr->netdev); 463 } 464 465 /** 466 * ice_repr_set_traffic_vsi - set traffic VSI for port representor 467 * @repr: repr on with VSI will be set 468 * @vsi: pointer to VSI that will be used by port representor to pass traffic 469 */ 470 void ice_repr_set_traffic_vsi(struct ice_repr *repr, struct ice_vsi *vsi) 471 { 472 struct ice_netdev_priv *np = netdev_priv(repr->netdev); 473 474 np->vsi = vsi; 475 } 476