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