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