Ethernet controller class.
More...
|
| #define | PBL_ETH_ADDR_LEN 6U |
| | Length of a MAC address.
|
| |
| #define | PBL_ETH_HDR_LEN 14U |
| | Length of the Ethernet header: destination, source and EtherType.
|
| |
| #define | PBL_ETH_MTU 1500U |
| | Largest payload of a frame.
|
| |
| #define | PBL_ETH_FRAME_MAX (PBL_ETH_HDR_LEN + PBL_ETH_MTU) |
| | Largest frame, without the FCS.
|
| |
| #define | PBL_ETH_FILTER_PROMISC PBL_BIT(0) |
| | Receive every frame, whatever its destination.
|
| |
| #define | PBL_ETH_FILTER_ALLMULTI PBL_BIT(1) |
| | Receive every multicast frame.
|
| |
| #define | PBL_ETH_CAP_TX_CSUM PBL_BIT(0) |
| | The controller inserts the checksums of IPv4 and IPv6 packets without extension headers: the IPv4 header checksum and the TCP, UDP, ICMP or ICMPv6 checksum, of frames sent with PBL_ETH_TX_CSUM.
|
| |
| #define | PBL_ETH_TX_CSUM PBL_BIT(0) |
| | Insert the checksums of the frame, an IPv4 or IPv6 packet as PBL_ETH_CAP_TX_CSUM says, whose checksum fields are zero.
|
| |
| #define | PBL_ETH_STATE_DEFINE(sym) |
| | Define the runtime state of an Ethernet controller.
|
| |
| #define | PBL_ETH_INIT(sym, _name, _parent, _ops, _phy, _addr) |
| | Initialize a struct pbl_eth.
|
| |
|
| void | pbl_eth_set_callbacks (const struct pbl_eth *eth, const struct pbl_eth_callbacks *cb, void *arg) |
| | Set what is done with what the controller reports.
|
| |
| int | pbl_eth_start (const struct pbl_eth *eth) |
| | Start a controller and its PHY.
|
| |
| int | pbl_eth_stop (const struct pbl_eth *eth) |
| | Stop a controller and power its PHY down.
|
| |
| int | pbl_eth_send (const struct pbl_eth *eth, const struct pbl_eth_buf *bufs, size_t num_bufs, uint32_t flags) |
| | Send a frame.
|
| |
| uint32_t | pbl_eth_get_caps (const struct pbl_eth *eth) |
| | Get the offloads of a controller.
|
| |
| int | pbl_eth_recv (const struct pbl_eth *eth, void *buf, size_t len) |
| | Receive a frame.
|
| |
| int | pbl_eth_set_filter (const struct pbl_eth *eth, uint32_t flags, const uint8_t(*mcast)[PBL_ETH_ADDR_LEN], size_t num_mcast) |
| | Choose the frames to receive.
|
| |
| void | pbl_eth_get_addr (const struct pbl_eth *eth, uint8_t addr[PBL_ETH_ADDR_LEN]) |
| | Get the MAC address.
|
| |
| bool | pbl_eth_link_is_up (const struct pbl_eth *eth) |
| | Check whether the link is up.
|
| |
| void | pbl_eth_report_rx (const struct pbl_eth *eth) |
| | Report received frames waiting.
|
| |
| void | pbl_eth_report_tx (const struct pbl_eth *eth) |
| | Report room to send coming back.
|
| |
|
| const struct pbl_eth *const | ETHERNET |
| | The board's Ethernet controller, from its ethernet role.
|
| |
Ethernet controller class.
An Ethernet MAC with its PHY. Frames go in and out by copy: pbl_eth_send() gathers a frame into the controller's own buffers and pbl_eth_recv() copies the next received one out, so the controller's DMA memory never leaves the driver. Frames exclude the preamble and the FCS, which the controller adds and checks: a frame starts with the destination address and is at most PBL_ETH_FRAME_MAX bytes. The controller pads short frames.
A controller may offload work, as its capabilities say: inserting the checksums of frames sent with PBL_ETH_TX_CSUM, for instance.
Received frames are checked by the driver before they are handed out: those with a receive error, a bad FCS, a length out of range or spread over several buffers are dropped.
The user sets the callbacks with pbl_eth_set_callbacks() before pbl_eth_start(). The controller says frames are waiting, or room to send came back, from its interrupt: the user then calls pbl_eth_recv() until it returns -EAGAIN, or retries pbl_eth_send(), from thread context. The link coming up or going down is reported from thread context, once the controller matches the speed and duplex of the PHY.
The MAC address is the one the driver gives, or else is derived from the microcontroller's unique ID, as a locally administered address that stays the same across boots.
...
while ((len =
pbl_eth_recv(eth, frame,
sizeof(frame))) >= 0) {
handle(frame, len);
}
int pbl_eth_recv(const struct pbl_eth *eth, void *buf, size_t len)
Receive a frame.
void pbl_eth_set_callbacks(const struct pbl_eth *eth, const struct pbl_eth_callbacks *cb, void *arg)
Set what is done with what the controller reports.
int pbl_eth_start(const struct pbl_eth *eth)
Start a controller and its PHY.
◆ pbl_eth_buf
A piece of a frame to send.
| Data Fields |
|
const void * |
data |
Bytes. |
|
size_t |
len |
Number of bytes. |
◆ pbl_eth
| Data Fields |
|
const uint8_t * |
addr |
MAC address, or NULL to derive one. |
|
struct pbl_device |
dev |
Device. |
|
const struct pbl_eth_ops * |
ops |
Driver operations. |
|
const struct pbl_net_phy * |
phy |
PHY. |
|
struct pbl_eth_state * |
state |
Runtime state. |
◆ PBL_ETH_ADDR_LEN
| #define PBL_ETH_ADDR_LEN 6U |
◆ PBL_ETH_CAP_TX_CSUM
| #define PBL_ETH_CAP_TX_CSUM PBL_BIT(0) |
The controller inserts the checksums of IPv4 and IPv6 packets without extension headers: the IPv4 header checksum and the TCP, UDP, ICMP or ICMPv6 checksum, of frames sent with PBL_ETH_TX_CSUM.
◆ PBL_ETH_FILTER_ALLMULTI
| #define PBL_ETH_FILTER_ALLMULTI PBL_BIT(1) |
Receive every multicast frame.
◆ PBL_ETH_FILTER_PROMISC
| #define PBL_ETH_FILTER_PROMISC PBL_BIT(0) |
Receive every frame, whatever its destination.
◆ PBL_ETH_FRAME_MAX
Largest frame, without the FCS.
◆ PBL_ETH_HDR_LEN
| #define PBL_ETH_HDR_LEN 14U |
Length of the Ethernet header: destination, source and EtherType.
◆ PBL_ETH_INIT
| #define PBL_ETH_INIT |
( |
|
sym, |
|
|
|
_name, |
|
|
|
_parent, |
|
|
|
_ops, |
|
|
|
_phy, |
|
|
|
_addr |
|
) |
| |
Value: { \
.ops = (_ops), \
.state = &sym##_eth_state, \
.phy = (_phy), \
.addr = (_addr), \
}
#define PBL_DEVICE_INIT_CLASS(sym, _name, _init, _parent, _cls)
Initializer for the struct pbl_device of device instance sym, in class _cls.
Definition device.h:153
Initialize a struct pbl_eth.
- Parameters
-
| sym | Symbol of the instance, as passed to PBL_ETH_STATE_DEFINE(). |
| _name | Device name. |
| _parent | Parent device, or NULL. |
| _ops | Driver operations. |
| _phy | PHY. |
| _addr | MAC address, an array of PBL_ETH_ADDR_LEN bytes, or NULL to derive one. |
◆ PBL_ETH_MTU
| #define PBL_ETH_MTU 1500U |
Largest payload of a frame.
◆ PBL_ETH_STATE_DEFINE
| #define PBL_ETH_STATE_DEFINE |
( |
|
sym | ) |
|
Value:
static struct pbl_eth_state sym##_eth_state
#define PBL_DEVICE_STATE_DEFINE(sym)
Define the runtime state of device instance sym.
Definition device.h:132
Define the runtime state of an Ethernet controller.
- Parameters
-
| sym | Symbol of the instance. |
◆ PBL_ETH_TX_CSUM
Insert the checksums of the frame, an IPv4 or IPv6 packet as PBL_ETH_CAP_TX_CSUM says, whose checksum fields are zero.
◆ pbl_eth_get_addr()
| void pbl_eth_get_addr |
( |
const struct pbl_eth * |
eth, |
|
|
uint8_t |
addr[PBL_ETH_ADDR_LEN] |
|
) |
| |
Get the MAC address.
- Parameters
-
| eth | Ethernet controller. |
| [out] | addr | MAC address. |
◆ pbl_eth_get_caps()
| uint32_t pbl_eth_get_caps |
( |
const struct pbl_eth * |
eth | ) |
|
Get the offloads of a controller.
- Parameters
-
- Returns
- PBL_ETH_CAP_* flags.
◆ pbl_eth_link_is_up()
| bool pbl_eth_link_is_up |
( |
const struct pbl_eth * |
eth | ) |
|
Check whether the link is up.
- Parameters
-
- Returns
- true while started with the link up.
◆ pbl_eth_recv()
| int pbl_eth_recv |
( |
const struct pbl_eth * |
eth, |
|
|
void * |
buf, |
|
|
size_t |
len |
|
) |
| |
Receive a frame.
- Parameters
-
| eth | Ethernet controller. |
| buf | Buffer, PBL_ETH_FRAME_MAX bytes to take any frame. |
| len | Size of buf. |
- Returns
- Length of the frame,
-EAGAIN without one, -EMSGSIZE if it did not fit and was dropped, -ENETDOWN while stopped, or another negative errno.
◆ pbl_eth_report_rx()
| void pbl_eth_report_rx |
( |
const struct pbl_eth * |
eth | ) |
|
Report received frames waiting.
For drivers, from their interrupt.
- Parameters
-
◆ pbl_eth_report_tx()
| void pbl_eth_report_tx |
( |
const struct pbl_eth * |
eth | ) |
|
Report room to send coming back.
For drivers, from their interrupt.
- Parameters
-
◆ pbl_eth_send()
| int pbl_eth_send |
( |
const struct pbl_eth * |
eth, |
|
|
const struct pbl_eth_buf * |
bufs, |
|
|
size_t |
num_bufs, |
|
|
uint32_t |
flags |
|
) |
| |
Send a frame.
- Parameters
-
| eth | Ethernet controller. |
| bufs | Pieces of the frame, which may be released on return. |
| num_bufs | Number of pieces. |
| flags | PBL_ETH_TX_* flags. |
- Returns
- 0,
-EMSGSIZE for a frame shorter than the header or longer than PBL_ETH_FRAME_MAX, -ENOTSUP for a flag the controller has no capability for, -ENETDOWN while stopped or without link, -ENOBUFS while the controller has no room, or another negative errno.
◆ pbl_eth_set_callbacks()
Set what is done with what the controller reports.
- Parameters
-
| eth | Ethernet controller, stopped. |
| cb | Callbacks, which stay in use. |
| arg | Argument of the callbacks. |
◆ pbl_eth_set_filter()
| int pbl_eth_set_filter |
( |
const struct pbl_eth * |
eth, |
|
|
uint32_t |
flags, |
|
|
const uint8_t(*) |
mcast[PBL_ETH_ADDR_LEN], |
|
|
size_t |
num_mcast |
|
) |
| |
Choose the frames to receive.
- Parameters
-
| eth | Ethernet controller. |
| flags | PBL_ETH_FILTER_* flags. |
| mcast | Multicast addresses to receive. |
| num_mcast | Number of multicast addresses. |
- Returns
- 0,
-EINVAL for an address that is not multicast, or another negative errno.
◆ pbl_eth_start()
| int pbl_eth_start |
( |
const struct pbl_eth * |
eth | ) |
|
Start a controller and its PHY.
- Parameters
-
- Returns
- 0,
-EALREADY if started, or another negative errno.
◆ pbl_eth_stop()
| int pbl_eth_stop |
( |
const struct pbl_eth * |
eth | ) |
|
Stop a controller and power its PHY down.
A link that was up is reported down.
- Parameters
-
- Returns
- 0,
-EALREADY if stopped, or another negative errno.
◆ ETHERNET
| const struct pbl_eth* const ETHERNET |
|
extern |
The board's Ethernet controller, from its ethernet role.