|
Packit |
7e09eb |
/*
|
|
Packit |
7e09eb |
* AMD LED control
|
|
Packit Service |
cb68d2 |
* Copyright (C) 2021, Advanced Micro Devices, Inc.
|
|
Packit |
7e09eb |
*
|
|
Packit |
7e09eb |
* This program is free software; you can redistribute it and/or modify it
|
|
Packit |
7e09eb |
* under the terms and conditions of the GNU General Public License,
|
|
Packit |
7e09eb |
* version 2, as published by the Free Software Foundation.
|
|
Packit |
7e09eb |
*
|
|
Packit |
7e09eb |
* This program is distributed in the hope it will be useful, but WITHOUT
|
|
Packit |
7e09eb |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
Packit |
7e09eb |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
|
Packit |
7e09eb |
* more details.
|
|
Packit |
7e09eb |
*
|
|
Packit |
7e09eb |
* You should have received a copy of the GNU General Public License along with
|
|
Packit |
7e09eb |
* this program; if not, write to the Free Software Foundation, Inc.,
|
|
Packit |
7e09eb |
* 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
|
|
Packit |
7e09eb |
*
|
|
Packit |
7e09eb |
*/
|
|
Packit |
7e09eb |
|
|
Packit |
7e09eb |
#include <errno.h>
|
|
Packit |
7e09eb |
#include <fcntl.h>
|
|
Packit |
7e09eb |
#include <limits.h>
|
|
Packit |
7e09eb |
#include <stdint.h>
|
|
Packit |
7e09eb |
#include <stdio.h>
|
|
Packit |
7e09eb |
#include <stdlib.h>
|
|
Packit |
7e09eb |
#include <string.h>
|
|
Packit |
7e09eb |
#include <unistd.h>
|
|
Packit |
7e09eb |
#include <libgen.h>
|
|
Packit |
7e09eb |
#include <inttypes.h>
|
|
Packit |
7e09eb |
#include <sys/stat.h>
|
|
Packit |
7e09eb |
#include <sys/mman.h>
|
|
Packit |
7e09eb |
#include <sys/sysinfo.h>
|
|
Packit |
7e09eb |
#include <sys/file.h>
|
|
Packit |
7e09eb |
|
|
Packit |
7e09eb |
#if _HAVE_DMALLOC_H
|
|
Packit |
7e09eb |
#include <dmalloc.h>
|
|
Packit |
7e09eb |
#endif
|
|
Packit |
7e09eb |
|
|
Packit |
7e09eb |
#include "config.h"
|
|
Packit |
7e09eb |
#include "ibpi.h"
|
|
Packit |
7e09eb |
#include "list.h"
|
|
Packit |
7e09eb |
#include "utils.h"
|
|
Packit |
7e09eb |
#include "amd.h"
|
|
Packit |
7e09eb |
#include "amd_sgpio.h"
|
|
Packit |
7e09eb |
#include "amd_ipmi.h"
|
|
Packit |
7e09eb |
|
|
Packit |
7e09eb |
enum amd_led_interfaces amd_interface = AMD_INTF_UNSET;
|
|
Packit Service |
cb68d2 |
enum amd_ipmi_platforms amd_ipmi_platform = AMD_PLATFORM_UNSET;
|
|
Packit |
7e09eb |
|
|
Packit |
7e09eb |
int _find_file_path(const char *start_path, const char *filename,
|
|
Packit |
7e09eb |
char *path, size_t path_len)
|
|
Packit |
7e09eb |
{
|
|
Packit |
7e09eb |
int rc, found;
|
|
Packit |
7e09eb |
struct stat sbuf;
|
|
Packit |
7e09eb |
struct list dir;
|
|
Packit |
7e09eb |
char *dir_name;
|
|
Packit |
7e09eb |
const char *dir_path;
|
|
Packit |
7e09eb |
|
|
Packit |
7e09eb |
rc = scan_dir(start_path, &dir;;
|
|
Packit |
7e09eb |
if (rc) {
|
|
Packit |
7e09eb |
log_info("Failed to scan %s", start_path);
|
|
Packit |
7e09eb |
return 0;
|
|
Packit |
7e09eb |
}
|
|
Packit |
7e09eb |
|
|
Packit |
7e09eb |
found = 0;
|
|
Packit |
7e09eb |
list_for_each(&dir, dir_path) {
|
|
Packit |
7e09eb |
dir_name = strrchr(dir_path, '/');
|
|
Packit |
7e09eb |
if (!dir_name)
|
|
Packit |
7e09eb |
continue;
|
|
Packit |
7e09eb |
|
|
Packit |
7e09eb |
/* skip past the leading '/' */
|
|
Packit |
7e09eb |
dir_name++;
|
|
Packit |
7e09eb |
|
|
Packit |
7e09eb |
if (strncmp(dir_name, filename, strlen(filename)) == 0) {
|
|
Packit |
7e09eb |
char tmp[PATH_MAX + 1];
|
|
Packit |
7e09eb |
|
|
Packit |
7e09eb |
strncpy(tmp, dir_path, path_len);
|
|
Packit |
7e09eb |
snprintf(path, path_len, "%s", dirname(tmp));
|
|
Packit |
7e09eb |
|
|
Packit |
7e09eb |
found = 1;
|
|
Packit |
7e09eb |
break;
|
|
Packit |
7e09eb |
}
|
|
Packit |
7e09eb |
|
|
Packit |
7e09eb |
if (lstat(dir_path, &sbuf) == -1)
|
|
Packit |
7e09eb |
continue;
|
|
Packit |
7e09eb |
|
|
Packit |
7e09eb |
if (S_ISDIR(sbuf.st_mode)) {
|
|
Packit |
7e09eb |
found = _find_file_path(dir_path, filename,
|
|
Packit |
7e09eb |
path, path_len);
|
|
Packit |
7e09eb |
if (found)
|
|
Packit |
7e09eb |
break;
|
|
Packit |
7e09eb |
}
|
|
Packit |
7e09eb |
}
|
|
Packit |
7e09eb |
|
|
Packit |
7e09eb |
list_erase(&dir;;
|
|
Packit |
7e09eb |
return found;
|
|
Packit |
7e09eb |
}
|
|
Packit |
7e09eb |
|
|
Packit Service |
cb68d2 |
/* For AMD platforms to use IPMI for LED control we need to know
|
|
Packit Service |
cb68d2 |
* the platform we're running on. This enables us to select the
|
|
Packit Service |
cb68d2 |
* proper channel and slave address when making IPMI requests.
|
|
Packit Service |
cb68d2 |
* Platforms not checked for IPMI enablement default to using SGPIO.
|
|
Packit Service |
cb68d2 |
*/
|
|
Packit Service |
cb68d2 |
int amd_em_enabled(const char *path)
|
|
Packit |
7e09eb |
{
|
|
Packit Service |
cb68d2 |
char *platform;
|
|
Packit Service |
cb68d2 |
int rc;
|
|
Packit |
7e09eb |
|
|
Packit Service |
cb68d2 |
/* Default to SGPIO interface */
|
|
Packit Service |
cb68d2 |
amd_interface = AMD_INTF_SGPIO;
|
|
Packit |
7e09eb |
|
|
Packit Service |
cb68d2 |
platform = get_text("/sys/class/dmi/id", "product_name");
|
|
Packit Service |
cb68d2 |
if (!platform)
|
|
Packit Service |
cb68d2 |
return 0;
|
|
Packit Service |
cb68d2 |
|
|
Packit Service |
cb68d2 |
/* Check IPMI platforms */
|
|
Packit Service |
cb68d2 |
if (!strncmp(platform, "ETHANOL_X", 9)) {
|
|
Packit |
7e09eb |
amd_interface = AMD_INTF_IPMI;
|
|
Packit Service |
cb68d2 |
amd_ipmi_platform = AMD_PLATFORM_ETHANOL_X;
|
|
Packit Service |
cb68d2 |
} else if (!strncmp(platform, "DAYTONA_X", 9)) {
|
|
Packit |
7e09eb |
amd_interface = AMD_INTF_IPMI;
|
|
Packit Service |
cb68d2 |
amd_ipmi_platform = AMD_PLATFORM_DAYTONA_X;
|
|
Packit |
7e09eb |
}
|
|
Packit |
7e09eb |
|
|
Packit |
7e09eb |
switch (amd_interface) {
|
|
Packit |
7e09eb |
case AMD_INTF_SGPIO:
|
|
Packit |
7e09eb |
rc = _amd_sgpio_em_enabled(path);
|
|
Packit |
7e09eb |
break;
|
|
Packit |
7e09eb |
case AMD_INTF_IPMI:
|
|
Packit |
7e09eb |
rc = _amd_ipmi_em_enabled(path);
|
|
Packit |
7e09eb |
break;
|
|
Packit |
7e09eb |
default:
|
|
Packit Service |
cb68d2 |
log_error("Unknown interface for AMD %s platform\n",
|
|
Packit Service |
cb68d2 |
platform);
|
|
Packit |
7e09eb |
rc = -EOPNOTSUPP;
|
|
Packit |
7e09eb |
break;
|
|
Packit |
7e09eb |
}
|
|
Packit |
7e09eb |
|
|
Packit |
7e09eb |
return rc;
|
|
Packit |
7e09eb |
}
|
|
Packit |
7e09eb |
|
|
Packit |
7e09eb |
int amd_write(struct block_device *device, enum ibpi_pattern ibpi)
|
|
Packit |
7e09eb |
{
|
|
Packit |
7e09eb |
int rc;
|
|
Packit |
7e09eb |
|
|
Packit |
7e09eb |
/* write only if state has changed */
|
|
Packit |
7e09eb |
if (ibpi == device->ibpi_prev)
|
|
Packit |
7e09eb |
return 1;
|
|
Packit |
7e09eb |
|
|
Packit |
7e09eb |
switch (amd_interface) {
|
|
Packit |
7e09eb |
case AMD_INTF_SGPIO:
|
|
Packit |
7e09eb |
rc = _amd_sgpio_write(device, ibpi);
|
|
Packit |
7e09eb |
break;
|
|
Packit |
7e09eb |
case AMD_INTF_IPMI:
|
|
Packit |
7e09eb |
rc = _amd_ipmi_write(device, ibpi);
|
|
Packit |
7e09eb |
break;
|
|
Packit |
7e09eb |
case AMD_INTF_UNSET:
|
|
Packit |
7e09eb |
default:
|
|
Packit |
7e09eb |
log_error("Unsupported AMD interface\n");
|
|
Packit |
7e09eb |
rc = -EOPNOTSUPP;
|
|
Packit |
7e09eb |
break;
|
|
Packit |
7e09eb |
}
|
|
Packit |
7e09eb |
|
|
Packit |
7e09eb |
return rc;
|
|
Packit |
7e09eb |
}
|
|
Packit |
7e09eb |
|
|
Packit |
7e09eb |
char *amd_get_path(const char *cntrl_path, const char *sysfs_path)
|
|
Packit |
7e09eb |
{
|
|
Packit |
7e09eb |
char *path;
|
|
Packit |
7e09eb |
|
|
Packit |
7e09eb |
switch (amd_interface) {
|
|
Packit |
7e09eb |
case AMD_INTF_SGPIO:
|
|
Packit |
7e09eb |
path = _amd_sgpio_get_path(sysfs_path);
|
|
Packit |
7e09eb |
break;
|
|
Packit |
7e09eb |
case AMD_INTF_IPMI:
|
|
Packit |
7e09eb |
path = _amd_ipmi_get_path(cntrl_path, sysfs_path);
|
|
Packit |
7e09eb |
break;
|
|
Packit |
7e09eb |
case AMD_INTF_UNSET:
|
|
Packit |
7e09eb |
default:
|
|
Packit |
7e09eb |
log_error("Unsupported AMD interface\n");
|
|
Packit |
7e09eb |
path = NULL;
|
|
Packit |
7e09eb |
break;
|
|
Packit |
7e09eb |
}
|
|
Packit |
7e09eb |
|
|
Packit |
7e09eb |
return path;
|
|
Packit |
7e09eb |
}
|