1 /** @file
2  @brief Ethernet
3 
4  This is not to be included by the application.
5  */
6 
7 /*
8  * Copyright (c) 2016 Intel Corporation
9  *
10  * SPDX-License-Identifier: Apache-2.0
11  */
12 
13 #ifndef ZEPHYR_INCLUDE_NET_ETHERNET_H_
14 #define ZEPHYR_INCLUDE_NET_ETHERNET_H_
15 
16 #include <zephyr/kernel.h>
17 #include <zephyr/types.h>
18 #include <stdbool.h>
19 #include <zephyr/sys/atomic.h>
20 
21 #include <zephyr/net/net_ip.h>
22 #include <zephyr/net/net_pkt.h>
23 #include <zephyr/net/lldp.h>
24 #include <zephyr/sys/util.h>
25 #include <zephyr/net/net_if.h>
26 #include <zephyr/net/ethernet_vlan.h>
27 #include <zephyr/net/ptp_time.h>
28 
29 #if defined(CONFIG_NET_DSA)
30 #include <zephyr/net/dsa.h>
31 #endif
32 
33 #if defined(CONFIG_NET_ETHERNET_BRIDGE)
34 #include <zephyr/net/ethernet_bridge.h>
35 #endif
36 
37 #ifdef __cplusplus
38 extern "C" {
39 #endif
40 
41 /**
42  * @brief Ethernet support functions
43  * @defgroup ethernet Ethernet Support Functions
44  * @since 1.0
45  * @version 0.8.0
46  * @ingroup networking
47  * @{
48  */
49 
50 #define NET_ETH_ADDR_LEN		6U /**< Ethernet MAC address length */
51 
52 /** Ethernet address */
53 struct net_eth_addr {
54 	uint8_t addr[NET_ETH_ADDR_LEN]; /**< Buffer storing the address */
55 };
56 
57 /** @cond INTERNAL_HIDDEN */
58 
59 #define NET_ETH_HDR(pkt) ((struct net_eth_hdr *)net_pkt_data(pkt))
60 
61 #define NET_ETH_PTYPE_CAN		0x000C /* CAN: Controller Area Network */
62 #define NET_ETH_PTYPE_CANFD		0x000D /* CANFD: CAN flexible data rate*/
63 #define NET_ETH_PTYPE_HDLC		0x0019 /* HDLC frames (like in PPP) */
64 #define NET_ETH_PTYPE_ARP		0x0806
65 #define NET_ETH_PTYPE_IP		0x0800
66 #define NET_ETH_PTYPE_TSN		0x22f0 /* TSN (IEEE 1722) packet */
67 #define NET_ETH_PTYPE_IPV6		0x86dd
68 #define NET_ETH_PTYPE_VLAN		0x8100
69 #define NET_ETH_PTYPE_PTP		0x88f7
70 #define NET_ETH_PTYPE_LLDP		0x88cc
71 #define NET_ETH_PTYPE_ALL               0x0003 /* from linux/if_ether.h */
72 #define NET_ETH_PTYPE_ECAT		0x88a4
73 #define NET_ETH_PTYPE_EAPOL		0x888e
74 #define NET_ETH_PTYPE_IEEE802154	0x00F6 /* from linux/if_ether.h: IEEE802.15.4 frame */
75 
76 #if !defined(ETH_P_ALL)
77 #define ETH_P_ALL	NET_ETH_PTYPE_ALL
78 #endif
79 #if !defined(ETH_P_IP)
80 #define ETH_P_IP	NET_ETH_PTYPE_IP
81 #endif
82 #if !defined(ETH_P_ARP)
83 #define ETH_P_ARP	NET_ETH_PTYPE_ARP
84 #endif
85 #if !defined(ETH_P_IPV6)
86 #define ETH_P_IPV6	NET_ETH_PTYPE_IPV6
87 #endif
88 #if !defined(ETH_P_8021Q)
89 #define ETH_P_8021Q	NET_ETH_PTYPE_VLAN
90 #endif
91 #if !defined(ETH_P_TSN)
92 #define ETH_P_TSN	NET_ETH_PTYPE_TSN
93 #endif
94 #if !defined(ETH_P_ECAT)
95 #define  ETH_P_ECAT	NET_ETH_PTYPE_ECAT
96 #endif
97 #if !defined(ETH_P_EAPOL)
98 #define ETH_P_EAPOL	NET_ETH_PTYPE_EAPOL
99 #endif
100 #if !defined(ETH_P_IEEE802154)
101 #define  ETH_P_IEEE802154 NET_ETH_PTYPE_IEEE802154
102 #endif
103 #if !defined(ETH_P_CAN)
104 #define ETH_P_CAN	NET_ETH_PTYPE_CAN
105 #endif
106 #if !defined(ETH_P_CANFD)
107 #define ETH_P_CANFD	NET_ETH_PTYPE_CANFD
108 #endif
109 #if !defined(ETH_P_HDLC)
110 #define ETH_P_HDLC	NET_ETH_PTYPE_HDLC
111 #endif
112 
113 /** @endcond */
114 
115 #define NET_ETH_MINIMAL_FRAME_SIZE	60   /**< Minimum Ethernet frame size */
116 #define NET_ETH_MTU			1500 /**< Ethernet MTU size */
117 
118 /** @cond INTERNAL_HIDDEN */
119 
120 #if defined(CONFIG_NET_VLAN)
121 #define _NET_ETH_MAX_HDR_SIZE		(sizeof(struct net_eth_vlan_hdr))
122 #else
123 #define _NET_ETH_MAX_HDR_SIZE		(sizeof(struct net_eth_hdr))
124 #endif
125 
126 #define _NET_ETH_MAX_FRAME_SIZE	(NET_ETH_MTU + _NET_ETH_MAX_HDR_SIZE)
127 /*
128  * Extend the max frame size for DSA (KSZ8794) by one byte (to 1519) to
129  * store tail tag.
130  */
131 #if defined(CONFIG_NET_DSA)
132 #define NET_ETH_MAX_FRAME_SIZE (_NET_ETH_MAX_FRAME_SIZE + DSA_TAG_SIZE)
133 #define NET_ETH_MAX_HDR_SIZE (_NET_ETH_MAX_HDR_SIZE + DSA_TAG_SIZE)
134 #else
135 #define NET_ETH_MAX_FRAME_SIZE (_NET_ETH_MAX_FRAME_SIZE)
136 #define NET_ETH_MAX_HDR_SIZE (_NET_ETH_MAX_HDR_SIZE)
137 #endif
138 
139 #define NET_ETH_VLAN_HDR_SIZE	4
140 
141 /** @endcond */
142 
143 /** @brief Ethernet hardware capabilities */
144 enum ethernet_hw_caps {
145 	/** TX Checksum offloading supported for all of IPv4, UDP, TCP */
146 	ETHERNET_HW_TX_CHKSUM_OFFLOAD	= BIT(0),
147 
148 	/** RX Checksum offloading supported for all of IPv4, UDP, TCP */
149 	ETHERNET_HW_RX_CHKSUM_OFFLOAD	= BIT(1),
150 
151 	/** VLAN supported */
152 	ETHERNET_HW_VLAN		= BIT(2),
153 
154 	/** Enabling/disabling auto negotiation supported */
155 	ETHERNET_AUTO_NEGOTIATION_SET	= BIT(3),
156 
157 	/** 10 Mbits link supported */
158 	ETHERNET_LINK_10BASE_T		= BIT(4),
159 
160 	/** 100 Mbits link supported */
161 	ETHERNET_LINK_100BASE_T		= BIT(5),
162 
163 	/** 1 Gbits link supported */
164 	ETHERNET_LINK_1000BASE_T	= BIT(6),
165 
166 	/** Changing duplex (half/full) supported */
167 	ETHERNET_DUPLEX_SET		= BIT(7),
168 
169 	/** IEEE 802.1AS (gPTP) clock supported */
170 	ETHERNET_PTP			= BIT(8),
171 
172 	/** IEEE 802.1Qav (credit-based shaping) supported */
173 	ETHERNET_QAV			= BIT(9),
174 
175 	/** Promiscuous mode supported */
176 	ETHERNET_PROMISC_MODE		= BIT(10),
177 
178 	/** Priority queues available */
179 	ETHERNET_PRIORITY_QUEUES	= BIT(11),
180 
181 	/** MAC address filtering supported */
182 	ETHERNET_HW_FILTERING		= BIT(12),
183 
184 	/** Link Layer Discovery Protocol supported */
185 	ETHERNET_LLDP			= BIT(13),
186 
187 	/** VLAN Tag stripping */
188 	ETHERNET_HW_VLAN_TAG_STRIP	= BIT(14),
189 
190 	/** DSA switch slave port */
191 	ETHERNET_DSA_SLAVE_PORT		= BIT(15),
192 
193 	/** DSA switch master port */
194 	ETHERNET_DSA_MASTER_PORT	= BIT(16),
195 
196 	/** IEEE 802.1Qbv (scheduled traffic) supported */
197 	ETHERNET_QBV			= BIT(17),
198 
199 	/** IEEE 802.1Qbu (frame preemption) supported */
200 	ETHERNET_QBU			= BIT(18),
201 
202 	/** TXTIME supported */
203 	ETHERNET_TXTIME			= BIT(19),
204 
205 	/** TX-Injection supported */
206 	ETHERNET_TXINJECTION_MODE	= BIT(20),
207 
208 	/** 2.5 Gbits link supported */
209 	ETHERNET_LINK_2500BASE_T	= BIT(21),
210 
211 	/** 5 Gbits link supported */
212 	ETHERNET_LINK_5000BASE_T	= BIT(22),
213 };
214 
215 /** @cond INTERNAL_HIDDEN */
216 
217 enum ethernet_config_type {
218 	ETHERNET_CONFIG_TYPE_AUTO_NEG,
219 	ETHERNET_CONFIG_TYPE_LINK,
220 	ETHERNET_CONFIG_TYPE_DUPLEX,
221 	ETHERNET_CONFIG_TYPE_MAC_ADDRESS,
222 	ETHERNET_CONFIG_TYPE_QAV_PARAM,
223 	ETHERNET_CONFIG_TYPE_QBV_PARAM,
224 	ETHERNET_CONFIG_TYPE_QBU_PARAM,
225 	ETHERNET_CONFIG_TYPE_TXTIME_PARAM,
226 	ETHERNET_CONFIG_TYPE_PROMISC_MODE,
227 	ETHERNET_CONFIG_TYPE_PRIORITY_QUEUES_NUM,
228 	ETHERNET_CONFIG_TYPE_FILTER,
229 	ETHERNET_CONFIG_TYPE_PORTS_NUM,
230 	ETHERNET_CONFIG_TYPE_T1S_PARAM,
231 	ETHERNET_CONFIG_TYPE_TXINJECTION_MODE,
232 	ETHERNET_CONFIG_TYPE_RX_CHECKSUM_SUPPORT,
233 	ETHERNET_CONFIG_TYPE_TX_CHECKSUM_SUPPORT
234 };
235 
236 enum ethernet_qav_param_type {
237 	ETHERNET_QAV_PARAM_TYPE_DELTA_BANDWIDTH,
238 	ETHERNET_QAV_PARAM_TYPE_IDLE_SLOPE,
239 	ETHERNET_QAV_PARAM_TYPE_OPER_IDLE_SLOPE,
240 	ETHERNET_QAV_PARAM_TYPE_TRAFFIC_CLASS,
241 	ETHERNET_QAV_PARAM_TYPE_STATUS,
242 };
243 
244 enum ethernet_t1s_param_type {
245 	ETHERNET_T1S_PARAM_TYPE_PLCA_CONFIG,
246 };
247 
248 /** @endcond */
249 
250 /** Ethernet T1S specific parameters */
251 struct ethernet_t1s_param {
252 	/** Type of T1S parameter */
253 	enum ethernet_t1s_param_type type;
254 	union {
255 		/**
256 		 * PLCA is the Physical Layer (PHY) Collision
257 		 * Avoidance technique employed with multidrop
258 		 * 10Base-T1S standard.
259 		 *
260 		 * The PLCA parameters are described in standard [1]
261 		 * as registers in memory map 4 (MMS = 4) (point 9.6).
262 		 *
263 		 * IDVER	(PLCA ID Version)
264 		 * CTRL0	(PLCA Control 0)
265 		 * CTRL1	(PLCA Control 1)
266 		 * STATUS	(PLCA Status)
267 		 * TOTMR	(PLCA TO Control)
268 		 * BURST	(PLCA Burst Control)
269 		 *
270 		 * Those registers are implemented by each OA TC6
271 		 * compliant vendor (like for e.g. LAN865x - e.g. [2]).
272 		 *
273 		 * Documents:
274 		 * [1] - "OPEN Alliance 10BASE-T1x MAC-PHY Serial
275 		 *       Interface" (ver. 1.1)
276 		 * [2] - "DS60001734C" - LAN865x data sheet
277 		 */
278 		struct {
279 			/** T1S PLCA enabled */
280 			bool enable;
281 			/** T1S PLCA node id range: 0 to 254 */
282 			uint8_t node_id;
283 			/** T1S PLCA node count range: 1 to 255 */
284 			uint8_t node_count;
285 			/** T1S PLCA burst count range: 0x0 to 0xFF */
286 			uint8_t burst_count;
287 			/** T1S PLCA burst timer */
288 			uint8_t burst_timer;
289 			/** T1S PLCA TO value */
290 			uint8_t to_timer;
291 		} plca;
292 	};
293 };
294 
295 /** Ethernet Qav specific parameters */
296 struct ethernet_qav_param {
297 	/** ID of the priority queue to use */
298 	int queue_id;
299 	/** Type of Qav parameter */
300 	enum ethernet_qav_param_type type;
301 	union {
302 		/** True if Qav is enabled for queue */
303 		bool enabled;
304 		/** Delta Bandwidth (percentage of bandwidth) */
305 		unsigned int delta_bandwidth;
306 		/** Idle Slope (bits per second) */
307 		unsigned int idle_slope;
308 		/** Oper Idle Slope (bits per second) */
309 		unsigned int oper_idle_slope;
310 		/** Traffic class the queue is bound to */
311 		unsigned int traffic_class;
312 	};
313 };
314 
315 /** @cond INTERNAL_HIDDEN */
316 
317 enum ethernet_qbv_param_type {
318 	ETHERNET_QBV_PARAM_TYPE_STATUS,
319 	ETHERNET_QBV_PARAM_TYPE_GATE_CONTROL_LIST,
320 	ETHERNET_QBV_PARAM_TYPE_GATE_CONTROL_LIST_LEN,
321 	ETHERNET_QBV_PARAM_TYPE_TIME,
322 };
323 
324 enum ethernet_qbv_state_type {
325 	ETHERNET_QBV_STATE_TYPE_ADMIN,
326 	ETHERNET_QBV_STATE_TYPE_OPER,
327 };
328 
329 enum ethernet_gate_state_operation {
330 	ETHERNET_SET_GATE_STATE,
331 	ETHERNET_SET_AND_HOLD_MAC_STATE,
332 	ETHERNET_SET_AND_RELEASE_MAC_STATE,
333 };
334 
335 /** @endcond */
336 
337 /** Ethernet Qbv specific parameters */
338 struct ethernet_qbv_param {
339 	/** Port id */
340 	int port_id;
341 	/** Type of Qbv parameter */
342 	enum ethernet_qbv_param_type type;
343 	/** What state (Admin/Oper) parameters are these */
344 	enum ethernet_qbv_state_type state;
345 	union {
346 		/** True if Qbv is enabled or not */
347 		bool enabled;
348 
349 		/** Gate control information */
350 		struct {
351 			/** True = open, False = closed */
352 			bool gate_status[NET_TC_TX_COUNT];
353 
354 			/** GateState operation */
355 			enum ethernet_gate_state_operation operation;
356 
357 			/** Time interval ticks (nanoseconds) */
358 			uint32_t time_interval;
359 
360 			/** Gate control list row */
361 			uint16_t row;
362 		} gate_control;
363 
364 		/** Number of entries in gate control list */
365 		uint32_t gate_control_list_len;
366 
367 		/**
368 		 * The time values are set in one go when type is set to
369 		 * ETHERNET_QBV_PARAM_TYPE_TIME
370 		 */
371 		struct {
372 			/** Base time */
373 			struct net_ptp_extended_time base_time;
374 
375 			/** Cycle time */
376 			struct net_ptp_time cycle_time;
377 
378 			/** Extension time (nanoseconds) */
379 			uint32_t extension_time;
380 		};
381 	};
382 };
383 
384 /** @cond INTERNAL_HIDDEN */
385 
386 enum ethernet_qbu_param_type {
387 	ETHERNET_QBU_PARAM_TYPE_STATUS,
388 	ETHERNET_QBU_PARAM_TYPE_RELEASE_ADVANCE,
389 	ETHERNET_QBU_PARAM_TYPE_HOLD_ADVANCE,
390 	ETHERNET_QBU_PARAM_TYPE_PREEMPTION_STATUS_TABLE,
391 
392 	/* Some preemption settings are from Qbr spec. */
393 	ETHERNET_QBR_PARAM_TYPE_LINK_PARTNER_STATUS,
394 	ETHERNET_QBR_PARAM_TYPE_ADDITIONAL_FRAGMENT_SIZE,
395 };
396 
397 enum ethernet_qbu_preempt_status {
398 	ETHERNET_QBU_STATUS_EXPRESS,
399 	ETHERNET_QBU_STATUS_PREEMPTABLE
400 } __packed;
401 
402 /** @endcond */
403 
404 /** Ethernet Qbu specific parameters */
405 struct ethernet_qbu_param {
406 	/** Port id */
407 	int port_id;
408 	/** Type of Qbu parameter */
409 	enum ethernet_qbu_param_type type;
410 	union {
411 		/** Hold advance (nanoseconds) */
412 		uint32_t hold_advance;
413 
414 		/** Release advance (nanoseconds) */
415 		uint32_t release_advance;
416 
417 		/** sequence of framePreemptionAdminStatus values */
418 		enum ethernet_qbu_preempt_status
419 				frame_preempt_statuses[NET_TC_TX_COUNT];
420 
421 		/** True if Qbu is enabled or not */
422 		bool enabled;
423 
424 		/** Link partner status (from Qbr) */
425 		bool link_partner_status;
426 
427 		/**
428 		 * Additional fragment size (from Qbr). The minimum non-final
429 		 * fragment size is (additional_fragment_size + 1) * 64 octets
430 		 */
431 		uint8_t additional_fragment_size : 2;
432 	};
433 };
434 
435 /** @cond INTERNAL_HIDDEN */
436 
437 enum ethernet_filter_type {
438 	ETHERNET_FILTER_TYPE_SRC_MAC_ADDRESS,
439 	ETHERNET_FILTER_TYPE_DST_MAC_ADDRESS,
440 };
441 
442 /** @endcond */
443 
444 /** Types of Ethernet L2 */
445 enum ethernet_if_types {
446 	/** IEEE 802.3 Ethernet (default) */
447 	L2_ETH_IF_TYPE_ETHERNET,
448 
449 	/** IEEE 802.11 Wi-Fi*/
450 	L2_ETH_IF_TYPE_WIFI,
451 } __packed;
452 
453 /** Ethernet filter description */
454 struct ethernet_filter {
455 	/** Type of filter */
456 	enum ethernet_filter_type type;
457 	/** MAC address to filter */
458 	struct net_eth_addr mac_address;
459 	/** Set (true) or unset (false) the filter */
460 	bool set;
461 };
462 
463 /** @cond INTERNAL_HIDDEN */
464 
465 enum ethernet_txtime_param_type {
466 	ETHERNET_TXTIME_PARAM_TYPE_ENABLE_QUEUES,
467 };
468 
469 /** @endcond */
470 
471 /** Ethernet TXTIME specific parameters */
472 struct ethernet_txtime_param {
473 	/** Type of TXTIME parameter */
474 	enum ethernet_txtime_param_type type;
475 	/** Queue number for configuring TXTIME */
476 	int queue_id;
477 	/** Enable or disable TXTIME per queue */
478 	bool enable_txtime;
479 };
480 
481 /** Protocols that are supported by checksum offloading */
482 enum ethernet_checksum_support {
483 	/** Device does not support any L3/L4 checksum offloading */
484 	ETHERNET_CHECKSUM_SUPPORT_NONE			= NET_IF_CHECKSUM_NONE_BIT,
485 	/** Device supports checksum offloading for the IPv4 header */
486 	ETHERNET_CHECKSUM_SUPPORT_IPV4_HEADER		= NET_IF_CHECKSUM_IPV4_HEADER_BIT,
487 	/** Device supports checksum offloading for ICMPv4 payload (implies IPv4 header) */
488 	ETHERNET_CHECKSUM_SUPPORT_IPV4_ICMP		= NET_IF_CHECKSUM_IPV4_ICMP_BIT,
489 	/** Device supports checksum offloading for the IPv6 header */
490 	ETHERNET_CHECKSUM_SUPPORT_IPV6_HEADER		= NET_IF_CHECKSUM_IPV6_HEADER_BIT,
491 	/** Device supports checksum offloading for ICMPv6 payload (implies IPv6 header) */
492 	ETHERNET_CHECKSUM_SUPPORT_IPV6_ICMP		= NET_IF_CHECKSUM_IPV6_ICMP_BIT,
493 	/** Device supports TCP checksum offloading for all supported IP protocols */
494 	ETHERNET_CHECKSUM_SUPPORT_TCP			= NET_IF_CHECKSUM_TCP_BIT,
495 	/** Device supports UDP checksum offloading for all supported IP protocols */
496 	ETHERNET_CHECKSUM_SUPPORT_UDP			= NET_IF_CHECKSUM_UDP_BIT,
497 };
498 
499 /** @cond INTERNAL_HIDDEN */
500 
501 struct ethernet_config {
502 	union {
503 		bool auto_negotiation;
504 		bool full_duplex;
505 		bool promisc_mode;
506 		bool txinjection_mode;
507 
508 		struct {
509 			bool link_10bt;
510 			bool link_100bt;
511 			bool link_1000bt;
512 		} l;
513 
514 		struct net_eth_addr mac_address;
515 
516 		struct ethernet_t1s_param t1s_param;
517 		struct ethernet_qav_param qav_param;
518 		struct ethernet_qbv_param qbv_param;
519 		struct ethernet_qbu_param qbu_param;
520 		struct ethernet_txtime_param txtime_param;
521 
522 		int priority_queues_num;
523 		int ports_num;
524 
525 		enum ethernet_checksum_support chksum_support;
526 
527 		struct ethernet_filter filter;
528 	};
529 };
530 
531 /** @endcond */
532 
533 /** Ethernet L2 API operations. */
534 struct ethernet_api {
535 	/**
536 	 * The net_if_api must be placed in first position in this
537 	 * struct so that we are compatible with network interface API.
538 	 */
539 	struct net_if_api iface_api;
540 
541 	/** Collect optional ethernet specific statistics. This pointer
542 	 * should be set by driver if statistics needs to be collected
543 	 * for that driver.
544 	 */
545 #if defined(CONFIG_NET_STATISTICS_ETHERNET)
546 	struct net_stats_eth *(*get_stats)(const struct device *dev);
547 #endif
548 
549 	/** Start the device */
550 	int (*start)(const struct device *dev);
551 
552 	/** Stop the device */
553 	int (*stop)(const struct device *dev);
554 
555 	/** Get the device capabilities */
556 	enum ethernet_hw_caps (*get_capabilities)(const struct device *dev);
557 
558 	/** Set specific hardware configuration */
559 	int (*set_config)(const struct device *dev,
560 			  enum ethernet_config_type type,
561 			  const struct ethernet_config *config);
562 
563 	/** Get hardware specific configuration */
564 	int (*get_config)(const struct device *dev,
565 			  enum ethernet_config_type type,
566 			  struct ethernet_config *config);
567 
568 	/** The IP stack will call this function when a VLAN tag is enabled
569 	 * or disabled. If enable is set to true, then the VLAN tag was added,
570 	 * if it is false then the tag was removed. The driver can utilize
571 	 * this information if needed.
572 	 */
573 #if defined(CONFIG_NET_VLAN)
574 	int (*vlan_setup)(const struct device *dev, struct net_if *iface,
575 			  uint16_t tag, bool enable);
576 #endif /* CONFIG_NET_VLAN */
577 
578 	/** Return ptp_clock device that is tied to this ethernet device */
579 #if defined(CONFIG_PTP_CLOCK)
580 	const struct device *(*get_ptp_clock)(const struct device *dev);
581 #endif /* CONFIG_PTP_CLOCK */
582 
583 	/** Return PHY device that is tied to this ethernet device */
584 	const struct device *(*get_phy)(const struct device *dev);
585 
586 	/** Send a network packet */
587 	int (*send)(const struct device *dev, struct net_pkt *pkt);
588 };
589 
590 /** @cond INTERNAL_HIDDEN */
591 
592 /* Make sure that the network interface API is properly setup inside
593  * Ethernet API struct (it is the first one).
594  */
595 BUILD_ASSERT(offsetof(struct ethernet_api, iface_api) == 0);
596 
597 struct net_eth_hdr {
598 	struct net_eth_addr dst;
599 	struct net_eth_addr src;
600 	uint16_t type;
601 } __packed;
602 
603 struct ethernet_vlan {
604 	/** Network interface that has VLAN enabled */
605 	struct net_if *iface;
606 
607 	/** VLAN tag */
608 	uint16_t tag;
609 };
610 
611 #if defined(CONFIG_NET_VLAN_COUNT)
612 #define NET_VLAN_MAX_COUNT CONFIG_NET_VLAN_COUNT
613 #else
614 /* Even thou there are no VLAN support, the minimum count must be set to 1.
615  */
616 #define NET_VLAN_MAX_COUNT 1
617 #endif
618 
619 /** @endcond */
620 
621 /** Ethernet LLDP specific parameters */
622 struct ethernet_lldp {
623 	/** Used for track timers */
624 	sys_snode_t node;
625 
626 	/** LLDP Data Unit mandatory TLVs for the interface. */
627 	const struct net_lldpdu *lldpdu;
628 
629 	/** LLDP Data Unit optional TLVs for the interface */
630 	const uint8_t *optional_du;
631 
632 	/** Length of the optional Data Unit TLVs */
633 	size_t optional_len;
634 
635 	/** Network interface that has LLDP supported. */
636 	struct net_if *iface;
637 
638 	/** LLDP TX timer start time */
639 	int64_t tx_timer_start;
640 
641 	/** LLDP TX timeout */
642 	uint32_t tx_timer_timeout;
643 
644 	/** LLDP RX callback function */
645 	net_lldp_recv_cb_t cb;
646 };
647 
648 /** @cond INTERNAL_HIDDEN */
649 
650 enum ethernet_flags {
651 	ETH_CARRIER_UP,
652 };
653 
654 /** Ethernet L2 context that is needed for VLAN */
655 struct ethernet_context {
656 	/** Flags representing ethernet state, which are accessed from multiple
657 	 * threads.
658 	 */
659 	atomic_t flags;
660 
661 #if defined(CONFIG_NET_ETHERNET_BRIDGE)
662 	struct net_if *bridge;
663 #endif
664 
665 	/** Carrier ON/OFF handler worker. This is used to create
666 	 * network interface UP/DOWN event when ethernet L2 driver
667 	 * notices carrier ON/OFF situation. We must not create another
668 	 * network management event from inside management handler thus
669 	 * we use worker thread to trigger the UP/DOWN event.
670 	 */
671 	struct k_work carrier_work;
672 
673 	/** Network interface. */
674 	struct net_if *iface;
675 
676 #if defined(CONFIG_NET_LLDP)
677 	struct ethernet_lldp lldp[NET_VLAN_MAX_COUNT];
678 #endif
679 
680 	/**
681 	 * This tells what L2 features does ethernet support.
682 	 */
683 	enum net_l2_flags ethernet_l2_flags;
684 
685 #if defined(CONFIG_NET_L2_PTP)
686 	/** The PTP port number for this network device. We need to store the
687 	 * port number here so that we do not need to fetch it for every
688 	 * incoming PTP packet.
689 	 */
690 	int port;
691 #endif
692 
693 #if defined(CONFIG_NET_DSA)
694 	/** DSA RX callback function - for custom processing - like e.g.
695 	 * redirecting packets when MAC address is caught
696 	 */
697 	dsa_net_recv_cb_t dsa_recv_cb;
698 
699 	/** Switch physical port number */
700 	uint8_t dsa_port_idx;
701 
702 	/** DSA context pointer */
703 	struct dsa_context *dsa_ctx;
704 
705 	/** Send a network packet via DSA master port */
706 	dsa_send_t dsa_send;
707 #endif
708 
709 	/** Is network carrier up */
710 	bool is_net_carrier_up : 1;
711 
712 	/** Is this context already initialized */
713 	bool is_init : 1;
714 
715 	/** Types of Ethernet network interfaces */
716 	enum ethernet_if_types eth_if_type;
717 };
718 
719 /**
720  * @brief Initialize Ethernet L2 stack for a given interface
721  *
722  * @param iface A valid pointer to a network interface
723  */
724 void ethernet_init(struct net_if *iface);
725 
726 #define ETHERNET_L2_CTX_TYPE	struct ethernet_context
727 
728 /* Separate header for VLAN as some of device interfaces might not
729  * support VLAN.
730  */
731 struct net_eth_vlan_hdr {
732 	struct net_eth_addr dst;
733 	struct net_eth_addr src;
734 	struct {
735 		uint16_t tpid; /* tag protocol id  */
736 		uint16_t tci;  /* tag control info */
737 	} vlan;
738 	uint16_t type;
739 } __packed;
740 
741 /** @endcond */
742 
743 /**
744  * @brief Check if the Ethernet MAC address is a broadcast address.
745  *
746  * @param addr A valid pointer to a Ethernet MAC address.
747  *
748  * @return true if address is a broadcast address, false if not
749  */
net_eth_is_addr_broadcast(struct net_eth_addr * addr)750 static inline bool net_eth_is_addr_broadcast(struct net_eth_addr *addr)
751 {
752 	if (addr->addr[0] == 0xff &&
753 	    addr->addr[1] == 0xff &&
754 	    addr->addr[2] == 0xff &&
755 	    addr->addr[3] == 0xff &&
756 	    addr->addr[4] == 0xff &&
757 	    addr->addr[5] == 0xff) {
758 		return true;
759 	}
760 
761 	return false;
762 }
763 
764 /**
765  * @brief Check if the Ethernet MAC address is a all zeroes address.
766  *
767  * @param addr A valid pointer to an Ethernet MAC address.
768  *
769  * @return true if address is an all zeroes address, false if not
770  */
net_eth_is_addr_all_zeroes(struct net_eth_addr * addr)771 static inline bool net_eth_is_addr_all_zeroes(struct net_eth_addr *addr)
772 {
773 	if (addr->addr[0] == 0x00 &&
774 	    addr->addr[1] == 0x00 &&
775 	    addr->addr[2] == 0x00 &&
776 	    addr->addr[3] == 0x00 &&
777 	    addr->addr[4] == 0x00 &&
778 	    addr->addr[5] == 0x00) {
779 		return true;
780 	}
781 
782 	return false;
783 }
784 
785 /**
786  * @brief Check if the Ethernet MAC address is unspecified.
787  *
788  * @param addr A valid pointer to a Ethernet MAC address.
789  *
790  * @return true if address is unspecified, false if not
791  */
net_eth_is_addr_unspecified(struct net_eth_addr * addr)792 static inline bool net_eth_is_addr_unspecified(struct net_eth_addr *addr)
793 {
794 	if (addr->addr[0] == 0x00 &&
795 	    addr->addr[1] == 0x00 &&
796 	    addr->addr[2] == 0x00 &&
797 	    addr->addr[3] == 0x00 &&
798 	    addr->addr[4] == 0x00 &&
799 	    addr->addr[5] == 0x00) {
800 		return true;
801 	}
802 
803 	return false;
804 }
805 
806 /**
807  * @brief Check if the Ethernet MAC address is a multicast address.
808  *
809  * @param addr A valid pointer to a Ethernet MAC address.
810  *
811  * @return true if address is a multicast address, false if not
812  */
net_eth_is_addr_multicast(struct net_eth_addr * addr)813 static inline bool net_eth_is_addr_multicast(struct net_eth_addr *addr)
814 {
815 #if defined(CONFIG_NET_IPV6)
816 	if (addr->addr[0] == 0x33 &&
817 	    addr->addr[1] == 0x33) {
818 		return true;
819 	}
820 #endif
821 
822 #if defined(CONFIG_NET_IPV4)
823 	if (addr->addr[0] == 0x01 &&
824 	    addr->addr[1] == 0x00 &&
825 	    addr->addr[2] == 0x5e) {
826 		return true;
827 	}
828 #endif
829 
830 	return false;
831 }
832 
833 /**
834  * @brief Check if the Ethernet MAC address is a group address.
835  *
836  * @param addr A valid pointer to a Ethernet MAC address.
837  *
838  * @return true if address is a group address, false if not
839  */
net_eth_is_addr_group(struct net_eth_addr * addr)840 static inline bool net_eth_is_addr_group(struct net_eth_addr *addr)
841 {
842 	return addr->addr[0] & 0x01;
843 }
844 
845 /**
846  * @brief Check if the Ethernet MAC address is valid.
847  *
848  * @param addr A valid pointer to a Ethernet MAC address.
849  *
850  * @return true if address is valid, false if not
851  */
net_eth_is_addr_valid(struct net_eth_addr * addr)852 static inline bool net_eth_is_addr_valid(struct net_eth_addr *addr)
853 {
854 	return !net_eth_is_addr_unspecified(addr) && !net_eth_is_addr_group(addr);
855 }
856 
857 /**
858  * @brief Check if the Ethernet MAC address is a LLDP multicast address.
859  *
860  * @param addr A valid pointer to a Ethernet MAC address.
861  *
862  * @return true if address is a LLDP multicast address, false if not
863  */
net_eth_is_addr_lldp_multicast(struct net_eth_addr * addr)864 static inline bool net_eth_is_addr_lldp_multicast(struct net_eth_addr *addr)
865 {
866 #if defined(CONFIG_NET_GPTP) || defined(CONFIG_NET_LLDP)
867 	if (addr->addr[0] == 0x01 &&
868 	    addr->addr[1] == 0x80 &&
869 	    addr->addr[2] == 0xc2 &&
870 	    addr->addr[3] == 0x00 &&
871 	    addr->addr[4] == 0x00 &&
872 	    addr->addr[5] == 0x0e) {
873 		return true;
874 	}
875 #else
876 	ARG_UNUSED(addr);
877 #endif
878 
879 	return false;
880 }
881 
882 /**
883  * @brief Check if the Ethernet MAC address is a PTP multicast address.
884  *
885  * @param addr A valid pointer to a Ethernet MAC address.
886  *
887  * @return true if address is a PTP multicast address, false if not
888  */
net_eth_is_addr_ptp_multicast(struct net_eth_addr * addr)889 static inline bool net_eth_is_addr_ptp_multicast(struct net_eth_addr *addr)
890 {
891 #if defined(CONFIG_NET_GPTP)
892 	if (addr->addr[0] == 0x01 &&
893 	    addr->addr[1] == 0x1b &&
894 	    addr->addr[2] == 0x19 &&
895 	    addr->addr[3] == 0x00 &&
896 	    addr->addr[4] == 0x00 &&
897 	    addr->addr[5] == 0x00) {
898 		return true;
899 	}
900 #else
901 	ARG_UNUSED(addr);
902 #endif
903 
904 	return false;
905 }
906 
907 /**
908  * @brief Return Ethernet broadcast address.
909  *
910  * @return Ethernet broadcast address.
911  */
912 const struct net_eth_addr *net_eth_broadcast_addr(void);
913 
914 /**
915  * @brief Convert IPv4 multicast address to Ethernet address.
916  *
917  * @param ipv4_addr IPv4 multicast address
918  * @param mac_addr Output buffer for Ethernet address
919  */
920 void net_eth_ipv4_mcast_to_mac_addr(const struct in_addr *ipv4_addr,
921 				    struct net_eth_addr *mac_addr);
922 
923 /**
924  * @brief Convert IPv6 multicast address to Ethernet address.
925  *
926  * @param ipv6_addr IPv6 multicast address
927  * @param mac_addr Output buffer for Ethernet address
928  */
929 void net_eth_ipv6_mcast_to_mac_addr(const struct in6_addr *ipv6_addr,
930 				    struct net_eth_addr *mac_addr);
931 
932 /**
933  * @brief Return ethernet device hardware capability information.
934  *
935  * @param iface Network interface
936  *
937  * @return Hardware capabilities
938  */
939 static inline
net_eth_get_hw_capabilities(struct net_if * iface)940 enum ethernet_hw_caps net_eth_get_hw_capabilities(struct net_if *iface)
941 {
942 	const struct device *dev = net_if_get_device(iface);
943 	const struct ethernet_api *api = (struct ethernet_api *)dev->api;
944 
945 	if (!api || !api->get_capabilities) {
946 		return (enum ethernet_hw_caps)0;
947 	}
948 
949 	return api->get_capabilities(dev);
950 }
951 
952 /**
953  * @brief Return ethernet device hardware configuration information.
954  *
955  * @param iface Network interface
956  * @param type configuration type
957  * @param config Ethernet configuration
958  *
959  * @return 0 if ok, <0 if error
960  */
961 static inline
net_eth_get_hw_config(struct net_if * iface,enum ethernet_config_type type,struct ethernet_config * config)962 int net_eth_get_hw_config(struct net_if *iface, enum ethernet_config_type type,
963 			 struct ethernet_config *config)
964 {
965 	const struct ethernet_api *eth =
966 		(struct ethernet_api *)net_if_get_device(iface)->api;
967 
968 	if (!eth->get_config) {
969 		return -ENOTSUP;
970 	}
971 
972 	return eth->get_config(net_if_get_device(iface), type, config);
973 }
974 
975 
976 /**
977  * @brief Add VLAN tag to the interface.
978  *
979  * @param iface Interface to use.
980  * @param tag VLAN tag to add
981  *
982  * @return 0 if ok, <0 if error
983  */
984 #if defined(CONFIG_NET_VLAN)
985 int net_eth_vlan_enable(struct net_if *iface, uint16_t tag);
986 #else
net_eth_vlan_enable(struct net_if * iface,uint16_t tag)987 static inline int net_eth_vlan_enable(struct net_if *iface, uint16_t tag)
988 {
989 	ARG_UNUSED(iface);
990 	ARG_UNUSED(tag);
991 
992 	return -EINVAL;
993 }
994 #endif
995 
996 /**
997  * @brief Remove VLAN tag from the interface.
998  *
999  * @param iface Interface to use.
1000  * @param tag VLAN tag to remove
1001  *
1002  * @return 0 if ok, <0 if error
1003  */
1004 #if defined(CONFIG_NET_VLAN)
1005 int net_eth_vlan_disable(struct net_if *iface, uint16_t tag);
1006 #else
net_eth_vlan_disable(struct net_if * iface,uint16_t tag)1007 static inline int net_eth_vlan_disable(struct net_if *iface, uint16_t tag)
1008 {
1009 	ARG_UNUSED(iface);
1010 	ARG_UNUSED(tag);
1011 
1012 	return -EINVAL;
1013 }
1014 #endif
1015 
1016 /**
1017  * @brief Return VLAN tag specified to network interface.
1018  *
1019  * Note that the interface parameter must be the VLAN interface,
1020  * and not the Ethernet one.
1021  *
1022  * @param iface VLAN network interface.
1023  *
1024  * @return VLAN tag for this interface or NET_VLAN_TAG_UNSPEC if VLAN
1025  * is not configured for that interface.
1026  */
1027 #if defined(CONFIG_NET_VLAN)
1028 uint16_t net_eth_get_vlan_tag(struct net_if *iface);
1029 #else
net_eth_get_vlan_tag(struct net_if * iface)1030 static inline uint16_t net_eth_get_vlan_tag(struct net_if *iface)
1031 {
1032 	ARG_UNUSED(iface);
1033 
1034 	return NET_VLAN_TAG_UNSPEC;
1035 }
1036 #endif
1037 
1038 /**
1039  * @brief Return network interface related to this VLAN tag
1040  *
1041  * @param iface Main network interface (not the VLAN one).
1042  * @param tag VLAN tag
1043  *
1044  * @return Network interface related to this tag or NULL if no such interface
1045  * exists.
1046  */
1047 #if defined(CONFIG_NET_VLAN)
1048 struct net_if *net_eth_get_vlan_iface(struct net_if *iface, uint16_t tag);
1049 #else
1050 static inline
net_eth_get_vlan_iface(struct net_if * iface,uint16_t tag)1051 struct net_if *net_eth_get_vlan_iface(struct net_if *iface, uint16_t tag)
1052 {
1053 	ARG_UNUSED(iface);
1054 	ARG_UNUSED(tag);
1055 
1056 	return NULL;
1057 }
1058 #endif
1059 
1060 /**
1061  * @brief Return main network interface that is attached to this VLAN tag.
1062  *
1063  * @param iface VLAN network interface. This is used to get the
1064  *        pointer to ethernet L2 context
1065  *
1066  * @return Network interface related to this tag or NULL if no such interface
1067  * exists.
1068  */
1069 #if defined(CONFIG_NET_VLAN)
1070 struct net_if *net_eth_get_vlan_main(struct net_if *iface);
1071 #else
1072 static inline
net_eth_get_vlan_main(struct net_if * iface)1073 struct net_if *net_eth_get_vlan_main(struct net_if *iface)
1074 {
1075 	ARG_UNUSED(iface);
1076 
1077 	return NULL;
1078 }
1079 #endif
1080 
1081 /**
1082  * @brief Check if there are any VLAN interfaces enabled to this specific
1083  *        Ethernet network interface.
1084  *
1085  * Note that the iface must be the actual Ethernet interface and not the
1086  * virtual VLAN interface.
1087  *
1088  * @param ctx Ethernet context
1089  * @param iface Ethernet network interface
1090  *
1091  * @return True if there are enabled VLANs for this network interface,
1092  *         false if not.
1093  */
1094 #if defined(CONFIG_NET_VLAN)
1095 bool net_eth_is_vlan_enabled(struct ethernet_context *ctx,
1096 			     struct net_if *iface);
1097 #else
net_eth_is_vlan_enabled(struct ethernet_context * ctx,struct net_if * iface)1098 static inline bool net_eth_is_vlan_enabled(struct ethernet_context *ctx,
1099 					   struct net_if *iface)
1100 {
1101 	ARG_UNUSED(ctx);
1102 	ARG_UNUSED(iface);
1103 
1104 	return false;
1105 }
1106 #endif
1107 
1108 /**
1109  * @brief Get VLAN status for a given network interface (enabled or not).
1110  *
1111  * @param iface Network interface
1112  *
1113  * @return True if VLAN is enabled for this network interface, false if not.
1114  */
1115 #if defined(CONFIG_NET_VLAN)
1116 bool net_eth_get_vlan_status(struct net_if *iface);
1117 #else
net_eth_get_vlan_status(struct net_if * iface)1118 static inline bool net_eth_get_vlan_status(struct net_if *iface)
1119 {
1120 	ARG_UNUSED(iface);
1121 
1122 	return false;
1123 }
1124 #endif
1125 
1126 /**
1127  * @brief Check if the given interface is a VLAN interface.
1128  *
1129  * @param iface Network interface
1130  *
1131  * @return True if this network interface is VLAN one, false if not.
1132  */
1133 #if defined(CONFIG_NET_VLAN)
1134 bool net_eth_is_vlan_interface(struct net_if *iface);
1135 #else
net_eth_is_vlan_interface(struct net_if * iface)1136 static inline bool net_eth_is_vlan_interface(struct net_if *iface)
1137 {
1138 	ARG_UNUSED(iface);
1139 
1140 	return false;
1141 }
1142 #endif
1143 
1144 /** @cond INTERNAL_HIDDEN */
1145 
1146 #if !defined(CONFIG_ETH_DRIVER_RAW_MODE)
1147 
1148 #define Z_ETH_NET_DEVICE_INIT_INSTANCE(node_id, dev_id, name, instance,	\
1149 				       init_fn, pm, data, config, prio,	\
1150 				       api, mtu)			\
1151 	Z_NET_DEVICE_INIT_INSTANCE(node_id, dev_id, name, instance,	\
1152 				   init_fn, pm, data, config, prio,	\
1153 				   api, ETHERNET_L2,			\
1154 				   NET_L2_GET_CTX_TYPE(ETHERNET_L2), mtu)
1155 
1156 #else /* CONFIG_ETH_DRIVER_RAW_MODE */
1157 
1158 #define Z_ETH_NET_DEVICE_INIT_INSTANCE(node_id, dev_id, name, instance,	\
1159 				       init_fn, pm, data, config, prio,	\
1160 				       api, mtu)			\
1161 	Z_DEVICE_STATE_DEFINE(dev_id);					\
1162 	Z_DEVICE_DEFINE(node_id, dev_id, name, init_fn, pm, data,	\
1163 			config, POST_KERNEL, prio, api,			\
1164 			&Z_DEVICE_STATE_NAME(dev_id));
1165 
1166 #endif /* CONFIG_ETH_DRIVER_RAW_MODE */
1167 
1168 #define Z_ETH_NET_DEVICE_INIT(node_id, dev_id, name, init_fn, pm, data,	\
1169 			      config, prio, api, mtu)			\
1170 	Z_ETH_NET_DEVICE_INIT_INSTANCE(node_id, dev_id, name, 0,	\
1171 				       init_fn, pm, data, config, prio,	\
1172 				       api, mtu)
1173 
1174 /** @endcond */
1175 
1176 /**
1177  * @brief Create an Ethernet network interface and bind it to network device.
1178  *
1179  * @param dev_id Network device id.
1180  * @param name The name this instance of the driver exposes to
1181  * the system.
1182  * @param init_fn Address to the init function of the driver.
1183  * @param pm Reference to struct pm_device associated with the device.
1184  * (optional).
1185  * @param data Pointer to the device's private data.
1186  * @param config The address to the structure containing the
1187  * configuration information for this instance of the driver.
1188  * @param prio The initialization level at which configuration occurs.
1189  * @param api Provides an initial pointer to the API function struct
1190  * used by the driver. Can be NULL.
1191  * @param mtu Maximum transfer unit in bytes for this network interface.
1192  */
1193 #define ETH_NET_DEVICE_INIT(dev_id, name, init_fn, pm, data, config,	\
1194 			    prio, api, mtu)				\
1195 	Z_ETH_NET_DEVICE_INIT(DT_INVALID_NODE, dev_id, name, init_fn,	\
1196 			      pm, data, config, prio, api, mtu)
1197 
1198 /**
1199  * @brief Create multiple Ethernet network interfaces and bind them to network
1200  * devices.
1201  * If your network device needs more than one instance of a network interface,
1202  * use this macro below and provide a different instance suffix each time
1203  * (0, 1, 2, ... or a, b, c ... whatever works for you)
1204  *
1205  * @param dev_id Network device id.
1206  * @param name The name this instance of the driver exposes to
1207  * the system.
1208  * @param instance Instance identifier.
1209  * @param init_fn Address to the init function of the driver.
1210  * @param pm Reference to struct pm_device associated with the device.
1211  * (optional).
1212  * @param data Pointer to the device's private data.
1213  * @param config The address to the structure containing the
1214  * configuration information for this instance of the driver.
1215  * @param prio The initialization level at which configuration occurs.
1216  * @param api Provides an initial pointer to the API function struct
1217  * used by the driver. Can be NULL.
1218  * @param mtu Maximum transfer unit in bytes for this network interface.
1219  */
1220 #define ETH_NET_DEVICE_INIT_INSTANCE(dev_id, name, instance, init_fn,	\
1221 				     pm, data, config, prio, api, mtu)	\
1222 	Z_ETH_NET_DEVICE_INIT_INSTANCE(DT_INVALID_NODE, dev_id, name,	\
1223 				       instance, init_fn, pm, data,	\
1224 				       config, prio, api, mtu)
1225 
1226 /**
1227  * @brief Like ETH_NET_DEVICE_INIT but taking metadata from a devicetree.
1228  * Create an Ethernet network interface and bind it to network device.
1229  *
1230  * @param node_id The devicetree node identifier.
1231  * @param init_fn Address to the init function of the driver.
1232  * @param pm Reference to struct pm_device associated with the device.
1233  * (optional).
1234  * @param data Pointer to the device's private data.
1235  * @param config The address to the structure containing the
1236  * configuration information for this instance of the driver.
1237  * @param prio The initialization level at which configuration occurs.
1238  * @param api Provides an initial pointer to the API function struct
1239  * used by the driver. Can be NULL.
1240  * @param mtu Maximum transfer unit in bytes for this network interface.
1241  */
1242 #define ETH_NET_DEVICE_DT_DEFINE(node_id, init_fn, pm, data, config,	\
1243 				 prio, api, mtu)			\
1244 	Z_ETH_NET_DEVICE_INIT(node_id, Z_DEVICE_DT_DEV_ID(node_id),	\
1245 			      DEVICE_DT_NAME(node_id), init_fn, pm,	\
1246 			      data, config, prio, api, mtu)
1247 
1248 /**
1249  * @brief Like ETH_NET_DEVICE_DT_DEFINE for an instance of a DT_DRV_COMPAT
1250  * compatible
1251  *
1252  * @param inst instance number.  This is replaced by
1253  * <tt>DT_DRV_COMPAT(inst)</tt> in the call to ETH_NET_DEVICE_DT_DEFINE.
1254  *
1255  * @param ... other parameters as expected by ETH_NET_DEVICE_DT_DEFINE.
1256  */
1257 #define ETH_NET_DEVICE_DT_INST_DEFINE(inst, ...) \
1258 	ETH_NET_DEVICE_DT_DEFINE(DT_DRV_INST(inst), __VA_ARGS__)
1259 
1260 /**
1261  * @brief Inform ethernet L2 driver that ethernet carrier is detected.
1262  * This happens when cable is connected.
1263  *
1264  * @param iface Network interface
1265  */
1266 void net_eth_carrier_on(struct net_if *iface);
1267 
1268 /**
1269  * @brief Inform ethernet L2 driver that ethernet carrier was lost.
1270  * This happens when cable is disconnected.
1271  *
1272  * @param iface Network interface
1273  */
1274 void net_eth_carrier_off(struct net_if *iface);
1275 
1276 /**
1277  * @brief Set promiscuous mode either ON or OFF.
1278  *
1279  * @param iface Network interface
1280  *
1281  * @param enable on (true) or off (false)
1282  *
1283  * @return 0 if mode set or unset was successful, <0 otherwise.
1284  */
1285 int net_eth_promisc_mode(struct net_if *iface, bool enable);
1286 
1287 /**
1288  * @brief Set TX-Injection mode either ON or OFF.
1289  *
1290  * @param iface Network interface
1291  *
1292  * @param enable on (true) or off (false)
1293  *
1294  * @return 0 if mode set or unset was successful, <0 otherwise.
1295  */
1296 int net_eth_txinjection_mode(struct net_if *iface, bool enable);
1297 
1298 /**
1299  * @brief Set or unset HW filtering for MAC address @p mac.
1300  *
1301  * @param iface Network interface
1302  * @param mac Pointer to an ethernet MAC address
1303  * @param type Filter type, either source or destination
1304  * @param enable Set (true) or unset (false)
1305  *
1306  * @return 0 if filter set or unset was successful, <0 otherwise.
1307  */
1308 int net_eth_mac_filter(struct net_if *iface, struct net_eth_addr *mac,
1309 		       enum ethernet_filter_type type, bool enable);
1310 
1311 /**
1312  * @brief Return the PHY device that is tied to this ethernet network interface.
1313  *
1314  * @param iface Network interface
1315  *
1316  * @return Pointer to PHY device if found, NULL if not found.
1317  */
1318 const struct device *net_eth_get_phy(struct net_if *iface);
1319 
1320 /**
1321  * @brief Return PTP clock that is tied to this ethernet network interface.
1322  *
1323  * @param iface Network interface
1324  *
1325  * @return Pointer to PTP clock if found, NULL if not found or if this
1326  * ethernet interface does not support PTP.
1327  */
1328 #if defined(CONFIG_PTP_CLOCK)
1329 const struct device *net_eth_get_ptp_clock(struct net_if *iface);
1330 #else
net_eth_get_ptp_clock(struct net_if * iface)1331 static inline const struct device *net_eth_get_ptp_clock(struct net_if *iface)
1332 {
1333 	ARG_UNUSED(iface);
1334 
1335 	return NULL;
1336 }
1337 #endif
1338 
1339 /**
1340  * @brief Return PTP clock that is tied to this ethernet network interface
1341  * index.
1342  *
1343  * @param index Network interface index
1344  *
1345  * @return Pointer to PTP clock if found, NULL if not found or if this
1346  * ethernet interface index does not support PTP.
1347  */
1348 __syscall const struct device *net_eth_get_ptp_clock_by_index(int index);
1349 
1350 /**
1351  * @brief Return PTP port number attached to this interface.
1352  *
1353  * @param iface Network interface
1354  *
1355  * @return Port number, no such port if < 0
1356  */
1357 #if defined(CONFIG_NET_L2_PTP)
1358 int net_eth_get_ptp_port(struct net_if *iface);
1359 #else
net_eth_get_ptp_port(struct net_if * iface)1360 static inline int net_eth_get_ptp_port(struct net_if *iface)
1361 {
1362 	ARG_UNUSED(iface);
1363 
1364 	return -ENODEV;
1365 }
1366 #endif /* CONFIG_NET_L2_PTP */
1367 
1368 /**
1369  * @brief Set PTP port number attached to this interface.
1370  *
1371  * @param iface Network interface
1372  * @param port Port number to set
1373  */
1374 #if defined(CONFIG_NET_L2_PTP)
1375 void net_eth_set_ptp_port(struct net_if *iface, int port);
1376 #else
net_eth_set_ptp_port(struct net_if * iface,int port)1377 static inline void net_eth_set_ptp_port(struct net_if *iface, int port)
1378 {
1379 	ARG_UNUSED(iface);
1380 	ARG_UNUSED(port);
1381 }
1382 #endif /* CONFIG_NET_L2_PTP */
1383 
1384 /**
1385  * @brief Check if the Ethernet L2 network interface can perform Wi-Fi.
1386  *
1387  * @param iface Pointer to network interface
1388  *
1389  * @return True if interface supports Wi-Fi, False otherwise.
1390  */
net_eth_type_is_wifi(struct net_if * iface)1391 static inline bool net_eth_type_is_wifi(struct net_if *iface)
1392 {
1393 	const struct ethernet_context *ctx = (struct ethernet_context *)
1394 		net_if_l2_data(iface);
1395 
1396 	return ctx->eth_if_type == L2_ETH_IF_TYPE_WIFI;
1397 }
1398 
1399 /**
1400  * @}
1401  */
1402 
1403 #ifdef __cplusplus
1404 }
1405 #endif
1406 
1407 #include <zephyr/syscalls/ethernet.h>
1408 
1409 #endif /* ZEPHYR_INCLUDE_NET_ETHERNET_H_ */
1410