1 /*-
2  * Copyright (c) 2010 Isilon Systems, Inc.
3  * Copyright (c) 2010 iX Systems, Inc.
4  * Copyright (c) 2010 Panasas, Inc.
5  * Copyright (c) 2013-2019 Mellanox Technologies, Ltd.
6  * All rights reserved.
7  * Copyright (c) 2020-2021 The FreeBSD Foundation
8  * Copyright (c) 2020-2022 Bjoern A. Zeeb
9  *
10  * Portions of this software were developed by Björn Zeeb
11  * under sponsorship from the FreeBSD Foundation.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice unmodified, this list of conditions, and the following
18  *    disclaimer.
19  * 2. Redistributions in binary form must reproduce the above copyright
20  *    notice, this list of conditions and the following disclaimer in the
21  *    documentation and/or other materials provided with the distribution.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  */
34 #ifndef	_LINUXKPI_LINUX_NETDEVICE_H
35 #define	_LINUXKPI_LINUX_NETDEVICE_H
36 
37 #include <linux/types.h>
38 #include <linux/netdev_features.h>
39 
40 #include <sys/param.h>
41 #include <sys/kernel.h>
42 #include <sys/lock.h>
43 #include <sys/mutex.h>
44 #include <sys/malloc.h>
45 #include <sys/queue.h>
46 #include <sys/socket.h>
47 #include <sys/taskqueue.h>
48 
49 #include <net/if_types.h>
50 #include <net/if.h>
51 #include <net/if_var.h>
52 #include <net/if_dl.h>
53 
54 #include <linux/kernel.h>
55 #include <linux/bitops.h>
56 #include <linux/list.h>
57 #include <linux/device.h>
58 #include <linux/net.h>
59 #include <linux/if_ether.h>
60 #include <linux/notifier.h>
61 #include <linux/random.h>
62 #include <linux/rcupdate.h>
63 
64 #ifdef VIMAGE
65 #define	init_net *vnet0
66 #else
67 #define	init_net *((struct vnet *)0)
68 #endif
69 
70 struct sk_buff;
71 struct net_device;
72 struct wireless_dev;		/* net/cfg80211.h */
73 
74 #define	MAX_ADDR_LEN		20
75 
76 #define	NET_NAME_UNKNOWN	0
77 
78 enum netdev_tx {
79 	NETDEV_TX_OK		= 0,
80 };
81 typedef	enum netdev_tx		netdev_tx_t;
82 
83 struct netdev_hw_addr {
84 	struct list_head	addr_list;
85 	uint8_t			addr[MAX_ADDR_LEN];
86 };
87 
88 struct netdev_hw_addr_list {
89 	struct list_head	addr_list;
90 	int			count;
91 };
92 
93 enum net_device_reg_state {
94 	NETREG_DUMMY		= 1,
95 	NETREG_REGISTERED,
96 };
97 
98 struct net_device_ops {
99 	int (*ndo_open)(struct net_device *);
100 	int (*ndo_stop)(struct net_device *);
101 	int (*ndo_set_mac_address)(struct net_device *,  void *);
102 	netdev_tx_t (*ndo_start_xmit)(struct sk_buff *, struct net_device *);
103 	void (*ndo_set_rx_mode)(struct net_device *);
104 };
105 
106 struct net_device {
107 	/* net_device fields seen publicly. */
108 	/* XXX can we later make some aliases to ifnet? */
109 	char				name[IFNAMSIZ];
110 	struct wireless_dev		*ieee80211_ptr;
111 	uint8_t				dev_addr[ETH_ALEN];
112 	struct netdev_hw_addr_list	mc;
113 	netdev_features_t		features;
114 	struct {
115 		unsigned long		multicast;
116 
117 		unsigned long		rx_bytes;
118 		unsigned long		rx_errors;
119 		unsigned long		rx_packets;
120 		unsigned long		tx_bytes;
121 		unsigned long		tx_dropped;
122 		unsigned long		tx_errors;
123 		unsigned long		tx_packets;
124 	} stats;
125 	enum net_device_reg_state	reg_state;
126 	const struct ethtool_ops	*ethtool_ops;
127 	const struct net_device_ops	*netdev_ops;
128 
129 	bool				needs_free_netdev;
130 	/* Not properly typed as-of now. */
131 	int	flags, type;
132 	int	name_assign_type, needed_headroom;
133 	int	threaded;
134 
135 	void (*priv_destructor)(struct net_device *);
136 
137 	/* net_device internal. */
138 	struct device			dev;
139 
140 	/*
141 	 * In case we delete the net_device we need to be able to clear all
142 	 * NAPI consumers.
143 	 */
144 	struct mtx			napi_mtx;
145 	TAILQ_HEAD(, napi_struct)	napi_head;
146 	struct taskqueue		*napi_tq;
147 
148 	/* Must stay last. */
149 	uint8_t				drv_priv[0] __aligned(CACHE_LINE_SIZE);
150 };
151 
152 #define	SET_NETDEV_DEV(_ndev, _dev)	(_ndev)->dev.parent = _dev;
153 
154 /* -------------------------------------------------------------------------- */
155 /* According to linux::ipoib_main.c. */
156 struct netdev_notifier_info {
157 	struct net_device	*dev;
158 	struct ifnet		*ifp;
159 };
160 
161 static inline struct net_device *
162 netdev_notifier_info_to_dev(struct netdev_notifier_info *ni)
163 {
164 	return (ni->dev);
165 }
166 
167 static inline struct ifnet *
168 netdev_notifier_info_to_ifp(struct netdev_notifier_info *ni)
169 {
170 	return (ni->ifp);
171 }
172 
173 int	register_netdevice_notifier(struct notifier_block *);
174 int	register_inetaddr_notifier(struct notifier_block *);
175 int	unregister_netdevice_notifier(struct notifier_block *);
176 int	unregister_inetaddr_notifier(struct notifier_block *);
177 
178 /* -------------------------------------------------------------------------- */
179 
180 #define	NAPI_POLL_WEIGHT			64	/* budget */
181 
182 /*
183  * There are drivers directly testing napi state bits, so we need to publicly
184  * expose them.  If you ask me, those accesses should be hid behind an
185  * inline function and the bit flags not be directly exposed.
186  */
187 enum napi_state_bits {
188 	/*
189 	 * Official Linux flags encountered.
190 	 */
191 	NAPI_STATE_SCHED = 1,
192 
193 	/*
194 	 * Our internal versions (for now).
195 	 */
196 	/* Do not schedule new things while we are waiting to clear things. */
197 	LKPI_NAPI_FLAG_DISABLE_PENDING = 0,
198 	/* To synchronise that only one poll is ever running. */
199 	LKPI_NAPI_FLAG_IS_SCHEDULED = 1,
200 	/* If trying to schedule while poll is running. Need to re-schedule. */
201 	LKPI_NAPI_FLAG_LOST_RACE_TRY_AGAIN = 2,
202 	/* When shutting down forcefully prevent anything from running task/poll. */
203 	LKPI_NAPI_FLAG_SHUTDOWN = 3,
204 };
205 
206 struct napi_struct {
207 	TAILQ_ENTRY(napi_struct)	entry;
208 
209 	struct list_head	rx_list;
210 	struct net_device	*dev;
211 	int			(*poll)(struct napi_struct *, int);
212 	int			budget;
213 	int			rx_count;
214 
215 
216 	/*
217 	 * These flags mostly need to be checked/changed atomically
218 	 * (multiple together in some cases).
219 	 */
220 	volatile unsigned long	state;
221 
222 	/* FreeBSD internal. */
223 	/* Use task for now, so we can easily switch between direct and task. */
224 	struct task		napi_task;
225 };
226 
227 void linuxkpi_init_dummy_netdev(struct net_device *);
228 void linuxkpi_netif_napi_add(struct net_device *, struct napi_struct *,
229     int(*napi_poll)(struct napi_struct *, int));
230 void linuxkpi_netif_napi_del(struct napi_struct *);
231 bool linuxkpi_napi_schedule_prep(struct napi_struct *);
232 void linuxkpi___napi_schedule(struct napi_struct *);
233 void linuxkpi_napi_schedule(struct napi_struct *);
234 void linuxkpi_napi_reschedule(struct napi_struct *);
235 bool linuxkpi_napi_complete_done(struct napi_struct *, int);
236 bool linuxkpi_napi_complete(struct napi_struct *);
237 void linuxkpi_napi_disable(struct napi_struct *);
238 void linuxkpi_napi_enable(struct napi_struct *);
239 void linuxkpi_napi_synchronize(struct napi_struct *);
240 
241 #define	init_dummy_netdev(_n)						\
242 	linuxkpi_init_dummy_netdev(_n)
243 #define	netif_napi_add(_nd, _ns, _p)					\
244 	linuxkpi_netif_napi_add(_nd, _ns, _p)
245 #define	netif_napi_del(_n)						\
246 	linuxkpi_netif_napi_del(_n)
247 #define	napi_schedule_prep(_n)						\
248 	linuxkpi_napi_schedule_prep(_n)
249 #define	__napi_schedule(_n)						\
250 	linuxkpi___napi_schedule(_n)
251 #define	napi_schedule(_n)						\
252 	linuxkpi_napi_schedule(_n)
253 #define	napi_reschedule(_n)						\
254 	linuxkpi_napi_reschedule(_n)
255 #define	napi_complete_done(_n, _r)					\
256 	linuxkpi_napi_complete_done(_n, _r)
257 #define	napi_complete(_n)						\
258 	linuxkpi_napi_complete(_n)
259 #define	napi_disable(_n)						\
260 	linuxkpi_napi_disable(_n)
261 #define	napi_enable(_n)							\
262 	linuxkpi_napi_enable(_n)
263 #define	napi_synchronize(_n)						\
264 	linuxkpi_napi_synchronize(_n)
265 
266 
267 static inline void
268 netif_napi_add_tx(struct net_device *dev, struct napi_struct *napi,
269     int(*napi_poll)(struct napi_struct *, int))
270 {
271 
272 	netif_napi_add(dev, napi, napi_poll);
273 }
274 
275 /* -------------------------------------------------------------------------- */
276 
277 static inline void
278 netdev_rss_key_fill(uint32_t *buf, size_t len)
279 {
280 
281 	/*
282 	 * Remembering from a previous life there was discussions on what is
283 	 * a good RSS hash key.  See end of rss_init() in net/rss_config.c.
284 	 * iwlwifi is looking for a 10byte "secret" so stay with random for now.
285 	 */
286 	get_random_bytes(buf, len);
287 }
288 
289 static inline int
290 netdev_hw_addr_list_count(struct netdev_hw_addr_list *list)
291 {
292 
293 	return (list->count);
294 }
295 
296 static inline int
297 netdev_mc_count(struct net_device *ndev)
298 {
299 
300 	return (netdev_hw_addr_list_count(&ndev->mc));
301 }
302 
303 #define	netdev_hw_addr_list_for_each(_addr, _list)			\
304 	list_for_each_entry((_addr), &(_list)->addr_list, addr_list)
305 
306 #define	netdev_for_each_mc_addr(na, ndev)				\
307 	netdev_hw_addr_list_for_each(na, &(ndev)->mc)
308 
309 static __inline void
310 synchronize_net(void)
311 {
312 
313 	/* We probably cannot do that unconditionally at some point anymore. */
314 	synchronize_rcu();
315 }
316 
317 static __inline void
318 netif_receive_skb_list(struct list_head *head)
319 {
320 
321 	pr_debug("%s: TODO\n", __func__);
322 }
323 
324 static __inline int
325 napi_gro_receive(struct napi_struct *napi, struct sk_buff *skb)
326 {
327 
328 	pr_debug("%s: TODO\n", __func__);
329 	return (-1);
330 }
331 
332 static __inline void
333 ether_setup(struct net_device *ndev)
334 {
335 
336 	pr_debug("%s: TODO\n", __func__);
337 }
338 
339 static __inline void
340 dev_net_set(struct net_device *ndev, void *p)
341 {
342 
343 	pr_debug("%s: TODO\n", __func__);
344 }
345 
346 static __inline int
347 dev_set_threaded(struct net_device *ndev, bool threaded)
348 {
349 
350 	pr_debug("%s: TODO\n", __func__);
351 	return (-ENODEV);
352 }
353 
354 /* -------------------------------------------------------------------------- */
355 
356 static __inline bool
357 netif_carrier_ok(struct net_device *ndev)
358 {
359 	pr_debug("%s: TODO\n", __func__);
360 	return (false);
361 }
362 
363 static __inline void
364 netif_carrier_off(struct net_device *ndev)
365 {
366 	pr_debug("%s: TODO\n", __func__);
367 }
368 
369 static __inline void
370 netif_carrier_on(struct net_device *ndev)
371 {
372 	pr_debug("%s: TODO\n", __func__);
373 }
374 
375 /* -------------------------------------------------------------------------- */
376 
377 static __inline bool
378 netif_queue_stopped(struct net_device *ndev)
379 {
380 	pr_debug("%s: TODO\n", __func__);
381 	return (false);
382 }
383 
384 static __inline void
385 netif_stop_queue(struct net_device *ndev)
386 {
387 	pr_debug("%s: TODO\n", __func__);
388 }
389 
390 static __inline void
391 netif_wake_queue(struct net_device *ndev)
392 {
393 	pr_debug("%s: TODO\n", __func__);
394 }
395 
396 /* -------------------------------------------------------------------------- */
397 
398 static __inline int
399 register_netdevice(struct net_device *ndev)
400 {
401 
402 	/* assert rtnl_locked? */
403 	pr_debug("%s: TODO\n", __func__);
404 	return (0);
405 }
406 
407 static __inline int
408 register_netdev(struct net_device *ndev)
409 {
410 	int error;
411 
412 	/* lock */
413 	error = register_netdevice(ndev);
414 	/* unlock */
415 	pr_debug("%s: TODO\n", __func__);
416 	return (error);
417 }
418 
419 static __inline void
420 unregister_netdev(struct net_device *ndev)
421 {
422 	pr_debug("%s: TODO\n", __func__);
423 }
424 
425 static __inline void
426 unregister_netdevice(struct net_device *ndev)
427 {
428 	pr_debug("%s: TODO\n", __func__);
429 }
430 
431 /* -------------------------------------------------------------------------- */
432 
433 static __inline void
434 netif_rx(struct sk_buff *skb)
435 {
436 	pr_debug("%s: TODO\n", __func__);
437 }
438 
439 static __inline void
440 netif_rx_ni(struct sk_buff *skb)
441 {
442 	pr_debug("%s: TODO\n", __func__);
443 }
444 
445 /* -------------------------------------------------------------------------- */
446 
447 struct net_device *linuxkpi_alloc_netdev(size_t, const char *, uint32_t,
448     void(*)(struct net_device *));
449 void linuxkpi_free_netdev(struct net_device *);
450 
451 #define	alloc_netdev(_l, _n, _f, _func)						\
452 	linuxkpi_alloc_netdev(_l, _n, _f, _func)
453 #define	free_netdev(_n)								\
454 	linuxkpi_free_netdev(_n)
455 
456 static inline void *
457 netdev_priv(const struct net_device *ndev)
458 {
459 
460 	return (__DECONST(void *, ndev->drv_priv));
461 }
462 
463 /* -------------------------------------------------------------------------- */
464 /* This is really rtnetlink and probably belongs elsewhere. */
465 
466 #define	rtnl_lock()		do { } while(0)
467 #define	rtnl_unlock()		do { } while(0)
468 #define	rcu_dereference_rtnl(x)	READ_ONCE(x)
469 
470 #endif	/* _LINUXKPI_LINUX_NETDEVICE_H */
471