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 16 #include "bnxt_hsi.h" 17 #include "bnxt.h" 18 #include "bnxt_vfr.h" 19 #include "bnxt_devlink.h" 20 #include "bnxt_tc.h" 21 22 #ifdef CONFIG_BNXT_SRIOV 23 24 #define CFA_HANDLE_INVALID 0xffff 25 #define VF_IDX_INVALID 0xffff 26 27 static int hwrm_cfa_vfr_alloc(struct bnxt *bp, u16 vf_idx, 28 u16 *tx_cfa_action, u16 *rx_cfa_code) 29 { 30 struct hwrm_cfa_vfr_alloc_output *resp = bp->hwrm_cmd_resp_addr; 31 struct hwrm_cfa_vfr_alloc_input req = { 0 }; 32 int rc; 33 34 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_VFR_ALLOC, -1, -1); 35 req.vf_id = cpu_to_le16(vf_idx); 36 sprintf(req.vfr_name, "vfr%d", vf_idx); 37 38 mutex_lock(&bp->hwrm_cmd_lock); 39 rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT); 40 if (!rc) { 41 *tx_cfa_action = le16_to_cpu(resp->tx_cfa_action); 42 *rx_cfa_code = le16_to_cpu(resp->rx_cfa_code); 43 netdev_dbg(bp->dev, "tx_cfa_action=0x%x, rx_cfa_code=0x%x", 44 *tx_cfa_action, *rx_cfa_code); 45 } else { 46 netdev_info(bp->dev, "%s error rc=%d", __func__, rc); 47 } 48 49 mutex_unlock(&bp->hwrm_cmd_lock); 50 return rc; 51 } 52 53 static int hwrm_cfa_vfr_free(struct bnxt *bp, u16 vf_idx) 54 { 55 struct hwrm_cfa_vfr_free_input req = { 0 }; 56 int rc; 57 58 bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_VFR_FREE, -1, -1); 59 sprintf(req.vfr_name, "vfr%d", vf_idx); 60 61 rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT); 62 if (rc) 63 netdev_info(bp->dev, "%s error rc=%d", __func__, rc); 64 return rc; 65 } 66 67 static int bnxt_vf_rep_open(struct net_device *dev) 68 { 69 struct bnxt_vf_rep *vf_rep = netdev_priv(dev); 70 struct bnxt *bp = vf_rep->bp; 71 72 /* Enable link and TX only if the parent PF is open. */ 73 if (netif_running(bp->dev)) { 74 netif_carrier_on(dev); 75 netif_tx_start_all_queues(dev); 76 } 77 return 0; 78 } 79 80 static int bnxt_vf_rep_close(struct net_device *dev) 81 { 82 netif_carrier_off(dev); 83 netif_tx_disable(dev); 84 85 return 0; 86 } 87 88 static netdev_tx_t bnxt_vf_rep_xmit(struct sk_buff *skb, 89 struct net_device *dev) 90 { 91 struct bnxt_vf_rep *vf_rep = netdev_priv(dev); 92 int rc, len = skb->len; 93 94 skb_dst_drop(skb); 95 dst_hold((struct dst_entry *)vf_rep->dst); 96 skb_dst_set(skb, (struct dst_entry *)vf_rep->dst); 97 skb->dev = vf_rep->dst->u.port_info.lower_dev; 98 99 rc = dev_queue_xmit(skb); 100 if (!rc) { 101 vf_rep->tx_stats.packets++; 102 vf_rep->tx_stats.bytes += len; 103 } 104 return rc; 105 } 106 107 static void 108 bnxt_vf_rep_get_stats64(struct net_device *dev, 109 struct rtnl_link_stats64 *stats) 110 { 111 struct bnxt_vf_rep *vf_rep = netdev_priv(dev); 112 113 stats->rx_packets = vf_rep->rx_stats.packets; 114 stats->rx_bytes = vf_rep->rx_stats.bytes; 115 stats->tx_packets = vf_rep->tx_stats.packets; 116 stats->tx_bytes = vf_rep->tx_stats.bytes; 117 } 118 119 static int bnxt_vf_rep_setup_tc_block_cb(enum tc_setup_type type, 120 void *type_data, 121 void *cb_priv) 122 { 123 struct bnxt_vf_rep *vf_rep = cb_priv; 124 struct bnxt *bp = vf_rep->bp; 125 int vf_fid = bp->pf.vf[vf_rep->vf_idx].fw_fid; 126 127 if (!bnxt_tc_flower_enabled(vf_rep->bp) || 128 !tc_cls_can_offload_and_chain0(bp->dev, type_data)) 129 return -EOPNOTSUPP; 130 131 switch (type) { 132 case TC_SETUP_CLSFLOWER: 133 return bnxt_tc_setup_flower(bp, vf_fid, type_data); 134 default: 135 return -EOPNOTSUPP; 136 } 137 } 138 139 static int bnxt_vf_rep_setup_tc_block(struct net_device *dev, 140 struct tc_block_offload *f) 141 { 142 struct bnxt_vf_rep *vf_rep = netdev_priv(dev); 143 144 if (f->binder_type != TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS) 145 return -EOPNOTSUPP; 146 147 switch (f->command) { 148 case TC_BLOCK_BIND: 149 return tcf_block_cb_register(f->block, 150 bnxt_vf_rep_setup_tc_block_cb, 151 vf_rep, vf_rep); 152 case TC_BLOCK_UNBIND: 153 tcf_block_cb_unregister(f->block, 154 bnxt_vf_rep_setup_tc_block_cb, vf_rep); 155 return 0; 156 default: 157 return -EOPNOTSUPP; 158 } 159 } 160 161 static int bnxt_vf_rep_setup_tc(struct net_device *dev, enum tc_setup_type type, 162 void *type_data) 163 { 164 switch (type) { 165 case TC_SETUP_BLOCK: 166 return bnxt_vf_rep_setup_tc_block(dev, type_data); 167 default: 168 return -EOPNOTSUPP; 169 } 170 } 171 172 struct net_device *bnxt_get_vf_rep(struct bnxt *bp, u16 cfa_code) 173 { 174 u16 vf_idx; 175 176 if (cfa_code && bp->cfa_code_map && BNXT_PF(bp)) { 177 vf_idx = bp->cfa_code_map[cfa_code]; 178 if (vf_idx != VF_IDX_INVALID) 179 return bp->vf_reps[vf_idx]->dev; 180 } 181 return NULL; 182 } 183 184 void bnxt_vf_rep_rx(struct bnxt *bp, struct sk_buff *skb) 185 { 186 struct bnxt_vf_rep *vf_rep = netdev_priv(skb->dev); 187 struct bnxt_vf_rep_stats *rx_stats; 188 189 rx_stats = &vf_rep->rx_stats; 190 vf_rep->rx_stats.bytes += skb->len; 191 vf_rep->rx_stats.packets++; 192 193 netif_receive_skb(skb); 194 } 195 196 static int bnxt_vf_rep_get_phys_port_name(struct net_device *dev, char *buf, 197 size_t len) 198 { 199 struct bnxt_vf_rep *vf_rep = netdev_priv(dev); 200 struct pci_dev *pf_pdev = vf_rep->bp->pdev; 201 int rc; 202 203 rc = snprintf(buf, len, "pf%dvf%d", PCI_FUNC(pf_pdev->devfn), 204 vf_rep->vf_idx); 205 if (rc >= len) 206 return -EOPNOTSUPP; 207 return 0; 208 } 209 210 static void bnxt_vf_rep_get_drvinfo(struct net_device *dev, 211 struct ethtool_drvinfo *info) 212 { 213 strlcpy(info->driver, DRV_MODULE_NAME, sizeof(info->driver)); 214 strlcpy(info->version, DRV_MODULE_VERSION, sizeof(info->version)); 215 } 216 217 static int bnxt_vf_rep_port_attr_get(struct net_device *dev, 218 struct switchdev_attr *attr) 219 { 220 struct bnxt_vf_rep *vf_rep = netdev_priv(dev); 221 222 /* as only PORT_PARENT_ID is supported currently use common code 223 * between PF and VF-rep for now. 224 */ 225 return bnxt_port_attr_get(vf_rep->bp, attr); 226 } 227 228 static const struct switchdev_ops bnxt_vf_rep_switchdev_ops = { 229 .switchdev_port_attr_get = bnxt_vf_rep_port_attr_get 230 }; 231 232 static const struct ethtool_ops bnxt_vf_rep_ethtool_ops = { 233 .get_drvinfo = bnxt_vf_rep_get_drvinfo 234 }; 235 236 static const struct net_device_ops bnxt_vf_rep_netdev_ops = { 237 .ndo_open = bnxt_vf_rep_open, 238 .ndo_stop = bnxt_vf_rep_close, 239 .ndo_start_xmit = bnxt_vf_rep_xmit, 240 .ndo_get_stats64 = bnxt_vf_rep_get_stats64, 241 .ndo_setup_tc = bnxt_vf_rep_setup_tc, 242 .ndo_get_phys_port_name = bnxt_vf_rep_get_phys_port_name 243 }; 244 245 bool bnxt_dev_is_vf_rep(struct net_device *dev) 246 { 247 return dev->netdev_ops == &bnxt_vf_rep_netdev_ops; 248 } 249 250 /* Called when the parent PF interface is closed: 251 * As the mode transition from SWITCHDEV to LEGACY 252 * happens under the rtnl_lock() this routine is safe 253 * under the rtnl_lock() 254 */ 255 void bnxt_vf_reps_close(struct bnxt *bp) 256 { 257 struct bnxt_vf_rep *vf_rep; 258 u16 num_vfs, i; 259 260 if (bp->eswitch_mode != DEVLINK_ESWITCH_MODE_SWITCHDEV) 261 return; 262 263 num_vfs = pci_num_vf(bp->pdev); 264 for (i = 0; i < num_vfs; i++) { 265 vf_rep = bp->vf_reps[i]; 266 if (netif_running(vf_rep->dev)) 267 bnxt_vf_rep_close(vf_rep->dev); 268 } 269 } 270 271 /* Called when the parent PF interface is opened (re-opened): 272 * As the mode transition from SWITCHDEV to LEGACY 273 * happen under the rtnl_lock() this routine is safe 274 * under the rtnl_lock() 275 */ 276 void bnxt_vf_reps_open(struct bnxt *bp) 277 { 278 int i; 279 280 if (bp->eswitch_mode != DEVLINK_ESWITCH_MODE_SWITCHDEV) 281 return; 282 283 for (i = 0; i < pci_num_vf(bp->pdev); i++) 284 bnxt_vf_rep_open(bp->vf_reps[i]->dev); 285 } 286 287 static void __bnxt_vf_reps_destroy(struct bnxt *bp) 288 { 289 u16 num_vfs = pci_num_vf(bp->pdev); 290 struct bnxt_vf_rep *vf_rep; 291 int i; 292 293 for (i = 0; i < num_vfs; i++) { 294 vf_rep = bp->vf_reps[i]; 295 if (vf_rep) { 296 dst_release((struct dst_entry *)vf_rep->dst); 297 298 if (vf_rep->tx_cfa_action != CFA_HANDLE_INVALID) 299 hwrm_cfa_vfr_free(bp, vf_rep->vf_idx); 300 301 if (vf_rep->dev) { 302 /* if register_netdev failed, then netdev_ops 303 * would have been set to NULL 304 */ 305 if (vf_rep->dev->netdev_ops) 306 unregister_netdev(vf_rep->dev); 307 free_netdev(vf_rep->dev); 308 } 309 } 310 } 311 312 kfree(bp->vf_reps); 313 bp->vf_reps = NULL; 314 } 315 316 void bnxt_vf_reps_destroy(struct bnxt *bp) 317 { 318 bool closed = false; 319 320 if (bp->eswitch_mode != DEVLINK_ESWITCH_MODE_SWITCHDEV) 321 return; 322 323 if (!bp->vf_reps) 324 return; 325 326 /* Ensure that parent PF's and VF-reps' RX/TX has been quiesced 327 * before proceeding with VF-rep cleanup. 328 */ 329 rtnl_lock(); 330 if (netif_running(bp->dev)) { 331 bnxt_close_nic(bp, false, false); 332 closed = true; 333 } 334 /* un-publish cfa_code_map so that RX path can't see it anymore */ 335 kfree(bp->cfa_code_map); 336 bp->cfa_code_map = NULL; 337 bp->eswitch_mode = DEVLINK_ESWITCH_MODE_LEGACY; 338 339 if (closed) 340 bnxt_open_nic(bp, false, false); 341 rtnl_unlock(); 342 343 /* Need to call vf_reps_destroy() outside of rntl_lock 344 * as unregister_netdev takes rtnl_lock 345 */ 346 __bnxt_vf_reps_destroy(bp); 347 } 348 349 /* Use the OUI of the PF's perm addr and report the same mac addr 350 * for the same VF-rep each time 351 */ 352 static void bnxt_vf_rep_eth_addr_gen(u8 *src_mac, u16 vf_idx, u8 *mac) 353 { 354 u32 addr; 355 356 ether_addr_copy(mac, src_mac); 357 358 addr = jhash(src_mac, ETH_ALEN, 0) + vf_idx; 359 mac[3] = (u8)(addr & 0xFF); 360 mac[4] = (u8)((addr >> 8) & 0xFF); 361 mac[5] = (u8)((addr >> 16) & 0xFF); 362 } 363 364 static void bnxt_vf_rep_netdev_init(struct bnxt *bp, struct bnxt_vf_rep *vf_rep, 365 struct net_device *dev) 366 { 367 struct net_device *pf_dev = bp->dev; 368 369 dev->netdev_ops = &bnxt_vf_rep_netdev_ops; 370 dev->ethtool_ops = &bnxt_vf_rep_ethtool_ops; 371 SWITCHDEV_SET_OPS(dev, &bnxt_vf_rep_switchdev_ops); 372 /* Just inherit all the featues of the parent PF as the VF-R 373 * uses the RX/TX rings of the parent PF 374 */ 375 dev->hw_features = pf_dev->hw_features; 376 dev->gso_partial_features = pf_dev->gso_partial_features; 377 dev->vlan_features = pf_dev->vlan_features; 378 dev->hw_enc_features = pf_dev->hw_enc_features; 379 dev->features |= pf_dev->features; 380 bnxt_vf_rep_eth_addr_gen(bp->pf.mac_addr, vf_rep->vf_idx, 381 dev->perm_addr); 382 ether_addr_copy(dev->dev_addr, dev->perm_addr); 383 } 384 385 static int bnxt_pcie_dsn_get(struct bnxt *bp, u8 dsn[]) 386 { 387 struct pci_dev *pdev = bp->pdev; 388 int pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_DSN); 389 u32 dw; 390 391 if (!pos) { 392 netdev_info(bp->dev, "Unable do read adapter's DSN"); 393 return -EOPNOTSUPP; 394 } 395 396 /* DSN (two dw) is at an offset of 4 from the cap pos */ 397 pos += 4; 398 pci_read_config_dword(pdev, pos, &dw); 399 put_unaligned_le32(dw, &dsn[0]); 400 pci_read_config_dword(pdev, pos + 4, &dw); 401 put_unaligned_le32(dw, &dsn[4]); 402 return 0; 403 } 404 405 static int bnxt_vf_reps_create(struct bnxt *bp) 406 { 407 u16 *cfa_code_map = NULL, num_vfs = pci_num_vf(bp->pdev); 408 struct bnxt_vf_rep *vf_rep; 409 struct net_device *dev; 410 int rc, i; 411 412 bp->vf_reps = kcalloc(num_vfs, sizeof(vf_rep), GFP_KERNEL); 413 if (!bp->vf_reps) 414 return -ENOMEM; 415 416 /* storage for cfa_code to vf-idx mapping */ 417 cfa_code_map = kmalloc(sizeof(*bp->cfa_code_map) * MAX_CFA_CODE, 418 GFP_KERNEL); 419 if (!cfa_code_map) { 420 rc = -ENOMEM; 421 goto err; 422 } 423 for (i = 0; i < MAX_CFA_CODE; i++) 424 cfa_code_map[i] = VF_IDX_INVALID; 425 426 for (i = 0; i < num_vfs; i++) { 427 dev = alloc_etherdev(sizeof(*vf_rep)); 428 if (!dev) { 429 rc = -ENOMEM; 430 goto err; 431 } 432 433 vf_rep = netdev_priv(dev); 434 bp->vf_reps[i] = vf_rep; 435 vf_rep->dev = dev; 436 vf_rep->bp = bp; 437 vf_rep->vf_idx = i; 438 vf_rep->tx_cfa_action = CFA_HANDLE_INVALID; 439 440 /* get cfa handles from FW */ 441 rc = hwrm_cfa_vfr_alloc(bp, vf_rep->vf_idx, 442 &vf_rep->tx_cfa_action, 443 &vf_rep->rx_cfa_code); 444 if (rc) { 445 rc = -ENOLINK; 446 goto err; 447 } 448 cfa_code_map[vf_rep->rx_cfa_code] = vf_rep->vf_idx; 449 450 vf_rep->dst = metadata_dst_alloc(0, METADATA_HW_PORT_MUX, 451 GFP_KERNEL); 452 if (!vf_rep->dst) { 453 rc = -ENOMEM; 454 goto err; 455 } 456 /* only cfa_action is needed to mux a packet while TXing */ 457 vf_rep->dst->u.port_info.port_id = vf_rep->tx_cfa_action; 458 vf_rep->dst->u.port_info.lower_dev = bp->dev; 459 460 bnxt_vf_rep_netdev_init(bp, vf_rep, dev); 461 rc = register_netdev(dev); 462 if (rc) { 463 /* no need for unregister_netdev in cleanup */ 464 dev->netdev_ops = NULL; 465 goto err; 466 } 467 } 468 469 /* Read the adapter's DSN to use as the eswitch switch_id */ 470 rc = bnxt_pcie_dsn_get(bp, bp->switch_id); 471 if (rc) 472 goto err; 473 474 /* publish cfa_code_map only after all VF-reps have been initialized */ 475 bp->cfa_code_map = cfa_code_map; 476 bp->eswitch_mode = DEVLINK_ESWITCH_MODE_SWITCHDEV; 477 netif_keep_dst(bp->dev); 478 return 0; 479 480 err: 481 netdev_info(bp->dev, "%s error=%d", __func__, rc); 482 kfree(cfa_code_map); 483 __bnxt_vf_reps_destroy(bp); 484 return rc; 485 } 486 487 /* Devlink related routines */ 488 int bnxt_dl_eswitch_mode_get(struct devlink *devlink, u16 *mode) 489 { 490 struct bnxt *bp = bnxt_get_bp_from_dl(devlink); 491 492 *mode = bp->eswitch_mode; 493 return 0; 494 } 495 496 int bnxt_dl_eswitch_mode_set(struct devlink *devlink, u16 mode) 497 { 498 struct bnxt *bp = bnxt_get_bp_from_dl(devlink); 499 int rc = 0; 500 501 mutex_lock(&bp->sriov_lock); 502 if (bp->eswitch_mode == mode) { 503 netdev_info(bp->dev, "already in %s eswitch mode", 504 mode == DEVLINK_ESWITCH_MODE_LEGACY ? 505 "legacy" : "switchdev"); 506 rc = -EINVAL; 507 goto done; 508 } 509 510 switch (mode) { 511 case DEVLINK_ESWITCH_MODE_LEGACY: 512 bnxt_vf_reps_destroy(bp); 513 break; 514 515 case DEVLINK_ESWITCH_MODE_SWITCHDEV: 516 if (pci_num_vf(bp->pdev) == 0) { 517 netdev_info(bp->dev, 518 "Enable VFs before setting switchdev mode"); 519 rc = -EPERM; 520 goto done; 521 } 522 rc = bnxt_vf_reps_create(bp); 523 break; 524 525 default: 526 rc = -EINVAL; 527 goto done; 528 } 529 done: 530 mutex_unlock(&bp->sriov_lock); 531 return rc; 532 } 533 534 #endif 535