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