AM243x MCU+ SDK  09.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 */
57 #ifndef CB_LLD_ETHERNET_H
58 #define CB_LLD_ETHERNET_H
59 
60 #include <sys/types.h>
61 #include <errno.h>
62 #include "lldenet.h"
63 #include "lldtsync.h"
64 
65 #ifdef __cplusplus
66 extern "C" {
67 #endif
68 
69 #ifndef htons
70 #define htons __htons
71 #endif
72 #ifndef ntohs
73 #define ntohs __ntohs
74 #endif
75 #ifndef htonl
76 #define htonl __htonl
77 #endif
78 #ifndef ntohl
79 #define ntohl __ntohl
80 #endif
81 
82 #define MAX_NUMBER_ENET_DEVS LLDENET_MAX_PORTS
83 
84 #define CB_SOCKET_VALID(x) ((x)!=NULL)
85 #define CB_SOCKET_INVALID_VALUE NULL
86 #define CB_SOCKET_T lld_socket_t*
87 #define CB_ETHHDR_T struct ethhdr
88 #define CB_SOCKADDR_LL_T struct sockaddr
89 #define CB_SOCKADDR_T struct sockaddr
90 #define CB_SOCK_SENDTO cb_lld_sendto
91 #define CB_SOCK_CLOSE cb_rawsock_close
92 
93 #define CB_OPEN open
94 #define CB_CLOSE close
95 #define CB_WRITE write
96 
97 /* does not support */
98 #define CB_IN_ADDR_T void*
99 
100 #ifndef IFNAMSIZ
101 #define IFNAMSIZ 16
102 #endif
103 
104 #define ETH_ALEN 6 /* Octets in one ethernet addr */
105 #define ETH_HLEN 14 /* Total octets in header. */
106 #define ETH_DATA_LEN 1500 /* Max. octets in payload */
107 
108 #define ETH_P_8021Q 0x8100 /* 802.1Q VLAN Extended Header */
109 #define ETH_P_1588 0x88F7 /* IEEE 1588 Timesync */
110 #define ETH_P_LLDP 0x88CC /* 802.1AB LLDP */
111 #define ETH_P_TSN 0x22F0 /* TSN (IEEE 1722) packet */
112 #define ETH_P_NETLINK 0x22F1 /* A virtual prototol for netlink rawsockt on TI platforms */
113 
114 #define CB_MAX_NETDEVNAME IFNAMSIZ
115 
116 #define MKPIPE(x) (-1)
117 
118 #define CB_LLD_MAX_ETH_FRAME_SIZE (1522U)
119 
123 struct ethhdr {
127  uint8_t h_dest[ETH_ALEN];
131  uint8_t h_source[ETH_ALEN];
135  uint16_t h_proto;
136 } __attribute__((packed));
137 
141 struct sockaddr {
145  unsigned char sll_addr[6];
149  int macport;
154  int tcid;
158  uint64_t rxts;
159 };
160 
164 typedef struct lld_socket lld_socket_t;
165 
169 typedef struct {
173  char *netdev;
177  uint8_t macport;
181  uint8_t srcmac[ETH_ALEN];
182 } lld_ethdev_t;
183 
187 typedef struct {
188  /* below are keys to match the expected socket */
192  const char *dev;
197  uint16_t proto;
201  uint16_t vlanid;
202  /* below are params can be updated to LLDEnetCfg_t */
207  uint32_t nTxPkts;
217  uint32_t pktSize;
254  void (*rxDefaultDataCb)(void *data, int size, int port, void *cbArg);
263  uint32_t numRxChannels;
265 
266 /* Number of elements in a statistics block */
267 #define CB_TILLD_STATS_BLOCK_ELEM_NUM (128U)
268 typedef struct cb_tilld_port_stats {
271 
279 
297 int cb_lld_init_devs_table(lld_ethdev_t *ethdevs, uint32_t ndevs,
298  uint32_t enet_type, uint32_t instance_id);
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 
340 int cb_lld_recv(CB_SOCKET_T sfd, void *buf, int size,
341  CB_SOCKADDR_LL_T *addr, int addrsize);
342 
343 typedef void (*cb_lld_zerocopy_recv_cb_t)(void *buf, int size,
344  CB_SOCKADDR_LL_T *addr, void *cbarg);
354  void *cbarg);
355 
364 int cb_lld_set_txnotify_cb(CB_SOCKET_T sfd, void (*txnotify_cb)(void *arg), void *arg);
365 
374 int cb_lld_set_rxnotify_cb(CB_SOCKET_T sfd, void (*rxnotify_cb)(void *arg), void *arg);
375 
385  void (*default_rxdata_cb)(void *data, int size, int port, void *arg), void *arg);
386 
393 int cb_lld_get_type_instance(uint32_t *enet_type, uint32_t *instance_id);
394 
401 int cb_lld_get_netdevs(char* netdevs[], int *len);
402 
411 int cb_lld_get_link_state(CB_SOCKET_T cfd, const char *dev, uint32_t *linkstate);
412 
422 int cb_lld_get_link_info(CB_SOCKET_T cfd, const char *dev, uint32_t *speed, uint32_t *duplex);
423 
433 
441 
442 #ifdef __cplusplus
443 }
444 #endif
445 #endif //CB_LLD_ETHERNET_H
446 
ethhdr::h_dest
uint8_t h_dest[ETH_ALEN]
Definition: cb_lld_ethernet.h:127
sockaddr::sll_addr
unsigned char sll_addr[6]
Definition: cb_lld_ethernet.h:145
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.
size
uint16_t size
Definition: tisci_boardcfg.h:1
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.
ethhdr::h_proto
uint16_t h_proto
Definition: cb_lld_ethernet.h:135
lld_ethdev_t
Structure representing the lld_ethdev instance.
Definition: cb_lld_ethernet.h:169
lldenet.h
TI Ethernet Low Level Driver abstract APIs.
cb_socket_lldcfg_update_t::dmaRxShared
int dmaRxShared
Definition: cb_lld_ethernet.h:243
cb_socket_lldcfg_update_t::dmaTxChId
int dmaTxChId
Definition: cb_lld_ethernet.h:222
cb_lld_recv_zerocopy
int cb_lld_recv_zerocopy(CB_SOCKET_T sfd, cb_lld_zerocopy_recv_cb_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_socket_t
struct lld_socket lld_socket_t
Structure representing the lld_socket instance.
Definition: cb_lld_ethernet.h:164
sockaddr::rxts
uint64_t rxts
Definition: cb_lld_ethernet.h:158
ethhdr
Ethernet header structure.
Definition: cb_lld_ethernet.h:123
data
uint32_t data
Definition: tisci_rm_psil.h:1
cb_lld_netdev_to_macport
int cb_lld_netdev_to_macport(const char *netdev)
Convert net device name to mac port.
addr
uint64_t addr
Definition: csl_udmap_tr.h:3
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.
sockaddr
Structure representing the lld_sockaddr instance.
Definition: cb_lld_ethernet.h:141
lld_ethdev_t::netdev
char * netdev
Definition: cb_lld_ethernet.h:173
cb_socket_lldcfg_update_t::rxDefaultCbArg
void * rxDefaultCbArg
Definition: cb_lld_ethernet.h:259
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:197
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()
ethhdr::h_source
uint8_t h_source[ETH_ALEN]
Definition: cb_lld_ethernet.h:131
CB_SOCKADDR_LL_T
#define CB_SOCKADDR_LL_T
Definition: cb_lld_ethernet.h:88
cb_socket_lldcfg_update_t::dmaRxOwner
int dmaRxOwner
Definition: cb_lld_ethernet.h:249
MAX_NUM_RX_DMA_CH_PER_INSTANCE
#define MAX_NUM_RX_DMA_CH_PER_INSTANCE
Definition: lldtype.h:114
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:278
cb_socket_lldcfg_update_t::pktSize
uint32_t pktSize
Definition: cb_lld_ethernet.h:217
cb_tilld_port_stats_t
Definition: cb_lld_ethernet.h:268
cb_socket_lldcfg_update_t::nTxPkts
uint32_t nTxPkts
Definition: cb_lld_ethernet.h:207
__attribute__
struct sockaddr __attribute__
CB_TILLD_STATS_BLOCK_ELEM_NUM
#define CB_TILLD_STATS_BLOCK_ELEM_NUM
Definition: cb_lld_ethernet.h:267
lld_ethdev_t::macport
uint8_t macport
Definition: cb_lld_ethernet.h:177
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_init_devs_table
int cb_lld_init_devs_table(lld_ethdev_t *ethdevs, uint32_t ndevs, uint32_t enet_type, uint32_t instance_id)
Initialize a LLD device table.
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:187
cb_socket_lldcfg_update_t::unusedDmaTx
int unusedDmaTx
Definition: cb_lld_ethernet.h:237
ETH_ALEN
#define ETH_ALEN
Definition: cb_lld_ethernet.h:104
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.
sockaddr::macport
int macport
Definition: cb_lld_ethernet.h:149
cb_lld_zerocopy_recv_cb_t
void(* cb_lld_zerocopy_recv_cb_t)(void *buf, int size, CB_SOCKADDR_LL_T *addr, void *cbarg)
Definition: cb_lld_ethernet.h:343
cb_socket_lldcfg_update_t::vlanid
uint16_t vlanid
Definition: cb_lld_ethernet.h:201
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.
flags
uint32_t flags
Definition: csl_udmap_tr.h:0
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:192
cb_socket_lldcfg_update_t::numRxChannels
uint32_t numRxChannels
Definition: cb_lld_ethernet.h:263
CB_SOCKET_T
#define CB_SOCKET_T
Definition: cb_lld_ethernet.h:86
sockaddr::tcid
int tcid
Definition: cb_lld_ethernet.h:154
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'
cb_socket_lldcfg_update_t::unusedDmaRx
int unusedDmaRx
Definition: cb_lld_ethernet.h:232