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(err == DRV_ERROR_NONE, return IDE_DAEMON_ERROR,
95 : "drvDeviceGetPhyIdByIndex devIds: %u failed, err: %d", devLogIds[i], err);
96 0 : devs[i] = phyId;
97 : }
98 0 : return IDE_DAEMON_OK;
99 0 : }
100 :
101 : /**
102 : * @brief According to the physical ID of the input, it is judged whether the ID has been scanned.
103 : * @param [in] desPhyId: Physical ID of the device
104 : * @param [out] logId: logical ID of the device
105 : * @return
106 : * IDE_DAEMON_OK: Find the logical ID corresponding to the physical ID
107 : * IDE_DAEMON_ERROR: Unable to find the logical ID corresponding to the physical ID
108 : */
109 1 : int32_t IdeGetLogIdByPhyId(uint32_t desPhyId, IdeU32Pt logId)
110 : {
111 1 : drvError_t err = DRV_ERROR_NONE;
112 1 : uint32_t devNum = 0;
113 1 : std::vector<uint32_t> devIds(DEVICE_NUM_MAX, 0);
114 :
115 1 : IDE_CTRL_VALUE_FAILED(logId != nullptr, return IDE_DAEMON_ERROR, "logId is nullptr");
116 :
117 : // get logic device id list
118 1 : int32_t ret = IdeGetDevList(&devNum, devIds, DEVICE_NUM_MAX);
119 1 : IDE_CTRL_VALUE_FAILED(ret == IDE_DAEMON_OK, return IDE_DAEMON_ERROR, "IdeGetDevList failed");
120 :
121 : // check if phyid exist
122 1 : if (devNum > 0 && devNum <= DEVICE_NUM_MAX) {
123 1 : uint32_t i = 0;
124 1 : uint32_t phyId = 0;
125 1 : for (i = 0; i < devNum; i++) {
126 1 : err = drvDeviceGetPhyIdByIndex(devIds[i], &phyId);
127 2 : IDE_CTRL_VALUE_FAILED(err == DRV_ERROR_NONE, return IDE_DAEMON_ERROR,
128 : "drvDeviceGetPhyIdByIndex devIds: %u failed, err: %d", devIds[i], err);
129 1 : if (phyId == desPhyId) {
130 1 : IDE_LOGI("the logical ID(%u) is a corresponding physical ID(%u)", devIds[i], phyId);
131 1 : *logId = devIds[i];
132 1 : return IDE_DAEMON_OK;
133 : }
134 : }
135 : }
136 :
137 0 : return IDE_DAEMON_ERROR;
138 1 : }
139 :
140 : /**
141 : * @brief According to the physical ID of the input, it is judged whether the ID has been scanned.
142 : * @param [in] desPhyId: Physical ID of the device
143 : * @param [out] logId: logical ID of the device
144 : * @return
145 : * IDE_DAEMON_OK: Find the logical ID corresponding to the physical ID
146 : * IDE_DAEMON_ERROR: Unable to find the logical ID corresponding to the physical ID
147 : */
148 1 : int32_t AdxGetLogIdByPhyId(uint32_t desPhyId, IdeU32Pt logId)
149 : {
150 1 : if (desPhyId > DEVICE_NUM_MAX || logId == nullptr) {
151 0 : return IDE_DAEMON_ERROR;
152 : }
153 :
154 1 : drvError_t err = drvDeviceGetIndexByPhyId(desPhyId, logId);
155 1 : IDE_CTRL_VALUE_FAILED(err == DRV_ERROR_NONE, return IDE_DAEMON_ERROR,
156 : "drvDeviceGetIndexByPhyId devIds: %u failed, err: %d", desPhyId, err);
157 :
158 1 : return IDE_DAEMON_OK;
159 : }
160 : }
|