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(
20 : const ChannelHandle* channelList, uint32_t listNum, const HcommChannelDesc* channelDescs,
21 : const int32_t* linkStatusList)
22 : {
23 6 : CHK_PTR_NULL(channelList);
24 6 : CHK_PTR_NULL(linkStatusList);
25 6 : CHK_PRT_RET((listNum == 0), HCCL_ERROR("[%s]Invalid listNum, listNum[%u]", __func__, listNum), HCCL_E_PARA);
26 :
27 12 : for (uint32_t i = 0; i < listNum; i++) {
28 6 : if (linkStatusList[i] != HCOMM_CHANNEL_STATUS_READY) {
29 6 : continue;
30 : }
31 2 : CommProtocol protocol = channelDescs[i].remoteEndpoint.protocol;
32 2 : void* channelPtr = nullptr;
33 2 : CHK_RET(ChannelProcess::ChannelGet(channelList[i], &channelPtr));
34 2 : CHK_PTR_NULL(channelPtr);
35 2 : auto* channel = static_cast<Channel*>(channelPtr);
36 2 : if (channel->IsDeviceEntityReady()) {
37 2 : continue;
38 : }
39 :
40 0 : if (protocol == COMM_PROTOCOL_ROCE) {
41 0 : auto* roceChannel = static_cast<AicpuTsRoceChannelV2*>(channelPtr);
42 0 : CHK_RET(roceChannel->FillDevChannelEntity());
43 : }
44 :
45 0 : if (protocol == COMM_PROTOCOL_UBC_CTP || protocol == COMM_PROTOCOL_UBC_TP || 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(
57 : const ChannelHandle* channelList, uint32_t listNum, const HcommChannelDesc* channelDescs,
58 : const std::vector<int32_t>& linkStatusList, int32_t* statusList)
59 : {
60 6 : HcclResult fillRet = FillDevEntities(channelList, listNum, channelDescs, linkStatusList.data());
61 6 : if (fillRet != HCCL_SUCCESS) {
62 0 : HCCL_ERROR("[%s] FillDevEntities failed, ret[%d]", __func__, fillRet);
63 0 : return HCCL_E_INTERNAL;
64 : }
65 12 : for (uint32_t i = 0; i < listNum; i++) {
66 6 : statusList[i] = linkStatusList[i];
67 : }
68 6 : return HCCL_SUCCESS;
69 : }
70 :
71 0 : HcclResult AivChannelHelper::PreAllocChannels(
72 : ChannelHandle* targetChannels, ChannelHandle* userChannels, HcommChannelDesc* channelDescs, uint32_t channelNum)
73 : {
74 0 : CHK_PTR_NULL(targetChannels);
75 0 : CHK_PTR_NULL(userChannels);
76 0 : CHK_PRT_RET(
77 : (channelNum == 0), HCCL_ERROR("[%s]Invalid channelNum, channelNum[%u]", __func__, channelNum), HCCL_E_PARA);
78 :
79 0 : bool needD2HMap = false;
80 0 : for (uint32_t i = 0; i < channelNum; i++) {
81 0 : CommProtocol protocol = channelDescs[i].remoteEndpoint.protocol;
82 :
83 0 : if (protocol == COMM_PROTOCOL_ROCE) {
84 0 : needD2HMap = true;
85 0 : auto* channel = reinterpret_cast<AicpuTsRoceChannelV2*>(targetChannels[i]);
86 0 : CHK_PTR_NULL(channel);
87 0 : CHK_RET(channel->PreAllocDevChannelEntity(&userChannels[i]));
88 0 : HCCL_INFO(
89 : "[%s] channel[%u] pre-alloc dev entity success, devEntityPtr[%p]", __func__, i,
90 : reinterpret_cast<void*>(static_cast<uintptr_t>(userChannels[i])));
91 0 : } else if (
92 0 : protocol == COMM_PROTOCOL_UBC_CTP || protocol == COMM_PROTOCOL_UBC_TP || protocol == COMM_PROTOCOL_UBG) {
93 0 : needD2HMap = true;
94 0 : auto* channel = reinterpret_cast<AivUrmaChannel*>(targetChannels[i]);
95 0 : CHK_PTR_NULL(channel);
96 :
97 0 : void* devChannelEntity = nullptr;
98 0 : HcclResult ret = channel->PreAllocChannelEntityToDevice(&devChannelEntity);
99 0 : CHK_PRT_RET(
100 : ret != HCCL_SUCCESS,
101 : HCCL_ERROR("[%s] channel[%u] PreAllocChannelEntityToDevice failed, ret[%d]", __func__, i, ret), ret);
102 0 : CHK_PTR_NULL(devChannelEntity);
103 0 : userChannels[i] = static_cast<ChannelHandle>(reinterpret_cast<uintptr_t>(devChannelEntity));
104 0 : HCCL_INFO(
105 : "[%s] channel[%u] pre-alloc dev entity success, devEntityPtr[%p]", __func__, i,
106 : reinterpret_cast<void*>(static_cast<uintptr_t>(userChannels[i])));
107 0 : } else {
108 0 : userChannels[i] = targetChannels[i];
109 0 : HCCL_INFO(
110 : "[%s] AIV engine channel protocol not supported pre-alloc, idx[%u], protocol[%d]. "
111 : "Return host channel handle.",
112 : __func__, i, static_cast<int>(protocol));
113 : }
114 : }
115 :
116 0 : if (needD2HMap) {
117 0 : CHK_RET(ChannelProcess::FillChannelD2HMap(userChannels, targetChannels, channelNum));
118 : }
119 0 : return HCCL_SUCCESS;
120 : }
|