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