Line data Source code
1 : /**
2 : * Copyright (c) 2025 Huawei Technologies Co., Ltd.
3 : * This program is free software, you can redistribute it and/or modify it under the terms and conditions of
4 : * CANN Open Software License Agreement Version 2.0 (the "License").
5 : * Please refer to the License for details. You may not use this file except in compliance with the License.
6 : * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
7 : * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
8 : * See LICENSE in the root of the software repository for the full text of the License.
9 : */
10 : #include "aiv_channel_helper.h"
11 : #include "channel_process.h"
12 : #include "channel.h"
13 : #include "aicpu_ts_roce_channel_v2.h"
14 : #include "aiv_urma_channel.h"
15 : #include "comm_engine_utils.h"
16 :
17 : using namespace hcomm;
18 :
19 6 : HcclResult AivChannelHelper::FillDevEntities(const ChannelHandle *channelList, uint32_t listNum,
20 : const HcommChannelDesc *channelDescs, const int32_t *linkStatusList)
21 : {
22 6 : CHK_PTR_NULL(channelList);
23 6 : CHK_PTR_NULL(linkStatusList);
24 6 : CHK_PRT_RET((listNum == 0), HCCL_ERROR("[%s]Invalid listNum, listNum[%u]", __func__, listNum), HCCL_E_PARA);
25 :
26 12 : for (uint32_t i = 0; i < listNum; i++) {
27 6 : if (linkStatusList[i] != HCOMM_CHANNEL_STATUS_READY) {
28 6 : continue;
29 : }
30 2 : CommProtocol protocol = channelDescs[i].remoteEndpoint.protocol;
31 2 : void *channelPtr = nullptr;
32 2 : CHK_RET(ChannelProcess::ChannelGet(channelList[i], &channelPtr));
33 2 : CHK_PTR_NULL(channelPtr);
34 2 : auto *channel = static_cast<Channel *>(channelPtr);
35 2 : if (channel->IsDeviceEntityReady()) {
36 2 : continue;
37 : }
38 :
39 0 : if (protocol == COMM_PROTOCOL_ROCE) {
40 0 : auto *roceChannel = static_cast<AicpuTsRoceChannelV2 *>(channelPtr);
41 0 : CHK_RET(roceChannel->FillDevChannelEntity());
42 : }
43 :
44 0 : if (protocol == COMM_PROTOCOL_UBC_CTP || protocol == COMM_PROTOCOL_UBC_TP) {
45 0 : auto *aivChannel = static_cast<AivUrmaChannel *>(channelPtr);
46 0 : CHK_RET(aivChannel->FillChannelEntityToDevice());
47 : }
48 :
49 0 : channel->SetDeviceEntityReady();
50 0 : HCCL_INFO("[%s] channel[%u] fill dev entity success.", __func__, i);
51 : }
52 6 : return HCCL_SUCCESS;
53 : }
54 :
55 6 : HcclResult AivChannelHelper::HandleStatus(const ChannelHandle *channelList, uint32_t listNum,
56 : const HcommChannelDesc *channelDescs, const std::vector<int32_t> &linkStatusList, int32_t *statusList)
57 : {
58 6 : HcclResult fillRet = FillDevEntities(channelList, listNum, channelDescs, linkStatusList.data());
59 6 : if (fillRet != HCCL_SUCCESS) {
60 0 : HCCL_ERROR("[%s] FillDevEntities failed, ret[%d]", __func__, fillRet);
61 0 : return HCCL_E_INTERNAL;
62 : }
63 12 : for (uint32_t i = 0; i < listNum; i++) {
64 6 : statusList[i] = linkStatusList[i];
65 : }
66 6 : return HCCL_SUCCESS;
67 : }
68 :
69 0 : HcclResult AivChannelHelper::PreAllocChannels(
70 : ChannelHandle *targetChannels, ChannelHandle *userChannels, HcommChannelDesc *channelDescs, uint32_t channelNum)
71 : {
72 0 : CHK_PTR_NULL(targetChannels);
73 0 : CHK_PTR_NULL(userChannels);
74 0 : CHK_PRT_RET(
75 : (channelNum == 0), HCCL_ERROR("[%s]Invalid channelNum, channelNum[%u]", __func__, channelNum), HCCL_E_PARA);
76 :
77 0 : bool needD2HMap = false;
78 0 : for (uint32_t i = 0; i < channelNum; i++) {
79 0 : CommProtocol protocol = channelDescs[i].remoteEndpoint.protocol;
80 :
81 0 : if (protocol == COMM_PROTOCOL_ROCE) {
82 0 : needD2HMap = true;
83 0 : auto *channel = reinterpret_cast<AicpuTsRoceChannelV2 *>(targetChannels[i]);
84 0 : CHK_PTR_NULL(channel);
85 0 : CHK_RET(channel->PreAllocDevChannelEntity(&userChannels[i]));
86 0 : HCCL_INFO("[%s] channel[%u] pre-alloc dev entity success, devEntityPtr[%p]", __func__, i,
87 : reinterpret_cast<void *>(static_cast<uintptr_t>(userChannels[i])));
88 0 : } else if (protocol == COMM_PROTOCOL_UBC_CTP || protocol == COMM_PROTOCOL_UBC_TP) {
89 0 : needD2HMap = true;
90 0 : auto *channel = reinterpret_cast<AivUrmaChannel *>(targetChannels[i]);
91 0 : CHK_PTR_NULL(channel);
92 :
93 0 : void *devChannelEntity = nullptr;
94 0 : HcclResult ret = channel->PreAllocChannelEntityToDevice(&devChannelEntity);
95 0 : CHK_PRT_RET(ret != HCCL_SUCCESS,
96 : HCCL_ERROR("[%s] channel[%u] PreAllocChannelEntityToDevice failed, ret[%d]", __func__, i, ret), ret);
97 0 : CHK_PTR_NULL(devChannelEntity);
98 0 : userChannels[i] = static_cast<ChannelHandle>(reinterpret_cast<uintptr_t>(devChannelEntity));
99 0 : HCCL_INFO("[%s] channel[%u] pre-alloc dev entity success, devEntityPtr[%p]", __func__, i,
100 : reinterpret_cast<void *>(static_cast<uintptr_t>(userChannels[i])));
101 0 : } else {
102 0 : userChannels[i] = targetChannels[i];
103 0 : HCCL_INFO("[%s] AIV engine channel protocol not supported pre-alloc, idx[%u], protocol[%d]. "
104 : "Return host channel handle.",
105 : __func__, i, static_cast<int>(protocol));
106 : }
107 : }
108 :
109 0 : if (needD2HMap) {
110 0 : CHK_RET(ChannelProcess::FillChannelD2HMap(userChannels, targetChannels, channelNum));
111 : }
112 0 : return HCCL_SUCCESS;
113 : }
|