0001
0002
0003
0004 #include <linux/bitfield.h>
0005 #include <linux/bitmap.h>
0006 #include <linux/etherdevice.h>
0007 #include <linux/lockdep.h>
0008 #include <linux/netdevice.h>
0009 #include <linux/rcupdate.h>
0010 #include <linux/rtnetlink.h>
0011 #include <linux/slab.h>
0012
0013 #include "../nfpcore/nfp.h"
0014 #include "../nfpcore/nfp_cpp.h"
0015 #include "../nfpcore/nfp_nsp.h"
0016 #include "../nfp_app.h"
0017 #include "../nfp_main.h"
0018 #include "../nfp_net.h"
0019 #include "../nfp_net_repr.h"
0020 #include "../nfp_port.h"
0021 #include "main.h"
0022
0023 static u32 nfp_abm_portid(enum nfp_repr_type rtype, unsigned int id)
0024 {
0025 return FIELD_PREP(NFP_ABM_PORTID_TYPE, rtype) |
0026 FIELD_PREP(NFP_ABM_PORTID_ID, id);
0027 }
0028
0029 static int
0030 nfp_abm_setup_tc(struct nfp_app *app, struct net_device *netdev,
0031 enum tc_setup_type type, void *type_data)
0032 {
0033 struct nfp_repr *repr = netdev_priv(netdev);
0034 struct nfp_port *port;
0035
0036 port = nfp_port_from_netdev(netdev);
0037 if (!port || port->type != NFP_PORT_PF_PORT)
0038 return -EOPNOTSUPP;
0039
0040 switch (type) {
0041 case TC_SETUP_ROOT_QDISC:
0042 return nfp_abm_setup_root(netdev, repr->app_priv, type_data);
0043 case TC_SETUP_QDISC_MQ:
0044 return nfp_abm_setup_tc_mq(netdev, repr->app_priv, type_data);
0045 case TC_SETUP_QDISC_RED:
0046 return nfp_abm_setup_tc_red(netdev, repr->app_priv, type_data);
0047 case TC_SETUP_QDISC_GRED:
0048 return nfp_abm_setup_tc_gred(netdev, repr->app_priv, type_data);
0049 case TC_SETUP_BLOCK:
0050 return nfp_abm_setup_cls_block(netdev, repr, type_data);
0051 default:
0052 return -EOPNOTSUPP;
0053 }
0054 }
0055
0056 static struct net_device *
0057 nfp_abm_repr_get(struct nfp_app *app, u32 port_id, bool *redir_egress)
0058 {
0059 enum nfp_repr_type rtype;
0060 struct nfp_reprs *reprs;
0061 u8 port;
0062
0063 rtype = FIELD_GET(NFP_ABM_PORTID_TYPE, port_id);
0064 port = FIELD_GET(NFP_ABM_PORTID_ID, port_id);
0065
0066 reprs = rcu_dereference(app->reprs[rtype]);
0067 if (!reprs)
0068 return NULL;
0069
0070 if (port >= reprs->num_reprs)
0071 return NULL;
0072
0073 return rcu_dereference(reprs->reprs[port]);
0074 }
0075
0076 static int
0077 nfp_abm_spawn_repr(struct nfp_app *app, struct nfp_abm_link *alink,
0078 enum nfp_port_type ptype)
0079 {
0080 struct net_device *netdev;
0081 enum nfp_repr_type rtype;
0082 struct nfp_reprs *reprs;
0083 struct nfp_repr *repr;
0084 struct nfp_port *port;
0085 unsigned int txqs;
0086 int err;
0087
0088 if (ptype == NFP_PORT_PHYS_PORT) {
0089 rtype = NFP_REPR_TYPE_PHYS_PORT;
0090 txqs = 1;
0091 } else {
0092 rtype = NFP_REPR_TYPE_PF;
0093 txqs = alink->vnic->max_rx_rings;
0094 }
0095
0096 netdev = nfp_repr_alloc_mqs(app, txqs, 1);
0097 if (!netdev)
0098 return -ENOMEM;
0099 repr = netdev_priv(netdev);
0100 repr->app_priv = alink;
0101
0102 port = nfp_port_alloc(app, ptype, netdev);
0103 if (IS_ERR(port)) {
0104 err = PTR_ERR(port);
0105 goto err_free_repr;
0106 }
0107
0108 if (ptype == NFP_PORT_PHYS_PORT) {
0109 port->eth_forced = true;
0110 err = nfp_port_init_phy_port(app->pf, app, port, alink->id);
0111 if (err)
0112 goto err_free_port;
0113 } else {
0114 port->pf_id = alink->abm->pf_id;
0115 port->pf_split = app->pf->max_data_vnics > 1;
0116 port->pf_split_id = alink->id;
0117 port->vnic = alink->vnic->dp.ctrl_bar;
0118 }
0119
0120 SET_NETDEV_DEV(netdev, &alink->vnic->pdev->dev);
0121 eth_hw_addr_random(netdev);
0122
0123 err = nfp_repr_init(app, netdev, nfp_abm_portid(rtype, alink->id),
0124 port, alink->vnic->dp.netdev);
0125 if (err)
0126 goto err_free_port;
0127
0128 reprs = nfp_reprs_get_locked(app, rtype);
0129 WARN(nfp_repr_get_locked(app, reprs, alink->id), "duplicate repr");
0130 rtnl_lock();
0131 rcu_assign_pointer(reprs->reprs[alink->id], netdev);
0132 rtnl_unlock();
0133
0134 nfp_info(app->cpp, "%s Port %d Representor(%s) created\n",
0135 ptype == NFP_PORT_PF_PORT ? "PCIe" : "Phys",
0136 alink->id, netdev->name);
0137
0138 return 0;
0139
0140 err_free_port:
0141 nfp_port_free(port);
0142 err_free_repr:
0143 nfp_repr_free(netdev);
0144 return err;
0145 }
0146
0147 static void
0148 nfp_abm_kill_repr(struct nfp_app *app, struct nfp_abm_link *alink,
0149 enum nfp_repr_type rtype)
0150 {
0151 struct net_device *netdev;
0152 struct nfp_reprs *reprs;
0153
0154 reprs = nfp_reprs_get_locked(app, rtype);
0155 netdev = nfp_repr_get_locked(app, reprs, alink->id);
0156 if (!netdev)
0157 return;
0158 rtnl_lock();
0159 rcu_assign_pointer(reprs->reprs[alink->id], NULL);
0160 rtnl_unlock();
0161 synchronize_rcu();
0162
0163 nfp_repr_clean_and_free((struct nfp_repr *)netdev_priv(netdev));
0164 }
0165
0166 static void
0167 nfp_abm_kill_reprs(struct nfp_abm *abm, struct nfp_abm_link *alink)
0168 {
0169 nfp_abm_kill_repr(abm->app, alink, NFP_REPR_TYPE_PF);
0170 nfp_abm_kill_repr(abm->app, alink, NFP_REPR_TYPE_PHYS_PORT);
0171 }
0172
0173 static void nfp_abm_kill_reprs_all(struct nfp_abm *abm)
0174 {
0175 struct nfp_pf *pf = abm->app->pf;
0176 struct nfp_net *nn;
0177
0178 list_for_each_entry(nn, &pf->vnics, vnic_list)
0179 nfp_abm_kill_reprs(abm, (struct nfp_abm_link *)nn->app_priv);
0180 }
0181
0182 static enum devlink_eswitch_mode nfp_abm_eswitch_mode_get(struct nfp_app *app)
0183 {
0184 struct nfp_abm *abm = app->priv;
0185
0186 return abm->eswitch_mode;
0187 }
0188
0189 static int nfp_abm_eswitch_set_legacy(struct nfp_abm *abm)
0190 {
0191 nfp_abm_kill_reprs_all(abm);
0192 nfp_abm_ctrl_qm_disable(abm);
0193
0194 abm->eswitch_mode = DEVLINK_ESWITCH_MODE_LEGACY;
0195 return 0;
0196 }
0197
0198 static void nfp_abm_eswitch_clean_up(struct nfp_abm *abm)
0199 {
0200 if (abm->eswitch_mode != DEVLINK_ESWITCH_MODE_LEGACY)
0201 WARN_ON(nfp_abm_eswitch_set_legacy(abm));
0202 }
0203
0204 static int nfp_abm_eswitch_set_switchdev(struct nfp_abm *abm)
0205 {
0206 struct nfp_app *app = abm->app;
0207 struct nfp_pf *pf = app->pf;
0208 struct nfp_net *nn;
0209 int err;
0210
0211 if (!abm->red_support)
0212 return -EOPNOTSUPP;
0213
0214 err = nfp_abm_ctrl_qm_enable(abm);
0215 if (err)
0216 return err;
0217
0218 list_for_each_entry(nn, &pf->vnics, vnic_list) {
0219 struct nfp_abm_link *alink = nn->app_priv;
0220
0221 err = nfp_abm_spawn_repr(app, alink, NFP_PORT_PHYS_PORT);
0222 if (err)
0223 goto err_kill_all_reprs;
0224
0225 err = nfp_abm_spawn_repr(app, alink, NFP_PORT_PF_PORT);
0226 if (err)
0227 goto err_kill_all_reprs;
0228 }
0229
0230 abm->eswitch_mode = DEVLINK_ESWITCH_MODE_SWITCHDEV;
0231 return 0;
0232
0233 err_kill_all_reprs:
0234 nfp_abm_kill_reprs_all(abm);
0235 nfp_abm_ctrl_qm_disable(abm);
0236 return err;
0237 }
0238
0239 static int nfp_abm_eswitch_mode_set(struct nfp_app *app, u16 mode)
0240 {
0241 struct nfp_abm *abm = app->priv;
0242
0243 if (abm->eswitch_mode == mode)
0244 return 0;
0245
0246 switch (mode) {
0247 case DEVLINK_ESWITCH_MODE_LEGACY:
0248 return nfp_abm_eswitch_set_legacy(abm);
0249 case DEVLINK_ESWITCH_MODE_SWITCHDEV:
0250 return nfp_abm_eswitch_set_switchdev(abm);
0251 default:
0252 return -EINVAL;
0253 }
0254 }
0255
0256 static void
0257 nfp_abm_vnic_set_mac(struct nfp_pf *pf, struct nfp_abm *abm, struct nfp_net *nn,
0258 unsigned int id)
0259 {
0260 struct nfp_eth_table_port *eth_port = &pf->eth_tbl->ports[id];
0261 u8 mac_addr[ETH_ALEN];
0262 struct nfp_nsp *nsp;
0263 char hwinfo[32];
0264 int err;
0265
0266 if (id > pf->eth_tbl->count) {
0267 nfp_warn(pf->cpp, "No entry for persistent MAC address\n");
0268 eth_hw_addr_random(nn->dp.netdev);
0269 return;
0270 }
0271
0272 snprintf(hwinfo, sizeof(hwinfo), "eth%u.mac.pf%u",
0273 eth_port->eth_index, abm->pf_id);
0274
0275 nsp = nfp_nsp_open(pf->cpp);
0276 if (IS_ERR(nsp)) {
0277 nfp_warn(pf->cpp, "Failed to access the NSP for persistent MAC address: %ld\n",
0278 PTR_ERR(nsp));
0279 eth_hw_addr_random(nn->dp.netdev);
0280 return;
0281 }
0282
0283 if (!nfp_nsp_has_hwinfo_lookup(nsp)) {
0284 nfp_warn(pf->cpp, "NSP doesn't support PF MAC generation\n");
0285 eth_hw_addr_random(nn->dp.netdev);
0286 nfp_nsp_close(nsp);
0287 return;
0288 }
0289
0290 err = nfp_nsp_hwinfo_lookup(nsp, hwinfo, sizeof(hwinfo));
0291 nfp_nsp_close(nsp);
0292 if (err) {
0293 nfp_warn(pf->cpp, "Reading persistent MAC address failed: %d\n",
0294 err);
0295 eth_hw_addr_random(nn->dp.netdev);
0296 return;
0297 }
0298
0299 if (sscanf(hwinfo, "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
0300 &mac_addr[0], &mac_addr[1], &mac_addr[2],
0301 &mac_addr[3], &mac_addr[4], &mac_addr[5]) != 6) {
0302 nfp_warn(pf->cpp, "Can't parse persistent MAC address (%s)\n",
0303 hwinfo);
0304 eth_hw_addr_random(nn->dp.netdev);
0305 return;
0306 }
0307
0308 eth_hw_addr_set(nn->dp.netdev, mac_addr);
0309 ether_addr_copy(nn->dp.netdev->perm_addr, mac_addr);
0310 }
0311
0312 static int
0313 nfp_abm_vnic_alloc(struct nfp_app *app, struct nfp_net *nn, unsigned int id)
0314 {
0315 struct nfp_eth_table_port *eth_port = &app->pf->eth_tbl->ports[id];
0316 struct nfp_abm *abm = app->priv;
0317 struct nfp_abm_link *alink;
0318 int err;
0319
0320 alink = kzalloc(sizeof(*alink), GFP_KERNEL);
0321 if (!alink)
0322 return -ENOMEM;
0323 nn->app_priv = alink;
0324 alink->abm = abm;
0325 alink->vnic = nn;
0326 alink->id = id;
0327 alink->total_queues = alink->vnic->max_rx_rings;
0328
0329 INIT_LIST_HEAD(&alink->dscp_map);
0330
0331 err = nfp_abm_ctrl_read_params(alink);
0332 if (err)
0333 goto err_free_alink;
0334
0335 alink->prio_map = kzalloc(abm->prio_map_len, GFP_KERNEL);
0336 if (!alink->prio_map) {
0337 err = -ENOMEM;
0338 goto err_free_alink;
0339 }
0340
0341
0342
0343
0344 err = nfp_eth_set_configured(app->cpp, eth_port->index, true);
0345 if (err < 0)
0346 goto err_free_priomap;
0347
0348 netif_keep_dst(nn->dp.netdev);
0349
0350 nfp_abm_vnic_set_mac(app->pf, abm, nn, id);
0351 INIT_RADIX_TREE(&alink->qdiscs, GFP_KERNEL);
0352
0353 return 0;
0354
0355 err_free_priomap:
0356 kfree(alink->prio_map);
0357 err_free_alink:
0358 kfree(alink);
0359 return err;
0360 }
0361
0362 static void nfp_abm_vnic_free(struct nfp_app *app, struct nfp_net *nn)
0363 {
0364 struct nfp_abm_link *alink = nn->app_priv;
0365
0366 nfp_abm_kill_reprs(alink->abm, alink);
0367 WARN(!radix_tree_empty(&alink->qdiscs), "left over qdiscs\n");
0368 kfree(alink->prio_map);
0369 kfree(alink);
0370 }
0371
0372 static int nfp_abm_vnic_init(struct nfp_app *app, struct nfp_net *nn)
0373 {
0374 struct nfp_abm_link *alink = nn->app_priv;
0375
0376 if (nfp_abm_has_prio(alink->abm))
0377 return nfp_abm_ctrl_prio_map_update(alink, alink->prio_map);
0378 return 0;
0379 }
0380
0381 static u64 *
0382 nfp_abm_port_get_stats(struct nfp_app *app, struct nfp_port *port, u64 *data)
0383 {
0384 struct nfp_repr *repr = netdev_priv(port->netdev);
0385 struct nfp_abm_link *alink;
0386 unsigned int i;
0387
0388 if (port->type != NFP_PORT_PF_PORT)
0389 return data;
0390 alink = repr->app_priv;
0391 for (i = 0; i < alink->vnic->dp.num_r_vecs; i++) {
0392 *data++ = nfp_abm_ctrl_stat_non_sto(alink, i);
0393 *data++ = nfp_abm_ctrl_stat_sto(alink, i);
0394 }
0395 return data;
0396 }
0397
0398 static int
0399 nfp_abm_port_get_stats_count(struct nfp_app *app, struct nfp_port *port)
0400 {
0401 struct nfp_repr *repr = netdev_priv(port->netdev);
0402 struct nfp_abm_link *alink;
0403
0404 if (port->type != NFP_PORT_PF_PORT)
0405 return 0;
0406 alink = repr->app_priv;
0407 return alink->vnic->dp.num_r_vecs * 2;
0408 }
0409
0410 static u8 *
0411 nfp_abm_port_get_stats_strings(struct nfp_app *app, struct nfp_port *port,
0412 u8 *data)
0413 {
0414 struct nfp_repr *repr = netdev_priv(port->netdev);
0415 struct nfp_abm_link *alink;
0416 unsigned int i;
0417
0418 if (port->type != NFP_PORT_PF_PORT)
0419 return data;
0420 alink = repr->app_priv;
0421 for (i = 0; i < alink->vnic->dp.num_r_vecs; i++) {
0422 ethtool_sprintf(&data, "q%u_no_wait", i);
0423 ethtool_sprintf(&data, "q%u_delayed", i);
0424 }
0425 return data;
0426 }
0427
0428 static int nfp_abm_fw_init_reset(struct nfp_abm *abm)
0429 {
0430 unsigned int i;
0431
0432 if (!abm->red_support)
0433 return 0;
0434
0435 for (i = 0; i < abm->num_bands * NFP_NET_MAX_RX_RINGS; i++) {
0436 __nfp_abm_ctrl_set_q_lvl(abm, i, NFP_ABM_LVL_INFINITY);
0437 __nfp_abm_ctrl_set_q_act(abm, i, NFP_ABM_ACT_DROP);
0438 }
0439
0440 return nfp_abm_ctrl_qm_disable(abm);
0441 }
0442
0443 static int nfp_abm_init(struct nfp_app *app)
0444 {
0445 struct nfp_pf *pf = app->pf;
0446 struct nfp_reprs *reprs;
0447 struct nfp_abm *abm;
0448 int err;
0449
0450 if (!pf->eth_tbl) {
0451 nfp_err(pf->cpp, "ABM NIC requires ETH table\n");
0452 return -EINVAL;
0453 }
0454 if (pf->max_data_vnics != pf->eth_tbl->count) {
0455 nfp_err(pf->cpp, "ETH entries don't match vNICs (%d vs %d)\n",
0456 pf->max_data_vnics, pf->eth_tbl->count);
0457 return -EINVAL;
0458 }
0459 if (!pf->mac_stats_bar) {
0460 nfp_warn(app->cpp, "ABM NIC requires mac_stats symbol\n");
0461 return -EINVAL;
0462 }
0463
0464 abm = kzalloc(sizeof(*abm), GFP_KERNEL);
0465 if (!abm)
0466 return -ENOMEM;
0467 app->priv = abm;
0468 abm->app = app;
0469
0470 err = nfp_abm_ctrl_find_addrs(abm);
0471 if (err)
0472 goto err_free_abm;
0473
0474 err = -ENOMEM;
0475 abm->num_thresholds = array_size(abm->num_bands, NFP_NET_MAX_RX_RINGS);
0476 abm->threshold_undef = bitmap_zalloc(abm->num_thresholds, GFP_KERNEL);
0477 if (!abm->threshold_undef)
0478 goto err_free_abm;
0479
0480 abm->thresholds = kvcalloc(abm->num_thresholds,
0481 sizeof(*abm->thresholds), GFP_KERNEL);
0482 if (!abm->thresholds)
0483 goto err_free_thresh_umap;
0484
0485 abm->actions = kvcalloc(abm->num_thresholds, sizeof(*abm->actions),
0486 GFP_KERNEL);
0487 if (!abm->actions)
0488 goto err_free_thresh;
0489
0490
0491 err = nfp_abm_fw_init_reset(abm);
0492 if (err)
0493 goto err_free_act;
0494
0495 err = -ENOMEM;
0496 reprs = nfp_reprs_alloc(pf->max_data_vnics);
0497 if (!reprs)
0498 goto err_free_act;
0499 RCU_INIT_POINTER(app->reprs[NFP_REPR_TYPE_PHYS_PORT], reprs);
0500
0501 reprs = nfp_reprs_alloc(pf->max_data_vnics);
0502 if (!reprs)
0503 goto err_free_phys;
0504 RCU_INIT_POINTER(app->reprs[NFP_REPR_TYPE_PF], reprs);
0505
0506 return 0;
0507
0508 err_free_phys:
0509 nfp_reprs_clean_and_free_by_type(app, NFP_REPR_TYPE_PHYS_PORT);
0510 err_free_act:
0511 kvfree(abm->actions);
0512 err_free_thresh:
0513 kvfree(abm->thresholds);
0514 err_free_thresh_umap:
0515 bitmap_free(abm->threshold_undef);
0516 err_free_abm:
0517 kfree(abm);
0518 app->priv = NULL;
0519 return err;
0520 }
0521
0522 static void nfp_abm_clean(struct nfp_app *app)
0523 {
0524 struct nfp_abm *abm = app->priv;
0525
0526 nfp_abm_eswitch_clean_up(abm);
0527 nfp_reprs_clean_and_free_by_type(app, NFP_REPR_TYPE_PF);
0528 nfp_reprs_clean_and_free_by_type(app, NFP_REPR_TYPE_PHYS_PORT);
0529 bitmap_free(abm->threshold_undef);
0530 kvfree(abm->actions);
0531 kvfree(abm->thresholds);
0532 kfree(abm);
0533 app->priv = NULL;
0534 }
0535
0536 const struct nfp_app_type app_abm = {
0537 .id = NFP_APP_ACTIVE_BUFFER_MGMT_NIC,
0538 .name = "abm",
0539
0540 .init = nfp_abm_init,
0541 .clean = nfp_abm_clean,
0542
0543 .vnic_alloc = nfp_abm_vnic_alloc,
0544 .vnic_free = nfp_abm_vnic_free,
0545 .vnic_init = nfp_abm_vnic_init,
0546
0547 .port_get_stats = nfp_abm_port_get_stats,
0548 .port_get_stats_count = nfp_abm_port_get_stats_count,
0549 .port_get_stats_strings = nfp_abm_port_get_stats_strings,
0550
0551 .setup_tc = nfp_abm_setup_tc,
0552
0553 .eswitch_mode_get = nfp_abm_eswitch_mode_get,
0554 .eswitch_mode_set = nfp_abm_eswitch_mode_set,
0555
0556 .dev_get = nfp_abm_repr_get,
0557 };