1 /* Broadcom NetXtreme-C/E network driver. 2 * 3 * Copyright (c) 2016-2017 Broadcom Limited 4 * 5 * This program is free software; you can redistribute it and/or modify 6 * it under the terms of the GNU General Public License as published by 7 * the Free Software Foundation. 8 */ 9 #include <linux/pci.h> 10 #include <linux/netdevice.h> 11 #include <linux/etherdevice.h> 12 #include <linux/rtnetlink.h> 13 #include <linux/jhash.h> 14 #include <net/pkt_cls.h> 15 #include <linux/bnxt/hsi.h> 16 17 #include "bnxt.h" 18 #include "bnxt_hwrm.h" 19 #include "bnxt_vfr.h" 20 #include "bnxt_devlink.h" 21 #include "bnxt_tc.h" 22 23 #ifdef CONFIG_BNXT_SRIOV 24 25 #define CFA_HANDLE_INVALID 0xffff 26 #define VF_IDX_INVALID 0xffff 27 28 static int hwrm_cfa_vfr_alloc(struct bnxt *bp, u16 vf_idx, 29 u16 *tx_cfa_action, u16 *rx_cfa_code) 30 { 31 struct hwrm_cfa_vfr_alloc_output *resp; 32 struct hwrm_cfa_vfr_alloc_input *req; 33 int rc; 34 35 rc = hwrm_req_init(bp, req, HWRM_CFA_VFR_ALLOC); 36 if (!rc) { 37 req->vf_id = cpu_to_le16(vf_idx); 38 sprintf(req->vfr_name, "vfr%d", vf_idx); 39 40 resp = hwrm_req_hold(bp, req); 41 rc = hwrm_req_send(bp, req); 42 if (!rc) { 43 *tx_cfa_action = le16_to_cpu(resp->tx_cfa_action); 44 *rx_cfa_code = le16_to_cpu(resp->rx_cfa_code); 45 netdev_dbg(bp->dev, "tx_cfa_action=0x%x, rx_cfa_code=0x%x", 46 *tx_cfa_action, *rx_cfa_code); 47 } 48 hwrm_req_drop(bp, req); 49 } 50 if (rc) 51 netdev_info(bp->dev, "%s error rc=%d\n", __func__, rc); 52 return rc; 53 } 54 55 static int hwrm_cfa_vfr_free(struct bnxt *bp, u16 vf_idx) 56 { 57 struct hwrm_cfa_vfr_free_input *req; 58 int rc; 59 60 rc = hwrm_req_init(bp, req, HWRM_CFA_VFR_FREE); 61 if (!rc) { 62 sprintf(req->vfr_name, "vfr%d", vf_idx); 63 rc = hwrm_req_send(bp, req); 64 } 65 if (rc) 66 netdev_info(bp->dev, "%s error rc=%d\n", __func__, rc); 67 return rc; 68 } 69 70 static int bnxt_hwrm_vfr_qcfg(struct bnxt *bp, struct bnxt_vf_rep *vf_rep, 71 u16 *max_mtu) 72 { 73 struct hwrm_func_qcfg_output *resp; 74 struct hwrm_func_qcfg_input *req; 75 u16 mtu; 76 int rc; 77 78 rc = hwrm_req_init(bp, req, HWRM_FUNC_QCFG); 79 if (rc) 80 return rc; 81 82 req->fid = cpu_to_le16(bp->pf.vf[vf_rep->vf_idx].fw_fid); 83 resp = hwrm_req_hold(bp, req); 84 rc = hwrm_req_send(bp, req); 85 if (!rc) { 86 mtu = le16_to_cpu(resp->max_mtu_configured); 87 if (!mtu) 88 *max_mtu = BNXT_MAX_MTU; 89 else 90 *max_mtu = mtu; 91 } 92 hwrm_req_drop(bp, req); 93 return rc; 94 } 95 96 static int bnxt_vf_rep_open(struct net_device *dev) 97 { 98 struct bnxt_vf_rep *vf_rep = netdev_priv(dev); 99 struct bnxt *bp = vf_rep->bp; 100 101 /* Enable link and TX only if the parent PF is open. */ 102 if (netif_running(bp->dev)) { 103 netif_carrier_on(dev); 104 netif_tx_start_all_queues(dev); 105 } 106 return 0; 107 } 108 109 static int bnxt_vf_rep_close(struct net_device *dev) 110 { 111 netif_carrier_off(dev); 112 netif_tx_disable(dev); 113 114 return 0; 115 } 116 117 static netdev_tx_t bnxt_vf_rep_xmit(struct sk_buff *skb, 118 struct net_device *dev) 119 { 120 struct bnxt_vf_rep *vf_rep = netdev_priv(dev); 121 int rc, len = skb->len; 122 123 skb_dst_drop(skb); 124 dst_hold((struct dst_entry *)vf_rep->dst); 125 skb_dst_set(skb, (struct dst_entry *)vf_rep->dst); 126 skb->dev = vf_rep->dst->u.port_info.lower_dev; 127 128 rc = dev_queue_xmit(skb); 129 if (!rc) { 130 vf_rep->tx_stats.packets++; 131 vf_rep->tx_stats.bytes += len; 132 } 133 return rc; 134 } 135 136 static void 137 bnxt_vf_rep_get_stats64(struct net_device *dev, 138 struct rtnl_link_stats64 *stats) 139 { 140 struct bnxt_vf_rep *vf_rep = netdev_priv(dev); 141 142 stats->rx_packets = vf_rep->rx_stats.packets; 143 stats->rx_bytes = vf_rep->rx_stats.bytes; 144 stats->tx_packets = vf_rep->tx_stats.packets; 145 stats->tx_bytes = vf_rep->tx_stats.bytes; 146 } 147 148 static int bnxt_vf_rep_setup_tc_block_cb(enum tc_setup_type type, 149 void *type_data, 150 void *cb_priv) 151 { 152 struct bnxt_vf_rep *vf_rep = cb_priv; 153 struct bnxt *bp = vf_rep->bp; 154 int vf_fid = bp->pf.vf[vf_rep->vf_idx].fw_fid; 155 156 if (!bnxt_tc_flower_enabled(vf_rep->bp) || 157 !tc_cls_can_offload_and_chain0(bp->dev, type_data)) 158 return -EOPNOTSUPP; 159 160 switch (type) { 161 case TC_SETUP_CLSFLOWER: 162 return bnxt_tc_setup_flower(bp, vf_fid, type_data); 163 default: 164 return -EOPNOTSUPP; 165 } 166 } 167 168 static LIST_HEAD(bnxt_vf_block_cb_list); 169 170 static int bnxt_vf_rep_setup_tc(struct net_device *dev, enum tc_setup_type type, 171 void *type_data) 172 { 173 struct bnxt_vf_rep *vf_rep = netdev_priv(dev); 174 175 switch (type) { 176 case TC_SETUP_BLOCK: 177 return flow_block_cb_setup_simple(type_data, 178 &bnxt_vf_block_cb_list, 179 bnxt_vf_rep_setup_tc_block_cb, 180 vf_rep, vf_rep, true); 181 default: 182 return -EOPNOTSUPP; 183 } 184 } 185 186 struct net_device *bnxt_get_vf_rep(struct bnxt *bp, u16 cfa_code) 187 { 188 u16 vf_idx; 189 190 if (cfa_code && bp->cfa_code_map && BNXT_PF(bp)) { 191 vf_idx = bp->cfa_code_map[cfa_code]; 192 if (vf_idx != VF_IDX_INVALID) 193 return bp->vf_reps[vf_idx]->dev; 194 } 195 return NULL; 196 } 197 198 void bnxt_vf_rep_rx(struct bnxt *bp, struct sk_buff *skb) 199 { 200 struct bnxt_vf_rep *vf_rep = netdev_priv(skb->dev); 201 202 vf_rep->rx_stats.bytes += skb->len; 203 vf_rep->rx_stats.packets++; 204 205 netif_receive_skb(skb); 206 } 207 208 static int bnxt_vf_rep_get_phys_port_name(struct net_device *dev, char *buf, 209 size_t len) 210 { 211 struct bnxt_vf_rep *vf_rep = netdev_priv(dev); 212 struct pci_dev *pf_pdev = vf_rep->bp->pdev; 213 int rc; 214 215 rc = snprintf(buf, len, "pf%dvf%d", PCI_FUNC(pf_pdev->devfn), 216 vf_rep->vf_idx); 217 if (rc >= len) 218 return -EOPNOTSUPP; 219 return 0; 220 } 221 222 static void bnxt_vf_rep_get_drvinfo(struct net_device *dev, 223 struct ethtool_drvinfo *info) 224 { 225 strscpy(info->driver, DRV_MODULE_NAME, sizeof(info->driver)); 226 } 227 228 static int bnxt_vf_rep_get_port_parent_id(struct net_device *dev, 229 struct netdev_phys_item_id *ppid) 230 { 231 struct bnxt_vf_rep *vf_rep = netdev_priv(dev); 232 233 /* as only PORT_PARENT_ID is supported currently use common code 234 * between PF and VF-rep for now. 235 */ 236 return bnxt_get_port_parent_id(vf_rep->bp->dev, ppid); 237 } 238 239 static const struct ethtool_ops bnxt_vf_rep_ethtool_ops = { 240 .get_drvinfo = bnxt_vf_rep_get_drvinfo 241 }; 242 243 static const struct net_device_ops bnxt_vf_rep_netdev_ops = { 244 .ndo_open = bnxt_vf_rep_open, 245 .ndo_stop = bnxt_vf_rep_close, 246 .ndo_start_xmit = bnxt_vf_rep_xmit, 247 .ndo_get_stats64 = bnxt_vf_rep_get_stats64, 248 .ndo_setup_tc = bnxt_vf_rep_setup_tc, 249 .ndo_get_port_parent_id = bnxt_vf_rep_get_port_parent_id, 250 .ndo_get_phys_port_name = bnxt_vf_rep_get_phys_port_name 251 }; 252 253 bool bnxt_dev_is_vf_rep(struct net_device *dev) 254 { 255 return dev->netdev_ops == &bnxt_vf_rep_netdev_ops; 256 } 257 258 /* Called when the parent PF interface is closed: 259 * As the mode transition from SWITCHDEV to LEGACY 260 * happens under the netdev instance lock this routine is safe 261 */ 262 void bnxt_vf_reps_close(struct bnxt *bp) 263 { 264 struct bnxt_vf_rep *vf_rep; 265 u16 num_vfs, i; 266 267 if (bp->eswitch_mode != DEVLINK_ESWITCH_MODE_SWITCHDEV) 268 return; 269 270 num_vfs = pci_num_vf(bp->pdev); 271 for (i = 0; i < num_vfs; i++) { 272 vf_rep = bp->vf_reps[i]; 273 if (netif_running(vf_rep->dev)) 274 bnxt_vf_rep_close(vf_rep->dev); 275 } 276 } 277 278 /* Called when the parent PF interface is opened (re-opened): 279 * As the mode transition from SWITCHDEV to LEGACY 280 * happen under the netdev instance lock this routine is safe 281 */ 282 void bnxt_vf_reps_open(struct bnxt *bp) 283 { 284 int i; 285 286 if (bp->eswitch_mode != DEVLINK_ESWITCH_MODE_SWITCHDEV) 287 return; 288 289 for (i = 0; i < pci_num_vf(bp->pdev); i++) { 290 /* Open the VF-Rep only if it is allocated in the FW */ 291 if (bp->vf_reps[i]->tx_cfa_action != CFA_HANDLE_INVALID) 292 bnxt_vf_rep_open(bp->vf_reps[i]->dev); 293 } 294 } 295 296 static void __bnxt_free_one_vf_rep(struct bnxt *bp, struct bnxt_vf_rep *vf_rep) 297 { 298 if (!vf_rep) 299 return; 300 301 if (vf_rep->dst) { 302 dst_release((struct dst_entry *)vf_rep->dst); 303 vf_rep->dst = NULL; 304 } 305 if (vf_rep->tx_cfa_action != CFA_HANDLE_INVALID) { 306 hwrm_cfa_vfr_free(bp, vf_rep->vf_idx); 307 vf_rep->tx_cfa_action = CFA_HANDLE_INVALID; 308 } 309 } 310 311 static void __bnxt_vf_reps_destroy(struct bnxt *bp) 312 { 313 u16 num_vfs = pci_num_vf(bp->pdev); 314 struct bnxt_vf_rep *vf_rep; 315 int i; 316 317 for (i = 0; i < num_vfs; i++) { 318 vf_rep = bp->vf_reps[i]; 319 if (vf_rep) { 320 __bnxt_free_one_vf_rep(bp, vf_rep); 321 if (vf_rep->dev) { 322 /* if register_netdev failed, then netdev_ops 323 * would have been set to NULL 324 */ 325 if (vf_rep->dev->netdev_ops) 326 unregister_netdev(vf_rep->dev); 327 free_netdev(vf_rep->dev); 328 } 329 } 330 } 331 332 kfree(bp->vf_reps); 333 bp->vf_reps = NULL; 334 } 335 336 void bnxt_vf_reps_destroy(struct bnxt *bp) 337 { 338 bool closed = false; 339 340 if (bp->eswitch_mode != DEVLINK_ESWITCH_MODE_SWITCHDEV) 341 return; 342 343 if (!bp->vf_reps) 344 return; 345 346 /* Ensure that parent PF's and VF-reps' RX/TX has been quiesced 347 * before proceeding with VF-rep cleanup. 348 */ 349 netdev_lock(bp->dev); 350 if (netif_running(bp->dev)) { 351 bnxt_close_nic(bp, false, false); 352 closed = true; 353 } 354 /* un-publish cfa_code_map so that RX path can't see it anymore */ 355 kfree(bp->cfa_code_map); 356 bp->cfa_code_map = NULL; 357 358 if (closed) { 359 /* Temporarily set legacy mode to avoid re-opening 360 * representors and restore switchdev mode after that. 361 */ 362 bp->eswitch_mode = DEVLINK_ESWITCH_MODE_LEGACY; 363 bnxt_open_nic(bp, false, false); 364 bp->eswitch_mode = DEVLINK_ESWITCH_MODE_SWITCHDEV; 365 } 366 netdev_unlock(bp->dev); 367 368 /* Need to call vf_reps_destroy() outside of netdev instance lock 369 * as unregister_netdev takes it 370 */ 371 __bnxt_vf_reps_destroy(bp); 372 } 373 374 /* Free the VF-Reps in firmware, during firmware hot-reset processing. 375 * Note that the VF-Rep netdevs are still active (not unregistered) during 376 * this process. As the mode transition from SWITCHDEV to LEGACY happens 377 * under the netdev instance lock this routine is safe. 378 */ 379 void bnxt_vf_reps_free(struct bnxt *bp) 380 { 381 u16 num_vfs = pci_num_vf(bp->pdev); 382 int i; 383 384 if (bp->eswitch_mode != DEVLINK_ESWITCH_MODE_SWITCHDEV) 385 return; 386 387 for (i = 0; i < num_vfs; i++) 388 __bnxt_free_one_vf_rep(bp, bp->vf_reps[i]); 389 } 390 391 static int bnxt_alloc_vf_rep(struct bnxt *bp, struct bnxt_vf_rep *vf_rep, 392 u16 *cfa_code_map) 393 { 394 /* get cfa handles from FW */ 395 if (hwrm_cfa_vfr_alloc(bp, vf_rep->vf_idx, &vf_rep->tx_cfa_action, 396 &vf_rep->rx_cfa_code)) 397 return -ENOLINK; 398 399 cfa_code_map[vf_rep->rx_cfa_code] = vf_rep->vf_idx; 400 vf_rep->dst = metadata_dst_alloc(0, METADATA_HW_PORT_MUX, GFP_KERNEL); 401 if (!vf_rep->dst) 402 return -ENOMEM; 403 404 /* only cfa_action is needed to mux a packet while TXing */ 405 vf_rep->dst->u.port_info.port_id = vf_rep->tx_cfa_action; 406 vf_rep->dst->u.port_info.lower_dev = bp->dev; 407 408 return 0; 409 } 410 411 /* Allocate the VF-Reps in firmware, during firmware hot-reset processing. 412 * Note that the VF-Rep netdevs are still active (not unregistered) during 413 * this process. As the mode transition from SWITCHDEV to LEGACY happens 414 * under the netdev instance lock this routine is safe. 415 */ 416 int bnxt_vf_reps_alloc(struct bnxt *bp) 417 { 418 u16 *cfa_code_map = bp->cfa_code_map, num_vfs = pci_num_vf(bp->pdev); 419 struct bnxt_vf_rep *vf_rep; 420 int rc, i; 421 422 if (bp->eswitch_mode != DEVLINK_ESWITCH_MODE_SWITCHDEV) 423 return 0; 424 425 if (!cfa_code_map) 426 return -EINVAL; 427 428 for (i = 0; i < MAX_CFA_CODE; i++) 429 cfa_code_map[i] = VF_IDX_INVALID; 430 431 for (i = 0; i < num_vfs; i++) { 432 vf_rep = bp->vf_reps[i]; 433 vf_rep->vf_idx = i; 434 435 rc = bnxt_alloc_vf_rep(bp, vf_rep, cfa_code_map); 436 if (rc) 437 goto err; 438 } 439 440 return 0; 441 442 err: 443 netdev_info(bp->dev, "%s error=%d\n", __func__, rc); 444 bnxt_vf_reps_free(bp); 445 return rc; 446 } 447 448 /* Use the OUI of the PF's perm addr and report the same mac addr 449 * for the same VF-rep each time 450 */ 451 static void bnxt_vf_rep_eth_addr_gen(u8 *src_mac, u16 vf_idx, u8 *mac) 452 { 453 u32 addr; 454 455 ether_addr_copy(mac, src_mac); 456 457 addr = jhash(src_mac, ETH_ALEN, 0) + vf_idx; 458 mac[3] = (u8)(addr & 0xFF); 459 mac[4] = (u8)((addr >> 8) & 0xFF); 460 mac[5] = (u8)((addr >> 16) & 0xFF); 461 } 462 463 static void bnxt_vf_rep_netdev_init(struct bnxt *bp, struct bnxt_vf_rep *vf_rep, 464 struct net_device *dev) 465 { 466 struct net_device *pf_dev = bp->dev; 467 u16 max_mtu; 468 469 SET_NETDEV_DEV(dev, &bp->pdev->dev); 470 dev->netdev_ops = &bnxt_vf_rep_netdev_ops; 471 dev->ethtool_ops = &bnxt_vf_rep_ethtool_ops; 472 /* Just inherit all the featues of the parent PF as the VF-R 473 * uses the RX/TX rings of the parent PF 474 */ 475 dev->hw_features = pf_dev->hw_features; 476 dev->gso_partial_features = pf_dev->gso_partial_features; 477 dev->vlan_features = pf_dev->vlan_features; 478 dev->hw_enc_features = pf_dev->hw_enc_features; 479 dev->features |= pf_dev->features; 480 bnxt_vf_rep_eth_addr_gen(bp->pf.mac_addr, vf_rep->vf_idx, 481 dev->perm_addr); 482 eth_hw_addr_set(dev, dev->perm_addr); 483 /* Set VF-Rep's max-mtu to the corresponding VF's max-mtu */ 484 if (!bnxt_hwrm_vfr_qcfg(bp, vf_rep, &max_mtu)) 485 dev->max_mtu = max_mtu; 486 dev->min_mtu = ETH_ZLEN; 487 } 488 489 int bnxt_vf_reps_create(struct bnxt *bp) 490 { 491 u16 *cfa_code_map = NULL, num_vfs = pci_num_vf(bp->pdev); 492 struct bnxt_vf_rep *vf_rep; 493 struct net_device *dev; 494 int rc, i; 495 496 if (!(bp->flags & BNXT_FLAG_DSN_VALID)) 497 return -ENODEV; 498 499 bp->vf_reps = kcalloc(num_vfs, sizeof(vf_rep), GFP_KERNEL); 500 if (!bp->vf_reps) 501 return -ENOMEM; 502 503 /* storage for cfa_code to vf-idx mapping */ 504 cfa_code_map = kmalloc_array(MAX_CFA_CODE, sizeof(*bp->cfa_code_map), 505 GFP_KERNEL); 506 if (!cfa_code_map) { 507 rc = -ENOMEM; 508 goto err; 509 } 510 for (i = 0; i < MAX_CFA_CODE; i++) 511 cfa_code_map[i] = VF_IDX_INVALID; 512 513 for (i = 0; i < num_vfs; i++) { 514 dev = alloc_etherdev(sizeof(*vf_rep)); 515 if (!dev) { 516 rc = -ENOMEM; 517 goto err; 518 } 519 520 vf_rep = netdev_priv(dev); 521 bp->vf_reps[i] = vf_rep; 522 vf_rep->dev = dev; 523 vf_rep->bp = bp; 524 vf_rep->vf_idx = i; 525 vf_rep->tx_cfa_action = CFA_HANDLE_INVALID; 526 527 rc = bnxt_alloc_vf_rep(bp, vf_rep, cfa_code_map); 528 if (rc) 529 goto err; 530 531 bnxt_vf_rep_netdev_init(bp, vf_rep, dev); 532 rc = register_netdev(dev); 533 if (rc) { 534 /* no need for unregister_netdev in cleanup */ 535 dev->netdev_ops = NULL; 536 goto err; 537 } 538 } 539 540 /* publish cfa_code_map only after all VF-reps have been initialized */ 541 bp->cfa_code_map = cfa_code_map; 542 netif_keep_dst(bp->dev); 543 return 0; 544 545 err: 546 netdev_info(bp->dev, "%s error=%d\n", __func__, rc); 547 kfree(cfa_code_map); 548 __bnxt_vf_reps_destroy(bp); 549 return rc; 550 } 551 552 /* Devlink related routines */ 553 int bnxt_dl_eswitch_mode_get(struct devlink *devlink, u16 *mode) 554 { 555 struct bnxt *bp = bnxt_get_bp_from_dl(devlink); 556 557 *mode = bp->eswitch_mode; 558 return 0; 559 } 560 561 int bnxt_dl_eswitch_mode_set(struct devlink *devlink, u16 mode, 562 struct netlink_ext_ack *extack) 563 { 564 struct bnxt *bp = bnxt_get_bp_from_dl(devlink); 565 int ret = 0; 566 567 if (bp->eswitch_mode == mode) { 568 netdev_info(bp->dev, "already in %s eswitch mode\n", 569 mode == DEVLINK_ESWITCH_MODE_LEGACY ? 570 "legacy" : "switchdev"); 571 return -EINVAL; 572 } 573 574 switch (mode) { 575 case DEVLINK_ESWITCH_MODE_LEGACY: 576 bnxt_vf_reps_destroy(bp); 577 break; 578 579 case DEVLINK_ESWITCH_MODE_SWITCHDEV: 580 if (bp->hwrm_spec_code < 0x10803) { 581 netdev_warn(bp->dev, "FW does not support SRIOV E-Switch SWITCHDEV mode\n"); 582 return -ENOTSUPP; 583 } 584 585 /* Create representors for existing VFs */ 586 if (pci_num_vf(bp->pdev) > 0) 587 ret = bnxt_vf_reps_create(bp); 588 break; 589 590 default: 591 return -EINVAL; 592 } 593 594 if (!ret) 595 bp->eswitch_mode = mode; 596 597 return ret; 598 } 599 600 #endif 601