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