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