18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only 28c2ecf20Sopenharmony_ci/**************************************************************************** 38c2ecf20Sopenharmony_ci * Driver for Solarflare network controllers and boards 48c2ecf20Sopenharmony_ci * Copyright 2011-2013 Solarflare Communications Inc. 58c2ecf20Sopenharmony_ci */ 68c2ecf20Sopenharmony_ci 78c2ecf20Sopenharmony_ci#include <linux/bitops.h> 88c2ecf20Sopenharmony_ci#include <linux/slab.h> 98c2ecf20Sopenharmony_ci#include <linux/hwmon.h> 108c2ecf20Sopenharmony_ci#include <linux/stat.h> 118c2ecf20Sopenharmony_ci 128c2ecf20Sopenharmony_ci#include "net_driver.h" 138c2ecf20Sopenharmony_ci#include "mcdi.h" 148c2ecf20Sopenharmony_ci#include "mcdi_pcol.h" 158c2ecf20Sopenharmony_ci#include "nic.h" 168c2ecf20Sopenharmony_ci 178c2ecf20Sopenharmony_cienum efx_hwmon_type { 188c2ecf20Sopenharmony_ci EFX_HWMON_UNKNOWN, 198c2ecf20Sopenharmony_ci EFX_HWMON_TEMP, /* temperature */ 208c2ecf20Sopenharmony_ci EFX_HWMON_COOL, /* cooling device, probably a heatsink */ 218c2ecf20Sopenharmony_ci EFX_HWMON_IN, /* voltage */ 228c2ecf20Sopenharmony_ci EFX_HWMON_CURR, /* current */ 238c2ecf20Sopenharmony_ci EFX_HWMON_POWER, /* power */ 248c2ecf20Sopenharmony_ci EFX_HWMON_TYPES_COUNT 258c2ecf20Sopenharmony_ci}; 268c2ecf20Sopenharmony_ci 278c2ecf20Sopenharmony_cistatic const char *const efx_hwmon_unit[EFX_HWMON_TYPES_COUNT] = { 288c2ecf20Sopenharmony_ci [EFX_HWMON_TEMP] = " degC", 298c2ecf20Sopenharmony_ci [EFX_HWMON_COOL] = " rpm", /* though nonsense for a heatsink */ 308c2ecf20Sopenharmony_ci [EFX_HWMON_IN] = " mV", 318c2ecf20Sopenharmony_ci [EFX_HWMON_CURR] = " mA", 328c2ecf20Sopenharmony_ci [EFX_HWMON_POWER] = " W", 338c2ecf20Sopenharmony_ci}; 348c2ecf20Sopenharmony_ci 358c2ecf20Sopenharmony_cistatic const struct { 368c2ecf20Sopenharmony_ci const char *label; 378c2ecf20Sopenharmony_ci enum efx_hwmon_type hwmon_type; 388c2ecf20Sopenharmony_ci int port; 398c2ecf20Sopenharmony_ci} efx_mcdi_sensor_type[] = { 408c2ecf20Sopenharmony_ci#define SENSOR(name, label, hwmon_type, port) \ 418c2ecf20Sopenharmony_ci [MC_CMD_SENSOR_##name] = { label, EFX_HWMON_ ## hwmon_type, port } 428c2ecf20Sopenharmony_ci SENSOR(CONTROLLER_TEMP, "Controller board temp.", TEMP, -1), 438c2ecf20Sopenharmony_ci SENSOR(PHY_COMMON_TEMP, "PHY temp.", TEMP, -1), 448c2ecf20Sopenharmony_ci SENSOR(CONTROLLER_COOLING, "Controller heat sink", COOL, -1), 458c2ecf20Sopenharmony_ci SENSOR(PHY0_TEMP, "PHY temp.", TEMP, 0), 468c2ecf20Sopenharmony_ci SENSOR(PHY0_COOLING, "PHY heat sink", COOL, 0), 478c2ecf20Sopenharmony_ci SENSOR(PHY1_TEMP, "PHY temp.", TEMP, 1), 488c2ecf20Sopenharmony_ci SENSOR(PHY1_COOLING, "PHY heat sink", COOL, 1), 498c2ecf20Sopenharmony_ci SENSOR(IN_1V0, "1.0V supply", IN, -1), 508c2ecf20Sopenharmony_ci SENSOR(IN_1V2, "1.2V supply", IN, -1), 518c2ecf20Sopenharmony_ci SENSOR(IN_1V8, "1.8V supply", IN, -1), 528c2ecf20Sopenharmony_ci SENSOR(IN_2V5, "2.5V supply", IN, -1), 538c2ecf20Sopenharmony_ci SENSOR(IN_3V3, "3.3V supply", IN, -1), 548c2ecf20Sopenharmony_ci SENSOR(IN_12V0, "12.0V supply", IN, -1), 558c2ecf20Sopenharmony_ci SENSOR(IN_1V2A, "1.2V analogue supply", IN, -1), 568c2ecf20Sopenharmony_ci SENSOR(IN_VREF, "Ref. voltage", IN, -1), 578c2ecf20Sopenharmony_ci SENSOR(OUT_VAOE, "AOE FPGA supply", IN, -1), 588c2ecf20Sopenharmony_ci SENSOR(AOE_TEMP, "AOE FPGA temp.", TEMP, -1), 598c2ecf20Sopenharmony_ci SENSOR(PSU_AOE_TEMP, "AOE regulator temp.", TEMP, -1), 608c2ecf20Sopenharmony_ci SENSOR(PSU_TEMP, "Controller regulator temp.", 618c2ecf20Sopenharmony_ci TEMP, -1), 628c2ecf20Sopenharmony_ci SENSOR(FAN_0, "Fan 0", COOL, -1), 638c2ecf20Sopenharmony_ci SENSOR(FAN_1, "Fan 1", COOL, -1), 648c2ecf20Sopenharmony_ci SENSOR(FAN_2, "Fan 2", COOL, -1), 658c2ecf20Sopenharmony_ci SENSOR(FAN_3, "Fan 3", COOL, -1), 668c2ecf20Sopenharmony_ci SENSOR(FAN_4, "Fan 4", COOL, -1), 678c2ecf20Sopenharmony_ci SENSOR(IN_VAOE, "AOE input supply", IN, -1), 688c2ecf20Sopenharmony_ci SENSOR(OUT_IAOE, "AOE output current", CURR, -1), 698c2ecf20Sopenharmony_ci SENSOR(IN_IAOE, "AOE input current", CURR, -1), 708c2ecf20Sopenharmony_ci SENSOR(NIC_POWER, "Board power use", POWER, -1), 718c2ecf20Sopenharmony_ci SENSOR(IN_0V9, "0.9V supply", IN, -1), 728c2ecf20Sopenharmony_ci SENSOR(IN_I0V9, "0.9V supply current", CURR, -1), 738c2ecf20Sopenharmony_ci SENSOR(IN_I1V2, "1.2V supply current", CURR, -1), 748c2ecf20Sopenharmony_ci SENSOR(IN_0V9_ADC, "0.9V supply (ext. ADC)", IN, -1), 758c2ecf20Sopenharmony_ci SENSOR(CONTROLLER_2_TEMP, "Controller board temp. 2", TEMP, -1), 768c2ecf20Sopenharmony_ci SENSOR(VREG_INTERNAL_TEMP, "Regulator die temp.", TEMP, -1), 778c2ecf20Sopenharmony_ci SENSOR(VREG_0V9_TEMP, "0.9V regulator temp.", TEMP, -1), 788c2ecf20Sopenharmony_ci SENSOR(VREG_1V2_TEMP, "1.2V regulator temp.", TEMP, -1), 798c2ecf20Sopenharmony_ci SENSOR(CONTROLLER_VPTAT, 808c2ecf20Sopenharmony_ci "Controller PTAT voltage (int. ADC)", IN, -1), 818c2ecf20Sopenharmony_ci SENSOR(CONTROLLER_INTERNAL_TEMP, 828c2ecf20Sopenharmony_ci "Controller die temp. (int. ADC)", TEMP, -1), 838c2ecf20Sopenharmony_ci SENSOR(CONTROLLER_VPTAT_EXTADC, 848c2ecf20Sopenharmony_ci "Controller PTAT voltage (ext. ADC)", IN, -1), 858c2ecf20Sopenharmony_ci SENSOR(CONTROLLER_INTERNAL_TEMP_EXTADC, 868c2ecf20Sopenharmony_ci "Controller die temp. (ext. ADC)", TEMP, -1), 878c2ecf20Sopenharmony_ci SENSOR(AMBIENT_TEMP, "Ambient temp.", TEMP, -1), 888c2ecf20Sopenharmony_ci SENSOR(AIRFLOW, "Air flow raw", IN, -1), 898c2ecf20Sopenharmony_ci SENSOR(VDD08D_VSS08D_CSR, "0.9V die (int. ADC)", IN, -1), 908c2ecf20Sopenharmony_ci SENSOR(VDD08D_VSS08D_CSR_EXTADC, "0.9V die (ext. ADC)", IN, -1), 918c2ecf20Sopenharmony_ci SENSOR(HOTPOINT_TEMP, "Controller board temp. (hotpoint)", TEMP, -1), 928c2ecf20Sopenharmony_ci#undef SENSOR 938c2ecf20Sopenharmony_ci}; 948c2ecf20Sopenharmony_ci 958c2ecf20Sopenharmony_cistatic const char *const sensor_status_names[] = { 968c2ecf20Sopenharmony_ci [MC_CMD_SENSOR_STATE_OK] = "OK", 978c2ecf20Sopenharmony_ci [MC_CMD_SENSOR_STATE_WARNING] = "Warning", 988c2ecf20Sopenharmony_ci [MC_CMD_SENSOR_STATE_FATAL] = "Fatal", 998c2ecf20Sopenharmony_ci [MC_CMD_SENSOR_STATE_BROKEN] = "Device failure", 1008c2ecf20Sopenharmony_ci [MC_CMD_SENSOR_STATE_NO_READING] = "No reading", 1018c2ecf20Sopenharmony_ci}; 1028c2ecf20Sopenharmony_ci 1038c2ecf20Sopenharmony_civoid efx_mcdi_sensor_event(struct efx_nic *efx, efx_qword_t *ev) 1048c2ecf20Sopenharmony_ci{ 1058c2ecf20Sopenharmony_ci unsigned int type, state, value; 1068c2ecf20Sopenharmony_ci enum efx_hwmon_type hwmon_type = EFX_HWMON_UNKNOWN; 1078c2ecf20Sopenharmony_ci const char *name = NULL, *state_txt, *unit; 1088c2ecf20Sopenharmony_ci 1098c2ecf20Sopenharmony_ci type = EFX_QWORD_FIELD(*ev, MCDI_EVENT_SENSOREVT_MONITOR); 1108c2ecf20Sopenharmony_ci state = EFX_QWORD_FIELD(*ev, MCDI_EVENT_SENSOREVT_STATE); 1118c2ecf20Sopenharmony_ci value = EFX_QWORD_FIELD(*ev, MCDI_EVENT_SENSOREVT_VALUE); 1128c2ecf20Sopenharmony_ci 1138c2ecf20Sopenharmony_ci /* Deal gracefully with the board having more drivers than we 1148c2ecf20Sopenharmony_ci * know about, but do not expect new sensor states. */ 1158c2ecf20Sopenharmony_ci if (type < ARRAY_SIZE(efx_mcdi_sensor_type)) { 1168c2ecf20Sopenharmony_ci name = efx_mcdi_sensor_type[type].label; 1178c2ecf20Sopenharmony_ci hwmon_type = efx_mcdi_sensor_type[type].hwmon_type; 1188c2ecf20Sopenharmony_ci } 1198c2ecf20Sopenharmony_ci if (!name) 1208c2ecf20Sopenharmony_ci name = "No sensor name available"; 1218c2ecf20Sopenharmony_ci EFX_WARN_ON_PARANOID(state >= ARRAY_SIZE(sensor_status_names)); 1228c2ecf20Sopenharmony_ci state_txt = sensor_status_names[state]; 1238c2ecf20Sopenharmony_ci EFX_WARN_ON_PARANOID(hwmon_type >= EFX_HWMON_TYPES_COUNT); 1248c2ecf20Sopenharmony_ci unit = efx_hwmon_unit[hwmon_type]; 1258c2ecf20Sopenharmony_ci if (!unit) 1268c2ecf20Sopenharmony_ci unit = ""; 1278c2ecf20Sopenharmony_ci 1288c2ecf20Sopenharmony_ci netif_err(efx, hw, efx->net_dev, 1298c2ecf20Sopenharmony_ci "Sensor %d (%s) reports condition '%s' for value %d%s\n", 1308c2ecf20Sopenharmony_ci type, name, state_txt, value, unit); 1318c2ecf20Sopenharmony_ci} 1328c2ecf20Sopenharmony_ci 1338c2ecf20Sopenharmony_ci#ifdef CONFIG_SFC_MCDI_MON 1348c2ecf20Sopenharmony_ci 1358c2ecf20Sopenharmony_cistruct efx_mcdi_mon_attribute { 1368c2ecf20Sopenharmony_ci struct device_attribute dev_attr; 1378c2ecf20Sopenharmony_ci unsigned int index; 1388c2ecf20Sopenharmony_ci unsigned int type; 1398c2ecf20Sopenharmony_ci enum efx_hwmon_type hwmon_type; 1408c2ecf20Sopenharmony_ci unsigned int limit_value; 1418c2ecf20Sopenharmony_ci char name[12]; 1428c2ecf20Sopenharmony_ci}; 1438c2ecf20Sopenharmony_ci 1448c2ecf20Sopenharmony_cistatic int efx_mcdi_mon_update(struct efx_nic *efx) 1458c2ecf20Sopenharmony_ci{ 1468c2ecf20Sopenharmony_ci struct efx_mcdi_mon *hwmon = efx_mcdi_mon(efx); 1478c2ecf20Sopenharmony_ci MCDI_DECLARE_BUF(inbuf, MC_CMD_READ_SENSORS_EXT_IN_LEN); 1488c2ecf20Sopenharmony_ci int rc; 1498c2ecf20Sopenharmony_ci 1508c2ecf20Sopenharmony_ci MCDI_SET_QWORD(inbuf, READ_SENSORS_EXT_IN_DMA_ADDR, 1518c2ecf20Sopenharmony_ci hwmon->dma_buf.dma_addr); 1528c2ecf20Sopenharmony_ci MCDI_SET_DWORD(inbuf, READ_SENSORS_EXT_IN_LENGTH, hwmon->dma_buf.len); 1538c2ecf20Sopenharmony_ci 1548c2ecf20Sopenharmony_ci rc = efx_mcdi_rpc(efx, MC_CMD_READ_SENSORS, 1558c2ecf20Sopenharmony_ci inbuf, sizeof(inbuf), NULL, 0, NULL); 1568c2ecf20Sopenharmony_ci if (rc == 0) 1578c2ecf20Sopenharmony_ci hwmon->last_update = jiffies; 1588c2ecf20Sopenharmony_ci return rc; 1598c2ecf20Sopenharmony_ci} 1608c2ecf20Sopenharmony_ci 1618c2ecf20Sopenharmony_cistatic int efx_mcdi_mon_get_entry(struct device *dev, unsigned int index, 1628c2ecf20Sopenharmony_ci efx_dword_t *entry) 1638c2ecf20Sopenharmony_ci{ 1648c2ecf20Sopenharmony_ci struct efx_nic *efx = dev_get_drvdata(dev->parent); 1658c2ecf20Sopenharmony_ci struct efx_mcdi_mon *hwmon = efx_mcdi_mon(efx); 1668c2ecf20Sopenharmony_ci int rc; 1678c2ecf20Sopenharmony_ci 1688c2ecf20Sopenharmony_ci BUILD_BUG_ON(MC_CMD_READ_SENSORS_OUT_LEN != 0); 1698c2ecf20Sopenharmony_ci 1708c2ecf20Sopenharmony_ci mutex_lock(&hwmon->update_lock); 1718c2ecf20Sopenharmony_ci 1728c2ecf20Sopenharmony_ci /* Use cached value if last update was < 1 s ago */ 1738c2ecf20Sopenharmony_ci if (time_before(jiffies, hwmon->last_update + HZ)) 1748c2ecf20Sopenharmony_ci rc = 0; 1758c2ecf20Sopenharmony_ci else 1768c2ecf20Sopenharmony_ci rc = efx_mcdi_mon_update(efx); 1778c2ecf20Sopenharmony_ci 1788c2ecf20Sopenharmony_ci /* Copy out the requested entry */ 1798c2ecf20Sopenharmony_ci *entry = ((efx_dword_t *)hwmon->dma_buf.addr)[index]; 1808c2ecf20Sopenharmony_ci 1818c2ecf20Sopenharmony_ci mutex_unlock(&hwmon->update_lock); 1828c2ecf20Sopenharmony_ci 1838c2ecf20Sopenharmony_ci return rc; 1848c2ecf20Sopenharmony_ci} 1858c2ecf20Sopenharmony_ci 1868c2ecf20Sopenharmony_cistatic ssize_t efx_mcdi_mon_show_value(struct device *dev, 1878c2ecf20Sopenharmony_ci struct device_attribute *attr, 1888c2ecf20Sopenharmony_ci char *buf) 1898c2ecf20Sopenharmony_ci{ 1908c2ecf20Sopenharmony_ci struct efx_mcdi_mon_attribute *mon_attr = 1918c2ecf20Sopenharmony_ci container_of(attr, struct efx_mcdi_mon_attribute, dev_attr); 1928c2ecf20Sopenharmony_ci efx_dword_t entry; 1938c2ecf20Sopenharmony_ci unsigned int value, state; 1948c2ecf20Sopenharmony_ci int rc; 1958c2ecf20Sopenharmony_ci 1968c2ecf20Sopenharmony_ci rc = efx_mcdi_mon_get_entry(dev, mon_attr->index, &entry); 1978c2ecf20Sopenharmony_ci if (rc) 1988c2ecf20Sopenharmony_ci return rc; 1998c2ecf20Sopenharmony_ci 2008c2ecf20Sopenharmony_ci state = EFX_DWORD_FIELD(entry, MC_CMD_SENSOR_VALUE_ENTRY_TYPEDEF_STATE); 2018c2ecf20Sopenharmony_ci if (state == MC_CMD_SENSOR_STATE_NO_READING) 2028c2ecf20Sopenharmony_ci return -EBUSY; 2038c2ecf20Sopenharmony_ci 2048c2ecf20Sopenharmony_ci value = EFX_DWORD_FIELD(entry, MC_CMD_SENSOR_VALUE_ENTRY_TYPEDEF_VALUE); 2058c2ecf20Sopenharmony_ci 2068c2ecf20Sopenharmony_ci switch (mon_attr->hwmon_type) { 2078c2ecf20Sopenharmony_ci case EFX_HWMON_TEMP: 2088c2ecf20Sopenharmony_ci /* Convert temperature from degrees to milli-degrees Celsius */ 2098c2ecf20Sopenharmony_ci value *= 1000; 2108c2ecf20Sopenharmony_ci break; 2118c2ecf20Sopenharmony_ci case EFX_HWMON_POWER: 2128c2ecf20Sopenharmony_ci /* Convert power from watts to microwatts */ 2138c2ecf20Sopenharmony_ci value *= 1000000; 2148c2ecf20Sopenharmony_ci break; 2158c2ecf20Sopenharmony_ci default: 2168c2ecf20Sopenharmony_ci /* No conversion needed */ 2178c2ecf20Sopenharmony_ci break; 2188c2ecf20Sopenharmony_ci } 2198c2ecf20Sopenharmony_ci 2208c2ecf20Sopenharmony_ci return sprintf(buf, "%u\n", value); 2218c2ecf20Sopenharmony_ci} 2228c2ecf20Sopenharmony_ci 2238c2ecf20Sopenharmony_cistatic ssize_t efx_mcdi_mon_show_limit(struct device *dev, 2248c2ecf20Sopenharmony_ci struct device_attribute *attr, 2258c2ecf20Sopenharmony_ci char *buf) 2268c2ecf20Sopenharmony_ci{ 2278c2ecf20Sopenharmony_ci struct efx_mcdi_mon_attribute *mon_attr = 2288c2ecf20Sopenharmony_ci container_of(attr, struct efx_mcdi_mon_attribute, dev_attr); 2298c2ecf20Sopenharmony_ci unsigned int value; 2308c2ecf20Sopenharmony_ci 2318c2ecf20Sopenharmony_ci value = mon_attr->limit_value; 2328c2ecf20Sopenharmony_ci 2338c2ecf20Sopenharmony_ci switch (mon_attr->hwmon_type) { 2348c2ecf20Sopenharmony_ci case EFX_HWMON_TEMP: 2358c2ecf20Sopenharmony_ci /* Convert temperature from degrees to milli-degrees Celsius */ 2368c2ecf20Sopenharmony_ci value *= 1000; 2378c2ecf20Sopenharmony_ci break; 2388c2ecf20Sopenharmony_ci case EFX_HWMON_POWER: 2398c2ecf20Sopenharmony_ci /* Convert power from watts to microwatts */ 2408c2ecf20Sopenharmony_ci value *= 1000000; 2418c2ecf20Sopenharmony_ci break; 2428c2ecf20Sopenharmony_ci default: 2438c2ecf20Sopenharmony_ci /* No conversion needed */ 2448c2ecf20Sopenharmony_ci break; 2458c2ecf20Sopenharmony_ci } 2468c2ecf20Sopenharmony_ci 2478c2ecf20Sopenharmony_ci return sprintf(buf, "%u\n", value); 2488c2ecf20Sopenharmony_ci} 2498c2ecf20Sopenharmony_ci 2508c2ecf20Sopenharmony_cistatic ssize_t efx_mcdi_mon_show_alarm(struct device *dev, 2518c2ecf20Sopenharmony_ci struct device_attribute *attr, 2528c2ecf20Sopenharmony_ci char *buf) 2538c2ecf20Sopenharmony_ci{ 2548c2ecf20Sopenharmony_ci struct efx_mcdi_mon_attribute *mon_attr = 2558c2ecf20Sopenharmony_ci container_of(attr, struct efx_mcdi_mon_attribute, dev_attr); 2568c2ecf20Sopenharmony_ci efx_dword_t entry; 2578c2ecf20Sopenharmony_ci int state; 2588c2ecf20Sopenharmony_ci int rc; 2598c2ecf20Sopenharmony_ci 2608c2ecf20Sopenharmony_ci rc = efx_mcdi_mon_get_entry(dev, mon_attr->index, &entry); 2618c2ecf20Sopenharmony_ci if (rc) 2628c2ecf20Sopenharmony_ci return rc; 2638c2ecf20Sopenharmony_ci 2648c2ecf20Sopenharmony_ci state = EFX_DWORD_FIELD(entry, MC_CMD_SENSOR_VALUE_ENTRY_TYPEDEF_STATE); 2658c2ecf20Sopenharmony_ci return sprintf(buf, "%d\n", state != MC_CMD_SENSOR_STATE_OK); 2668c2ecf20Sopenharmony_ci} 2678c2ecf20Sopenharmony_ci 2688c2ecf20Sopenharmony_cistatic ssize_t efx_mcdi_mon_show_label(struct device *dev, 2698c2ecf20Sopenharmony_ci struct device_attribute *attr, 2708c2ecf20Sopenharmony_ci char *buf) 2718c2ecf20Sopenharmony_ci{ 2728c2ecf20Sopenharmony_ci struct efx_mcdi_mon_attribute *mon_attr = 2738c2ecf20Sopenharmony_ci container_of(attr, struct efx_mcdi_mon_attribute, dev_attr); 2748c2ecf20Sopenharmony_ci return sprintf(buf, "%s\n", 2758c2ecf20Sopenharmony_ci efx_mcdi_sensor_type[mon_attr->type].label); 2768c2ecf20Sopenharmony_ci} 2778c2ecf20Sopenharmony_ci 2788c2ecf20Sopenharmony_cistatic void 2798c2ecf20Sopenharmony_ciefx_mcdi_mon_add_attr(struct efx_nic *efx, const char *name, 2808c2ecf20Sopenharmony_ci ssize_t (*reader)(struct device *, 2818c2ecf20Sopenharmony_ci struct device_attribute *, char *), 2828c2ecf20Sopenharmony_ci unsigned int index, unsigned int type, 2838c2ecf20Sopenharmony_ci unsigned int limit_value) 2848c2ecf20Sopenharmony_ci{ 2858c2ecf20Sopenharmony_ci struct efx_mcdi_mon *hwmon = efx_mcdi_mon(efx); 2868c2ecf20Sopenharmony_ci struct efx_mcdi_mon_attribute *attr = &hwmon->attrs[hwmon->n_attrs]; 2878c2ecf20Sopenharmony_ci 2888c2ecf20Sopenharmony_ci strlcpy(attr->name, name, sizeof(attr->name)); 2898c2ecf20Sopenharmony_ci attr->index = index; 2908c2ecf20Sopenharmony_ci attr->type = type; 2918c2ecf20Sopenharmony_ci if (type < ARRAY_SIZE(efx_mcdi_sensor_type)) 2928c2ecf20Sopenharmony_ci attr->hwmon_type = efx_mcdi_sensor_type[type].hwmon_type; 2938c2ecf20Sopenharmony_ci else 2948c2ecf20Sopenharmony_ci attr->hwmon_type = EFX_HWMON_UNKNOWN; 2958c2ecf20Sopenharmony_ci attr->limit_value = limit_value; 2968c2ecf20Sopenharmony_ci sysfs_attr_init(&attr->dev_attr.attr); 2978c2ecf20Sopenharmony_ci attr->dev_attr.attr.name = attr->name; 2988c2ecf20Sopenharmony_ci attr->dev_attr.attr.mode = 0444; 2998c2ecf20Sopenharmony_ci attr->dev_attr.show = reader; 3008c2ecf20Sopenharmony_ci hwmon->group.attrs[hwmon->n_attrs++] = &attr->dev_attr.attr; 3018c2ecf20Sopenharmony_ci} 3028c2ecf20Sopenharmony_ci 3038c2ecf20Sopenharmony_ciint efx_mcdi_mon_probe(struct efx_nic *efx) 3048c2ecf20Sopenharmony_ci{ 3058c2ecf20Sopenharmony_ci unsigned int n_temp = 0, n_cool = 0, n_in = 0, n_curr = 0, n_power = 0; 3068c2ecf20Sopenharmony_ci struct efx_mcdi_mon *hwmon = efx_mcdi_mon(efx); 3078c2ecf20Sopenharmony_ci MCDI_DECLARE_BUF(inbuf, MC_CMD_SENSOR_INFO_EXT_IN_LEN); 3088c2ecf20Sopenharmony_ci MCDI_DECLARE_BUF(outbuf, MC_CMD_SENSOR_INFO_OUT_LENMAX); 3098c2ecf20Sopenharmony_ci unsigned int n_pages, n_sensors, n_attrs, page; 3108c2ecf20Sopenharmony_ci size_t outlen; 3118c2ecf20Sopenharmony_ci char name[12]; 3128c2ecf20Sopenharmony_ci u32 mask; 3138c2ecf20Sopenharmony_ci int rc, i, j, type; 3148c2ecf20Sopenharmony_ci 3158c2ecf20Sopenharmony_ci /* Find out how many sensors are present */ 3168c2ecf20Sopenharmony_ci n_sensors = 0; 3178c2ecf20Sopenharmony_ci page = 0; 3188c2ecf20Sopenharmony_ci do { 3198c2ecf20Sopenharmony_ci MCDI_SET_DWORD(inbuf, SENSOR_INFO_EXT_IN_PAGE, page); 3208c2ecf20Sopenharmony_ci 3218c2ecf20Sopenharmony_ci rc = efx_mcdi_rpc(efx, MC_CMD_SENSOR_INFO, inbuf, sizeof(inbuf), 3228c2ecf20Sopenharmony_ci outbuf, sizeof(outbuf), &outlen); 3238c2ecf20Sopenharmony_ci if (rc) 3248c2ecf20Sopenharmony_ci return rc; 3258c2ecf20Sopenharmony_ci if (outlen < MC_CMD_SENSOR_INFO_OUT_LENMIN) 3268c2ecf20Sopenharmony_ci return -EIO; 3278c2ecf20Sopenharmony_ci 3288c2ecf20Sopenharmony_ci mask = MCDI_DWORD(outbuf, SENSOR_INFO_OUT_MASK); 3298c2ecf20Sopenharmony_ci n_sensors += hweight32(mask & ~(1 << MC_CMD_SENSOR_PAGE0_NEXT)); 3308c2ecf20Sopenharmony_ci ++page; 3318c2ecf20Sopenharmony_ci } while (mask & (1 << MC_CMD_SENSOR_PAGE0_NEXT)); 3328c2ecf20Sopenharmony_ci n_pages = page; 3338c2ecf20Sopenharmony_ci 3348c2ecf20Sopenharmony_ci /* Don't create a device if there are none */ 3358c2ecf20Sopenharmony_ci if (n_sensors == 0) 3368c2ecf20Sopenharmony_ci return 0; 3378c2ecf20Sopenharmony_ci 3388c2ecf20Sopenharmony_ci rc = efx_nic_alloc_buffer( 3398c2ecf20Sopenharmony_ci efx, &hwmon->dma_buf, 3408c2ecf20Sopenharmony_ci n_sensors * MC_CMD_SENSOR_VALUE_ENTRY_TYPEDEF_LEN, 3418c2ecf20Sopenharmony_ci GFP_KERNEL); 3428c2ecf20Sopenharmony_ci if (rc) 3438c2ecf20Sopenharmony_ci return rc; 3448c2ecf20Sopenharmony_ci 3458c2ecf20Sopenharmony_ci mutex_init(&hwmon->update_lock); 3468c2ecf20Sopenharmony_ci efx_mcdi_mon_update(efx); 3478c2ecf20Sopenharmony_ci 3488c2ecf20Sopenharmony_ci /* Allocate space for the maximum possible number of 3498c2ecf20Sopenharmony_ci * attributes for this set of sensors: 3508c2ecf20Sopenharmony_ci * value, min, max, crit, alarm and label for each sensor. 3518c2ecf20Sopenharmony_ci */ 3528c2ecf20Sopenharmony_ci n_attrs = 6 * n_sensors; 3538c2ecf20Sopenharmony_ci hwmon->attrs = kcalloc(n_attrs, sizeof(*hwmon->attrs), GFP_KERNEL); 3548c2ecf20Sopenharmony_ci if (!hwmon->attrs) { 3558c2ecf20Sopenharmony_ci rc = -ENOMEM; 3568c2ecf20Sopenharmony_ci goto fail; 3578c2ecf20Sopenharmony_ci } 3588c2ecf20Sopenharmony_ci hwmon->group.attrs = kcalloc(n_attrs + 1, sizeof(struct attribute *), 3598c2ecf20Sopenharmony_ci GFP_KERNEL); 3608c2ecf20Sopenharmony_ci if (!hwmon->group.attrs) { 3618c2ecf20Sopenharmony_ci rc = -ENOMEM; 3628c2ecf20Sopenharmony_ci goto fail; 3638c2ecf20Sopenharmony_ci } 3648c2ecf20Sopenharmony_ci 3658c2ecf20Sopenharmony_ci for (i = 0, j = -1, type = -1; ; i++) { 3668c2ecf20Sopenharmony_ci enum efx_hwmon_type hwmon_type; 3678c2ecf20Sopenharmony_ci const char *hwmon_prefix; 3688c2ecf20Sopenharmony_ci unsigned hwmon_index; 3698c2ecf20Sopenharmony_ci u16 min1, max1, min2, max2; 3708c2ecf20Sopenharmony_ci 3718c2ecf20Sopenharmony_ci /* Find next sensor type or exit if there is none */ 3728c2ecf20Sopenharmony_ci do { 3738c2ecf20Sopenharmony_ci type++; 3748c2ecf20Sopenharmony_ci 3758c2ecf20Sopenharmony_ci if ((type % 32) == 0) { 3768c2ecf20Sopenharmony_ci page = type / 32; 3778c2ecf20Sopenharmony_ci j = -1; 3788c2ecf20Sopenharmony_ci if (page == n_pages) 3798c2ecf20Sopenharmony_ci goto hwmon_register; 3808c2ecf20Sopenharmony_ci 3818c2ecf20Sopenharmony_ci MCDI_SET_DWORD(inbuf, SENSOR_INFO_EXT_IN_PAGE, 3828c2ecf20Sopenharmony_ci page); 3838c2ecf20Sopenharmony_ci rc = efx_mcdi_rpc(efx, MC_CMD_SENSOR_INFO, 3848c2ecf20Sopenharmony_ci inbuf, sizeof(inbuf), 3858c2ecf20Sopenharmony_ci outbuf, sizeof(outbuf), 3868c2ecf20Sopenharmony_ci &outlen); 3878c2ecf20Sopenharmony_ci if (rc) 3888c2ecf20Sopenharmony_ci goto fail; 3898c2ecf20Sopenharmony_ci if (outlen < MC_CMD_SENSOR_INFO_OUT_LENMIN) { 3908c2ecf20Sopenharmony_ci rc = -EIO; 3918c2ecf20Sopenharmony_ci goto fail; 3928c2ecf20Sopenharmony_ci } 3938c2ecf20Sopenharmony_ci 3948c2ecf20Sopenharmony_ci mask = (MCDI_DWORD(outbuf, 3958c2ecf20Sopenharmony_ci SENSOR_INFO_OUT_MASK) & 3968c2ecf20Sopenharmony_ci ~(1 << MC_CMD_SENSOR_PAGE0_NEXT)); 3978c2ecf20Sopenharmony_ci 3988c2ecf20Sopenharmony_ci /* Check again for short response */ 3998c2ecf20Sopenharmony_ci if (outlen < 4008c2ecf20Sopenharmony_ci MC_CMD_SENSOR_INFO_OUT_LEN(hweight32(mask))) { 4018c2ecf20Sopenharmony_ci rc = -EIO; 4028c2ecf20Sopenharmony_ci goto fail; 4038c2ecf20Sopenharmony_ci } 4048c2ecf20Sopenharmony_ci } 4058c2ecf20Sopenharmony_ci } while (!(mask & (1 << type % 32))); 4068c2ecf20Sopenharmony_ci j++; 4078c2ecf20Sopenharmony_ci 4088c2ecf20Sopenharmony_ci if (type < ARRAY_SIZE(efx_mcdi_sensor_type)) { 4098c2ecf20Sopenharmony_ci hwmon_type = efx_mcdi_sensor_type[type].hwmon_type; 4108c2ecf20Sopenharmony_ci 4118c2ecf20Sopenharmony_ci /* Skip sensors specific to a different port */ 4128c2ecf20Sopenharmony_ci if (hwmon_type != EFX_HWMON_UNKNOWN && 4138c2ecf20Sopenharmony_ci efx_mcdi_sensor_type[type].port >= 0 && 4148c2ecf20Sopenharmony_ci efx_mcdi_sensor_type[type].port != 4158c2ecf20Sopenharmony_ci efx_port_num(efx)) 4168c2ecf20Sopenharmony_ci continue; 4178c2ecf20Sopenharmony_ci } else { 4188c2ecf20Sopenharmony_ci hwmon_type = EFX_HWMON_UNKNOWN; 4198c2ecf20Sopenharmony_ci } 4208c2ecf20Sopenharmony_ci 4218c2ecf20Sopenharmony_ci switch (hwmon_type) { 4228c2ecf20Sopenharmony_ci case EFX_HWMON_TEMP: 4238c2ecf20Sopenharmony_ci hwmon_prefix = "temp"; 4248c2ecf20Sopenharmony_ci hwmon_index = ++n_temp; /* 1-based */ 4258c2ecf20Sopenharmony_ci break; 4268c2ecf20Sopenharmony_ci case EFX_HWMON_COOL: 4278c2ecf20Sopenharmony_ci /* This is likely to be a heatsink, but there 4288c2ecf20Sopenharmony_ci * is no convention for representing cooling 4298c2ecf20Sopenharmony_ci * devices other than fans. 4308c2ecf20Sopenharmony_ci */ 4318c2ecf20Sopenharmony_ci hwmon_prefix = "fan"; 4328c2ecf20Sopenharmony_ci hwmon_index = ++n_cool; /* 1-based */ 4338c2ecf20Sopenharmony_ci break; 4348c2ecf20Sopenharmony_ci default: 4358c2ecf20Sopenharmony_ci hwmon_prefix = "in"; 4368c2ecf20Sopenharmony_ci hwmon_index = n_in++; /* 0-based */ 4378c2ecf20Sopenharmony_ci break; 4388c2ecf20Sopenharmony_ci case EFX_HWMON_CURR: 4398c2ecf20Sopenharmony_ci hwmon_prefix = "curr"; 4408c2ecf20Sopenharmony_ci hwmon_index = ++n_curr; /* 1-based */ 4418c2ecf20Sopenharmony_ci break; 4428c2ecf20Sopenharmony_ci case EFX_HWMON_POWER: 4438c2ecf20Sopenharmony_ci hwmon_prefix = "power"; 4448c2ecf20Sopenharmony_ci hwmon_index = ++n_power; /* 1-based */ 4458c2ecf20Sopenharmony_ci break; 4468c2ecf20Sopenharmony_ci } 4478c2ecf20Sopenharmony_ci 4488c2ecf20Sopenharmony_ci min1 = MCDI_ARRAY_FIELD(outbuf, SENSOR_ENTRY, 4498c2ecf20Sopenharmony_ci SENSOR_INFO_ENTRY, j, MIN1); 4508c2ecf20Sopenharmony_ci max1 = MCDI_ARRAY_FIELD(outbuf, SENSOR_ENTRY, 4518c2ecf20Sopenharmony_ci SENSOR_INFO_ENTRY, j, MAX1); 4528c2ecf20Sopenharmony_ci min2 = MCDI_ARRAY_FIELD(outbuf, SENSOR_ENTRY, 4538c2ecf20Sopenharmony_ci SENSOR_INFO_ENTRY, j, MIN2); 4548c2ecf20Sopenharmony_ci max2 = MCDI_ARRAY_FIELD(outbuf, SENSOR_ENTRY, 4558c2ecf20Sopenharmony_ci SENSOR_INFO_ENTRY, j, MAX2); 4568c2ecf20Sopenharmony_ci 4578c2ecf20Sopenharmony_ci if (min1 != max1) { 4588c2ecf20Sopenharmony_ci snprintf(name, sizeof(name), "%s%u_input", 4598c2ecf20Sopenharmony_ci hwmon_prefix, hwmon_index); 4608c2ecf20Sopenharmony_ci efx_mcdi_mon_add_attr( 4618c2ecf20Sopenharmony_ci efx, name, efx_mcdi_mon_show_value, i, type, 0); 4628c2ecf20Sopenharmony_ci 4638c2ecf20Sopenharmony_ci if (hwmon_type != EFX_HWMON_POWER) { 4648c2ecf20Sopenharmony_ci snprintf(name, sizeof(name), "%s%u_min", 4658c2ecf20Sopenharmony_ci hwmon_prefix, hwmon_index); 4668c2ecf20Sopenharmony_ci efx_mcdi_mon_add_attr( 4678c2ecf20Sopenharmony_ci efx, name, efx_mcdi_mon_show_limit, 4688c2ecf20Sopenharmony_ci i, type, min1); 4698c2ecf20Sopenharmony_ci } 4708c2ecf20Sopenharmony_ci 4718c2ecf20Sopenharmony_ci snprintf(name, sizeof(name), "%s%u_max", 4728c2ecf20Sopenharmony_ci hwmon_prefix, hwmon_index); 4738c2ecf20Sopenharmony_ci efx_mcdi_mon_add_attr( 4748c2ecf20Sopenharmony_ci efx, name, efx_mcdi_mon_show_limit, 4758c2ecf20Sopenharmony_ci i, type, max1); 4768c2ecf20Sopenharmony_ci 4778c2ecf20Sopenharmony_ci if (min2 != max2) { 4788c2ecf20Sopenharmony_ci /* Assume max2 is critical value. 4798c2ecf20Sopenharmony_ci * But we have no good way to expose min2. 4808c2ecf20Sopenharmony_ci */ 4818c2ecf20Sopenharmony_ci snprintf(name, sizeof(name), "%s%u_crit", 4828c2ecf20Sopenharmony_ci hwmon_prefix, hwmon_index); 4838c2ecf20Sopenharmony_ci efx_mcdi_mon_add_attr( 4848c2ecf20Sopenharmony_ci efx, name, efx_mcdi_mon_show_limit, 4858c2ecf20Sopenharmony_ci i, type, max2); 4868c2ecf20Sopenharmony_ci } 4878c2ecf20Sopenharmony_ci } 4888c2ecf20Sopenharmony_ci 4898c2ecf20Sopenharmony_ci snprintf(name, sizeof(name), "%s%u_alarm", 4908c2ecf20Sopenharmony_ci hwmon_prefix, hwmon_index); 4918c2ecf20Sopenharmony_ci efx_mcdi_mon_add_attr( 4928c2ecf20Sopenharmony_ci efx, name, efx_mcdi_mon_show_alarm, i, type, 0); 4938c2ecf20Sopenharmony_ci 4948c2ecf20Sopenharmony_ci if (type < ARRAY_SIZE(efx_mcdi_sensor_type) && 4958c2ecf20Sopenharmony_ci efx_mcdi_sensor_type[type].label) { 4968c2ecf20Sopenharmony_ci snprintf(name, sizeof(name), "%s%u_label", 4978c2ecf20Sopenharmony_ci hwmon_prefix, hwmon_index); 4988c2ecf20Sopenharmony_ci efx_mcdi_mon_add_attr( 4998c2ecf20Sopenharmony_ci efx, name, efx_mcdi_mon_show_label, i, type, 0); 5008c2ecf20Sopenharmony_ci } 5018c2ecf20Sopenharmony_ci } 5028c2ecf20Sopenharmony_ci 5038c2ecf20Sopenharmony_cihwmon_register: 5048c2ecf20Sopenharmony_ci hwmon->groups[0] = &hwmon->group; 5058c2ecf20Sopenharmony_ci hwmon->device = hwmon_device_register_with_groups(&efx->pci_dev->dev, 5068c2ecf20Sopenharmony_ci KBUILD_MODNAME, NULL, 5078c2ecf20Sopenharmony_ci hwmon->groups); 5088c2ecf20Sopenharmony_ci if (IS_ERR(hwmon->device)) { 5098c2ecf20Sopenharmony_ci rc = PTR_ERR(hwmon->device); 5108c2ecf20Sopenharmony_ci goto fail; 5118c2ecf20Sopenharmony_ci } 5128c2ecf20Sopenharmony_ci 5138c2ecf20Sopenharmony_ci return 0; 5148c2ecf20Sopenharmony_ci 5158c2ecf20Sopenharmony_cifail: 5168c2ecf20Sopenharmony_ci efx_mcdi_mon_remove(efx); 5178c2ecf20Sopenharmony_ci return rc; 5188c2ecf20Sopenharmony_ci} 5198c2ecf20Sopenharmony_ci 5208c2ecf20Sopenharmony_civoid efx_mcdi_mon_remove(struct efx_nic *efx) 5218c2ecf20Sopenharmony_ci{ 5228c2ecf20Sopenharmony_ci struct efx_mcdi_mon *hwmon = efx_mcdi_mon(efx); 5238c2ecf20Sopenharmony_ci 5248c2ecf20Sopenharmony_ci if (hwmon->device) 5258c2ecf20Sopenharmony_ci hwmon_device_unregister(hwmon->device); 5268c2ecf20Sopenharmony_ci kfree(hwmon->attrs); 5278c2ecf20Sopenharmony_ci kfree(hwmon->group.attrs); 5288c2ecf20Sopenharmony_ci efx_nic_free_buffer(efx, &hwmon->dma_buf); 5298c2ecf20Sopenharmony_ci} 5308c2ecf20Sopenharmony_ci 5318c2ecf20Sopenharmony_ci#endif /* CONFIG_SFC_MCDI_MON */ 532