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