From f4782b65f8b1fdac8e92ac15f99484630b2bd73f Mon Sep 17 00:00:00 2001 From: lzl Date: Sat, 11 Dec 2021 06:46:48 +0000 Subject: [PATCH 01/10] PCIE Framework code Signed-off-by: lzl --- include/platform/pcie_if.h | 101 +++++++ support/platform/include/pcie/pcie_core.h | 87 ++++++ support/platform/include/pcie/pcie_dispatch.h | 38 +++ support/platform/src/pcie/pcie_core.c | 183 +++++++++++++ support/platform/src/pcie/pcie_dispatch.c | 139 ++++++++++ support/platform/src/pcie/pcie_if.c | 252 ++++++++++++++++++ .../test/unittest/common/hdf_pcie_test.cpp | 128 +++++++++ test/unittest/common/hdf_main_test.c | 6 + test/unittest/platform/common/pcie_test.c | 197 ++++++++++++++ test/unittest/platform/common/pcie_test.h | 32 +++ .../platform/entry/hdf_pcie_entry_test.c | 32 +++ .../platform/entry/hdf_pcie_entry_test.h | 16 ++ test/unittest/platform/virtual/pcie_virtual.c | 167 ++++++++++++ 13 files changed, 1378 insertions(+) create mode 100644 include/platform/pcie_if.h create mode 100644 support/platform/include/pcie/pcie_core.h create mode 100644 support/platform/include/pcie/pcie_dispatch.h create mode 100644 support/platform/src/pcie/pcie_core.c create mode 100644 support/platform/src/pcie/pcie_dispatch.c create mode 100644 support/platform/src/pcie/pcie_if.c create mode 100644 support/platform/test/unittest/common/hdf_pcie_test.cpp create mode 100644 test/unittest/platform/common/pcie_test.c create mode 100644 test/unittest/platform/common/pcie_test.h create mode 100644 test/unittest/platform/entry/hdf_pcie_entry_test.c create mode 100644 test/unittest/platform/entry/hdf_pcie_entry_test.h create mode 100644 test/unittest/platform/virtual/pcie_virtual.c diff --git a/include/platform/pcie_if.h b/include/platform/pcie_if.h new file mode 100644 index 00000000..d15c0b37 --- /dev/null +++ b/include/platform/pcie_if.h @@ -0,0 +1,101 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * + * HDF is dual licensed: you can use it either under the terms of + * the GPL, or the BSD license, at your option. + * See the LICENSE file in the root of this repository for complete details. + */ + +/** + * @addtogroup PCIE + * @{ + * + * @brief Declares standard APIs of basic Peripheral Component Interconnect Express (PCIE) capabilities. + * + * You can use this module to access the PCIE and enable the driver to operate an PCIE device. + * These capabilities include read and write the PCIE configuration Space. + * + * @since 1.0 + */ + +/** + * @file pcie_if.h + * + * @brief Declares the standard PCIE interface functions. + * + * @since 1.0 + */ + +#ifndef PCIE_IF_H +#define PCIE_IF_H + +#include "hdf_platform.h" + +#ifdef __cplusplus +#if __cplusplus +extern "C" { +#endif +#endif /* __cplusplus */ + +/** + * @brief Opens an PCIE controller with a specified bus number. + * + * Before using the PCIE interface, you can obtain the device handle of the PCIE controller + * by calling {@link PcieOpen}. This function is used in pair with {@link PcieClose}. + * + * @param busNum Indicates the bus number. + * + * @return Returns the device handle {@link DevHandle} of the PCIE controller if the operation is successful; + * returns NULL otherwise. + * + * @since 1.0 + */ +DevHandle PcieOpen(uint16_t busNum); + +/** + * @brief Reads a given length of data from the PCIE configuration Space. + * + * @param handle Indicates the pointer to the device handle of the PCIE controller obtained by {@link PcieOpen}. + * @param pos Indicates the start address of the data to read. + * @param data Indicates the pointer to the data to read. + * @param len Indicates the length of the data to read. + * + * @return Returns 0 if the operation is successful; returns a negative value if the operation fails. + * + * @since 1.0 + */ +int32_t PcieRead(DevHandle handle, uint32_t pos, uint8_t *data, uint32_t len); + +/** + * @brief Writes a given length of data into the PCIE configuration Space. + * + * @param handle Indicates the pointer to the device handle of the PCIE controller obtained by {@link PcieOpen}. + * @param pos Indicates the start address of the data to write. + * @param data Indicates the pointer to the data to write. + * @param len Indicates the length of the data to write. + * + * @return Returns 0 if the operation is successful; returns a negative value if the operation fails. + * + * @since 1.0 + */ +int32_t PcieWrite(DevHandle handle, uint32_t pos, uint8_t *data, uint32_t len); + +/** + * @brief Closes an PCIE controller. + * + * After the PCIE interface is used, you can close the PCIE controller by calling {@link PcieClose}. + * This function is used in pair with {@link PcieOpen}. + * + * @param handle Indicates the pointer to the device handle of the PCIE controller. + * + * @since 1.0 + */ +void PcieClose(DevHandle handle); + +#ifdef __cplusplus +#if __cplusplus +} +#endif +#endif /* __cplusplus */ + +#endif /* PCIE_IF_H */ diff --git a/support/platform/include/pcie/pcie_core.h b/support/platform/include/pcie/pcie_core.h new file mode 100644 index 00000000..92d7fc18 --- /dev/null +++ b/support/platform/include/pcie/pcie_core.h @@ -0,0 +1,87 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * + * HDF is dual licensed: you can use it either under the terms of + * the GPL, or the BSD license, at your option. + * See the LICENSE file in the root of this repository for complete details. + */ + +#ifndef PCIE_CORE_H +#define PCIE_CORE_H + +#include "hdf_base.h" +#include "hdf_device_desc.h" +#include "osal_mutex.h" +#include "platform_core.h" + +#ifdef __cplusplus +#if __cplusplus +extern "C" { +#endif +#endif /* __cplusplus */ + +struct PcieCntlr; + +struct PcieCntlrOps { + int32_t (*init)(struct PcieCntlr *cntlr, uint16_t busNum); + int32_t (*read)(struct PcieCntlr *cntlr, uint32_t pos, uint8_t *data, uint32_t len); + int32_t (*write)(struct PcieCntlr *cntlr, uint32_t pos, uint8_t *data, uint32_t len); + int32_t (*deinit)(struct PcieCntlr *cntlr); +}; + +struct PcieDevCfgInfo { + uint32_t busNum; + uint32_t vendorId; + uint32_t devId; +}; + +struct PcieCntlr { + struct IDeviceIoService service; + struct HdfDeviceObject *hdfDevObj; + struct PlatformDevice device; + struct OsalMutex mutex; + struct PcieCntlrOps *ops; + struct PcieDevCfgInfo devInfo; + void *priv; +}; + +static inline void PcieCntlrLock(struct PcieCntlr *cntlr) +{ + if (cntlr != NULL) { + (void)OsalMutexLock(&cntlr->mutex); + } +} + +static inline void PcieCntlrUnlock(struct PcieCntlr *cntlr) +{ + if (cntlr != NULL) { + (void)OsalMutexUnlock(&cntlr->mutex); + } +} + +static inline struct PcieCntlr *PcieCntlrGetByBusNum(uint16_t num) +{ + struct PlatformDevice *device = PlatformManagerGetDeviceByMagic(PlatformManagerGet(PLATFORM_MODULE_PCIE), num); + + if (device == NULL) { + return NULL; + } + return CONTAINER_OF(device, struct PcieCntlr, device); +} + +int32_t PcieCntlrParse(struct PcieCntlr *cntlr, struct HdfDeviceObject *obj); +int32_t PcieCntlrAdd(struct PcieCntlr *cntlr); +void PcieCntlrRemove(struct PcieCntlr *cntlr); + +int32_t PcieCntlrOpen(struct PcieCntlr *cntlr, uint16_t busNum); +int32_t PcieCntlrRead(struct PcieCntlr *cntlr, uint32_t pos, uint8_t *data, uint32_t len); +int32_t PcieCntlrWrite(struct PcieCntlr *cntlr, uint32_t pos, uint8_t *data, uint32_t len); +void PcieCntlrClose(struct PcieCntlr *cntlr); + +#ifdef __cplusplus +#if __cplusplus +} +#endif +#endif /* __cplusplus */ + +#endif /* PCIE_CORE_H */ diff --git a/support/platform/include/pcie/pcie_dispatch.h b/support/platform/include/pcie/pcie_dispatch.h new file mode 100644 index 00000000..2ce6b25b --- /dev/null +++ b/support/platform/include/pcie/pcie_dispatch.h @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * + * HDF is dual licensed: you can use it either under the terms of + * the GPL, or the BSD license, at your option. + * See the LICENSE file in the root of this repository for complete details. + */ + +#ifndef PCIE_DISPATCH_H +#define PCIE_DISPATCH_H + +#include "hdf_base.h" + +#ifndef __USER__ +#include "devsvc_manager_clnt.h" +#endif + +#ifdef __USER__ +#include "hdf_io_service_if.h" +#endif + +#ifdef __cplusplus +#if __cplusplus +extern "C" { +#endif +#endif /* __cplusplus */ + +#ifndef __USER__ +int32_t PcieIoDispatch(struct HdfDeviceIoClient *client, int cmd, struct HdfSBuf *data, struct HdfSBuf *reply); +#endif + +#ifdef __cplusplus +#if __cplusplus +} +#endif +#endif /* __cplusplus */ + +#endif /* PCIE_DISPATCH_H */ diff --git a/support/platform/src/pcie/pcie_core.c b/support/platform/src/pcie/pcie_core.c new file mode 100644 index 00000000..b04625a8 --- /dev/null +++ b/support/platform/src/pcie/pcie_core.c @@ -0,0 +1,183 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * + * HDF is dual licensed: you can use it either under the terms of + * the GPL, or the BSD license, at your option. + * See the LICENSE file in the root of this repository for complete details. + */ + +#include "pcie_core.h" +#include "device_resource_if.h" +#include "hdf_log.h" +#include "osal_mem.h" +#include "osal_time.h" +#include "pcie_dispatch.h" +#include "securec.h" + +#define HDF_LOG_TAG pcie_core_c + +static int32_t PcieCntlrInit(struct PcieCntlr *cntlr) +{ + int32_t ret; + + if (cntlr == NULL) { + return HDF_ERR_INVALID_OBJECT; + } + + if (cntlr->hdfDevObj == NULL) { + HDF_LOGE("PcieCntlrInit: no HdfDeviceObject attached!"); + return HDF_ERR_INVALID_OBJECT; + } + + ret = OsalMutexInit(&cntlr->mutex); + if (ret != HDF_SUCCESS) { + HDF_LOGE("PcieCntlrInit: mutex init fail!"); + return ret; + } + + cntlr->service.Dispatch = PcieIoDispatch; + cntlr->hdfDevObj->service = &(cntlr->service); + cntlr->device.magic = cntlr->devInfo.busNum; + cntlr->device.hdfDev = cntlr->hdfDevObj; + return HDF_SUCCESS; +} + +static void PcieCntlrUninit(struct PcieCntlr *cntlr) +{ + if (cntlr != NULL) { + (void)OsalMutexDestroy(&cntlr->mutex); + } +} + +int32_t PcieCntlrAdd(struct PcieCntlr *cntlr) +{ + int32_t ret; + + if (cntlr == NULL) { + return HDF_ERR_INVALID_OBJECT; + } + + ret = PcieCntlrInit(cntlr); + if (ret != HDF_SUCCESS) { + return ret; + } + + cntlr->device.manager = PlatformManagerGet(PLATFORM_MODULE_PCIE); + ret = PlatformDeviceAdd(&cntlr->device); + if (ret != HDF_SUCCESS) { + HDF_LOGE("PcieCntlrAdd: device add fail!"); + PcieCntlrUninit(cntlr); + return ret; + } + return HDF_SUCCESS; +} + +void PcieCntlrRemove(struct PcieCntlr *cntlr) +{ + if (cntlr != NULL) { + PcieCntlrUninit(cntlr); + PlatformDeviceDel(&cntlr->device); + } +} + +int32_t PcieCntlrParse(struct PcieCntlr *cntlr, struct HdfDeviceObject *obj) +{ + const struct DeviceResourceNode *node = NULL; + struct DeviceResourceIface *drsOps = NULL; + int32_t ret; + + if (obj == NULL || cntlr == NULL) { + HDF_LOGE("PcieCntlrParse: input param is NULL."); + return HDF_FAILURE; + } + + node = obj->property; + if (node == NULL) { + HDF_LOGE("PcieCntlrParse: drs node is NULL."); + return HDF_FAILURE; + } + drsOps = DeviceResourceGetIfaceInstance(HDF_CONFIG_SOURCE); + if (drsOps == NULL || drsOps->GetString == NULL || drsOps->GetUint32 == NULL) { + HDF_LOGE("PcieCntlrParse: invalid drs ops fail."); + return HDF_FAILURE; + } + + ret = drsOps->GetUint32(node, "busNum", &(cntlr->devInfo.busNum), 0); + if (ret != HDF_SUCCESS) { + HDF_LOGE("PcieCntlrParse: read busNum fail!"); + return ret; + } + + ret = drsOps->GetUint32(node, "vendorId", &(cntlr->devInfo.vendorId), 0); + if (ret != HDF_SUCCESS) { + HDF_LOGE("PcieCntlrParse: read vendorId fail!"); + return HDF_FAILURE; + } + + ret = drsOps->GetUint32(node, "devId", &(cntlr->devInfo.devId), 0); + if (ret != HDF_SUCCESS) { + HDF_LOGE("PcieCntlrParse: read devId fail!"); + return HDF_FAILURE; + } + return HDF_SUCCESS; +} + +int32_t PcieCntlrOpen(struct PcieCntlr *cntlr, uint16_t busNum) +{ + int32_t ret; + + if (cntlr == NULL || cntlr->ops == NULL || cntlr->ops->init == NULL) { + return HDF_ERR_INVALID_OBJECT; + } + + PcieCntlrLock(cntlr); + ret = cntlr->ops->init(cntlr, busNum); + PcieCntlrUnlock(cntlr); + return ret; +} + +int32_t PcieCntlrRead(struct PcieCntlr *cntlr, uint32_t pos, uint8_t *data, uint32_t len) +{ + int32_t ret; + + if (cntlr == NULL || cntlr->ops == NULL || cntlr->ops->read == NULL) { + return HDF_ERR_INVALID_OBJECT; + } + if (data == NULL || len == 0) { + return HDF_ERR_INVALID_PARAM; + } + + PcieCntlrLock(cntlr); + ret = cntlr->ops->read(cntlr, pos, data, len); + PcieCntlrUnlock(cntlr); + return ret; +} + +int32_t PcieCntlrWrite(struct PcieCntlr *cntlr, uint32_t pos, uint8_t *data, uint32_t len) +{ + int32_t ret; + if (cntlr == NULL || cntlr->ops == NULL || cntlr->ops->write == NULL) { + return HDF_ERR_INVALID_OBJECT; + } + if (data == NULL || len == 0) { + return HDF_ERR_INVALID_PARAM; + } + + PcieCntlrLock(cntlr); + ret = cntlr->ops->write(cntlr, pos, data, len); + PcieCntlrUnlock(cntlr); + return ret; +} + +void PcieCntlrClose(struct PcieCntlr *cntlr) +{ + if (cntlr == NULL || cntlr->ops == NULL || cntlr->ops->deinit == NULL) { + return; + } + + PcieCntlrLock(cntlr); + if (cntlr->ops->deinit(cntlr) != HDF_SUCCESS) { + HDF_LOGE("pcie deinit fail"); + } + PcieCntlrUnlock(cntlr); +} \ No newline at end of file diff --git a/support/platform/src/pcie/pcie_dispatch.c b/support/platform/src/pcie/pcie_dispatch.c new file mode 100644 index 00000000..be1e5093 --- /dev/null +++ b/support/platform/src/pcie/pcie_dispatch.c @@ -0,0 +1,139 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * + * HDF is dual licensed: you can use it either under the terms of + * the GPL, or the BSD license, at your option. + * See the LICENSE file in the root of this repository for complete details. + */ + +#include "pcie_dispatch.h" +#include "hdf_log.h" +#include "osal_mem.h" +#include "pcie_core.h" +#include "pcie_if.h" + +#define HDF_LOG_TAG pcie_dispatch_c + +enum PcieIoCmd { + PCIE_CMD_OPEN, + PCIE_CMD_CLOSE, + PCIE_CMD_READ, + PCIE_CMD_WRITE, + PCIE_CMD_BUTT, +}; + +struct PcieDispatchFunc { + int32_t cmd; + int32_t (*func)(struct PcieCntlr *cntlr, struct HdfSBuf *data, struct HdfSBuf *reply); +}; + +static int32_t PcieCmdOpen(struct PcieCntlr *cntlr, struct HdfSBuf *data, struct HdfSBuf *reply) +{ + uint16_t busNum; + (void)reply; + + if (!HdfSbufReadUint16(data, &busNum)) { + HDF_LOGE("PcieCmdRead: read busNum fail"); + return HDF_ERR_IO; + } + return PcieCntlrOpen(cntlr, busNum); +} + +static int32_t PcieCmdClose(struct PcieCntlr *cntlr, struct HdfSBuf *data, struct HdfSBuf *reply) +{ + (void)data; + (void)reply; + + PcieCntlrClose(cntlr); + return HDF_SUCCESS; +} + +static int32_t PcieCmdRead(struct PcieCntlr *cntlr, struct HdfSBuf *data, struct HdfSBuf *reply) +{ + uint32_t len, pos; + uint8_t *buf = NULL; + int32_t ret; + + if (!HdfSbufReadUint32(data, &len)) { + HDF_LOGE("PcieCmdRead: read len fail"); + return HDF_ERR_IO; + } + if (len == 0) { + return HDF_ERR_INVALID_PARAM; + } + if (!HdfSbufReadUint32(data, &pos)) { + HDF_LOGE("PcieCmdRead: read pos fail"); + return HDF_ERR_IO; + } + + buf = (uint8_t *)OsalMemCalloc(sizeof(*buf) * len); + if (buf == NULL) { + HDF_LOGE("PcieCmdRead: OsalMemCalloc error"); + return HDF_ERR_MALLOC_FAIL; + } + + ret = PcieCntlrRead(cntlr, pos, buf, len); + if (ret != HDF_SUCCESS) { + HDF_LOGE("PcieCntlrRead: error, ret is %d", ret); + goto _EXIST; + } + if (!HdfSbufWriteBuffer(reply, buf, len)) { + HDF_LOGE("PcieCntlrRead: sbuf write buffer failed"); + ret = HDF_ERR_IO; + goto _EXIST; + } + ret = HDF_SUCCESS; +_EXIST: + OsalMemFree(buf); + return ret; +} + +static int32_t PcieCmdWrite(struct PcieCntlr *cntlr, struct HdfSBuf *data, struct HdfSBuf *reply) +{ + uint32_t size, pos; + uint8_t *buf = NULL; + (void)reply; + + if (!HdfSbufReadUint32(data, &pos)) { + HDF_LOGE("PcieCmdWrite: read pos fail"); + return HDF_ERR_IO; + } + if (!HdfSbufReadBuffer(data, (const void **)&buf, &size)) { + HDF_LOGE("PcieCmdWrite: sbuf read buffer failed"); + return HDF_ERR_IO; + } + return PcieCntlrWrite(cntlr, pos, buf, size); +} + +int32_t PcieIoDispatch(struct HdfDeviceIoClient *client, int32_t cmd, struct HdfSBuf *data, struct HdfSBuf *reply) +{ + struct PcieCntlr *cntlr = NULL; + uint32_t i, len; + struct PcieDispatchFunc dispatchFunc[] = { + { PCIE_CMD_OPEN, PcieCmdOpen }, + { PCIE_CMD_CLOSE, PcieCmdClose }, + { PCIE_CMD_READ, PcieCmdRead }, + { PCIE_CMD_WRITE, PcieCmdWrite }, + }; + + if (client == NULL || client->device == NULL) { + HDF_LOGE("PcieIoDispatch: client or hdf dev obj is NULL"); + return HDF_ERR_INVALID_OBJECT; + } + + cntlr = (struct PcieCntlr *)client->device->service; + if (cntlr == NULL) { + HDF_LOGE("PcieIoDispatch: service is NULL"); + return HDF_ERR_INVALID_OBJECT; + } + + len = sizeof(dispatchFunc) / sizeof(dispatchFunc[0]); + for (i = 0; i < len; i++) { + if (dispatchFunc[i].cmd == cmd) { + return dispatchFunc[i].func(cntlr, data, reply); + } + } + + HDF_LOGE("PcieIoDispatch: cmd %d is not support", cmd); + return HDF_ERR_NOT_SUPPORT; +} diff --git a/support/platform/src/pcie/pcie_if.c b/support/platform/src/pcie/pcie_if.c new file mode 100644 index 00000000..7b6ac2c6 --- /dev/null +++ b/support/platform/src/pcie/pcie_if.c @@ -0,0 +1,252 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * + * HDF is dual licensed: you can use it either under the terms of + * the GPL, or the BSD license, at your option. + * See the LICENSE file in the root of this repository for complete details. + */ + +#include "pcie_if.h" +#ifndef __USER__ +#include "devsvc_manager_clnt.h" +#include "pcie_core.h" +#endif +#include "hdf_base.h" +#include "hdf_log.h" +#ifdef __USER__ +#include "hdf_io_service_if.h" +#endif +#include "osal_mem.h" +#include "securec.h" + +#define HDF_LOG_TAG pcie_if_c + +#define PCIE_SERVICE_NAME_LEN 32 + +#ifdef __USER__ +enum PcieIoCmd { + PCIE_CMD_OPEN, + PCIE_CMD_CLOSE, + PCIE_CMD_READ, + PCIE_CMD_WRITE, + PCIE_CMD_BUTT, +}; + +static int32_t PcieGetDataFromReply(struct HdfSBuf *reply, uint8_t *data, uint32_t size) +{ + uint32_t rLen; + const void *rBuf = NULL; + + if (HdfSbufReadBuffer(reply, &rBuf, &rLen) == false) { + HDF_LOGE("PcieGetDataFromReply: read rBuf fail!"); + return HDF_ERR_IO; + } + if (size != rLen) { + HDF_LOGE("PcieGetDataFromReply: err len:%u, rLen:%u", size, rLen); + if (rLen > size) { + rLen = size; + } + } + + if (memcpy_s(data, size, rBuf, rLen) != EOK) { + HDF_LOGE("PcieGetDataFromReply: memcpy rBuf fail!"); + return HDF_ERR_IO; + } + return HDF_SUCCESS; +} + +static void PcieUserClose(DevHandle handle) +{ + struct HdfIoService *service = (struct HdfIoService *)handle; + int32_t ret; + + if (service == NULL || service->dispatcher == NULL || service->dispatcher->Dispatch == NULL) { + HDF_LOGE("PcieUserClose: service is invalid"); + return; + } + + ret = service->dispatcher->Dispatch(&service->object, PCIE_CMD_CLOSE, NULL, NULL); + if (ret != HDF_SUCCESS) { + HDF_LOGE("PcieUserClose: failed to send service call:%d", ret); + } +} + +static int32_t PcieUserRead(DevHandle handle, uint32_t pos, uint8_t *data, uint32_t len) +{ + int32_t ret; + struct HdfSBuf *reply = NULL; + struct HdfSBuf *buf = NULL; + struct HdfIoService *service = (struct HdfIoService *)handle; + + if (service == NULL || service->dispatcher == NULL || service->dispatcher->Dispatch == NULL) { + HDF_LOGE("PcieUserRead: service is invalid"); + return HDF_ERR_INVALID_PARAM; + } + if (data == NULL || len == 0) { + return HDF_ERR_INVALID_PARAM; + } + + buf = HdfSBufObtainDefaultSize(); + if (buf == NULL) { + HDF_LOGE("PcieUserRead: failed to obtain buf"); + ret = HDF_ERR_MALLOC_FAIL; + goto __EXIT; + } + if (!HdfSbufWriteUint32(buf, len)) { + HDF_LOGE("PcieUserRead: sbuf write uint32 failed"); + ret = HDF_ERR_IO; + goto __EXIT; + } + if (!HdfSbufWriteUint32(buf, pos)) { + HDF_LOGE("PcieUserRead: sbuf write uint32 failed"); + ret = HDF_ERR_IO; + goto __EXIT; + } + + reply = HdfSBufObtainDefaultSize(); + if (reply == NULL) { + HDF_LOGE("PcieUserRead: failed to obtain reply"); + ret = HDF_ERR_MALLOC_FAIL; + goto __EXIT; + } + + ret = service->dispatcher->Dispatch(&service->object, PCIE_CMD_READ, buf, reply); + if (ret != HDF_SUCCESS) { + HDF_LOGE("PcieUserRead: failed to write, ret %d", ret); + } else { + ret = PcieGetDataFromReply(reply, data, len); + } + +__EXIT : + if (reply != NULL) { + HdfSBufRecycle(reply); + } + if (buf != NULL) { + HdfSBufRecycle(buf); + } + return ret; +} + +static int32_t PcieUserWrite(DevHandle handle, uint32_t pos, uint8_t *data, uint32_t len) +{ + int32_t ret; + struct HdfSBuf *buf = NULL; + struct HdfIoService *service = (struct HdfIoService *)handle; + + if (service == NULL || service->dispatcher == NULL || service->dispatcher->Dispatch == NULL) { + HDF_LOGE("PcieUserWrite: service is invalid"); + return HDF_ERR_INVALID_PARAM; + } + buf = HdfSBufObtainDefaultSize(); + if (buf == NULL) { + HDF_LOGE("PcieUserWrite: failed to obtain buf"); + return HDF_ERR_MALLOC_FAIL; + } + if (!HdfSbufWriteUint32(buf, pos)) { + HDF_LOGE("PcieUserWrite: sbuf write uint32 failed"); + ret = HDF_ERR_IO; + goto __EXIT; + } + if (!HdfSbufWriteBuffer(buf, data, len)) { + HDF_LOGE("PcieUserWrite: sbuf write buffer failed"); + ret = HDF_ERR_IO; + goto __EXIT; + } + + ret = service->dispatcher->Dispatch(&service->object, PCIE_CMD_WRITE, buf, NULL); + if (ret != HDF_SUCCESS) { + HDF_LOGE("PcieUserWrite: failed to write, ret %d", ret); + } +__EXIT: + HdfSBufRecycle(buf); + return ret; +} +#endif + +static void *PcieCntlrObjGet(uint16_t busNum) +{ + char *serviceName = NULL; + void *obj = NULL; + + serviceName = (char *)OsalMemCalloc(PCIE_SERVICE_NAME_LEN + 1); + if (serviceName == NULL) { + HDF_LOGE("HDMI service name malloc fail."); + return NULL; + } + if (snprintf_s(serviceName, (PCIE_SERVICE_NAME_LEN + 1), + PCIE_SERVICE_NAME_LEN, "HDF_PLATFORM_PCIE_%d", busNum) < 0) { + HDF_LOGE("get PCIE service name fail."); + goto __ERR; + } +#ifdef __USER__ + obj = (void *)HdfIoServiceBind(serviceName); +#else + obj = (void *)PcieCntlrGetByBusNum(busNum); +#endif +__ERR: + OsalMemFree(serviceName); + return obj; +} + +DevHandle PcieOpen(uint16_t busNum) +{ + DevHandle *obj = (DevHandle *)PcieCntlrObjGet(busNum); + int32_t ret; + + if (obj == NULL) { + return NULL; + } +#ifdef __USER__ + struct HdfIoService *service = (struct HdfIoService *)obj; + if (service->dispatcher == NULL || service->dispatcher->Dispatch == NULL) { + HDF_LOGE("PcieOpen: dispatcher or Dispatch is NULL!"); + return NULL; + } + + struct HdfSBuf *buf = HdfSBufObtainDefaultSize(); + if (!HdfSbufWriteUint16(buf, busNum)) { + HDF_LOGE("PcieOpen: sbuf write uint16 failed"); + HdfSBufRecycle(buf); + return NULL; + } + ret = service->dispatcher->Dispatch(&service->object, PCIE_CMD_OPEN, buf, NULL); + if (ret != HDF_SUCCESS) { + HDF_LOGE("PcieOpen: failed to send service call:%d", ret); + return NULL; + } + HdfSBufRecycle(buf); +#else + ret = PcieCntlrOpen((struct PcieCntlr *)obj, busNum); + if (ret != HDF_SUCCESS) { + return NULL; + } +#endif + return obj; +} + +int32_t PcieRead(DevHandle handle, uint32_t pos, uint8_t *data, uint32_t len) +{ +#ifdef __USER__ + return PcieUserRead(handle, pos, data, len); +#else + return PcieCntlrRead((struct PcieCntlr *)handle, pos, data, len); +#endif +} + +int32_t PcieWrite(DevHandle handle, uint32_t pos, uint8_t *data, uint32_t len) +{ +#ifdef __USER__ + return PcieUserWrite(handle, pos, data, len); +#else + return PcieCntlrWrite((struct PcieCntlr *)handle, pos, data, len); +#endif +} + +void PcieClose(DevHandle handle) +{ +#ifdef __USER__ + PcieUserClose(handle); +#else + PcieCntlrClose((struct PcieCntlr *)handle); +#endif +} diff --git a/support/platform/test/unittest/common/hdf_pcie_test.cpp b/support/platform/test/unittest/common/hdf_pcie_test.cpp new file mode 100644 index 00000000..0b8f942a --- /dev/null +++ b/support/platform/test/unittest/common/hdf_pcie_test.cpp @@ -0,0 +1,128 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * + * HDF is dual licensed: you can use it either under the terms of + * the GPL, or the BSD license, at your option. + * See the LICENSE file in the root of this repository for complete details. + */ + +#include +#include +#include +#include +#include +#include +#include +#include "hdf_io_service_if.h" +#include "hdf_uhdf_test.h" +#include "pcie_if.h" + +using namespace testing::ext; + +#define PCIE_DISABLE_ADDR 0xB7 +#define PCIE_UPPER_ADDR 0x28 +#define PCIE_CMD_ADDR 0x04 + +enum PcieTestCmd { + PCIE_READ_AND_WRITE_01 = 0, +}; + +class HdfPcieTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + void SetUp(); + void TearDown(); +}; + +void HdfPcieTest::SetUpTestCase() +{ + HdfTestOpenService(); +} + +void HdfPcieTest::TearDownTestCase() +{ + HdfTestCloseService(); +} + +void HdfPcieTest::SetUp() +{ +} + +void HdfPcieTest::TearDown() +{ +} + +static void PcieUserTest(void) +{ + DevHandle handle = NULL; + int32_t ret; + uint8_t disable; + uint32_t upper; + uint16_t cmd; + + handle = PcieOpen(0); + if (handle == NULL) { + printf("PcieOpen fail."); + } + + ret = PcieRead(handle, PCIE_DISABLE_ADDR, &disable, sizeof(disable)); + if (ret != HDF_SUCCESS) { + printf("PcieRead failed ret = %d.", ret); + return; + } + printf("disable is %d", disable); + ret = PcieWrite(handle, PCIE_DISABLE_ADDR, &disable, sizeof(disable)); + if (ret != HDF_SUCCESS) { + printf("PcieWrite failed ret = %d.", ret); + return; + } + + ret = PcieRead(handle, PCIE_UPPER_ADDR, (uint8_t *)&upper, sizeof(upper)); + if (ret != HDF_SUCCESS) { + printf("PcieRead failed ret = %d.", ret); + return; + } + printf("upper is 0x%x", upper); + ret = PcieWrite(handle, PCIE_UPPER_ADDR, (uint8_t *)&upper, sizeof(upper)); + if (ret != HDF_SUCCESS) { + printf("PcieWrite failed ret = %d.", ret); + return; + } + + ret = PcieRead(handle, PCIE_CMD_ADDR, (uint8_t *)&cmd, sizeof(cmd)); + if (ret != HDF_SUCCESS) { + printf("PcieRead failed ret = %d.", ret); + return; + } + printf("cmd is 0x%x", cmd); + ret = PcieWrite(handle, PCIE_CMD_ADDR, (uint8_t *)&cmd, sizeof(cmd)); + if (ret != HDF_SUCCESS) { + printf("PcieWrite failed ret = %d.", ret); + return; + } + PcieClose(handle); +} + +/** + * @tc.name: PcieReadAndWrite001 + * @tc.desc: test PcieRead/PcieWrite interface in kernel status. + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(HdfPcieTest, PcieReadAndWrite001, TestSize.Level1) +{ + struct HdfTestMsg msg = { TEST_PAL_PCIE_TYPE, PCIE_READ_AND_WRITE_01, -1 }; + EXPECT_EQ(0, HdfTestSendMsgToService(&msg)); +} + +/** + * @tc.name: PcieUserTest001 + * @tc.desc: test pcie all interface in user status. + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(HdfPcieTest, PcieUserTest001, TestSize.Level1) +{ + PcieUserTest(); +} diff --git a/test/unittest/common/hdf_main_test.c b/test/unittest/common/hdf_main_test.c index 52a8d2f1..24dac1f2 100644 --- a/test/unittest/common/hdf_main_test.c +++ b/test/unittest/common/hdf_main_test.c @@ -44,6 +44,9 @@ #if defined(LOSCFG_DRIVERS_HDF_PLATFORM_SDIO) || defined(CONFIG_DRIVERS_HDF_PLATFORM_SDIO) #include "hdf_sdio_entry_test.h" #endif +#if defined(LOSCFG_DRIVERS_HDF_PLATFORM_PCIE) || defined(CONFIG_DRIVERS_HDF_PLATFORM_PCIE) +#include "hdf_pcie_entry_test.h" +#endif #if defined(LOSCFG_DRIVERS_HDF_PLATFORM_UART) || defined(CONFIG_DRIVERS_HDF_PLATFORM_UART) #include "hdf_uart_entry_test.h" #endif @@ -131,6 +134,9 @@ HdfTestFuncList g_hdfTestFuncList[] = { #if defined(LOSCFG_DRIVERS_HDF_PLATFORM_SDIO) || defined(CONFIG_DRIVERS_HDF_PLATFORM_SDIO) { TEST_PAL_SDIO_TYPE, HdfSdioUnitTestEntry }, #endif +#if defined(LOSCFG_DRIVERS_HDF_PLATFORM_PCIE) || defined(CONFIG_DRIVERS_HDF_PLATFORM_PCIE) + { TEST_PAL_PCIE_TYPE, HdfPcieUnitTestEntry }, +#endif #if (defined(LOSCFG_STORAGE_EMMC) && defined(LOSCFG_DRIVERS_HDF_PLATFORM_EMMC)) || \ defined(CONFIG_DRIVERS_HDF_PLATFORM_EMMC) { TEST_PAL_EMMC_TYPE, HdfEmmcUnitTestEntry }, diff --git a/test/unittest/platform/common/pcie_test.c b/test/unittest/platform/common/pcie_test.c new file mode 100644 index 00000000..3de7e5c3 --- /dev/null +++ b/test/unittest/platform/common/pcie_test.c @@ -0,0 +1,197 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * + * HDF is dual licensed: you can use it either under the terms of + * the GPL, or the BSD license, at your option. + * See the LICENSE file in the root of this repository for complete details. + */ + +#include "device_resource_if.h" +#include "hdf_base.h" +#include "hdf_log.h" +#include "osal_time.h" +#include "pcie_if.h" +#include "pcie_test.h" + +#define HDF_LOG_TAG pcie_test_c + +#define PCIE_TEST_DISABLE_ADDR 0xB7 +#define PCIE_TEST_UPPER_ADDR 0x28 +#define PCIE_TEST_CMD_ADDR 0x04 + +struct PcieTestFunc { + enum PcieTestCmd type; + int32_t (*Func)(struct PcieTester *tester); +}; + +static DevHandle PcieTestGetHandle(struct PcieTester *tester) +{ + if (tester == NULL) { + HDF_LOGE("%s: tester is null", __func__); + return NULL; + } + return PcieOpen(tester->busNum); +} + +static void PcieTestReleaseHandle(DevHandle handle) +{ + if (handle == NULL) { + HDF_LOGE("%s: pcie handle is null", __func__); + return; + } + PcieClose(handle); +} + +static int32_t TestPcieReadAndWrite(struct PcieTester *tester) +{ + int32_t ret; + uint8_t disable; + uint32_t upper; + uint16_t cmd; + + ret = PcieRead(tester->handle, PCIE_TEST_DISABLE_ADDR, &disable, sizeof(disable)); + if (ret != HDF_SUCCESS) { + HDF_LOGE("%s: PcieRead failed ret = %d.", __func__, ret); + return ret; + } + HDF_LOGD("%s: disable is %d", __func__, disable); + ret = PcieWrite(tester->handle, PCIE_TEST_DISABLE_ADDR, &disable, sizeof(disable)); + if (ret != HDF_SUCCESS) { + HDF_LOGE("%s: PcieWrite failed ret = %d.", __func__, ret); + return ret; + } + + ret = PcieRead(tester->handle, PCIE_TEST_UPPER_ADDR, (uint8_t *)&upper, sizeof(upper)); + if (ret != HDF_SUCCESS) { + HDF_LOGE("%s: PcieRead failed ret = %d.", __func__, ret); + return ret; + } + HDF_LOGD("%s: upper is 0x%x", __func__, upper); + ret = PcieWrite(tester->handle, PCIE_TEST_UPPER_ADDR, (uint8_t *)&upper, sizeof(upper)); + if (ret != HDF_SUCCESS) { + HDF_LOGE("%s: PcieWrite failed ret = %d.", __func__, ret); + return ret; + } + + ret = PcieRead(tester->handle, PCIE_TEST_CMD_ADDR, (uint8_t *)&cmd, sizeof(cmd)); + if (ret != HDF_SUCCESS) { + HDF_LOGE("%s: PcieRead failed ret = %d.", __func__, ret); + return ret; + } + HDF_LOGD("%s: cmd is 0x%x", __func__, cmd); + ret = PcieWrite(tester->handle, PCIE_TEST_CMD_ADDR, (uint8_t *)&cmd, sizeof(cmd)); + if (ret != HDF_SUCCESS) { + HDF_LOGE("%s: PcieWrite failed ret = %d.", __func__, ret); + } + return ret; +} + +struct PcieTestFunc g_pcieTestFunc[] = { + { PCIE_READ_AND_WRITE_01, TestPcieReadAndWrite }, +}; + +static int32_t PcieTestEntry(struct PcieTester *tester, int32_t cmd) +{ + int32_t i; + int32_t ret = HDF_SUCCESS; + bool isFind = false; + + if (tester == NULL) { + HDF_LOGE("%s: tester is NULL", __func__); + return HDF_ERR_INVALID_OBJECT; + } + tester->handle = PcieTestGetHandle(tester); + if (tester->handle == NULL) { + HDF_LOGE("%s: pcie test get handle failed", __func__); + return HDF_FAILURE; + } + for (i = 0; i < sizeof(g_pcieTestFunc) / sizeof(g_pcieTestFunc[0]); i++) { + if (cmd == g_pcieTestFunc[i].type && g_pcieTestFunc[i].Func != NULL) { + ret = g_pcieTestFunc[i].Func(tester); + isFind = true; + break; + } + } + if (!isFind) { + ret = HDF_ERR_NOT_SUPPORT; + HDF_LOGE("%s: cmd %d not supported", __func__, cmd); + } + PcieTestReleaseHandle(tester->handle); + return ret; +} + +static int32_t PcieTestFillConfig(struct PcieTester *tester, const struct DeviceResourceNode *node) +{ + int32_t ret; + struct DeviceResourceIface *drsOps = NULL; + + drsOps = DeviceResourceGetIfaceInstance(HDF_CONFIG_SOURCE); + if (drsOps == NULL || drsOps->GetUint32 == NULL) { + HDF_LOGE("%s: invalid drs ops", __func__); + return HDF_FAILURE; + } + + ret = drsOps->GetUint32(node, "busNum", &(tester->busNum), 0); + if (ret != HDF_SUCCESS) { + HDF_LOGE("%s: fill bus num failed", __func__); + return ret; + } + + HDF_LOGE("%s: busNum:%d.", __func__, tester->busNum); + return HDF_SUCCESS; +} + +static int32_t PcieTestBind(struct HdfDeviceObject *device) +{ + static struct PcieTester tester; + + if (device == NULL) { + HDF_LOGE("%s: device or config is null!", __func__); + return HDF_ERR_IO; + } + + device->service = &tester.service; + HDF_LOGE("%s: PCIE_TEST service init success!", __func__); + return HDF_SUCCESS; +} + +static int32_t PcieTestInit(struct HdfDeviceObject *device) +{ + struct PcieTester *tester = NULL; + int32_t ret; + + if (device == NULL || device->service == NULL || device->property == NULL) { + HDF_LOGE("%s: invalid parameter", __func__); + return HDF_ERR_INVALID_PARAM; + } + + tester = (struct PcieTester *)device->service; + if (tester == NULL) { + HDF_LOGE("%s: tester is NULL", __func__); + return HDF_ERR_INVALID_PARAM; + } + ret = PcieTestFillConfig(tester, device->property); + if (ret != HDF_SUCCESS) { + HDF_LOGE("%s: read config failed", __func__); + return ret; + } + tester->TestEntry = PcieTestEntry; + HDF_LOGE("%s: success", __func__); + return HDF_SUCCESS; +} + +static void PcieTestRelease(struct HdfDeviceObject *device) +{ + if (device != NULL) { + device->service = NULL; + } +} + +struct HdfDriverEntry g_pcieTestEntry = { + .moduleVersion = 1, + .Bind = PcieTestBind, + .Init = PcieTestInit, + .Release = PcieTestRelease, + .moduleName = "PLATFORM_PCIE_TEST", +}; +HDF_INIT(g_pcieTestEntry); diff --git a/test/unittest/platform/common/pcie_test.h b/test/unittest/platform/common/pcie_test.h new file mode 100644 index 00000000..e3226c8b --- /dev/null +++ b/test/unittest/platform/common/pcie_test.h @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * + * HDF is dual licensed: you can use it either under the terms of + * the GPL, or the BSD license, at your option. + * See the LICENSE file in the root of this repository for complete details. + */ + +#ifndef PCIE_TEST_H +#define PCIE_TEST_H + +#include "hdf_device_desc.h" +#include "hdf_platform.h" + +enum PcieTestCmd { + PCIE_READ_AND_WRITE_01 = 0, +}; + +struct PcieTester { + struct IDeviceIoService service; + struct HdfDeviceObject *device; + int32_t (*TestEntry)(struct PcieTester *tester, int32_t cmd); + uint32_t busNum; + DevHandle handle; +}; + +static inline struct PcieTester *GetPcieTester(void) +{ + return (struct PcieTester *)DevSvcManagerClntGetService("PCIE_TEST"); +} + +#endif /* PCIE_TEST_H */ diff --git a/test/unittest/platform/entry/hdf_pcie_entry_test.c b/test/unittest/platform/entry/hdf_pcie_entry_test.c new file mode 100644 index 00000000..b163ffa0 --- /dev/null +++ b/test/unittest/platform/entry/hdf_pcie_entry_test.c @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * + * HDF is dual licensed: you can use it either under the terms of + * the GPL, or the BSD license, at your option. + * See the LICENSE file in the root of this repository for complete details. + */ + +#include "hdf_log.h" +#include "hdf_pcie_entry_test.h" +#include "pcie_if.h" +#include "pcie_test.h" + +#define HDF_LOG_TAG hdf_pcie_entry_test + +int32_t HdfPcieUnitTestEntry(HdfTestMsg *msg) +{ + struct PcieTester *tester = NULL; + + if (msg == NULL) { + HDF_LOGE("HdfPcieUnitTestEntry: msg is NULL"); + return HDF_FAILURE; + } + tester = GetPcieTester(); + if (tester == NULL || tester->TestEntry == NULL) { + HDF_LOGE("HdfPcieUnitTestEntry: tester/TestEntry is NULL"); + msg->result = HDF_FAILURE; + return HDF_FAILURE; + } + msg->result = tester->TestEntry(tester, msg->subCmd); + return msg->result; +} diff --git a/test/unittest/platform/entry/hdf_pcie_entry_test.h b/test/unittest/platform/entry/hdf_pcie_entry_test.h new file mode 100644 index 00000000..8620268c --- /dev/null +++ b/test/unittest/platform/entry/hdf_pcie_entry_test.h @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * + * HDF is dual licensed: you can use it either under the terms of + * the GPL, or the BSD license, at your option. + * See the LICENSE file in the root of this repository for complete details. + */ + +#ifndef HDF_PCIE_ENTRY_TEST_H +#define HDF_PCIE_ENTRY_TEST_H + +#include "hdf_main_test.h" + +int32_t HdfPcieUnitTestEntry(HdfTestMsg *msg); + +#endif // HDF_PCIE_ENTRY_TEST_H \ No newline at end of file diff --git a/test/unittest/platform/virtual/pcie_virtual.c b/test/unittest/platform/virtual/pcie_virtual.c new file mode 100644 index 00000000..498e3567 --- /dev/null +++ b/test/unittest/platform/virtual/pcie_virtual.c @@ -0,0 +1,167 @@ +/* + * Copyright (c) 2020-2021 Huawei Device Co., Ltd. + * + * HDF is dual licensed: you can use it either under the terms of + * the GPL, or the BSD license, at your option. + * See the LICENSE file in the root of this repository for complete details. + */ + +#include "device_resource_if.h" +#include "hdf_log.h" +#include "osal_mem.h" +#include "pcie_core.h" +#include "pcie_if.h" + +#define HDF_LOG_TAG pcie_virtual_c + +#define PCIE_ADAPTER_ONE_BYTE 1 +#define PCIE_ADAPTER_TWO_BYTE 2 +#define PCIE_ADAPTER_FOUR_BYTE 4 +#define PCIE_READ_DATA_1 0x95 +#define PCIE_READ_DATA_2 0x27 +#define PCIE_READ_DATA_3 0x89 + +struct PcieAdapterHost { + struct PcieCntlr cntlr; +}; + +static int32_t pcieAdapterInit(struct PcieCntlr *cntlr, uint16_t busNum) +{ + if (cntlr == NULL) { + return HDF_ERR_INVALID_OBJECT; + } + if (busNum != cntlr->devInfo.busNum) { + HDF_LOGE("pci bus num not match."); + return HDF_ERR_NOT_SUPPORT; + } + + HDF_LOGE("pcieAdapterInit:success! vendorId is %d, devId is %d", cntlr->devInfo.vendorId, cntlr->devInfo.devId); + return HDF_SUCCESS; +} + +static int32_t pcieAdapterRead(struct PcieCntlr *cntlr, uint32_t pos, uint8_t *data, uint32_t len) +{ + if (cntlr == NULL) { + return HDF_ERR_INVALID_OBJECT; + } + + HDF_LOGE("pcieAdapterRead: pos = 0x%x, data = 0x%x, data val = %u, len = %d", pos, (uint32_t)data, + *data, len); + if (len == 1) { + *data = PCIE_READ_DATA_1; + } else if (len == 2) { + *data = PCIE_READ_DATA_1; + data++; + *data = PCIE_READ_DATA_2; + } else { + *data = PCIE_READ_DATA_1; + data++; + *data = PCIE_READ_DATA_2; + data++; + *data = PCIE_READ_DATA_2; + data++; + *data = PCIE_READ_DATA_3; + } + return HDF_SUCCESS; +} + +static int32_t pcieAdapterWrite(struct PcieCntlr *cntlr, uint32_t pos, uint8_t *data, uint32_t len) +{ + if (cntlr == NULL) { + return HDF_ERR_INVALID_OBJECT; + } + + HDF_LOGE("pcieAdapterWrite: pos = 0x%x, data = 0x%x, data val = %u, len = %d", pos, (uint32_t)data, + *data, len); + return HDF_SUCCESS; +} + +static int32_t pcieAdapterDeinit(struct PcieCntlr *cntlr) +{ + if (cntlr == NULL) { + return HDF_ERR_INVALID_OBJECT; + } + cntlr->priv = NULL; + return HDF_SUCCESS; +} + +static struct PcieCntlrOps g_pcieAdapterHostOps = { + .init = pcieAdapterInit, + .read = pcieAdapterRead, + .write = pcieAdapterWrite, + .deinit = pcieAdapterDeinit, +}; + +static int32_t PcieAdapterBind(struct HdfDeviceObject *obj) +{ + struct PcieAdapterHost *host = NULL; + int32_t ret; + + if (obj == NULL) { + HDF_LOGE("PcieAdapterBind: Fail, device is NULL."); + return HDF_ERR_INVALID_OBJECT; + } + + host = (struct PcieAdapterHost *)OsalMemCalloc(sizeof(struct PcieAdapterHost)); + if (host == NULL) { + HDF_LOGE("PcieAdapterBind: no mem for PcieAdapterHost."); + return HDF_ERR_MALLOC_FAIL; + } + host->cntlr.ops = &g_pcieAdapterHostOps; + host->cntlr.hdfDevObj = obj; + obj->service = &(host->cntlr.service); + + ret = PcieCntlrParse(&(host->cntlr), obj); + if (ret != HDF_SUCCESS) { + goto _ERR; + } + + ret = PcieCntlrAdd(&(host->cntlr)); + if (ret != HDF_SUCCESS) { + goto _ERR; + } + + HDF_LOGE("PcieAdapterBind: success."); + return HDF_SUCCESS; +_ERR: + PcieCntlrRemove(&(host->cntlr)); + OsalMemFree(host); + HDF_LOGD("PcieAdapterBind: fail, err = %d.", ret); + return ret; +} + +static int32_t PcieAdapterInit(struct HdfDeviceObject *obj) +{ + (void)obj; + + HDF_LOGE("PcieAdapterInit: success."); + return HDF_SUCCESS; +} + +static void PcieAdapterRelease(struct HdfDeviceObject *obj) +{ + struct PcieCntlr *cntlr = NULL; + struct PcieAdapterHost *host = NULL; + + if (obj == NULL) { + return; + } + + cntlr = (struct PcieCntlr *)obj->service; + if (cntlr == NULL) { + return; + } + PcieCntlrRemove(cntlr); + host = (struct PcieAdapterHost *)cntlr; + OsalMemFree(host); + HDF_LOGD("PcieAdapterRelease: success."); +} + +struct HdfDriverEntry g_pcieVirtualDriverEntry = { + .moduleVersion = 1, + .Bind = PcieAdapterBind, + .Init = PcieAdapterInit, + .Release = PcieAdapterRelease, + .moduleName = "PLATFORM_PCIE_VIRTUAL", +}; +HDF_INIT(g_pcieVirtualDriverEntry); From 83b0fc8d02b031983a999cd2980d7303caddab43 Mon Sep 17 00:00:00 2001 From: lzl Date: Sat, 11 Dec 2021 08:41:52 +0000 Subject: [PATCH 02/10] PCIE Framework code Signed-off-by: lzl --- support/platform/include/pcie/pcie_core.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/support/platform/include/pcie/pcie_core.h b/support/platform/include/pcie/pcie_core.h index 92d7fc18..90685046 100644 --- a/support/platform/include/pcie/pcie_core.h +++ b/support/platform/include/pcie/pcie_core.h @@ -13,6 +13,8 @@ #include "hdf_device_desc.h" #include "osal_mutex.h" #include "platform_core.h" +#include "platform_device.h" +#include "platform_manager.h" #ifdef __cplusplus #if __cplusplus From ffbbb293601ee838436e4681dbb9ebd0ea395fe5 Mon Sep 17 00:00:00 2001 From: lzl Date: Sat, 11 Dec 2021 08:52:42 +0000 Subject: [PATCH 03/10] PCIE Framework Code Signed-off-by: lzl --- support/platform/include/pcie/pcie_core.h | 4 +--- support/platform/src/pcie/pcie_core.c | 2 +- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/support/platform/include/pcie/pcie_core.h b/support/platform/include/pcie/pcie_core.h index 90685046..3511a7e1 100644 --- a/support/platform/include/pcie/pcie_core.h +++ b/support/platform/include/pcie/pcie_core.h @@ -13,8 +13,6 @@ #include "hdf_device_desc.h" #include "osal_mutex.h" #include "platform_core.h" -#include "platform_device.h" -#include "platform_manager.h" #ifdef __cplusplus #if __cplusplus @@ -63,7 +61,7 @@ static inline void PcieCntlrUnlock(struct PcieCntlr *cntlr) static inline struct PcieCntlr *PcieCntlrGetByBusNum(uint16_t num) { - struct PlatformDevice *device = PlatformManagerGetDeviceByMagic(PlatformManagerGet(PLATFORM_MODULE_PCIE), num); + struct PlatformDevice *device = PlatformManagerGetDeviceByNumber(PlatformManagerGet(PLATFORM_MODULE_PCIE), num); if (device == NULL) { return NULL; diff --git a/support/platform/src/pcie/pcie_core.c b/support/platform/src/pcie/pcie_core.c index b04625a8..577ada9d 100644 --- a/support/platform/src/pcie/pcie_core.c +++ b/support/platform/src/pcie/pcie_core.c @@ -37,7 +37,7 @@ static int32_t PcieCntlrInit(struct PcieCntlr *cntlr) cntlr->service.Dispatch = PcieIoDispatch; cntlr->hdfDevObj->service = &(cntlr->service); - cntlr->device.magic = cntlr->devInfo.busNum; + cntlr->device.number = cntlr->devInfo.busNum; cntlr->device.hdfDev = cntlr->hdfDevObj; return HDF_SUCCESS; } From d51bd911c6493f11d22933fdb7779b0590e94a01 Mon Sep 17 00:00:00 2001 From: lzl Date: Wed, 22 Dec 2021 09:32:26 +0000 Subject: [PATCH 04/10] PCIE fgramework code---Modify the Commiter's review Signed-off-by: lzl --- support/platform/include/pcie/pcie_core.h | 4 - support/platform/src/pcie/pcie_core.c | 31 +----- support/platform/src/pcie/pcie_dispatch.c | 25 ----- support/platform/src/pcie/pcie_if.c | 57 +--------- test/unittest/platform/virtual/pcie_virtual.c | 104 +++++++----------- 5 files changed, 45 insertions(+), 176 deletions(-) diff --git a/support/platform/include/pcie/pcie_core.h b/support/platform/include/pcie/pcie_core.h index 3511a7e1..e928141f 100644 --- a/support/platform/include/pcie/pcie_core.h +++ b/support/platform/include/pcie/pcie_core.h @@ -23,10 +23,8 @@ extern "C" { struct PcieCntlr; struct PcieCntlrOps { - int32_t (*init)(struct PcieCntlr *cntlr, uint16_t busNum); int32_t (*read)(struct PcieCntlr *cntlr, uint32_t pos, uint8_t *data, uint32_t len); int32_t (*write)(struct PcieCntlr *cntlr, uint32_t pos, uint8_t *data, uint32_t len); - int32_t (*deinit)(struct PcieCntlr *cntlr); }; struct PcieDevCfgInfo { @@ -73,10 +71,8 @@ int32_t PcieCntlrParse(struct PcieCntlr *cntlr, struct HdfDeviceObject *obj); int32_t PcieCntlrAdd(struct PcieCntlr *cntlr); void PcieCntlrRemove(struct PcieCntlr *cntlr); -int32_t PcieCntlrOpen(struct PcieCntlr *cntlr, uint16_t busNum); int32_t PcieCntlrRead(struct PcieCntlr *cntlr, uint32_t pos, uint8_t *data, uint32_t len); int32_t PcieCntlrWrite(struct PcieCntlr *cntlr, uint32_t pos, uint8_t *data, uint32_t len); -void PcieCntlrClose(struct PcieCntlr *cntlr); #ifdef __cplusplus #if __cplusplus diff --git a/support/platform/src/pcie/pcie_core.c b/support/platform/src/pcie/pcie_core.c index 577ada9d..402ea9a2 100644 --- a/support/platform/src/pcie/pcie_core.c +++ b/support/platform/src/pcie/pcie_core.c @@ -75,8 +75,8 @@ int32_t PcieCntlrAdd(struct PcieCntlr *cntlr) void PcieCntlrRemove(struct PcieCntlr *cntlr) { if (cntlr != NULL) { - PcieCntlrUninit(cntlr); PlatformDeviceDel(&cntlr->device); + PcieCntlrUninit(cntlr); } } @@ -97,7 +97,7 @@ int32_t PcieCntlrParse(struct PcieCntlr *cntlr, struct HdfDeviceObject *obj) return HDF_FAILURE; } drsOps = DeviceResourceGetIfaceInstance(HDF_CONFIG_SOURCE); - if (drsOps == NULL || drsOps->GetString == NULL || drsOps->GetUint32 == NULL) { + if (drsOps == NULL || drsOps->GetUint32 == NULL) { HDF_LOGE("PcieCntlrParse: invalid drs ops fail."); return HDF_FAILURE; } @@ -122,20 +122,6 @@ int32_t PcieCntlrParse(struct PcieCntlr *cntlr, struct HdfDeviceObject *obj) return HDF_SUCCESS; } -int32_t PcieCntlrOpen(struct PcieCntlr *cntlr, uint16_t busNum) -{ - int32_t ret; - - if (cntlr == NULL || cntlr->ops == NULL || cntlr->ops->init == NULL) { - return HDF_ERR_INVALID_OBJECT; - } - - PcieCntlrLock(cntlr); - ret = cntlr->ops->init(cntlr, busNum); - PcieCntlrUnlock(cntlr); - return ret; -} - int32_t PcieCntlrRead(struct PcieCntlr *cntlr, uint32_t pos, uint8_t *data, uint32_t len) { int32_t ret; @@ -168,16 +154,3 @@ int32_t PcieCntlrWrite(struct PcieCntlr *cntlr, uint32_t pos, uint8_t *data, uin PcieCntlrUnlock(cntlr); return ret; } - -void PcieCntlrClose(struct PcieCntlr *cntlr) -{ - if (cntlr == NULL || cntlr->ops == NULL || cntlr->ops->deinit == NULL) { - return; - } - - PcieCntlrLock(cntlr); - if (cntlr->ops->deinit(cntlr) != HDF_SUCCESS) { - HDF_LOGE("pcie deinit fail"); - } - PcieCntlrUnlock(cntlr); -} \ No newline at end of file diff --git a/support/platform/src/pcie/pcie_dispatch.c b/support/platform/src/pcie/pcie_dispatch.c index be1e5093..d7debd0b 100644 --- a/support/platform/src/pcie/pcie_dispatch.c +++ b/support/platform/src/pcie/pcie_dispatch.c @@ -15,8 +15,6 @@ #define HDF_LOG_TAG pcie_dispatch_c enum PcieIoCmd { - PCIE_CMD_OPEN, - PCIE_CMD_CLOSE, PCIE_CMD_READ, PCIE_CMD_WRITE, PCIE_CMD_BUTT, @@ -27,27 +25,6 @@ struct PcieDispatchFunc { int32_t (*func)(struct PcieCntlr *cntlr, struct HdfSBuf *data, struct HdfSBuf *reply); }; -static int32_t PcieCmdOpen(struct PcieCntlr *cntlr, struct HdfSBuf *data, struct HdfSBuf *reply) -{ - uint16_t busNum; - (void)reply; - - if (!HdfSbufReadUint16(data, &busNum)) { - HDF_LOGE("PcieCmdRead: read busNum fail"); - return HDF_ERR_IO; - } - return PcieCntlrOpen(cntlr, busNum); -} - -static int32_t PcieCmdClose(struct PcieCntlr *cntlr, struct HdfSBuf *data, struct HdfSBuf *reply) -{ - (void)data; - (void)reply; - - PcieCntlrClose(cntlr); - return HDF_SUCCESS; -} - static int32_t PcieCmdRead(struct PcieCntlr *cntlr, struct HdfSBuf *data, struct HdfSBuf *reply) { uint32_t len, pos; @@ -110,8 +87,6 @@ int32_t PcieIoDispatch(struct HdfDeviceIoClient *client, int32_t cmd, struct Hdf struct PcieCntlr *cntlr = NULL; uint32_t i, len; struct PcieDispatchFunc dispatchFunc[] = { - { PCIE_CMD_OPEN, PcieCmdOpen }, - { PCIE_CMD_CLOSE, PcieCmdClose }, { PCIE_CMD_READ, PcieCmdRead }, { PCIE_CMD_WRITE, PcieCmdWrite }, }; diff --git a/support/platform/src/pcie/pcie_if.c b/support/platform/src/pcie/pcie_if.c index 7b6ac2c6..79bb5c1a 100644 --- a/support/platform/src/pcie/pcie_if.c +++ b/support/platform/src/pcie/pcie_if.c @@ -25,8 +25,6 @@ #ifdef __USER__ enum PcieIoCmd { - PCIE_CMD_OPEN, - PCIE_CMD_CLOSE, PCIE_CMD_READ, PCIE_CMD_WRITE, PCIE_CMD_BUTT, @@ -55,22 +53,6 @@ static int32_t PcieGetDataFromReply(struct HdfSBuf *reply, uint8_t *data, uint32 return HDF_SUCCESS; } -static void PcieUserClose(DevHandle handle) -{ - struct HdfIoService *service = (struct HdfIoService *)handle; - int32_t ret; - - if (service == NULL || service->dispatcher == NULL || service->dispatcher->Dispatch == NULL) { - HDF_LOGE("PcieUserClose: service is invalid"); - return; - } - - ret = service->dispatcher->Dispatch(&service->object, PCIE_CMD_CLOSE, NULL, NULL); - if (ret != HDF_SUCCESS) { - HDF_LOGE("PcieUserClose: failed to send service call:%d", ret); - } -} - static int32_t PcieUserRead(DevHandle handle, uint32_t pos, uint8_t *data, uint32_t len) { int32_t ret; @@ -190,38 +172,7 @@ __ERR: DevHandle PcieOpen(uint16_t busNum) { - DevHandle *obj = (DevHandle *)PcieCntlrObjGet(busNum); - int32_t ret; - - if (obj == NULL) { - return NULL; - } -#ifdef __USER__ - struct HdfIoService *service = (struct HdfIoService *)obj; - if (service->dispatcher == NULL || service->dispatcher->Dispatch == NULL) { - HDF_LOGE("PcieOpen: dispatcher or Dispatch is NULL!"); - return NULL; - } - - struct HdfSBuf *buf = HdfSBufObtainDefaultSize(); - if (!HdfSbufWriteUint16(buf, busNum)) { - HDF_LOGE("PcieOpen: sbuf write uint16 failed"); - HdfSBufRecycle(buf); - return NULL; - } - ret = service->dispatcher->Dispatch(&service->object, PCIE_CMD_OPEN, buf, NULL); - if (ret != HDF_SUCCESS) { - HDF_LOGE("PcieOpen: failed to send service call:%d", ret); - return NULL; - } - HdfSBufRecycle(buf); -#else - ret = PcieCntlrOpen((struct PcieCntlr *)obj, busNum); - if (ret != HDF_SUCCESS) { - return NULL; - } -#endif - return obj; + return (DevHandle)PcieCntlrObjGet(busNum); } int32_t PcieRead(DevHandle handle, uint32_t pos, uint8_t *data, uint32_t len) @@ -244,9 +195,9 @@ int32_t PcieWrite(DevHandle handle, uint32_t pos, uint8_t *data, uint32_t len) void PcieClose(DevHandle handle) { + if (handle != NULL) { #ifdef __USER__ - PcieUserClose(handle); -#else - PcieCntlrClose((struct PcieCntlr *)handle); + HdfIoServiceRecycle((struct HdfIoService *)handle); #endif + } } diff --git a/test/unittest/platform/virtual/pcie_virtual.c b/test/unittest/platform/virtual/pcie_virtual.c index 498e3567..75bc4dda 100644 --- a/test/unittest/platform/virtual/pcie_virtual.c +++ b/test/unittest/platform/virtual/pcie_virtual.c @@ -10,104 +10,78 @@ #include "hdf_log.h" #include "osal_mem.h" #include "pcie_core.h" -#include "pcie_if.h" #define HDF_LOG_TAG pcie_virtual_c -#define PCIE_ADAPTER_ONE_BYTE 1 -#define PCIE_ADAPTER_TWO_BYTE 2 -#define PCIE_ADAPTER_FOUR_BYTE 4 -#define PCIE_READ_DATA_1 0x95 -#define PCIE_READ_DATA_2 0x27 -#define PCIE_READ_DATA_3 0x89 +#define PCIE_VIRTUAL_ADAPTER_ONE_BYTE 1 +#define PCIE_VIRTUAL_ADAPTER_TWO_BYTE 2 +#define PCIE_VIRTUAL_ADAPTER_FOUR_BYTE 4 +#define PCIE_VIRTUAL_ADAPTER_READ_DATA_1 0x95 +#define PCIE_VIRTUAL_ADAPTER_READ_DATA_2 0x27 +#define PCIE_VIRTUAL_ADAPTER_READ_DATA_3 0x89 -struct PcieAdapterHost { +struct PcieVirtualAdapterHost { struct PcieCntlr cntlr; }; -static int32_t pcieAdapterInit(struct PcieCntlr *cntlr, uint16_t busNum) -{ - if (cntlr == NULL) { - return HDF_ERR_INVALID_OBJECT; - } - if (busNum != cntlr->devInfo.busNum) { - HDF_LOGE("pci bus num not match."); - return HDF_ERR_NOT_SUPPORT; - } - - HDF_LOGE("pcieAdapterInit:success! vendorId is %d, devId is %d", cntlr->devInfo.vendorId, cntlr->devInfo.devId); - return HDF_SUCCESS; -} - -static int32_t pcieAdapterRead(struct PcieCntlr *cntlr, uint32_t pos, uint8_t *data, uint32_t len) +static int32_t PcieVirtualAdapterRead(struct PcieCntlr *cntlr, uint32_t pos, uint8_t *data, uint32_t len) { if (cntlr == NULL) { return HDF_ERR_INVALID_OBJECT; } - HDF_LOGE("pcieAdapterRead: pos = 0x%x, data = 0x%x, data val = %u, len = %d", pos, (uint32_t)data, - *data, len); - if (len == 1) { - *data = PCIE_READ_DATA_1; - } else if (len == 2) { - *data = PCIE_READ_DATA_1; + HDF_LOGE("PcieVirtualAdapterRead: pos = 0x%x, data = 0x%x, data val = %u, len = %d", + pos, (uint32_t)data, *data, len); + if (len == PCIE_VIRTUAL_ADAPTER_ONE_BYTE) { + *data = PCIE_VIRTUAL_ADAPTER_READ_DATA_1; + } else if (len == PCIE_VIRTUAL_ADAPTER_TWO_BYTE) { + *data = PCIE_VIRTUAL_ADAPTER_READ_DATA_1; data++; - *data = PCIE_READ_DATA_2; + *data = PCIE_VIRTUAL_ADAPTER_READ_DATA_2; } else { - *data = PCIE_READ_DATA_1; + *data = PCIE_VIRTUAL_ADAPTER_READ_DATA_1; data++; - *data = PCIE_READ_DATA_2; + *data = PCIE_VIRTUAL_ADAPTER_READ_DATA_2; data++; - *data = PCIE_READ_DATA_2; + *data = PCIE_VIRTUAL_ADAPTER_READ_DATA_2; data++; - *data = PCIE_READ_DATA_3; + *data = PCIE_VIRTUAL_ADAPTER_READ_DATA_3; } return HDF_SUCCESS; } -static int32_t pcieAdapterWrite(struct PcieCntlr *cntlr, uint32_t pos, uint8_t *data, uint32_t len) +static int32_t PcieVirtualAdapterWrite(struct PcieCntlr *cntlr, uint32_t pos, uint8_t *data, uint32_t len) { if (cntlr == NULL) { return HDF_ERR_INVALID_OBJECT; } - HDF_LOGE("pcieAdapterWrite: pos = 0x%x, data = 0x%x, data val = %u, len = %d", pos, (uint32_t)data, + HDF_LOGE("PcieVirtualAdapterWrite: pos = 0x%x, data = 0x%x, data val = %u, len = %d", pos, (uint32_t)data, *data, len); return HDF_SUCCESS; } -static int32_t pcieAdapterDeinit(struct PcieCntlr *cntlr) -{ - if (cntlr == NULL) { - return HDF_ERR_INVALID_OBJECT; - } - cntlr->priv = NULL; - return HDF_SUCCESS; -} - -static struct PcieCntlrOps g_pcieAdapterHostOps = { - .init = pcieAdapterInit, - .read = pcieAdapterRead, - .write = pcieAdapterWrite, - .deinit = pcieAdapterDeinit, +static struct PcieCntlrOps g_pcieVirtualAdapterHostOps = { + .read = PcieVirtualAdapterRead, + .write = PcieVirtualAdapterWrite, }; -static int32_t PcieAdapterBind(struct HdfDeviceObject *obj) +static int32_t PcieVirtualAdapterBind(struct HdfDeviceObject *obj) { - struct PcieAdapterHost *host = NULL; + struct PcieVirtualAdapterHost *host = NULL; int32_t ret; if (obj == NULL) { - HDF_LOGE("PcieAdapterBind: Fail, device is NULL."); + HDF_LOGE("PcieVirtualAdapterBind: Fail, device is NULL."); return HDF_ERR_INVALID_OBJECT; } - host = (struct PcieAdapterHost *)OsalMemCalloc(sizeof(struct PcieAdapterHost)); + host = (struct PcieVirtualAdapterHost *)OsalMemCalloc(sizeof(struct PcieVirtualAdapterHost)); if (host == NULL) { - HDF_LOGE("PcieAdapterBind: no mem for PcieAdapterHost."); + HDF_LOGE("PcieVirtualAdapterBind: no mem for PcieAdapterHost."); return HDF_ERR_MALLOC_FAIL; } - host->cntlr.ops = &g_pcieAdapterHostOps; + host->cntlr.ops = &g_pcieVirtualAdapterHostOps; host->cntlr.hdfDevObj = obj; obj->service = &(host->cntlr.service); @@ -121,7 +95,7 @@ static int32_t PcieAdapterBind(struct HdfDeviceObject *obj) goto _ERR; } - HDF_LOGE("PcieAdapterBind: success."); + HDF_LOGD("PcieVirtualAdapterBind: success."); return HDF_SUCCESS; _ERR: PcieCntlrRemove(&(host->cntlr)); @@ -130,18 +104,18 @@ _ERR: return ret; } -static int32_t PcieAdapterInit(struct HdfDeviceObject *obj) +static int32_t PcieVirtualAdapterInit(struct HdfDeviceObject *obj) { (void)obj; - HDF_LOGE("PcieAdapterInit: success."); + HDF_LOGD("PcieVirtualAdapterInit: success."); return HDF_SUCCESS; } -static void PcieAdapterRelease(struct HdfDeviceObject *obj) +static void PcieVirtualAdapterRelease(struct HdfDeviceObject *obj) { struct PcieCntlr *cntlr = NULL; - struct PcieAdapterHost *host = NULL; + struct PcieVirtualAdapterHost *host = NULL; if (obj == NULL) { return; @@ -152,16 +126,16 @@ static void PcieAdapterRelease(struct HdfDeviceObject *obj) return; } PcieCntlrRemove(cntlr); - host = (struct PcieAdapterHost *)cntlr; + host = (struct PcieVirtualAdapterHost *)cntlr; OsalMemFree(host); HDF_LOGD("PcieAdapterRelease: success."); } struct HdfDriverEntry g_pcieVirtualDriverEntry = { .moduleVersion = 1, - .Bind = PcieAdapterBind, - .Init = PcieAdapterInit, - .Release = PcieAdapterRelease, + .Bind = PcieVirtualAdapterBind, + .Init = PcieVirtualAdapterInit, + .Release = PcieVirtualAdapterRelease, .moduleName = "PLATFORM_PCIE_VIRTUAL", }; HDF_INIT(g_pcieVirtualDriverEntry); From 25318bbeb60735562c919b8ae2436f1b154c71be Mon Sep 17 00:00:00 2001 From: liangxuewu Date: Mon, 27 Dec 2021 11:03:54 +0000 Subject: [PATCH 05/10] PCIE framework code--Modify feihu's reviews Signed-off-by: liangxuewu --- support/platform/src/pcie/pcie_dispatch.c | 9 +++++---- test/unittest/platform/virtual/pcie_virtual.c | 4 ++-- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/support/platform/src/pcie/pcie_dispatch.c b/support/platform/src/pcie/pcie_dispatch.c index d7debd0b..5d9db1ac 100644 --- a/support/platform/src/pcie/pcie_dispatch.c +++ b/support/platform/src/pcie/pcie_dispatch.c @@ -52,15 +52,15 @@ static int32_t PcieCmdRead(struct PcieCntlr *cntlr, struct HdfSBuf *data, struct ret = PcieCntlrRead(cntlr, pos, buf, len); if (ret != HDF_SUCCESS) { HDF_LOGE("PcieCntlrRead: error, ret is %d", ret); - goto _EXIST; + goto EXIST; } if (!HdfSbufWriteBuffer(reply, buf, len)) { HDF_LOGE("PcieCntlrRead: sbuf write buffer failed"); ret = HDF_ERR_IO; - goto _EXIST; + goto EXIST; } ret = HDF_SUCCESS; -_EXIST: +EXIST: OsalMemFree(buf); return ret; } @@ -85,7 +85,8 @@ static int32_t PcieCmdWrite(struct PcieCntlr *cntlr, struct HdfSBuf *data, struc int32_t PcieIoDispatch(struct HdfDeviceIoClient *client, int32_t cmd, struct HdfSBuf *data, struct HdfSBuf *reply) { struct PcieCntlr *cntlr = NULL; - uint32_t i, len; + uint32_t i; + uint32_t len; struct PcieDispatchFunc dispatchFunc[] = { { PCIE_CMD_READ, PcieCmdRead }, { PCIE_CMD_WRITE, PcieCmdWrite }, diff --git a/test/unittest/platform/virtual/pcie_virtual.c b/test/unittest/platform/virtual/pcie_virtual.c index 75bc4dda..b917a0e0 100644 --- a/test/unittest/platform/virtual/pcie_virtual.c +++ b/test/unittest/platform/virtual/pcie_virtual.c @@ -30,7 +30,7 @@ static int32_t PcieVirtualAdapterRead(struct PcieCntlr *cntlr, uint32_t pos, uin return HDF_ERR_INVALID_OBJECT; } - HDF_LOGE("PcieVirtualAdapterRead: pos = 0x%x, data = 0x%x, data val = %u, len = %d", + HDF_LOGD("PcieVirtualAdapterRead: pos = 0x%x, data = 0x%x, data val = %u, len = %d", pos, (uint32_t)data, *data, len); if (len == PCIE_VIRTUAL_ADAPTER_ONE_BYTE) { *data = PCIE_VIRTUAL_ADAPTER_READ_DATA_1; @@ -56,7 +56,7 @@ static int32_t PcieVirtualAdapterWrite(struct PcieCntlr *cntlr, uint32_t pos, ui return HDF_ERR_INVALID_OBJECT; } - HDF_LOGE("PcieVirtualAdapterWrite: pos = 0x%x, data = 0x%x, data val = %u, len = %d", pos, (uint32_t)data, + HDF_LOGD("PcieVirtualAdapterWrite: pos = 0x%x, data = 0x%x, data val = %u, len = %d", pos, (uint32_t)data, *data, len); return HDF_SUCCESS; } From a24756d29d0efd0d45342f7835e03a1010c3b2b3 Mon Sep 17 00:00:00 2001 From: liangxuewu Date: Tue, 28 Dec 2021 03:40:57 +0000 Subject: [PATCH 06/10] PCIE framework code Signed-off-by: liangxuewu --- include/platform/pcie_if.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/platform/pcie_if.h b/include/platform/pcie_if.h index d15c0b37..45cef6ad 100644 --- a/include/platform/pcie_if.h +++ b/include/platform/pcie_if.h @@ -29,7 +29,7 @@ #ifndef PCIE_IF_H #define PCIE_IF_H -#include "hdf_platform.h" +#include "platform_if.h" #ifdef __cplusplus #if __cplusplus From 5b80ef61035889796bcb1916763ff0bf17ed8f92 Mon Sep 17 00:00:00 2001 From: liangxuewu Date: Tue, 28 Dec 2021 04:05:09 +0000 Subject: [PATCH 07/10] PCIE framework code Signed-off-by: liangxuewu --- test/unittest/platform/common/pcie_test.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/unittest/platform/common/pcie_test.h b/test/unittest/platform/common/pcie_test.h index e3226c8b..2d0265dd 100644 --- a/test/unittest/platform/common/pcie_test.h +++ b/test/unittest/platform/common/pcie_test.h @@ -10,7 +10,7 @@ #define PCIE_TEST_H #include "hdf_device_desc.h" -#include "hdf_platform.h" +#include "platform_if.h" enum PcieTestCmd { PCIE_READ_AND_WRITE_01 = 0, From 369d0882878a303a65cf97b89e1f2e9bda95bca8 Mon Sep 17 00:00:00 2001 From: liangxuewu Date: Tue, 28 Dec 2021 11:44:46 +0000 Subject: [PATCH 08/10] PCIE framework code Signed-off-by: liangxuewu --- support/platform/src/pcie/pcie_dispatch.c | 6 +++--- support/platform/src/pcie/pcie_if.c | 10 +++++----- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/support/platform/src/pcie/pcie_dispatch.c b/support/platform/src/pcie/pcie_dispatch.c index 5d9db1ac..d4a5bb65 100644 --- a/support/platform/src/pcie/pcie_dispatch.c +++ b/support/platform/src/pcie/pcie_dispatch.c @@ -52,15 +52,15 @@ static int32_t PcieCmdRead(struct PcieCntlr *cntlr, struct HdfSBuf *data, struct ret = PcieCntlrRead(cntlr, pos, buf, len); if (ret != HDF_SUCCESS) { HDF_LOGE("PcieCntlrRead: error, ret is %d", ret); - goto EXIST; + goto EXIT; } if (!HdfSbufWriteBuffer(reply, buf, len)) { HDF_LOGE("PcieCntlrRead: sbuf write buffer failed"); ret = HDF_ERR_IO; - goto EXIST; + goto EXIT; } ret = HDF_SUCCESS; -EXIST: +EXIT: OsalMemFree(buf); return ret; } diff --git a/support/platform/src/pcie/pcie_if.c b/support/platform/src/pcie/pcie_if.c index 79bb5c1a..98f5dfd5 100644 --- a/support/platform/src/pcie/pcie_if.c +++ b/support/platform/src/pcie/pcie_if.c @@ -72,24 +72,24 @@ static int32_t PcieUserRead(DevHandle handle, uint32_t pos, uint8_t *data, uint3 if (buf == NULL) { HDF_LOGE("PcieUserRead: failed to obtain buf"); ret = HDF_ERR_MALLOC_FAIL; - goto __EXIT; + goto EXIT; } if (!HdfSbufWriteUint32(buf, len)) { HDF_LOGE("PcieUserRead: sbuf write uint32 failed"); ret = HDF_ERR_IO; - goto __EXIT; + goto EXIT; } if (!HdfSbufWriteUint32(buf, pos)) { HDF_LOGE("PcieUserRead: sbuf write uint32 failed"); ret = HDF_ERR_IO; - goto __EXIT; + goto EXIT; } reply = HdfSBufObtainDefaultSize(); if (reply == NULL) { HDF_LOGE("PcieUserRead: failed to obtain reply"); ret = HDF_ERR_MALLOC_FAIL; - goto __EXIT; + goto EXIT; } ret = service->dispatcher->Dispatch(&service->object, PCIE_CMD_READ, buf, reply); @@ -99,7 +99,7 @@ static int32_t PcieUserRead(DevHandle handle, uint32_t pos, uint8_t *data, uint3 ret = PcieGetDataFromReply(reply, data, len); } -__EXIT : +EXIT : if (reply != NULL) { HdfSBufRecycle(reply); } From 82bc6be2b208037883fe1f634138ae519870bba4 Mon Sep 17 00:00:00 2001 From: liangxuewu Date: Tue, 28 Dec 2021 11:49:17 +0000 Subject: [PATCH 09/10] PCIE framework code Signed-off-by: liangxuewu --- support/platform/src/pcie/pcie_if.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/support/platform/src/pcie/pcie_if.c b/support/platform/src/pcie/pcie_if.c index 98f5dfd5..42c69797 100644 --- a/support/platform/src/pcie/pcie_if.c +++ b/support/platform/src/pcie/pcie_if.c @@ -127,19 +127,19 @@ static int32_t PcieUserWrite(DevHandle handle, uint32_t pos, uint8_t *data, uint if (!HdfSbufWriteUint32(buf, pos)) { HDF_LOGE("PcieUserWrite: sbuf write uint32 failed"); ret = HDF_ERR_IO; - goto __EXIT; + goto EXIT; } if (!HdfSbufWriteBuffer(buf, data, len)) { HDF_LOGE("PcieUserWrite: sbuf write buffer failed"); ret = HDF_ERR_IO; - goto __EXIT; + goto EXIT; } ret = service->dispatcher->Dispatch(&service->object, PCIE_CMD_WRITE, buf, NULL); if (ret != HDF_SUCCESS) { HDF_LOGE("PcieUserWrite: failed to write, ret %d", ret); } -__EXIT: +EXIT: HdfSBufRecycle(buf); return ret; } @@ -158,14 +158,14 @@ static void *PcieCntlrObjGet(uint16_t busNum) if (snprintf_s(serviceName, (PCIE_SERVICE_NAME_LEN + 1), PCIE_SERVICE_NAME_LEN, "HDF_PLATFORM_PCIE_%d", busNum) < 0) { HDF_LOGE("get PCIE service name fail."); - goto __ERR; + goto EXIT; } #ifdef __USER__ obj = (void *)HdfIoServiceBind(serviceName); #else obj = (void *)PcieCntlrGetByBusNum(busNum); #endif -__ERR: +EXIT: OsalMemFree(serviceName); return obj; } From b02f47fc2f19ac42262a1f21d2f4bd0d0b6b78e8 Mon Sep 17 00:00:00 2001 From: liangxuewu Date: Tue, 28 Dec 2021 12:55:23 +0000 Subject: [PATCH 10/10] PCIE framework code Signed-off-by: liangxuewu --- test/unittest/platform/virtual/pcie_virtual.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/unittest/platform/virtual/pcie_virtual.c b/test/unittest/platform/virtual/pcie_virtual.c index b917a0e0..26098f58 100644 --- a/test/unittest/platform/virtual/pcie_virtual.c +++ b/test/unittest/platform/virtual/pcie_virtual.c @@ -87,17 +87,17 @@ static int32_t PcieVirtualAdapterBind(struct HdfDeviceObject *obj) ret = PcieCntlrParse(&(host->cntlr), obj); if (ret != HDF_SUCCESS) { - goto _ERR; + goto ERR; } ret = PcieCntlrAdd(&(host->cntlr)); if (ret != HDF_SUCCESS) { - goto _ERR; + goto ERR; } HDF_LOGD("PcieVirtualAdapterBind: success."); return HDF_SUCCESS; -_ERR: +ERR: PcieCntlrRemove(&(host->cntlr)); OsalMemFree(host); HDF_LOGD("PcieAdapterBind: fail, err = %d.", ret);