1 /* 2 * Copyright (c) 2018 Cumulus Networks. All rights reserved. 3 * Copyright (c) 2018 David Ahern <dsa@cumulusnetworks.com> 4 * Copyright (c) 2019 Mellanox Technologies. All rights reserved. 5 * 6 * This software is licensed under the GNU General License Version 2, 7 * June 1991 as shown in the file COPYING in the top-level directory of this 8 * source tree. 9 * 10 * THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" 11 * WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, 12 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 13 * FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE 14 * OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME 15 * THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 16 */ 17 18 #include <linux/debugfs.h> 19 #include <linux/device.h> 20 #include <linux/etherdevice.h> 21 #include <linux/inet.h> 22 #include <linux/jiffies.h> 23 #include <linux/kernel.h> 24 #include <linux/list.h> 25 #include <linux/mutex.h> 26 #include <linux/random.h> 27 #include <linux/rtnetlink.h> 28 #include <linux/workqueue.h> 29 #include <net/devlink.h> 30 #include <net/ip.h> 31 #include <net/flow_offload.h> 32 #include <uapi/linux/devlink.h> 33 #include <uapi/linux/ip.h> 34 #include <uapi/linux/udp.h> 35 36 #include "netdevsim.h" 37 38 static unsigned int 39 nsim_dev_port_index(enum nsim_dev_port_type type, unsigned int port_index) 40 { 41 switch (type) { 42 case NSIM_DEV_PORT_TYPE_VF: 43 port_index = NSIM_DEV_VF_PORT_INDEX_BASE + port_index; 44 break; 45 case NSIM_DEV_PORT_TYPE_PF: 46 break; 47 } 48 49 return port_index; 50 } 51 52 static inline unsigned int nsim_dev_port_index_to_vf_index(unsigned int port_index) 53 { 54 return port_index - NSIM_DEV_VF_PORT_INDEX_BASE; 55 } 56 57 static struct dentry *nsim_dev_ddir; 58 59 unsigned int nsim_dev_get_vfs(struct nsim_dev *nsim_dev) 60 { 61 WARN_ON(!lockdep_rtnl_is_held() && 62 !lockdep_is_held(&nsim_dev->vfs_lock)); 63 64 return nsim_dev->nsim_bus_dev->num_vfs; 65 } 66 67 static void 68 nsim_bus_dev_set_vfs(struct nsim_bus_dev *nsim_bus_dev, unsigned int num_vfs) 69 { 70 rtnl_lock(); 71 nsim_bus_dev->num_vfs = num_vfs; 72 rtnl_unlock(); 73 } 74 75 #define NSIM_DEV_DUMMY_REGION_SIZE (1024 * 32) 76 77 static int 78 nsim_dev_take_snapshot(struct devlink *devlink, 79 const struct devlink_region_ops *ops, 80 struct netlink_ext_ack *extack, 81 u8 **data) 82 { 83 void *dummy_data; 84 85 dummy_data = kmalloc(NSIM_DEV_DUMMY_REGION_SIZE, GFP_KERNEL); 86 if (!dummy_data) 87 return -ENOMEM; 88 89 get_random_bytes(dummy_data, NSIM_DEV_DUMMY_REGION_SIZE); 90 91 *data = dummy_data; 92 93 return 0; 94 } 95 96 static ssize_t nsim_dev_take_snapshot_write(struct file *file, 97 const char __user *data, 98 size_t count, loff_t *ppos) 99 { 100 struct nsim_dev *nsim_dev = file->private_data; 101 struct devlink *devlink; 102 u8 *dummy_data; 103 int err; 104 u32 id; 105 106 devlink = priv_to_devlink(nsim_dev); 107 108 err = nsim_dev_take_snapshot(devlink, NULL, NULL, &dummy_data); 109 if (err) 110 return err; 111 112 err = devlink_region_snapshot_id_get(devlink, &id); 113 if (err) { 114 pr_err("Failed to get snapshot id\n"); 115 kfree(dummy_data); 116 return err; 117 } 118 err = devlink_region_snapshot_create(nsim_dev->dummy_region, 119 dummy_data, id); 120 devlink_region_snapshot_id_put(devlink, id); 121 if (err) { 122 pr_err("Failed to create region snapshot\n"); 123 kfree(dummy_data); 124 return err; 125 } 126 127 return count; 128 } 129 130 static const struct file_operations nsim_dev_take_snapshot_fops = { 131 .open = simple_open, 132 .write = nsim_dev_take_snapshot_write, 133 .llseek = generic_file_llseek, 134 .owner = THIS_MODULE, 135 }; 136 137 static ssize_t nsim_dev_trap_fa_cookie_read(struct file *file, 138 char __user *data, 139 size_t count, loff_t *ppos) 140 { 141 struct nsim_dev *nsim_dev = file->private_data; 142 struct flow_action_cookie *fa_cookie; 143 unsigned int buf_len; 144 ssize_t ret; 145 char *buf; 146 147 spin_lock(&nsim_dev->fa_cookie_lock); 148 fa_cookie = nsim_dev->fa_cookie; 149 if (!fa_cookie) { 150 ret = -EINVAL; 151 goto errout; 152 } 153 buf_len = fa_cookie->cookie_len * 2; 154 buf = kmalloc(buf_len, GFP_ATOMIC); 155 if (!buf) { 156 ret = -ENOMEM; 157 goto errout; 158 } 159 bin2hex(buf, fa_cookie->cookie, fa_cookie->cookie_len); 160 spin_unlock(&nsim_dev->fa_cookie_lock); 161 162 ret = simple_read_from_buffer(data, count, ppos, buf, buf_len); 163 164 kfree(buf); 165 return ret; 166 167 errout: 168 spin_unlock(&nsim_dev->fa_cookie_lock); 169 return ret; 170 } 171 172 static ssize_t nsim_dev_trap_fa_cookie_write(struct file *file, 173 const char __user *data, 174 size_t count, loff_t *ppos) 175 { 176 struct nsim_dev *nsim_dev = file->private_data; 177 struct flow_action_cookie *fa_cookie; 178 size_t cookie_len; 179 ssize_t ret; 180 char *buf; 181 182 if (*ppos != 0) 183 return -EINVAL; 184 cookie_len = (count - 1) / 2; 185 if ((count - 1) % 2) 186 return -EINVAL; 187 buf = kmalloc(count, GFP_KERNEL | __GFP_NOWARN); 188 if (!buf) 189 return -ENOMEM; 190 191 ret = simple_write_to_buffer(buf, count, ppos, data, count); 192 if (ret < 0) 193 goto free_buf; 194 195 fa_cookie = kmalloc(sizeof(*fa_cookie) + cookie_len, 196 GFP_KERNEL | __GFP_NOWARN); 197 if (!fa_cookie) { 198 ret = -ENOMEM; 199 goto free_buf; 200 } 201 202 fa_cookie->cookie_len = cookie_len; 203 ret = hex2bin(fa_cookie->cookie, buf, cookie_len); 204 if (ret) 205 goto free_fa_cookie; 206 kfree(buf); 207 208 spin_lock(&nsim_dev->fa_cookie_lock); 209 kfree(nsim_dev->fa_cookie); 210 nsim_dev->fa_cookie = fa_cookie; 211 spin_unlock(&nsim_dev->fa_cookie_lock); 212 213 return count; 214 215 free_fa_cookie: 216 kfree(fa_cookie); 217 free_buf: 218 kfree(buf); 219 return ret; 220 } 221 222 static const struct file_operations nsim_dev_trap_fa_cookie_fops = { 223 .open = simple_open, 224 .read = nsim_dev_trap_fa_cookie_read, 225 .write = nsim_dev_trap_fa_cookie_write, 226 .llseek = generic_file_llseek, 227 .owner = THIS_MODULE, 228 }; 229 230 static ssize_t nsim_bus_dev_max_vfs_read(struct file *file, char __user *data, 231 size_t count, loff_t *ppos) 232 { 233 struct nsim_dev *nsim_dev = file->private_data; 234 char buf[11]; 235 ssize_t len; 236 237 len = scnprintf(buf, sizeof(buf), "%u\n", 238 READ_ONCE(nsim_dev->nsim_bus_dev->max_vfs)); 239 240 return simple_read_from_buffer(data, count, ppos, buf, len); 241 } 242 243 static ssize_t nsim_bus_dev_max_vfs_write(struct file *file, 244 const char __user *data, 245 size_t count, loff_t *ppos) 246 { 247 struct nsim_vf_config *vfconfigs; 248 struct nsim_dev *nsim_dev; 249 char buf[10]; 250 ssize_t ret; 251 u32 val; 252 253 if (*ppos != 0) 254 return 0; 255 256 if (count >= sizeof(buf)) 257 return -ENOSPC; 258 259 ret = copy_from_user(buf, data, count); 260 if (ret) 261 return -EFAULT; 262 buf[count] = '\0'; 263 264 ret = kstrtouint(buf, 10, &val); 265 if (ret) 266 return -EINVAL; 267 268 /* max_vfs limited by the maximum number of provided port indexes */ 269 if (val > NSIM_DEV_VF_PORT_INDEX_MAX - NSIM_DEV_VF_PORT_INDEX_BASE) 270 return -ERANGE; 271 272 vfconfigs = kcalloc(val, sizeof(struct nsim_vf_config), 273 GFP_KERNEL | __GFP_NOWARN); 274 if (!vfconfigs) 275 return -ENOMEM; 276 277 nsim_dev = file->private_data; 278 mutex_lock(&nsim_dev->vfs_lock); 279 /* Reject if VFs are configured */ 280 if (nsim_dev_get_vfs(nsim_dev)) { 281 ret = -EBUSY; 282 } else { 283 swap(nsim_dev->vfconfigs, vfconfigs); 284 WRITE_ONCE(nsim_dev->nsim_bus_dev->max_vfs, val); 285 *ppos += count; 286 ret = count; 287 } 288 mutex_unlock(&nsim_dev->vfs_lock); 289 290 kfree(vfconfigs); 291 return ret; 292 } 293 294 static const struct file_operations nsim_dev_max_vfs_fops = { 295 .open = simple_open, 296 .read = nsim_bus_dev_max_vfs_read, 297 .write = nsim_bus_dev_max_vfs_write, 298 .llseek = generic_file_llseek, 299 .owner = THIS_MODULE, 300 }; 301 302 static int nsim_dev_debugfs_init(struct nsim_dev *nsim_dev) 303 { 304 char dev_ddir_name[sizeof(DRV_NAME) + 10]; 305 int err; 306 307 sprintf(dev_ddir_name, DRV_NAME "%u", nsim_dev->nsim_bus_dev->dev.id); 308 nsim_dev->ddir = debugfs_create_dir(dev_ddir_name, nsim_dev_ddir); 309 if (IS_ERR(nsim_dev->ddir)) 310 return PTR_ERR(nsim_dev->ddir); 311 nsim_dev->ports_ddir = debugfs_create_dir("ports", nsim_dev->ddir); 312 if (IS_ERR(nsim_dev->ports_ddir)) 313 return PTR_ERR(nsim_dev->ports_ddir); 314 debugfs_create_bool("fw_update_status", 0600, nsim_dev->ddir, 315 &nsim_dev->fw_update_status); 316 debugfs_create_u32("fw_update_overwrite_mask", 0600, nsim_dev->ddir, 317 &nsim_dev->fw_update_overwrite_mask); 318 debugfs_create_u32("max_macs", 0600, nsim_dev->ddir, 319 &nsim_dev->max_macs); 320 debugfs_create_bool("test1", 0600, nsim_dev->ddir, 321 &nsim_dev->test1); 322 nsim_dev->take_snapshot = debugfs_create_file("take_snapshot", 323 0200, 324 nsim_dev->ddir, 325 nsim_dev, 326 &nsim_dev_take_snapshot_fops); 327 debugfs_create_bool("dont_allow_reload", 0600, nsim_dev->ddir, 328 &nsim_dev->dont_allow_reload); 329 debugfs_create_bool("fail_reload", 0600, nsim_dev->ddir, 330 &nsim_dev->fail_reload); 331 debugfs_create_file("trap_flow_action_cookie", 0600, nsim_dev->ddir, 332 nsim_dev, &nsim_dev_trap_fa_cookie_fops); 333 debugfs_create_bool("fail_trap_group_set", 0600, 334 nsim_dev->ddir, 335 &nsim_dev->fail_trap_group_set); 336 debugfs_create_bool("fail_trap_policer_set", 0600, 337 nsim_dev->ddir, 338 &nsim_dev->fail_trap_policer_set); 339 debugfs_create_bool("fail_trap_policer_counter_get", 0600, 340 nsim_dev->ddir, 341 &nsim_dev->fail_trap_policer_counter_get); 342 debugfs_create_file("max_vfs", 0600, nsim_dev->ddir, 343 nsim_dev, &nsim_dev_max_vfs_fops); 344 345 nsim_dev->nodes_ddir = debugfs_create_dir("rate_nodes", nsim_dev->ddir); 346 if (IS_ERR(nsim_dev->nodes_ddir)) { 347 err = PTR_ERR(nsim_dev->nodes_ddir); 348 goto err_out; 349 } 350 debugfs_create_bool("fail_trap_drop_counter_get", 0600, 351 nsim_dev->ddir, 352 &nsim_dev->fail_trap_drop_counter_get); 353 nsim_udp_tunnels_debugfs_create(nsim_dev); 354 return 0; 355 356 err_out: 357 debugfs_remove_recursive(nsim_dev->ports_ddir); 358 debugfs_remove_recursive(nsim_dev->ddir); 359 return err; 360 } 361 362 static void nsim_dev_debugfs_exit(struct nsim_dev *nsim_dev) 363 { 364 debugfs_remove_recursive(nsim_dev->nodes_ddir); 365 debugfs_remove_recursive(nsim_dev->ports_ddir); 366 debugfs_remove_recursive(nsim_dev->ddir); 367 } 368 369 static ssize_t nsim_dev_rate_parent_read(struct file *file, 370 char __user *data, 371 size_t count, loff_t *ppos) 372 { 373 char **name_ptr = file->private_data; 374 size_t len; 375 376 if (!*name_ptr) 377 return 0; 378 379 len = strlen(*name_ptr); 380 return simple_read_from_buffer(data, count, ppos, *name_ptr, len); 381 } 382 383 static const struct file_operations nsim_dev_rate_parent_fops = { 384 .open = simple_open, 385 .read = nsim_dev_rate_parent_read, 386 .llseek = generic_file_llseek, 387 .owner = THIS_MODULE, 388 }; 389 390 static int nsim_dev_port_debugfs_init(struct nsim_dev *nsim_dev, 391 struct nsim_dev_port *nsim_dev_port) 392 { 393 struct nsim_bus_dev *nsim_bus_dev = nsim_dev->nsim_bus_dev; 394 unsigned int port_index = nsim_dev_port->port_index; 395 char port_ddir_name[16]; 396 char dev_link_name[32]; 397 398 sprintf(port_ddir_name, "%u", port_index); 399 nsim_dev_port->ddir = debugfs_create_dir(port_ddir_name, 400 nsim_dev->ports_ddir); 401 if (IS_ERR(nsim_dev_port->ddir)) 402 return PTR_ERR(nsim_dev_port->ddir); 403 404 sprintf(dev_link_name, "../../../" DRV_NAME "%u", nsim_bus_dev->dev.id); 405 if (nsim_dev_port_is_vf(nsim_dev_port)) { 406 unsigned int vf_id = nsim_dev_port_index_to_vf_index(port_index); 407 408 debugfs_create_u16("tx_share", 0400, nsim_dev_port->ddir, 409 &nsim_dev->vfconfigs[vf_id].min_tx_rate); 410 debugfs_create_u16("tx_max", 0400, nsim_dev_port->ddir, 411 &nsim_dev->vfconfigs[vf_id].max_tx_rate); 412 nsim_dev_port->rate_parent = debugfs_create_file("rate_parent", 413 0400, 414 nsim_dev_port->ddir, 415 &nsim_dev_port->parent_name, 416 &nsim_dev_rate_parent_fops); 417 } 418 debugfs_create_symlink("dev", nsim_dev_port->ddir, dev_link_name); 419 420 return 0; 421 } 422 423 static void nsim_dev_port_debugfs_exit(struct nsim_dev_port *nsim_dev_port) 424 { 425 debugfs_remove_recursive(nsim_dev_port->ddir); 426 } 427 428 static int nsim_dev_resources_register(struct devlink *devlink) 429 { 430 struct devlink_resource_size_params params = { 431 .size_max = (u64)-1, 432 .size_granularity = 1, 433 .unit = DEVLINK_RESOURCE_UNIT_ENTRY 434 }; 435 int err; 436 437 /* Resources for IPv4 */ 438 err = devlink_resource_register(devlink, "IPv4", (u64)-1, 439 NSIM_RESOURCE_IPV4, 440 DEVLINK_RESOURCE_ID_PARENT_TOP, 441 ¶ms); 442 if (err) { 443 pr_err("Failed to register IPv4 top resource\n"); 444 goto out; 445 } 446 447 err = devlink_resource_register(devlink, "fib", (u64)-1, 448 NSIM_RESOURCE_IPV4_FIB, 449 NSIM_RESOURCE_IPV4, ¶ms); 450 if (err) { 451 pr_err("Failed to register IPv4 FIB resource\n"); 452 return err; 453 } 454 455 err = devlink_resource_register(devlink, "fib-rules", (u64)-1, 456 NSIM_RESOURCE_IPV4_FIB_RULES, 457 NSIM_RESOURCE_IPV4, ¶ms); 458 if (err) { 459 pr_err("Failed to register IPv4 FIB rules resource\n"); 460 return err; 461 } 462 463 /* Resources for IPv6 */ 464 err = devlink_resource_register(devlink, "IPv6", (u64)-1, 465 NSIM_RESOURCE_IPV6, 466 DEVLINK_RESOURCE_ID_PARENT_TOP, 467 ¶ms); 468 if (err) { 469 pr_err("Failed to register IPv6 top resource\n"); 470 goto out; 471 } 472 473 err = devlink_resource_register(devlink, "fib", (u64)-1, 474 NSIM_RESOURCE_IPV6_FIB, 475 NSIM_RESOURCE_IPV6, ¶ms); 476 if (err) { 477 pr_err("Failed to register IPv6 FIB resource\n"); 478 return err; 479 } 480 481 err = devlink_resource_register(devlink, "fib-rules", (u64)-1, 482 NSIM_RESOURCE_IPV6_FIB_RULES, 483 NSIM_RESOURCE_IPV6, ¶ms); 484 if (err) { 485 pr_err("Failed to register IPv6 FIB rules resource\n"); 486 return err; 487 } 488 489 /* Resources for nexthops */ 490 err = devlink_resource_register(devlink, "nexthops", (u64)-1, 491 NSIM_RESOURCE_NEXTHOPS, 492 DEVLINK_RESOURCE_ID_PARENT_TOP, 493 ¶ms); 494 495 out: 496 return err; 497 } 498 499 enum nsim_devlink_param_id { 500 NSIM_DEVLINK_PARAM_ID_BASE = DEVLINK_PARAM_GENERIC_ID_MAX, 501 NSIM_DEVLINK_PARAM_ID_TEST1, 502 }; 503 504 static const struct devlink_param nsim_devlink_params[] = { 505 DEVLINK_PARAM_GENERIC(MAX_MACS, 506 BIT(DEVLINK_PARAM_CMODE_DRIVERINIT), 507 NULL, NULL, NULL), 508 DEVLINK_PARAM_DRIVER(NSIM_DEVLINK_PARAM_ID_TEST1, 509 "test1", DEVLINK_PARAM_TYPE_BOOL, 510 BIT(DEVLINK_PARAM_CMODE_DRIVERINIT), 511 NULL, NULL, NULL), 512 }; 513 514 static void nsim_devlink_set_params_init_values(struct nsim_dev *nsim_dev, 515 struct devlink *devlink) 516 { 517 union devlink_param_value value; 518 519 value.vu32 = nsim_dev->max_macs; 520 devlink_param_driverinit_value_set(devlink, 521 DEVLINK_PARAM_GENERIC_ID_MAX_MACS, 522 value); 523 value.vbool = nsim_dev->test1; 524 devlink_param_driverinit_value_set(devlink, 525 NSIM_DEVLINK_PARAM_ID_TEST1, 526 value); 527 } 528 529 static void nsim_devlink_param_load_driverinit_values(struct devlink *devlink) 530 { 531 struct nsim_dev *nsim_dev = devlink_priv(devlink); 532 union devlink_param_value saved_value; 533 int err; 534 535 err = devlink_param_driverinit_value_get(devlink, 536 DEVLINK_PARAM_GENERIC_ID_MAX_MACS, 537 &saved_value); 538 if (!err) 539 nsim_dev->max_macs = saved_value.vu32; 540 err = devlink_param_driverinit_value_get(devlink, 541 NSIM_DEVLINK_PARAM_ID_TEST1, 542 &saved_value); 543 if (!err) 544 nsim_dev->test1 = saved_value.vbool; 545 } 546 547 #define NSIM_DEV_DUMMY_REGION_SNAPSHOT_MAX 16 548 549 static const struct devlink_region_ops dummy_region_ops = { 550 .name = "dummy", 551 .destructor = &kfree, 552 .snapshot = nsim_dev_take_snapshot, 553 }; 554 555 static int nsim_dev_dummy_region_init(struct nsim_dev *nsim_dev, 556 struct devlink *devlink) 557 { 558 nsim_dev->dummy_region = 559 devlink_region_create(devlink, &dummy_region_ops, 560 NSIM_DEV_DUMMY_REGION_SNAPSHOT_MAX, 561 NSIM_DEV_DUMMY_REGION_SIZE); 562 return PTR_ERR_OR_ZERO(nsim_dev->dummy_region); 563 } 564 565 static void nsim_dev_dummy_region_exit(struct nsim_dev *nsim_dev) 566 { 567 devlink_region_destroy(nsim_dev->dummy_region); 568 } 569 570 static void __nsim_dev_port_del(struct nsim_dev_port *nsim_dev_port); 571 572 static int nsim_esw_legacy_enable(struct nsim_dev *nsim_dev, 573 struct netlink_ext_ack *extack) 574 { 575 struct devlink *devlink = priv_to_devlink(nsim_dev); 576 struct nsim_dev_port *nsim_dev_port, *tmp; 577 578 devlink_rate_nodes_destroy(devlink); 579 mutex_lock(&nsim_dev->port_list_lock); 580 list_for_each_entry_safe(nsim_dev_port, tmp, &nsim_dev->port_list, list) 581 if (nsim_dev_port_is_vf(nsim_dev_port)) 582 __nsim_dev_port_del(nsim_dev_port); 583 mutex_unlock(&nsim_dev->port_list_lock); 584 nsim_dev->esw_mode = DEVLINK_ESWITCH_MODE_LEGACY; 585 return 0; 586 } 587 588 static int nsim_esw_switchdev_enable(struct nsim_dev *nsim_dev, 589 struct netlink_ext_ack *extack) 590 { 591 struct nsim_bus_dev *nsim_bus_dev = nsim_dev->nsim_bus_dev; 592 int i, err; 593 594 for (i = 0; i < nsim_dev_get_vfs(nsim_dev); i++) { 595 err = nsim_drv_port_add(nsim_bus_dev, NSIM_DEV_PORT_TYPE_VF, i); 596 if (err) { 597 NL_SET_ERR_MSG_MOD(extack, "Failed to initialize VFs' netdevsim ports"); 598 pr_err("Failed to initialize VF id=%d. %d.\n", i, err); 599 goto err_port_add_vfs; 600 } 601 } 602 nsim_dev->esw_mode = DEVLINK_ESWITCH_MODE_SWITCHDEV; 603 return 0; 604 605 err_port_add_vfs: 606 for (i--; i >= 0; i--) 607 nsim_drv_port_del(nsim_bus_dev, NSIM_DEV_PORT_TYPE_VF, i); 608 return err; 609 } 610 611 static int nsim_devlink_eswitch_mode_set(struct devlink *devlink, u16 mode, 612 struct netlink_ext_ack *extack) 613 { 614 struct nsim_dev *nsim_dev = devlink_priv(devlink); 615 int err = 0; 616 617 mutex_lock(&nsim_dev->vfs_lock); 618 if (mode == nsim_dev->esw_mode) 619 goto unlock; 620 621 if (mode == DEVLINK_ESWITCH_MODE_LEGACY) 622 err = nsim_esw_legacy_enable(nsim_dev, extack); 623 else if (mode == DEVLINK_ESWITCH_MODE_SWITCHDEV) 624 err = nsim_esw_switchdev_enable(nsim_dev, extack); 625 else 626 err = -EINVAL; 627 628 unlock: 629 mutex_unlock(&nsim_dev->vfs_lock); 630 return err; 631 } 632 633 static int nsim_devlink_eswitch_mode_get(struct devlink *devlink, u16 *mode) 634 { 635 struct nsim_dev *nsim_dev = devlink_priv(devlink); 636 637 *mode = nsim_dev->esw_mode; 638 return 0; 639 } 640 641 struct nsim_trap_item { 642 void *trap_ctx; 643 enum devlink_trap_action action; 644 }; 645 646 struct nsim_trap_data { 647 struct delayed_work trap_report_dw; 648 struct nsim_trap_item *trap_items_arr; 649 u64 *trap_policers_cnt_arr; 650 u64 trap_pkt_cnt; 651 struct nsim_dev *nsim_dev; 652 spinlock_t trap_lock; /* Protects trap_items_arr */ 653 }; 654 655 /* All driver-specific traps must be documented in 656 * Documentation/networking/devlink/netdevsim.rst 657 */ 658 enum { 659 NSIM_TRAP_ID_BASE = DEVLINK_TRAP_GENERIC_ID_MAX, 660 NSIM_TRAP_ID_FID_MISS, 661 }; 662 663 #define NSIM_TRAP_NAME_FID_MISS "fid_miss" 664 665 #define NSIM_TRAP_METADATA DEVLINK_TRAP_METADATA_TYPE_F_IN_PORT 666 667 #define NSIM_TRAP_DROP(_id, _group_id) \ 668 DEVLINK_TRAP_GENERIC(DROP, DROP, _id, \ 669 DEVLINK_TRAP_GROUP_GENERIC_ID_##_group_id, \ 670 NSIM_TRAP_METADATA) 671 #define NSIM_TRAP_DROP_EXT(_id, _group_id, _metadata) \ 672 DEVLINK_TRAP_GENERIC(DROP, DROP, _id, \ 673 DEVLINK_TRAP_GROUP_GENERIC_ID_##_group_id, \ 674 NSIM_TRAP_METADATA | (_metadata)) 675 #define NSIM_TRAP_EXCEPTION(_id, _group_id) \ 676 DEVLINK_TRAP_GENERIC(EXCEPTION, TRAP, _id, \ 677 DEVLINK_TRAP_GROUP_GENERIC_ID_##_group_id, \ 678 NSIM_TRAP_METADATA) 679 #define NSIM_TRAP_CONTROL(_id, _group_id, _action) \ 680 DEVLINK_TRAP_GENERIC(CONTROL, _action, _id, \ 681 DEVLINK_TRAP_GROUP_GENERIC_ID_##_group_id, \ 682 NSIM_TRAP_METADATA) 683 #define NSIM_TRAP_DRIVER_EXCEPTION(_id, _group_id) \ 684 DEVLINK_TRAP_DRIVER(EXCEPTION, TRAP, NSIM_TRAP_ID_##_id, \ 685 NSIM_TRAP_NAME_##_id, \ 686 DEVLINK_TRAP_GROUP_GENERIC_ID_##_group_id, \ 687 NSIM_TRAP_METADATA) 688 689 #define NSIM_DEV_TRAP_POLICER_MIN_RATE 1 690 #define NSIM_DEV_TRAP_POLICER_MAX_RATE 8000 691 #define NSIM_DEV_TRAP_POLICER_MIN_BURST 8 692 #define NSIM_DEV_TRAP_POLICER_MAX_BURST 65536 693 694 #define NSIM_TRAP_POLICER(_id, _rate, _burst) \ 695 DEVLINK_TRAP_POLICER(_id, _rate, _burst, \ 696 NSIM_DEV_TRAP_POLICER_MAX_RATE, \ 697 NSIM_DEV_TRAP_POLICER_MIN_RATE, \ 698 NSIM_DEV_TRAP_POLICER_MAX_BURST, \ 699 NSIM_DEV_TRAP_POLICER_MIN_BURST) 700 701 static const struct devlink_trap_policer nsim_trap_policers_arr[] = { 702 NSIM_TRAP_POLICER(1, 1000, 128), 703 NSIM_TRAP_POLICER(2, 2000, 256), 704 NSIM_TRAP_POLICER(3, 3000, 512), 705 }; 706 707 static const struct devlink_trap_group nsim_trap_groups_arr[] = { 708 DEVLINK_TRAP_GROUP_GENERIC(L2_DROPS, 0), 709 DEVLINK_TRAP_GROUP_GENERIC(L3_DROPS, 1), 710 DEVLINK_TRAP_GROUP_GENERIC(L3_EXCEPTIONS, 1), 711 DEVLINK_TRAP_GROUP_GENERIC(BUFFER_DROPS, 2), 712 DEVLINK_TRAP_GROUP_GENERIC(ACL_DROPS, 3), 713 DEVLINK_TRAP_GROUP_GENERIC(MC_SNOOPING, 3), 714 }; 715 716 static const struct devlink_trap nsim_traps_arr[] = { 717 NSIM_TRAP_DROP(SMAC_MC, L2_DROPS), 718 NSIM_TRAP_DROP(VLAN_TAG_MISMATCH, L2_DROPS), 719 NSIM_TRAP_DROP(INGRESS_VLAN_FILTER, L2_DROPS), 720 NSIM_TRAP_DROP(INGRESS_STP_FILTER, L2_DROPS), 721 NSIM_TRAP_DROP(EMPTY_TX_LIST, L2_DROPS), 722 NSIM_TRAP_DROP(PORT_LOOPBACK_FILTER, L2_DROPS), 723 NSIM_TRAP_DRIVER_EXCEPTION(FID_MISS, L2_DROPS), 724 NSIM_TRAP_DROP(BLACKHOLE_ROUTE, L3_DROPS), 725 NSIM_TRAP_EXCEPTION(TTL_ERROR, L3_EXCEPTIONS), 726 NSIM_TRAP_DROP(TAIL_DROP, BUFFER_DROPS), 727 NSIM_TRAP_DROP_EXT(INGRESS_FLOW_ACTION_DROP, ACL_DROPS, 728 DEVLINK_TRAP_METADATA_TYPE_F_FA_COOKIE), 729 NSIM_TRAP_DROP_EXT(EGRESS_FLOW_ACTION_DROP, ACL_DROPS, 730 DEVLINK_TRAP_METADATA_TYPE_F_FA_COOKIE), 731 NSIM_TRAP_CONTROL(IGMP_QUERY, MC_SNOOPING, MIRROR), 732 NSIM_TRAP_CONTROL(IGMP_V1_REPORT, MC_SNOOPING, TRAP), 733 }; 734 735 #define NSIM_TRAP_L4_DATA_LEN 100 736 737 static struct sk_buff *nsim_dev_trap_skb_build(void) 738 { 739 int tot_len, data_len = NSIM_TRAP_L4_DATA_LEN; 740 struct sk_buff *skb; 741 struct udphdr *udph; 742 struct ethhdr *eth; 743 struct iphdr *iph; 744 745 skb = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC); 746 if (!skb) 747 return NULL; 748 tot_len = sizeof(struct iphdr) + sizeof(struct udphdr) + data_len; 749 750 skb_reset_mac_header(skb); 751 eth = skb_put(skb, sizeof(struct ethhdr)); 752 eth_random_addr(eth->h_dest); 753 eth_random_addr(eth->h_source); 754 eth->h_proto = htons(ETH_P_IP); 755 skb->protocol = htons(ETH_P_IP); 756 757 skb_set_network_header(skb, skb->len); 758 iph = skb_put(skb, sizeof(struct iphdr)); 759 iph->protocol = IPPROTO_UDP; 760 iph->saddr = in_aton("192.0.2.1"); 761 iph->daddr = in_aton("198.51.100.1"); 762 iph->version = 0x4; 763 iph->frag_off = 0; 764 iph->ihl = 0x5; 765 iph->tot_len = htons(tot_len); 766 iph->ttl = 100; 767 iph->check = 0; 768 iph->check = ip_fast_csum((unsigned char *)iph, iph->ihl); 769 770 skb_set_transport_header(skb, skb->len); 771 udph = skb_put_zero(skb, sizeof(struct udphdr) + data_len); 772 get_random_bytes(&udph->source, sizeof(u16)); 773 get_random_bytes(&udph->dest, sizeof(u16)); 774 udph->len = htons(sizeof(struct udphdr) + data_len); 775 776 return skb; 777 } 778 779 static void nsim_dev_trap_report(struct nsim_dev_port *nsim_dev_port) 780 { 781 struct nsim_dev *nsim_dev = nsim_dev_port->ns->nsim_dev; 782 struct devlink *devlink = priv_to_devlink(nsim_dev); 783 struct nsim_trap_data *nsim_trap_data; 784 int i; 785 786 nsim_trap_data = nsim_dev->trap_data; 787 788 spin_lock(&nsim_trap_data->trap_lock); 789 for (i = 0; i < ARRAY_SIZE(nsim_traps_arr); i++) { 790 struct flow_action_cookie *fa_cookie = NULL; 791 struct nsim_trap_item *nsim_trap_item; 792 struct sk_buff *skb; 793 bool has_fa_cookie; 794 795 has_fa_cookie = nsim_traps_arr[i].metadata_cap & 796 DEVLINK_TRAP_METADATA_TYPE_F_FA_COOKIE; 797 798 nsim_trap_item = &nsim_trap_data->trap_items_arr[i]; 799 if (nsim_trap_item->action == DEVLINK_TRAP_ACTION_DROP) 800 continue; 801 802 skb = nsim_dev_trap_skb_build(); 803 if (!skb) 804 continue; 805 skb->dev = nsim_dev_port->ns->netdev; 806 807 /* Trapped packets are usually passed to devlink in softIRQ, 808 * but in this case they are generated in a workqueue. Disable 809 * softIRQs to prevent lockdep from complaining about 810 * "incosistent lock state". 811 */ 812 813 spin_lock_bh(&nsim_dev->fa_cookie_lock); 814 fa_cookie = has_fa_cookie ? nsim_dev->fa_cookie : NULL; 815 devlink_trap_report(devlink, skb, nsim_trap_item->trap_ctx, 816 &nsim_dev_port->devlink_port, fa_cookie); 817 spin_unlock_bh(&nsim_dev->fa_cookie_lock); 818 consume_skb(skb); 819 } 820 spin_unlock(&nsim_trap_data->trap_lock); 821 } 822 823 #define NSIM_TRAP_REPORT_INTERVAL_MS 100 824 825 static void nsim_dev_trap_report_work(struct work_struct *work) 826 { 827 struct nsim_trap_data *nsim_trap_data; 828 struct nsim_dev_port *nsim_dev_port; 829 struct nsim_dev *nsim_dev; 830 831 nsim_trap_data = container_of(work, struct nsim_trap_data, 832 trap_report_dw.work); 833 nsim_dev = nsim_trap_data->nsim_dev; 834 835 /* For each running port and enabled packet trap, generate a UDP 836 * packet with a random 5-tuple and report it. 837 */ 838 mutex_lock(&nsim_dev->port_list_lock); 839 list_for_each_entry(nsim_dev_port, &nsim_dev->port_list, list) { 840 if (!netif_running(nsim_dev_port->ns->netdev)) 841 continue; 842 843 nsim_dev_trap_report(nsim_dev_port); 844 } 845 mutex_unlock(&nsim_dev->port_list_lock); 846 847 schedule_delayed_work(&nsim_dev->trap_data->trap_report_dw, 848 msecs_to_jiffies(NSIM_TRAP_REPORT_INTERVAL_MS)); 849 } 850 851 static int nsim_dev_traps_init(struct devlink *devlink) 852 { 853 size_t policers_count = ARRAY_SIZE(nsim_trap_policers_arr); 854 struct nsim_dev *nsim_dev = devlink_priv(devlink); 855 struct nsim_trap_data *nsim_trap_data; 856 int err; 857 858 nsim_trap_data = kzalloc(sizeof(*nsim_trap_data), GFP_KERNEL); 859 if (!nsim_trap_data) 860 return -ENOMEM; 861 862 nsim_trap_data->trap_items_arr = kcalloc(ARRAY_SIZE(nsim_traps_arr), 863 sizeof(struct nsim_trap_item), 864 GFP_KERNEL); 865 if (!nsim_trap_data->trap_items_arr) { 866 err = -ENOMEM; 867 goto err_trap_data_free; 868 } 869 870 nsim_trap_data->trap_policers_cnt_arr = kcalloc(policers_count, 871 sizeof(u64), 872 GFP_KERNEL); 873 if (!nsim_trap_data->trap_policers_cnt_arr) { 874 err = -ENOMEM; 875 goto err_trap_items_free; 876 } 877 878 /* The lock is used to protect the action state of the registered 879 * traps. The value is written by user and read in delayed work when 880 * iterating over all the traps. 881 */ 882 spin_lock_init(&nsim_trap_data->trap_lock); 883 nsim_trap_data->nsim_dev = nsim_dev; 884 nsim_dev->trap_data = nsim_trap_data; 885 886 err = devlink_trap_policers_register(devlink, nsim_trap_policers_arr, 887 policers_count); 888 if (err) 889 goto err_trap_policers_cnt_free; 890 891 err = devlink_trap_groups_register(devlink, nsim_trap_groups_arr, 892 ARRAY_SIZE(nsim_trap_groups_arr)); 893 if (err) 894 goto err_trap_policers_unregister; 895 896 err = devlink_traps_register(devlink, nsim_traps_arr, 897 ARRAY_SIZE(nsim_traps_arr), NULL); 898 if (err) 899 goto err_trap_groups_unregister; 900 901 INIT_DELAYED_WORK(&nsim_dev->trap_data->trap_report_dw, 902 nsim_dev_trap_report_work); 903 schedule_delayed_work(&nsim_dev->trap_data->trap_report_dw, 904 msecs_to_jiffies(NSIM_TRAP_REPORT_INTERVAL_MS)); 905 906 return 0; 907 908 err_trap_groups_unregister: 909 devlink_trap_groups_unregister(devlink, nsim_trap_groups_arr, 910 ARRAY_SIZE(nsim_trap_groups_arr)); 911 err_trap_policers_unregister: 912 devlink_trap_policers_unregister(devlink, nsim_trap_policers_arr, 913 ARRAY_SIZE(nsim_trap_policers_arr)); 914 err_trap_policers_cnt_free: 915 kfree(nsim_trap_data->trap_policers_cnt_arr); 916 err_trap_items_free: 917 kfree(nsim_trap_data->trap_items_arr); 918 err_trap_data_free: 919 kfree(nsim_trap_data); 920 return err; 921 } 922 923 static void nsim_dev_traps_exit(struct devlink *devlink) 924 { 925 struct nsim_dev *nsim_dev = devlink_priv(devlink); 926 927 cancel_delayed_work_sync(&nsim_dev->trap_data->trap_report_dw); 928 devlink_traps_unregister(devlink, nsim_traps_arr, 929 ARRAY_SIZE(nsim_traps_arr)); 930 devlink_trap_groups_unregister(devlink, nsim_trap_groups_arr, 931 ARRAY_SIZE(nsim_trap_groups_arr)); 932 devlink_trap_policers_unregister(devlink, nsim_trap_policers_arr, 933 ARRAY_SIZE(nsim_trap_policers_arr)); 934 kfree(nsim_dev->trap_data->trap_policers_cnt_arr); 935 kfree(nsim_dev->trap_data->trap_items_arr); 936 kfree(nsim_dev->trap_data); 937 } 938 939 static int nsim_dev_reload_create(struct nsim_dev *nsim_dev, 940 struct netlink_ext_ack *extack); 941 static void nsim_dev_reload_destroy(struct nsim_dev *nsim_dev); 942 943 static int nsim_dev_reload_down(struct devlink *devlink, bool netns_change, 944 enum devlink_reload_action action, enum devlink_reload_limit limit, 945 struct netlink_ext_ack *extack) 946 { 947 struct nsim_dev *nsim_dev = devlink_priv(devlink); 948 struct nsim_bus_dev *nsim_bus_dev; 949 950 nsim_bus_dev = nsim_dev->nsim_bus_dev; 951 if (!mutex_trylock(&nsim_bus_dev->nsim_bus_reload_lock)) 952 return -EOPNOTSUPP; 953 954 if (nsim_dev->dont_allow_reload) { 955 /* For testing purposes, user set debugfs dont_allow_reload 956 * value to true. So forbid it. 957 */ 958 NL_SET_ERR_MSG_MOD(extack, "User forbid the reload for testing purposes"); 959 mutex_unlock(&nsim_bus_dev->nsim_bus_reload_lock); 960 return -EOPNOTSUPP; 961 } 962 nsim_bus_dev->in_reload = true; 963 964 nsim_dev_reload_destroy(nsim_dev); 965 mutex_unlock(&nsim_bus_dev->nsim_bus_reload_lock); 966 return 0; 967 } 968 969 static int nsim_dev_reload_up(struct devlink *devlink, enum devlink_reload_action action, 970 enum devlink_reload_limit limit, u32 *actions_performed, 971 struct netlink_ext_ack *extack) 972 { 973 struct nsim_dev *nsim_dev = devlink_priv(devlink); 974 struct nsim_bus_dev *nsim_bus_dev; 975 int ret; 976 977 nsim_bus_dev = nsim_dev->nsim_bus_dev; 978 mutex_lock(&nsim_bus_dev->nsim_bus_reload_lock); 979 nsim_bus_dev->in_reload = false; 980 981 if (nsim_dev->fail_reload) { 982 /* For testing purposes, user set debugfs fail_reload 983 * value to true. Fail right away. 984 */ 985 NL_SET_ERR_MSG_MOD(extack, "User setup the reload to fail for testing purposes"); 986 mutex_unlock(&nsim_bus_dev->nsim_bus_reload_lock); 987 return -EINVAL; 988 } 989 990 *actions_performed = BIT(DEVLINK_RELOAD_ACTION_DRIVER_REINIT); 991 ret = nsim_dev_reload_create(nsim_dev, extack); 992 mutex_unlock(&nsim_bus_dev->nsim_bus_reload_lock); 993 return ret; 994 } 995 996 static int nsim_dev_info_get(struct devlink *devlink, 997 struct devlink_info_req *req, 998 struct netlink_ext_ack *extack) 999 { 1000 return devlink_info_driver_name_put(req, DRV_NAME); 1001 } 1002 1003 #define NSIM_DEV_FLASH_SIZE 500000 1004 #define NSIM_DEV_FLASH_CHUNK_SIZE 1000 1005 #define NSIM_DEV_FLASH_CHUNK_TIME_MS 10 1006 1007 static int nsim_dev_flash_update(struct devlink *devlink, 1008 struct devlink_flash_update_params *params, 1009 struct netlink_ext_ack *extack) 1010 { 1011 struct nsim_dev *nsim_dev = devlink_priv(devlink); 1012 int i; 1013 1014 if ((params->overwrite_mask & ~nsim_dev->fw_update_overwrite_mask) != 0) 1015 return -EOPNOTSUPP; 1016 1017 if (nsim_dev->fw_update_status) { 1018 devlink_flash_update_status_notify(devlink, 1019 "Preparing to flash", 1020 params->component, 0, 0); 1021 } 1022 1023 for (i = 0; i < NSIM_DEV_FLASH_SIZE / NSIM_DEV_FLASH_CHUNK_SIZE; i++) { 1024 if (nsim_dev->fw_update_status) 1025 devlink_flash_update_status_notify(devlink, "Flashing", 1026 params->component, 1027 i * NSIM_DEV_FLASH_CHUNK_SIZE, 1028 NSIM_DEV_FLASH_SIZE); 1029 msleep(NSIM_DEV_FLASH_CHUNK_TIME_MS); 1030 } 1031 1032 if (nsim_dev->fw_update_status) { 1033 devlink_flash_update_status_notify(devlink, "Flashing", 1034 params->component, 1035 NSIM_DEV_FLASH_SIZE, 1036 NSIM_DEV_FLASH_SIZE); 1037 devlink_flash_update_timeout_notify(devlink, "Flash select", 1038 params->component, 81); 1039 devlink_flash_update_status_notify(devlink, "Flashing done", 1040 params->component, 0, 0); 1041 } 1042 1043 return 0; 1044 } 1045 1046 static struct nsim_trap_item * 1047 nsim_dev_trap_item_lookup(struct nsim_dev *nsim_dev, u16 trap_id) 1048 { 1049 struct nsim_trap_data *nsim_trap_data = nsim_dev->trap_data; 1050 int i; 1051 1052 for (i = 0; i < ARRAY_SIZE(nsim_traps_arr); i++) { 1053 if (nsim_traps_arr[i].id == trap_id) 1054 return &nsim_trap_data->trap_items_arr[i]; 1055 } 1056 1057 return NULL; 1058 } 1059 1060 static int nsim_dev_devlink_trap_init(struct devlink *devlink, 1061 const struct devlink_trap *trap, 1062 void *trap_ctx) 1063 { 1064 struct nsim_dev *nsim_dev = devlink_priv(devlink); 1065 struct nsim_trap_item *nsim_trap_item; 1066 1067 nsim_trap_item = nsim_dev_trap_item_lookup(nsim_dev, trap->id); 1068 if (WARN_ON(!nsim_trap_item)) 1069 return -ENOENT; 1070 1071 nsim_trap_item->trap_ctx = trap_ctx; 1072 nsim_trap_item->action = trap->init_action; 1073 1074 return 0; 1075 } 1076 1077 static int 1078 nsim_dev_devlink_trap_action_set(struct devlink *devlink, 1079 const struct devlink_trap *trap, 1080 enum devlink_trap_action action, 1081 struct netlink_ext_ack *extack) 1082 { 1083 struct nsim_dev *nsim_dev = devlink_priv(devlink); 1084 struct nsim_trap_item *nsim_trap_item; 1085 1086 nsim_trap_item = nsim_dev_trap_item_lookup(nsim_dev, trap->id); 1087 if (WARN_ON(!nsim_trap_item)) 1088 return -ENOENT; 1089 1090 spin_lock(&nsim_dev->trap_data->trap_lock); 1091 nsim_trap_item->action = action; 1092 spin_unlock(&nsim_dev->trap_data->trap_lock); 1093 1094 return 0; 1095 } 1096 1097 static int 1098 nsim_dev_devlink_trap_group_set(struct devlink *devlink, 1099 const struct devlink_trap_group *group, 1100 const struct devlink_trap_policer *policer, 1101 struct netlink_ext_ack *extack) 1102 { 1103 struct nsim_dev *nsim_dev = devlink_priv(devlink); 1104 1105 if (nsim_dev->fail_trap_group_set) 1106 return -EINVAL; 1107 1108 return 0; 1109 } 1110 1111 static int 1112 nsim_dev_devlink_trap_policer_set(struct devlink *devlink, 1113 const struct devlink_trap_policer *policer, 1114 u64 rate, u64 burst, 1115 struct netlink_ext_ack *extack) 1116 { 1117 struct nsim_dev *nsim_dev = devlink_priv(devlink); 1118 1119 if (nsim_dev->fail_trap_policer_set) { 1120 NL_SET_ERR_MSG_MOD(extack, "User setup the operation to fail for testing purposes"); 1121 return -EINVAL; 1122 } 1123 1124 return 0; 1125 } 1126 1127 static int 1128 nsim_dev_devlink_trap_policer_counter_get(struct devlink *devlink, 1129 const struct devlink_trap_policer *policer, 1130 u64 *p_drops) 1131 { 1132 struct nsim_dev *nsim_dev = devlink_priv(devlink); 1133 u64 *cnt; 1134 1135 if (nsim_dev->fail_trap_policer_counter_get) 1136 return -EINVAL; 1137 1138 cnt = &nsim_dev->trap_data->trap_policers_cnt_arr[policer->id - 1]; 1139 *p_drops = (*cnt)++; 1140 1141 return 0; 1142 } 1143 1144 #define NSIM_LINK_SPEED_MAX 5000 /* Mbps */ 1145 #define NSIM_LINK_SPEED_UNIT 125000 /* 1 Mbps given in bytes/sec to avoid 1146 * u64 overflow during conversion from 1147 * bytes to bits. 1148 */ 1149 1150 static int nsim_rate_bytes_to_units(char *name, u64 *rate, struct netlink_ext_ack *extack) 1151 { 1152 u64 val; 1153 u32 rem; 1154 1155 val = div_u64_rem(*rate, NSIM_LINK_SPEED_UNIT, &rem); 1156 if (rem) { 1157 pr_err("%s rate value %lluBps not in link speed units of 1Mbps.\n", 1158 name, *rate); 1159 NL_SET_ERR_MSG_MOD(extack, "TX rate value not in link speed units of 1Mbps."); 1160 return -EINVAL; 1161 } 1162 1163 if (val > NSIM_LINK_SPEED_MAX) { 1164 pr_err("%s rate value %lluMbps exceed link maximum speed 5000Mbps.\n", 1165 name, val); 1166 NL_SET_ERR_MSG_MOD(extack, "TX rate value exceed link maximum speed 5000Mbps."); 1167 return -EINVAL; 1168 } 1169 *rate = val; 1170 return 0; 1171 } 1172 1173 static int nsim_leaf_tx_share_set(struct devlink_rate *devlink_rate, void *priv, 1174 u64 tx_share, struct netlink_ext_ack *extack) 1175 { 1176 struct nsim_dev_port *nsim_dev_port = priv; 1177 struct nsim_dev *nsim_dev = nsim_dev_port->ns->nsim_dev; 1178 int vf_id = nsim_dev_port_index_to_vf_index(nsim_dev_port->port_index); 1179 int err; 1180 1181 err = nsim_rate_bytes_to_units("tx_share", &tx_share, extack); 1182 if (err) 1183 return err; 1184 1185 nsim_dev->vfconfigs[vf_id].min_tx_rate = tx_share; 1186 return 0; 1187 } 1188 1189 static int nsim_leaf_tx_max_set(struct devlink_rate *devlink_rate, void *priv, 1190 u64 tx_max, struct netlink_ext_ack *extack) 1191 { 1192 struct nsim_dev_port *nsim_dev_port = priv; 1193 struct nsim_dev *nsim_dev = nsim_dev_port->ns->nsim_dev; 1194 int vf_id = nsim_dev_port_index_to_vf_index(nsim_dev_port->port_index); 1195 int err; 1196 1197 err = nsim_rate_bytes_to_units("tx_max", &tx_max, extack); 1198 if (err) 1199 return err; 1200 1201 nsim_dev->vfconfigs[vf_id].max_tx_rate = tx_max; 1202 return 0; 1203 } 1204 1205 struct nsim_rate_node { 1206 struct dentry *ddir; 1207 struct dentry *rate_parent; 1208 char *parent_name; 1209 u16 tx_share; 1210 u16 tx_max; 1211 }; 1212 1213 static int nsim_node_tx_share_set(struct devlink_rate *devlink_rate, void *priv, 1214 u64 tx_share, struct netlink_ext_ack *extack) 1215 { 1216 struct nsim_rate_node *nsim_node = priv; 1217 int err; 1218 1219 err = nsim_rate_bytes_to_units("tx_share", &tx_share, extack); 1220 if (err) 1221 return err; 1222 1223 nsim_node->tx_share = tx_share; 1224 return 0; 1225 } 1226 1227 static int nsim_node_tx_max_set(struct devlink_rate *devlink_rate, void *priv, 1228 u64 tx_max, struct netlink_ext_ack *extack) 1229 { 1230 struct nsim_rate_node *nsim_node = priv; 1231 int err; 1232 1233 err = nsim_rate_bytes_to_units("tx_max", &tx_max, extack); 1234 if (err) 1235 return err; 1236 1237 nsim_node->tx_max = tx_max; 1238 return 0; 1239 } 1240 1241 static int nsim_rate_node_new(struct devlink_rate *node, void **priv, 1242 struct netlink_ext_ack *extack) 1243 { 1244 struct nsim_dev *nsim_dev = devlink_priv(node->devlink); 1245 struct nsim_rate_node *nsim_node; 1246 1247 if (!nsim_esw_mode_is_switchdev(nsim_dev)) { 1248 NL_SET_ERR_MSG_MOD(extack, "Node creation allowed only in switchdev mode."); 1249 return -EOPNOTSUPP; 1250 } 1251 1252 nsim_node = kzalloc(sizeof(*nsim_node), GFP_KERNEL); 1253 if (!nsim_node) 1254 return -ENOMEM; 1255 1256 nsim_node->ddir = debugfs_create_dir(node->name, nsim_dev->nodes_ddir); 1257 1258 debugfs_create_u16("tx_share", 0400, nsim_node->ddir, &nsim_node->tx_share); 1259 debugfs_create_u16("tx_max", 0400, nsim_node->ddir, &nsim_node->tx_max); 1260 nsim_node->rate_parent = debugfs_create_file("rate_parent", 0400, 1261 nsim_node->ddir, 1262 &nsim_node->parent_name, 1263 &nsim_dev_rate_parent_fops); 1264 1265 *priv = nsim_node; 1266 return 0; 1267 } 1268 1269 static int nsim_rate_node_del(struct devlink_rate *node, void *priv, 1270 struct netlink_ext_ack *extack) 1271 { 1272 struct nsim_rate_node *nsim_node = priv; 1273 1274 debugfs_remove(nsim_node->rate_parent); 1275 debugfs_remove_recursive(nsim_node->ddir); 1276 kfree(nsim_node); 1277 return 0; 1278 } 1279 1280 static int nsim_rate_leaf_parent_set(struct devlink_rate *child, 1281 struct devlink_rate *parent, 1282 void *priv_child, void *priv_parent, 1283 struct netlink_ext_ack *extack) 1284 { 1285 struct nsim_dev_port *nsim_dev_port = priv_child; 1286 1287 if (parent) 1288 nsim_dev_port->parent_name = parent->name; 1289 else 1290 nsim_dev_port->parent_name = NULL; 1291 return 0; 1292 } 1293 1294 static int nsim_rate_node_parent_set(struct devlink_rate *child, 1295 struct devlink_rate *parent, 1296 void *priv_child, void *priv_parent, 1297 struct netlink_ext_ack *extack) 1298 { 1299 struct nsim_rate_node *nsim_node = priv_child; 1300 1301 if (parent) 1302 nsim_node->parent_name = parent->name; 1303 else 1304 nsim_node->parent_name = NULL; 1305 return 0; 1306 } 1307 1308 static int 1309 nsim_dev_devlink_trap_drop_counter_get(struct devlink *devlink, 1310 const struct devlink_trap *trap, 1311 u64 *p_drops) 1312 { 1313 struct nsim_dev *nsim_dev = devlink_priv(devlink); 1314 u64 *cnt; 1315 1316 if (nsim_dev->fail_trap_drop_counter_get) 1317 return -EINVAL; 1318 1319 cnt = &nsim_dev->trap_data->trap_pkt_cnt; 1320 *p_drops = (*cnt)++; 1321 1322 return 0; 1323 } 1324 1325 static const struct devlink_ops nsim_dev_devlink_ops = { 1326 .eswitch_mode_set = nsim_devlink_eswitch_mode_set, 1327 .eswitch_mode_get = nsim_devlink_eswitch_mode_get, 1328 .supported_flash_update_params = DEVLINK_SUPPORT_FLASH_UPDATE_COMPONENT | 1329 DEVLINK_SUPPORT_FLASH_UPDATE_OVERWRITE_MASK, 1330 .reload_actions = BIT(DEVLINK_RELOAD_ACTION_DRIVER_REINIT), 1331 .reload_down = nsim_dev_reload_down, 1332 .reload_up = nsim_dev_reload_up, 1333 .info_get = nsim_dev_info_get, 1334 .flash_update = nsim_dev_flash_update, 1335 .trap_init = nsim_dev_devlink_trap_init, 1336 .trap_action_set = nsim_dev_devlink_trap_action_set, 1337 .trap_group_set = nsim_dev_devlink_trap_group_set, 1338 .trap_policer_set = nsim_dev_devlink_trap_policer_set, 1339 .trap_policer_counter_get = nsim_dev_devlink_trap_policer_counter_get, 1340 .rate_leaf_tx_share_set = nsim_leaf_tx_share_set, 1341 .rate_leaf_tx_max_set = nsim_leaf_tx_max_set, 1342 .rate_node_tx_share_set = nsim_node_tx_share_set, 1343 .rate_node_tx_max_set = nsim_node_tx_max_set, 1344 .rate_node_new = nsim_rate_node_new, 1345 .rate_node_del = nsim_rate_node_del, 1346 .rate_leaf_parent_set = nsim_rate_leaf_parent_set, 1347 .rate_node_parent_set = nsim_rate_node_parent_set, 1348 .trap_drop_counter_get = nsim_dev_devlink_trap_drop_counter_get, 1349 }; 1350 1351 #define NSIM_DEV_MAX_MACS_DEFAULT 32 1352 #define NSIM_DEV_TEST1_DEFAULT true 1353 1354 static int __nsim_dev_port_add(struct nsim_dev *nsim_dev, enum nsim_dev_port_type type, 1355 unsigned int port_index) 1356 { 1357 struct devlink_port_attrs attrs = {}; 1358 struct nsim_dev_port *nsim_dev_port; 1359 struct devlink_port *devlink_port; 1360 int err; 1361 1362 if (type == NSIM_DEV_PORT_TYPE_VF && !nsim_dev_get_vfs(nsim_dev)) 1363 return -EINVAL; 1364 1365 nsim_dev_port = kzalloc(sizeof(*nsim_dev_port), GFP_KERNEL); 1366 if (!nsim_dev_port) 1367 return -ENOMEM; 1368 nsim_dev_port->port_index = nsim_dev_port_index(type, port_index); 1369 nsim_dev_port->port_type = type; 1370 1371 devlink_port = &nsim_dev_port->devlink_port; 1372 if (nsim_dev_port_is_pf(nsim_dev_port)) { 1373 attrs.flavour = DEVLINK_PORT_FLAVOUR_PHYSICAL; 1374 attrs.phys.port_number = port_index + 1; 1375 } else { 1376 attrs.flavour = DEVLINK_PORT_FLAVOUR_PCI_VF; 1377 attrs.pci_vf.pf = 0; 1378 attrs.pci_vf.vf = port_index; 1379 } 1380 memcpy(attrs.switch_id.id, nsim_dev->switch_id.id, nsim_dev->switch_id.id_len); 1381 attrs.switch_id.id_len = nsim_dev->switch_id.id_len; 1382 devlink_port_attrs_set(devlink_port, &attrs); 1383 err = devlink_port_register(priv_to_devlink(nsim_dev), devlink_port, 1384 nsim_dev_port->port_index); 1385 if (err) 1386 goto err_port_free; 1387 1388 err = nsim_dev_port_debugfs_init(nsim_dev, nsim_dev_port); 1389 if (err) 1390 goto err_dl_port_unregister; 1391 1392 nsim_dev_port->ns = nsim_create(nsim_dev, nsim_dev_port); 1393 if (IS_ERR(nsim_dev_port->ns)) { 1394 err = PTR_ERR(nsim_dev_port->ns); 1395 goto err_port_debugfs_exit; 1396 } 1397 1398 if (nsim_dev_port_is_vf(nsim_dev_port)) { 1399 err = devlink_rate_leaf_create(&nsim_dev_port->devlink_port, 1400 nsim_dev_port); 1401 if (err) 1402 goto err_nsim_destroy; 1403 } 1404 1405 devlink_port_type_eth_set(devlink_port, nsim_dev_port->ns->netdev); 1406 list_add(&nsim_dev_port->list, &nsim_dev->port_list); 1407 1408 return 0; 1409 1410 err_nsim_destroy: 1411 nsim_destroy(nsim_dev_port->ns); 1412 err_port_debugfs_exit: 1413 nsim_dev_port_debugfs_exit(nsim_dev_port); 1414 err_dl_port_unregister: 1415 devlink_port_unregister(devlink_port); 1416 err_port_free: 1417 kfree(nsim_dev_port); 1418 return err; 1419 } 1420 1421 static void __nsim_dev_port_del(struct nsim_dev_port *nsim_dev_port) 1422 { 1423 struct devlink_port *devlink_port = &nsim_dev_port->devlink_port; 1424 1425 list_del(&nsim_dev_port->list); 1426 if (nsim_dev_port_is_vf(nsim_dev_port)) 1427 devlink_rate_leaf_destroy(&nsim_dev_port->devlink_port); 1428 devlink_port_type_clear(devlink_port); 1429 nsim_destroy(nsim_dev_port->ns); 1430 nsim_dev_port_debugfs_exit(nsim_dev_port); 1431 devlink_port_unregister(devlink_port); 1432 kfree(nsim_dev_port); 1433 } 1434 1435 static void nsim_dev_port_del_all(struct nsim_dev *nsim_dev) 1436 { 1437 struct nsim_dev_port *nsim_dev_port, *tmp; 1438 1439 mutex_lock(&nsim_dev->port_list_lock); 1440 list_for_each_entry_safe(nsim_dev_port, tmp, 1441 &nsim_dev->port_list, list) 1442 __nsim_dev_port_del(nsim_dev_port); 1443 mutex_unlock(&nsim_dev->port_list_lock); 1444 } 1445 1446 static int nsim_dev_port_add_all(struct nsim_dev *nsim_dev, 1447 unsigned int port_count) 1448 { 1449 int i, err; 1450 1451 for (i = 0; i < port_count; i++) { 1452 err = __nsim_dev_port_add(nsim_dev, NSIM_DEV_PORT_TYPE_PF, i); 1453 if (err) 1454 goto err_port_del_all; 1455 } 1456 return 0; 1457 1458 err_port_del_all: 1459 nsim_dev_port_del_all(nsim_dev); 1460 return err; 1461 } 1462 1463 static int nsim_dev_reload_create(struct nsim_dev *nsim_dev, 1464 struct netlink_ext_ack *extack) 1465 { 1466 struct nsim_bus_dev *nsim_bus_dev = nsim_dev->nsim_bus_dev; 1467 struct devlink *devlink; 1468 int err; 1469 1470 devlink = priv_to_devlink(nsim_dev); 1471 nsim_dev = devlink_priv(devlink); 1472 INIT_LIST_HEAD(&nsim_dev->port_list); 1473 mutex_init(&nsim_dev->port_list_lock); 1474 nsim_dev->fw_update_status = true; 1475 nsim_dev->fw_update_overwrite_mask = 0; 1476 1477 nsim_devlink_param_load_driverinit_values(devlink); 1478 1479 err = nsim_dev_dummy_region_init(nsim_dev, devlink); 1480 if (err) 1481 return err; 1482 1483 err = nsim_dev_traps_init(devlink); 1484 if (err) 1485 goto err_dummy_region_exit; 1486 1487 nsim_dev->fib_data = nsim_fib_create(devlink, extack); 1488 if (IS_ERR(nsim_dev->fib_data)) { 1489 err = PTR_ERR(nsim_dev->fib_data); 1490 goto err_traps_exit; 1491 } 1492 1493 err = nsim_dev_health_init(nsim_dev, devlink); 1494 if (err) 1495 goto err_fib_destroy; 1496 1497 err = nsim_dev_psample_init(nsim_dev); 1498 if (err) 1499 goto err_health_exit; 1500 1501 err = nsim_dev_hwstats_init(nsim_dev); 1502 if (err) 1503 goto err_psample_exit; 1504 1505 err = nsim_dev_port_add_all(nsim_dev, nsim_bus_dev->port_count); 1506 if (err) 1507 goto err_hwstats_exit; 1508 1509 nsim_dev->take_snapshot = debugfs_create_file("take_snapshot", 1510 0200, 1511 nsim_dev->ddir, 1512 nsim_dev, 1513 &nsim_dev_take_snapshot_fops); 1514 return 0; 1515 1516 err_hwstats_exit: 1517 nsim_dev_hwstats_exit(nsim_dev); 1518 err_psample_exit: 1519 nsim_dev_psample_exit(nsim_dev); 1520 err_health_exit: 1521 nsim_dev_health_exit(nsim_dev); 1522 err_fib_destroy: 1523 nsim_fib_destroy(devlink, nsim_dev->fib_data); 1524 err_traps_exit: 1525 nsim_dev_traps_exit(devlink); 1526 err_dummy_region_exit: 1527 nsim_dev_dummy_region_exit(nsim_dev); 1528 return err; 1529 } 1530 1531 int nsim_drv_probe(struct nsim_bus_dev *nsim_bus_dev) 1532 { 1533 struct nsim_dev *nsim_dev; 1534 struct devlink *devlink; 1535 int err; 1536 1537 devlink = devlink_alloc_ns(&nsim_dev_devlink_ops, sizeof(*nsim_dev), 1538 nsim_bus_dev->initial_net, &nsim_bus_dev->dev); 1539 if (!devlink) 1540 return -ENOMEM; 1541 nsim_dev = devlink_priv(devlink); 1542 nsim_dev->nsim_bus_dev = nsim_bus_dev; 1543 nsim_dev->switch_id.id_len = sizeof(nsim_dev->switch_id.id); 1544 get_random_bytes(nsim_dev->switch_id.id, nsim_dev->switch_id.id_len); 1545 INIT_LIST_HEAD(&nsim_dev->port_list); 1546 mutex_init(&nsim_dev->vfs_lock); 1547 mutex_init(&nsim_dev->port_list_lock); 1548 nsim_dev->fw_update_status = true; 1549 nsim_dev->fw_update_overwrite_mask = 0; 1550 nsim_dev->max_macs = NSIM_DEV_MAX_MACS_DEFAULT; 1551 nsim_dev->test1 = NSIM_DEV_TEST1_DEFAULT; 1552 spin_lock_init(&nsim_dev->fa_cookie_lock); 1553 1554 dev_set_drvdata(&nsim_bus_dev->dev, nsim_dev); 1555 1556 nsim_dev->vfconfigs = kcalloc(nsim_bus_dev->max_vfs, 1557 sizeof(struct nsim_vf_config), 1558 GFP_KERNEL | __GFP_NOWARN); 1559 if (!nsim_dev->vfconfigs) { 1560 err = -ENOMEM; 1561 goto err_devlink_free; 1562 } 1563 1564 err = nsim_dev_resources_register(devlink); 1565 if (err) 1566 goto err_vfc_free; 1567 1568 err = devlink_params_register(devlink, nsim_devlink_params, 1569 ARRAY_SIZE(nsim_devlink_params)); 1570 if (err) 1571 goto err_dl_unregister; 1572 nsim_devlink_set_params_init_values(nsim_dev, devlink); 1573 1574 err = nsim_dev_dummy_region_init(nsim_dev, devlink); 1575 if (err) 1576 goto err_params_unregister; 1577 1578 err = nsim_dev_traps_init(devlink); 1579 if (err) 1580 goto err_dummy_region_exit; 1581 1582 err = nsim_dev_debugfs_init(nsim_dev); 1583 if (err) 1584 goto err_traps_exit; 1585 1586 nsim_dev->fib_data = nsim_fib_create(devlink, NULL); 1587 if (IS_ERR(nsim_dev->fib_data)) { 1588 err = PTR_ERR(nsim_dev->fib_data); 1589 goto err_debugfs_exit; 1590 } 1591 1592 err = nsim_dev_health_init(nsim_dev, devlink); 1593 if (err) 1594 goto err_fib_destroy; 1595 1596 err = nsim_bpf_dev_init(nsim_dev); 1597 if (err) 1598 goto err_health_exit; 1599 1600 err = nsim_dev_psample_init(nsim_dev); 1601 if (err) 1602 goto err_bpf_dev_exit; 1603 1604 err = nsim_dev_hwstats_init(nsim_dev); 1605 if (err) 1606 goto err_psample_exit; 1607 1608 err = nsim_dev_port_add_all(nsim_dev, nsim_bus_dev->port_count); 1609 if (err) 1610 goto err_hwstats_exit; 1611 1612 nsim_dev->esw_mode = DEVLINK_ESWITCH_MODE_LEGACY; 1613 devlink_set_features(devlink, DEVLINK_F_RELOAD); 1614 devlink_register(devlink); 1615 return 0; 1616 1617 err_hwstats_exit: 1618 nsim_dev_hwstats_exit(nsim_dev); 1619 err_psample_exit: 1620 nsim_dev_psample_exit(nsim_dev); 1621 err_bpf_dev_exit: 1622 nsim_bpf_dev_exit(nsim_dev); 1623 err_health_exit: 1624 nsim_dev_health_exit(nsim_dev); 1625 err_fib_destroy: 1626 nsim_fib_destroy(devlink, nsim_dev->fib_data); 1627 err_debugfs_exit: 1628 nsim_dev_debugfs_exit(nsim_dev); 1629 err_traps_exit: 1630 nsim_dev_traps_exit(devlink); 1631 err_dummy_region_exit: 1632 nsim_dev_dummy_region_exit(nsim_dev); 1633 err_params_unregister: 1634 devlink_params_unregister(devlink, nsim_devlink_params, 1635 ARRAY_SIZE(nsim_devlink_params)); 1636 err_dl_unregister: 1637 devlink_resources_unregister(devlink); 1638 err_vfc_free: 1639 kfree(nsim_dev->vfconfigs); 1640 err_devlink_free: 1641 devlink_free(devlink); 1642 dev_set_drvdata(&nsim_bus_dev->dev, NULL); 1643 return err; 1644 } 1645 1646 static void nsim_dev_reload_destroy(struct nsim_dev *nsim_dev) 1647 { 1648 struct devlink *devlink = priv_to_devlink(nsim_dev); 1649 1650 if (devlink_is_reload_failed(devlink)) 1651 return; 1652 debugfs_remove(nsim_dev->take_snapshot); 1653 1654 mutex_lock(&nsim_dev->vfs_lock); 1655 if (nsim_dev_get_vfs(nsim_dev)) { 1656 nsim_bus_dev_set_vfs(nsim_dev->nsim_bus_dev, 0); 1657 if (nsim_esw_mode_is_switchdev(nsim_dev)) 1658 nsim_esw_legacy_enable(nsim_dev, NULL); 1659 } 1660 mutex_unlock(&nsim_dev->vfs_lock); 1661 1662 nsim_dev_port_del_all(nsim_dev); 1663 nsim_dev_hwstats_exit(nsim_dev); 1664 nsim_dev_psample_exit(nsim_dev); 1665 nsim_dev_health_exit(nsim_dev); 1666 nsim_fib_destroy(devlink, nsim_dev->fib_data); 1667 nsim_dev_traps_exit(devlink); 1668 nsim_dev_dummy_region_exit(nsim_dev); 1669 mutex_destroy(&nsim_dev->port_list_lock); 1670 } 1671 1672 void nsim_drv_remove(struct nsim_bus_dev *nsim_bus_dev) 1673 { 1674 struct nsim_dev *nsim_dev = dev_get_drvdata(&nsim_bus_dev->dev); 1675 struct devlink *devlink = priv_to_devlink(nsim_dev); 1676 1677 devlink_unregister(devlink); 1678 nsim_dev_reload_destroy(nsim_dev); 1679 1680 nsim_bpf_dev_exit(nsim_dev); 1681 nsim_dev_debugfs_exit(nsim_dev); 1682 devlink_params_unregister(devlink, nsim_devlink_params, 1683 ARRAY_SIZE(nsim_devlink_params)); 1684 devlink_resources_unregister(devlink); 1685 kfree(nsim_dev->vfconfigs); 1686 devlink_free(devlink); 1687 dev_set_drvdata(&nsim_bus_dev->dev, NULL); 1688 } 1689 1690 static struct nsim_dev_port * 1691 __nsim_dev_port_lookup(struct nsim_dev *nsim_dev, enum nsim_dev_port_type type, 1692 unsigned int port_index) 1693 { 1694 struct nsim_dev_port *nsim_dev_port; 1695 1696 port_index = nsim_dev_port_index(type, port_index); 1697 list_for_each_entry(nsim_dev_port, &nsim_dev->port_list, list) 1698 if (nsim_dev_port->port_index == port_index) 1699 return nsim_dev_port; 1700 return NULL; 1701 } 1702 1703 int nsim_drv_port_add(struct nsim_bus_dev *nsim_bus_dev, enum nsim_dev_port_type type, 1704 unsigned int port_index) 1705 { 1706 struct nsim_dev *nsim_dev = dev_get_drvdata(&nsim_bus_dev->dev); 1707 int err; 1708 1709 mutex_lock(&nsim_dev->port_list_lock); 1710 if (__nsim_dev_port_lookup(nsim_dev, type, port_index)) 1711 err = -EEXIST; 1712 else 1713 err = __nsim_dev_port_add(nsim_dev, type, port_index); 1714 mutex_unlock(&nsim_dev->port_list_lock); 1715 return err; 1716 } 1717 1718 int nsim_drv_port_del(struct nsim_bus_dev *nsim_bus_dev, enum nsim_dev_port_type type, 1719 unsigned int port_index) 1720 { 1721 struct nsim_dev *nsim_dev = dev_get_drvdata(&nsim_bus_dev->dev); 1722 struct nsim_dev_port *nsim_dev_port; 1723 int err = 0; 1724 1725 mutex_lock(&nsim_dev->port_list_lock); 1726 nsim_dev_port = __nsim_dev_port_lookup(nsim_dev, type, port_index); 1727 if (!nsim_dev_port) 1728 err = -ENOENT; 1729 else 1730 __nsim_dev_port_del(nsim_dev_port); 1731 mutex_unlock(&nsim_dev->port_list_lock); 1732 return err; 1733 } 1734 1735 int nsim_drv_configure_vfs(struct nsim_bus_dev *nsim_bus_dev, 1736 unsigned int num_vfs) 1737 { 1738 struct nsim_dev *nsim_dev = dev_get_drvdata(&nsim_bus_dev->dev); 1739 int ret = 0; 1740 1741 mutex_lock(&nsim_dev->vfs_lock); 1742 if (nsim_bus_dev->num_vfs == num_vfs) 1743 goto exit_unlock; 1744 if (nsim_bus_dev->num_vfs && num_vfs) { 1745 ret = -EBUSY; 1746 goto exit_unlock; 1747 } 1748 if (nsim_bus_dev->max_vfs < num_vfs) { 1749 ret = -ENOMEM; 1750 goto exit_unlock; 1751 } 1752 1753 nsim_bus_dev_set_vfs(nsim_bus_dev, num_vfs); 1754 if (nsim_esw_mode_is_switchdev(nsim_dev)) { 1755 if (num_vfs) { 1756 ret = nsim_esw_switchdev_enable(nsim_dev, NULL); 1757 if (ret) { 1758 nsim_bus_dev_set_vfs(nsim_bus_dev, 0); 1759 goto exit_unlock; 1760 } 1761 } else { 1762 nsim_esw_legacy_enable(nsim_dev, NULL); 1763 } 1764 } 1765 1766 exit_unlock: 1767 mutex_unlock(&nsim_dev->vfs_lock); 1768 1769 return ret; 1770 } 1771 1772 int nsim_dev_init(void) 1773 { 1774 nsim_dev_ddir = debugfs_create_dir(DRV_NAME, NULL); 1775 return PTR_ERR_OR_ZERO(nsim_dev_ddir); 1776 } 1777 1778 void nsim_dev_exit(void) 1779 { 1780 debugfs_remove_recursive(nsim_dev_ddir); 1781 } 1782