diff --git a/projects/amdsmi/oam/CMakeLists.txt b/projects/amdsmi/oam/CMakeLists.txt old mode 100755 new mode 100644 diff --git a/projects/amdsmi/oam/example/oam_example.c b/projects/amdsmi/oam/example/oam_example.c old mode 100755 new mode 100644 index c683819c10..c68b4c9d9f --- a/projects/amdsmi/oam/example/oam_example.c +++ b/projects/amdsmi/oam/example/oam_example.c @@ -7,14 +7,23 @@ const oam_ops_t amd_oam_ops = { .free = amdoam_free, // .get_mapi_version = amdoam_get_mapi_version, .discover_devices = amdoam_discover_devices, + .get_dev_properties = amdoam_get_dev_properties, + .get_pci_properties = amdoam_get_pci_properties, + .get_sensors_count = amdoam_get_sensors_count, + .get_error_description = amdoam_get_error_description, }; int main() { uint32_t dev_cnt = 0; oam_mapi_version_t version; + oam_dev_properties_t *devs_prop; + int i; + oam_pci_info_t pci_info; + oam_sensor_count_t sensor_count; + const char *string; - if (amd_oam_ops.init(version)) { + if (amd_oam_ops.init()) { printf("init failed\n"); return -1; } @@ -22,7 +31,47 @@ int main() // amd_oam_ops.get_mapi_version(&version); if (!amd_oam_ops.discover_devices(&dev_cnt)) printf("%d AMD devices are discovered\n", dev_cnt); + if (!dev_cnt) { + printf("No devices are found.\n"); + return -1; + } + devs_prop = calloc(dev_cnt, sizeof(oam_dev_properties_t)); + if (!devs_prop) { + printf("Allocating dev_prop failed\n"); + return -1; + } + amd_oam_ops.get_dev_properties(dev_cnt, devs_prop); + for (i = 0; i < dev_cnt; i++) { + printf("Device %d:\n", i); + printf(" device id %d\n", devs_prop[i].device_id); + printf(" device_vendor %s\n", devs_prop[i].device_vendor); + printf(" device_name %s\n", devs_prop[i].device_name); + printf(" sku_name %s\n", devs_prop[i].sku_name); + printf(" board_name %s\n", devs_prop[i].board_name); + printf(" board_revision %s\n", devs_prop[i].board_revision); + printf(" board_serial_number %s\n", + devs_prop[i].board_serial_number); + + if (!amd_oam_ops.get_pci_properties(devs_prop[i].device_id, &pci_info)){ + printf(" PCI Domain : 0x%d \n", pci_info.domain); + printf(" PCI bus : 0x%d \n", pci_info.bus); + printf(" PCI device : 0x%d \n", pci_info.device); + printf(" PCI function : 0x%d \n", pci_info.function); + } + if(!amd_oam_ops.get_sensors_count(devs_prop[i].device_id, &sensor_count)) { + printf(" Temperature Sensors : %d \n", sensor_count.num_temperature_sensors); + printf(" Power Sensors : %d \n", sensor_count.num_power_sensors); + printf(" Voltage Sensors : %d \n", sensor_count.num_voltage_sensors); + printf(" Current Sensors : %d \n", sensor_count.num_current_sensors); + printf(" Fan Sensors : %d \n", sensor_count.num_fans); + } + } + + amd_oam_ops.get_error_description(1, &string); + printf("error code 1: %s\n", string); + + free(devs_prop); amd_oam_ops.free(); return 0; diff --git a/projects/amdsmi/oam/include/oam/amd_oam.h b/projects/amdsmi/oam/include/oam/amd_oam.h index 2d4083cafb..b25e2b7e7d 100755 --- a/projects/amdsmi/oam/include/oam/amd_oam.h +++ b/projects/amdsmi/oam/include/oam/amd_oam.h @@ -32,10 +32,34 @@ extern "C" { #include #endif // __cplusplus -int amdoam_init(oam_mapi_version_t version); +#include "oam/oam_mapi.h" + +typedef enum { + AMDOAM_STATUS_SUCCESS = 0x0, + /* copy RSMI errors */ + AMDOAM_STATUS_INVALID_ARGS, + AMDOAM_STATUS_NOT_SUPPORTED, + AMDOAM_STATUS_FILE_ERROR, + AMDOAM_STATUS_PERMISSION, + AMDOAM_STATUS_OUT_OF_RESOURCES, + AMDOAM_STATUS_INTERNAL_EXCEPTION, + AMDOAM_STATUS_INPUT_OUT_OF_BOUNDS, + AMDOAM_STATUS_INIT_ERROR, + /* end of RSMI error code */ + AMDOAM_STATUS_ERROR, // Generic error return if not otherwise specified + AMDOAM_STATUS_NOT_FOUND, +} amdoam_status_t; + +int amdoam_init(void); int amdoam_free(void); // int amdoam_get_mapi_version(oam_mapi_version_t *version); -int amdoam_discover_devices(int *device_count); +int amdoam_discover_devices(uint32_t *device_count); +int amdoam_get_dev_properties(uint32_t dev_inx, + oam_dev_properties_t *prop); +int amdoam_get_pci_properties(uint32_t device_id, oam_pci_info_t *pci_info); +int amdoam_get_sensors_count(uint32_t device_id, + oam_sensor_count_t *sensor_count); +int amdoam_get_error_description(int code, const char **description); #ifdef __cplusplus } diff --git a/projects/amdsmi/oam/include/oam/oam_mapi.h b/projects/amdsmi/oam/include/oam/oam_mapi.h index b8fae4b96f..2a3c6172ac 100755 --- a/projects/amdsmi/oam/include/oam/oam_mapi.h +++ b/projects/amdsmi/oam/include/oam/oam_mapi.h @@ -42,46 +42,35 @@ extern "C" { * \struct oam_mapi_version_t * \brief OAM library API version * \details TBD - * All the libraries versions are expected to be backward compatible. - * The major version increment indicates a new API has been added. - * Minor version increment indicates an interface change. + * All the libraries versions are expected to be backward compatible. + * The major version increment indicates a new API has been added. + * Minor version increment indicates an interface change. */ typedef struct oam_mapi_version { - uint32_t major; - uint32_t minor; + uint32_t major; + uint32_t minor; } oam_mapi_version_t; /** - * \struct oam_dev_properties_t - * \brief Local identifier for the device - * \details Immutable device identifier - * This is unique within the chassis. - */ -typedef struct oam_dev_id { - /*!< local identifier for the device */ - int device_id; -} oam_dev_id_t; - -/** - * \struct oam_dev_properties_t - * \brief Network identifier for the device + * \struct oam_dev_properties_t + * \brief Network identifier for the device * \details Immutable network identifier for the device. - * This is unique across the entire network. + * This is unique across the entire network. */ typedef struct oam_net_dev_id { - /*!< unique network identifier for the device */ - int network_id; + /*!< unique network identifier for the device */ + int network_id; } oam_net_dev_id_t; /* * various lengths for device properties */ -#define DEVICE_VENDOR_LEN 128 -#define DEVICE_NAME_LEN 128 -#define DEVICE_SKU_LEN 128 -#define BOARD_NAME_LEN 128 -#define BOARD_REVISION_LEN 128 -#define BOARD_SERIAL_NUM_LEN 128 +#define DEVICE_VENDOR_LEN 128 +#define DEVICE_NAME_LEN 128 +#define DEVICE_SKU_LEN 128 +#define BOARD_NAME_LEN 128 +#define BOARD_REVISION_LEN 128 +#define BOARD_SERIAL_NUM_LEN 128 /** * \struct oam_dev_properties_t @@ -89,23 +78,23 @@ typedef struct oam_net_dev_id { * \details TBD */ typedef struct oam_dev_properties { - /*!< unique network identifier for the device */ - oam_dev_id_t device_id; - /*!< vendor name */ - char device_vendor[DEVICE_VENDOR_LEN]; - /*!< Device name */ - char device_name[DEVICE_NAME_LEN]; - /*!< SKU name */ - char sku_name[DEVICE_SKU_LEN]; - /*!< Board name */ - char board_name[BOARD_NAME_LEN]; - /*!< Board revision */ - char board_revision[BOARD_REVISION_LEN]; - /*!< - * Board Serial Number or UUID any other identifier, which can be used - * to identify devices uniquely and physically. - */ - char board_serial_number[BOARD_SERIAL_NUM_LEN]; + /*!< Immutable local identifier for the device */ + uint32_t device_id; + /*!< vendor name */ + char device_vendor[DEVICE_VENDOR_LEN]; + /*!< Device name */ + char device_name[DEVICE_NAME_LEN]; + /*!< SKU name */ + char sku_name[DEVICE_SKU_LEN]; + /*!< Board name */ + char board_name[BOARD_NAME_LEN]; + /*!< Board revision */ + char board_revision[BOARD_REVISION_LEN]; + /*!< + * Board Serial Number or UUID any other identifier, which can be used + * to identify devices uniquely and physically. + */ + char board_serial_number[BOARD_SERIAL_NUM_LEN]; } oam_dev_properties_t; /** @@ -115,11 +104,11 @@ typedef struct oam_dev_properties { * Various sensor related information */ typedef struct oam_sensor_count { - uint32_t num_temperature_sensors; - uint32_t num_power_sensors; - uint32_t num_voltage_sensors; - uint32_t num_current_sensors; - uint32_t num_fans; + uint32_t num_temperature_sensors; + uint32_t num_power_sensors; + uint32_t num_voltage_sensors; + uint32_t num_current_sensors; + uint32_t num_fans; } oam_sensor_count_t; /** @@ -128,12 +117,12 @@ typedef struct oam_sensor_count { * \details This enumerated type defines available sensors types. */ typedef enum oam_sensor_type { - OAM_SENSOR_TYPE_POWER = 0, - OAM_SENSOR_TYPE_VOLTAGE, - OAM_SENSOR_TYPE_CURRENT, - OAM_SENSOR_TYPE_TEMP, - OAM_SENSOR_TYPE_FAN_SPEED, - OAM_SENSOR_TYPE_UNKNOWN = 0xFF + OAM_SENSOR_TYPE_POWER = 0, + OAM_SENSOR_TYPE_VOLTAGE, + OAM_SENSOR_TYPE_CURRENT, + OAM_SENSOR_TYPE_TEMP, + OAM_SENSOR_TYPE_FAN_SPEED, + OAM_SENSOR_TYPE_UNKNOWN = 0xFF } oam_sensor_type_t; /** @@ -142,9 +131,9 @@ typedef enum oam_sensor_type { * \details This enumerated type defines available scales for power measurements */ typedef enum oam_power_sensor_scale { - OAM_POWER_SCALE_uW = 0, - OAM_POWER_SCALE_mW, - OAM_POWER_SCALE_W, + OAM_POWER_SCALE_uW = 0, + OAM_POWER_SCALE_mW, + OAM_POWER_SCALE_W, } oam_power_sensor_scale_t; /** @@ -153,9 +142,9 @@ typedef enum oam_power_sensor_scale { * \details This enumerated type defines available scales for voltage measurements */ typedef enum oam_voltage_sensor_scale { - OAM_VOLTAGE_SCALE_uV = 0, - OAM_VOLTAGE_SCALE_mV, - OAM_VOLTAGE_SCALE_V, + OAM_VOLTAGE_SCALE_uV = 0, + OAM_VOLTAGE_SCALE_mV, + OAM_VOLTAGE_SCALE_V, } oam_voltage_sensor_scale_t; /** @@ -164,9 +153,9 @@ typedef enum oam_voltage_sensor_scale { * \details This enumerated type defines available scales for current measurements */ typedef enum oam_current_sensor_scale { - OAM_CURRENT_SCALE_uA = 0, - OAM_CURRENT_SCALE_mA, - OAM_CURRENT_SCALE_A, + OAM_CURRENT_SCALE_uA = 0, + OAM_CURRENT_SCALE_mA, + OAM_CURRENT_SCALE_A, } oam_current_sensor_scale_t; /** @@ -175,8 +164,8 @@ typedef enum oam_current_sensor_scale { * \details This enumerated type defines available scales for temp measurements */ typedef enum oam_temp_sensor_scale { - OAM_TEMP_SCALE_C = 0, - OAM_TEMP_SCALE_F + OAM_TEMP_SCALE_C = 0, + OAM_TEMP_SCALE_F } oam_temp_sensor_scale_t; /** @@ -185,17 +174,17 @@ typedef enum oam_temp_sensor_scale { * \details This enumerated type defines available scales for power measurements */ typedef enum oam_fan_sensor_scale { - OAM_FAN_SPEED_Hz = 0, - OAM_FAN_SPEED_KHz, - OAM_FAN_SPEED_MHz + OAM_FAN_SPEED_Hz = 0, + OAM_FAN_SPEED_KHz, + OAM_FAN_SPEED_MHz } oam_fan_sensor_scale_t; typedef union oam_sensor_scale { - oam_power_sensor_scale_t power_scale; - oam_voltage_sensor_scale_t volate_scale; - oam_current_sensor_scale_t current_scale; - oam_temp_sensor_scale_t temp_scale; - oam_fan_sensor_scale_t fan_scale; + oam_power_sensor_scale_t power_scale; + oam_voltage_sensor_scale_t volate_scale; + oam_current_sensor_scale_t current_scale; + oam_temp_sensor_scale_t temp_scale; + oam_fan_sensor_scale_t fan_scale; } oam_sensor_scale_t; /** @@ -206,7 +195,7 @@ typedef union oam_sensor_scale { * specific operation on that device. */ typedef struct oam_dev_handle { - void *handle; + void *handle; } oam_dev_handle_t; /** @@ -218,9 +207,9 @@ typedef struct oam_dev_handle { * device there are no side effects. */ typedef enum oam_dev_mode { - OAM_DEV_MODE_EXCLUSIVE = 0, - OAM_DEV_MODE_NONEXLUSIVE = 1, - OAM_DEV_MODE_UNKNOWN = 0xFF + OAM_DEV_MODE_EXCLUSIVE = 0, + OAM_DEV_MODE_NONEXLUSIVE = 1, + OAM_DEV_MODE_UNKNOWN = 0xFF } oam_dev_mode_t; /** @@ -237,10 +226,10 @@ typedef enum oam_dev_mode { * specific operation on that device. */ typedef struct oam_sensor_info { - char sensor_name[OAM_SENSOR_NAME_MAX]; - oam_sensor_type_t sensor_type; - oam_sensor_scale_t scale; - int32_t value; + char sensor_name[OAM_SENSOR_NAME_MAX]; + oam_sensor_type_t sensor_type; + oam_sensor_scale_t scale; + int32_t value; } oam_sensor_info_t; /** @@ -249,10 +238,10 @@ typedef struct oam_sensor_info { * \details Various types of errors reported by device. */ typedef struct oam_dev_error_count { - uint32_t total_error_count; - uint32_t fatal_error_count; - uint32_t unknown_error_count; - uint32_t ecc_error_count; + uint32_t total_error_count; + uint32_t fatal_error_count; + uint32_t unknown_error_count; + uint32_t ecc_error_count; } oam_dev_error_count_t; /** @@ -261,10 +250,10 @@ typedef struct oam_dev_error_count { * \details Structure to store various firmware versions of OAM module */ typedef struct oam_firmware_version { - oam_mapi_version_t device_boot_fw_version; - oam_mapi_version_t device_fw_version; - oam_mapi_version_t board_boot_fw_version; - oam_mapi_version_t board_fw_version; + oam_mapi_version_t device_boot_fw_version; + oam_mapi_version_t device_fw_version; + oam_mapi_version_t board_boot_fw_version; + oam_mapi_version_t board_fw_version; } oam_firmware_version_t; /** @@ -273,10 +262,10 @@ typedef struct oam_firmware_version { * \details Structure to store PCI (Domain, BDF) information of the device */ typedef struct oam_pci_info { - uint16_t domain; - uint8_t bus; - uint8_t device; - uint8_t function; + uint16_t domain; + uint8_t bus; + uint8_t device; + uint8_t function; } oam_pci_info_t; /** @@ -285,8 +274,8 @@ typedef struct oam_pci_info { * \details This enumerated type defines various states of the network port */ typedef enum oam_net_port_state { - OAM_NET_PORT_DISABLED = 0, - OAM_NET_PORT_ENABLED = 1 + OAM_NET_PORT_DISABLED = 0, + OAM_NET_PORT_ENABLED = 1 } oam_net_port_state_t; /** @@ -295,8 +284,8 @@ typedef enum oam_net_port_state { * \details This enumerated type defines various status of the network port */ typedef enum oam_net_port_status { - OAM_NET_PORT_UP = 0, - OAM_NET_PORT_DOWN = 1, + OAM_NET_PORT_UP = 0, + OAM_NET_PORT_DOWN = 1, } oam_net_port_status_t; /** @@ -305,10 +294,10 @@ typedef enum oam_net_port_status { * \details This enumerated type defines various identifiers for network ports */ typedef enum oam_net_port_id { - OAM_NET_PORT0 = 0, - OAM_NET_PORT1 = 1, - OAM_NET_PORT2 = 2, - OAM_NET_PORT_MAX = 0xFFFF + OAM_NET_PORT0 = 0, + OAM_NET_PORT1 = 1, + OAM_NET_PORT2 = 2, + OAM_NET_PORT_MAX = 0xFFFF } oam_net_port_id_t; /** @@ -318,8 +307,8 @@ typedef enum oam_net_port_id { * the device to update firmware. */ typedef enum oam_firmware_modes { - OAM_DOWNLOAD_ONLY = 0, - OAM_DOWNLOAD_ACTIVATE = 1 + OAM_DOWNLOAD_ONLY = 0, + OAM_DOWNLOAD_ACTIVATE = 1 } oam_firmware_modes_t; /** @@ -334,7 +323,7 @@ typedef enum oam_firmware_modes { * \details Structure to store additional details about the network port */ typedef struct oam_net_port_desc { - char name[OAM_NET_PORT_NAME]; + char name[OAM_NET_PORT_NAME]; } oam_net_port_desc_t; /** @@ -350,9 +339,9 @@ typedef struct oam_net_port_desc { * on a particular network. */ typedef struct oam_net_dev_info { - oam_net_dev_id_t net_dev_id; - char host_name[OAM_DEV_HOST_NAME]; - oam_pci_info_t pci_info; + oam_net_dev_id_t net_dev_id; + char host_name[OAM_DEV_HOST_NAME]; + oam_pci_info_t pci_info; } oam_net_dev_info_t; /** @@ -362,8 +351,8 @@ typedef struct oam_net_dev_info { * network */ typedef struct oam_neighbour_info { - oam_net_port_id_t device_port; - oam_net_dev_info_t device_info; + oam_net_port_id_t device_port; + oam_net_dev_info_t device_info; } oam_neighbour_info_t; /** @@ -372,10 +361,10 @@ typedef struct oam_neighbour_info { * \details This enumerated type defines various identifiers for TPCs */ typedef enum oam_dev_tpc_id { - OAM_DEV_TPC0, - OAM_DEV_TPC1, - OAM_DEV_TPC2, - OAM_DEV_TPC_MAX + OAM_DEV_TPC0, + OAM_DEV_TPC1, + OAM_DEV_TPC2, + OAM_DEV_TPC_MAX } oam_dev_tpc_id_t; /** @@ -391,7 +380,7 @@ typedef enum oam_dev_tpc_id { * to the id etc. */ typedef struct oam_tpc_desc { - char name[256]; + char name[256]; } oam_tpc_desc_t; /** @@ -401,7 +390,7 @@ typedef struct oam_tpc_desc { * e.g. TPC utilization */ typedef struct oam_dev_tpc_stats { - double util; + double util; } oam_dev_tpc_stats_t; /** @@ -410,10 +399,10 @@ typedef struct oam_dev_tpc_stats { * \details This enumerated type defines various identifiers for device memories */ typedef enum oam_dev_mem_id { - OAM_DEV_MEM0, - OAM_DEV_MEM1, - OAM_DEV_MEM2, - OAM_DEV_MEM_MAX + OAM_DEV_MEM0, + OAM_DEV_MEM1, + OAM_DEV_MEM2, + OAM_DEV_MEM_MAX } oam_dev_mem_id_t; /** @@ -422,7 +411,7 @@ typedef enum oam_dev_mem_id { * \details Structure to store additional details about device memories port */ typedef struct oam_mem_desc { - char name[256]; + char name[256]; } oam_mem_desc_t; /** @@ -432,9 +421,9 @@ typedef struct oam_mem_desc { * memory. */ typedef struct oam_dev_mem_stats { - uint32_t total_mem; - uint32_t allocated_mem; - uint32_t free_mem; + uint32_t total_mem; + uint32_t allocated_mem; + uint32_t free_mem; } oam_dev_mem_stats_t; /** @@ -444,10 +433,10 @@ typedef struct oam_dev_mem_stats { * packets on a given port. */ typedef struct oam_net_port_pkt_stats { - uint64_t rx_count; - uint64_t tx_count; - uint64_t rx_errors; - uint64_t tx_errors; + uint64_t rx_count; + uint64_t tx_count; + uint64_t rx_errors; + uint64_t tx_errors; } oam_net_port_pkt_stats_t; /** @@ -457,186 +446,187 @@ typedef struct oam_net_port_pkt_stats { * supported by the OAM library. */ typedef struct oam_ops { - /*!< - * to initialise library instance and perform version compatibility - * check - */ - int (*init)(oam_mapi_version_t version); - int (*free)(void); + /*!< + * to initialise library instance and perform version compatibility + * check + */ + int (*init)(void); + int (*free)(void); - /*!< - * To get error description from the error code - */ - int (*get_error_description)(int error_code, const char **error_description); + /*!< + * To get error description from the error code + */ + int (*get_error_description)(int error_code, const char **error_description); - /*!< - * To retrieve the OAM Management interface version - */ - int (*get_mapi_version)(oam_mapi_version_t *version); + /*!< + * To retrieve the OAM Management interface version + */ + int (*get_mapi_version)(oam_mapi_version_t *version); - /*!< - * To retrieve the number of devices present/discovered by the library - */ - int (*discover_devices)(int *device_count); + /*!< + * To retrieve the number of devices present/discovered by the library + */ + int (*discover_devices)(uint32_t *device_count); - /*!< - * To retrieve device properties for each discovered devices - */ - int (*get_dev_properties)(oam_dev_properties_t *devices); + /*!< + * To retrieve device properties for each discovered devices + */ + int (*get_dev_properties)(uint32_t device_count, + oam_dev_properties_t *devices); - /*!< - * To retrieve PCI properties of the device - */ - int (*get_pci_properties)(oam_dev_id_t *device_id, oam_pci_info_t *pci_info); + /*!< + * To retrieve PCI properties of the device + */ + int (*get_pci_properties)(uint32_t device_id, oam_pci_info_t *pci_info); - /*!< - * To query the number of various sensors present - */ - int (*get_sensors_count)(oam_dev_id_t *device_id, - oam_sensor_count_t *sensor_count); + /*!< + * To query the number of various sensors present + */ + int (*get_sensors_count)(uint32_t device_id, + oam_sensor_count_t *sensor_count); - /*!< - * Open the device and obtain handle - */ - int (*open_device)(oam_dev_id_t *dev_id, oam_dev_mode_t mode, - oam_dev_handle_t *handle); - int (*close_device)(oam_dev_handle_t *handle); + /*!< + * Open the device and obtain handle + */ + int (*open_device)(uint32_t *dev_id, oam_dev_mode_t mode, + oam_dev_handle_t *handle); + int (*close_device)(oam_dev_handle_t *handle); - /*!< - * To read various sensor values for a given sensor type - */ - int (*get_sensors_info)(oam_dev_handle_t *handle, - oam_sensor_type_t type, - uint32_t num_sensors, - oam_sensor_info_t sensor_info[]); - /*!< - * To read current error count of the device - */ - int (*get_device_error_count)(oam_dev_handle_t *handle, - oam_dev_error_count_t *count); + /*!< + * To read various sensor values for a given sensor type + */ + int (*get_sensors_info)(oam_dev_handle_t *handle, + oam_sensor_type_t type, + uint32_t num_sensors, + oam_sensor_info_t sensor_info[]); + /*!< + * To read current error count of the device + */ + int (*get_device_error_count)(oam_dev_handle_t *handle, + oam_dev_error_count_t *count); - /*!< - * To update firmware on the device - * fw_image contains a null terminated string which specifies complete - * path where the firmware image is located - */ - int (*download_firmware)(oam_dev_id_t *device_id, char *fw_image, - oam_firmware_modes_t mode); + /*!< + * To update firmware on the device + * fw_image contains a null terminated string which specifies complete + * path where the firmware image is located + */ + int (*download_firmware)(uint32_t *device_id, char *fw_image, + oam_firmware_modes_t mode); - /*!< - * To query firmware versions - */ - int (*get_firmware_version)(oam_dev_id_t *device_id, - oam_firmware_version_t *version); + /*!< + * To query firmware versions + */ + int (*get_firmware_version)(uint32_t *device_id, + oam_firmware_version_t *version); - /*!< - * to get network id from device id - */ - int (*get_net_dev_id)(oam_dev_id_t *device_id, oam_net_dev_id_t *net_device); + /*!< + * to get network id from device id + */ + int (*get_net_dev_id)(uint32_t *device_id, oam_net_dev_id_t *net_device); - /*!< - * Network management APIs. - */ + /*!< + * Network management APIs. + */ - /*!< - * discover network. - */ - int (*discover_network)(int *net_dev_count); - int (*get_dev_net_properties)(oam_net_dev_info_t *net_dev_info); + /*!< + * discover network. + */ + int (*discover_network)(int *net_dev_count); + int (*get_dev_net_properties)(oam_net_dev_info_t *net_dev_info); - int (*get_neighbour_count)(oam_dev_id_t *device, - oam_net_port_id_t local_port_id, - uint32_t *neighbor_count); + int (*get_neighbour_count)(uint32_t *device, + oam_net_port_id_t local_port_id, + uint32_t *neighbor_count); - int (*get_neighbours_info)(oam_dev_id_t *device, - oam_net_port_id_t local_port_id, - uint32_t *neighbors_count, - oam_neighbour_info_t *neighbours_info); + int (*get_neighbours_info)(uint32_t *device, + oam_net_port_id_t local_port_id, + uint32_t *neighbors_count, + oam_neighbour_info_t *neighbours_info); - int (*configure_network)(oam_net_dev_id_t *net_devices, - uint32_t *net_device_count, - char *network_name); + int (*configure_network)(oam_net_dev_id_t *net_devices, + uint32_t *net_device_count, + char *network_name); - int (*destroy_network)(char *network_name); + int (*destroy_network)(char *network_name); - int (*query_network)(char *network_name, oam_net_dev_info_t *devices, - uint32_t *device_count); + int (*query_network)(char *network_name, oam_net_dev_info_t *devices, + uint32_t *device_count); - int (*get_network_count)(uint32_t *network_count); - int (*list_networks)(char *network_names[]); + int (*get_network_count)(uint32_t *network_count); + int (*list_networks)(char *network_names[]); - /*!< - * Various statistics related to blocks - */ + /*!< + * Various statistics related to blocks + */ - /*!< - * To query number of ports - */ - int (*get_net_port_count)(oam_dev_handle_t *handle, uint32_t *count, - oam_net_port_id_t *port_ids); + /*!< + * To query number of ports + */ + int (*get_net_port_count)(oam_dev_handle_t *handle, uint32_t *count, + oam_net_port_id_t *port_ids); - int (*get_net_port_desc)(oam_dev_handle_t *handle, oam_net_port_id_t *port, - oam_net_port_desc_t *desc); + int (*get_net_port_desc)(oam_dev_handle_t *handle, oam_net_port_id_t *port, + oam_net_port_desc_t *desc); - int (*get_net_port_state)(oam_dev_handle_t *handle, oam_net_port_id_t *port, - oam_net_port_state_t *state); + int (*get_net_port_state)(oam_dev_handle_t *handle, oam_net_port_id_t *port, + oam_net_port_state_t *state); - int (*check_net_port_status)(oam_dev_handle_t *handle, - oam_net_port_id_t *port, - oam_net_port_status_t *status); - int (*get_net_port_pkt_stats)(oam_dev_handle_t *handle, - oam_net_port_id_t *port, - uint32_t duration_sec, - oam_net_port_pkt_stats_t *stats); + int (*check_net_port_status)(oam_dev_handle_t *handle, + oam_net_port_id_t *port, + oam_net_port_status_t *status); + int (*get_net_port_pkt_stats)(oam_dev_handle_t *handle, + oam_net_port_id_t *port, + uint32_t duration_sec, + oam_net_port_pkt_stats_t *stats); - int (*query_net_port_bandwidth)(oam_dev_handle_t *handle, - oam_net_port_id_t *port, - uint32_t duration_sec, - double *bandwidth); + int (*query_net_port_bandwidth)(oam_dev_handle_t *handle, + oam_net_port_id_t *port, + uint32_t duration_sec, + double *bandwidth); - int (*get_tpc_count)(oam_dev_handle_t *handle, uint32_t *count, - oam_dev_tpc_id_t *tpc_ids); + int (*get_tpc_count)(oam_dev_handle_t *handle, uint32_t *count, + oam_dev_tpc_id_t *tpc_ids); - int (*get_tpc_desc)(oam_dev_handle_t *handle, oam_dev_tpc_id_t *tpc_id, - oam_tpc_desc_t *desc); + int (*get_tpc_desc)(oam_dev_handle_t *handle, oam_dev_tpc_id_t *tpc_id, + oam_tpc_desc_t *desc); - int (*get_tpc_stats)(oam_dev_handle_t *handle, - oam_dev_tpc_id_t *port, - oam_dev_tpc_stats_t *stats, - uint32_t duration_sec); + int (*get_tpc_stats)(oam_dev_handle_t *handle, + oam_dev_tpc_id_t *port, + oam_dev_tpc_stats_t *stats, + uint32_t duration_sec); - int (*get_mem_count)(oam_dev_handle_t *handle, uint32_t *count, - oam_dev_mem_id_t *mem_ids); + int (*get_mem_count)(oam_dev_handle_t *handle, uint32_t *count, + oam_dev_mem_id_t *mem_ids); - int (*get_mem_desc)(oam_dev_handle_t *handle, oam_dev_mem_id_t *tpc_id, - oam_mem_desc_t *desc); + int (*get_mem_desc)(oam_dev_handle_t *handle, oam_dev_mem_id_t *tpc_id, + oam_mem_desc_t *desc); - int (*get_mem_stats)(oam_dev_handle_t *handle, oam_dev_mem_id_t *mem_id, - oam_dev_mem_stats_t *stats); + int (*get_mem_stats)(oam_dev_handle_t *handle, oam_dev_mem_id_t *mem_id, + oam_dev_mem_stats_t *stats); - /*!< - * To check the health of the individual components, libraries - * generates test workload to check if the block is functioning properly - * or not. So no other workload should be running while calling these - * APIs - */ - int (*check_tpc_health)(oam_dev_id_t *device_id, oam_dev_tpc_id_t *tpc_id); - int (*check_net_port_health)(oam_dev_id_t *device_id, - oam_net_port_id_t *port); - int (*check_mem_health)(oam_dev_id_t *device_id, oam_dev_mem_id_t *port); + /*!< + * To check the health of the individual components, libraries + * generates test workload to check if the block is functioning properly + * or not. So no other workload should be running while calling these + * APIs + */ + int (*check_tpc_health)(uint32_t *device_id, oam_dev_tpc_id_t *tpc_id); + int (*check_net_port_health)(uint32_t *device_id, oam_net_port_id_t *port); + int (*check_mem_health)(uint32_t *device_id, oam_dev_mem_id_t *port); - /* - * Following needs more attention, will work on in next - int (*get_fan_speed)(oam_dev_t *oam); - int (*set_fan_speed)(oam_dev_t *oam, int speed); + /* + * Following needs more attention, will work on in next + int (*get_fan_speed)(oam_dev_t *oam); + int (*set_fan_speed)(oam_dev_t *oam, int speed); - int (*get_power_cap)(oam_dev_t *oam); - int (*set_power_cap)(oam_dev_t *oam, int power); + int (*get_power_cap)(oam_dev_t *oam); + int (*set_power_cap)(oam_dev_t *oam, int power); + + int (*get_telemetry)(oam_dev_t *oam); + */ - int (*get_telemetry)(oam_dev_t *oam); - */ } oam_ops_t; diff --git a/projects/amdsmi/oam/src/amd_oam.cc b/projects/amdsmi/oam/src/amd_oam.cc index e3978134ff..26c0d79a22 100755 --- a/projects/amdsmi/oam/src/amd_oam.cc +++ b/projects/amdsmi/oam/src/amd_oam.cc @@ -23,8 +23,12 @@ */ #include - +#include #include +#include +#include +#include // NOLINT +#include #include "rocm_smi/rocm_smi_common.h" #include "rocm_smi/rocm_smi_main.h" @@ -38,52 +42,196 @@ #include "oam/oam_mapi.h" #include "oam/amd_oam.h" +static const std::map err_map = { + { AMDOAM_STATUS_INVALID_ARGS, "Invalid arguments" }, + { AMDOAM_STATUS_NOT_SUPPORTED, "Feature not supported" }, + { AMDOAM_STATUS_FILE_ERROR, "Problem accessing a file" }, + { AMDOAM_STATUS_PERMISSION, "Permission denied" }, + { AMDOAM_STATUS_OUT_OF_RESOURCES, "Not enough memory or other resource" }, + { AMDOAM_STATUS_INTERNAL_EXCEPTION, "An internal exception was caught" }, + { AMDOAM_STATUS_INPUT_OUT_OF_BOUNDS, + "The provided input is out of allowable or safe range" }, + { AMDOAM_STATUS_INIT_ERROR, "AMDOAM is not initialized or init failed" }, + { AMDOAM_STATUS_ERROR, "Generic error" }, + { AMDOAM_STATUS_NOT_FOUND, "An item was searched for but not found" } +}; + #define TRY try { #define CATCH } catch (...) {return handleRSMIException();} +static bool rsmi_initialized; -static int handleRSMIException() { - rsmi_status_t ret; - ret = amd::smi::handleException(); - - // TODO(x): convert RSMI return to OAM return - // For now, just return int equiv. - return static_cast(ret); +static int rsmi_status_to_amdoam_errorcode(rsmi_status_t status) { + if (status > RSMI_STATUS_INIT_ERROR) + return -AMDOAM_STATUS_ERROR; + else + return -status; } -int amdoam_init(oam_mapi_version_t version) { +static int handleRSMIException() { + rsmi_status_t ret = amd::smi::handleException(); + return rsmi_status_to_amdoam_errorcode(ret); +} + +int amdoam_get_error_description(int code, const char **description) { + if (description == nullptr) + return -AMDOAM_STATUS_INVALID_ARGS; + + auto search = err_map.find(code); + if (search == err_map.end()) + return -AMDOAM_STATUS_NOT_FOUND; + + *description = search->second; + return AMDOAM_STATUS_SUCCESS; +} + +int amdoam_init(void) { TRY - // TODO(x): handle version argument - (void)version; + rsmi_status_t status = rsmi_init(0); - rsmi_status_t ret = rsmi_init(0); + if (status != RSMI_STATUS_SUCCESS) + return rsmi_status_to_amdoam_errorcode(status); + + rsmi_initialized = true; + return AMDOAM_STATUS_SUCCESS; - return 0; CATCH } int amdoam_free(void) { - rsmi_status_t ret = rsmi_shut_down(); + rsmi_status_t status = rsmi_shut_down(); - // TODO(x) convert rsmi return to oam return val - return static_cast(ret); + if (status != RSMI_STATUS_SUCCESS) + return rsmi_status_to_amdoam_errorcode(status); + + return AMDOAM_STATUS_SUCCESS; } - -int amdoam_discover_devices(int *device_count) { - uint32_t dv_cnt; +int amdoam_discover_devices(uint32_t *device_count) { + rsmi_status_t status; if (device_count == nullptr) { - return -1; // TODO(x): return appropriate OAM code + return -AMDOAM_STATUS_INVALID_ARGS; } - rsmi_status_t ret = rsmi_num_monitor_devices(&dv_cnt); + status = rsmi_num_monitor_devices(device_count); + if (status != RSMI_STATUS_SUCCESS) + return rsmi_status_to_amdoam_errorcode(status); - *device_count = static_cast(dv_cnt); + return AMDOAM_STATUS_SUCCESS; +} - // TODO(x) convert rsmi return to oam return val - return static_cast(ret); +int amdoam_get_pci_properties(uint32_t device_id, oam_pci_info_t *pci_info) { + uint64_t bdfid; + + TRY + if (pci_info == nullptr) { + return -AMDOAM_STATUS_INVALID_ARGS; + } + + rsmi_status_t status = rsmi_dev_pci_id_get(device_id, &bdfid); + if (status != RSMI_STATUS_SUCCESS) + return rsmi_status_to_amdoam_errorcode(status); + + pci_info->domain = (uint16_t)(bdfid >> 32) & 0xffff; + pci_info->bus = (bdfid >> 8) & 0xff; + pci_info->device = (bdfid >> 3) & 0x1f; + pci_info->function = bdfid & 0x7; + CATCH + + return AMDOAM_STATUS_SUCCESS; +} + +int amdoam_get_dev_properties(uint32_t num_devices, + oam_dev_properties_t *devices) { + const size_t buf_size = 32; + char buf[buf_size] = ""; + uint32_t dev_inx; + oam_dev_properties_t *dev = devices; + +TRY + if (devices == nullptr) + return -AMDOAM_STATUS_INVALID_ARGS; + if (!rsmi_initialized) + return -AMDOAM_STATUS_INIT_ERROR; + + for (dev_inx = 0; dev_inx < num_devices; dev_inx++) { + dev->device_id = dev_inx; + /* If fails to get any following properties, it's not treated as a deal + * breaker. Variable not filled means that property is not available on + * this device or AMD doesn't support that property. + */ + rsmi_dev_vendor_name_get(dev_inx, dev->device_vendor, DEVICE_VENDOR_LEN); + rsmi_dev_name_get(dev_inx, dev->device_name, DEVICE_NAME_LEN); + rsmi_dev_vbios_version_get(dev_inx, buf, buf_size); + if (std::strlen(buf) > 0) { + std::strncpy(dev->sku_name, &buf[4], 6); + std::strncpy(dev->board_name, buf, 12); + } + rsmi_dev_serial_number_get(dev_inx, dev->board_serial_number, + BOARD_SERIAL_NUM_LEN); + ++dev; + } +CATCH + + return AMDOAM_STATUS_SUCCESS; +} + +static int get_num_sensors(std::string hwmon_path, std::string fn_reg_ex, + uint32_t *sensor_max) { + *sensor_max = 0; + fn_reg_ex = "\\b" + fn_reg_ex + "([0-9]+)([^ ]*)"; + std::string fn; + std::smatch m; + uint32_t temp = 0; + std::regex re(fn_reg_ex); + auto hwmon_dir = opendir(hwmon_path.c_str()); + if (hwmon_dir == nullptr) + return 0; + auto dentry = readdir(hwmon_dir); + while (dentry != nullptr) { + fn = dentry->d_name; + if (std::regex_search(fn, m, re)) { + std::string output = std::regex_replace( + fn, + std::regex("[^0-9]*([0-9]+).*"), + std::string("$1")); + temp = stoi(output); + if (temp > *sensor_max) { + *sensor_max = temp; + } + } + dentry = readdir(hwmon_dir); + } + + if (closedir(hwmon_dir)) { + return -errno; + } + return 0; +} + +int amdoam_get_sensors_count(uint32_t device_id, + oam_sensor_count_t *sensor_count) { + uint32_t dv_ind = device_id; + uint32_t num_sensors = 0; + + TRY + GET_DEV_FROM_INDX + std::string hwmon_path = dev->monitor()->path(); + get_num_sensors(hwmon_path, "temp", &num_sensors); + sensor_count->num_temperature_sensors = num_sensors; + get_num_sensors(hwmon_path, "fan", &num_sensors); + sensor_count->num_fans = num_sensors; + get_num_sensors(hwmon_path, "in", &num_sensors); + sensor_count->num_voltage_sensors = (num_sensors+1); + get_num_sensors(hwmon_path, "power", &num_sensors); + sensor_count->num_power_sensors = num_sensors; + get_num_sensors(hwmon_path, "current", &num_sensors); + sensor_count->num_current_sensors = num_sensors; + CATCH + + return AMDOAM_STATUS_SUCCESS; } // TODO(x): This function doesn't work for OAM. It's just a version @@ -158,4 +306,3 @@ get_device_error_count(oam_dev_handle_t *handle, return ret; CATCH } -