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 "adx_dsmi.h"
11 : #include <algorithm>
12 : #include "ascend_hal.h"
13 : #include "log/adx_log.h"
14 :
15 : namespace Adx {
16 : /**
17 : * @brief Check is vf id or not
18 : * @param devId: device id
19 : *
20 : * @return
21 : * true: is vf id
22 : * false: not vf id
23 : */
24 1 : bool CheckVfId(uint32_t devId)
25 : {
26 : #if defined(ADX_LIB_DEVICE_DRV)
27 : // 32~63 device vfid
28 1 : const uint32_t minVfid = 32;
29 1 : if ((devId >= minVfid) && (devId < MAX_LOCAL_DEVICE_NUM)) {
30 1 : return true;
31 : }
32 : #endif
33 : (void)devId;
34 0 : return false;
35 : }
36 :
37 : /**
38 : * @brief Get Device Info
39 : * @param devNum: device num from driver api
40 : * @param devs: array to save device id
41 : * @param len: the length of devs
42 : *
43 : * @return
44 : * IDE_DAEMON_OK: get device num succ
45 : * IDE_DAEMON_ERROR: get device num failed
46 : */
47 4 : int32_t IdeGetDevList(IdeU32Pt devNum, std::vector<uint32_t>& devs, uint32_t len)
48 : {
49 4 : IDE_CTRL_VALUE_FAILED(devNum != nullptr, return IDE_DAEMON_ERROR, "devNum is nullptr");
50 4 : IDE_CTRL_VALUE_FAILED(len == DEVICE_NUM_MAX, return IDE_DAEMON_ERROR, "len is invalid");
51 4 : drvError_t err = drvGetDevNum(devNum);
52 4 : if (err != DRV_ERROR_NONE) {
53 0 : IDE_LOGE("Get physical device number failed, err: %d", err);
54 0 : return IDE_DAEMON_ERROR;
55 : }
56 :
57 4 : if (*devNum > 0 && *devNum <= DEVICE_NUM_MAX) {
58 : #if (defined ADX_LIB_HOST) || (defined ADX_LIB_HOST_DRV)
59 0 : err = drvGetDevIDs(devs.data(), MAX_LOCAL_DEVICE_NUM); // host side get devId
60 : #else
61 4 : err = drvGetDeviceLocalIDs(devs.data(), MAX_LOCAL_DEVICE_NUM); // device side get devId
62 : #endif
63 4 : if (err != DRV_ERROR_NONE) {
64 0 : IDE_LOGE("get device IDs failed, err: %d", err);
65 0 : return IDE_DAEMON_ERROR;
66 : }
67 : }
68 4 : return IDE_DAEMON_OK;
69 : }
70 :
71 : /**
72 : * @brief Get Device Physical Info
73 : * @param devNum: device num from driver api
74 : * @param devs: array to save phy device id
75 : * @param len: the length of devs
76 : *
77 : * @return
78 : * IDE_DAEMON_OK: get device num succ
79 : * IDE_DAEMON_ERROR: get device num failed
80 : */
81 0 : int32_t IdeGetPhyDevList(IdeU32Pt devNum, std::vector<uint32_t>& devs, uint32_t len)
82 : {
83 0 : IDE_CTRL_VALUE_FAILED(devNum != nullptr, return IDE_DAEMON_ERROR, "devNum is nullptr");
84 0 : IDE_CTRL_VALUE_FAILED(len == DEVICE_NUM_MAX, return IDE_DAEMON_ERROR, "len is invalid");
85 :
86 0 : std::vector<uint32_t> devLogIds(DEVICE_NUM_MAX, 0);
87 0 : int32_t ret = IdeGetDevList(devNum, devLogIds, DEVICE_NUM_MAX);
88 0 : IDE_CTRL_VALUE_FAILED(ret == IDE_DAEMON_OK, return IDE_DAEMON_ERROR, "IdeGetDevList failed, ret: %d", ret);
89 :
90 0 : uint32_t i = 0;
91 0 : uint32_t phyId = 0;
92 0 : for (i = 0; i < *devNum && i < DEVICE_NUM_MAX; i++) {
93 0 : drvError_t err = drvDeviceGetPhyIdByIndex(devLogIds[i], &phyId);
94 0 : IDE_CTRL_VALUE_FAILED(
95 : err == DRV_ERROR_NONE, return IDE_DAEMON_ERROR, "drvDeviceGetPhyIdByIndex devIds: %u failed, err: %d",
96 : devLogIds[i], err);
97 0 : devs[i] = phyId;
98 : }
99 0 : return IDE_DAEMON_OK;
100 0 : }
101 :
102 : /**
103 : * @brief According to the physical ID of the input, it is judged whether the ID has been scanned.
104 : * @param [in] desPhyId: Physical ID of the device
105 : * @param [out] logId: logical ID of the device
106 : * @return
107 : * IDE_DAEMON_OK: Find the logical ID corresponding to the physical ID
108 : * IDE_DAEMON_ERROR: Unable to find the logical ID corresponding to the physical ID
109 : */
110 1 : int32_t IdeGetLogIdByPhyId(uint32_t desPhyId, IdeU32Pt logId)
111 : {
112 1 : drvError_t err = DRV_ERROR_NONE;
113 1 : uint32_t devNum = 0;
114 1 : std::vector<uint32_t> devIds(DEVICE_NUM_MAX, 0);
115 :
116 1 : IDE_CTRL_VALUE_FAILED(logId != nullptr, return IDE_DAEMON_ERROR, "logId is nullptr");
117 :
118 : // get logic device id list
119 1 : int32_t ret = IdeGetDevList(&devNum, devIds, DEVICE_NUM_MAX);
120 1 : IDE_CTRL_VALUE_FAILED(ret == IDE_DAEMON_OK, return IDE_DAEMON_ERROR, "IdeGetDevList failed");
121 :
122 : // check if phyid exist
123 1 : if (devNum > 0 && devNum <= DEVICE_NUM_MAX) {
124 1 : uint32_t i = 0;
125 1 : uint32_t phyId = 0;
126 1 : for (i = 0; i < devNum; i++) {
127 1 : err = drvDeviceGetPhyIdByIndex(devIds[i], &phyId);
128 2 : IDE_CTRL_VALUE_FAILED(
129 : err == DRV_ERROR_NONE, return IDE_DAEMON_ERROR, "drvDeviceGetPhyIdByIndex devIds: %u failed, err: %d",
130 : devIds[i], err);
131 1 : if (phyId == desPhyId) {
132 1 : IDE_LOGI("the logical ID(%u) is a corresponding physical ID(%u)", devIds[i], phyId);
133 1 : *logId = devIds[i];
134 1 : return IDE_DAEMON_OK;
135 : }
136 : }
137 : }
138 :
139 0 : return IDE_DAEMON_ERROR;
140 1 : }
141 :
142 : /**
143 : * @brief According to the physical ID of the input, it is judged whether the ID has been scanned.
144 : * @param [in] desPhyId: Physical ID of the device
145 : * @param [out] logId: logical ID of the device
146 : * @return
147 : * IDE_DAEMON_OK: Find the logical ID corresponding to the physical ID
148 : * IDE_DAEMON_ERROR: Unable to find the logical ID corresponding to the physical ID
149 : */
150 1 : int32_t AdxGetLogIdByPhyId(uint32_t desPhyId, IdeU32Pt logId)
151 : {
152 1 : if (desPhyId > DEVICE_NUM_MAX || logId == nullptr) {
153 0 : return IDE_DAEMON_ERROR;
154 : }
155 :
156 1 : drvError_t err = drvDeviceGetIndexByPhyId(desPhyId, logId);
157 1 : IDE_CTRL_VALUE_FAILED(
158 : err == DRV_ERROR_NONE, return IDE_DAEMON_ERROR, "drvDeviceGetIndexByPhyId devIds: %u failed, err: %d", desPhyId,
159 : err);
160 :
161 1 : return IDE_DAEMON_OK;
162 : }
163 : } // namespace Adx
|