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