rpmsgblk: rpmsg block device support

Like rpmsgdev and rpmsgmtd, rpmsgblk allow the local cpu to
access the block device in the remote cpu.

Signed-off-by: wangbowen6 <wangbowen6@xiaomi.com>
This commit is contained in:
wangbowen6 2022-10-26 13:14:49 +08:00 committed by Xiang Xiao
parent 2a20db7697
commit f56710c121
10 changed files with 1751 additions and 0 deletions

View File

@ -11,6 +11,7 @@ CONFIG_ARCH_BOARD="sim"
CONFIG_ARCH_BOARD_SIM=y
CONFIG_ARCH_CHIP="sim"
CONFIG_ARCH_SIM=y
CONFIG_BLK_RPMSG=y
CONFIG_BOARDCTL_POWEROFF=y
CONFIG_BUILTIN=y
CONFIG_DEBUG_ASSERTIONS=y

View File

@ -10,6 +10,7 @@ CONFIG_ARCH_BOARD="sim"
CONFIG_ARCH_BOARD_SIM=y
CONFIG_ARCH_CHIP="sim"
CONFIG_ARCH_SIM=y
CONFIG_BLK_RPMSG_SERVER=y
CONFIG_BOARDCTL_POWEROFF=y
CONFIG_BUILTIN=y
CONFIG_CLK=y

View File

@ -34,7 +34,9 @@
#include <nuttx/mtd/mtd.h>
#include <nuttx/fs/fs.h>
#include <nuttx/fs/nxffs.h>
#include <nuttx/drivers/ramdisk.h>
#include <nuttx/drivers/rpmsgdev.h>
#include <nuttx/drivers/rpmsgblk.h>
#include <nuttx/i2c/i2c_master.h>
#include <nuttx/spi/spi_transfer.h>
#include <nuttx/rc/lirc_dev.h>
@ -93,6 +95,9 @@ int sim_bringup(void)
#ifdef CONFIG_RAMMTD
uint8_t *ramstart;
#endif
#if !defined(CONFIG_DISABLE_MOUNTPOINT) && defined(CONFIG_BLK_RPMSG_SERVER)
uint8_t *ramdiskstart;
#endif
#ifdef CONFIG_SIM_I2CBUS
struct i2c_master_s *i2cbus;
#endif
@ -231,6 +236,16 @@ int sim_bringup(void)
}
#endif
#if !defined(CONFIG_DISABLE_MOUNTPOINT) && defined(CONFIG_BLK_RPMSG_SERVER)
ramdiskstart = (uint8_t *)kmm_malloc(512 * 2048);
ret = ramdisk_register(1, ramdiskstart, 2048, 512,
RDFLAG_WRENABLED | RDFLAG_FUNLINK);
if (ret < 0)
{
syslog(LOG_ERR, "ERROR: Failed to register ramdisk: %d\n", ret);
}
#endif
#ifdef CONFIG_ONESHOT
/* Get an instance of the simulated oneshot timer */
@ -443,6 +458,10 @@ int sim_bringup(void)
rpmsgdev_register("server", "/dev/ttyUSB0", "/dev/ttyUSB0");
#endif
#ifdef CONFIG_BLK_RPMSG
rpmsgblk_register("server", "/dev/ram1", NULL);
#endif
#ifdef CONFIG_RPMSGMTD
rpmsgmtd_register("server", "/dev/rammtd", NULL);
#endif

View File

@ -26,6 +26,7 @@
#include <nuttx/crypto/crypto.h>
#include <nuttx/drivers/drivers.h>
#include <nuttx/drivers/rpmsgdev.h>
#include <nuttx/drivers/rpmsgblk.h>
#include <nuttx/fs/loop.h>
#include <nuttx/input/uinput.h>
#include <nuttx/mtd/mtd.h>
@ -169,6 +170,10 @@ void drivers_initialize(void)
rpmsgdev_server_init();
#endif
#ifdef CONFIG_BLK_RPMSG_SERVER
rpmsgblk_server_init();
#endif
#ifdef CONFIG_RPMSGMTD_SERVER
rpmsgmtd_server_init();
#endif

View File

@ -34,6 +34,16 @@ config DRVR_MKRD
the selecting this option will also enable the BOARDIOC_MKRD
command that will support creation of RAM disks from applications.
config BLK_RPMSG
bool "RPMSG Block Client Support"
default n
depends on RPTUN
config BLK_RPMSG_SERVER
bool "RPMSG Block Server Support"
default n
depends on RPTUN
# ARCH needs to support memory access while CPU is running to be able to use
# the LWL CONSOLE

View File

@ -55,6 +55,14 @@ ifeq ($(CONFIG_DEV_RPMSG_SERVER),y)
CSRCS += rpmsgdev_server.c
endif
ifeq ($(CONFIG_BLK_RPMSG),y)
CSRCS += rpmsgblk.c
endif
ifeq ($(CONFIG_BLK_RPMSG_SERVER),y)
CSRCS += rpmsgblk_server.c
endif
# Include build support
DEPPATH += --dep-path misc

1106
drivers/misc/rpmsgblk.c Normal file

File diff suppressed because it is too large Load Diff

109
drivers/misc/rpmsgblk.h Normal file
View File

@ -0,0 +1,109 @@
/****************************************************************************
* drivers/misc/rpmsgblk.h
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/
#ifndef __DRIVERS_MISC_RPMSGBLK_H
#define __DRIVERS_MISC_RPMSGBLK_H
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/compiler.h>
/****************************************************************************
* Pre-processor definitions
****************************************************************************/
#ifndef ARRAY_SIZE
# define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
#endif
#define RPMSGBLK_NAME_PREFIX "rpmsgblk-"
#define RPMSGBLK_NAME_PREFIX_LEN 9
#define RPMSGBLK_OPEN 1
#define RPMSGBLK_CLOSE 2
#define RPMSGBLK_READ 3
#define RPMSGBLK_WRITE 4
#define RPMSGBLK_GEOMETRY 5
#define RPMSGBLK_IOCTL 6
#define RPMSGBLK_UNLINK 7
/****************************************************************************
* Public Types
****************************************************************************/
begin_packed_struct struct rpmsgblk_header_s
{
uint32_t command;
int32_t result;
uint64_t cookie;
} end_packed_struct;
begin_packed_struct struct rpmsgblk_open_s
{
struct rpmsgblk_header_s header;
} end_packed_struct;
#define rpmsgblk_close_s rpmsgblk_open_s
begin_packed_struct struct rpmsgblk_read_s
{
struct rpmsgblk_header_s header;
uint32_t startsector;
uint32_t nsectors;
int32_t sectorsize;
uint32_t count;
char buf[1];
} end_packed_struct;
#define rpmsgblk_write_s rpmsgblk_read_s
begin_packed_struct struct rpmsgblk_geometry_s
{
struct rpmsgblk_header_s header;
uint64_t arg;
uint32_t arglen;
char buf[1];
} end_packed_struct;
begin_packed_struct struct rpmsgblk_ioctl_s
{
struct rpmsgblk_header_s header;
uint64_t arg;
uint32_t arglen;
int32_t request;
char buf[1];
} end_packed_struct;
begin_packed_struct struct rpmsgblk_unlink_s
{
struct rpmsgblk_header_s header;
} end_packed_struct;
/****************************************************************************
* Internal function prototypes
****************************************************************************/
/****************************************************************************
* Internal data
****************************************************************************/
#endif /* __DRIVERS_MISC_RPMSGBLK_H */

View File

@ -0,0 +1,402 @@
/****************************************************************************
* drivers/misc/rpmsgblk_server.c
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <string.h>
#include <errno.h>
#include <debug.h>
#include <nuttx/kmalloc.h>
#include <nuttx/fs/fs.h>
#include <nuttx/rptun/openamp.h>
#include "rpmsgblk.h"
/****************************************************************************
* Pre-processor definitions
****************************************************************************/
/****************************************************************************
* Private Types
****************************************************************************/
struct rpmsgblk_server_s
{
struct rpmsg_endpoint ept;
FAR struct inode *blknode;
FAR const struct block_operations *bops;
};
/****************************************************************************
* Private Function Prototypes
****************************************************************************/
/* Functions handle the messages from the client cpu */
static int rpmsgblk_open_handler(FAR struct rpmsg_endpoint *ept,
FAR void *data, size_t len,
uint32_t src, FAR void *priv);
static int rpmsgblk_close_handler(FAR struct rpmsg_endpoint *ept,
FAR void *data, size_t len,
uint32_t src, FAR void *priv);
static int rpmsgblk_read_handler(FAR struct rpmsg_endpoint *ept,
FAR void *data, size_t len,
uint32_t src, FAR void *priv);
static int rpmsgblk_write_handler(FAR struct rpmsg_endpoint *ept,
FAR void *data, size_t len,
uint32_t src, FAR void *priv);
static int rpmsgblk_geometry_handler(FAR struct rpmsg_endpoint *ept,
FAR void *data, size_t len,
uint32_t src, FAR void *priv);
static int rpmsgblk_ioctl_handler(FAR struct rpmsg_endpoint *ept,
FAR void *data, size_t len,
uint32_t src, FAR void *priv);
static int rpmsgblk_unlink_handler(FAR struct rpmsg_endpoint *ept,
FAR void *data, size_t len,
uint32_t src, FAR void *priv);
/* Functions for creating communication with client cpu */
static bool rpmsgblk_ns_match(FAR struct rpmsg_device *rdev,
FAR void *priv, FAR const char *name,
uint32_t dest);
static void rpmsgblk_ns_bind(FAR struct rpmsg_device *rdev,
FAR void *priv, FAR const char *name,
uint32_t dest);
static void rpmsgblk_ns_unbind(FAR struct rpmsg_endpoint *ept);
static int rpmsgblk_ept_cb(FAR struct rpmsg_endpoint *ept,
FAR void *data, size_t len, uint32_t src,
FAR void *priv);
/****************************************************************************
* Private Data
****************************************************************************/
static const rpmsg_ept_cb g_rpmsgblk_handler[] =
{
[RPMSGBLK_OPEN] = rpmsgblk_open_handler,
[RPMSGBLK_CLOSE] = rpmsgblk_close_handler,
[RPMSGBLK_READ] = rpmsgblk_read_handler,
[RPMSGBLK_WRITE] = rpmsgblk_write_handler,
[RPMSGBLK_GEOMETRY] = rpmsgblk_geometry_handler,
[RPMSGBLK_IOCTL] = rpmsgblk_ioctl_handler,
[RPMSGBLK_UNLINK] = rpmsgblk_unlink_handler,
};
/****************************************************************************
* Private Functions
****************************************************************************/
/****************************************************************************
* Name: rpmsgblk_open_handler
****************************************************************************/
static int rpmsgblk_open_handler(FAR struct rpmsg_endpoint *ept,
FAR void *data, size_t len,
uint32_t src, FAR void *priv)
{
FAR struct rpmsgblk_server_s *server = ept->priv;
FAR struct rpmsgblk_open_s *msg = data;
if (server->blknode != NULL)
{
msg->header.result = -EBUSY;
goto out;
}
msg->header.result = open_blockdriver(&ept->name[RPMSGBLK_NAME_PREFIX_LEN],
0, &server->blknode);
if (msg->header.result < 0)
{
ferr("block device open failed, ret=%d\n", msg->header.result);
goto out;
}
server->bops = server->blknode->u.i_bops;
out:
return rpmsg_send(ept, msg, sizeof(*msg));
}
/****************************************************************************
* Name: rpmsgblk_close_handler
****************************************************************************/
static int rpmsgblk_close_handler(FAR struct rpmsg_endpoint *ept,
FAR void *data, size_t len,
uint32_t src, FAR void *priv)
{
FAR struct rpmsgblk_server_s *server = ept->priv;
FAR struct rpmsgblk_close_s *msg = data;
msg->header.result = close_blockdriver(server->blknode);
if (msg->header.result < 0)
{
ferr("block device close failed, ret=%d\n", msg->header.result);
goto out;
}
server->bops = NULL;
server->blknode = NULL;
out:
return rpmsg_send(ept, msg, sizeof(*msg));
}
/****************************************************************************
* Name: rpmsgblk_read_handler
****************************************************************************/
static int rpmsgblk_read_handler(FAR struct rpmsg_endpoint *ept,
FAR void *data, size_t len,
uint32_t src, FAR void *priv)
{
FAR struct rpmsgblk_server_s *server = ept->priv;
FAR struct rpmsgblk_read_s *msg = data;
FAR struct rpmsgblk_read_s *rsp;
int ret = -ENOENT;
size_t read = 0;
size_t nsectors;
uint32_t space;
while (read < msg->nsectors)
{
rsp = rpmsg_get_tx_payload_buffer(ept, &space, true);
if (rsp == NULL)
{
ferr("get tx payload failed or no enough space\n");
return -ENOMEM;
}
DEBUGASSERT(space >= sizeof(*msg) - 1 + msg->sectorsize);
*rsp = *msg;
nsectors = (space - sizeof(*msg) + 1) / msg->sectorsize;
if (nsectors > msg->nsectors - read)
{
nsectors = msg->nsectors - read;
}
ret = server->bops->read(server->blknode, (unsigned char *)rsp->buf,
msg->startsector, msg->nsectors);
rsp->header.result = ret;
rpmsg_send_nocopy(ept, rsp, (ret < 0 ? 0 : ret * msg->sectorsize) +
sizeof(*rsp) - 1);
if (ret <= 0)
{
ferr("mtd block read failed\n");
break;
}
read += ret;
}
return 0;
}
/****************************************************************************
* Name: rpmsgblk_write_handler
****************************************************************************/
static int rpmsgblk_write_handler(FAR struct rpmsg_endpoint *ept,
FAR void *data, size_t len,
uint32_t src, FAR void *priv)
{
FAR struct rpmsgblk_server_s *server = ept->priv;
FAR struct rpmsgblk_write_s *msg = data;
int ret;
ret = server->bops->write(server->blknode, (FAR unsigned char *)msg->buf,
msg->startsector, msg->nsectors);
if (ret <= 0)
{
ferr("mtd block write failed\n");
}
/* cookie != 0 indicate the data has been sent complete, so send back
* the total written blocks.
*/
if (msg->header.cookie != 0)
{
msg->header.result = ret;
return rpmsg_send(ept, msg, sizeof(*msg) - 1);
}
return 0;
}
/****************************************************************************
* Name: rpmsgblk_ioctl_handler
****************************************************************************/
static int rpmsgblk_geometry_handler(FAR struct rpmsg_endpoint *ept,
FAR void *data, size_t len,
uint32_t src, FAR void *priv)
{
FAR struct rpmsgblk_server_s *server = ept->priv;
FAR struct rpmsgblk_geometry_s *msg = data;
DEBUGASSERT(msg->arglen == sizeof(struct geometry));
msg->header.result = server->bops->geometry(
server->blknode, (FAR struct geometry *)msg->buf);
return rpmsg_send(ept, msg, len);
}
/****************************************************************************
* Name: rpmsgblk_ioctl_handler
****************************************************************************/
static int rpmsgblk_ioctl_handler(FAR struct rpmsg_endpoint *ept,
FAR void *data, size_t len,
uint32_t src, FAR void *priv)
{
FAR struct rpmsgblk_server_s *server = ept->priv;
FAR struct rpmsgblk_ioctl_s *msg = data;
msg->header.result = server->bops->ioctl(server->blknode, msg->request,
msg->arglen > 0 ?
(unsigned long)msg->buf :
msg->arg);
return rpmsg_send(ept, msg, len);
}
/****************************************************************************
* Name: rpmsgblk_unlink_handler
****************************************************************************/
static int rpmsgblk_unlink_handler(FAR struct rpmsg_endpoint *ept,
FAR void *data, size_t len,
uint32_t src, FAR void *priv)
{
FAR struct rpmsgblk_server_s *server = ept->priv;
FAR struct rpmsgblk_unlink_s *msg = data;
msg->header.result = server->bops->unlink(server->blknode);
return rpmsg_send(ept, msg, len);
}
/****************************************************************************
* Name: rpmsgblk_ns_match
****************************************************************************/
static bool rpmsgblk_ns_match(FAR struct rpmsg_device *rdev,
FAR void *priv, FAR const char *name,
uint32_t dest)
{
return !strncmp(name, RPMSGBLK_NAME_PREFIX, RPMSGBLK_NAME_PREFIX_LEN);
}
/****************************************************************************
* Name: rpmsgblk_ns_bind
****************************************************************************/
static void rpmsgblk_ns_bind(FAR struct rpmsg_device *rdev,
FAR void *priv, FAR const char *name,
uint32_t dest)
{
FAR struct rpmsgblk_server_s *server;
int ret;
server = kmm_zalloc(sizeof(*server));
if (server == NULL)
{
ferr("mtd server malloced failed\n");
return;
}
server->ept.priv = server;
ret = rpmsg_create_ept(&server->ept, rdev, name,
RPMSG_ADDR_ANY, dest,
rpmsgblk_ept_cb, rpmsgblk_ns_unbind);
if (ret < 0)
{
ferr("endpoint create failed, ret=%d\n", ret);
kmm_free(server);
}
}
/****************************************************************************
* Name: rpmsgblk_ns_unbind
****************************************************************************/
static void rpmsgblk_ns_unbind(FAR struct rpmsg_endpoint *ept)
{
FAR struct rpmsgblk_server_s *server = ept->priv;
rpmsg_destroy_ept(&server->ept);
kmm_free(server);
}
/****************************************************************************
* Name: rpmsgblk_ept_cb
****************************************************************************/
static int rpmsgblk_ept_cb(FAR struct rpmsg_endpoint *ept,
FAR void *data, size_t len, uint32_t src,
FAR void *priv)
{
FAR struct rpmsgblk_header_s *header = data;
uint32_t command = header->command;
if (command < ARRAY_SIZE(g_rpmsgblk_handler))
{
return g_rpmsgblk_handler[command](ept, data, len, src, priv);
}
return -EINVAL;
}
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: rpmsgblk_server_init
*
* Description:
* Rpmsg-mtd server initialize function, the server cpu should call
* this function.
*
* Parameters:
* None
*
* Returned Values:
* OK on success; A negated errno value is returned on any failure.
*
****************************************************************************/
int rpmsgblk_server_init(void)
{
return rpmsg_register_callback(NULL,
NULL,
NULL,
rpmsgblk_ns_match,
rpmsgblk_ns_bind);
}

View File

@ -0,0 +1,90 @@
/****************************************************************************
* include/nuttx/drivers/rpmsgblk.h
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/
#ifndef __INCLUDE_NUTTX_DRIVERS_RPMSGBLK_H
#define __INCLUDE_NUTTX_DRIVERS_RPMSGBLK_H
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
/****************************************************************************
* Public Function Prototypes
****************************************************************************/
#ifdef __cplusplus
#define EXTERN extern "C"
extern "C"
{
#else
#define EXTERN extern
#endif
/****************************************************************************
* Name: rpmsgblk_server_init
*
* Description:
* Rpmsg-device server initialize function, the server cpu should call
* this function.
*
* Parameters:
* None
*
* Returned Values:
* OK on success; A negated errno value is returned on any failure.
*
****************************************************************************/
#ifdef CONFIG_BLK_RPMSG_SERVER
int rpmsgblk_server_init(void);
#endif
/****************************************************************************
* Name: rpmsgblk_register
*
* Description:
* Rpmsg-device client initialize function, the client cpu should call
* this function in the board initialize process.
*
* Parameters:
* remotecpu - the server cpu name
* remotepath - the device you want to access in the remote cpu
* localpath - the device path in local cpu, if NULL, the localpath is
* same as the remotepath, provide this argument to supoort
* custom device path
*
* Returned Values:
* OK on success; A negated errno value is returned on any failure.
*
****************************************************************************/
#ifdef CONFIG_BLK_RPMSG
int rpmsgblk_register(FAR const char *remotecpu, FAR const char *remotepath,
FAR const char *localpath);
#endif
#undef EXTERN
#if defined(__cplusplus)
}
#endif
#endif /* __INCLUDE_NUTTX_DRIVERS_RPMSGDEV_H */