18c2ecf20Sopenharmony_ci/*
28c2ecf20Sopenharmony_ci * Copyright (c) 2018 Cumulus Networks. All rights reserved.
38c2ecf20Sopenharmony_ci * Copyright (c) 2018 David Ahern <dsa@cumulusnetworks.com>
48c2ecf20Sopenharmony_ci * Copyright (c) 2019 Mellanox Technologies. All rights reserved.
58c2ecf20Sopenharmony_ci *
68c2ecf20Sopenharmony_ci * This software is licensed under the GNU General License Version 2,
78c2ecf20Sopenharmony_ci * June 1991 as shown in the file COPYING in the top-level directory of this
88c2ecf20Sopenharmony_ci * source tree.
98c2ecf20Sopenharmony_ci *
108c2ecf20Sopenharmony_ci * THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS"
118c2ecf20Sopenharmony_ci * WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
128c2ecf20Sopenharmony_ci * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
138c2ecf20Sopenharmony_ci * FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE
148c2ecf20Sopenharmony_ci * OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME
158c2ecf20Sopenharmony_ci * THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
168c2ecf20Sopenharmony_ci */
178c2ecf20Sopenharmony_ci
188c2ecf20Sopenharmony_ci#include <linux/debugfs.h>
198c2ecf20Sopenharmony_ci#include <linux/device.h>
208c2ecf20Sopenharmony_ci#include <linux/etherdevice.h>
218c2ecf20Sopenharmony_ci#include <linux/inet.h>
228c2ecf20Sopenharmony_ci#include <linux/jiffies.h>
238c2ecf20Sopenharmony_ci#include <linux/kernel.h>
248c2ecf20Sopenharmony_ci#include <linux/list.h>
258c2ecf20Sopenharmony_ci#include <linux/mutex.h>
268c2ecf20Sopenharmony_ci#include <linux/random.h>
278c2ecf20Sopenharmony_ci#include <linux/rtnetlink.h>
288c2ecf20Sopenharmony_ci#include <linux/workqueue.h>
298c2ecf20Sopenharmony_ci#include <net/devlink.h>
308c2ecf20Sopenharmony_ci#include <net/ip.h>
318c2ecf20Sopenharmony_ci#include <net/flow_offload.h>
328c2ecf20Sopenharmony_ci#include <uapi/linux/devlink.h>
338c2ecf20Sopenharmony_ci#include <uapi/linux/ip.h>
348c2ecf20Sopenharmony_ci#include <uapi/linux/udp.h>
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_ci#include "netdevsim.h"
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_cistatic struct dentry *nsim_dev_ddir;
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_ci#define NSIM_DEV_DUMMY_REGION_SIZE (1024 * 32)
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_cistatic int
438c2ecf20Sopenharmony_cinsim_dev_take_snapshot(struct devlink *devlink,
448c2ecf20Sopenharmony_ci		       const struct devlink_region_ops *ops,
458c2ecf20Sopenharmony_ci		       struct netlink_ext_ack *extack,
468c2ecf20Sopenharmony_ci		       u8 **data)
478c2ecf20Sopenharmony_ci{
488c2ecf20Sopenharmony_ci	void *dummy_data;
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_ci	dummy_data = kmalloc(NSIM_DEV_DUMMY_REGION_SIZE, GFP_KERNEL);
518c2ecf20Sopenharmony_ci	if (!dummy_data)
528c2ecf20Sopenharmony_ci		return -ENOMEM;
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_ci	get_random_bytes(dummy_data, NSIM_DEV_DUMMY_REGION_SIZE);
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_ci	*data = dummy_data;
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_ci	return 0;
598c2ecf20Sopenharmony_ci}
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_cistatic ssize_t nsim_dev_take_snapshot_write(struct file *file,
628c2ecf20Sopenharmony_ci					    const char __user *data,
638c2ecf20Sopenharmony_ci					    size_t count, loff_t *ppos)
648c2ecf20Sopenharmony_ci{
658c2ecf20Sopenharmony_ci	struct nsim_dev *nsim_dev = file->private_data;
668c2ecf20Sopenharmony_ci	struct devlink *devlink;
678c2ecf20Sopenharmony_ci	u8 *dummy_data;
688c2ecf20Sopenharmony_ci	int err;
698c2ecf20Sopenharmony_ci	u32 id;
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_ci	devlink = priv_to_devlink(nsim_dev);
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_ci	err = nsim_dev_take_snapshot(devlink, NULL, NULL, &dummy_data);
748c2ecf20Sopenharmony_ci	if (err)
758c2ecf20Sopenharmony_ci		return err;
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_ci	err = devlink_region_snapshot_id_get(devlink, &id);
788c2ecf20Sopenharmony_ci	if (err) {
798c2ecf20Sopenharmony_ci		pr_err("Failed to get snapshot id\n");
808c2ecf20Sopenharmony_ci		kfree(dummy_data);
818c2ecf20Sopenharmony_ci		return err;
828c2ecf20Sopenharmony_ci	}
838c2ecf20Sopenharmony_ci	err = devlink_region_snapshot_create(nsim_dev->dummy_region,
848c2ecf20Sopenharmony_ci					     dummy_data, id);
858c2ecf20Sopenharmony_ci	devlink_region_snapshot_id_put(devlink, id);
868c2ecf20Sopenharmony_ci	if (err) {
878c2ecf20Sopenharmony_ci		pr_err("Failed to create region snapshot\n");
888c2ecf20Sopenharmony_ci		kfree(dummy_data);
898c2ecf20Sopenharmony_ci		return err;
908c2ecf20Sopenharmony_ci	}
918c2ecf20Sopenharmony_ci
928c2ecf20Sopenharmony_ci	return count;
938c2ecf20Sopenharmony_ci}
948c2ecf20Sopenharmony_ci
958c2ecf20Sopenharmony_cistatic const struct file_operations nsim_dev_take_snapshot_fops = {
968c2ecf20Sopenharmony_ci	.open = simple_open,
978c2ecf20Sopenharmony_ci	.write = nsim_dev_take_snapshot_write,
988c2ecf20Sopenharmony_ci	.llseek = generic_file_llseek,
998c2ecf20Sopenharmony_ci	.owner = THIS_MODULE,
1008c2ecf20Sopenharmony_ci};
1018c2ecf20Sopenharmony_ci
1028c2ecf20Sopenharmony_cistatic ssize_t nsim_dev_trap_fa_cookie_read(struct file *file,
1038c2ecf20Sopenharmony_ci					    char __user *data,
1048c2ecf20Sopenharmony_ci					    size_t count, loff_t *ppos)
1058c2ecf20Sopenharmony_ci{
1068c2ecf20Sopenharmony_ci	struct nsim_dev *nsim_dev = file->private_data;
1078c2ecf20Sopenharmony_ci	struct flow_action_cookie *fa_cookie;
1088c2ecf20Sopenharmony_ci	unsigned int buf_len;
1098c2ecf20Sopenharmony_ci	ssize_t ret;
1108c2ecf20Sopenharmony_ci	char *buf;
1118c2ecf20Sopenharmony_ci
1128c2ecf20Sopenharmony_ci	spin_lock(&nsim_dev->fa_cookie_lock);
1138c2ecf20Sopenharmony_ci	fa_cookie = nsim_dev->fa_cookie;
1148c2ecf20Sopenharmony_ci	if (!fa_cookie) {
1158c2ecf20Sopenharmony_ci		ret = -EINVAL;
1168c2ecf20Sopenharmony_ci		goto errout;
1178c2ecf20Sopenharmony_ci	}
1188c2ecf20Sopenharmony_ci	buf_len = fa_cookie->cookie_len * 2;
1198c2ecf20Sopenharmony_ci	buf = kmalloc(buf_len, GFP_ATOMIC);
1208c2ecf20Sopenharmony_ci	if (!buf) {
1218c2ecf20Sopenharmony_ci		ret = -ENOMEM;
1228c2ecf20Sopenharmony_ci		goto errout;
1238c2ecf20Sopenharmony_ci	}
1248c2ecf20Sopenharmony_ci	bin2hex(buf, fa_cookie->cookie, fa_cookie->cookie_len);
1258c2ecf20Sopenharmony_ci	spin_unlock(&nsim_dev->fa_cookie_lock);
1268c2ecf20Sopenharmony_ci
1278c2ecf20Sopenharmony_ci	ret = simple_read_from_buffer(data, count, ppos, buf, buf_len);
1288c2ecf20Sopenharmony_ci
1298c2ecf20Sopenharmony_ci	kfree(buf);
1308c2ecf20Sopenharmony_ci	return ret;
1318c2ecf20Sopenharmony_ci
1328c2ecf20Sopenharmony_cierrout:
1338c2ecf20Sopenharmony_ci	spin_unlock(&nsim_dev->fa_cookie_lock);
1348c2ecf20Sopenharmony_ci	return ret;
1358c2ecf20Sopenharmony_ci}
1368c2ecf20Sopenharmony_ci
1378c2ecf20Sopenharmony_cistatic ssize_t nsim_dev_trap_fa_cookie_write(struct file *file,
1388c2ecf20Sopenharmony_ci					     const char __user *data,
1398c2ecf20Sopenharmony_ci					     size_t count, loff_t *ppos)
1408c2ecf20Sopenharmony_ci{
1418c2ecf20Sopenharmony_ci	struct nsim_dev *nsim_dev = file->private_data;
1428c2ecf20Sopenharmony_ci	struct flow_action_cookie *fa_cookie;
1438c2ecf20Sopenharmony_ci	size_t cookie_len;
1448c2ecf20Sopenharmony_ci	ssize_t ret;
1458c2ecf20Sopenharmony_ci	char *buf;
1468c2ecf20Sopenharmony_ci
1478c2ecf20Sopenharmony_ci	if (*ppos != 0)
1488c2ecf20Sopenharmony_ci		return -EINVAL;
1498c2ecf20Sopenharmony_ci	cookie_len = (count - 1) / 2;
1508c2ecf20Sopenharmony_ci	if ((count - 1) % 2)
1518c2ecf20Sopenharmony_ci		return -EINVAL;
1528c2ecf20Sopenharmony_ci
1538c2ecf20Sopenharmony_ci	buf = memdup_user(data, count);
1548c2ecf20Sopenharmony_ci	if (IS_ERR(buf))
1558c2ecf20Sopenharmony_ci		return PTR_ERR(buf);
1568c2ecf20Sopenharmony_ci
1578c2ecf20Sopenharmony_ci	fa_cookie = kmalloc(sizeof(*fa_cookie) + cookie_len,
1588c2ecf20Sopenharmony_ci			    GFP_KERNEL | __GFP_NOWARN);
1598c2ecf20Sopenharmony_ci	if (!fa_cookie) {
1608c2ecf20Sopenharmony_ci		ret = -ENOMEM;
1618c2ecf20Sopenharmony_ci		goto free_buf;
1628c2ecf20Sopenharmony_ci	}
1638c2ecf20Sopenharmony_ci
1648c2ecf20Sopenharmony_ci	fa_cookie->cookie_len = cookie_len;
1658c2ecf20Sopenharmony_ci	ret = hex2bin(fa_cookie->cookie, buf, cookie_len);
1668c2ecf20Sopenharmony_ci	if (ret)
1678c2ecf20Sopenharmony_ci		goto free_fa_cookie;
1688c2ecf20Sopenharmony_ci	kfree(buf);
1698c2ecf20Sopenharmony_ci
1708c2ecf20Sopenharmony_ci	spin_lock(&nsim_dev->fa_cookie_lock);
1718c2ecf20Sopenharmony_ci	kfree(nsim_dev->fa_cookie);
1728c2ecf20Sopenharmony_ci	nsim_dev->fa_cookie = fa_cookie;
1738c2ecf20Sopenharmony_ci	spin_unlock(&nsim_dev->fa_cookie_lock);
1748c2ecf20Sopenharmony_ci
1758c2ecf20Sopenharmony_ci	return count;
1768c2ecf20Sopenharmony_ci
1778c2ecf20Sopenharmony_cifree_fa_cookie:
1788c2ecf20Sopenharmony_ci	kfree(fa_cookie);
1798c2ecf20Sopenharmony_cifree_buf:
1808c2ecf20Sopenharmony_ci	kfree(buf);
1818c2ecf20Sopenharmony_ci	return ret;
1828c2ecf20Sopenharmony_ci}
1838c2ecf20Sopenharmony_ci
1848c2ecf20Sopenharmony_cistatic const struct file_operations nsim_dev_trap_fa_cookie_fops = {
1858c2ecf20Sopenharmony_ci	.open = simple_open,
1868c2ecf20Sopenharmony_ci	.read = nsim_dev_trap_fa_cookie_read,
1878c2ecf20Sopenharmony_ci	.write = nsim_dev_trap_fa_cookie_write,
1888c2ecf20Sopenharmony_ci	.llseek = generic_file_llseek,
1898c2ecf20Sopenharmony_ci	.owner = THIS_MODULE,
1908c2ecf20Sopenharmony_ci};
1918c2ecf20Sopenharmony_ci
1928c2ecf20Sopenharmony_cistatic int nsim_dev_debugfs_init(struct nsim_dev *nsim_dev)
1938c2ecf20Sopenharmony_ci{
1948c2ecf20Sopenharmony_ci	char dev_ddir_name[sizeof(DRV_NAME) + 10];
1958c2ecf20Sopenharmony_ci
1968c2ecf20Sopenharmony_ci	sprintf(dev_ddir_name, DRV_NAME "%u", nsim_dev->nsim_bus_dev->dev.id);
1978c2ecf20Sopenharmony_ci	nsim_dev->ddir = debugfs_create_dir(dev_ddir_name, nsim_dev_ddir);
1988c2ecf20Sopenharmony_ci	if (IS_ERR(nsim_dev->ddir))
1998c2ecf20Sopenharmony_ci		return PTR_ERR(nsim_dev->ddir);
2008c2ecf20Sopenharmony_ci	nsim_dev->ports_ddir = debugfs_create_dir("ports", nsim_dev->ddir);
2018c2ecf20Sopenharmony_ci	if (IS_ERR(nsim_dev->ports_ddir))
2028c2ecf20Sopenharmony_ci		return PTR_ERR(nsim_dev->ports_ddir);
2038c2ecf20Sopenharmony_ci	debugfs_create_bool("fw_update_status", 0600, nsim_dev->ddir,
2048c2ecf20Sopenharmony_ci			    &nsim_dev->fw_update_status);
2058c2ecf20Sopenharmony_ci	debugfs_create_u32("fw_update_overwrite_mask", 0600, nsim_dev->ddir,
2068c2ecf20Sopenharmony_ci			    &nsim_dev->fw_update_overwrite_mask);
2078c2ecf20Sopenharmony_ci	debugfs_create_u32("max_macs", 0600, nsim_dev->ddir,
2088c2ecf20Sopenharmony_ci			   &nsim_dev->max_macs);
2098c2ecf20Sopenharmony_ci	debugfs_create_bool("test1", 0600, nsim_dev->ddir,
2108c2ecf20Sopenharmony_ci			    &nsim_dev->test1);
2118c2ecf20Sopenharmony_ci	nsim_dev->take_snapshot = debugfs_create_file("take_snapshot",
2128c2ecf20Sopenharmony_ci						      0200,
2138c2ecf20Sopenharmony_ci						      nsim_dev->ddir,
2148c2ecf20Sopenharmony_ci						      nsim_dev,
2158c2ecf20Sopenharmony_ci						&nsim_dev_take_snapshot_fops);
2168c2ecf20Sopenharmony_ci	debugfs_create_bool("dont_allow_reload", 0600, nsim_dev->ddir,
2178c2ecf20Sopenharmony_ci			    &nsim_dev->dont_allow_reload);
2188c2ecf20Sopenharmony_ci	debugfs_create_bool("fail_reload", 0600, nsim_dev->ddir,
2198c2ecf20Sopenharmony_ci			    &nsim_dev->fail_reload);
2208c2ecf20Sopenharmony_ci	debugfs_create_file("trap_flow_action_cookie", 0600, nsim_dev->ddir,
2218c2ecf20Sopenharmony_ci			    nsim_dev, &nsim_dev_trap_fa_cookie_fops);
2228c2ecf20Sopenharmony_ci	debugfs_create_bool("fail_trap_group_set", 0600,
2238c2ecf20Sopenharmony_ci			    nsim_dev->ddir,
2248c2ecf20Sopenharmony_ci			    &nsim_dev->fail_trap_group_set);
2258c2ecf20Sopenharmony_ci	debugfs_create_bool("fail_trap_policer_set", 0600,
2268c2ecf20Sopenharmony_ci			    nsim_dev->ddir,
2278c2ecf20Sopenharmony_ci			    &nsim_dev->fail_trap_policer_set);
2288c2ecf20Sopenharmony_ci	debugfs_create_bool("fail_trap_policer_counter_get", 0600,
2298c2ecf20Sopenharmony_ci			    nsim_dev->ddir,
2308c2ecf20Sopenharmony_ci			    &nsim_dev->fail_trap_policer_counter_get);
2318c2ecf20Sopenharmony_ci	nsim_udp_tunnels_debugfs_create(nsim_dev);
2328c2ecf20Sopenharmony_ci	return 0;
2338c2ecf20Sopenharmony_ci}
2348c2ecf20Sopenharmony_ci
2358c2ecf20Sopenharmony_cistatic void nsim_dev_debugfs_exit(struct nsim_dev *nsim_dev)
2368c2ecf20Sopenharmony_ci{
2378c2ecf20Sopenharmony_ci	debugfs_remove_recursive(nsim_dev->ports_ddir);
2388c2ecf20Sopenharmony_ci	debugfs_remove_recursive(nsim_dev->ddir);
2398c2ecf20Sopenharmony_ci}
2408c2ecf20Sopenharmony_ci
2418c2ecf20Sopenharmony_cistatic int nsim_dev_port_debugfs_init(struct nsim_dev *nsim_dev,
2428c2ecf20Sopenharmony_ci				      struct nsim_dev_port *nsim_dev_port)
2438c2ecf20Sopenharmony_ci{
2448c2ecf20Sopenharmony_ci	char port_ddir_name[16];
2458c2ecf20Sopenharmony_ci	char dev_link_name[32];
2468c2ecf20Sopenharmony_ci
2478c2ecf20Sopenharmony_ci	sprintf(port_ddir_name, "%u", nsim_dev_port->port_index);
2488c2ecf20Sopenharmony_ci	nsim_dev_port->ddir = debugfs_create_dir(port_ddir_name,
2498c2ecf20Sopenharmony_ci						 nsim_dev->ports_ddir);
2508c2ecf20Sopenharmony_ci	if (IS_ERR(nsim_dev_port->ddir))
2518c2ecf20Sopenharmony_ci		return PTR_ERR(nsim_dev_port->ddir);
2528c2ecf20Sopenharmony_ci
2538c2ecf20Sopenharmony_ci	sprintf(dev_link_name, "../../../" DRV_NAME "%u",
2548c2ecf20Sopenharmony_ci		nsim_dev->nsim_bus_dev->dev.id);
2558c2ecf20Sopenharmony_ci	debugfs_create_symlink("dev", nsim_dev_port->ddir, dev_link_name);
2568c2ecf20Sopenharmony_ci
2578c2ecf20Sopenharmony_ci	return 0;
2588c2ecf20Sopenharmony_ci}
2598c2ecf20Sopenharmony_ci
2608c2ecf20Sopenharmony_cistatic void nsim_dev_port_debugfs_exit(struct nsim_dev_port *nsim_dev_port)
2618c2ecf20Sopenharmony_ci{
2628c2ecf20Sopenharmony_ci	debugfs_remove_recursive(nsim_dev_port->ddir);
2638c2ecf20Sopenharmony_ci}
2648c2ecf20Sopenharmony_ci
2658c2ecf20Sopenharmony_cistatic int nsim_dev_resources_register(struct devlink *devlink)
2668c2ecf20Sopenharmony_ci{
2678c2ecf20Sopenharmony_ci	struct devlink_resource_size_params params = {
2688c2ecf20Sopenharmony_ci		.size_max = (u64)-1,
2698c2ecf20Sopenharmony_ci		.size_granularity = 1,
2708c2ecf20Sopenharmony_ci		.unit = DEVLINK_RESOURCE_UNIT_ENTRY
2718c2ecf20Sopenharmony_ci	};
2728c2ecf20Sopenharmony_ci	int err;
2738c2ecf20Sopenharmony_ci
2748c2ecf20Sopenharmony_ci	/* Resources for IPv4 */
2758c2ecf20Sopenharmony_ci	err = devlink_resource_register(devlink, "IPv4", (u64)-1,
2768c2ecf20Sopenharmony_ci					NSIM_RESOURCE_IPV4,
2778c2ecf20Sopenharmony_ci					DEVLINK_RESOURCE_ID_PARENT_TOP,
2788c2ecf20Sopenharmony_ci					&params);
2798c2ecf20Sopenharmony_ci	if (err) {
2808c2ecf20Sopenharmony_ci		pr_err("Failed to register IPv4 top resource\n");
2818c2ecf20Sopenharmony_ci		goto out;
2828c2ecf20Sopenharmony_ci	}
2838c2ecf20Sopenharmony_ci
2848c2ecf20Sopenharmony_ci	err = devlink_resource_register(devlink, "fib", (u64)-1,
2858c2ecf20Sopenharmony_ci					NSIM_RESOURCE_IPV4_FIB,
2868c2ecf20Sopenharmony_ci					NSIM_RESOURCE_IPV4, &params);
2878c2ecf20Sopenharmony_ci	if (err) {
2888c2ecf20Sopenharmony_ci		pr_err("Failed to register IPv4 FIB resource\n");
2898c2ecf20Sopenharmony_ci		return err;
2908c2ecf20Sopenharmony_ci	}
2918c2ecf20Sopenharmony_ci
2928c2ecf20Sopenharmony_ci	err = devlink_resource_register(devlink, "fib-rules", (u64)-1,
2938c2ecf20Sopenharmony_ci					NSIM_RESOURCE_IPV4_FIB_RULES,
2948c2ecf20Sopenharmony_ci					NSIM_RESOURCE_IPV4, &params);
2958c2ecf20Sopenharmony_ci	if (err) {
2968c2ecf20Sopenharmony_ci		pr_err("Failed to register IPv4 FIB rules resource\n");
2978c2ecf20Sopenharmony_ci		return err;
2988c2ecf20Sopenharmony_ci	}
2998c2ecf20Sopenharmony_ci
3008c2ecf20Sopenharmony_ci	/* Resources for IPv6 */
3018c2ecf20Sopenharmony_ci	err = devlink_resource_register(devlink, "IPv6", (u64)-1,
3028c2ecf20Sopenharmony_ci					NSIM_RESOURCE_IPV6,
3038c2ecf20Sopenharmony_ci					DEVLINK_RESOURCE_ID_PARENT_TOP,
3048c2ecf20Sopenharmony_ci					&params);
3058c2ecf20Sopenharmony_ci	if (err) {
3068c2ecf20Sopenharmony_ci		pr_err("Failed to register IPv6 top resource\n");
3078c2ecf20Sopenharmony_ci		goto out;
3088c2ecf20Sopenharmony_ci	}
3098c2ecf20Sopenharmony_ci
3108c2ecf20Sopenharmony_ci	err = devlink_resource_register(devlink, "fib", (u64)-1,
3118c2ecf20Sopenharmony_ci					NSIM_RESOURCE_IPV6_FIB,
3128c2ecf20Sopenharmony_ci					NSIM_RESOURCE_IPV6, &params);
3138c2ecf20Sopenharmony_ci	if (err) {
3148c2ecf20Sopenharmony_ci		pr_err("Failed to register IPv6 FIB resource\n");
3158c2ecf20Sopenharmony_ci		return err;
3168c2ecf20Sopenharmony_ci	}
3178c2ecf20Sopenharmony_ci
3188c2ecf20Sopenharmony_ci	err = devlink_resource_register(devlink, "fib-rules", (u64)-1,
3198c2ecf20Sopenharmony_ci					NSIM_RESOURCE_IPV6_FIB_RULES,
3208c2ecf20Sopenharmony_ci					NSIM_RESOURCE_IPV6, &params);
3218c2ecf20Sopenharmony_ci	if (err) {
3228c2ecf20Sopenharmony_ci		pr_err("Failed to register IPv6 FIB rules resource\n");
3238c2ecf20Sopenharmony_ci		return err;
3248c2ecf20Sopenharmony_ci	}
3258c2ecf20Sopenharmony_ci
3268c2ecf20Sopenharmony_ciout:
3278c2ecf20Sopenharmony_ci	return err;
3288c2ecf20Sopenharmony_ci}
3298c2ecf20Sopenharmony_ci
3308c2ecf20Sopenharmony_cienum nsim_devlink_param_id {
3318c2ecf20Sopenharmony_ci	NSIM_DEVLINK_PARAM_ID_BASE = DEVLINK_PARAM_GENERIC_ID_MAX,
3328c2ecf20Sopenharmony_ci	NSIM_DEVLINK_PARAM_ID_TEST1,
3338c2ecf20Sopenharmony_ci};
3348c2ecf20Sopenharmony_ci
3358c2ecf20Sopenharmony_cistatic const struct devlink_param nsim_devlink_params[] = {
3368c2ecf20Sopenharmony_ci	DEVLINK_PARAM_GENERIC(MAX_MACS,
3378c2ecf20Sopenharmony_ci			      BIT(DEVLINK_PARAM_CMODE_DRIVERINIT),
3388c2ecf20Sopenharmony_ci			      NULL, NULL, NULL),
3398c2ecf20Sopenharmony_ci	DEVLINK_PARAM_DRIVER(NSIM_DEVLINK_PARAM_ID_TEST1,
3408c2ecf20Sopenharmony_ci			     "test1", DEVLINK_PARAM_TYPE_BOOL,
3418c2ecf20Sopenharmony_ci			     BIT(DEVLINK_PARAM_CMODE_DRIVERINIT),
3428c2ecf20Sopenharmony_ci			     NULL, NULL, NULL),
3438c2ecf20Sopenharmony_ci};
3448c2ecf20Sopenharmony_ci
3458c2ecf20Sopenharmony_cistatic void nsim_devlink_set_params_init_values(struct nsim_dev *nsim_dev,
3468c2ecf20Sopenharmony_ci						struct devlink *devlink)
3478c2ecf20Sopenharmony_ci{
3488c2ecf20Sopenharmony_ci	union devlink_param_value value;
3498c2ecf20Sopenharmony_ci
3508c2ecf20Sopenharmony_ci	value.vu32 = nsim_dev->max_macs;
3518c2ecf20Sopenharmony_ci	devlink_param_driverinit_value_set(devlink,
3528c2ecf20Sopenharmony_ci					   DEVLINK_PARAM_GENERIC_ID_MAX_MACS,
3538c2ecf20Sopenharmony_ci					   value);
3548c2ecf20Sopenharmony_ci	value.vbool = nsim_dev->test1;
3558c2ecf20Sopenharmony_ci	devlink_param_driverinit_value_set(devlink,
3568c2ecf20Sopenharmony_ci					   NSIM_DEVLINK_PARAM_ID_TEST1,
3578c2ecf20Sopenharmony_ci					   value);
3588c2ecf20Sopenharmony_ci}
3598c2ecf20Sopenharmony_ci
3608c2ecf20Sopenharmony_cistatic void nsim_devlink_param_load_driverinit_values(struct devlink *devlink)
3618c2ecf20Sopenharmony_ci{
3628c2ecf20Sopenharmony_ci	struct nsim_dev *nsim_dev = devlink_priv(devlink);
3638c2ecf20Sopenharmony_ci	union devlink_param_value saved_value;
3648c2ecf20Sopenharmony_ci	int err;
3658c2ecf20Sopenharmony_ci
3668c2ecf20Sopenharmony_ci	err = devlink_param_driverinit_value_get(devlink,
3678c2ecf20Sopenharmony_ci						 DEVLINK_PARAM_GENERIC_ID_MAX_MACS,
3688c2ecf20Sopenharmony_ci						 &saved_value);
3698c2ecf20Sopenharmony_ci	if (!err)
3708c2ecf20Sopenharmony_ci		nsim_dev->max_macs = saved_value.vu32;
3718c2ecf20Sopenharmony_ci	err = devlink_param_driverinit_value_get(devlink,
3728c2ecf20Sopenharmony_ci						 NSIM_DEVLINK_PARAM_ID_TEST1,
3738c2ecf20Sopenharmony_ci						 &saved_value);
3748c2ecf20Sopenharmony_ci	if (!err)
3758c2ecf20Sopenharmony_ci		nsim_dev->test1 = saved_value.vbool;
3768c2ecf20Sopenharmony_ci}
3778c2ecf20Sopenharmony_ci
3788c2ecf20Sopenharmony_ci#define NSIM_DEV_DUMMY_REGION_SNAPSHOT_MAX 16
3798c2ecf20Sopenharmony_ci
3808c2ecf20Sopenharmony_cistatic const struct devlink_region_ops dummy_region_ops = {
3818c2ecf20Sopenharmony_ci	.name = "dummy",
3828c2ecf20Sopenharmony_ci	.destructor = &kfree,
3838c2ecf20Sopenharmony_ci	.snapshot = nsim_dev_take_snapshot,
3848c2ecf20Sopenharmony_ci};
3858c2ecf20Sopenharmony_ci
3868c2ecf20Sopenharmony_cistatic int nsim_dev_dummy_region_init(struct nsim_dev *nsim_dev,
3878c2ecf20Sopenharmony_ci				      struct devlink *devlink)
3888c2ecf20Sopenharmony_ci{
3898c2ecf20Sopenharmony_ci	nsim_dev->dummy_region =
3908c2ecf20Sopenharmony_ci		devlink_region_create(devlink, &dummy_region_ops,
3918c2ecf20Sopenharmony_ci				      NSIM_DEV_DUMMY_REGION_SNAPSHOT_MAX,
3928c2ecf20Sopenharmony_ci				      NSIM_DEV_DUMMY_REGION_SIZE);
3938c2ecf20Sopenharmony_ci	return PTR_ERR_OR_ZERO(nsim_dev->dummy_region);
3948c2ecf20Sopenharmony_ci}
3958c2ecf20Sopenharmony_ci
3968c2ecf20Sopenharmony_cistatic void nsim_dev_dummy_region_exit(struct nsim_dev *nsim_dev)
3978c2ecf20Sopenharmony_ci{
3988c2ecf20Sopenharmony_ci	devlink_region_destroy(nsim_dev->dummy_region);
3998c2ecf20Sopenharmony_ci}
4008c2ecf20Sopenharmony_ci
4018c2ecf20Sopenharmony_cistruct nsim_trap_item {
4028c2ecf20Sopenharmony_ci	void *trap_ctx;
4038c2ecf20Sopenharmony_ci	enum devlink_trap_action action;
4048c2ecf20Sopenharmony_ci};
4058c2ecf20Sopenharmony_ci
4068c2ecf20Sopenharmony_cistruct nsim_trap_data {
4078c2ecf20Sopenharmony_ci	struct delayed_work trap_report_dw;
4088c2ecf20Sopenharmony_ci	struct nsim_trap_item *trap_items_arr;
4098c2ecf20Sopenharmony_ci	u64 *trap_policers_cnt_arr;
4108c2ecf20Sopenharmony_ci	struct nsim_dev *nsim_dev;
4118c2ecf20Sopenharmony_ci	spinlock_t trap_lock;	/* Protects trap_items_arr */
4128c2ecf20Sopenharmony_ci};
4138c2ecf20Sopenharmony_ci
4148c2ecf20Sopenharmony_ci/* All driver-specific traps must be documented in
4158c2ecf20Sopenharmony_ci * Documentation/networking/devlink/netdevsim.rst
4168c2ecf20Sopenharmony_ci */
4178c2ecf20Sopenharmony_cienum {
4188c2ecf20Sopenharmony_ci	NSIM_TRAP_ID_BASE = DEVLINK_TRAP_GENERIC_ID_MAX,
4198c2ecf20Sopenharmony_ci	NSIM_TRAP_ID_FID_MISS,
4208c2ecf20Sopenharmony_ci};
4218c2ecf20Sopenharmony_ci
4228c2ecf20Sopenharmony_ci#define NSIM_TRAP_NAME_FID_MISS "fid_miss"
4238c2ecf20Sopenharmony_ci
4248c2ecf20Sopenharmony_ci#define NSIM_TRAP_METADATA DEVLINK_TRAP_METADATA_TYPE_F_IN_PORT
4258c2ecf20Sopenharmony_ci
4268c2ecf20Sopenharmony_ci#define NSIM_TRAP_DROP(_id, _group_id)					      \
4278c2ecf20Sopenharmony_ci	DEVLINK_TRAP_GENERIC(DROP, DROP, _id,				      \
4288c2ecf20Sopenharmony_ci			     DEVLINK_TRAP_GROUP_GENERIC_ID_##_group_id,	      \
4298c2ecf20Sopenharmony_ci			     NSIM_TRAP_METADATA)
4308c2ecf20Sopenharmony_ci#define NSIM_TRAP_DROP_EXT(_id, _group_id, _metadata)			      \
4318c2ecf20Sopenharmony_ci	DEVLINK_TRAP_GENERIC(DROP, DROP, _id,				      \
4328c2ecf20Sopenharmony_ci			     DEVLINK_TRAP_GROUP_GENERIC_ID_##_group_id,	      \
4338c2ecf20Sopenharmony_ci			     NSIM_TRAP_METADATA | (_metadata))
4348c2ecf20Sopenharmony_ci#define NSIM_TRAP_EXCEPTION(_id, _group_id)				      \
4358c2ecf20Sopenharmony_ci	DEVLINK_TRAP_GENERIC(EXCEPTION, TRAP, _id,			      \
4368c2ecf20Sopenharmony_ci			     DEVLINK_TRAP_GROUP_GENERIC_ID_##_group_id,	      \
4378c2ecf20Sopenharmony_ci			     NSIM_TRAP_METADATA)
4388c2ecf20Sopenharmony_ci#define NSIM_TRAP_CONTROL(_id, _group_id, _action)			      \
4398c2ecf20Sopenharmony_ci	DEVLINK_TRAP_GENERIC(CONTROL, _action, _id,			      \
4408c2ecf20Sopenharmony_ci			     DEVLINK_TRAP_GROUP_GENERIC_ID_##_group_id,	      \
4418c2ecf20Sopenharmony_ci			     NSIM_TRAP_METADATA)
4428c2ecf20Sopenharmony_ci#define NSIM_TRAP_DRIVER_EXCEPTION(_id, _group_id)			      \
4438c2ecf20Sopenharmony_ci	DEVLINK_TRAP_DRIVER(EXCEPTION, TRAP, NSIM_TRAP_ID_##_id,	      \
4448c2ecf20Sopenharmony_ci			    NSIM_TRAP_NAME_##_id,			      \
4458c2ecf20Sopenharmony_ci			    DEVLINK_TRAP_GROUP_GENERIC_ID_##_group_id,	      \
4468c2ecf20Sopenharmony_ci			    NSIM_TRAP_METADATA)
4478c2ecf20Sopenharmony_ci
4488c2ecf20Sopenharmony_ci#define NSIM_DEV_TRAP_POLICER_MIN_RATE	1
4498c2ecf20Sopenharmony_ci#define NSIM_DEV_TRAP_POLICER_MAX_RATE	8000
4508c2ecf20Sopenharmony_ci#define NSIM_DEV_TRAP_POLICER_MIN_BURST	8
4518c2ecf20Sopenharmony_ci#define NSIM_DEV_TRAP_POLICER_MAX_BURST	65536
4528c2ecf20Sopenharmony_ci
4538c2ecf20Sopenharmony_ci#define NSIM_TRAP_POLICER(_id, _rate, _burst)				      \
4548c2ecf20Sopenharmony_ci	DEVLINK_TRAP_POLICER(_id, _rate, _burst,			      \
4558c2ecf20Sopenharmony_ci			     NSIM_DEV_TRAP_POLICER_MAX_RATE,		      \
4568c2ecf20Sopenharmony_ci			     NSIM_DEV_TRAP_POLICER_MIN_RATE,		      \
4578c2ecf20Sopenharmony_ci			     NSIM_DEV_TRAP_POLICER_MAX_BURST,		      \
4588c2ecf20Sopenharmony_ci			     NSIM_DEV_TRAP_POLICER_MIN_BURST)
4598c2ecf20Sopenharmony_ci
4608c2ecf20Sopenharmony_cistatic const struct devlink_trap_policer nsim_trap_policers_arr[] = {
4618c2ecf20Sopenharmony_ci	NSIM_TRAP_POLICER(1, 1000, 128),
4628c2ecf20Sopenharmony_ci	NSIM_TRAP_POLICER(2, 2000, 256),
4638c2ecf20Sopenharmony_ci	NSIM_TRAP_POLICER(3, 3000, 512),
4648c2ecf20Sopenharmony_ci};
4658c2ecf20Sopenharmony_ci
4668c2ecf20Sopenharmony_cistatic const struct devlink_trap_group nsim_trap_groups_arr[] = {
4678c2ecf20Sopenharmony_ci	DEVLINK_TRAP_GROUP_GENERIC(L2_DROPS, 0),
4688c2ecf20Sopenharmony_ci	DEVLINK_TRAP_GROUP_GENERIC(L3_DROPS, 1),
4698c2ecf20Sopenharmony_ci	DEVLINK_TRAP_GROUP_GENERIC(L3_EXCEPTIONS, 1),
4708c2ecf20Sopenharmony_ci	DEVLINK_TRAP_GROUP_GENERIC(BUFFER_DROPS, 2),
4718c2ecf20Sopenharmony_ci	DEVLINK_TRAP_GROUP_GENERIC(ACL_DROPS, 3),
4728c2ecf20Sopenharmony_ci	DEVLINK_TRAP_GROUP_GENERIC(MC_SNOOPING, 3),
4738c2ecf20Sopenharmony_ci};
4748c2ecf20Sopenharmony_ci
4758c2ecf20Sopenharmony_cistatic const struct devlink_trap nsim_traps_arr[] = {
4768c2ecf20Sopenharmony_ci	NSIM_TRAP_DROP(SMAC_MC, L2_DROPS),
4778c2ecf20Sopenharmony_ci	NSIM_TRAP_DROP(VLAN_TAG_MISMATCH, L2_DROPS),
4788c2ecf20Sopenharmony_ci	NSIM_TRAP_DROP(INGRESS_VLAN_FILTER, L2_DROPS),
4798c2ecf20Sopenharmony_ci	NSIM_TRAP_DROP(INGRESS_STP_FILTER, L2_DROPS),
4808c2ecf20Sopenharmony_ci	NSIM_TRAP_DROP(EMPTY_TX_LIST, L2_DROPS),
4818c2ecf20Sopenharmony_ci	NSIM_TRAP_DROP(PORT_LOOPBACK_FILTER, L2_DROPS),
4828c2ecf20Sopenharmony_ci	NSIM_TRAP_DRIVER_EXCEPTION(FID_MISS, L2_DROPS),
4838c2ecf20Sopenharmony_ci	NSIM_TRAP_DROP(BLACKHOLE_ROUTE, L3_DROPS),
4848c2ecf20Sopenharmony_ci	NSIM_TRAP_EXCEPTION(TTL_ERROR, L3_EXCEPTIONS),
4858c2ecf20Sopenharmony_ci	NSIM_TRAP_DROP(TAIL_DROP, BUFFER_DROPS),
4868c2ecf20Sopenharmony_ci	NSIM_TRAP_DROP_EXT(INGRESS_FLOW_ACTION_DROP, ACL_DROPS,
4878c2ecf20Sopenharmony_ci			   DEVLINK_TRAP_METADATA_TYPE_F_FA_COOKIE),
4888c2ecf20Sopenharmony_ci	NSIM_TRAP_DROP_EXT(EGRESS_FLOW_ACTION_DROP, ACL_DROPS,
4898c2ecf20Sopenharmony_ci			   DEVLINK_TRAP_METADATA_TYPE_F_FA_COOKIE),
4908c2ecf20Sopenharmony_ci	NSIM_TRAP_CONTROL(IGMP_QUERY, MC_SNOOPING, MIRROR),
4918c2ecf20Sopenharmony_ci	NSIM_TRAP_CONTROL(IGMP_V1_REPORT, MC_SNOOPING, TRAP),
4928c2ecf20Sopenharmony_ci};
4938c2ecf20Sopenharmony_ci
4948c2ecf20Sopenharmony_ci#define NSIM_TRAP_L4_DATA_LEN 100
4958c2ecf20Sopenharmony_ci
4968c2ecf20Sopenharmony_cistatic struct sk_buff *nsim_dev_trap_skb_build(void)
4978c2ecf20Sopenharmony_ci{
4988c2ecf20Sopenharmony_ci	int tot_len, data_len = NSIM_TRAP_L4_DATA_LEN;
4998c2ecf20Sopenharmony_ci	struct sk_buff *skb;
5008c2ecf20Sopenharmony_ci	struct udphdr *udph;
5018c2ecf20Sopenharmony_ci	struct ethhdr *eth;
5028c2ecf20Sopenharmony_ci	struct iphdr *iph;
5038c2ecf20Sopenharmony_ci
5048c2ecf20Sopenharmony_ci	skb = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC);
5058c2ecf20Sopenharmony_ci	if (!skb)
5068c2ecf20Sopenharmony_ci		return NULL;
5078c2ecf20Sopenharmony_ci	tot_len = sizeof(struct iphdr) + sizeof(struct udphdr) + data_len;
5088c2ecf20Sopenharmony_ci
5098c2ecf20Sopenharmony_ci	skb_reset_mac_header(skb);
5108c2ecf20Sopenharmony_ci	eth = skb_put(skb, sizeof(struct ethhdr));
5118c2ecf20Sopenharmony_ci	eth_random_addr(eth->h_dest);
5128c2ecf20Sopenharmony_ci	eth_random_addr(eth->h_source);
5138c2ecf20Sopenharmony_ci	eth->h_proto = htons(ETH_P_IP);
5148c2ecf20Sopenharmony_ci	skb->protocol = htons(ETH_P_IP);
5158c2ecf20Sopenharmony_ci
5168c2ecf20Sopenharmony_ci	skb_set_network_header(skb, skb->len);
5178c2ecf20Sopenharmony_ci	iph = skb_put(skb, sizeof(struct iphdr));
5188c2ecf20Sopenharmony_ci	iph->protocol = IPPROTO_UDP;
5198c2ecf20Sopenharmony_ci	iph->saddr = in_aton("192.0.2.1");
5208c2ecf20Sopenharmony_ci	iph->daddr = in_aton("198.51.100.1");
5218c2ecf20Sopenharmony_ci	iph->version = 0x4;
5228c2ecf20Sopenharmony_ci	iph->frag_off = 0;
5238c2ecf20Sopenharmony_ci	iph->ihl = 0x5;
5248c2ecf20Sopenharmony_ci	iph->tot_len = htons(tot_len);
5258c2ecf20Sopenharmony_ci	iph->ttl = 100;
5268c2ecf20Sopenharmony_ci	iph->check = 0;
5278c2ecf20Sopenharmony_ci	iph->check = ip_fast_csum((unsigned char *)iph, iph->ihl);
5288c2ecf20Sopenharmony_ci
5298c2ecf20Sopenharmony_ci	skb_set_transport_header(skb, skb->len);
5308c2ecf20Sopenharmony_ci	udph = skb_put_zero(skb, sizeof(struct udphdr) + data_len);
5318c2ecf20Sopenharmony_ci	get_random_bytes(&udph->source, sizeof(u16));
5328c2ecf20Sopenharmony_ci	get_random_bytes(&udph->dest, sizeof(u16));
5338c2ecf20Sopenharmony_ci	udph->len = htons(sizeof(struct udphdr) + data_len);
5348c2ecf20Sopenharmony_ci
5358c2ecf20Sopenharmony_ci	return skb;
5368c2ecf20Sopenharmony_ci}
5378c2ecf20Sopenharmony_ci
5388c2ecf20Sopenharmony_cistatic void nsim_dev_trap_report(struct nsim_dev_port *nsim_dev_port)
5398c2ecf20Sopenharmony_ci{
5408c2ecf20Sopenharmony_ci	struct nsim_dev *nsim_dev = nsim_dev_port->ns->nsim_dev;
5418c2ecf20Sopenharmony_ci	struct devlink *devlink = priv_to_devlink(nsim_dev);
5428c2ecf20Sopenharmony_ci	struct nsim_trap_data *nsim_trap_data;
5438c2ecf20Sopenharmony_ci	int i;
5448c2ecf20Sopenharmony_ci
5458c2ecf20Sopenharmony_ci	nsim_trap_data = nsim_dev->trap_data;
5468c2ecf20Sopenharmony_ci
5478c2ecf20Sopenharmony_ci	spin_lock(&nsim_trap_data->trap_lock);
5488c2ecf20Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(nsim_traps_arr); i++) {
5498c2ecf20Sopenharmony_ci		struct flow_action_cookie *fa_cookie = NULL;
5508c2ecf20Sopenharmony_ci		struct nsim_trap_item *nsim_trap_item;
5518c2ecf20Sopenharmony_ci		struct sk_buff *skb;
5528c2ecf20Sopenharmony_ci		bool has_fa_cookie;
5538c2ecf20Sopenharmony_ci
5548c2ecf20Sopenharmony_ci		has_fa_cookie = nsim_traps_arr[i].metadata_cap &
5558c2ecf20Sopenharmony_ci				DEVLINK_TRAP_METADATA_TYPE_F_FA_COOKIE;
5568c2ecf20Sopenharmony_ci
5578c2ecf20Sopenharmony_ci		nsim_trap_item = &nsim_trap_data->trap_items_arr[i];
5588c2ecf20Sopenharmony_ci		if (nsim_trap_item->action == DEVLINK_TRAP_ACTION_DROP)
5598c2ecf20Sopenharmony_ci			continue;
5608c2ecf20Sopenharmony_ci
5618c2ecf20Sopenharmony_ci		skb = nsim_dev_trap_skb_build();
5628c2ecf20Sopenharmony_ci		if (!skb)
5638c2ecf20Sopenharmony_ci			continue;
5648c2ecf20Sopenharmony_ci		skb->dev = nsim_dev_port->ns->netdev;
5658c2ecf20Sopenharmony_ci
5668c2ecf20Sopenharmony_ci		/* Trapped packets are usually passed to devlink in softIRQ,
5678c2ecf20Sopenharmony_ci		 * but in this case they are generated in a workqueue. Disable
5688c2ecf20Sopenharmony_ci		 * softIRQs to prevent lockdep from complaining about
5698c2ecf20Sopenharmony_ci		 * "incosistent lock state".
5708c2ecf20Sopenharmony_ci		 */
5718c2ecf20Sopenharmony_ci
5728c2ecf20Sopenharmony_ci		spin_lock_bh(&nsim_dev->fa_cookie_lock);
5738c2ecf20Sopenharmony_ci		fa_cookie = has_fa_cookie ? nsim_dev->fa_cookie : NULL;
5748c2ecf20Sopenharmony_ci		devlink_trap_report(devlink, skb, nsim_trap_item->trap_ctx,
5758c2ecf20Sopenharmony_ci				    &nsim_dev_port->devlink_port, fa_cookie);
5768c2ecf20Sopenharmony_ci		spin_unlock_bh(&nsim_dev->fa_cookie_lock);
5778c2ecf20Sopenharmony_ci		consume_skb(skb);
5788c2ecf20Sopenharmony_ci	}
5798c2ecf20Sopenharmony_ci	spin_unlock(&nsim_trap_data->trap_lock);
5808c2ecf20Sopenharmony_ci}
5818c2ecf20Sopenharmony_ci
5828c2ecf20Sopenharmony_ci#define NSIM_TRAP_REPORT_INTERVAL_MS	100
5838c2ecf20Sopenharmony_ci
5848c2ecf20Sopenharmony_cistatic void nsim_dev_trap_report_work(struct work_struct *work)
5858c2ecf20Sopenharmony_ci{
5868c2ecf20Sopenharmony_ci	struct nsim_trap_data *nsim_trap_data;
5878c2ecf20Sopenharmony_ci	struct nsim_dev_port *nsim_dev_port;
5888c2ecf20Sopenharmony_ci	struct nsim_dev *nsim_dev;
5898c2ecf20Sopenharmony_ci
5908c2ecf20Sopenharmony_ci	nsim_trap_data = container_of(work, struct nsim_trap_data,
5918c2ecf20Sopenharmony_ci				      trap_report_dw.work);
5928c2ecf20Sopenharmony_ci	nsim_dev = nsim_trap_data->nsim_dev;
5938c2ecf20Sopenharmony_ci
5948c2ecf20Sopenharmony_ci	/* For each running port and enabled packet trap, generate a UDP
5958c2ecf20Sopenharmony_ci	 * packet with a random 5-tuple and report it.
5968c2ecf20Sopenharmony_ci	 */
5978c2ecf20Sopenharmony_ci	mutex_lock(&nsim_dev->port_list_lock);
5988c2ecf20Sopenharmony_ci	list_for_each_entry(nsim_dev_port, &nsim_dev->port_list, list) {
5998c2ecf20Sopenharmony_ci		if (!netif_running(nsim_dev_port->ns->netdev))
6008c2ecf20Sopenharmony_ci			continue;
6018c2ecf20Sopenharmony_ci
6028c2ecf20Sopenharmony_ci		nsim_dev_trap_report(nsim_dev_port);
6038c2ecf20Sopenharmony_ci	}
6048c2ecf20Sopenharmony_ci	mutex_unlock(&nsim_dev->port_list_lock);
6058c2ecf20Sopenharmony_ci
6068c2ecf20Sopenharmony_ci	schedule_delayed_work(&nsim_dev->trap_data->trap_report_dw,
6078c2ecf20Sopenharmony_ci			      msecs_to_jiffies(NSIM_TRAP_REPORT_INTERVAL_MS));
6088c2ecf20Sopenharmony_ci}
6098c2ecf20Sopenharmony_ci
6108c2ecf20Sopenharmony_cistatic int nsim_dev_traps_init(struct devlink *devlink)
6118c2ecf20Sopenharmony_ci{
6128c2ecf20Sopenharmony_ci	size_t policers_count = ARRAY_SIZE(nsim_trap_policers_arr);
6138c2ecf20Sopenharmony_ci	struct nsim_dev *nsim_dev = devlink_priv(devlink);
6148c2ecf20Sopenharmony_ci	struct nsim_trap_data *nsim_trap_data;
6158c2ecf20Sopenharmony_ci	int err;
6168c2ecf20Sopenharmony_ci
6178c2ecf20Sopenharmony_ci	nsim_trap_data = kzalloc(sizeof(*nsim_trap_data), GFP_KERNEL);
6188c2ecf20Sopenharmony_ci	if (!nsim_trap_data)
6198c2ecf20Sopenharmony_ci		return -ENOMEM;
6208c2ecf20Sopenharmony_ci
6218c2ecf20Sopenharmony_ci	nsim_trap_data->trap_items_arr = kcalloc(ARRAY_SIZE(nsim_traps_arr),
6228c2ecf20Sopenharmony_ci						 sizeof(struct nsim_trap_item),
6238c2ecf20Sopenharmony_ci						 GFP_KERNEL);
6248c2ecf20Sopenharmony_ci	if (!nsim_trap_data->trap_items_arr) {
6258c2ecf20Sopenharmony_ci		err = -ENOMEM;
6268c2ecf20Sopenharmony_ci		goto err_trap_data_free;
6278c2ecf20Sopenharmony_ci	}
6288c2ecf20Sopenharmony_ci
6298c2ecf20Sopenharmony_ci	nsim_trap_data->trap_policers_cnt_arr = kcalloc(policers_count,
6308c2ecf20Sopenharmony_ci							sizeof(u64),
6318c2ecf20Sopenharmony_ci							GFP_KERNEL);
6328c2ecf20Sopenharmony_ci	if (!nsim_trap_data->trap_policers_cnt_arr) {
6338c2ecf20Sopenharmony_ci		err = -ENOMEM;
6348c2ecf20Sopenharmony_ci		goto err_trap_items_free;
6358c2ecf20Sopenharmony_ci	}
6368c2ecf20Sopenharmony_ci
6378c2ecf20Sopenharmony_ci	/* The lock is used to protect the action state of the registered
6388c2ecf20Sopenharmony_ci	 * traps. The value is written by user and read in delayed work when
6398c2ecf20Sopenharmony_ci	 * iterating over all the traps.
6408c2ecf20Sopenharmony_ci	 */
6418c2ecf20Sopenharmony_ci	spin_lock_init(&nsim_trap_data->trap_lock);
6428c2ecf20Sopenharmony_ci	nsim_trap_data->nsim_dev = nsim_dev;
6438c2ecf20Sopenharmony_ci	nsim_dev->trap_data = nsim_trap_data;
6448c2ecf20Sopenharmony_ci
6458c2ecf20Sopenharmony_ci	err = devlink_trap_policers_register(devlink, nsim_trap_policers_arr,
6468c2ecf20Sopenharmony_ci					     policers_count);
6478c2ecf20Sopenharmony_ci	if (err)
6488c2ecf20Sopenharmony_ci		goto err_trap_policers_cnt_free;
6498c2ecf20Sopenharmony_ci
6508c2ecf20Sopenharmony_ci	err = devlink_trap_groups_register(devlink, nsim_trap_groups_arr,
6518c2ecf20Sopenharmony_ci					   ARRAY_SIZE(nsim_trap_groups_arr));
6528c2ecf20Sopenharmony_ci	if (err)
6538c2ecf20Sopenharmony_ci		goto err_trap_policers_unregister;
6548c2ecf20Sopenharmony_ci
6558c2ecf20Sopenharmony_ci	err = devlink_traps_register(devlink, nsim_traps_arr,
6568c2ecf20Sopenharmony_ci				     ARRAY_SIZE(nsim_traps_arr), NULL);
6578c2ecf20Sopenharmony_ci	if (err)
6588c2ecf20Sopenharmony_ci		goto err_trap_groups_unregister;
6598c2ecf20Sopenharmony_ci
6608c2ecf20Sopenharmony_ci	INIT_DELAYED_WORK(&nsim_dev->trap_data->trap_report_dw,
6618c2ecf20Sopenharmony_ci			  nsim_dev_trap_report_work);
6628c2ecf20Sopenharmony_ci	schedule_delayed_work(&nsim_dev->trap_data->trap_report_dw,
6638c2ecf20Sopenharmony_ci			      msecs_to_jiffies(NSIM_TRAP_REPORT_INTERVAL_MS));
6648c2ecf20Sopenharmony_ci
6658c2ecf20Sopenharmony_ci	return 0;
6668c2ecf20Sopenharmony_ci
6678c2ecf20Sopenharmony_cierr_trap_groups_unregister:
6688c2ecf20Sopenharmony_ci	devlink_trap_groups_unregister(devlink, nsim_trap_groups_arr,
6698c2ecf20Sopenharmony_ci				       ARRAY_SIZE(nsim_trap_groups_arr));
6708c2ecf20Sopenharmony_cierr_trap_policers_unregister:
6718c2ecf20Sopenharmony_ci	devlink_trap_policers_unregister(devlink, nsim_trap_policers_arr,
6728c2ecf20Sopenharmony_ci					 ARRAY_SIZE(nsim_trap_policers_arr));
6738c2ecf20Sopenharmony_cierr_trap_policers_cnt_free:
6748c2ecf20Sopenharmony_ci	kfree(nsim_trap_data->trap_policers_cnt_arr);
6758c2ecf20Sopenharmony_cierr_trap_items_free:
6768c2ecf20Sopenharmony_ci	kfree(nsim_trap_data->trap_items_arr);
6778c2ecf20Sopenharmony_cierr_trap_data_free:
6788c2ecf20Sopenharmony_ci	kfree(nsim_trap_data);
6798c2ecf20Sopenharmony_ci	return err;
6808c2ecf20Sopenharmony_ci}
6818c2ecf20Sopenharmony_ci
6828c2ecf20Sopenharmony_cistatic void nsim_dev_traps_exit(struct devlink *devlink)
6838c2ecf20Sopenharmony_ci{
6848c2ecf20Sopenharmony_ci	struct nsim_dev *nsim_dev = devlink_priv(devlink);
6858c2ecf20Sopenharmony_ci
6868c2ecf20Sopenharmony_ci	cancel_delayed_work_sync(&nsim_dev->trap_data->trap_report_dw);
6878c2ecf20Sopenharmony_ci	devlink_traps_unregister(devlink, nsim_traps_arr,
6888c2ecf20Sopenharmony_ci				 ARRAY_SIZE(nsim_traps_arr));
6898c2ecf20Sopenharmony_ci	devlink_trap_groups_unregister(devlink, nsim_trap_groups_arr,
6908c2ecf20Sopenharmony_ci				       ARRAY_SIZE(nsim_trap_groups_arr));
6918c2ecf20Sopenharmony_ci	devlink_trap_policers_unregister(devlink, nsim_trap_policers_arr,
6928c2ecf20Sopenharmony_ci					 ARRAY_SIZE(nsim_trap_policers_arr));
6938c2ecf20Sopenharmony_ci	kfree(nsim_dev->trap_data->trap_policers_cnt_arr);
6948c2ecf20Sopenharmony_ci	kfree(nsim_dev->trap_data->trap_items_arr);
6958c2ecf20Sopenharmony_ci	kfree(nsim_dev->trap_data);
6968c2ecf20Sopenharmony_ci}
6978c2ecf20Sopenharmony_ci
6988c2ecf20Sopenharmony_cistatic int nsim_dev_reload_create(struct nsim_dev *nsim_dev,
6998c2ecf20Sopenharmony_ci				  struct netlink_ext_ack *extack);
7008c2ecf20Sopenharmony_cistatic void nsim_dev_reload_destroy(struct nsim_dev *nsim_dev);
7018c2ecf20Sopenharmony_ci
7028c2ecf20Sopenharmony_cistatic int nsim_dev_reload_down(struct devlink *devlink, bool netns_change,
7038c2ecf20Sopenharmony_ci				enum devlink_reload_action action, enum devlink_reload_limit limit,
7048c2ecf20Sopenharmony_ci				struct netlink_ext_ack *extack)
7058c2ecf20Sopenharmony_ci{
7068c2ecf20Sopenharmony_ci	struct nsim_dev *nsim_dev = devlink_priv(devlink);
7078c2ecf20Sopenharmony_ci
7088c2ecf20Sopenharmony_ci	if (nsim_dev->dont_allow_reload) {
7098c2ecf20Sopenharmony_ci		/* For testing purposes, user set debugfs dont_allow_reload
7108c2ecf20Sopenharmony_ci		 * value to true. So forbid it.
7118c2ecf20Sopenharmony_ci		 */
7128c2ecf20Sopenharmony_ci		NL_SET_ERR_MSG_MOD(extack, "User forbid the reload for testing purposes");
7138c2ecf20Sopenharmony_ci		return -EOPNOTSUPP;
7148c2ecf20Sopenharmony_ci	}
7158c2ecf20Sopenharmony_ci
7168c2ecf20Sopenharmony_ci	nsim_dev_reload_destroy(nsim_dev);
7178c2ecf20Sopenharmony_ci	return 0;
7188c2ecf20Sopenharmony_ci}
7198c2ecf20Sopenharmony_ci
7208c2ecf20Sopenharmony_cistatic int nsim_dev_reload_up(struct devlink *devlink, enum devlink_reload_action action,
7218c2ecf20Sopenharmony_ci			      enum devlink_reload_limit limit, u32 *actions_performed,
7228c2ecf20Sopenharmony_ci			      struct netlink_ext_ack *extack)
7238c2ecf20Sopenharmony_ci{
7248c2ecf20Sopenharmony_ci	struct nsim_dev *nsim_dev = devlink_priv(devlink);
7258c2ecf20Sopenharmony_ci
7268c2ecf20Sopenharmony_ci	if (nsim_dev->fail_reload) {
7278c2ecf20Sopenharmony_ci		/* For testing purposes, user set debugfs fail_reload
7288c2ecf20Sopenharmony_ci		 * value to true. Fail right away.
7298c2ecf20Sopenharmony_ci		 */
7308c2ecf20Sopenharmony_ci		NL_SET_ERR_MSG_MOD(extack, "User setup the reload to fail for testing purposes");
7318c2ecf20Sopenharmony_ci		return -EINVAL;
7328c2ecf20Sopenharmony_ci	}
7338c2ecf20Sopenharmony_ci
7348c2ecf20Sopenharmony_ci	*actions_performed = BIT(DEVLINK_RELOAD_ACTION_DRIVER_REINIT);
7358c2ecf20Sopenharmony_ci	return nsim_dev_reload_create(nsim_dev, extack);
7368c2ecf20Sopenharmony_ci}
7378c2ecf20Sopenharmony_ci
7388c2ecf20Sopenharmony_cistatic int nsim_dev_info_get(struct devlink *devlink,
7398c2ecf20Sopenharmony_ci			     struct devlink_info_req *req,
7408c2ecf20Sopenharmony_ci			     struct netlink_ext_ack *extack)
7418c2ecf20Sopenharmony_ci{
7428c2ecf20Sopenharmony_ci	return devlink_info_driver_name_put(req, DRV_NAME);
7438c2ecf20Sopenharmony_ci}
7448c2ecf20Sopenharmony_ci
7458c2ecf20Sopenharmony_ci#define NSIM_DEV_FLASH_SIZE 500000
7468c2ecf20Sopenharmony_ci#define NSIM_DEV_FLASH_CHUNK_SIZE 1000
7478c2ecf20Sopenharmony_ci#define NSIM_DEV_FLASH_CHUNK_TIME_MS 10
7488c2ecf20Sopenharmony_ci
7498c2ecf20Sopenharmony_cistatic int nsim_dev_flash_update(struct devlink *devlink,
7508c2ecf20Sopenharmony_ci				 struct devlink_flash_update_params *params,
7518c2ecf20Sopenharmony_ci				 struct netlink_ext_ack *extack)
7528c2ecf20Sopenharmony_ci{
7538c2ecf20Sopenharmony_ci	struct nsim_dev *nsim_dev = devlink_priv(devlink);
7548c2ecf20Sopenharmony_ci	int i;
7558c2ecf20Sopenharmony_ci
7568c2ecf20Sopenharmony_ci	if ((params->overwrite_mask & ~nsim_dev->fw_update_overwrite_mask) != 0)
7578c2ecf20Sopenharmony_ci		return -EOPNOTSUPP;
7588c2ecf20Sopenharmony_ci
7598c2ecf20Sopenharmony_ci	if (nsim_dev->fw_update_status) {
7608c2ecf20Sopenharmony_ci		devlink_flash_update_begin_notify(devlink);
7618c2ecf20Sopenharmony_ci		devlink_flash_update_status_notify(devlink,
7628c2ecf20Sopenharmony_ci						   "Preparing to flash",
7638c2ecf20Sopenharmony_ci						   params->component, 0, 0);
7648c2ecf20Sopenharmony_ci	}
7658c2ecf20Sopenharmony_ci
7668c2ecf20Sopenharmony_ci	for (i = 0; i < NSIM_DEV_FLASH_SIZE / NSIM_DEV_FLASH_CHUNK_SIZE; i++) {
7678c2ecf20Sopenharmony_ci		if (nsim_dev->fw_update_status)
7688c2ecf20Sopenharmony_ci			devlink_flash_update_status_notify(devlink, "Flashing",
7698c2ecf20Sopenharmony_ci							   params->component,
7708c2ecf20Sopenharmony_ci							   i * NSIM_DEV_FLASH_CHUNK_SIZE,
7718c2ecf20Sopenharmony_ci							   NSIM_DEV_FLASH_SIZE);
7728c2ecf20Sopenharmony_ci		msleep(NSIM_DEV_FLASH_CHUNK_TIME_MS);
7738c2ecf20Sopenharmony_ci	}
7748c2ecf20Sopenharmony_ci
7758c2ecf20Sopenharmony_ci	if (nsim_dev->fw_update_status) {
7768c2ecf20Sopenharmony_ci		devlink_flash_update_status_notify(devlink, "Flashing",
7778c2ecf20Sopenharmony_ci						   params->component,
7788c2ecf20Sopenharmony_ci						   NSIM_DEV_FLASH_SIZE,
7798c2ecf20Sopenharmony_ci						   NSIM_DEV_FLASH_SIZE);
7808c2ecf20Sopenharmony_ci		devlink_flash_update_timeout_notify(devlink, "Flash select",
7818c2ecf20Sopenharmony_ci						    params->component, 81);
7828c2ecf20Sopenharmony_ci		devlink_flash_update_status_notify(devlink, "Flashing done",
7838c2ecf20Sopenharmony_ci						   params->component, 0, 0);
7848c2ecf20Sopenharmony_ci		devlink_flash_update_end_notify(devlink);
7858c2ecf20Sopenharmony_ci	}
7868c2ecf20Sopenharmony_ci
7878c2ecf20Sopenharmony_ci	return 0;
7888c2ecf20Sopenharmony_ci}
7898c2ecf20Sopenharmony_ci
7908c2ecf20Sopenharmony_cistatic struct nsim_trap_item *
7918c2ecf20Sopenharmony_cinsim_dev_trap_item_lookup(struct nsim_dev *nsim_dev, u16 trap_id)
7928c2ecf20Sopenharmony_ci{
7938c2ecf20Sopenharmony_ci	struct nsim_trap_data *nsim_trap_data = nsim_dev->trap_data;
7948c2ecf20Sopenharmony_ci	int i;
7958c2ecf20Sopenharmony_ci
7968c2ecf20Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(nsim_traps_arr); i++) {
7978c2ecf20Sopenharmony_ci		if (nsim_traps_arr[i].id == trap_id)
7988c2ecf20Sopenharmony_ci			return &nsim_trap_data->trap_items_arr[i];
7998c2ecf20Sopenharmony_ci	}
8008c2ecf20Sopenharmony_ci
8018c2ecf20Sopenharmony_ci	return NULL;
8028c2ecf20Sopenharmony_ci}
8038c2ecf20Sopenharmony_ci
8048c2ecf20Sopenharmony_cistatic int nsim_dev_devlink_trap_init(struct devlink *devlink,
8058c2ecf20Sopenharmony_ci				      const struct devlink_trap *trap,
8068c2ecf20Sopenharmony_ci				      void *trap_ctx)
8078c2ecf20Sopenharmony_ci{
8088c2ecf20Sopenharmony_ci	struct nsim_dev *nsim_dev = devlink_priv(devlink);
8098c2ecf20Sopenharmony_ci	struct nsim_trap_item *nsim_trap_item;
8108c2ecf20Sopenharmony_ci
8118c2ecf20Sopenharmony_ci	nsim_trap_item = nsim_dev_trap_item_lookup(nsim_dev, trap->id);
8128c2ecf20Sopenharmony_ci	if (WARN_ON(!nsim_trap_item))
8138c2ecf20Sopenharmony_ci		return -ENOENT;
8148c2ecf20Sopenharmony_ci
8158c2ecf20Sopenharmony_ci	nsim_trap_item->trap_ctx = trap_ctx;
8168c2ecf20Sopenharmony_ci	nsim_trap_item->action = trap->init_action;
8178c2ecf20Sopenharmony_ci
8188c2ecf20Sopenharmony_ci	return 0;
8198c2ecf20Sopenharmony_ci}
8208c2ecf20Sopenharmony_ci
8218c2ecf20Sopenharmony_cistatic int
8228c2ecf20Sopenharmony_cinsim_dev_devlink_trap_action_set(struct devlink *devlink,
8238c2ecf20Sopenharmony_ci				 const struct devlink_trap *trap,
8248c2ecf20Sopenharmony_ci				 enum devlink_trap_action action,
8258c2ecf20Sopenharmony_ci				 struct netlink_ext_ack *extack)
8268c2ecf20Sopenharmony_ci{
8278c2ecf20Sopenharmony_ci	struct nsim_dev *nsim_dev = devlink_priv(devlink);
8288c2ecf20Sopenharmony_ci	struct nsim_trap_item *nsim_trap_item;
8298c2ecf20Sopenharmony_ci
8308c2ecf20Sopenharmony_ci	nsim_trap_item = nsim_dev_trap_item_lookup(nsim_dev, trap->id);
8318c2ecf20Sopenharmony_ci	if (WARN_ON(!nsim_trap_item))
8328c2ecf20Sopenharmony_ci		return -ENOENT;
8338c2ecf20Sopenharmony_ci
8348c2ecf20Sopenharmony_ci	spin_lock(&nsim_dev->trap_data->trap_lock);
8358c2ecf20Sopenharmony_ci	nsim_trap_item->action = action;
8368c2ecf20Sopenharmony_ci	spin_unlock(&nsim_dev->trap_data->trap_lock);
8378c2ecf20Sopenharmony_ci
8388c2ecf20Sopenharmony_ci	return 0;
8398c2ecf20Sopenharmony_ci}
8408c2ecf20Sopenharmony_ci
8418c2ecf20Sopenharmony_cistatic int
8428c2ecf20Sopenharmony_cinsim_dev_devlink_trap_group_set(struct devlink *devlink,
8438c2ecf20Sopenharmony_ci				const struct devlink_trap_group *group,
8448c2ecf20Sopenharmony_ci				const struct devlink_trap_policer *policer,
8458c2ecf20Sopenharmony_ci				struct netlink_ext_ack *extack)
8468c2ecf20Sopenharmony_ci{
8478c2ecf20Sopenharmony_ci	struct nsim_dev *nsim_dev = devlink_priv(devlink);
8488c2ecf20Sopenharmony_ci
8498c2ecf20Sopenharmony_ci	if (nsim_dev->fail_trap_group_set)
8508c2ecf20Sopenharmony_ci		return -EINVAL;
8518c2ecf20Sopenharmony_ci
8528c2ecf20Sopenharmony_ci	return 0;
8538c2ecf20Sopenharmony_ci}
8548c2ecf20Sopenharmony_ci
8558c2ecf20Sopenharmony_cistatic int
8568c2ecf20Sopenharmony_cinsim_dev_devlink_trap_policer_set(struct devlink *devlink,
8578c2ecf20Sopenharmony_ci				  const struct devlink_trap_policer *policer,
8588c2ecf20Sopenharmony_ci				  u64 rate, u64 burst,
8598c2ecf20Sopenharmony_ci				  struct netlink_ext_ack *extack)
8608c2ecf20Sopenharmony_ci{
8618c2ecf20Sopenharmony_ci	struct nsim_dev *nsim_dev = devlink_priv(devlink);
8628c2ecf20Sopenharmony_ci
8638c2ecf20Sopenharmony_ci	if (nsim_dev->fail_trap_policer_set) {
8648c2ecf20Sopenharmony_ci		NL_SET_ERR_MSG_MOD(extack, "User setup the operation to fail for testing purposes");
8658c2ecf20Sopenharmony_ci		return -EINVAL;
8668c2ecf20Sopenharmony_ci	}
8678c2ecf20Sopenharmony_ci
8688c2ecf20Sopenharmony_ci	return 0;
8698c2ecf20Sopenharmony_ci}
8708c2ecf20Sopenharmony_ci
8718c2ecf20Sopenharmony_cistatic int
8728c2ecf20Sopenharmony_cinsim_dev_devlink_trap_policer_counter_get(struct devlink *devlink,
8738c2ecf20Sopenharmony_ci					  const struct devlink_trap_policer *policer,
8748c2ecf20Sopenharmony_ci					  u64 *p_drops)
8758c2ecf20Sopenharmony_ci{
8768c2ecf20Sopenharmony_ci	struct nsim_dev *nsim_dev = devlink_priv(devlink);
8778c2ecf20Sopenharmony_ci	u64 *cnt;
8788c2ecf20Sopenharmony_ci
8798c2ecf20Sopenharmony_ci	if (nsim_dev->fail_trap_policer_counter_get)
8808c2ecf20Sopenharmony_ci		return -EINVAL;
8818c2ecf20Sopenharmony_ci
8828c2ecf20Sopenharmony_ci	cnt = &nsim_dev->trap_data->trap_policers_cnt_arr[policer->id - 1];
8838c2ecf20Sopenharmony_ci	*p_drops = (*cnt)++;
8848c2ecf20Sopenharmony_ci
8858c2ecf20Sopenharmony_ci	return 0;
8868c2ecf20Sopenharmony_ci}
8878c2ecf20Sopenharmony_ci
8888c2ecf20Sopenharmony_cistatic const struct devlink_ops nsim_dev_devlink_ops = {
8898c2ecf20Sopenharmony_ci	.supported_flash_update_params = DEVLINK_SUPPORT_FLASH_UPDATE_COMPONENT |
8908c2ecf20Sopenharmony_ci					 DEVLINK_SUPPORT_FLASH_UPDATE_OVERWRITE_MASK,
8918c2ecf20Sopenharmony_ci	.reload_actions = BIT(DEVLINK_RELOAD_ACTION_DRIVER_REINIT),
8928c2ecf20Sopenharmony_ci	.reload_down = nsim_dev_reload_down,
8938c2ecf20Sopenharmony_ci	.reload_up = nsim_dev_reload_up,
8948c2ecf20Sopenharmony_ci	.info_get = nsim_dev_info_get,
8958c2ecf20Sopenharmony_ci	.flash_update = nsim_dev_flash_update,
8968c2ecf20Sopenharmony_ci	.trap_init = nsim_dev_devlink_trap_init,
8978c2ecf20Sopenharmony_ci	.trap_action_set = nsim_dev_devlink_trap_action_set,
8988c2ecf20Sopenharmony_ci	.trap_group_set = nsim_dev_devlink_trap_group_set,
8998c2ecf20Sopenharmony_ci	.trap_policer_set = nsim_dev_devlink_trap_policer_set,
9008c2ecf20Sopenharmony_ci	.trap_policer_counter_get = nsim_dev_devlink_trap_policer_counter_get,
9018c2ecf20Sopenharmony_ci};
9028c2ecf20Sopenharmony_ci
9038c2ecf20Sopenharmony_ci#define NSIM_DEV_MAX_MACS_DEFAULT 32
9048c2ecf20Sopenharmony_ci#define NSIM_DEV_TEST1_DEFAULT true
9058c2ecf20Sopenharmony_ci
9068c2ecf20Sopenharmony_cistatic int __nsim_dev_port_add(struct nsim_dev *nsim_dev,
9078c2ecf20Sopenharmony_ci			       unsigned int port_index)
9088c2ecf20Sopenharmony_ci{
9098c2ecf20Sopenharmony_ci	struct devlink_port_attrs attrs = {};
9108c2ecf20Sopenharmony_ci	struct nsim_dev_port *nsim_dev_port;
9118c2ecf20Sopenharmony_ci	struct devlink_port *devlink_port;
9128c2ecf20Sopenharmony_ci	int err;
9138c2ecf20Sopenharmony_ci
9148c2ecf20Sopenharmony_ci	nsim_dev_port = kzalloc(sizeof(*nsim_dev_port), GFP_KERNEL);
9158c2ecf20Sopenharmony_ci	if (!nsim_dev_port)
9168c2ecf20Sopenharmony_ci		return -ENOMEM;
9178c2ecf20Sopenharmony_ci	nsim_dev_port->port_index = port_index;
9188c2ecf20Sopenharmony_ci
9198c2ecf20Sopenharmony_ci	devlink_port = &nsim_dev_port->devlink_port;
9208c2ecf20Sopenharmony_ci	attrs.flavour = DEVLINK_PORT_FLAVOUR_PHYSICAL;
9218c2ecf20Sopenharmony_ci	attrs.phys.port_number = port_index + 1;
9228c2ecf20Sopenharmony_ci	memcpy(attrs.switch_id.id, nsim_dev->switch_id.id, nsim_dev->switch_id.id_len);
9238c2ecf20Sopenharmony_ci	attrs.switch_id.id_len = nsim_dev->switch_id.id_len;
9248c2ecf20Sopenharmony_ci	devlink_port_attrs_set(devlink_port, &attrs);
9258c2ecf20Sopenharmony_ci	err = devlink_port_register(priv_to_devlink(nsim_dev), devlink_port,
9268c2ecf20Sopenharmony_ci				    port_index);
9278c2ecf20Sopenharmony_ci	if (err)
9288c2ecf20Sopenharmony_ci		goto err_port_free;
9298c2ecf20Sopenharmony_ci
9308c2ecf20Sopenharmony_ci	err = nsim_dev_port_debugfs_init(nsim_dev, nsim_dev_port);
9318c2ecf20Sopenharmony_ci	if (err)
9328c2ecf20Sopenharmony_ci		goto err_dl_port_unregister;
9338c2ecf20Sopenharmony_ci
9348c2ecf20Sopenharmony_ci	nsim_dev_port->ns = nsim_create(nsim_dev, nsim_dev_port);
9358c2ecf20Sopenharmony_ci	if (IS_ERR(nsim_dev_port->ns)) {
9368c2ecf20Sopenharmony_ci		err = PTR_ERR(nsim_dev_port->ns);
9378c2ecf20Sopenharmony_ci		goto err_port_debugfs_exit;
9388c2ecf20Sopenharmony_ci	}
9398c2ecf20Sopenharmony_ci
9408c2ecf20Sopenharmony_ci	devlink_port_type_eth_set(devlink_port, nsim_dev_port->ns->netdev);
9418c2ecf20Sopenharmony_ci	list_add(&nsim_dev_port->list, &nsim_dev->port_list);
9428c2ecf20Sopenharmony_ci
9438c2ecf20Sopenharmony_ci	return 0;
9448c2ecf20Sopenharmony_ci
9458c2ecf20Sopenharmony_cierr_port_debugfs_exit:
9468c2ecf20Sopenharmony_ci	nsim_dev_port_debugfs_exit(nsim_dev_port);
9478c2ecf20Sopenharmony_cierr_dl_port_unregister:
9488c2ecf20Sopenharmony_ci	devlink_port_unregister(devlink_port);
9498c2ecf20Sopenharmony_cierr_port_free:
9508c2ecf20Sopenharmony_ci	kfree(nsim_dev_port);
9518c2ecf20Sopenharmony_ci	return err;
9528c2ecf20Sopenharmony_ci}
9538c2ecf20Sopenharmony_ci
9548c2ecf20Sopenharmony_cistatic void __nsim_dev_port_del(struct nsim_dev_port *nsim_dev_port)
9558c2ecf20Sopenharmony_ci{
9568c2ecf20Sopenharmony_ci	struct devlink_port *devlink_port = &nsim_dev_port->devlink_port;
9578c2ecf20Sopenharmony_ci
9588c2ecf20Sopenharmony_ci	list_del(&nsim_dev_port->list);
9598c2ecf20Sopenharmony_ci	devlink_port_type_clear(devlink_port);
9608c2ecf20Sopenharmony_ci	nsim_destroy(nsim_dev_port->ns);
9618c2ecf20Sopenharmony_ci	nsim_dev_port_debugfs_exit(nsim_dev_port);
9628c2ecf20Sopenharmony_ci	devlink_port_unregister(devlink_port);
9638c2ecf20Sopenharmony_ci	kfree(nsim_dev_port);
9648c2ecf20Sopenharmony_ci}
9658c2ecf20Sopenharmony_ci
9668c2ecf20Sopenharmony_cistatic void nsim_dev_port_del_all(struct nsim_dev *nsim_dev)
9678c2ecf20Sopenharmony_ci{
9688c2ecf20Sopenharmony_ci	struct nsim_dev_port *nsim_dev_port, *tmp;
9698c2ecf20Sopenharmony_ci
9708c2ecf20Sopenharmony_ci	mutex_lock(&nsim_dev->port_list_lock);
9718c2ecf20Sopenharmony_ci	list_for_each_entry_safe(nsim_dev_port, tmp,
9728c2ecf20Sopenharmony_ci				 &nsim_dev->port_list, list)
9738c2ecf20Sopenharmony_ci		__nsim_dev_port_del(nsim_dev_port);
9748c2ecf20Sopenharmony_ci	mutex_unlock(&nsim_dev->port_list_lock);
9758c2ecf20Sopenharmony_ci}
9768c2ecf20Sopenharmony_ci
9778c2ecf20Sopenharmony_cistatic int nsim_dev_port_add_all(struct nsim_dev *nsim_dev,
9788c2ecf20Sopenharmony_ci				 unsigned int port_count)
9798c2ecf20Sopenharmony_ci{
9808c2ecf20Sopenharmony_ci	int i, err;
9818c2ecf20Sopenharmony_ci
9828c2ecf20Sopenharmony_ci	for (i = 0; i < port_count; i++) {
9838c2ecf20Sopenharmony_ci		err = __nsim_dev_port_add(nsim_dev, i);
9848c2ecf20Sopenharmony_ci		if (err)
9858c2ecf20Sopenharmony_ci			goto err_port_del_all;
9868c2ecf20Sopenharmony_ci	}
9878c2ecf20Sopenharmony_ci	return 0;
9888c2ecf20Sopenharmony_ci
9898c2ecf20Sopenharmony_cierr_port_del_all:
9908c2ecf20Sopenharmony_ci	nsim_dev_port_del_all(nsim_dev);
9918c2ecf20Sopenharmony_ci	return err;
9928c2ecf20Sopenharmony_ci}
9938c2ecf20Sopenharmony_ci
9948c2ecf20Sopenharmony_cistatic int nsim_dev_reload_create(struct nsim_dev *nsim_dev,
9958c2ecf20Sopenharmony_ci				  struct netlink_ext_ack *extack)
9968c2ecf20Sopenharmony_ci{
9978c2ecf20Sopenharmony_ci	struct nsim_bus_dev *nsim_bus_dev = nsim_dev->nsim_bus_dev;
9988c2ecf20Sopenharmony_ci	struct devlink *devlink;
9998c2ecf20Sopenharmony_ci	int err;
10008c2ecf20Sopenharmony_ci
10018c2ecf20Sopenharmony_ci	devlink = priv_to_devlink(nsim_dev);
10028c2ecf20Sopenharmony_ci	nsim_dev = devlink_priv(devlink);
10038c2ecf20Sopenharmony_ci	INIT_LIST_HEAD(&nsim_dev->port_list);
10048c2ecf20Sopenharmony_ci	mutex_init(&nsim_dev->port_list_lock);
10058c2ecf20Sopenharmony_ci	nsim_dev->fw_update_status = true;
10068c2ecf20Sopenharmony_ci	nsim_dev->fw_update_overwrite_mask = 0;
10078c2ecf20Sopenharmony_ci
10088c2ecf20Sopenharmony_ci	nsim_devlink_param_load_driverinit_values(devlink);
10098c2ecf20Sopenharmony_ci
10108c2ecf20Sopenharmony_ci	err = nsim_dev_dummy_region_init(nsim_dev, devlink);
10118c2ecf20Sopenharmony_ci	if (err)
10128c2ecf20Sopenharmony_ci		return err;
10138c2ecf20Sopenharmony_ci
10148c2ecf20Sopenharmony_ci	err = nsim_dev_traps_init(devlink);
10158c2ecf20Sopenharmony_ci	if (err)
10168c2ecf20Sopenharmony_ci		goto err_dummy_region_exit;
10178c2ecf20Sopenharmony_ci
10188c2ecf20Sopenharmony_ci	nsim_dev->fib_data = nsim_fib_create(devlink, extack);
10198c2ecf20Sopenharmony_ci	if (IS_ERR(nsim_dev->fib_data)) {
10208c2ecf20Sopenharmony_ci		err = PTR_ERR(nsim_dev->fib_data);
10218c2ecf20Sopenharmony_ci		goto err_traps_exit;
10228c2ecf20Sopenharmony_ci	}
10238c2ecf20Sopenharmony_ci
10248c2ecf20Sopenharmony_ci	err = nsim_dev_health_init(nsim_dev, devlink);
10258c2ecf20Sopenharmony_ci	if (err)
10268c2ecf20Sopenharmony_ci		goto err_fib_destroy;
10278c2ecf20Sopenharmony_ci
10288c2ecf20Sopenharmony_ci	err = nsim_dev_port_add_all(nsim_dev, nsim_bus_dev->port_count);
10298c2ecf20Sopenharmony_ci	if (err)
10308c2ecf20Sopenharmony_ci		goto err_health_exit;
10318c2ecf20Sopenharmony_ci
10328c2ecf20Sopenharmony_ci	nsim_dev->take_snapshot = debugfs_create_file("take_snapshot",
10338c2ecf20Sopenharmony_ci						      0200,
10348c2ecf20Sopenharmony_ci						      nsim_dev->ddir,
10358c2ecf20Sopenharmony_ci						      nsim_dev,
10368c2ecf20Sopenharmony_ci						&nsim_dev_take_snapshot_fops);
10378c2ecf20Sopenharmony_ci	return 0;
10388c2ecf20Sopenharmony_ci
10398c2ecf20Sopenharmony_cierr_health_exit:
10408c2ecf20Sopenharmony_ci	nsim_dev_health_exit(nsim_dev);
10418c2ecf20Sopenharmony_cierr_fib_destroy:
10428c2ecf20Sopenharmony_ci	nsim_fib_destroy(devlink, nsim_dev->fib_data);
10438c2ecf20Sopenharmony_cierr_traps_exit:
10448c2ecf20Sopenharmony_ci	nsim_dev_traps_exit(devlink);
10458c2ecf20Sopenharmony_cierr_dummy_region_exit:
10468c2ecf20Sopenharmony_ci	nsim_dev_dummy_region_exit(nsim_dev);
10478c2ecf20Sopenharmony_ci	return err;
10488c2ecf20Sopenharmony_ci}
10498c2ecf20Sopenharmony_ci
10508c2ecf20Sopenharmony_ciint nsim_dev_probe(struct nsim_bus_dev *nsim_bus_dev)
10518c2ecf20Sopenharmony_ci{
10528c2ecf20Sopenharmony_ci	struct nsim_dev *nsim_dev;
10538c2ecf20Sopenharmony_ci	struct devlink *devlink;
10548c2ecf20Sopenharmony_ci	int err;
10558c2ecf20Sopenharmony_ci
10568c2ecf20Sopenharmony_ci	devlink = devlink_alloc(&nsim_dev_devlink_ops, sizeof(*nsim_dev));
10578c2ecf20Sopenharmony_ci	if (!devlink)
10588c2ecf20Sopenharmony_ci		return -ENOMEM;
10598c2ecf20Sopenharmony_ci	devlink_net_set(devlink, nsim_bus_dev->initial_net);
10608c2ecf20Sopenharmony_ci	nsim_dev = devlink_priv(devlink);
10618c2ecf20Sopenharmony_ci	nsim_dev->nsim_bus_dev = nsim_bus_dev;
10628c2ecf20Sopenharmony_ci	nsim_dev->switch_id.id_len = sizeof(nsim_dev->switch_id.id);
10638c2ecf20Sopenharmony_ci	get_random_bytes(nsim_dev->switch_id.id, nsim_dev->switch_id.id_len);
10648c2ecf20Sopenharmony_ci	INIT_LIST_HEAD(&nsim_dev->port_list);
10658c2ecf20Sopenharmony_ci	mutex_init(&nsim_dev->port_list_lock);
10668c2ecf20Sopenharmony_ci	nsim_dev->fw_update_status = true;
10678c2ecf20Sopenharmony_ci	nsim_dev->fw_update_overwrite_mask = 0;
10688c2ecf20Sopenharmony_ci	nsim_dev->max_macs = NSIM_DEV_MAX_MACS_DEFAULT;
10698c2ecf20Sopenharmony_ci	nsim_dev->test1 = NSIM_DEV_TEST1_DEFAULT;
10708c2ecf20Sopenharmony_ci	spin_lock_init(&nsim_dev->fa_cookie_lock);
10718c2ecf20Sopenharmony_ci
10728c2ecf20Sopenharmony_ci	dev_set_drvdata(&nsim_bus_dev->dev, nsim_dev);
10738c2ecf20Sopenharmony_ci
10748c2ecf20Sopenharmony_ci	err = nsim_dev_resources_register(devlink);
10758c2ecf20Sopenharmony_ci	if (err)
10768c2ecf20Sopenharmony_ci		goto err_devlink_free;
10778c2ecf20Sopenharmony_ci
10788c2ecf20Sopenharmony_ci	err = devlink_register(devlink, &nsim_bus_dev->dev);
10798c2ecf20Sopenharmony_ci	if (err)
10808c2ecf20Sopenharmony_ci		goto err_resources_unregister;
10818c2ecf20Sopenharmony_ci
10828c2ecf20Sopenharmony_ci	err = devlink_params_register(devlink, nsim_devlink_params,
10838c2ecf20Sopenharmony_ci				      ARRAY_SIZE(nsim_devlink_params));
10848c2ecf20Sopenharmony_ci	if (err)
10858c2ecf20Sopenharmony_ci		goto err_dl_unregister;
10868c2ecf20Sopenharmony_ci	nsim_devlink_set_params_init_values(nsim_dev, devlink);
10878c2ecf20Sopenharmony_ci
10888c2ecf20Sopenharmony_ci	err = nsim_dev_dummy_region_init(nsim_dev, devlink);
10898c2ecf20Sopenharmony_ci	if (err)
10908c2ecf20Sopenharmony_ci		goto err_params_unregister;
10918c2ecf20Sopenharmony_ci
10928c2ecf20Sopenharmony_ci	err = nsim_dev_traps_init(devlink);
10938c2ecf20Sopenharmony_ci	if (err)
10948c2ecf20Sopenharmony_ci		goto err_dummy_region_exit;
10958c2ecf20Sopenharmony_ci
10968c2ecf20Sopenharmony_ci	err = nsim_dev_debugfs_init(nsim_dev);
10978c2ecf20Sopenharmony_ci	if (err)
10988c2ecf20Sopenharmony_ci		goto err_traps_exit;
10998c2ecf20Sopenharmony_ci
11008c2ecf20Sopenharmony_ci	nsim_dev->fib_data = nsim_fib_create(devlink, NULL);
11018c2ecf20Sopenharmony_ci	if (IS_ERR(nsim_dev->fib_data)) {
11028c2ecf20Sopenharmony_ci		err = PTR_ERR(nsim_dev->fib_data);
11038c2ecf20Sopenharmony_ci		goto err_debugfs_exit;
11048c2ecf20Sopenharmony_ci	}
11058c2ecf20Sopenharmony_ci
11068c2ecf20Sopenharmony_ci	err = nsim_dev_health_init(nsim_dev, devlink);
11078c2ecf20Sopenharmony_ci	if (err)
11088c2ecf20Sopenharmony_ci		goto err_fib_destroy;
11098c2ecf20Sopenharmony_ci
11108c2ecf20Sopenharmony_ci	err = nsim_bpf_dev_init(nsim_dev);
11118c2ecf20Sopenharmony_ci	if (err)
11128c2ecf20Sopenharmony_ci		goto err_health_exit;
11138c2ecf20Sopenharmony_ci
11148c2ecf20Sopenharmony_ci	err = nsim_dev_port_add_all(nsim_dev, nsim_bus_dev->port_count);
11158c2ecf20Sopenharmony_ci	if (err)
11168c2ecf20Sopenharmony_ci		goto err_bpf_dev_exit;
11178c2ecf20Sopenharmony_ci
11188c2ecf20Sopenharmony_ci	devlink_params_publish(devlink);
11198c2ecf20Sopenharmony_ci	devlink_reload_enable(devlink);
11208c2ecf20Sopenharmony_ci	return 0;
11218c2ecf20Sopenharmony_ci
11228c2ecf20Sopenharmony_cierr_bpf_dev_exit:
11238c2ecf20Sopenharmony_ci	nsim_bpf_dev_exit(nsim_dev);
11248c2ecf20Sopenharmony_cierr_health_exit:
11258c2ecf20Sopenharmony_ci	nsim_dev_health_exit(nsim_dev);
11268c2ecf20Sopenharmony_cierr_fib_destroy:
11278c2ecf20Sopenharmony_ci	nsim_fib_destroy(devlink, nsim_dev->fib_data);
11288c2ecf20Sopenharmony_cierr_debugfs_exit:
11298c2ecf20Sopenharmony_ci	nsim_dev_debugfs_exit(nsim_dev);
11308c2ecf20Sopenharmony_cierr_traps_exit:
11318c2ecf20Sopenharmony_ci	nsim_dev_traps_exit(devlink);
11328c2ecf20Sopenharmony_cierr_dummy_region_exit:
11338c2ecf20Sopenharmony_ci	nsim_dev_dummy_region_exit(nsim_dev);
11348c2ecf20Sopenharmony_cierr_params_unregister:
11358c2ecf20Sopenharmony_ci	devlink_params_unregister(devlink, nsim_devlink_params,
11368c2ecf20Sopenharmony_ci				  ARRAY_SIZE(nsim_devlink_params));
11378c2ecf20Sopenharmony_cierr_dl_unregister:
11388c2ecf20Sopenharmony_ci	devlink_unregister(devlink);
11398c2ecf20Sopenharmony_cierr_resources_unregister:
11408c2ecf20Sopenharmony_ci	devlink_resources_unregister(devlink, NULL);
11418c2ecf20Sopenharmony_cierr_devlink_free:
11428c2ecf20Sopenharmony_ci	devlink_free(devlink);
11438c2ecf20Sopenharmony_ci	return err;
11448c2ecf20Sopenharmony_ci}
11458c2ecf20Sopenharmony_ci
11468c2ecf20Sopenharmony_cistatic void nsim_dev_reload_destroy(struct nsim_dev *nsim_dev)
11478c2ecf20Sopenharmony_ci{
11488c2ecf20Sopenharmony_ci	struct devlink *devlink = priv_to_devlink(nsim_dev);
11498c2ecf20Sopenharmony_ci
11508c2ecf20Sopenharmony_ci	if (devlink_is_reload_failed(devlink))
11518c2ecf20Sopenharmony_ci		return;
11528c2ecf20Sopenharmony_ci	debugfs_remove(nsim_dev->take_snapshot);
11538c2ecf20Sopenharmony_ci	nsim_dev_port_del_all(nsim_dev);
11548c2ecf20Sopenharmony_ci	nsim_dev_health_exit(nsim_dev);
11558c2ecf20Sopenharmony_ci	nsim_fib_destroy(devlink, nsim_dev->fib_data);
11568c2ecf20Sopenharmony_ci	nsim_dev_traps_exit(devlink);
11578c2ecf20Sopenharmony_ci	nsim_dev_dummy_region_exit(nsim_dev);
11588c2ecf20Sopenharmony_ci	mutex_destroy(&nsim_dev->port_list_lock);
11598c2ecf20Sopenharmony_ci}
11608c2ecf20Sopenharmony_ci
11618c2ecf20Sopenharmony_civoid nsim_dev_remove(struct nsim_bus_dev *nsim_bus_dev)
11628c2ecf20Sopenharmony_ci{
11638c2ecf20Sopenharmony_ci	struct nsim_dev *nsim_dev = dev_get_drvdata(&nsim_bus_dev->dev);
11648c2ecf20Sopenharmony_ci	struct devlink *devlink = priv_to_devlink(nsim_dev);
11658c2ecf20Sopenharmony_ci
11668c2ecf20Sopenharmony_ci	devlink_reload_disable(devlink);
11678c2ecf20Sopenharmony_ci
11688c2ecf20Sopenharmony_ci	nsim_dev_reload_destroy(nsim_dev);
11698c2ecf20Sopenharmony_ci
11708c2ecf20Sopenharmony_ci	nsim_bpf_dev_exit(nsim_dev);
11718c2ecf20Sopenharmony_ci	nsim_dev_debugfs_exit(nsim_dev);
11728c2ecf20Sopenharmony_ci	devlink_params_unregister(devlink, nsim_devlink_params,
11738c2ecf20Sopenharmony_ci				  ARRAY_SIZE(nsim_devlink_params));
11748c2ecf20Sopenharmony_ci	devlink_unregister(devlink);
11758c2ecf20Sopenharmony_ci	devlink_resources_unregister(devlink, NULL);
11768c2ecf20Sopenharmony_ci	devlink_free(devlink);
11778c2ecf20Sopenharmony_ci}
11788c2ecf20Sopenharmony_ci
11798c2ecf20Sopenharmony_cistatic struct nsim_dev_port *
11808c2ecf20Sopenharmony_ci__nsim_dev_port_lookup(struct nsim_dev *nsim_dev, unsigned int port_index)
11818c2ecf20Sopenharmony_ci{
11828c2ecf20Sopenharmony_ci	struct nsim_dev_port *nsim_dev_port;
11838c2ecf20Sopenharmony_ci
11848c2ecf20Sopenharmony_ci	list_for_each_entry(nsim_dev_port, &nsim_dev->port_list, list)
11858c2ecf20Sopenharmony_ci		if (nsim_dev_port->port_index == port_index)
11868c2ecf20Sopenharmony_ci			return nsim_dev_port;
11878c2ecf20Sopenharmony_ci	return NULL;
11888c2ecf20Sopenharmony_ci}
11898c2ecf20Sopenharmony_ci
11908c2ecf20Sopenharmony_ciint nsim_dev_port_add(struct nsim_bus_dev *nsim_bus_dev,
11918c2ecf20Sopenharmony_ci		      unsigned int port_index)
11928c2ecf20Sopenharmony_ci{
11938c2ecf20Sopenharmony_ci	struct nsim_dev *nsim_dev = dev_get_drvdata(&nsim_bus_dev->dev);
11948c2ecf20Sopenharmony_ci	int err;
11958c2ecf20Sopenharmony_ci
11968c2ecf20Sopenharmony_ci	mutex_lock(&nsim_dev->port_list_lock);
11978c2ecf20Sopenharmony_ci	if (__nsim_dev_port_lookup(nsim_dev, port_index))
11988c2ecf20Sopenharmony_ci		err = -EEXIST;
11998c2ecf20Sopenharmony_ci	else
12008c2ecf20Sopenharmony_ci		err = __nsim_dev_port_add(nsim_dev, port_index);
12018c2ecf20Sopenharmony_ci	mutex_unlock(&nsim_dev->port_list_lock);
12028c2ecf20Sopenharmony_ci	return err;
12038c2ecf20Sopenharmony_ci}
12048c2ecf20Sopenharmony_ci
12058c2ecf20Sopenharmony_ciint nsim_dev_port_del(struct nsim_bus_dev *nsim_bus_dev,
12068c2ecf20Sopenharmony_ci		      unsigned int port_index)
12078c2ecf20Sopenharmony_ci{
12088c2ecf20Sopenharmony_ci	struct nsim_dev *nsim_dev = dev_get_drvdata(&nsim_bus_dev->dev);
12098c2ecf20Sopenharmony_ci	struct nsim_dev_port *nsim_dev_port;
12108c2ecf20Sopenharmony_ci	int err = 0;
12118c2ecf20Sopenharmony_ci
12128c2ecf20Sopenharmony_ci	mutex_lock(&nsim_dev->port_list_lock);
12138c2ecf20Sopenharmony_ci	nsim_dev_port = __nsim_dev_port_lookup(nsim_dev, port_index);
12148c2ecf20Sopenharmony_ci	if (!nsim_dev_port)
12158c2ecf20Sopenharmony_ci		err = -ENOENT;
12168c2ecf20Sopenharmony_ci	else
12178c2ecf20Sopenharmony_ci		__nsim_dev_port_del(nsim_dev_port);
12188c2ecf20Sopenharmony_ci	mutex_unlock(&nsim_dev->port_list_lock);
12198c2ecf20Sopenharmony_ci	return err;
12208c2ecf20Sopenharmony_ci}
12218c2ecf20Sopenharmony_ci
12228c2ecf20Sopenharmony_ciint nsim_dev_init(void)
12238c2ecf20Sopenharmony_ci{
12248c2ecf20Sopenharmony_ci	nsim_dev_ddir = debugfs_create_dir(DRV_NAME, NULL);
12258c2ecf20Sopenharmony_ci	return PTR_ERR_OR_ZERO(nsim_dev_ddir);
12268c2ecf20Sopenharmony_ci}
12278c2ecf20Sopenharmony_ci
12288c2ecf20Sopenharmony_civoid nsim_dev_exit(void)
12298c2ecf20Sopenharmony_ci{
12308c2ecf20Sopenharmony_ci	debugfs_remove_recursive(nsim_dev_ddir);
12318c2ecf20Sopenharmony_ci}
1232