AM261x MCU+ SDK  10.02.00
cb_lld_ethernet.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2023 Texas Instruments Incorporated
3  * Copyright (c) 2023 Excelfore Corporation (https://excelfore.com)
4  *
5  * All rights reserved not granted herein.
6  * Limited License.
7  *
8  * Texas Instruments Incorporated grants a world-wide, royalty-free,
9  * non-exclusive license under copyrights and patents it now or hereafter
10  * owns or controls to make, have made, use, import, offer to sell and sell ("Utilize")
11  * this software subject to the terms herein. With respect to the foregoing patent
12  * license, such license is granted solely to the extent that any such patent is necessary
13  * to Utilize the software alone. The patent license shall not apply to any combinations which
14  * include this software, other than combinations with devices manufactured by or for TI ("TI Devices").
15  * No hardware patent is licensed hereunder.
16  *
17  * Redistributions must preserve existing copyright notices and reproduce this license (including the
18  * above copyright notice and the disclaimer and (if applicable) source code license limitations below)
19  * in the documentation and/or other materials provided with the distribution
20  *
21  * Redistribution and use in binary form, without modification, are permitted provided that the following
22  * conditions are met:
23  *
24  * * No reverse engineering, decompilation, or disassembly of this software is permitted with respect to any
25  * software provided in binary form.
26  * * any redistribution and use are licensed by TI for use only with TI Devices.
27  * * Nothing shall obligate TI to provide you with source code for the software licensed and provided to you in object code.
28  *
29  * If software source code is provided to you, modification and redistribution of the source code are permitted
30  * provided that the following conditions are met:
31  *
32  * * any redistribution and use of the source code, including any resulting derivative works, are licensed by
33  * TI for use only with TI Devices.
34  * * any redistribution and use of any object code compiled from the source code and any resulting derivative
35  * works, are licensed by TI for use only with TI Devices.
36  *
37  * Neither the name of Texas Instruments Incorporated nor the names of its suppliers may be used to endorse or
38  * promote products derived from this software without specific prior written permission.
39  *
40  * DISCLAIMER.
41  *
42  * THIS SOFTWARE IS PROVIDED BY TI AND TI"S LICENSORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
43  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
44  * IN NO EVENT SHALL TI AND TI"S LICENSORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
45  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
46  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
47  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
48  * POSSIBILITY OF SUCH DAMAGE.
49 */
58 #ifndef CB_LLD_ETHERNET_H
59 #define CB_LLD_ETHERNET_H
60 
61 #include <sys/types.h>
62 #include <errno.h>
63 #include "lldenet.h"
64 #include "lldtsync.h"
65 #ifdef __cplusplus
66 extern "C" {
67 #endif
68 
69 // To prevent dupplicated declaration of below APIs
70 #define LWIP_DONT_PROVIDE_BYTEORDER_FUNCTIONS
71 #ifndef htons
72 #define htons __htons
73 #endif
74 #ifndef ntohs
75 #define ntohs __ntohs
76 #endif
77 #ifndef htonl
78 #define htonl __htonl
79 #endif
80 #ifndef ntohl
81 #define ntohl __ntohl
82 #endif
83 
84 #define MAX_NUMBER_ENET_DEVS LLDENET_MAX_PORTS
85 
86 #define CB_SOCKET_VALID(x) ((x)!=NULL)
87 #define CB_SOCKET_INVALID_VALUE NULL
88 #define CB_SOCKET_T lld_socket_t*
89 #define CB_ETHHDR_T struct lld_ethhdr
90 #define CB_SOCKADDR_LL_T struct lld_sockaddr
91 #define CB_SOCKADDR_T struct lld_sockaddr
92 #define CB_SOCK_SENDTO cb_lld_sendto
93 #define CB_SOCK_CLOSE cb_rawsock_close
94 
95 #define CB_OPEN open
96 #define CB_CLOSE close
97 #define CB_WRITE write
98 
99 /* does not support */
100 #define CB_IN_ADDR_T void*
101 
102 #ifndef IFNAMSIZ
103 #define IFNAMSIZ 16
104 #endif
105 
106 #define ETH_ALEN 6 /* Octets in one ethernet addr */
107 #define ETH_HLEN 14 /* Total octets in header. */
108 #define ETH_DATA_LEN 1500 /* Max. octets in payload */
109 
110 #define ETH_P_8021Q 0x8100 /* 802.1Q VLAN Extended Header */
111 #define ETH_P_1588 0x88F7 /* IEEE 1588 Timesync */
112 #define ETH_P_LLDP 0x88CC /* 802.1AB LLDP */
113 #define ETH_P_TSN 0x22F0 /* TSN (IEEE 1722) packet */
114 #define ETH_P_NETLINK 0x22F1 /* A virtual prototol for netlink rawsockt on TI platforms */
115 
116 #define CB_MAX_NETDEVNAME 16
117 
118 #define MKPIPE(x) (-1)
119 
120 #define CB_LLD_MAX_ETH_FRAME_SIZE (1522U)
121 
125 struct lld_ethhdr {
129  uint8_t h_dest[ETH_ALEN];
133  uint8_t h_source[ETH_ALEN];
137  uint16_t h_proto;
138 } __attribute__((packed));
139 
143 struct lld_sockaddr {
147  unsigned char sll_addr[6];
151  int macport;
156  int tcid;
160  uint64_t rxts;
161 };
162 
166 typedef struct lld_socket lld_socket_t;
167 
171 typedef struct {
175  char *netdev;
179  uint8_t macport;
183  uint8_t srcmac[ETH_ALEN];
184 } lld_ethdev_t;
185 
189 typedef struct {
190  /* below are keys to match the expected socket */
194  const char *dev;
199  uint16_t proto;
203  uint16_t vlanid;
204  /* below are params can be updated to LLDEnetCfg_t */
209  uint32_t nTxPkts;
219  uint32_t pktSize;
252  void (*rxDefaultDataCb)(void *data, int size, int port, void *cbArg);
263 
264 /* Number of elements in a statistics block */
265 #define CB_TILLD_STATS_BLOCK_ELEM_NUM (128U)
266 typedef struct cb_tilld_port_stats {
269 
277 
296 int cb_lld_init_devs_table(lld_ethdev_t *ethdevs, uint32_t ndevs,
297  uint32_t enet_type, uint32_t instance_id,
298  const LLDTsyncTsSource tsSource);
299 
309 
315 int cb_lld_netdev_to_macport(const char *netdev);
316 
327 int cb_lld_sendto(CB_SOCKET_T sfd, void *sdata, int psize, int flags,
328  const CB_SOCKADDR_LL_T *addr, int addrsize);
329 
338  const CB_SOCKADDR_LL_T *addr);
339 
350 int cb_lld_recv(CB_SOCKET_T sfd, void *buf, int size,
351  CB_SOCKADDR_LL_T *addr, int addrsize);
352 
361 int cb_lld_recv_zerocopy(CB_SOCKET_T sfd, LLDEnetRecvCb_t cblld_recv_cb, void *cbarg);
362 
371 int cb_lld_set_txnotify_cb(CB_SOCKET_T sfd, void (*txnotify_cb)(void *arg), void *arg);
372 
381 int cb_lld_set_rxnotify_cb(CB_SOCKET_T sfd, void (*rxnotify_cb)(void *arg), void *arg);
382 
392  void (*default_rxdata_cb)(void *data, int size, int port, void *arg), void *arg);
393 
400 int cb_lld_get_type_instance(uint32_t *enet_type, uint32_t *instance_id);
401 
408 
415 int cb_lld_get_netdevs(char* netdevs[], int *len);
416 
425 int cb_lld_get_link_state(CB_SOCKET_T cfd, const char *dev, uint32_t *linkstate);
426 
436 int cb_lld_get_link_info(CB_SOCKET_T cfd, const char *dev, uint32_t *speed, uint32_t *duplex);
437 
447 
455 
462 
463 #ifdef __cplusplus
464 }
465 #endif
466 #endif //CB_LLD_ETHERNET_H
467 
cb_lld_get_port_stats
int cb_lld_get_port_stats(CB_SOCKET_T sfd, int port, cb_tilld_port_stats_t *stats)
Get stats info from host or mac port.
cb_lld_sendto
int cb_lld_sendto(CB_SOCKET_T sfd, void *sdata, int psize, int flags, const CB_SOCKADDR_LL_T *addr, int addrsize)
Send a TX ethernet L2 packet.
cb_lld_recv_zerocopy
int cb_lld_recv_zerocopy(CB_SOCKET_T sfd, LLDEnetRecvCb_t cblld_recv_cb, void *cbarg)
Receive a RX ethernet L2 packet n the zero-copy way. This should be called after a rxnotify_cb is inv...
lld_ethdev_t
Structure representing the lld_ethdev instance.
Definition: cb_lld_ethernet.h:171
lldenet.h
TI Ethernet Low Level Driver abstract APIs.
cb_socket_lldcfg_update_t::dmaRxShared
int dmaRxShared
Definition: cb_lld_ethernet.h:241
cb_socket_lldcfg_update_t::dmaTxChId
int dmaTxChId
Definition: cb_lld_ethernet.h:224
cb_lld_init_devs_table
int cb_lld_init_devs_table(lld_ethdev_t *ethdevs, uint32_t ndevs, uint32_t enet_type, uint32_t instance_id, const LLDTsyncTsSource tsSource)
Initialize a LLD device table.
lld_ethhdr::h_proto
uint16_t h_proto
Definition: cb_lld_ethernet.h:137
lld_socket_t
struct lld_socket lld_socket_t
Structure representing the lld_socket instance.
Definition: cb_lld_ethernet.h:166
lld_ethhdr::h_source
uint8_t h_source[ETH_ALEN]
Definition: cb_lld_ethernet.h:133
LLDEnetRecvCb_t
void(* LLDEnetRecvCb_t)(LLDEnetFrame_t *frame, void *cbArg)
Receives callback an Ethernet frame using LLDEnet in the Zero-Copy way.
Definition: lldenet.h:337
lld_sockaddr::sll_addr
unsigned char sll_addr[6]
Definition: cb_lld_ethernet.h:147
lld_sockaddr::macport
int macport
Definition: cb_lld_ethernet.h:151
cb_lld_netdev_to_macport
int cb_lld_netdev_to_macport(const char *netdev)
Convert net device name to mac port.
cb_lld_recv
int cb_lld_recv(CB_SOCKET_T sfd, void *buf, int size, CB_SOCKADDR_LL_T *addr, int addrsize)
Receive a RX ethernet L2 packet. This should be called after a rxnotify_cb is invoked.
cb_socket_lldcfg_update_t::numRxChannels
int numRxChannels
Definition: cb_lld_ethernet.h:261
lld_ethdev_t::netdev
char * netdev
Definition: cb_lld_ethernet.h:175
cb_socket_lldcfg_update_t::rxDefaultCbArg
void * rxDefaultCbArg
Definition: cb_lld_ethernet.h:257
cb_socket_set_lldcfg_update_cb
int cb_socket_set_lldcfg_update_cb(cb_socket_lldcfg_update_cb_t lldcfg_update_cb)
This function sets the callback for updating LLD config parameters. It enables the user to update con...
cb_socket_lldcfg_update_t::proto
uint16_t proto
Definition: cb_lld_ethernet.h:199
lld_sockaddr::tcid
int tcid
Definition: cb_lld_ethernet.h:156
cb_lld_get_type_instance
int cb_lld_get_type_instance(uint32_t *enet_type, uint32_t *instance_id)
Get enet type and instance that is set via cb_lld_init_devs_table()
__attribute__
struct lld_sockaddr __attribute__
This is the SecureBoot Stream type which holds the data for a specific bootloader to HSM call....
CB_SOCKADDR_LL_T
#define CB_SOCKADDR_LL_T
Definition: cb_lld_ethernet.h:90
cb_socket_lldcfg_update_t::dmaRxOwner
int dmaRxOwner
Definition: cb_lld_ethernet.h:247
MAX_NUM_RX_DMA_CH_PER_INSTANCE
#define MAX_NUM_RX_DMA_CH_PER_INSTANCE
Definition: lldtype.h:114
flags
uint8_t flags
Definition: hsmclient_msg.h:2
LLDEnetFrameScatter_t
Structure representing an Ethernet frame.
Definition: lldenet.h:203
cb_socket_lldcfg_update_cb_t
int(* cb_socket_lldcfg_update_cb_t)(cb_socket_lldcfg_update_t *update_cfg)
Callback function type for cb_socket_lldcfg_update.
Definition: cb_lld_ethernet.h:276
cb_socket_lldcfg_update_t::pktSize
uint32_t pktSize
Definition: cb_lld_ethernet.h:219
lld_ethhdr
Ethernet header structure.
Definition: cb_lld_ethernet.h:125
cb_tilld_port_stats_t
Definition: cb_lld_ethernet.h:266
cb_lld_sendto_scatter
int cb_lld_sendto_scatter(CB_SOCKET_T sfd, LLDEnetFrameScatter_t *frame, const CB_SOCKADDR_LL_T *addr)
Send a TX ethernet L2 packet in the scatter way.
cb_socket_lldcfg_update_t::nTxPkts
uint32_t nTxPkts
Definition: cb_lld_ethernet.h:209
CB_TILLD_STATS_BLOCK_ELEM_NUM
#define CB_TILLD_STATS_BLOCK_ELEM_NUM
Definition: cb_lld_ethernet.h:265
cb_lld_process_status_frames
int cb_lld_process_status_frames(CB_SOCKET_T sfd)
process the Phy status frames.
lld_ethdev_t::macport
uint8_t macport
Definition: cb_lld_ethernet.h:179
lld_ethhdr::h_dest
uint8_t h_dest[ETH_ALEN]
Definition: cb_lld_ethernet.h:129
cb_socket_lldcfg_update_t::dmaTxShared
int dmaTxShared
Definition: cb_lld_ethernet.h:235
cb_lld_set_rxnotify_cb
int cb_lld_set_rxnotify_cb(CB_SOCKET_T sfd, void(*rxnotify_cb)(void *arg), void *arg)
Set the RX notify callback that is invoked when an RX packet is ready to be received.
cb_lld_set_default_rxdata_cb
int cb_lld_set_default_rxdata_cb(CB_SOCKET_T sfd, void(*default_rxdata_cb)(void *data, int size, int port, void *arg), void *arg)
Set the default RX data callback that is invoked when an RX packet does not match any filters.
cb_lld_set_txnotify_cb
int cb_lld_set_txnotify_cb(CB_SOCKET_T sfd, void(*txnotify_cb)(void *arg), void *arg)
Set the TX notify callback that is invoked when an TX packet is sent successfully.
cb_socket_lldcfg_update_t
Structure representing the cb_socket_lldcfg_update instance.
Definition: cb_lld_ethernet.h:189
ETH_ALEN
#define ETH_ALEN
Definition: cb_lld_ethernet.h:106
cb_lld_get_netdevs
int cb_lld_get_netdevs(char *netdevs[], int *len)
Get all virtual network interfaces which were defined for the TI platform.
cb_socket_lldcfg_update_t::vlanid
uint16_t vlanid
Definition: cb_lld_ethernet.h:203
cb_lld_reset_port_stats
void cb_lld_reset_port_stats(CB_SOCKET_T sfd, int port)
reset stats info from host or mac port.
lldtsync.h
TI Timesync Low Level Driver abstract APIs.
LLDTsyncTsSource
LLDTsyncTsSource
Definition: lldtsync.h:76
lld_sockaddr::rxts
uint64_t rxts
Definition: cb_lld_ethernet.h:160
cb_lld_get_ts_source
int cb_lld_get_ts_source(LLDTsyncTsSource *tsSource)
Get Timestamp source that is set via cb_lld_init_devs_table()
lld_sockaddr
Structure representing the lld_sockaddr instance.
Definition: cb_lld_ethernet.h:143
cb_lld_get_link_state
int cb_lld_get_link_state(CB_SOCKET_T cfd, const char *dev, uint32_t *linkstate)
get link state from device name like 'eth0'
cb_socket_lldcfg_update_t::dev
const char * dev
Definition: cb_lld_ethernet.h:194
CB_SOCKET_T
#define CB_SOCKET_T
Definition: cb_lld_ethernet.h:88
cb_lld_get_link_info
int cb_lld_get_link_info(CB_SOCKET_T cfd, const char *dev, uint32_t *speed, uint32_t *duplex)
get link info(speed and duplex) from device name like 'eth0'