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_virtchnl_pf.h" 8 #include "ice_tc_lib.h" 9 10 /** 11 * ice_repr_get_sw_port_id - get port ID associated with representor 12 * @repr: pointer to port representor 13 */ 14 static int ice_repr_get_sw_port_id(struct ice_repr *repr) 15 { 16 return repr->vf->pf->hw.port_info->lport; 17 } 18 19 /** 20 * ice_repr_get_phys_port_name - get phys port name 21 * @netdev: pointer to port representor netdev 22 * @buf: write here port name 23 * @len: max length of buf 24 */ 25 static int 26 ice_repr_get_phys_port_name(struct net_device *netdev, char *buf, size_t len) 27 { 28 struct ice_netdev_priv *np = netdev_priv(netdev); 29 struct ice_repr *repr = np->repr; 30 int res; 31 32 /* Devlink port is registered and devlink core is taking care of name formatting. */ 33 if (repr->vf->devlink_port.devlink) 34 return -EOPNOTSUPP; 35 36 res = snprintf(buf, len, "pf%dvfr%d", ice_repr_get_sw_port_id(repr), 37 repr->vf->vf_id); 38 if (res <= 0) 39 return -EOPNOTSUPP; 40 return 0; 41 } 42 43 /** 44 * ice_repr_get_stats64 - get VF stats for VFPR use 45 * @netdev: pointer to port representor netdev 46 * @stats: pointer to struct where stats can be stored 47 */ 48 static void 49 ice_repr_get_stats64(struct net_device *netdev, struct rtnl_link_stats64 *stats) 50 { 51 struct ice_netdev_priv *np = netdev_priv(netdev); 52 struct ice_eth_stats *eth_stats; 53 struct ice_vsi *vsi; 54 55 if (ice_is_vf_disabled(np->repr->vf)) 56 return; 57 vsi = np->repr->src_vsi; 58 59 ice_update_vsi_stats(vsi); 60 eth_stats = &vsi->eth_stats; 61 62 stats->tx_packets = eth_stats->tx_unicast + eth_stats->tx_broadcast + 63 eth_stats->tx_multicast; 64 stats->rx_packets = eth_stats->rx_unicast + eth_stats->rx_broadcast + 65 eth_stats->rx_multicast; 66 stats->tx_bytes = eth_stats->tx_bytes; 67 stats->rx_bytes = eth_stats->rx_bytes; 68 stats->multicast = eth_stats->rx_multicast; 69 stats->tx_errors = eth_stats->tx_errors; 70 stats->tx_dropped = eth_stats->tx_discards; 71 stats->rx_dropped = eth_stats->rx_discards; 72 } 73 74 /** 75 * ice_netdev_to_repr - Get port representor for given netdevice 76 * @netdev: pointer to port representor netdev 77 */ 78 struct ice_repr *ice_netdev_to_repr(struct net_device *netdev) 79 { 80 struct ice_netdev_priv *np = netdev_priv(netdev); 81 82 return np->repr; 83 } 84 85 /** 86 * ice_repr_open - Enable port representor's network interface 87 * @netdev: network interface device structure 88 * 89 * The open entry point is called when a port representor's network 90 * interface is made active by the system (IFF_UP). Corresponding 91 * VF is notified about link status change. 92 * 93 * Returns 0 on success 94 */ 95 static int ice_repr_open(struct net_device *netdev) 96 { 97 struct ice_repr *repr = ice_netdev_to_repr(netdev); 98 struct ice_vf *vf; 99 100 vf = repr->vf; 101 vf->link_forced = true; 102 vf->link_up = true; 103 ice_vc_notify_vf_link_state(vf); 104 105 netif_carrier_on(netdev); 106 netif_tx_start_all_queues(netdev); 107 108 return 0; 109 } 110 111 /** 112 * ice_repr_stop - Disable port representor's network interface 113 * @netdev: network interface device structure 114 * 115 * The stop entry point is called when a port representor's network 116 * interface is de-activated by the system. Corresponding 117 * VF is notified about link status change. 118 * 119 * Returns 0 on success 120 */ 121 static int ice_repr_stop(struct net_device *netdev) 122 { 123 struct ice_repr *repr = ice_netdev_to_repr(netdev); 124 struct ice_vf *vf; 125 126 vf = repr->vf; 127 vf->link_forced = true; 128 vf->link_up = false; 129 ice_vc_notify_vf_link_state(vf); 130 131 netif_carrier_off(netdev); 132 netif_tx_stop_all_queues(netdev); 133 134 return 0; 135 } 136 137 static struct devlink_port * 138 ice_repr_get_devlink_port(struct net_device *netdev) 139 { 140 struct ice_repr *repr = ice_netdev_to_repr(netdev); 141 142 return &repr->vf->devlink_port; 143 } 144 145 static int 146 ice_repr_setup_tc_cls_flower(struct ice_repr *repr, 147 struct flow_cls_offload *flower) 148 { 149 switch (flower->command) { 150 case FLOW_CLS_REPLACE: 151 return ice_add_cls_flower(repr->netdev, repr->src_vsi, flower); 152 case FLOW_CLS_DESTROY: 153 return ice_del_cls_flower(repr->src_vsi, flower); 154 default: 155 return -EINVAL; 156 } 157 } 158 159 static int 160 ice_repr_setup_tc_block_cb(enum tc_setup_type type, void *type_data, 161 void *cb_priv) 162 { 163 struct flow_cls_offload *flower = (struct flow_cls_offload *)type_data; 164 struct ice_netdev_priv *np = (struct ice_netdev_priv *)cb_priv; 165 166 switch (type) { 167 case TC_SETUP_CLSFLOWER: 168 return ice_repr_setup_tc_cls_flower(np->repr, flower); 169 default: 170 return -EOPNOTSUPP; 171 } 172 } 173 174 static LIST_HEAD(ice_repr_block_cb_list); 175 176 static int 177 ice_repr_setup_tc(struct net_device *netdev, enum tc_setup_type type, 178 void *type_data) 179 { 180 struct ice_netdev_priv *np = netdev_priv(netdev); 181 182 switch (type) { 183 case TC_SETUP_BLOCK: 184 return flow_block_cb_setup_simple((struct flow_block_offload *) 185 type_data, 186 &ice_repr_block_cb_list, 187 ice_repr_setup_tc_block_cb, 188 np, np, true); 189 default: 190 return -EOPNOTSUPP; 191 } 192 } 193 194 static const struct net_device_ops ice_repr_netdev_ops = { 195 .ndo_get_phys_port_name = ice_repr_get_phys_port_name, 196 .ndo_get_stats64 = ice_repr_get_stats64, 197 .ndo_open = ice_repr_open, 198 .ndo_stop = ice_repr_stop, 199 .ndo_start_xmit = ice_eswitch_port_start_xmit, 200 .ndo_get_devlink_port = ice_repr_get_devlink_port, 201 .ndo_setup_tc = ice_repr_setup_tc, 202 }; 203 204 /** 205 * ice_is_port_repr_netdev - Check if a given netdevice is a port representor netdev 206 * @netdev: pointer to netdev 207 */ 208 bool ice_is_port_repr_netdev(struct net_device *netdev) 209 { 210 return netdev && (netdev->netdev_ops == &ice_repr_netdev_ops); 211 } 212 213 /** 214 * ice_repr_reg_netdev - register port representor netdev 215 * @netdev: pointer to port representor netdev 216 */ 217 static int 218 ice_repr_reg_netdev(struct net_device *netdev) 219 { 220 eth_hw_addr_random(netdev); 221 netdev->netdev_ops = &ice_repr_netdev_ops; 222 ice_set_ethtool_repr_ops(netdev); 223 224 netdev->hw_features |= NETIF_F_HW_TC; 225 226 netif_carrier_off(netdev); 227 netif_tx_stop_all_queues(netdev); 228 229 return register_netdev(netdev); 230 } 231 232 /** 233 * ice_repr_add - add representor for VF 234 * @vf: pointer to VF structure 235 */ 236 static int ice_repr_add(struct ice_vf *vf) 237 { 238 struct ice_q_vector *q_vector; 239 struct ice_netdev_priv *np; 240 struct ice_repr *repr; 241 int err; 242 243 repr = kzalloc(sizeof(*repr), GFP_KERNEL); 244 if (!repr) 245 return -ENOMEM; 246 247 #ifdef CONFIG_ICE_SWITCHDEV 248 repr->mac_rule = kzalloc(sizeof(*repr->mac_rule), GFP_KERNEL); 249 if (!repr->mac_rule) { 250 err = -ENOMEM; 251 goto err_alloc_rule; 252 } 253 #endif 254 255 repr->netdev = alloc_etherdev(sizeof(struct ice_netdev_priv)); 256 if (!repr->netdev) { 257 err = -ENOMEM; 258 goto err_alloc; 259 } 260 261 repr->src_vsi = ice_get_vf_vsi(vf); 262 repr->vf = vf; 263 vf->repr = repr; 264 np = netdev_priv(repr->netdev); 265 np->repr = repr; 266 267 q_vector = kzalloc(sizeof(*q_vector), GFP_KERNEL); 268 if (!q_vector) { 269 err = -ENOMEM; 270 goto err_alloc_q_vector; 271 } 272 repr->q_vector = q_vector; 273 274 err = ice_devlink_create_vf_port(vf); 275 if (err) 276 goto err_devlink; 277 278 repr->netdev->min_mtu = ETH_MIN_MTU; 279 repr->netdev->max_mtu = ICE_MAX_MTU; 280 281 err = ice_repr_reg_netdev(repr->netdev); 282 if (err) 283 goto err_netdev; 284 285 devlink_port_type_eth_set(&vf->devlink_port, repr->netdev); 286 287 return 0; 288 289 err_netdev: 290 ice_devlink_destroy_vf_port(vf); 291 err_devlink: 292 kfree(repr->q_vector); 293 vf->repr->q_vector = NULL; 294 err_alloc_q_vector: 295 free_netdev(repr->netdev); 296 repr->netdev = NULL; 297 err_alloc: 298 #ifdef CONFIG_ICE_SWITCHDEV 299 kfree(repr->mac_rule); 300 repr->mac_rule = NULL; 301 err_alloc_rule: 302 #endif 303 kfree(repr); 304 vf->repr = NULL; 305 return err; 306 } 307 308 /** 309 * ice_repr_rem - remove representor from VF 310 * @vf: pointer to VF structure 311 */ 312 static void ice_repr_rem(struct ice_vf *vf) 313 { 314 ice_devlink_destroy_vf_port(vf); 315 kfree(vf->repr->q_vector); 316 vf->repr->q_vector = NULL; 317 unregister_netdev(vf->repr->netdev); 318 free_netdev(vf->repr->netdev); 319 vf->repr->netdev = NULL; 320 #ifdef CONFIG_ICE_SWITCHDEV 321 kfree(vf->repr->mac_rule); 322 vf->repr->mac_rule = NULL; 323 #endif 324 kfree(vf->repr); 325 vf->repr = NULL; 326 } 327 328 /** 329 * ice_repr_add_for_all_vfs - add port representor for all VFs 330 * @pf: pointer to PF structure 331 */ 332 int ice_repr_add_for_all_vfs(struct ice_pf *pf) 333 { 334 int err; 335 int i; 336 337 ice_for_each_vf(pf, i) { 338 struct ice_vf *vf = &pf->vf[i]; 339 340 err = ice_repr_add(vf); 341 if (err) 342 goto err; 343 344 ice_vc_change_ops_to_repr(&vf->vc_ops); 345 } 346 347 return 0; 348 349 err: 350 for (i = i - 1; i >= 0; i--) { 351 struct ice_vf *vf = &pf->vf[i]; 352 353 ice_repr_rem(vf); 354 ice_vc_set_dflt_vf_ops(&vf->vc_ops); 355 } 356 357 return err; 358 } 359 360 /** 361 * ice_repr_rem_from_all_vfs - remove port representor for all VFs 362 * @pf: pointer to PF structure 363 */ 364 void ice_repr_rem_from_all_vfs(struct ice_pf *pf) 365 { 366 int i; 367 368 ice_for_each_vf(pf, i) { 369 struct ice_vf *vf = &pf->vf[i]; 370 371 ice_repr_rem(vf); 372 ice_vc_set_dflt_vf_ops(&vf->vc_ops); 373 } 374 } 375 376 /** 377 * ice_repr_start_tx_queues - start Tx queues of port representor 378 * @repr: pointer to repr structure 379 */ 380 void ice_repr_start_tx_queues(struct ice_repr *repr) 381 { 382 netif_carrier_on(repr->netdev); 383 netif_tx_start_all_queues(repr->netdev); 384 } 385 386 /** 387 * ice_repr_stop_tx_queues - stop Tx queues of port representor 388 * @repr: pointer to repr structure 389 */ 390 void ice_repr_stop_tx_queues(struct ice_repr *repr) 391 { 392 netif_carrier_off(repr->netdev); 393 netif_tx_stop_all_queues(repr->netdev); 394 } 395 396 /** 397 * ice_repr_set_traffic_vsi - set traffic VSI for port representor 398 * @repr: repr on with VSI will be set 399 * @vsi: pointer to VSI that will be used by port representor to pass traffic 400 */ 401 void ice_repr_set_traffic_vsi(struct ice_repr *repr, struct ice_vsi *vsi) 402 { 403 struct ice_netdev_priv *np = netdev_priv(repr->netdev); 404 405 np->vsi = vsi; 406 } 407