Add documentation for the RPMSG Based Virtual Ethernet Driver (rpmsg-eth). The documentation describes the driver's architecture, shared memory layout, RPMSG communication protocol, and requirements for vendor firmware to interoperate with the driver. It details the use of a magic number for shared memory validation, outlines the information exchanged between the host and remote processor, and provides a how-to guide for vendors to implement compatible firmware. Signed-off-by: MD Danish Anwar --- .../device_drivers/ethernet/index.rst | 1 + .../device_drivers/ethernet/rpmsg_eth.rst | 424 ++++++++++++++++++ 2 files changed, 425 insertions(+) create mode 100644 Documentation/networking/device_drivers/ethernet/rpmsg_eth.rst diff --git a/Documentation/networking/device_drivers/ethernet/index.rst b/Documentation/networking/device_drivers/ethernet/index.rst index 0b0a3eef6aae..20513a595af1 100644 --- a/Documentation/networking/device_drivers/ethernet/index.rst +++ b/Documentation/networking/device_drivers/ethernet/index.rst @@ -51,6 +51,7 @@ Contents: netronome/nfp pensando/ionic qualcomm/ppe/ppe + rpmsg_eth smsc/smc9 stmicro/stmmac ti/cpsw diff --git a/Documentation/networking/device_drivers/ethernet/rpmsg_eth.rst b/Documentation/networking/device_drivers/ethernet/rpmsg_eth.rst new file mode 100644 index 000000000000..7a9d94291de4 --- /dev/null +++ b/Documentation/networking/device_drivers/ethernet/rpmsg_eth.rst @@ -0,0 +1,424 @@ +.. SPDX-License-Identifier: GPL-2.0 + +=================================== +RPMSG Based Virtual Ethernet Driver +=================================== + +Overview +======== + +The RPMSG Based Virtual Ethernet Driver provides a virtual Ethernet interface for +communication between a host processor and a remote processor using the RPMSG +framework. This driver enables Ethernet-like packet transmission and reception +over shared memory, facilitating inter-core communication in systems with +heterogeneous processors. + +The driver is designed to work with the RPMSG framework, which is part of the +Linux Remote Processor (remoteproc) subsystem. It uses shared memory for data +exchange and supports features like multicast address management, dynamic MAC +address assignment, and efficient packet processing using NAPI. + +This driver is generic and can be used by any vendor. Vendors can develop their +own firmware for the remote processor to make it compatible with this driver. +The firmware must adhere to the shared memory layout, RPMSG communication +protocol, and data exchange requirements described in this documentation. + +Naming Convention +================= + +Throughout this documentation and in the driver implementation, the following naming +convention is used to describe the direction of communication: + +- **Firmware**: The firmware / RTOS binary running on the remote core. This takes the primary role. +- **Driver**: The Linux driver running on the host core. This takes the secondary role. + +This convention is important for understanding the data flow and the responsibilities +of each side in the communication channel. + +Key Features +============ + +- Virtual Ethernet interface using RPMSG. +- Shared memory-based packet transmission and reception. +- Support for multicast address management. +- Dynamic MAC address assignment. +- NAPI (New API) support for efficient packet processing. +- State machine for managing interface states. +- Workqueue-based asynchronous operations. +- Support for notifications and responses from the firmware. + +Magic Number +============ + +A **magic number** is used in the shared memory layout to validate that the +memory region is correctly initialized and accessible by both driver and the +firmware. This value is a unique constant ``0xABCDABCD`` that is written to +specific locations (such as the head and tail structures) in the shared memory +by the firmware and checked by the driver during the handshake process. + +Purpose of the Magic Number +--------------------------- + +- **Validation:** Ensures that the shared memory region has been properly set up + and is not corrupted or uninitialized. +- **Synchronization:** Both driver and firmware must agree on the magic number + value, which helps detect mismatches in memory layout or protocol version. +- **Error Detection:** If the driver detects an incorrect magic number during + initialization or runtime, it can abort the handshake and report an error, + preventing undefined behavior. + +Implementation Details +---------------------- + +- The magic number is defined as a macro in the driver source (e.g., + ``#define RPMSG_ETH_SHM_MAGIC_NUM 0xABCDABCD``). +- The firmware must write this value to the ``magic_num`` field of the head and + tail structures in the shared memory region. +- During the handshake, the Linux driver reads these fields and compares them to + the expected value. If any mismatch is detected, the driver will log an error + and refuse to proceed. + +Example Usage in Shared Memory +------------------------------ + +.. code-block:: text + + Shared Memory Layout: + --------------------------- + | MAGIC_NUM (0xABCDABCD) | <-- rpmsg_eth_shm_head + | HEAD | + --------------------------- + | MAGIC_NUM (0xABCDABCD) | <-- rpmsg_eth_shm_tail + | TAIL | + --------------------------- + +The magic number must be present in both the head and tail structures for the +handshake to succeed. + +Firmware developers must ensure that the correct magic number is written to the +appropriate locations in shared memory before the Linux driver attempts to +initialize the interface. + +Shared Memory Layout +==================== + +The RPMSG Based Virtual Ethernet Driver uses a shared memory region to exchange +data between driver and firmware. The shared memory is divided into transmit +and receive regions, each with its own `head` and `tail` indices to track the +buffer state. The base address of this shared memory is configured in the +device tree. See :ref:`Configuration ` for details. + +Shared Memory Parameters +------------------------ + +The following parameters are exchanged between the driver and the firmware to +configure the shared memory layout: + +1. **num_pkt_bufs**: + + - The total number of packet buffers available in the shared memory. + - This determines the maximum number of packets that can be stored in the + shared memory at any given time. + +2. **buff_slot_size**: + + - The size of each buffer slot in the shared memory. + - This includes space for the packet length, metadata, and the actual packet + data. + +3. **tx_offset**: + + - The offset from the `base_addr` where the transmit buffers begin. + - This is used by driver to write packets for transmission. + +4. **rx_offset**: + + - The offset from the `base_addr` where the receive buffers begin. + - This is used by driver to read packets received from the firmware. + +Shared Memory Structure +----------------------- + +The shared memory layout is as follows: + +.. code-block:: text + + Shared Memory Layout: + --------------------------- + | MAGIC_NUM | rpmsg_eth_shm_head + | HEAD_IDX | + --------------------------- + | MAGIC_NUM | + | PKT_1_LEN | + | PKT_1 | + --------------------------- + | ... | + --------------------------- + | MAGIC_NUM | + | TAIL_IDX | rpmsg_eth_shm_tail + --------------------------- + +1. **MAGIC_NUM**: + + - A unique identifier used to validate the shared memory region. + - Ensures that the memory region is correctly initialized and accessible. + +2. **HEAD Index**: + + - Tracks the start of the buffer for packet transmission or reception. + - Updated by the producer (Driver or firmware) after writing a packet. + +3. **TAIL Index**: + + - Tracks the end of the buffer for packet transmission or reception. + - Updated by the consumer (Driver or firmware) after reading a packet. + +4. **Packet Buffers**: + + - Each packet buffer contains: + + - **Packet Length**: A 4-byte field indicating the size of the packet. + - **Packet Data**: The actual Ethernet frame data. + +5. **Buffer Size**: + + - Each buffer has a fixed size defined by `RPMSG_ETH_BUFFER_SIZE`, which + includes space for the packet length and data. + +Buffer Management +----------------- + +- The driver and firmware use a circular buffer mechanism to manage the shared + memory. +- The `head` and `tail` indices are used to determine the number of packets + available for processing: + + .. code-block:: c + + num_pkts = head - tail; + num_pkts = num_pkts >= 0 ? num_pkts : (num_pkts + max_buffers); + +- The producer writes packets to the buffer and increments the `head` index. +- The consumer reads packets from the buffer and increments the `tail` index. + +RPMSG Communication +=================== + +The driver uses RPMSG channels to exchange control messages with the firmware. +These messages are used to manage the state of the Ethernet interface, +configure settings, notify events, and exchange runtime information. + +Information Exchanged Between RPMSG Channels +-------------------------------------------- + +1. **Requests from Driver to Firmware**: + + - `RPMSG_ETH_REQ_SHM_INFO`: Request shared memory information, such as + ``num_pkt_bufs``, ``buff_slot_size``, ``tx_offset``, and + ``rx_offset``. + - `RPMSG_ETH_REQ_SET_MAC_ADDR`: Set the MAC address of the Ethernet + interface. + - `RPMSG_ETH_REQ_ADD_MC_ADDR`: Add a multicast address to the firmware's + filter list. + - `RPMSG_ETH_REQ_DEL_MC_ADDR`: Remove a multicast address from firmware's + filter list. + +2. **Responses from Firmware to Driver**: + + - `RPMSG_ETH_RESP_SET_MAC_ADDR`: Acknowledge the MAC address configuration. + - `RPMSG_ETH_RESP_ADD_MC_ADDR`: Acknowledge the addition of a multicast + address. + - `RPMSG_ETH_RESP_DEL_MC_ADDR`: Acknowledge the removal of a multicast + address. + - `RPMSG_ETH_RESP_SHM_INFO`: Respond with shared memory information such as + ``num_pkt_bufs``, ``buff_slot_size``, ``tx_offset``, and + ``rx_offset``. + +3. **Notifications from Firmware to Driver**: + + - `RPMSG_ETH_NOTIFY_PORT_UP`: Notify that the Ethernet port is up and ready + for communication. + - `RPMSG_ETH_NOTIFY_PORT_DOWN`: Notify that the Ethernet port is down. + - `RPMSG_ETH_NOTIFY_REMOTE_READY`: Notify that the firmware is ready for + communication. + +4. **Runtime Information Exchanged**: + + - **Link State**: Notifications about link state changes (e.g., link up or + link down). + - **Statistics**: Runtime statistics such as transmitted/received packets, + errors, and dropped packets. + - **Error Notifications**: Notifications about errors like buffer overflows + or invalid packets. + - **Configuration Updates**: Notifications about changes in configuration, + such as updated MTU or VLAN settings. + +How-To Guide for Vendors +======================== + +This section provides a guide for vendors to develop firmware for the remote +processor that is compatible with the RPMSG Based Virtual Ethernet Driver. + +1. **Implement Shared Memory Layout**: + + - Allocate a shared memory region for packet transmission and reception. + - Initialize the `MAGIC_NUM`, `num_pkt_bufs`, `buff_slot_size`, `tx_offset`, + and `rx_offset`. + +2. **Magic Number Requirements** + + - The firmware must write a unique magic number ``0xABCDABCD`` to the + `magic_num` field of both the head and tail structures in the shared + memory region. + - This magic number is used by the Linux driver to validate that the shared + memory region is correctly initialized and accessible. + - If the driver detects an incorrect magic number during the handshake, it + will abort initialization and report an error. + - Vendors must ensure the magic number matches the value expected by the + Linux driver, see the `RPMSG_ETH_SHM_MAGIC_NUM` macro in the driver + source. + +3. **Handle RPMSG Requests**: + + - Implement handlers for the following RPMSG requests: + + - `RPMSG_ETH_REQ_SHM_INFO` + - `RPMSG_ETH_REQ_SET_MAC_ADDR` + - `RPMSG_ETH_REQ_ADD_MC_ADDR` + - `RPMSG_ETH_REQ_DEL_MC_ADDR` + +4. **Send RPMSG Notifications**: + + - Notify the Driver about the state of the Ethernet interface using the + notifications described above. + +5. **Send Runtime Information**: + + - Implement mechanisms to send runtime information such as link state + changes, statistics, and error notifications. + +6. **Implement Packet Processing**: + + - Process packets in the shared memory transmit and receive buffers. + +7. **Test the Firmware**: + + - Use the RPMSG Based Virtual Ethernet Driver on the host to test packet + transmission and reception. + +.. _rpmsg_config: + +Configuration +============= + +The driver relies on the device tree for configuration. The shared memory +region need to be specified in the remote processor device's "memory-region". + +Example Device Tree Node +------------------------ +Here is an example of how the device tree node might look: + +.. code-block:: dts + + { + compatible = "shared-dma-pool"; + reg = ; + }; + + { + memory-region = <&>; + }; + +In this example, ```` is the remote processor device node, and +```` is the shared memory region node. The remote processor +device references the shared memory region node using the ``memory-region`` +property. + +Vendors can create their own ```` and add it to their remote +processor device node i.e. ```` + +Driver Configuration +-------------------- + +Vendors need to configure the driver as well by adding a new entry of type +:c:type:`rpmsg_device_id` in the array ``rpmsg_eth_id_table``. + +The :c:type:`rpmsg_device_id` structure contains two members: + +* :c:member:`name` - a string that identifies the RPMsg device +* :c:member:`driver_data` - a pointer to a :c:type:`rpmsg_eth_data` structure + +Overview of rpmsg_eth_data +~~~~~~~~~~~~~~~~~~~~~~~~~~ + +The ``rpmsg_eth_data`` structure is a vendor-specific configuration structure +that provides customization options for the RPMsg Ethernet driver. + +.. code-block:: c + + /** + * struct rpmsg_eth_data - RPMSG ETH device data + * @shm_region_index: Shared memory region index + */ + struct rpmsg_eth_data { + u8 shm_region_index; + }; + +Currently, the structure contains a single field, but it is designed to be +extensible for future enhancements. + +The shm_region_index Field +~~~~~~~~~~~~~~~~~~~~~~~~~~ + +The ``shm_region_index`` field is an 8-bit unsigned integer within the +``rpmsg_eth_data`` structure that specifies which memory-region phandle to use +from the device tree for the RPMsg Ethernet shared memory. + +This field is used in the ``rpmsg_eth_get_shm_info`` function to retrieve the +correct shared memory region from the device tree using the +``of_parse_phandle`` function: + +.. code-block:: c + + rmem_np = of_parse_phandle(np, "memory-region", common->data.shm_region_index); + +The ``shm_region_index`` indicates which memory region to use when multiple +memory regions are defined in the remote processor device tree node. + +Example +~~~~~~~ + +The following example shows how to create a custom RPMsg device ID: + +.. code-block:: c + + static const struct rpmsg_eth_data my_rpmsg_eth_data = { + .shm_region_index = 2, + }; + + static struct rpmsg_device_id my_rpmsg_eth_id_table[] = { + { .name = "my.shm-eth", .driver_data = (kernel_ulong_t)&my_rpmsg_eth_data }, + {}, + }; + +Limitations +=========== + +- The driver assumes a specific shared memory layout and may not work with other + configurations. +- The driver currently supports only one transmit and one receive queue. +- The current implementation only supports Linux driver running on the "host" + core as secondary and firmware running on the "remote" core as primary. It + does not support Linux-to-Linux communication where Linux driver would run on + both ends. + +References +========== + +- :doc:`RPMSG Framework documentation ` +- :doc:`Network device documentation ` + +Authors +======= + +- MD Danish Anwar -- 2.34.1 Introduces a basic RPMSG Ethernet driver and add it's basic skeleton. Add support for creating virtual Ethernet devices over RPMSG channels, allowing user-space programs to send and receive messages using a standard Ethernet protocol. The driver includes message handling, probe, and remove functions, along with necessary data structures. Signed-off-by: MD Danish Anwar --- drivers/net/ethernet/Kconfig | 11 +++ drivers/net/ethernet/Makefile | 1 + drivers/net/ethernet/rpmsg_eth.c | 144 +++++++++++++++++++++++++++++++ drivers/net/ethernet/rpmsg_eth.h | 85 ++++++++++++++++++ 4 files changed, 241 insertions(+) create mode 100644 drivers/net/ethernet/rpmsg_eth.c create mode 100644 drivers/net/ethernet/rpmsg_eth.h diff --git a/drivers/net/ethernet/Kconfig b/drivers/net/ethernet/Kconfig index f86d4557d8d7..7d00e02a2c8f 100644 --- a/drivers/net/ethernet/Kconfig +++ b/drivers/net/ethernet/Kconfig @@ -170,6 +170,17 @@ config OA_TC6 To know the implementation details, refer documentation in . +config RPMSG_ETH + tristate "RPMsg Based Virtual Ethernet driver" + depends on RPMSG + depends on REMOTEPROC + help + This makes it possible for user-space programs to send and receive + rpmsg messages as a standard eth protocol. + + To compile this driver as a module, choose M here: the module will be + called rpmsg_eth. + source "drivers/net/ethernet/packetengines/Kconfig" source "drivers/net/ethernet/pasemi/Kconfig" source "drivers/net/ethernet/pensando/Kconfig" diff --git a/drivers/net/ethernet/Makefile b/drivers/net/ethernet/Makefile index 67182339469a..aebd15993e3c 100644 --- a/drivers/net/ethernet/Makefile +++ b/drivers/net/ethernet/Makefile @@ -107,3 +107,4 @@ obj-$(CONFIG_NET_VENDOR_XIRCOM) += xircom/ obj-$(CONFIG_NET_VENDOR_SYNOPSYS) += synopsys/ obj-$(CONFIG_NET_VENDOR_PENSANDO) += pensando/ obj-$(CONFIG_OA_TC6) += oa_tc6.o +obj-$(CONFIG_RPMSG_ETH) += rpmsg_eth.o diff --git a/drivers/net/ethernet/rpmsg_eth.c b/drivers/net/ethernet/rpmsg_eth.c new file mode 100644 index 000000000000..7224fbc89646 --- /dev/null +++ b/drivers/net/ethernet/rpmsg_eth.c @@ -0,0 +1,144 @@ +// SPDX-License-Identifier: GPL-2.0 +/* RPMsg Based Virtual Ethernet Driver + * + * Copyright (C) 2025 Texas Instruments Incorporated - https://www.ti.com/ + */ + +#include +#include +#include +#include + +#include "rpmsg_eth.h" + +static int rpmsg_eth_rpmsg_cb(struct rpmsg_device *rpdev, void *data, int len, + void *priv, u32 src) +{ + struct rpmsg_eth_common *common = dev_get_drvdata(&rpdev->dev); + struct message *msg = (struct message *)data; + u32 msg_type = msg->msg_hdr.msg_type; + int ret = 0; + + switch (msg_type) { + case RPMSG_ETH_REQUEST_MSG: + case RPMSG_ETH_RESPONSE_MSG: + case RPMSG_ETH_NOTIFY_MSG: + dev_dbg(common->dev, "Msg type = %d, Src Id = %d\n", + msg_type, msg->msg_hdr.src_id); + break; + default: + dev_err(common->dev, "Invalid msg type\n"); + ret = -EINVAL; + break; + } + return ret; +} + +/** + * rpmsg_eth_get_shm_info - Retrieve shared memory region for RPMsg Ethernet + * @common: Pointer to rpmsg_eth_common structure + * + * This function locates and maps the reserved memory region for the RPMsg + * Ethernet device by traversing the device tree hierarchy. It first identifies + * the associated remote processor (rproc), then locates the "rpmsg-eth" child + * node within the rproc's device tree node, and finally retrieves the + * "memory-region" phandle that points to the reserved memory region. + * Once found, the shared memory region is mapped into the + * kernel's virtual address space using devm_ioremap() + * + * Return: 0 on success, negative error code on failure. + */ +static int rpmsg_eth_get_shm_info(struct rpmsg_eth_common *common) +{ + struct device_node *np, *rmem_np; + struct reserved_mem *rmem; + struct rproc *rproc; + + /* Get the remote processor associated with this device */ + rproc = rproc_get_by_child(&common->rpdev->dev); + if (!rproc) { + dev_err(common->dev, "rpmsg eth device not child of rproc\n"); + return -EINVAL; + } + + /* Get the device node from rproc or its parent */ + np = rproc->dev.of_node ?: (rproc->dev.parent ? rproc->dev.parent->of_node : NULL); + if (!np) { + dev_err(common->dev, "Cannot find rproc device node\n"); + return -ENODEV; + } + + /* Parse the memory-region phandle */ + rmem_np = of_parse_phandle(np, "memory-region", common->data.shm_region_index); + of_node_put(np); + if (!rmem_np) { + dev_err(common->dev, "Cannot find shared memory region\n"); + return -EINVAL; + } + + /* Lookup the reserved memory region */ + rmem = of_reserved_mem_lookup(rmem_np); + of_node_put(rmem_np); + if (!rmem) + return -EINVAL; + + common->port->shm = devm_ioremap(common->dev, rmem->base, rmem->size); + if (IS_ERR(common->port->shm)) + return PTR_ERR(common->port->shm); + + common->port->buf_size = rmem->size; + + return 0; +} + +static int rpmsg_eth_probe(struct rpmsg_device *rpdev) +{ + struct device *dev = &rpdev->dev; + struct rpmsg_eth_common *common; + int ret = 0; + + common = devm_kzalloc(&rpdev->dev, sizeof(*common), GFP_KERNEL); + if (!common) + return -ENOMEM; + + dev_set_drvdata(dev, common); + + common->port = devm_kzalloc(dev, sizeof(*common->port), GFP_KERNEL); + common->dev = dev; + common->rpdev = rpdev; + common->data = *(const struct rpmsg_eth_data *)rpdev->id.driver_data; + + ret = rpmsg_eth_get_shm_info(common); + if (ret) + return ret; + + return 0; +} + +static void rpmsg_eth_remove(struct rpmsg_device *rpdev) +{ + dev_dbg(&rpdev->dev, "rpmsg-eth client driver is removed\n"); +} + +static const struct rpmsg_eth_data ti_rpmsg_eth_data = { + .shm_region_index = 2, +}; + +static struct rpmsg_device_id rpmsg_eth_id_table[] = { + { .name = "ti.shm-eth", .driver_data = (kernel_ulong_t)&ti_rpmsg_eth_data }, + {}, +}; +MODULE_DEVICE_TABLE(rpmsg, rpmsg_eth_id_table); + +static struct rpmsg_driver rpmsg_eth_rpmsg_client = { + .drv.name = KBUILD_MODNAME, + .id_table = rpmsg_eth_id_table, + .probe = rpmsg_eth_probe, + .callback = rpmsg_eth_rpmsg_cb, + .remove = rpmsg_eth_remove, +}; +module_rpmsg_driver(rpmsg_eth_rpmsg_client); + +MODULE_LICENSE("GPL"); +MODULE_AUTHOR("MD Danish Anwar "); +MODULE_DESCRIPTION("RPMsg Based Virtual Ethernet driver"); diff --git a/drivers/net/ethernet/rpmsg_eth.h b/drivers/net/ethernet/rpmsg_eth.h new file mode 100644 index 000000000000..0d6f96f755eb --- /dev/null +++ b/drivers/net/ethernet/rpmsg_eth.h @@ -0,0 +1,85 @@ +/* SPDX-License-Identifier: GPL-2.0 + * RPMsg Based Virtual Ethernet Driver common header + * + * Copyright (C) 2025 Texas Instruments Incorporated - https://www.ti.com/ + */ + +#ifndef __RPMSG_ETH_H__ +#define __RPMSG_ETH_H__ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define RPMSG_ETH_SHM_MAGIC_NUM 0xABCDABCD + +enum rpmsg_eth_msg_type { + RPMSG_ETH_REQUEST_MSG = 0, + RPMSG_ETH_RESPONSE_MSG, + RPMSG_ETH_NOTIFY_MSG, +}; + +/** + * struct message_header - message header structure for RPMSG Ethernet + * @src_id: Source endpoint ID + * @msg_type: Message type + */ +struct message_header { + u32 src_id; + u32 msg_type; +} __packed; + +/** + * struct message - RPMSG Ethernet message structure + * + * @msg_hdr: Message header contains source and destination endpoint and + * the type of message + * + * This structure is used to send and receive messages between the RPMSG + * Ethernet ports. + */ +struct message { + struct message_header msg_hdr; +} __packed; + +/** + * struct rpmsg_eth_data - RPMSG ETH device data + * @shm_region_index: Shared memory region index + */ +struct rpmsg_eth_data { + u8 shm_region_index; +}; + +/** + * struct rpmsg_eth_common - common structure for RPMSG Ethernet + * @rpdev: RPMSG device + * @port: Ethernet port + * @dev: Device + * @data: Vendor specific data + */ +struct rpmsg_eth_common { + struct rpmsg_device *rpdev; + struct rpmsg_eth_port *port; + struct device *dev; + struct rpmsg_eth_data data; +}; + +/** + * struct rpmsg_eth_port - Ethernet port structure for RPMSG Ethernet + * @common: Pointer to the common RPMSG Ethernet structure + * @shm: Shared memory region mapping + * @buf_size: Size (in bytes) of the shared memory buffer for this port + */ +struct rpmsg_eth_port { + struct rpmsg_eth_common *common; + void __iomem *shm; + phys_addr_t buf_size; +}; + +#endif /* __RPMSG_ETH_H__ */ -- 2.34.1 Register the rpmsg-eth device as a netdev and enhance the rpmsg callback function to handle shared memory for tx and rx buffers. Introduce structures for shared memory layout, including head, buffer, and tail indices. Add initialization for the netdev, including setting up MAC address, MTU, and netdev operations. Allocate memory for tx and rx buffers and map shared memory regions. Update the probe function to initialize the netdev and set the device state. Add necessary headers, constants, and enums for shared memory and state management. Define shared memory layout and buffer structures for efficient data handling. Implement helper macros for accessing private data and shared memory buffers. Ensure proper error handling during memory allocation and device registration. Signed-off-by: MD Danish Anwar --- drivers/net/ethernet/rpmsg_eth.c | 132 ++++++++++++++++++++- drivers/net/ethernet/rpmsg_eth.h | 195 +++++++++++++++++++++++++++++++ 2 files changed, 325 insertions(+), 2 deletions(-) diff --git a/drivers/net/ethernet/rpmsg_eth.c b/drivers/net/ethernet/rpmsg_eth.c index 7224fbc89646..f599318633ea 100644 --- a/drivers/net/ethernet/rpmsg_eth.c +++ b/drivers/net/ethernet/rpmsg_eth.c @@ -11,20 +11,101 @@ #include "rpmsg_eth.h" +/** + * rpmsg_eth_validate_handshake - Validate handshake parameters from remote + * @port: Pointer to rpmsg_eth_port structure + * @shm_info: Pointer to shared memory info received from remote + * + * Checks buffer size, magic numbers, and TX/RX offsets in the handshake + * response to ensure they match expected values and are within valid ranges. + * + * Return: 0 on success, -EINVAL on validation failure. + */ +static int rpmsg_eth_validate_handshake(struct rpmsg_eth_port *port, + struct rpmsg_eth_shm *shm_info) +{ + u32 tx_head_magic_num, tx_tail_magic_num; + u32 rx_head_magic_num, rx_tail_magic_num; + + if (shm_info->buff_slot_size != RPMSG_ETH_BUFFER_SIZE) { + dev_err(port->common->dev, "Buffer configuration mismatch in handshake: expected_buf_size=%zu, received_buf_size=%d\n", + RPMSG_ETH_BUFFER_SIZE, + shm_info->buff_slot_size); + return -EINVAL; + } + + if (shm_info->tx_offset >= port->buf_size || + shm_info->rx_offset >= port->buf_size) { + dev_err(port->common->dev, "TX/RX offset out of range in handshake: tx_offset=0x%x, rx_offset=0x%x, size=0x%llx\n", + shm_info->tx_offset, + shm_info->rx_offset, + port->buf_size); + return -EINVAL; + } + + tx_head_magic_num = readl(port->shm + shm_info->tx_offset + + HEAD_MAGIC_NUM_OFFSET); + rx_head_magic_num = readl(port->shm + shm_info->rx_offset + + HEAD_MAGIC_NUM_OFFSET); + tx_tail_magic_num = readl(port->shm + shm_info->tx_offset + + TAIL_MAGIC_NUM_OFFSET(shm_info->num_pkt_bufs)); + rx_tail_magic_num = readl(port->shm + shm_info->rx_offset + + TAIL_MAGIC_NUM_OFFSET(shm_info->num_pkt_bufs)); + + if (tx_head_magic_num != RPMSG_ETH_SHM_MAGIC_NUM || + rx_head_magic_num != RPMSG_ETH_SHM_MAGIC_NUM || + tx_tail_magic_num != RPMSG_ETH_SHM_MAGIC_NUM || + rx_tail_magic_num != RPMSG_ETH_SHM_MAGIC_NUM) { + dev_err(port->common->dev, "Magic number mismatch in handshake at head/tail\n"); + return -EINVAL; + } + + return 0; +} + static int rpmsg_eth_rpmsg_cb(struct rpmsg_device *rpdev, void *data, int len, void *priv, u32 src) { struct rpmsg_eth_common *common = dev_get_drvdata(&rpdev->dev); struct message *msg = (struct message *)data; + struct rpmsg_eth_port *port = common->port; u32 msg_type = msg->msg_hdr.msg_type; + u32 rpmsg_type; int ret = 0; switch (msg_type) { case RPMSG_ETH_REQUEST_MSG: + rpmsg_type = msg->req_msg.type; + dev_dbg(common->dev, "Msg type = %d, RPMsg type = %d, Src Id = %d, Msg Id = %d\n", + msg_type, rpmsg_type, msg->msg_hdr.src_id, msg->req_msg.id); + break; case RPMSG_ETH_RESPONSE_MSG: + rpmsg_type = msg->resp_msg.type; + dev_dbg(common->dev, "Msg type = %d, RPMsg type = %d, Src Id = %d, Msg Id = %d\n", + msg_type, rpmsg_type, msg->msg_hdr.src_id, msg->resp_msg.id); + switch (rpmsg_type) { + case RPMSG_ETH_RESP_SHM_INFO: + /* Handshake validation */ + ret = rpmsg_eth_validate_handshake(port, &msg->resp_msg.shm_info); + if (ret) { + dev_err(common->dev, "RPMSG handshake failed %d\n", ret); + return ret; + } + + /* Retrieve Tx and Rx shared memory info from msg */ + port->tx_offset = msg->resp_msg.shm_info.tx_offset; + port->rx_offset = msg->resp_msg.shm_info.rx_offset; + port->tx_max_buffers = + msg->resp_msg.shm_info.num_pkt_bufs; + port->rx_max_buffers = + msg->resp_msg.shm_info.num_pkt_bufs; + break; + } + break; case RPMSG_ETH_NOTIFY_MSG: - dev_dbg(common->dev, "Msg type = %d, Src Id = %d\n", - msg_type, msg->msg_hdr.src_id); + rpmsg_type = msg->notify_msg.type; + dev_dbg(common->dev, "Msg type = %d, RPMsg type = %d, Src Id = %d, Msg Id = %d\n", + msg_type, rpmsg_type, msg->msg_hdr.src_id, msg->notify_msg.id); break; default: dev_err(common->dev, "Invalid msg type\n"); @@ -91,6 +172,47 @@ static int rpmsg_eth_get_shm_info(struct rpmsg_eth_common *common) return 0; } +static int rpmsg_eth_init_ndev(struct rpmsg_eth_common *common) +{ + struct device *dev = &common->rpdev->dev; + struct rpmsg_eth_ndev_priv *ndev_priv; + struct rpmsg_eth_port *port; + static u32 port_id; + int err = 0; + + port = common->port; + port->common = common; + port->port_id = port_id++; + + port->ndev = devm_alloc_etherdev_mqs(common->dev, sizeof(*ndev_priv), + RPMSG_ETH_MAX_TX_QUEUES, + RPMSG_ETH_MAX_RX_QUEUES); + + if (!port->ndev) { + dev_err(dev, "error allocating net_device\n"); + return -ENOMEM; + } + + ndev_priv = netdev_priv(port->ndev); + ndev_priv->port = port; + SET_NETDEV_DEV(port->ndev, dev); + + port->ndev->min_mtu = RPMSG_ETH_MIN_PACKET_SIZE; + port->ndev->max_mtu = RPMSG_ETH_MAX_MTU; + + if (!is_valid_ether_addr(port->ndev->dev_addr)) { + eth_hw_addr_random(port->ndev); + dev_dbg(dev, "Using random MAC address %pM\n", port->ndev->dev_addr); + } + + netif_carrier_off(port->ndev); + err = register_netdev(port->ndev); + if (err) + dev_err(dev, "error registering rpmsg_eth net device %d\n", err); + + return err; +} + static int rpmsg_eth_probe(struct rpmsg_device *rpdev) { struct device *dev = &rpdev->dev; @@ -107,11 +229,17 @@ static int rpmsg_eth_probe(struct rpmsg_device *rpdev) common->dev = dev; common->rpdev = rpdev; common->data = *(const struct rpmsg_eth_data *)rpdev->id.driver_data; + common->state = RPMSG_ETH_STATE_PROBE; ret = rpmsg_eth_get_shm_info(common); if (ret) return ret; + /* Register the network device */ + ret = rpmsg_eth_init_ndev(common); + if (ret) + return ret; + return 0; } diff --git a/drivers/net/ethernet/rpmsg_eth.h b/drivers/net/ethernet/rpmsg_eth.h index 0d6f96f755eb..0a0695e857df 100644 --- a/drivers/net/ethernet/rpmsg_eth.h +++ b/drivers/net/ethernet/rpmsg_eth.h @@ -18,6 +18,35 @@ #include #define RPMSG_ETH_SHM_MAGIC_NUM 0xABCDABCD +#define RPMSG_ETH_MIN_PACKET_SIZE ETH_ZLEN +#define RPMSG_ETH_PACKET_BUFFER_SIZE 1540 +#define RPMSG_ETH_MAX_MTU \ + (RPMSG_ETH_PACKET_BUFFER_SIZE - (ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN)) + +#define RPMSG_ETH_MAX_TX_QUEUES 1 +#define RPMSG_ETH_MAX_RX_QUEUES 1 +#define PKT_LEN_SIZE_TYPE sizeof(u32) +#define MAGIC_NUM_SIZE_TYPE sizeof(u32) + +/* 4 bytes to hold packet length and RPMSG_ETH_PACKET_BUFFER_SIZE to hold packet */ +#define RPMSG_ETH_BUFFER_SIZE \ + (RPMSG_ETH_PACKET_BUFFER_SIZE + PKT_LEN_SIZE_TYPE + MAGIC_NUM_SIZE_TYPE) + +#define RX_POLL_TIMEOUT_JIFFIES usecs_to_jiffies(1000) +#define RX_POLL_JIFFIES (jiffies + RX_POLL_TIMEOUT_JIFFIES) +#define STATE_MACHINE_TIME_JIFFIES msecs_to_jiffies(100) +#define RPMSG_ETH_REQ_TIMEOUT_JIFFIES msecs_to_jiffies(100) + +#define HEAD_MAGIC_NUM_OFFSET 0x0 +#define HEAD_IDX_OFFSET (HEAD_MAGIC_NUM_OFFSET + MAGIC_NUM_SIZE_TYPE) +#define PKT_START_OFFSET(n) \ + ((HEAD_IDX_OFFSET + MAGIC_NUM_SIZE_TYPE) + ((n) * RPMSG_ETH_BUFFER_SIZE)) +#define TAIL_MAGIC_NUM_OFFSET(n) PKT_START_OFFSET((n)) +#define TAIL_IDX_OFFSET(n) (TAIL_MAGIC_NUM_OFFSET((n)) + MAGIC_NUM_SIZE_TYPE) + +#define rpmsg_eth_ndev_to_priv(ndev) ((struct rpmsg_eth_ndev_priv *)netdev_priv(ndev)) +#define rpmsg_eth_ndev_to_port(ndev) (rpmsg_eth_ndev_to_priv(ndev)->port) +#define rpmsg_eth_ndev_to_common(ndev) (rpmsg_eth_ndev_to_port(ndev)->common) enum rpmsg_eth_msg_type { RPMSG_ETH_REQUEST_MSG = 0, @@ -25,6 +54,87 @@ enum rpmsg_eth_msg_type { RPMSG_ETH_NOTIFY_MSG, }; +enum rpmsg_eth_rpmsg_type { + /* Request types */ + RPMSG_ETH_REQ_SHM_INFO = 0, + RPMSG_ETH_REQ_SET_MAC_ADDR, + + /* Response types */ + RPMSG_ETH_RESP_SHM_INFO, + RPMSG_ETH_RESP_SET_MAC_ADDR, + + /* Notification types */ + RPMSG_ETH_NOTIFY_PORT_UP, + RPMSG_ETH_NOTIFY_PORT_DOWN, + RPMSG_ETH_NOTIFY_PORT_READY, + RPMSG_ETH_NOTIFY_REMOTE_READY, +}; + +/** + * struct rpmsg_eth_shm - Shared memory layout for RPMsg Ethernet + * @num_pkt_bufs: Number of packet buffers available in the shared memory + * @buff_slot_size: Size of each buffer slot in bytes + * @tx_offset: Offset for the transmit buffer region within the shared memory + * @rx_offset: Offset for the receive buffer region within the shared memory + * + * This structure defines the layout of the shared memory used for + * communication between the host and the remote processor in an RPMsg + * Ethernet driver. It specifies the configuration and memory offsets + * required for transmitting and receiving Ethernet packets. + */ +struct rpmsg_eth_shm { + u32 num_pkt_bufs; + u32 buff_slot_size; + u32 tx_offset; + u32 rx_offset; +} __packed; + +/** + * struct rpmsg_eth_mac_addr - MAC address information for RPMSG Ethernet + * @addr: MAC address + */ +struct rpmsg_eth_mac_addr { + char addr[ETH_ALEN]; +} __packed; + +/** + * struct request_message - request message structure for RPMSG Ethernet + * @type: Request Type + * @id: Request ID + * @mac_addr: MAC address (if request type is MAC address related) + */ +struct request_message { + u32 type; + u32 id; + union { + struct rpmsg_eth_mac_addr mac_addr; + }; +} __packed; + +/** + * struct response_message - response message structure for RPMSG Ethernet + * @type: Response Type + * @id: Response ID + * @shm_info: rpmsg shared memory info + */ +struct response_message { + u32 type; + u32 id; + union { + struct rpmsg_eth_shm shm_info; + }; +} __packed; + +/** + * struct notify_message - notification message structure for RPMSG Ethernet + * @type: Notify Type + * @id: Notify ID + */ +struct notify_message { + u32 type; + u32 id; +} __packed; + /** * struct message_header - message header structure for RPMSG Ethernet * @src_id: Source endpoint ID @@ -40,12 +150,20 @@ struct message_header { * * @msg_hdr: Message header contains source and destination endpoint and * the type of message + * @req_msg: Request message structure contains the request type and ID + * @resp_msg: Response message structure contains the response type and ID + * @notify_msg: Notification message structure contains the notify type and ID * * This structure is used to send and receive messages between the RPMSG * Ethernet ports. */ struct message { struct message_header msg_hdr; + union { + struct request_message req_msg; + struct response_message resp_msg; + struct notify_message notify_msg; + }; } __packed; /** @@ -56,30 +174,107 @@ struct rpmsg_eth_data { u8 shm_region_index; }; +/* Shared Memory Layout + * + * --------------------------- ***************** + * | MAGIC_NUM | rpmsg_eth_shm_head + * | HEAD_IDX | + * --------------------------- ***************** + * | MAGIC_NUM | + * | PKT_1_LEN | + * | PKT_1 | + * --------------------------- + * | MAGIC_NUM | + * | PKT_2_LEN | rpmsg_eth_shm_buf + * | PKT_2 | + * --------------------------- + * | . | + * | . | + * --------------------------- + * | MAGIC_NUM | + * | PKT_N_LEN | + * | PKT_N | + * --------------------------- **************** + * | MAGIC_NUM | rpmsg_eth_shm_tail + * | TAIL_IDX | + * --------------------------- **************** + */ + +enum rpmsg_eth_state { + RPMSG_ETH_STATE_PROBE, + RPMSG_ETH_STATE_OPEN, + RPMSG_ETH_STATE_CLOSE, + RPMSG_ETH_STATE_READY, + RPMSG_ETH_STATE_RUNNING, + +}; + /** * struct rpmsg_eth_common - common structure for RPMSG Ethernet * @rpdev: RPMSG device + * @send_msg: Send message + * @recv_msg: Receive message * @port: Ethernet port * @dev: Device * @data: Vendor specific data + * @state: Interface state + * @state_work: Delayed work for state machine */ struct rpmsg_eth_common { struct rpmsg_device *rpdev; + /** @send_msg_lock: Lock for sending RPMSG */ + spinlock_t send_msg_lock; + /** @recv_msg_lock: Lock for receiving RPMSG */ + spinlock_t recv_msg_lock; + struct message send_msg; + struct message recv_msg; struct rpmsg_eth_port *port; struct device *dev; struct rpmsg_eth_data data; + enum rpmsg_eth_state state; + /** @state_lock: Lock for changing interface state */ + struct mutex state_lock; + struct delayed_work state_work; +}; + +/** + * struct rpmsg_eth_ndev_priv - private structure for RPMSG Ethernet net device + * @port: Ethernet port + * @dev: Device + */ +struct rpmsg_eth_ndev_priv { + struct rpmsg_eth_port *port; + struct device *dev; }; /** * struct rpmsg_eth_port - Ethernet port structure for RPMSG Ethernet * @common: Pointer to the common RPMSG Ethernet structure * @shm: Shared memory region mapping + * @tx_offset: Offset for TX region in shared memory + * @rx_offset: Offset for RX region in shared memory * @buf_size: Size (in bytes) of the shared memory buffer for this port + * @rx_timer: Timer for rx polling + * @rx_napi: NAPI structure for rx polling + * @local_mac_addr: Local MAC address + * @ndev: Network device + * @tx_max_buffers: Maximum number of tx buffers + * @rx_max_buffers: Maximum number of rx buffers + * @port_id: Port ID */ struct rpmsg_eth_port { struct rpmsg_eth_common *common; void __iomem *shm; + u32 tx_offset; + u32 rx_offset; phys_addr_t buf_size; + struct timer_list rx_timer; + struct napi_struct rx_napi; + u8 local_mac_addr[ETH_ALEN]; + struct net_device *ndev; + u32 tx_max_buffers; + u32 rx_max_buffers; + u32 port_id; }; #endif /* __RPMSG_ETH_H__ */ -- 2.34.1 Add netdev ops for rpmsg-eth driver. This patch introduces the netdev operations for the rpmsg-eth driver, enabling the driver to interact with the Linux networking stack. The following functionalities are implemented: 1. `ndo_open` and `ndo_stop`: - Handles the initialization and cleanup of the network device during open and stop operations. - Manages the state transitions of the rpmsg-eth driver. 2. `ndo_start_xmit`: - Implements the transmit functionality by copying data from the skb to the shared memory buffer and updating the head index. 3. `ndo_set_mac_address`: - Allows setting the MAC address of the network device and sends the updated MAC address to the remote processor. 4. RX Path: - Adds a timer-based mechanism to poll for received packets in shared memory. - Implements NAPI-based packet processing to handle received packets efficiently. 5. State Machine: - Introduces a state machine to manage the driver's state transitions, such as PROBE, OPEN, READY, and RUNNING. 6. Initialization: - Adds necessary initialization for locks, timers, and work structures. - Registers the network device and sets up NAPI and RX timer. 7. Cleanup: - Ensures proper cleanup of resources during driver removal, including NAPI and timers. This patch enhances the rpmsg-eth driver to function as a fully operational network device in the Linux kernel. Signed-off-by: MD Danish Anwar --- drivers/net/ethernet/rpmsg_eth.c | 318 +++++++++++++++++++++++++++++++ drivers/net/ethernet/rpmsg_eth.h | 2 + 2 files changed, 320 insertions(+) diff --git a/drivers/net/ethernet/rpmsg_eth.c b/drivers/net/ethernet/rpmsg_eth.c index f599318633ea..43196e1d7eeb 100644 --- a/drivers/net/ethernet/rpmsg_eth.c +++ b/drivers/net/ethernet/rpmsg_eth.c @@ -63,6 +63,108 @@ static int rpmsg_eth_validate_handshake(struct rpmsg_eth_port *port, return 0; } +static int create_request(struct rpmsg_eth_common *common, + enum rpmsg_eth_rpmsg_type rpmsg_type) +{ + struct message *msg = &common->send_msg; + + msg->msg_hdr.src_id = common->port->port_id; + msg->req_msg.type = rpmsg_type; + + switch (rpmsg_type) { + case RPMSG_ETH_REQ_SHM_INFO: + msg->msg_hdr.msg_type = RPMSG_ETH_REQUEST_MSG; + break; + case RPMSG_ETH_REQ_SET_MAC_ADDR: + msg->msg_hdr.msg_type = RPMSG_ETH_REQUEST_MSG; + ether_addr_copy(msg->req_msg.mac_addr.addr, + common->port->ndev->dev_addr); + break; + case RPMSG_ETH_NOTIFY_PORT_UP: + case RPMSG_ETH_NOTIFY_PORT_DOWN: + msg->msg_hdr.msg_type = RPMSG_ETH_NOTIFY_MSG; + break; + default: + dev_err(common->dev, "Invalid RPMSG request\n"); + return -EINVAL; + } + return 0; +} + +static int rpmsg_eth_create_send_request(struct rpmsg_eth_common *common, + enum rpmsg_eth_rpmsg_type rpmsg_type, + bool wait) +{ + unsigned long flags; + int ret; + + if (wait) + reinit_completion(&common->sync_msg); + + spin_lock_irqsave(&common->send_msg_lock, flags); + ret = create_request(common, rpmsg_type); + if (ret) + goto release_lock; + + ret = rpmsg_send(common->rpdev->ept, (void *)(&common->send_msg), + sizeof(common->send_msg)); + if (ret) { + dev_err(common->dev, "Failed to send RPMSG message\n"); + goto release_lock; + } + + spin_unlock_irqrestore(&common->send_msg_lock, flags); + if (wait) { + ret = wait_for_completion_timeout(&common->sync_msg, + RPMSG_ETH_REQ_TIMEOUT_JIFFIES); + + if (!ret) { + dev_err(common->dev, "Failed to receive response within %ld jiffies\n", + RPMSG_ETH_REQ_TIMEOUT_JIFFIES); + return -ETIMEDOUT; + } + ret = 0; + } + return ret; +release_lock: + spin_unlock_irqrestore(&common->send_msg_lock, flags); + return ret; +} + +static void rpmsg_eth_state_machine(struct work_struct *work) +{ + struct delayed_work *dwork = to_delayed_work(work); + struct rpmsg_eth_common *common; + struct rpmsg_eth_port *port; + int ret; + + common = container_of(dwork, struct rpmsg_eth_common, state_work); + port = common->port; + + mutex_lock(&common->state_lock); + + switch (common->state) { + case RPMSG_ETH_STATE_PROBE: + break; + case RPMSG_ETH_STATE_OPEN: + rpmsg_eth_create_send_request(common, RPMSG_ETH_REQ_SHM_INFO, false); + break; + case RPMSG_ETH_STATE_CLOSE: + break; + case RPMSG_ETH_STATE_READY: + ret = rpmsg_eth_create_send_request(common, RPMSG_ETH_REQ_SET_MAC_ADDR, false); + if (!ret) { + napi_enable(&port->rx_napi); + netif_carrier_on(port->ndev); + mod_timer(&port->rx_timer, RX_POLL_TIMEOUT_JIFFIES); + } + break; + case RPMSG_ETH_STATE_RUNNING: + break; + } + mutex_unlock(&common->state_lock); +} + static int rpmsg_eth_rpmsg_cb(struct rpmsg_device *rpdev, void *data, int len, void *priv, u32 src) { @@ -99,6 +201,17 @@ static int rpmsg_eth_rpmsg_cb(struct rpmsg_device *rpdev, void *data, int len, msg->resp_msg.shm_info.num_pkt_bufs; port->rx_max_buffers = msg->resp_msg.shm_info.num_pkt_bufs; + + mutex_lock(&common->state_lock); + common->state = RPMSG_ETH_STATE_READY; + mutex_unlock(&common->state_lock); + + mod_delayed_work(system_wq, + &common->state_work, + STATE_MACHINE_TIME_JIFFIES); + + break; + case RPMSG_ETH_RESP_SET_MAC_ADDR: break; } break; @@ -106,6 +219,20 @@ static int rpmsg_eth_rpmsg_cb(struct rpmsg_device *rpdev, void *data, int len, rpmsg_type = msg->notify_msg.type; dev_dbg(common->dev, "Msg type = %d, RPMsg type = %d, Src Id = %d, Msg Id = %d\n", msg_type, rpmsg_type, msg->msg_hdr.src_id, msg->notify_msg.id); + switch (rpmsg_type) { + case RPMSG_ETH_NOTIFY_REMOTE_READY: + mutex_lock(&common->state_lock); + common->state = RPMSG_ETH_STATE_RUNNING; + mutex_unlock(&common->state_lock); + + mod_delayed_work(system_wq, + &common->state_work, + STATE_MACHINE_TIME_JIFFIES); + break; + case RPMSG_ETH_NOTIFY_PORT_UP: + case RPMSG_ETH_NOTIFY_PORT_DOWN: + break; + } break; default: dev_err(common->dev, "Invalid msg type\n"); @@ -172,6 +299,181 @@ static int rpmsg_eth_get_shm_info(struct rpmsg_eth_common *common) return 0; } +static void rpmsg_eth_rx_timer(struct timer_list *timer) +{ + struct rpmsg_eth_port *port = timer_container_of(port, timer, rx_timer); + struct napi_struct *napi; + int num_pkts = 0; + u32 head, tail; + + head = readl(port->shm + port->rx_offset + HEAD_IDX_OFFSET); + tail = readl(port->shm + port->rx_offset + + TAIL_IDX_OFFSET(port->rx_max_buffers)); + + num_pkts = tail - head; + num_pkts = num_pkts >= 0 ? num_pkts : + (num_pkts + port->rx_max_buffers); + + napi = &port->rx_napi; + if (num_pkts && likely(napi_schedule_prep(napi))) + __napi_schedule(napi); + else + mod_timer(&port->rx_timer, RX_POLL_JIFFIES); +} + +static int rpmsg_eth_rx_packets(struct napi_struct *napi, int budget) +{ + struct rpmsg_eth_port *port = container_of(napi, struct rpmsg_eth_port, rx_napi); + u32 count, process_pkts; + struct sk_buff *skb; + u32 head, tail; + int num_pkts; + u32 pkt_len; + + head = readl(port->shm + port->rx_offset + HEAD_IDX_OFFSET); + tail = readl(port->shm + port->rx_offset + + TAIL_IDX_OFFSET(port->rx_max_buffers)); + + num_pkts = head - tail; + + num_pkts = num_pkts >= 0 ? num_pkts : + (num_pkts + port->rx_max_buffers); + process_pkts = min(num_pkts, budget); + count = 0; + while (count < process_pkts) { + memcpy_fromio((void *)&pkt_len, + port->shm + port->rx_offset + MAGIC_NUM_SIZE_TYPE + + PKT_START_OFFSET((tail + count) % port->rx_max_buffers), + PKT_LEN_SIZE_TYPE); + /* Start building the skb */ + skb = napi_alloc_skb(napi, pkt_len); + if (!skb) { + port->ndev->stats.rx_dropped++; + goto rx_dropped; + } + + skb->dev = port->ndev; + skb_put(skb, pkt_len); + memcpy_fromio((void *)skb->data, + port->shm + port->rx_offset + PKT_LEN_SIZE_TYPE + + MAGIC_NUM_SIZE_TYPE + + PKT_START_OFFSET((tail + count) % port->rx_max_buffers), + pkt_len); + + skb->protocol = eth_type_trans(skb, port->ndev); + + /* Push skb into network stack */ + napi_gro_receive(napi, skb); + + count++; + port->ndev->stats.rx_packets++; + port->ndev->stats.rx_bytes += skb->len; + } + +rx_dropped: + + if (num_pkts) { + writel((tail + count) % port->rx_max_buffers, + port->shm + port->rx_offset + + TAIL_IDX_OFFSET(port->rx_max_buffers)); + + if (num_pkts < budget && napi_complete_done(napi, count)) + mod_timer(&port->rx_timer, RX_POLL_TIMEOUT_JIFFIES); + } + + return count; +} + +static int rpmsg_eth_ndo_open(struct net_device *ndev) +{ + struct rpmsg_eth_common *common = rpmsg_eth_ndev_to_common(ndev); + + mutex_lock(&common->state_lock); + common->state = RPMSG_ETH_STATE_OPEN; + mutex_unlock(&common->state_lock); + mod_delayed_work(system_wq, &common->state_work, msecs_to_jiffies(100)); + + return 0; +} + +static int rpmsg_eth_ndo_stop(struct net_device *ndev) +{ + struct rpmsg_eth_common *common = rpmsg_eth_ndev_to_common(ndev); + struct rpmsg_eth_port *port = rpmsg_eth_ndev_to_port(ndev); + + mutex_lock(&common->state_lock); + common->state = RPMSG_ETH_STATE_CLOSE; + mutex_unlock(&common->state_lock); + + netif_carrier_off(port->ndev); + + cancel_delayed_work_sync(&common->state_work); + timer_delete_sync(&port->rx_timer); + napi_disable(&port->rx_napi); + + return 0; +} + +static netdev_tx_t rpmsg_eth_start_xmit(struct sk_buff *skb, struct net_device *ndev) +{ + struct rpmsg_eth_port *port = rpmsg_eth_ndev_to_port(ndev); + u32 head, tail; + int num_pkts; + u32 len; + + len = skb_headlen(skb); + head = readl(port->shm + port->tx_offset + HEAD_IDX_OFFSET); + tail = readl(port->shm + port->tx_offset + + TAIL_IDX_OFFSET(port->tx_max_buffers)); + + /* If the buffer queue is full, then drop packet */ + num_pkts = head - tail; + num_pkts = num_pkts >= 0 ? num_pkts : + (num_pkts + port->tx_max_buffers); + + if ((num_pkts + 1) == port->tx_max_buffers) { + netdev_warn(ndev, "Tx buffer full %d\n", num_pkts); + goto ring_full; + } + /* Copy length */ + memcpy_toio(port->shm + port->tx_offset + PKT_START_OFFSET(head) + MAGIC_NUM_SIZE_TYPE, + (void *)&len, PKT_LEN_SIZE_TYPE); + /* Copy data to shared mem */ + memcpy_toio(port->shm + port->tx_offset + PKT_START_OFFSET(head) + MAGIC_NUM_SIZE_TYPE + + PKT_LEN_SIZE_TYPE, (void *)skb->data, len); + writel((head + 1) % port->tx_max_buffers, + port->shm + port->tx_offset + HEAD_IDX_OFFSET); + + ndev->stats.tx_packets++; + ndev->stats.tx_bytes += skb->len; + + dev_consume_skb_any(skb); + return NETDEV_TX_OK; + +ring_full: + return NETDEV_TX_BUSY; +} + +static int rpmsg_eth_set_mac_address(struct net_device *ndev, void *addr) +{ + struct rpmsg_eth_common *common = rpmsg_eth_ndev_to_common(ndev); + int ret; + + ret = eth_mac_addr(ndev, addr); + + if (ret < 0) + return ret; + + return rpmsg_eth_create_send_request(common, RPMSG_ETH_REQ_SET_MAC_ADDR, false); +} + +static const struct net_device_ops rpmsg_eth_netdev_ops = { + .ndo_open = rpmsg_eth_ndo_open, + .ndo_stop = rpmsg_eth_ndo_stop, + .ndo_start_xmit = rpmsg_eth_start_xmit, + .ndo_set_mac_address = rpmsg_eth_set_mac_address, +}; + static int rpmsg_eth_init_ndev(struct rpmsg_eth_common *common) { struct device *dev = &common->rpdev->dev; @@ -195,6 +497,7 @@ static int rpmsg_eth_init_ndev(struct rpmsg_eth_common *common) ndev_priv = netdev_priv(port->ndev); ndev_priv->port = port; + port->ndev->netdev_ops = &rpmsg_eth_netdev_ops; SET_NETDEV_DEV(port->ndev, dev); port->ndev->min_mtu = RPMSG_ETH_MIN_PACKET_SIZE; @@ -206,6 +509,8 @@ static int rpmsg_eth_init_ndev(struct rpmsg_eth_common *common) } netif_carrier_off(port->ndev); + netif_napi_add(port->ndev, &port->rx_napi, rpmsg_eth_rx_packets); + timer_setup(&port->rx_timer, rpmsg_eth_rx_timer, 0); err = register_netdev(port->ndev); if (err) dev_err(dev, "error registering rpmsg_eth net device %d\n", err); @@ -235,6 +540,12 @@ static int rpmsg_eth_probe(struct rpmsg_device *rpdev) if (ret) return ret; + spin_lock_init(&common->send_msg_lock); + spin_lock_init(&common->recv_msg_lock); + mutex_init(&common->state_lock); + INIT_DELAYED_WORK(&common->state_work, rpmsg_eth_state_machine); + init_completion(&common->sync_msg); + /* Register the network device */ ret = rpmsg_eth_init_ndev(common); if (ret) @@ -245,6 +556,13 @@ static int rpmsg_eth_probe(struct rpmsg_device *rpdev) static void rpmsg_eth_remove(struct rpmsg_device *rpdev) { + struct rpmsg_eth_common *common = dev_get_drvdata(&rpdev->dev); + struct rpmsg_eth_port *port = common->port; + + netif_napi_del(&port->rx_napi); + timer_delete_sync(&port->rx_timer); + unregister_netdev(port->ndev); + dev_dbg(&rpdev->dev, "rpmsg-eth client driver is removed\n"); } diff --git a/drivers/net/ethernet/rpmsg_eth.h b/drivers/net/ethernet/rpmsg_eth.h index 0a0695e857df..80fa07be1678 100644 --- a/drivers/net/ethernet/rpmsg_eth.h +++ b/drivers/net/ethernet/rpmsg_eth.h @@ -219,6 +219,7 @@ enum rpmsg_eth_state { * @data: Vendor specific data * @state: Interface state * @state_work: Delayed work for state machine + * @sync_msg: Completion for synchronous message */ struct rpmsg_eth_common { struct rpmsg_device *rpdev; @@ -235,6 +236,7 @@ struct rpmsg_eth_common { /** @state_lock: Lock for changing interface state */ struct mutex state_lock; struct delayed_work state_work; + struct completion sync_msg; }; /** -- 2.34.1 Add support for multicast filtering for RPMSG ETH driver. Implement the ndo_set_rx_mode callback as icve_set_rx_mode() API. rx_mode_workqueue is initialized in rpmsg_eth_probe() and queued in rpmsg_eth_set_rx_mode(). Signed-off-by: MD Danish Anwar --- drivers/net/ethernet/rpmsg_eth.c | 63 ++++++++++++++++++++++++++++++++ drivers/net/ethernet/rpmsg_eth.h | 12 ++++++ 2 files changed, 75 insertions(+) diff --git a/drivers/net/ethernet/rpmsg_eth.c b/drivers/net/ethernet/rpmsg_eth.c index 43196e1d7eeb..cde19d924b89 100644 --- a/drivers/net/ethernet/rpmsg_eth.c +++ b/drivers/net/ethernet/rpmsg_eth.c @@ -80,6 +80,11 @@ static int create_request(struct rpmsg_eth_common *common, ether_addr_copy(msg->req_msg.mac_addr.addr, common->port->ndev->dev_addr); break; + case RPMSG_ETH_REQ_ADD_MC_ADDR: + case RPMSG_ETH_REQ_DEL_MC_ADDR: + ether_addr_copy(msg->req_msg.mac_addr.addr, + common->mcast_addr); + break; case RPMSG_ETH_NOTIFY_PORT_UP: case RPMSG_ETH_NOTIFY_PORT_DOWN: msg->msg_hdr.msg_type = RPMSG_ETH_NOTIFY_MSG; @@ -131,6 +136,22 @@ static int rpmsg_eth_create_send_request(struct rpmsg_eth_common *common, return ret; } +static int rpmsg_eth_add_mc_addr(struct net_device *ndev, const u8 *addr) +{ + struct rpmsg_eth_common *common = rpmsg_eth_ndev_to_common(ndev); + + ether_addr_copy(common->mcast_addr, addr); + return rpmsg_eth_create_send_request(common, RPMSG_ETH_REQ_ADD_MC_ADDR, true); +} + +static int rpmsg_eth_del_mc_addr(struct net_device *ndev, const u8 *addr) +{ + struct rpmsg_eth_common *common = rpmsg_eth_ndev_to_common(ndev); + + ether_addr_copy(common->mcast_addr, addr); + return rpmsg_eth_create_send_request(common, RPMSG_ETH_REQ_DEL_MC_ADDR, true); +} + static void rpmsg_eth_state_machine(struct work_struct *work) { struct delayed_work *dwork = to_delayed_work(work); @@ -213,6 +234,10 @@ static int rpmsg_eth_rpmsg_cb(struct rpmsg_device *rpdev, void *data, int len, break; case RPMSG_ETH_RESP_SET_MAC_ADDR: break; + case RPMSG_ETH_RESP_ADD_MC_ADDR: + case RPMSG_ETH_RESP_DEL_MC_ADDR: + complete(&common->sync_msg); + break; } break; case RPMSG_ETH_NOTIFY_MSG: @@ -407,10 +432,15 @@ static int rpmsg_eth_ndo_stop(struct net_device *ndev) netif_carrier_off(port->ndev); + __dev_mc_unsync(ndev, rpmsg_eth_del_mc_addr); + __hw_addr_init(&common->mc_list); + cancel_delayed_work_sync(&common->state_work); timer_delete_sync(&port->rx_timer); napi_disable(&port->rx_napi); + cancel_work_sync(&common->rx_mode_work); + return 0; } @@ -467,10 +497,35 @@ static int rpmsg_eth_set_mac_address(struct net_device *ndev, void *addr) return rpmsg_eth_create_send_request(common, RPMSG_ETH_REQ_SET_MAC_ADDR, false); } +static void rpmsg_eth_ndo_set_rx_mode_work(struct work_struct *work) +{ + struct rpmsg_eth_common *common; + struct net_device *ndev; + + common = container_of(work, struct rpmsg_eth_common, rx_mode_work); + ndev = common->port->ndev; + + /* make a mc list copy */ + netif_addr_lock_bh(ndev); + __hw_addr_sync(&common->mc_list, &ndev->mc, ndev->addr_len); + netif_addr_unlock_bh(ndev); + + __hw_addr_sync_dev(&common->mc_list, ndev, rpmsg_eth_add_mc_addr, + rpmsg_eth_del_mc_addr); +} + +static void rpmsg_eth_set_rx_mode(struct net_device *ndev) +{ + struct rpmsg_eth_common *common = rpmsg_eth_ndev_to_common(ndev); + + queue_work(common->cmd_wq, &common->rx_mode_work); +} + static const struct net_device_ops rpmsg_eth_netdev_ops = { .ndo_open = rpmsg_eth_ndo_open, .ndo_stop = rpmsg_eth_ndo_stop, .ndo_start_xmit = rpmsg_eth_start_xmit, + .ndo_set_rx_mode = rpmsg_eth_set_rx_mode, .ndo_set_mac_address = rpmsg_eth_set_mac_address, }; @@ -546,6 +601,13 @@ static int rpmsg_eth_probe(struct rpmsg_device *rpdev) INIT_DELAYED_WORK(&common->state_work, rpmsg_eth_state_machine); init_completion(&common->sync_msg); + __hw_addr_init(&common->mc_list); + INIT_WORK(&common->rx_mode_work, rpmsg_eth_ndo_set_rx_mode_work); + common->cmd_wq = create_singlethread_workqueue("rpmsg_eth_rx_work"); + if (!common->cmd_wq) { + dev_err(dev, "Failure requesting workqueue\n"); + return -ENOMEM; + } /* Register the network device */ ret = rpmsg_eth_init_ndev(common); if (ret) @@ -562,6 +624,7 @@ static void rpmsg_eth_remove(struct rpmsg_device *rpdev) netif_napi_del(&port->rx_napi); timer_delete_sync(&port->rx_timer); unregister_netdev(port->ndev); + destroy_workqueue(common->cmd_wq); dev_dbg(&rpdev->dev, "rpmsg-eth client driver is removed\n"); } diff --git a/drivers/net/ethernet/rpmsg_eth.h b/drivers/net/ethernet/rpmsg_eth.h index 80fa07be1678..d6112cb9269a 100644 --- a/drivers/net/ethernet/rpmsg_eth.h +++ b/drivers/net/ethernet/rpmsg_eth.h @@ -58,10 +58,14 @@ enum rpmsg_eth_rpmsg_type { /* Request types */ RPMSG_ETH_REQ_SHM_INFO = 0, RPMSG_ETH_REQ_SET_MAC_ADDR, + RPMSG_ETH_REQ_ADD_MC_ADDR, + RPMSG_ETH_REQ_DEL_MC_ADDR, /* Response types */ RPMSG_ETH_RESP_SHM_INFO, RPMSG_ETH_RESP_SET_MAC_ADDR, + RPMSG_ETH_RESP_ADD_MC_ADDR, + RPMSG_ETH_RESP_DEL_MC_ADDR, /* Notification types */ RPMSG_ETH_NOTIFY_PORT_UP, @@ -220,6 +224,10 @@ enum rpmsg_eth_state { * @state: Interface state * @state_work: Delayed work for state machine * @sync_msg: Completion for synchronous message + * @rx_mode_work: Work structure for rx mode + * @cmd_wq: Workqueue for commands + * @mc_list: List of multicast addresses + * @mcast_addr: Multicast address filter */ struct rpmsg_eth_common { struct rpmsg_device *rpdev; @@ -237,6 +245,10 @@ struct rpmsg_eth_common { struct mutex state_lock; struct delayed_work state_work; struct completion sync_msg; + struct work_struct rx_mode_work; + struct workqueue_struct *cmd_wq; + struct netdev_hw_addr_list mc_list; + u8 mcast_addr[ETH_ALEN]; }; /** -- 2.34.1 Add an entry to MAINTAINERS file for the rpmsg_eth driver with appropriate maintainer information and mailing list. Signed-off-by: MD Danish Anwar --- MAINTAINERS | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/MAINTAINERS b/MAINTAINERS index e3907f0c1243..9fd0f6a602d0 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -22001,6 +22001,12 @@ L: linux-remoteproc@vger.kernel.org S: Maintained F: drivers/tty/rpmsg_tty.c +RPMSG ETHERNET DRIVER +M: MD Danish Anwar +L: netdev@vger.kernel.org +S: Maintained +F: drivers/net/ethernet/rpmsg_eth* + RTASE ETHERNET DRIVER M: Justin Lai M: Larry Chiu -- 2.34.1 Reserve a shared memory region for rpmsg eth communication and add it to the remote proc device `main_r5fss0_core0`. Signed-off-by: MD Danish Anwar --- arch/arm64/boot/dts/ti/k3-am642-evm.dts | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/arch/arm64/boot/dts/ti/k3-am642-evm.dts b/arch/arm64/boot/dts/ti/k3-am642-evm.dts index e01866372293..6e8e2c39146b 100644 --- a/arch/arm64/boot/dts/ti/k3-am642-evm.dts +++ b/arch/arm64/boot/dts/ti/k3-am642-evm.dts @@ -61,7 +61,13 @@ main_r5fss0_core0_dma_memory_region: r5f-dma-memory@a0000000 { main_r5fss0_core0_memory_region: r5f-memory@a0100000 { compatible = "shared-dma-pool"; - reg = <0x00 0xa0100000 0x00 0xf00000>; + reg = <0x00 0xa0100000 0x00 0x300000>; + no-map; + }; + + main_r5fss0_core0_memory_region_shm: r5f-shm-memory@a0400000 { + compatible = "shared-dma-pool"; + reg = <0x00 0xa0400000 0x00 0xc00000>; no-map; }; @@ -767,7 +773,8 @@ mbox_m4_0: mbox-m4-0 { &main_r5fss0_core0 { mboxes = <&mailbox0_cluster2 &mbox_main_r5fss0_core0>; memory-region = <&main_r5fss0_core0_dma_memory_region>, - <&main_r5fss0_core0_memory_region>; + <&main_r5fss0_core0_memory_region>, + <&main_r5fss0_core0_memory_region_shm>; }; &main_r5fss0_core1 { -- 2.34.1