xref: /openbmc/linux/drivers/net/netdevsim/dev.c (revision 82df5b73)
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 struct dentry *nsim_dev_ddir;
39 
40 #define NSIM_DEV_DUMMY_REGION_SIZE (1024 * 32)
41 
42 static int
43 nsim_dev_take_snapshot(struct devlink *devlink, struct netlink_ext_ack *extack,
44 		       u8 **data)
45 {
46 	void *dummy_data;
47 
48 	dummy_data = kmalloc(NSIM_DEV_DUMMY_REGION_SIZE, GFP_KERNEL);
49 	if (!dummy_data)
50 		return -ENOMEM;
51 
52 	get_random_bytes(dummy_data, NSIM_DEV_DUMMY_REGION_SIZE);
53 
54 	*data = dummy_data;
55 
56 	return 0;
57 }
58 
59 static ssize_t nsim_dev_take_snapshot_write(struct file *file,
60 					    const char __user *data,
61 					    size_t count, loff_t *ppos)
62 {
63 	struct nsim_dev *nsim_dev = file->private_data;
64 	struct devlink *devlink;
65 	u8 *dummy_data;
66 	int err;
67 	u32 id;
68 
69 	devlink = priv_to_devlink(nsim_dev);
70 
71 	err = nsim_dev_take_snapshot(devlink, NULL, &dummy_data);
72 	if (err)
73 		return err;
74 
75 	err = devlink_region_snapshot_id_get(devlink, &id);
76 	if (err) {
77 		pr_err("Failed to get snapshot id\n");
78 		kfree(dummy_data);
79 		return err;
80 	}
81 	err = devlink_region_snapshot_create(nsim_dev->dummy_region,
82 					     dummy_data, id);
83 	devlink_region_snapshot_id_put(devlink, id);
84 	if (err) {
85 		pr_err("Failed to create region snapshot\n");
86 		kfree(dummy_data);
87 		return err;
88 	}
89 
90 	return count;
91 }
92 
93 static const struct file_operations nsim_dev_take_snapshot_fops = {
94 	.open = simple_open,
95 	.write = nsim_dev_take_snapshot_write,
96 	.llseek = generic_file_llseek,
97 };
98 
99 static ssize_t nsim_dev_trap_fa_cookie_read(struct file *file,
100 					    char __user *data,
101 					    size_t count, loff_t *ppos)
102 {
103 	struct nsim_dev *nsim_dev = file->private_data;
104 	struct flow_action_cookie *fa_cookie;
105 	unsigned int buf_len;
106 	ssize_t ret;
107 	char *buf;
108 
109 	spin_lock(&nsim_dev->fa_cookie_lock);
110 	fa_cookie = nsim_dev->fa_cookie;
111 	if (!fa_cookie) {
112 		ret = -EINVAL;
113 		goto errout;
114 	}
115 	buf_len = fa_cookie->cookie_len * 2;
116 	buf = kmalloc(buf_len, GFP_ATOMIC);
117 	if (!buf) {
118 		ret = -ENOMEM;
119 		goto errout;
120 	}
121 	bin2hex(buf, fa_cookie->cookie, fa_cookie->cookie_len);
122 	spin_unlock(&nsim_dev->fa_cookie_lock);
123 
124 	ret = simple_read_from_buffer(data, count, ppos, buf, buf_len);
125 
126 	kfree(buf);
127 	return ret;
128 
129 errout:
130 	spin_unlock(&nsim_dev->fa_cookie_lock);
131 	return ret;
132 }
133 
134 static ssize_t nsim_dev_trap_fa_cookie_write(struct file *file,
135 					     const char __user *data,
136 					     size_t count, loff_t *ppos)
137 {
138 	struct nsim_dev *nsim_dev = file->private_data;
139 	struct flow_action_cookie *fa_cookie;
140 	size_t cookie_len;
141 	ssize_t ret;
142 	char *buf;
143 
144 	if (*ppos != 0)
145 		return -EINVAL;
146 	cookie_len = (count - 1) / 2;
147 	if ((count - 1) % 2)
148 		return -EINVAL;
149 	buf = kmalloc(count, GFP_KERNEL | __GFP_NOWARN);
150 	if (!buf)
151 		return -ENOMEM;
152 
153 	ret = simple_write_to_buffer(buf, count, ppos, data, count);
154 	if (ret < 0)
155 		goto free_buf;
156 
157 	fa_cookie = kmalloc(sizeof(*fa_cookie) + cookie_len,
158 			    GFP_KERNEL | __GFP_NOWARN);
159 	if (!fa_cookie) {
160 		ret = -ENOMEM;
161 		goto free_buf;
162 	}
163 
164 	fa_cookie->cookie_len = cookie_len;
165 	ret = hex2bin(fa_cookie->cookie, buf, cookie_len);
166 	if (ret)
167 		goto free_fa_cookie;
168 	kfree(buf);
169 
170 	spin_lock(&nsim_dev->fa_cookie_lock);
171 	kfree(nsim_dev->fa_cookie);
172 	nsim_dev->fa_cookie = fa_cookie;
173 	spin_unlock(&nsim_dev->fa_cookie_lock);
174 
175 	return count;
176 
177 free_fa_cookie:
178 	kfree(fa_cookie);
179 free_buf:
180 	kfree(buf);
181 	return ret;
182 }
183 
184 static const struct file_operations nsim_dev_trap_fa_cookie_fops = {
185 	.open = simple_open,
186 	.read = nsim_dev_trap_fa_cookie_read,
187 	.write = nsim_dev_trap_fa_cookie_write,
188 	.llseek = generic_file_llseek,
189 };
190 
191 static int nsim_dev_debugfs_init(struct nsim_dev *nsim_dev)
192 {
193 	char dev_ddir_name[sizeof(DRV_NAME) + 10];
194 
195 	sprintf(dev_ddir_name, DRV_NAME "%u", nsim_dev->nsim_bus_dev->dev.id);
196 	nsim_dev->ddir = debugfs_create_dir(dev_ddir_name, nsim_dev_ddir);
197 	if (IS_ERR(nsim_dev->ddir))
198 		return PTR_ERR(nsim_dev->ddir);
199 	nsim_dev->ports_ddir = debugfs_create_dir("ports", nsim_dev->ddir);
200 	if (IS_ERR(nsim_dev->ports_ddir))
201 		return PTR_ERR(nsim_dev->ports_ddir);
202 	debugfs_create_bool("fw_update_status", 0600, nsim_dev->ddir,
203 			    &nsim_dev->fw_update_status);
204 	debugfs_create_u32("max_macs", 0600, nsim_dev->ddir,
205 			   &nsim_dev->max_macs);
206 	debugfs_create_bool("test1", 0600, nsim_dev->ddir,
207 			    &nsim_dev->test1);
208 	nsim_dev->take_snapshot = debugfs_create_file("take_snapshot",
209 						      0200,
210 						      nsim_dev->ddir,
211 						      nsim_dev,
212 						&nsim_dev_take_snapshot_fops);
213 	debugfs_create_bool("dont_allow_reload", 0600, nsim_dev->ddir,
214 			    &nsim_dev->dont_allow_reload);
215 	debugfs_create_bool("fail_reload", 0600, nsim_dev->ddir,
216 			    &nsim_dev->fail_reload);
217 	debugfs_create_file("trap_flow_action_cookie", 0600, nsim_dev->ddir,
218 			    nsim_dev, &nsim_dev_trap_fa_cookie_fops);
219 	debugfs_create_bool("fail_trap_group_set", 0600,
220 			    nsim_dev->ddir,
221 			    &nsim_dev->fail_trap_group_set);
222 	debugfs_create_bool("fail_trap_policer_set", 0600,
223 			    nsim_dev->ddir,
224 			    &nsim_dev->fail_trap_policer_set);
225 	debugfs_create_bool("fail_trap_policer_counter_get", 0600,
226 			    nsim_dev->ddir,
227 			    &nsim_dev->fail_trap_policer_counter_get);
228 	return 0;
229 }
230 
231 static void nsim_dev_debugfs_exit(struct nsim_dev *nsim_dev)
232 {
233 	debugfs_remove_recursive(nsim_dev->ports_ddir);
234 	debugfs_remove_recursive(nsim_dev->ddir);
235 }
236 
237 static int nsim_dev_port_debugfs_init(struct nsim_dev *nsim_dev,
238 				      struct nsim_dev_port *nsim_dev_port)
239 {
240 	char port_ddir_name[16];
241 	char dev_link_name[32];
242 
243 	sprintf(port_ddir_name, "%u", nsim_dev_port->port_index);
244 	nsim_dev_port->ddir = debugfs_create_dir(port_ddir_name,
245 						 nsim_dev->ports_ddir);
246 	if (IS_ERR(nsim_dev_port->ddir))
247 		return PTR_ERR(nsim_dev_port->ddir);
248 
249 	sprintf(dev_link_name, "../../../" DRV_NAME "%u",
250 		nsim_dev->nsim_bus_dev->dev.id);
251 	debugfs_create_symlink("dev", nsim_dev_port->ddir, dev_link_name);
252 
253 	return 0;
254 }
255 
256 static void nsim_dev_port_debugfs_exit(struct nsim_dev_port *nsim_dev_port)
257 {
258 	debugfs_remove_recursive(nsim_dev_port->ddir);
259 }
260 
261 static int nsim_dev_resources_register(struct devlink *devlink)
262 {
263 	struct devlink_resource_size_params params = {
264 		.size_max = (u64)-1,
265 		.size_granularity = 1,
266 		.unit = DEVLINK_RESOURCE_UNIT_ENTRY
267 	};
268 	int err;
269 
270 	/* Resources for IPv4 */
271 	err = devlink_resource_register(devlink, "IPv4", (u64)-1,
272 					NSIM_RESOURCE_IPV4,
273 					DEVLINK_RESOURCE_ID_PARENT_TOP,
274 					&params);
275 	if (err) {
276 		pr_err("Failed to register IPv4 top resource\n");
277 		goto out;
278 	}
279 
280 	err = devlink_resource_register(devlink, "fib", (u64)-1,
281 					NSIM_RESOURCE_IPV4_FIB,
282 					NSIM_RESOURCE_IPV4, &params);
283 	if (err) {
284 		pr_err("Failed to register IPv4 FIB resource\n");
285 		return err;
286 	}
287 
288 	err = devlink_resource_register(devlink, "fib-rules", (u64)-1,
289 					NSIM_RESOURCE_IPV4_FIB_RULES,
290 					NSIM_RESOURCE_IPV4, &params);
291 	if (err) {
292 		pr_err("Failed to register IPv4 FIB rules resource\n");
293 		return err;
294 	}
295 
296 	/* Resources for IPv6 */
297 	err = devlink_resource_register(devlink, "IPv6", (u64)-1,
298 					NSIM_RESOURCE_IPV6,
299 					DEVLINK_RESOURCE_ID_PARENT_TOP,
300 					&params);
301 	if (err) {
302 		pr_err("Failed to register IPv6 top resource\n");
303 		goto out;
304 	}
305 
306 	err = devlink_resource_register(devlink, "fib", (u64)-1,
307 					NSIM_RESOURCE_IPV6_FIB,
308 					NSIM_RESOURCE_IPV6, &params);
309 	if (err) {
310 		pr_err("Failed to register IPv6 FIB resource\n");
311 		return err;
312 	}
313 
314 	err = devlink_resource_register(devlink, "fib-rules", (u64)-1,
315 					NSIM_RESOURCE_IPV6_FIB_RULES,
316 					NSIM_RESOURCE_IPV6, &params);
317 	if (err) {
318 		pr_err("Failed to register IPv6 FIB rules resource\n");
319 		return err;
320 	}
321 
322 out:
323 	return err;
324 }
325 
326 enum nsim_devlink_param_id {
327 	NSIM_DEVLINK_PARAM_ID_BASE = DEVLINK_PARAM_GENERIC_ID_MAX,
328 	NSIM_DEVLINK_PARAM_ID_TEST1,
329 };
330 
331 static const struct devlink_param nsim_devlink_params[] = {
332 	DEVLINK_PARAM_GENERIC(MAX_MACS,
333 			      BIT(DEVLINK_PARAM_CMODE_DRIVERINIT),
334 			      NULL, NULL, NULL),
335 	DEVLINK_PARAM_DRIVER(NSIM_DEVLINK_PARAM_ID_TEST1,
336 			     "test1", DEVLINK_PARAM_TYPE_BOOL,
337 			     BIT(DEVLINK_PARAM_CMODE_DRIVERINIT),
338 			     NULL, NULL, NULL),
339 };
340 
341 static void nsim_devlink_set_params_init_values(struct nsim_dev *nsim_dev,
342 						struct devlink *devlink)
343 {
344 	union devlink_param_value value;
345 
346 	value.vu32 = nsim_dev->max_macs;
347 	devlink_param_driverinit_value_set(devlink,
348 					   DEVLINK_PARAM_GENERIC_ID_MAX_MACS,
349 					   value);
350 	value.vbool = nsim_dev->test1;
351 	devlink_param_driverinit_value_set(devlink,
352 					   NSIM_DEVLINK_PARAM_ID_TEST1,
353 					   value);
354 }
355 
356 static void nsim_devlink_param_load_driverinit_values(struct devlink *devlink)
357 {
358 	struct nsim_dev *nsim_dev = devlink_priv(devlink);
359 	union devlink_param_value saved_value;
360 	int err;
361 
362 	err = devlink_param_driverinit_value_get(devlink,
363 						 DEVLINK_PARAM_GENERIC_ID_MAX_MACS,
364 						 &saved_value);
365 	if (!err)
366 		nsim_dev->max_macs = saved_value.vu32;
367 	err = devlink_param_driverinit_value_get(devlink,
368 						 NSIM_DEVLINK_PARAM_ID_TEST1,
369 						 &saved_value);
370 	if (!err)
371 		nsim_dev->test1 = saved_value.vbool;
372 }
373 
374 #define NSIM_DEV_DUMMY_REGION_SNAPSHOT_MAX 16
375 
376 static const struct devlink_region_ops dummy_region_ops = {
377 	.name = "dummy",
378 	.destructor = &kfree,
379 	.snapshot = nsim_dev_take_snapshot,
380 };
381 
382 static int nsim_dev_dummy_region_init(struct nsim_dev *nsim_dev,
383 				      struct devlink *devlink)
384 {
385 	nsim_dev->dummy_region =
386 		devlink_region_create(devlink, &dummy_region_ops,
387 				      NSIM_DEV_DUMMY_REGION_SNAPSHOT_MAX,
388 				      NSIM_DEV_DUMMY_REGION_SIZE);
389 	return PTR_ERR_OR_ZERO(nsim_dev->dummy_region);
390 }
391 
392 static void nsim_dev_dummy_region_exit(struct nsim_dev *nsim_dev)
393 {
394 	devlink_region_destroy(nsim_dev->dummy_region);
395 }
396 
397 struct nsim_trap_item {
398 	void *trap_ctx;
399 	enum devlink_trap_action action;
400 };
401 
402 struct nsim_trap_data {
403 	struct delayed_work trap_report_dw;
404 	struct nsim_trap_item *trap_items_arr;
405 	u64 *trap_policers_cnt_arr;
406 	struct nsim_dev *nsim_dev;
407 	spinlock_t trap_lock;	/* Protects trap_items_arr */
408 };
409 
410 /* All driver-specific traps must be documented in
411  * Documentation/networking/devlink/netdevsim.rst
412  */
413 enum {
414 	NSIM_TRAP_ID_BASE = DEVLINK_TRAP_GENERIC_ID_MAX,
415 	NSIM_TRAP_ID_FID_MISS,
416 };
417 
418 #define NSIM_TRAP_NAME_FID_MISS "fid_miss"
419 
420 #define NSIM_TRAP_METADATA DEVLINK_TRAP_METADATA_TYPE_F_IN_PORT
421 
422 #define NSIM_TRAP_DROP(_id, _group_id)					      \
423 	DEVLINK_TRAP_GENERIC(DROP, DROP, _id,				      \
424 			     DEVLINK_TRAP_GROUP_GENERIC_ID_##_group_id,	      \
425 			     NSIM_TRAP_METADATA)
426 #define NSIM_TRAP_DROP_EXT(_id, _group_id, _metadata)			      \
427 	DEVLINK_TRAP_GENERIC(DROP, DROP, _id,				      \
428 			     DEVLINK_TRAP_GROUP_GENERIC_ID_##_group_id,	      \
429 			     NSIM_TRAP_METADATA | (_metadata))
430 #define NSIM_TRAP_EXCEPTION(_id, _group_id)				      \
431 	DEVLINK_TRAP_GENERIC(EXCEPTION, TRAP, _id,			      \
432 			     DEVLINK_TRAP_GROUP_GENERIC_ID_##_group_id,	      \
433 			     NSIM_TRAP_METADATA)
434 #define NSIM_TRAP_CONTROL(_id, _group_id, _action)			      \
435 	DEVLINK_TRAP_GENERIC(CONTROL, _action, _id,			      \
436 			     DEVLINK_TRAP_GROUP_GENERIC_ID_##_group_id,	      \
437 			     NSIM_TRAP_METADATA)
438 #define NSIM_TRAP_DRIVER_EXCEPTION(_id, _group_id)			      \
439 	DEVLINK_TRAP_DRIVER(EXCEPTION, TRAP, NSIM_TRAP_ID_##_id,	      \
440 			    NSIM_TRAP_NAME_##_id,			      \
441 			    DEVLINK_TRAP_GROUP_GENERIC_ID_##_group_id,	      \
442 			    NSIM_TRAP_METADATA)
443 
444 #define NSIM_DEV_TRAP_POLICER_MIN_RATE	1
445 #define NSIM_DEV_TRAP_POLICER_MAX_RATE	8000
446 #define NSIM_DEV_TRAP_POLICER_MIN_BURST	8
447 #define NSIM_DEV_TRAP_POLICER_MAX_BURST	65536
448 
449 #define NSIM_TRAP_POLICER(_id, _rate, _burst)				      \
450 	DEVLINK_TRAP_POLICER(_id, _rate, _burst,			      \
451 			     NSIM_DEV_TRAP_POLICER_MAX_RATE,		      \
452 			     NSIM_DEV_TRAP_POLICER_MIN_RATE,		      \
453 			     NSIM_DEV_TRAP_POLICER_MAX_BURST,		      \
454 			     NSIM_DEV_TRAP_POLICER_MIN_BURST)
455 
456 static const struct devlink_trap_policer nsim_trap_policers_arr[] = {
457 	NSIM_TRAP_POLICER(1, 1000, 128),
458 	NSIM_TRAP_POLICER(2, 2000, 256),
459 	NSIM_TRAP_POLICER(3, 3000, 512),
460 };
461 
462 static const struct devlink_trap_group nsim_trap_groups_arr[] = {
463 	DEVLINK_TRAP_GROUP_GENERIC(L2_DROPS, 0),
464 	DEVLINK_TRAP_GROUP_GENERIC(L3_DROPS, 1),
465 	DEVLINK_TRAP_GROUP_GENERIC(L3_EXCEPTIONS, 1),
466 	DEVLINK_TRAP_GROUP_GENERIC(BUFFER_DROPS, 2),
467 	DEVLINK_TRAP_GROUP_GENERIC(ACL_DROPS, 3),
468 	DEVLINK_TRAP_GROUP_GENERIC(MC_SNOOPING, 3),
469 };
470 
471 static const struct devlink_trap nsim_traps_arr[] = {
472 	NSIM_TRAP_DROP(SMAC_MC, L2_DROPS),
473 	NSIM_TRAP_DROP(VLAN_TAG_MISMATCH, L2_DROPS),
474 	NSIM_TRAP_DROP(INGRESS_VLAN_FILTER, L2_DROPS),
475 	NSIM_TRAP_DROP(INGRESS_STP_FILTER, L2_DROPS),
476 	NSIM_TRAP_DROP(EMPTY_TX_LIST, L2_DROPS),
477 	NSIM_TRAP_DROP(PORT_LOOPBACK_FILTER, L2_DROPS),
478 	NSIM_TRAP_DRIVER_EXCEPTION(FID_MISS, L2_DROPS),
479 	NSIM_TRAP_DROP(BLACKHOLE_ROUTE, L3_DROPS),
480 	NSIM_TRAP_EXCEPTION(TTL_ERROR, L3_EXCEPTIONS),
481 	NSIM_TRAP_DROP(TAIL_DROP, BUFFER_DROPS),
482 	NSIM_TRAP_DROP_EXT(INGRESS_FLOW_ACTION_DROP, ACL_DROPS,
483 			   DEVLINK_TRAP_METADATA_TYPE_F_FA_COOKIE),
484 	NSIM_TRAP_DROP_EXT(EGRESS_FLOW_ACTION_DROP, ACL_DROPS,
485 			   DEVLINK_TRAP_METADATA_TYPE_F_FA_COOKIE),
486 	NSIM_TRAP_CONTROL(IGMP_QUERY, MC_SNOOPING, MIRROR),
487 	NSIM_TRAP_CONTROL(IGMP_V1_REPORT, MC_SNOOPING, TRAP),
488 };
489 
490 #define NSIM_TRAP_L4_DATA_LEN 100
491 
492 static struct sk_buff *nsim_dev_trap_skb_build(void)
493 {
494 	int tot_len, data_len = NSIM_TRAP_L4_DATA_LEN;
495 	struct sk_buff *skb;
496 	struct udphdr *udph;
497 	struct ethhdr *eth;
498 	struct iphdr *iph;
499 
500 	skb = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC);
501 	if (!skb)
502 		return NULL;
503 	tot_len = sizeof(struct iphdr) + sizeof(struct udphdr) + data_len;
504 
505 	skb_reset_mac_header(skb);
506 	eth = skb_put(skb, sizeof(struct ethhdr));
507 	eth_random_addr(eth->h_dest);
508 	eth_random_addr(eth->h_source);
509 	eth->h_proto = htons(ETH_P_IP);
510 	skb->protocol = htons(ETH_P_IP);
511 
512 	skb_set_network_header(skb, skb->len);
513 	iph = skb_put(skb, sizeof(struct iphdr));
514 	iph->protocol = IPPROTO_UDP;
515 	iph->saddr = in_aton("192.0.2.1");
516 	iph->daddr = in_aton("198.51.100.1");
517 	iph->version = 0x4;
518 	iph->frag_off = 0;
519 	iph->ihl = 0x5;
520 	iph->tot_len = htons(tot_len);
521 	iph->ttl = 100;
522 	iph->check = 0;
523 	iph->check = ip_fast_csum((unsigned char *)iph, iph->ihl);
524 
525 	skb_set_transport_header(skb, skb->len);
526 	udph = skb_put_zero(skb, sizeof(struct udphdr) + data_len);
527 	get_random_bytes(&udph->source, sizeof(u16));
528 	get_random_bytes(&udph->dest, sizeof(u16));
529 	udph->len = htons(sizeof(struct udphdr) + data_len);
530 
531 	return skb;
532 }
533 
534 static void nsim_dev_trap_report(struct nsim_dev_port *nsim_dev_port)
535 {
536 	struct nsim_dev *nsim_dev = nsim_dev_port->ns->nsim_dev;
537 	struct devlink *devlink = priv_to_devlink(nsim_dev);
538 	struct nsim_trap_data *nsim_trap_data;
539 	int i;
540 
541 	nsim_trap_data = nsim_dev->trap_data;
542 
543 	spin_lock(&nsim_trap_data->trap_lock);
544 	for (i = 0; i < ARRAY_SIZE(nsim_traps_arr); i++) {
545 		struct flow_action_cookie *fa_cookie = NULL;
546 		struct nsim_trap_item *nsim_trap_item;
547 		struct sk_buff *skb;
548 		bool has_fa_cookie;
549 
550 		has_fa_cookie = nsim_traps_arr[i].metadata_cap &
551 				DEVLINK_TRAP_METADATA_TYPE_F_FA_COOKIE;
552 
553 		nsim_trap_item = &nsim_trap_data->trap_items_arr[i];
554 		if (nsim_trap_item->action == DEVLINK_TRAP_ACTION_DROP)
555 			continue;
556 
557 		skb = nsim_dev_trap_skb_build();
558 		if (!skb)
559 			continue;
560 		skb->dev = nsim_dev_port->ns->netdev;
561 
562 		/* Trapped packets are usually passed to devlink in softIRQ,
563 		 * but in this case they are generated in a workqueue. Disable
564 		 * softIRQs to prevent lockdep from complaining about
565 		 * "incosistent lock state".
566 		 */
567 
568 		spin_lock_bh(&nsim_dev->fa_cookie_lock);
569 		fa_cookie = has_fa_cookie ? nsim_dev->fa_cookie : NULL;
570 		devlink_trap_report(devlink, skb, nsim_trap_item->trap_ctx,
571 				    &nsim_dev_port->devlink_port, fa_cookie);
572 		spin_unlock_bh(&nsim_dev->fa_cookie_lock);
573 		consume_skb(skb);
574 	}
575 	spin_unlock(&nsim_trap_data->trap_lock);
576 }
577 
578 #define NSIM_TRAP_REPORT_INTERVAL_MS	100
579 
580 static void nsim_dev_trap_report_work(struct work_struct *work)
581 {
582 	struct nsim_trap_data *nsim_trap_data;
583 	struct nsim_dev_port *nsim_dev_port;
584 	struct nsim_dev *nsim_dev;
585 
586 	nsim_trap_data = container_of(work, struct nsim_trap_data,
587 				      trap_report_dw.work);
588 	nsim_dev = nsim_trap_data->nsim_dev;
589 
590 	/* For each running port and enabled packet trap, generate a UDP
591 	 * packet with a random 5-tuple and report it.
592 	 */
593 	mutex_lock(&nsim_dev->port_list_lock);
594 	list_for_each_entry(nsim_dev_port, &nsim_dev->port_list, list) {
595 		if (!netif_running(nsim_dev_port->ns->netdev))
596 			continue;
597 
598 		nsim_dev_trap_report(nsim_dev_port);
599 	}
600 	mutex_unlock(&nsim_dev->port_list_lock);
601 
602 	schedule_delayed_work(&nsim_dev->trap_data->trap_report_dw,
603 			      msecs_to_jiffies(NSIM_TRAP_REPORT_INTERVAL_MS));
604 }
605 
606 static int nsim_dev_traps_init(struct devlink *devlink)
607 {
608 	size_t policers_count = ARRAY_SIZE(nsim_trap_policers_arr);
609 	struct nsim_dev *nsim_dev = devlink_priv(devlink);
610 	struct nsim_trap_data *nsim_trap_data;
611 	int err;
612 
613 	nsim_trap_data = kzalloc(sizeof(*nsim_trap_data), GFP_KERNEL);
614 	if (!nsim_trap_data)
615 		return -ENOMEM;
616 
617 	nsim_trap_data->trap_items_arr = kcalloc(ARRAY_SIZE(nsim_traps_arr),
618 						 sizeof(struct nsim_trap_item),
619 						 GFP_KERNEL);
620 	if (!nsim_trap_data->trap_items_arr) {
621 		err = -ENOMEM;
622 		goto err_trap_data_free;
623 	}
624 
625 	nsim_trap_data->trap_policers_cnt_arr = kcalloc(policers_count,
626 							sizeof(u64),
627 							GFP_KERNEL);
628 	if (!nsim_trap_data->trap_policers_cnt_arr) {
629 		err = -ENOMEM;
630 		goto err_trap_items_free;
631 	}
632 
633 	/* The lock is used to protect the action state of the registered
634 	 * traps. The value is written by user and read in delayed work when
635 	 * iterating over all the traps.
636 	 */
637 	spin_lock_init(&nsim_trap_data->trap_lock);
638 	nsim_trap_data->nsim_dev = nsim_dev;
639 	nsim_dev->trap_data = nsim_trap_data;
640 
641 	err = devlink_trap_policers_register(devlink, nsim_trap_policers_arr,
642 					     policers_count);
643 	if (err)
644 		goto err_trap_policers_cnt_free;
645 
646 	err = devlink_trap_groups_register(devlink, nsim_trap_groups_arr,
647 					   ARRAY_SIZE(nsim_trap_groups_arr));
648 	if (err)
649 		goto err_trap_policers_unregister;
650 
651 	err = devlink_traps_register(devlink, nsim_traps_arr,
652 				     ARRAY_SIZE(nsim_traps_arr), NULL);
653 	if (err)
654 		goto err_trap_groups_unregister;
655 
656 	INIT_DELAYED_WORK(&nsim_dev->trap_data->trap_report_dw,
657 			  nsim_dev_trap_report_work);
658 	schedule_delayed_work(&nsim_dev->trap_data->trap_report_dw,
659 			      msecs_to_jiffies(NSIM_TRAP_REPORT_INTERVAL_MS));
660 
661 	return 0;
662 
663 err_trap_groups_unregister:
664 	devlink_trap_groups_unregister(devlink, nsim_trap_groups_arr,
665 				       ARRAY_SIZE(nsim_trap_groups_arr));
666 err_trap_policers_unregister:
667 	devlink_trap_policers_unregister(devlink, nsim_trap_policers_arr,
668 					 ARRAY_SIZE(nsim_trap_policers_arr));
669 err_trap_policers_cnt_free:
670 	kfree(nsim_trap_data->trap_policers_cnt_arr);
671 err_trap_items_free:
672 	kfree(nsim_trap_data->trap_items_arr);
673 err_trap_data_free:
674 	kfree(nsim_trap_data);
675 	return err;
676 }
677 
678 static void nsim_dev_traps_exit(struct devlink *devlink)
679 {
680 	struct nsim_dev *nsim_dev = devlink_priv(devlink);
681 
682 	cancel_delayed_work_sync(&nsim_dev->trap_data->trap_report_dw);
683 	devlink_traps_unregister(devlink, nsim_traps_arr,
684 				 ARRAY_SIZE(nsim_traps_arr));
685 	devlink_trap_groups_unregister(devlink, nsim_trap_groups_arr,
686 				       ARRAY_SIZE(nsim_trap_groups_arr));
687 	devlink_trap_policers_unregister(devlink, nsim_trap_policers_arr,
688 					 ARRAY_SIZE(nsim_trap_policers_arr));
689 	kfree(nsim_dev->trap_data->trap_policers_cnt_arr);
690 	kfree(nsim_dev->trap_data->trap_items_arr);
691 	kfree(nsim_dev->trap_data);
692 }
693 
694 static int nsim_dev_reload_create(struct nsim_dev *nsim_dev,
695 				  struct netlink_ext_ack *extack);
696 static void nsim_dev_reload_destroy(struct nsim_dev *nsim_dev);
697 
698 static int nsim_dev_reload_down(struct devlink *devlink, bool netns_change,
699 				struct netlink_ext_ack *extack)
700 {
701 	struct nsim_dev *nsim_dev = devlink_priv(devlink);
702 
703 	if (nsim_dev->dont_allow_reload) {
704 		/* For testing purposes, user set debugfs dont_allow_reload
705 		 * value to true. So forbid it.
706 		 */
707 		NL_SET_ERR_MSG_MOD(extack, "User forbid the reload for testing purposes");
708 		return -EOPNOTSUPP;
709 	}
710 
711 	nsim_dev_reload_destroy(nsim_dev);
712 	return 0;
713 }
714 
715 static int nsim_dev_reload_up(struct devlink *devlink,
716 			      struct netlink_ext_ack *extack)
717 {
718 	struct nsim_dev *nsim_dev = devlink_priv(devlink);
719 
720 	if (nsim_dev->fail_reload) {
721 		/* For testing purposes, user set debugfs fail_reload
722 		 * value to true. Fail right away.
723 		 */
724 		NL_SET_ERR_MSG_MOD(extack, "User setup the reload to fail for testing purposes");
725 		return -EINVAL;
726 	}
727 
728 	return nsim_dev_reload_create(nsim_dev, extack);
729 }
730 
731 static int nsim_dev_info_get(struct devlink *devlink,
732 			     struct devlink_info_req *req,
733 			     struct netlink_ext_ack *extack)
734 {
735 	return devlink_info_driver_name_put(req, DRV_NAME);
736 }
737 
738 #define NSIM_DEV_FLASH_SIZE 500000
739 #define NSIM_DEV_FLASH_CHUNK_SIZE 1000
740 #define NSIM_DEV_FLASH_CHUNK_TIME_MS 10
741 
742 static int nsim_dev_flash_update(struct devlink *devlink, const char *file_name,
743 				 const char *component,
744 				 struct netlink_ext_ack *extack)
745 {
746 	struct nsim_dev *nsim_dev = devlink_priv(devlink);
747 	int i;
748 
749 	if (nsim_dev->fw_update_status) {
750 		devlink_flash_update_begin_notify(devlink);
751 		devlink_flash_update_status_notify(devlink,
752 						   "Preparing to flash",
753 						   component, 0, 0);
754 	}
755 
756 	for (i = 0; i < NSIM_DEV_FLASH_SIZE / NSIM_DEV_FLASH_CHUNK_SIZE; i++) {
757 		if (nsim_dev->fw_update_status)
758 			devlink_flash_update_status_notify(devlink, "Flashing",
759 							   component,
760 							   i * NSIM_DEV_FLASH_CHUNK_SIZE,
761 							   NSIM_DEV_FLASH_SIZE);
762 		msleep(NSIM_DEV_FLASH_CHUNK_TIME_MS);
763 	}
764 
765 	if (nsim_dev->fw_update_status) {
766 		devlink_flash_update_status_notify(devlink, "Flashing",
767 						   component,
768 						   NSIM_DEV_FLASH_SIZE,
769 						   NSIM_DEV_FLASH_SIZE);
770 		devlink_flash_update_status_notify(devlink, "Flashing done",
771 						   component, 0, 0);
772 		devlink_flash_update_end_notify(devlink);
773 	}
774 
775 	return 0;
776 }
777 
778 static struct nsim_trap_item *
779 nsim_dev_trap_item_lookup(struct nsim_dev *nsim_dev, u16 trap_id)
780 {
781 	struct nsim_trap_data *nsim_trap_data = nsim_dev->trap_data;
782 	int i;
783 
784 	for (i = 0; i < ARRAY_SIZE(nsim_traps_arr); i++) {
785 		if (nsim_traps_arr[i].id == trap_id)
786 			return &nsim_trap_data->trap_items_arr[i];
787 	}
788 
789 	return NULL;
790 }
791 
792 static int nsim_dev_devlink_trap_init(struct devlink *devlink,
793 				      const struct devlink_trap *trap,
794 				      void *trap_ctx)
795 {
796 	struct nsim_dev *nsim_dev = devlink_priv(devlink);
797 	struct nsim_trap_item *nsim_trap_item;
798 
799 	nsim_trap_item = nsim_dev_trap_item_lookup(nsim_dev, trap->id);
800 	if (WARN_ON(!nsim_trap_item))
801 		return -ENOENT;
802 
803 	nsim_trap_item->trap_ctx = trap_ctx;
804 	nsim_trap_item->action = trap->init_action;
805 
806 	return 0;
807 }
808 
809 static int
810 nsim_dev_devlink_trap_action_set(struct devlink *devlink,
811 				 const struct devlink_trap *trap,
812 				 enum devlink_trap_action action)
813 {
814 	struct nsim_dev *nsim_dev = devlink_priv(devlink);
815 	struct nsim_trap_item *nsim_trap_item;
816 
817 	nsim_trap_item = nsim_dev_trap_item_lookup(nsim_dev, trap->id);
818 	if (WARN_ON(!nsim_trap_item))
819 		return -ENOENT;
820 
821 	spin_lock(&nsim_dev->trap_data->trap_lock);
822 	nsim_trap_item->action = action;
823 	spin_unlock(&nsim_dev->trap_data->trap_lock);
824 
825 	return 0;
826 }
827 
828 static int
829 nsim_dev_devlink_trap_group_set(struct devlink *devlink,
830 				const struct devlink_trap_group *group,
831 				const struct devlink_trap_policer *policer)
832 {
833 	struct nsim_dev *nsim_dev = devlink_priv(devlink);
834 
835 	if (nsim_dev->fail_trap_group_set)
836 		return -EINVAL;
837 
838 	return 0;
839 }
840 
841 static int
842 nsim_dev_devlink_trap_policer_set(struct devlink *devlink,
843 				  const struct devlink_trap_policer *policer,
844 				  u64 rate, u64 burst,
845 				  struct netlink_ext_ack *extack)
846 {
847 	struct nsim_dev *nsim_dev = devlink_priv(devlink);
848 
849 	if (nsim_dev->fail_trap_policer_set) {
850 		NL_SET_ERR_MSG_MOD(extack, "User setup the operation to fail for testing purposes");
851 		return -EINVAL;
852 	}
853 
854 	return 0;
855 }
856 
857 static int
858 nsim_dev_devlink_trap_policer_counter_get(struct devlink *devlink,
859 					  const struct devlink_trap_policer *policer,
860 					  u64 *p_drops)
861 {
862 	struct nsim_dev *nsim_dev = devlink_priv(devlink);
863 	u64 *cnt;
864 
865 	if (nsim_dev->fail_trap_policer_counter_get)
866 		return -EINVAL;
867 
868 	cnt = &nsim_dev->trap_data->trap_policers_cnt_arr[policer->id - 1];
869 	*p_drops = (*cnt)++;
870 
871 	return 0;
872 }
873 
874 static const struct devlink_ops nsim_dev_devlink_ops = {
875 	.reload_down = nsim_dev_reload_down,
876 	.reload_up = nsim_dev_reload_up,
877 	.info_get = nsim_dev_info_get,
878 	.flash_update = nsim_dev_flash_update,
879 	.trap_init = nsim_dev_devlink_trap_init,
880 	.trap_action_set = nsim_dev_devlink_trap_action_set,
881 	.trap_group_set = nsim_dev_devlink_trap_group_set,
882 	.trap_policer_set = nsim_dev_devlink_trap_policer_set,
883 	.trap_policer_counter_get = nsim_dev_devlink_trap_policer_counter_get,
884 };
885 
886 #define NSIM_DEV_MAX_MACS_DEFAULT 32
887 #define NSIM_DEV_TEST1_DEFAULT true
888 
889 static int __nsim_dev_port_add(struct nsim_dev *nsim_dev,
890 			       unsigned int port_index)
891 {
892 	struct nsim_dev_port *nsim_dev_port;
893 	struct devlink_port *devlink_port;
894 	int err;
895 
896 	nsim_dev_port = kzalloc(sizeof(*nsim_dev_port), GFP_KERNEL);
897 	if (!nsim_dev_port)
898 		return -ENOMEM;
899 	nsim_dev_port->port_index = port_index;
900 
901 	devlink_port = &nsim_dev_port->devlink_port;
902 	devlink_port_attrs_set(devlink_port, DEVLINK_PORT_FLAVOUR_PHYSICAL,
903 			       port_index + 1, 0, 0,
904 			       nsim_dev->switch_id.id,
905 			       nsim_dev->switch_id.id_len);
906 	err = devlink_port_register(priv_to_devlink(nsim_dev), devlink_port,
907 				    port_index);
908 	if (err)
909 		goto err_port_free;
910 
911 	err = nsim_dev_port_debugfs_init(nsim_dev, nsim_dev_port);
912 	if (err)
913 		goto err_dl_port_unregister;
914 
915 	nsim_dev_port->ns = nsim_create(nsim_dev, nsim_dev_port);
916 	if (IS_ERR(nsim_dev_port->ns)) {
917 		err = PTR_ERR(nsim_dev_port->ns);
918 		goto err_port_debugfs_exit;
919 	}
920 
921 	devlink_port_type_eth_set(devlink_port, nsim_dev_port->ns->netdev);
922 	list_add(&nsim_dev_port->list, &nsim_dev->port_list);
923 
924 	return 0;
925 
926 err_port_debugfs_exit:
927 	nsim_dev_port_debugfs_exit(nsim_dev_port);
928 err_dl_port_unregister:
929 	devlink_port_unregister(devlink_port);
930 err_port_free:
931 	kfree(nsim_dev_port);
932 	return err;
933 }
934 
935 static void __nsim_dev_port_del(struct nsim_dev_port *nsim_dev_port)
936 {
937 	struct devlink_port *devlink_port = &nsim_dev_port->devlink_port;
938 
939 	list_del(&nsim_dev_port->list);
940 	devlink_port_type_clear(devlink_port);
941 	nsim_destroy(nsim_dev_port->ns);
942 	nsim_dev_port_debugfs_exit(nsim_dev_port);
943 	devlink_port_unregister(devlink_port);
944 	kfree(nsim_dev_port);
945 }
946 
947 static void nsim_dev_port_del_all(struct nsim_dev *nsim_dev)
948 {
949 	struct nsim_dev_port *nsim_dev_port, *tmp;
950 
951 	mutex_lock(&nsim_dev->port_list_lock);
952 	list_for_each_entry_safe(nsim_dev_port, tmp,
953 				 &nsim_dev->port_list, list)
954 		__nsim_dev_port_del(nsim_dev_port);
955 	mutex_unlock(&nsim_dev->port_list_lock);
956 }
957 
958 static int nsim_dev_port_add_all(struct nsim_dev *nsim_dev,
959 				 unsigned int port_count)
960 {
961 	int i, err;
962 
963 	for (i = 0; i < port_count; i++) {
964 		err = __nsim_dev_port_add(nsim_dev, i);
965 		if (err)
966 			goto err_port_del_all;
967 	}
968 	return 0;
969 
970 err_port_del_all:
971 	nsim_dev_port_del_all(nsim_dev);
972 	return err;
973 }
974 
975 static int nsim_dev_reload_create(struct nsim_dev *nsim_dev,
976 				  struct netlink_ext_ack *extack)
977 {
978 	struct nsim_bus_dev *nsim_bus_dev = nsim_dev->nsim_bus_dev;
979 	struct devlink *devlink;
980 	int err;
981 
982 	devlink = priv_to_devlink(nsim_dev);
983 	nsim_dev = devlink_priv(devlink);
984 	INIT_LIST_HEAD(&nsim_dev->port_list);
985 	mutex_init(&nsim_dev->port_list_lock);
986 	nsim_dev->fw_update_status = true;
987 
988 	nsim_dev->fib_data = nsim_fib_create(devlink, extack);
989 	if (IS_ERR(nsim_dev->fib_data))
990 		return PTR_ERR(nsim_dev->fib_data);
991 
992 	nsim_devlink_param_load_driverinit_values(devlink);
993 
994 	err = nsim_dev_dummy_region_init(nsim_dev, devlink);
995 	if (err)
996 		goto err_fib_destroy;
997 
998 	err = nsim_dev_traps_init(devlink);
999 	if (err)
1000 		goto err_dummy_region_exit;
1001 
1002 	err = nsim_dev_health_init(nsim_dev, devlink);
1003 	if (err)
1004 		goto err_traps_exit;
1005 
1006 	err = nsim_dev_port_add_all(nsim_dev, nsim_bus_dev->port_count);
1007 	if (err)
1008 		goto err_health_exit;
1009 
1010 	nsim_dev->take_snapshot = debugfs_create_file("take_snapshot",
1011 						      0200,
1012 						      nsim_dev->ddir,
1013 						      nsim_dev,
1014 						&nsim_dev_take_snapshot_fops);
1015 	return 0;
1016 
1017 err_health_exit:
1018 	nsim_dev_health_exit(nsim_dev);
1019 err_traps_exit:
1020 	nsim_dev_traps_exit(devlink);
1021 err_dummy_region_exit:
1022 	nsim_dev_dummy_region_exit(nsim_dev);
1023 err_fib_destroy:
1024 	nsim_fib_destroy(devlink, nsim_dev->fib_data);
1025 	return err;
1026 }
1027 
1028 int nsim_dev_probe(struct nsim_bus_dev *nsim_bus_dev)
1029 {
1030 	struct nsim_dev *nsim_dev;
1031 	struct devlink *devlink;
1032 	int err;
1033 
1034 	devlink = devlink_alloc(&nsim_dev_devlink_ops, sizeof(*nsim_dev));
1035 	if (!devlink)
1036 		return -ENOMEM;
1037 	devlink_net_set(devlink, nsim_bus_dev->initial_net);
1038 	nsim_dev = devlink_priv(devlink);
1039 	nsim_dev->nsim_bus_dev = nsim_bus_dev;
1040 	nsim_dev->switch_id.id_len = sizeof(nsim_dev->switch_id.id);
1041 	get_random_bytes(nsim_dev->switch_id.id, nsim_dev->switch_id.id_len);
1042 	INIT_LIST_HEAD(&nsim_dev->port_list);
1043 	mutex_init(&nsim_dev->port_list_lock);
1044 	nsim_dev->fw_update_status = true;
1045 	nsim_dev->max_macs = NSIM_DEV_MAX_MACS_DEFAULT;
1046 	nsim_dev->test1 = NSIM_DEV_TEST1_DEFAULT;
1047 	spin_lock_init(&nsim_dev->fa_cookie_lock);
1048 
1049 	dev_set_drvdata(&nsim_bus_dev->dev, nsim_dev);
1050 
1051 	err = nsim_dev_resources_register(devlink);
1052 	if (err)
1053 		goto err_devlink_free;
1054 
1055 	nsim_dev->fib_data = nsim_fib_create(devlink, NULL);
1056 	if (IS_ERR(nsim_dev->fib_data)) {
1057 		err = PTR_ERR(nsim_dev->fib_data);
1058 		goto err_resources_unregister;
1059 	}
1060 
1061 	err = devlink_register(devlink, &nsim_bus_dev->dev);
1062 	if (err)
1063 		goto err_fib_destroy;
1064 
1065 	err = devlink_params_register(devlink, nsim_devlink_params,
1066 				      ARRAY_SIZE(nsim_devlink_params));
1067 	if (err)
1068 		goto err_dl_unregister;
1069 	nsim_devlink_set_params_init_values(nsim_dev, devlink);
1070 
1071 	err = nsim_dev_dummy_region_init(nsim_dev, devlink);
1072 	if (err)
1073 		goto err_params_unregister;
1074 
1075 	err = nsim_dev_traps_init(devlink);
1076 	if (err)
1077 		goto err_dummy_region_exit;
1078 
1079 	err = nsim_dev_debugfs_init(nsim_dev);
1080 	if (err)
1081 		goto err_traps_exit;
1082 
1083 	err = nsim_dev_health_init(nsim_dev, devlink);
1084 	if (err)
1085 		goto err_debugfs_exit;
1086 
1087 	err = nsim_bpf_dev_init(nsim_dev);
1088 	if (err)
1089 		goto err_health_exit;
1090 
1091 	err = nsim_dev_port_add_all(nsim_dev, nsim_bus_dev->port_count);
1092 	if (err)
1093 		goto err_bpf_dev_exit;
1094 
1095 	devlink_params_publish(devlink);
1096 	devlink_reload_enable(devlink);
1097 	return 0;
1098 
1099 err_bpf_dev_exit:
1100 	nsim_bpf_dev_exit(nsim_dev);
1101 err_health_exit:
1102 	nsim_dev_health_exit(nsim_dev);
1103 err_debugfs_exit:
1104 	nsim_dev_debugfs_exit(nsim_dev);
1105 err_traps_exit:
1106 	nsim_dev_traps_exit(devlink);
1107 err_dummy_region_exit:
1108 	nsim_dev_dummy_region_exit(nsim_dev);
1109 err_params_unregister:
1110 	devlink_params_unregister(devlink, nsim_devlink_params,
1111 				  ARRAY_SIZE(nsim_devlink_params));
1112 err_dl_unregister:
1113 	devlink_unregister(devlink);
1114 err_fib_destroy:
1115 	nsim_fib_destroy(devlink, nsim_dev->fib_data);
1116 err_resources_unregister:
1117 	devlink_resources_unregister(devlink, NULL);
1118 err_devlink_free:
1119 	devlink_free(devlink);
1120 	return err;
1121 }
1122 
1123 static void nsim_dev_reload_destroy(struct nsim_dev *nsim_dev)
1124 {
1125 	struct devlink *devlink = priv_to_devlink(nsim_dev);
1126 
1127 	if (devlink_is_reload_failed(devlink))
1128 		return;
1129 	debugfs_remove(nsim_dev->take_snapshot);
1130 	nsim_dev_port_del_all(nsim_dev);
1131 	nsim_dev_health_exit(nsim_dev);
1132 	nsim_dev_traps_exit(devlink);
1133 	nsim_dev_dummy_region_exit(nsim_dev);
1134 	mutex_destroy(&nsim_dev->port_list_lock);
1135 	nsim_fib_destroy(devlink, nsim_dev->fib_data);
1136 }
1137 
1138 void nsim_dev_remove(struct nsim_bus_dev *nsim_bus_dev)
1139 {
1140 	struct nsim_dev *nsim_dev = dev_get_drvdata(&nsim_bus_dev->dev);
1141 	struct devlink *devlink = priv_to_devlink(nsim_dev);
1142 
1143 	devlink_reload_disable(devlink);
1144 
1145 	nsim_dev_reload_destroy(nsim_dev);
1146 
1147 	nsim_bpf_dev_exit(nsim_dev);
1148 	nsim_dev_debugfs_exit(nsim_dev);
1149 	devlink_params_unregister(devlink, nsim_devlink_params,
1150 				  ARRAY_SIZE(nsim_devlink_params));
1151 	devlink_unregister(devlink);
1152 	devlink_resources_unregister(devlink, NULL);
1153 	devlink_free(devlink);
1154 }
1155 
1156 static struct nsim_dev_port *
1157 __nsim_dev_port_lookup(struct nsim_dev *nsim_dev, unsigned int port_index)
1158 {
1159 	struct nsim_dev_port *nsim_dev_port;
1160 
1161 	list_for_each_entry(nsim_dev_port, &nsim_dev->port_list, list)
1162 		if (nsim_dev_port->port_index == port_index)
1163 			return nsim_dev_port;
1164 	return NULL;
1165 }
1166 
1167 int nsim_dev_port_add(struct nsim_bus_dev *nsim_bus_dev,
1168 		      unsigned int port_index)
1169 {
1170 	struct nsim_dev *nsim_dev = dev_get_drvdata(&nsim_bus_dev->dev);
1171 	int err;
1172 
1173 	mutex_lock(&nsim_dev->port_list_lock);
1174 	if (__nsim_dev_port_lookup(nsim_dev, port_index))
1175 		err = -EEXIST;
1176 	else
1177 		err = __nsim_dev_port_add(nsim_dev, port_index);
1178 	mutex_unlock(&nsim_dev->port_list_lock);
1179 	return err;
1180 }
1181 
1182 int nsim_dev_port_del(struct nsim_bus_dev *nsim_bus_dev,
1183 		      unsigned int port_index)
1184 {
1185 	struct nsim_dev *nsim_dev = dev_get_drvdata(&nsim_bus_dev->dev);
1186 	struct nsim_dev_port *nsim_dev_port;
1187 	int err = 0;
1188 
1189 	mutex_lock(&nsim_dev->port_list_lock);
1190 	nsim_dev_port = __nsim_dev_port_lookup(nsim_dev, port_index);
1191 	if (!nsim_dev_port)
1192 		err = -ENOENT;
1193 	else
1194 		__nsim_dev_port_del(nsim_dev_port);
1195 	mutex_unlock(&nsim_dev->port_list_lock);
1196 	return err;
1197 }
1198 
1199 int nsim_dev_init(void)
1200 {
1201 	nsim_dev_ddir = debugfs_create_dir(DRV_NAME, NULL);
1202 	return PTR_ERR_OR_ZERO(nsim_dev_ddir);
1203 }
1204 
1205 void nsim_dev_exit(void)
1206 {
1207 	debugfs_remove_recursive(nsim_dev_ddir);
1208 }
1209