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 : protocol == COMM_PROTOCOL_UBG) {
46 0 : auto *aivChannel = static_cast<AivUrmaChannel *>(channelPtr);
47 0 : CHK_RET(aivChannel->FillChannelEntityToDevice());
48 : }
49 :
50 0 : channel->SetDeviceEntityReady();
51 0 : HCCL_INFO("[%s] channel[%u] fill dev entity success.", __func__, i);
52 : }
53 6 : return HCCL_SUCCESS;
54 : }
55 :
56 6 : HcclResult AivChannelHelper::HandleStatus(const ChannelHandle *channelList, uint32_t listNum,
57 : const HcommChannelDesc *channelDescs, const std::vector<int32_t> &linkStatusList, int32_t *statusList)
58 : {
59 6 : HcclResult fillRet = FillDevEntities(channelList, listNum, channelDescs, linkStatusList.data());
60 6 : if (fillRet != HCCL_SUCCESS) {
61 0 : HCCL_ERROR("[%s] FillDevEntities failed, ret[%d]", __func__, fillRet);
62 0 : return HCCL_E_INTERNAL;
63 : }
64 12 : for (uint32_t i = 0; i < listNum; i++) {
65 6 : statusList[i] = linkStatusList[i];
66 : }
67 6 : return HCCL_SUCCESS;
68 : }
69 :
70 0 : HcclResult AivChannelHelper::PreAllocChannels(
71 : ChannelHandle *targetChannels, ChannelHandle *userChannels, HcommChannelDesc *channelDescs, uint32_t channelNum)
72 : {
73 0 : CHK_PTR_NULL(targetChannels);
74 0 : CHK_PTR_NULL(userChannels);
75 0 : CHK_PRT_RET(
76 : (channelNum == 0), HCCL_ERROR("[%s]Invalid channelNum, channelNum[%u]", __func__, channelNum), HCCL_E_PARA);
77 :
78 0 : bool needD2HMap = false;
79 0 : for (uint32_t i = 0; i < channelNum; i++) {
80 0 : CommProtocol protocol = channelDescs[i].remoteEndpoint.protocol;
81 :
82 0 : if (protocol == COMM_PROTOCOL_ROCE) {
83 0 : needD2HMap = true;
84 0 : auto *channel = reinterpret_cast<AicpuTsRoceChannelV2 *>(targetChannels[i]);
85 0 : CHK_PTR_NULL(channel);
86 0 : CHK_RET(channel->PreAllocDevChannelEntity(&userChannels[i]));
87 0 : HCCL_INFO("[%s] channel[%u] pre-alloc dev entity success, devEntityPtr[%p]", __func__, i,
88 : reinterpret_cast<void *>(static_cast<uintptr_t>(userChannels[i])));
89 0 : } else if (protocol == COMM_PROTOCOL_UBC_CTP || protocol == COMM_PROTOCOL_UBC_TP ||
90 : protocol == COMM_PROTOCOL_UBG) {
91 0 : needD2HMap = true;
92 0 : auto *channel = reinterpret_cast<AivUrmaChannel *>(targetChannels[i]);
93 0 : CHK_PTR_NULL(channel);
94 :
95 0 : void *devChannelEntity = nullptr;
96 0 : HcclResult ret = channel->PreAllocChannelEntityToDevice(&devChannelEntity);
97 0 : CHK_PRT_RET(ret != HCCL_SUCCESS,
98 : HCCL_ERROR("[%s] channel[%u] PreAllocChannelEntityToDevice failed, ret[%d]", __func__, i, ret), ret);
99 0 : CHK_PTR_NULL(devChannelEntity);
100 0 : userChannels[i] = static_cast<ChannelHandle>(reinterpret_cast<uintptr_t>(devChannelEntity));
101 0 : HCCL_INFO("[%s] channel[%u] pre-alloc dev entity success, devEntityPtr[%p]", __func__, i,
102 : reinterpret_cast<void *>(static_cast<uintptr_t>(userChannels[i])));
103 0 : } else {
104 0 : userChannels[i] = targetChannels[i];
105 0 : HCCL_INFO("[%s] AIV engine channel protocol not supported pre-alloc, idx[%u], protocol[%d]. "
106 : "Return host channel handle.",
107 : __func__, i, static_cast<int>(protocol));
108 : }
109 : }
110 :
111 0 : if (needD2HMap) {
112 0 : CHK_RET(ChannelProcess::FillChannelD2HMap(userChannels, targetChannels, channelNum));
113 : }
114 0 : return HCCL_SUCCESS;
115 : }
|