Ben Hutchings | 55c5e0f8 | 2012-01-06 20:25:39 +0000 | [diff] [blame] | 1 | /**************************************************************************** |
Ben Hutchings | f7a6d2c | 2013-08-29 23:32:48 +0100 | [diff] [blame] | 2 | * Driver for Solarflare network controllers and boards |
| 3 | * Copyright 2011-2013 Solarflare Communications Inc. |
Ben Hutchings | 55c5e0f8 | 2012-01-06 20:25:39 +0000 | [diff] [blame] | 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License version 2 as published |
| 7 | * by the Free Software Foundation, incorporated herein by reference. |
| 8 | */ |
| 9 | |
| 10 | #include <linux/bitops.h> |
| 11 | #include <linux/slab.h> |
| 12 | #include <linux/hwmon.h> |
| 13 | #include <linux/stat.h> |
| 14 | |
| 15 | #include "net_driver.h" |
| 16 | #include "mcdi.h" |
| 17 | #include "mcdi_pcol.h" |
| 18 | #include "nic.h" |
| 19 | |
| 20 | enum efx_hwmon_type { |
| 21 | EFX_HWMON_UNKNOWN, |
| 22 | EFX_HWMON_TEMP, /* temperature */ |
| 23 | EFX_HWMON_COOL, /* cooling device, probably a heatsink */ |
Ben Hutchings | 38589cd | 2012-11-28 23:11:18 +0000 | [diff] [blame] | 24 | EFX_HWMON_IN, /* voltage */ |
| 25 | EFX_HWMON_CURR, /* current */ |
| 26 | EFX_HWMON_POWER, /* power */ |
Edward Cree | 2b216ce | 2013-09-30 10:52:49 +0100 | [diff] [blame] | 27 | EFX_HWMON_TYPES_COUNT |
| 28 | }; |
| 29 | |
| 30 | static const char *const efx_hwmon_unit[EFX_HWMON_TYPES_COUNT] = { |
| 31 | [EFX_HWMON_TEMP] = " degC", |
| 32 | [EFX_HWMON_COOL] = " rpm", /* though nonsense for a heatsink */ |
| 33 | [EFX_HWMON_IN] = " mV", |
| 34 | [EFX_HWMON_CURR] = " mA", |
| 35 | [EFX_HWMON_POWER] = " W", |
Ben Hutchings | 55c5e0f8 | 2012-01-06 20:25:39 +0000 | [diff] [blame] | 36 | }; |
| 37 | |
| 38 | static const struct { |
| 39 | const char *label; |
| 40 | enum efx_hwmon_type hwmon_type; |
| 41 | int port; |
Ben Hutchings | d4fbdcf | 2013-08-08 11:14:20 +0100 | [diff] [blame] | 42 | } efx_mcdi_sensor_type[] = { |
Ben Hutchings | 38589cd | 2012-11-28 23:11:18 +0000 | [diff] [blame] | 43 | #define SENSOR(name, label, hwmon_type, port) \ |
| 44 | [MC_CMD_SENSOR_##name] = { label, EFX_HWMON_ ## hwmon_type, port } |
Edward Cree | 0cf7a45 | 2013-10-03 19:06:18 +0100 | [diff] [blame] | 45 | SENSOR(CONTROLLER_TEMP, "Controller board temp.", TEMP, -1), |
Ben Hutchings | 38589cd | 2012-11-28 23:11:18 +0000 | [diff] [blame] | 46 | SENSOR(PHY_COMMON_TEMP, "PHY temp.", TEMP, -1), |
Edward Cree | 0cf7a45 | 2013-10-03 19:06:18 +0100 | [diff] [blame] | 47 | SENSOR(CONTROLLER_COOLING, "Controller heat sink", COOL, -1), |
Ben Hutchings | 38589cd | 2012-11-28 23:11:18 +0000 | [diff] [blame] | 48 | SENSOR(PHY0_TEMP, "PHY temp.", TEMP, 0), |
Edward Cree | 0cf7a45 | 2013-10-03 19:06:18 +0100 | [diff] [blame] | 49 | SENSOR(PHY0_COOLING, "PHY heat sink", COOL, 0), |
Ben Hutchings | 38589cd | 2012-11-28 23:11:18 +0000 | [diff] [blame] | 50 | SENSOR(PHY1_TEMP, "PHY temp.", TEMP, 1), |
Edward Cree | 0cf7a45 | 2013-10-03 19:06:18 +0100 | [diff] [blame] | 51 | SENSOR(PHY1_COOLING, "PHY heat sink", COOL, 1), |
Ben Hutchings | 38589cd | 2012-11-28 23:11:18 +0000 | [diff] [blame] | 52 | SENSOR(IN_1V0, "1.0V supply", IN, -1), |
| 53 | SENSOR(IN_1V2, "1.2V supply", IN, -1), |
| 54 | SENSOR(IN_1V8, "1.8V supply", IN, -1), |
| 55 | SENSOR(IN_2V5, "2.5V supply", IN, -1), |
| 56 | SENSOR(IN_3V3, "3.3V supply", IN, -1), |
| 57 | SENSOR(IN_12V0, "12.0V supply", IN, -1), |
| 58 | SENSOR(IN_1V2A, "1.2V analogue supply", IN, -1), |
Edward Cree | 0cf7a45 | 2013-10-03 19:06:18 +0100 | [diff] [blame] | 59 | SENSOR(IN_VREF, "Ref. voltage", IN, -1), |
| 60 | SENSOR(OUT_VAOE, "AOE FPGA supply", IN, -1), |
| 61 | SENSOR(AOE_TEMP, "AOE FPGA temp.", TEMP, -1), |
| 62 | SENSOR(PSU_AOE_TEMP, "AOE regulator temp.", TEMP, -1), |
| 63 | SENSOR(PSU_TEMP, "Controller regulator temp.", |
| 64 | TEMP, -1), |
| 65 | SENSOR(FAN_0, "Fan 0", COOL, -1), |
| 66 | SENSOR(FAN_1, "Fan 1", COOL, -1), |
| 67 | SENSOR(FAN_2, "Fan 2", COOL, -1), |
| 68 | SENSOR(FAN_3, "Fan 3", COOL, -1), |
| 69 | SENSOR(FAN_4, "Fan 4", COOL, -1), |
Ben Hutchings | 38589cd | 2012-11-28 23:11:18 +0000 | [diff] [blame] | 70 | SENSOR(IN_VAOE, "AOE input supply", IN, -1), |
| 71 | SENSOR(OUT_IAOE, "AOE output current", CURR, -1), |
| 72 | SENSOR(IN_IAOE, "AOE input current", CURR, -1), |
| 73 | SENSOR(NIC_POWER, "Board power use", POWER, -1), |
| 74 | SENSOR(IN_0V9, "0.9V supply", IN, -1), |
Edward Cree | 0cf7a45 | 2013-10-03 19:06:18 +0100 | [diff] [blame] | 75 | SENSOR(IN_I0V9, "0.9V supply current", CURR, -1), |
| 76 | SENSOR(IN_I1V2, "1.2V supply current", CURR, -1), |
| 77 | SENSOR(IN_0V9_ADC, "0.9V supply (ext. ADC)", IN, -1), |
| 78 | SENSOR(CONTROLLER_2_TEMP, "Controller board temp. 2", TEMP, -1), |
| 79 | SENSOR(VREG_INTERNAL_TEMP, "Regulator die temp.", TEMP, -1), |
Ben Hutchings | 38589cd | 2012-11-28 23:11:18 +0000 | [diff] [blame] | 80 | SENSOR(VREG_0V9_TEMP, "0.9V regulator temp.", TEMP, -1), |
| 81 | SENSOR(VREG_1V2_TEMP, "1.2V regulator temp.", TEMP, -1), |
Edward Cree | 0cf7a45 | 2013-10-03 19:06:18 +0100 | [diff] [blame] | 82 | SENSOR(CONTROLLER_VPTAT, |
| 83 | "Controller PTAT voltage (int. ADC)", IN, -1), |
| 84 | SENSOR(CONTROLLER_INTERNAL_TEMP, |
| 85 | "Controller die temp. (int. ADC)", TEMP, -1), |
Ben Hutchings | 38589cd | 2012-11-28 23:11:18 +0000 | [diff] [blame] | 86 | SENSOR(CONTROLLER_VPTAT_EXTADC, |
Edward Cree | 0cf7a45 | 2013-10-03 19:06:18 +0100 | [diff] [blame] | 87 | "Controller PTAT voltage (ext. ADC)", IN, -1), |
Ben Hutchings | 38589cd | 2012-11-28 23:11:18 +0000 | [diff] [blame] | 88 | SENSOR(CONTROLLER_INTERNAL_TEMP_EXTADC, |
Edward Cree | 0cf7a45 | 2013-10-03 19:06:18 +0100 | [diff] [blame] | 89 | "Controller die temp. (ext. ADC)", TEMP, -1), |
Ben Hutchings | 38589cd | 2012-11-28 23:11:18 +0000 | [diff] [blame] | 90 | SENSOR(AMBIENT_TEMP, "Ambient temp.", TEMP, -1), |
| 91 | SENSOR(AIRFLOW, "Air flow raw", IN, -1), |
Ben Hutchings | 8d13a37 | 2013-12-04 20:17:28 +0000 | [diff] [blame] | 92 | SENSOR(VDD08D_VSS08D_CSR, "0.9V die (int. ADC)", IN, -1), |
| 93 | SENSOR(VDD08D_VSS08D_CSR_EXTADC, "0.9V die (ext. ADC)", IN, -1), |
| 94 | SENSOR(HOTPOINT_TEMP, "Controller board temp. (hotpoint)", TEMP, -1), |
Ben Hutchings | 55c5e0f8 | 2012-01-06 20:25:39 +0000 | [diff] [blame] | 95 | #undef SENSOR |
| 96 | }; |
| 97 | |
| 98 | static const char *const sensor_status_names[] = { |
| 99 | [MC_CMD_SENSOR_STATE_OK] = "OK", |
| 100 | [MC_CMD_SENSOR_STATE_WARNING] = "Warning", |
| 101 | [MC_CMD_SENSOR_STATE_FATAL] = "Fatal", |
| 102 | [MC_CMD_SENSOR_STATE_BROKEN] = "Device failure", |
Alexandre Rames | 8c4e720 | 2013-07-03 09:47:34 +0100 | [diff] [blame] | 103 | [MC_CMD_SENSOR_STATE_NO_READING] = "No reading", |
Ben Hutchings | 55c5e0f8 | 2012-01-06 20:25:39 +0000 | [diff] [blame] | 104 | }; |
| 105 | |
| 106 | void efx_mcdi_sensor_event(struct efx_nic *efx, efx_qword_t *ev) |
| 107 | { |
| 108 | unsigned int type, state, value; |
Edward Cree | 2b216ce | 2013-09-30 10:52:49 +0100 | [diff] [blame] | 109 | enum efx_hwmon_type hwmon_type = EFX_HWMON_UNKNOWN; |
| 110 | const char *name = NULL, *state_txt, *unit; |
Ben Hutchings | 55c5e0f8 | 2012-01-06 20:25:39 +0000 | [diff] [blame] | 111 | |
| 112 | type = EFX_QWORD_FIELD(*ev, MCDI_EVENT_SENSOREVT_MONITOR); |
| 113 | state = EFX_QWORD_FIELD(*ev, MCDI_EVENT_SENSOREVT_STATE); |
| 114 | value = EFX_QWORD_FIELD(*ev, MCDI_EVENT_SENSOREVT_VALUE); |
| 115 | |
| 116 | /* Deal gracefully with the board having more drivers than we |
| 117 | * know about, but do not expect new sensor states. */ |
Edward Cree | 2b216ce | 2013-09-30 10:52:49 +0100 | [diff] [blame] | 118 | if (type < ARRAY_SIZE(efx_mcdi_sensor_type)) { |
Ben Hutchings | 55c5e0f8 | 2012-01-06 20:25:39 +0000 | [diff] [blame] | 119 | name = efx_mcdi_sensor_type[type].label; |
Edward Cree | 2b216ce | 2013-09-30 10:52:49 +0100 | [diff] [blame] | 120 | hwmon_type = efx_mcdi_sensor_type[type].hwmon_type; |
| 121 | } |
Ben Hutchings | 55c5e0f8 | 2012-01-06 20:25:39 +0000 | [diff] [blame] | 122 | if (!name) |
| 123 | name = "No sensor name available"; |
| 124 | EFX_BUG_ON_PARANOID(state >= ARRAY_SIZE(sensor_status_names)); |
| 125 | state_txt = sensor_status_names[state]; |
Edward Cree | 2b216ce | 2013-09-30 10:52:49 +0100 | [diff] [blame] | 126 | EFX_BUG_ON_PARANOID(hwmon_type >= EFX_HWMON_TYPES_COUNT); |
| 127 | unit = efx_hwmon_unit[hwmon_type]; |
| 128 | if (!unit) |
| 129 | unit = ""; |
Ben Hutchings | 55c5e0f8 | 2012-01-06 20:25:39 +0000 | [diff] [blame] | 130 | |
| 131 | netif_err(efx, hw, efx->net_dev, |
Edward Cree | 2b216ce | 2013-09-30 10:52:49 +0100 | [diff] [blame] | 132 | "Sensor %d (%s) reports condition '%s' for value %d%s\n", |
| 133 | type, name, state_txt, value, unit); |
Ben Hutchings | 55c5e0f8 | 2012-01-06 20:25:39 +0000 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | #ifdef CONFIG_SFC_MCDI_MON |
| 137 | |
| 138 | struct efx_mcdi_mon_attribute { |
| 139 | struct device_attribute dev_attr; |
| 140 | unsigned int index; |
| 141 | unsigned int type; |
Ben Hutchings | d4fbdcf | 2013-08-08 11:14:20 +0100 | [diff] [blame] | 142 | enum efx_hwmon_type hwmon_type; |
Ben Hutchings | 55c5e0f8 | 2012-01-06 20:25:39 +0000 | [diff] [blame] | 143 | unsigned int limit_value; |
| 144 | char name[12]; |
| 145 | }; |
| 146 | |
| 147 | static int efx_mcdi_mon_update(struct efx_nic *efx) |
| 148 | { |
| 149 | struct efx_mcdi_mon *hwmon = efx_mcdi_mon(efx); |
Ben Hutchings | d4fbdcf | 2013-08-08 11:14:20 +0100 | [diff] [blame] | 150 | MCDI_DECLARE_BUF(inbuf, MC_CMD_READ_SENSORS_EXT_IN_LEN); |
Ben Hutchings | 55c5e0f8 | 2012-01-06 20:25:39 +0000 | [diff] [blame] | 151 | int rc; |
| 152 | |
Ben Hutchings | d4fbdcf | 2013-08-08 11:14:20 +0100 | [diff] [blame] | 153 | MCDI_SET_QWORD(inbuf, READ_SENSORS_EXT_IN_DMA_ADDR, |
Ben Hutchings | 338f74d | 2012-10-10 23:20:17 +0100 | [diff] [blame] | 154 | hwmon->dma_buf.dma_addr); |
Ben Hutchings | d4fbdcf | 2013-08-08 11:14:20 +0100 | [diff] [blame] | 155 | MCDI_SET_DWORD(inbuf, READ_SENSORS_EXT_IN_LENGTH, hwmon->dma_buf.len); |
Ben Hutchings | 55c5e0f8 | 2012-01-06 20:25:39 +0000 | [diff] [blame] | 156 | |
| 157 | rc = efx_mcdi_rpc(efx, MC_CMD_READ_SENSORS, |
| 158 | inbuf, sizeof(inbuf), NULL, 0, NULL); |
| 159 | if (rc == 0) |
| 160 | hwmon->last_update = jiffies; |
| 161 | return rc; |
| 162 | } |
| 163 | |
Ben Hutchings | 55c5e0f8 | 2012-01-06 20:25:39 +0000 | [diff] [blame] | 164 | static int efx_mcdi_mon_get_entry(struct device *dev, unsigned int index, |
| 165 | efx_dword_t *entry) |
| 166 | { |
Guenter Roeck | 85493e6 | 2013-11-27 18:54:31 -0800 | [diff] [blame] | 167 | struct efx_nic *efx = dev_get_drvdata(dev->parent); |
Ben Hutchings | 55c5e0f8 | 2012-01-06 20:25:39 +0000 | [diff] [blame] | 168 | struct efx_mcdi_mon *hwmon = efx_mcdi_mon(efx); |
| 169 | int rc; |
| 170 | |
| 171 | BUILD_BUG_ON(MC_CMD_READ_SENSORS_OUT_LEN != 0); |
| 172 | |
| 173 | mutex_lock(&hwmon->update_lock); |
| 174 | |
| 175 | /* Use cached value if last update was < 1 s ago */ |
| 176 | if (time_before(jiffies, hwmon->last_update + HZ)) |
| 177 | rc = 0; |
| 178 | else |
| 179 | rc = efx_mcdi_mon_update(efx); |
| 180 | |
| 181 | /* Copy out the requested entry */ |
| 182 | *entry = ((efx_dword_t *)hwmon->dma_buf.addr)[index]; |
| 183 | |
| 184 | mutex_unlock(&hwmon->update_lock); |
| 185 | |
| 186 | return rc; |
| 187 | } |
| 188 | |
| 189 | static ssize_t efx_mcdi_mon_show_value(struct device *dev, |
| 190 | struct device_attribute *attr, |
| 191 | char *buf) |
| 192 | { |
| 193 | struct efx_mcdi_mon_attribute *mon_attr = |
| 194 | container_of(attr, struct efx_mcdi_mon_attribute, dev_attr); |
| 195 | efx_dword_t entry; |
Alexandre Rames | 8c4e720 | 2013-07-03 09:47:34 +0100 | [diff] [blame] | 196 | unsigned int value, state; |
Ben Hutchings | 55c5e0f8 | 2012-01-06 20:25:39 +0000 | [diff] [blame] | 197 | int rc; |
| 198 | |
| 199 | rc = efx_mcdi_mon_get_entry(dev, mon_attr->index, &entry); |
| 200 | if (rc) |
| 201 | return rc; |
| 202 | |
Alexandre Rames | 8c4e720 | 2013-07-03 09:47:34 +0100 | [diff] [blame] | 203 | state = EFX_DWORD_FIELD(entry, MC_CMD_SENSOR_VALUE_ENTRY_TYPEDEF_STATE); |
| 204 | if (state == MC_CMD_SENSOR_STATE_NO_READING) |
| 205 | return -EBUSY; |
| 206 | |
Ben Hutchings | 55c5e0f8 | 2012-01-06 20:25:39 +0000 | [diff] [blame] | 207 | value = EFX_DWORD_FIELD(entry, MC_CMD_SENSOR_VALUE_ENTRY_TYPEDEF_VALUE); |
| 208 | |
Ben Hutchings | 38589cd | 2012-11-28 23:11:18 +0000 | [diff] [blame] | 209 | switch (mon_attr->hwmon_type) { |
| 210 | case EFX_HWMON_TEMP: |
| 211 | /* Convert temperature from degrees to milli-degrees Celsius */ |
Ben Hutchings | 55c5e0f8 | 2012-01-06 20:25:39 +0000 | [diff] [blame] | 212 | value *= 1000; |
Ben Hutchings | 38589cd | 2012-11-28 23:11:18 +0000 | [diff] [blame] | 213 | break; |
| 214 | case EFX_HWMON_POWER: |
| 215 | /* Convert power from watts to microwatts */ |
| 216 | value *= 1000000; |
| 217 | break; |
| 218 | default: |
| 219 | /* No conversion needed */ |
| 220 | break; |
| 221 | } |
Ben Hutchings | 55c5e0f8 | 2012-01-06 20:25:39 +0000 | [diff] [blame] | 222 | |
| 223 | return sprintf(buf, "%u\n", value); |
| 224 | } |
| 225 | |
| 226 | static ssize_t efx_mcdi_mon_show_limit(struct device *dev, |
| 227 | struct device_attribute *attr, |
| 228 | char *buf) |
| 229 | { |
| 230 | struct efx_mcdi_mon_attribute *mon_attr = |
| 231 | container_of(attr, struct efx_mcdi_mon_attribute, dev_attr); |
| 232 | unsigned int value; |
| 233 | |
| 234 | value = mon_attr->limit_value; |
| 235 | |
Ben Hutchings | 38589cd | 2012-11-28 23:11:18 +0000 | [diff] [blame] | 236 | switch (mon_attr->hwmon_type) { |
| 237 | case EFX_HWMON_TEMP: |
| 238 | /* Convert temperature from degrees to milli-degrees Celsius */ |
Ben Hutchings | 55c5e0f8 | 2012-01-06 20:25:39 +0000 | [diff] [blame] | 239 | value *= 1000; |
Ben Hutchings | 38589cd | 2012-11-28 23:11:18 +0000 | [diff] [blame] | 240 | break; |
| 241 | case EFX_HWMON_POWER: |
| 242 | /* Convert power from watts to microwatts */ |
| 243 | value *= 1000000; |
| 244 | break; |
| 245 | default: |
| 246 | /* No conversion needed */ |
| 247 | break; |
| 248 | } |
Ben Hutchings | 55c5e0f8 | 2012-01-06 20:25:39 +0000 | [diff] [blame] | 249 | |
| 250 | return sprintf(buf, "%u\n", value); |
| 251 | } |
| 252 | |
| 253 | static ssize_t efx_mcdi_mon_show_alarm(struct device *dev, |
| 254 | struct device_attribute *attr, |
| 255 | char *buf) |
| 256 | { |
| 257 | struct efx_mcdi_mon_attribute *mon_attr = |
| 258 | container_of(attr, struct efx_mcdi_mon_attribute, dev_attr); |
| 259 | efx_dword_t entry; |
| 260 | int state; |
| 261 | int rc; |
| 262 | |
| 263 | rc = efx_mcdi_mon_get_entry(dev, mon_attr->index, &entry); |
| 264 | if (rc) |
| 265 | return rc; |
| 266 | |
| 267 | state = EFX_DWORD_FIELD(entry, MC_CMD_SENSOR_VALUE_ENTRY_TYPEDEF_STATE); |
| 268 | return sprintf(buf, "%d\n", state != MC_CMD_SENSOR_STATE_OK); |
| 269 | } |
| 270 | |
| 271 | static ssize_t efx_mcdi_mon_show_label(struct device *dev, |
| 272 | struct device_attribute *attr, |
| 273 | char *buf) |
| 274 | { |
| 275 | struct efx_mcdi_mon_attribute *mon_attr = |
| 276 | container_of(attr, struct efx_mcdi_mon_attribute, dev_attr); |
| 277 | return sprintf(buf, "%s\n", |
| 278 | efx_mcdi_sensor_type[mon_attr->type].label); |
| 279 | } |
| 280 | |
Guenter Roeck | 85493e6 | 2013-11-27 18:54:31 -0800 | [diff] [blame] | 281 | static void |
Ben Hutchings | 55c5e0f8 | 2012-01-06 20:25:39 +0000 | [diff] [blame] | 282 | efx_mcdi_mon_add_attr(struct efx_nic *efx, const char *name, |
| 283 | ssize_t (*reader)(struct device *, |
| 284 | struct device_attribute *, char *), |
| 285 | unsigned int index, unsigned int type, |
| 286 | unsigned int limit_value) |
| 287 | { |
| 288 | struct efx_mcdi_mon *hwmon = efx_mcdi_mon(efx); |
| 289 | struct efx_mcdi_mon_attribute *attr = &hwmon->attrs[hwmon->n_attrs]; |
Ben Hutchings | 55c5e0f8 | 2012-01-06 20:25:39 +0000 | [diff] [blame] | 290 | |
| 291 | strlcpy(attr->name, name, sizeof(attr->name)); |
| 292 | attr->index = index; |
| 293 | attr->type = type; |
Ben Hutchings | d4fbdcf | 2013-08-08 11:14:20 +0100 | [diff] [blame] | 294 | if (type < ARRAY_SIZE(efx_mcdi_sensor_type)) |
| 295 | attr->hwmon_type = efx_mcdi_sensor_type[type].hwmon_type; |
| 296 | else |
| 297 | attr->hwmon_type = EFX_HWMON_UNKNOWN; |
Ben Hutchings | 55c5e0f8 | 2012-01-06 20:25:39 +0000 | [diff] [blame] | 298 | attr->limit_value = limit_value; |
Michal Schmidt | a9ec6bd | 2012-07-19 07:04:45 +0000 | [diff] [blame] | 299 | sysfs_attr_init(&attr->dev_attr.attr); |
Ben Hutchings | 55c5e0f8 | 2012-01-06 20:25:39 +0000 | [diff] [blame] | 300 | attr->dev_attr.attr.name = attr->name; |
| 301 | attr->dev_attr.attr.mode = S_IRUGO; |
| 302 | attr->dev_attr.show = reader; |
Guenter Roeck | 85493e6 | 2013-11-27 18:54:31 -0800 | [diff] [blame] | 303 | hwmon->group.attrs[hwmon->n_attrs++] = &attr->dev_attr.attr; |
Ben Hutchings | 55c5e0f8 | 2012-01-06 20:25:39 +0000 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | int efx_mcdi_mon_probe(struct efx_nic *efx) |
| 307 | { |
Ben Hutchings | 38589cd | 2012-11-28 23:11:18 +0000 | [diff] [blame] | 308 | unsigned int n_temp = 0, n_cool = 0, n_in = 0, n_curr = 0, n_power = 0; |
Ben Hutchings | 55c5e0f8 | 2012-01-06 20:25:39 +0000 | [diff] [blame] | 309 | struct efx_mcdi_mon *hwmon = efx_mcdi_mon(efx); |
Ben Hutchings | d4fbdcf | 2013-08-08 11:14:20 +0100 | [diff] [blame] | 310 | MCDI_DECLARE_BUF(inbuf, MC_CMD_SENSOR_INFO_EXT_IN_LEN); |
Ben Hutchings | 59cfc47 | 2012-09-14 17:30:10 +0100 | [diff] [blame] | 311 | MCDI_DECLARE_BUF(outbuf, MC_CMD_SENSOR_INFO_OUT_LENMAX); |
Ben Hutchings | d4fbdcf | 2013-08-08 11:14:20 +0100 | [diff] [blame] | 312 | unsigned int n_pages, n_sensors, n_attrs, page; |
Ben Hutchings | 55c5e0f8 | 2012-01-06 20:25:39 +0000 | [diff] [blame] | 313 | size_t outlen; |
| 314 | char name[12]; |
| 315 | u32 mask; |
Ben Hutchings | d4fbdcf | 2013-08-08 11:14:20 +0100 | [diff] [blame] | 316 | int rc, i, j, type; |
Ben Hutchings | 55c5e0f8 | 2012-01-06 20:25:39 +0000 | [diff] [blame] | 317 | |
Ben Hutchings | d4fbdcf | 2013-08-08 11:14:20 +0100 | [diff] [blame] | 318 | /* Find out how many sensors are present */ |
| 319 | n_sensors = 0; |
| 320 | page = 0; |
| 321 | do { |
| 322 | MCDI_SET_DWORD(inbuf, SENSOR_INFO_EXT_IN_PAGE, page); |
Ben Hutchings | 55c5e0f8 | 2012-01-06 20:25:39 +0000 | [diff] [blame] | 323 | |
Ben Hutchings | d4fbdcf | 2013-08-08 11:14:20 +0100 | [diff] [blame] | 324 | rc = efx_mcdi_rpc(efx, MC_CMD_SENSOR_INFO, inbuf, sizeof(inbuf), |
| 325 | outbuf, sizeof(outbuf), &outlen); |
| 326 | if (rc) |
| 327 | return rc; |
| 328 | if (outlen < MC_CMD_SENSOR_INFO_OUT_LENMIN) |
| 329 | return -EIO; |
Ben Hutchings | 55c5e0f8 | 2012-01-06 20:25:39 +0000 | [diff] [blame] | 330 | |
Ben Hutchings | d4fbdcf | 2013-08-08 11:14:20 +0100 | [diff] [blame] | 331 | mask = MCDI_DWORD(outbuf, SENSOR_INFO_OUT_MASK); |
| 332 | n_sensors += hweight32(mask & ~(1 << MC_CMD_SENSOR_PAGE0_NEXT)); |
| 333 | ++page; |
| 334 | } while (mask & (1 << MC_CMD_SENSOR_PAGE0_NEXT)); |
| 335 | n_pages = page; |
| 336 | |
| 337 | /* Don't create a device if there are none */ |
| 338 | if (n_sensors == 0) |
Ben Hutchings | 55c5e0f8 | 2012-01-06 20:25:39 +0000 | [diff] [blame] | 339 | return 0; |
| 340 | |
Ben Hutchings | d4fbdcf | 2013-08-08 11:14:20 +0100 | [diff] [blame] | 341 | rc = efx_nic_alloc_buffer( |
| 342 | efx, &hwmon->dma_buf, |
| 343 | n_sensors * MC_CMD_SENSOR_VALUE_ENTRY_TYPEDEF_LEN, |
| 344 | GFP_KERNEL); |
Ben Hutchings | 55c5e0f8 | 2012-01-06 20:25:39 +0000 | [diff] [blame] | 345 | if (rc) |
| 346 | return rc; |
| 347 | |
| 348 | mutex_init(&hwmon->update_lock); |
| 349 | efx_mcdi_mon_update(efx); |
| 350 | |
| 351 | /* Allocate space for the maximum possible number of |
Guenter Roeck | 85493e6 | 2013-11-27 18:54:31 -0800 | [diff] [blame] | 352 | * attributes for this set of sensors: |
Ben Hutchings | 55c5e0f8 | 2012-01-06 20:25:39 +0000 | [diff] [blame] | 353 | * value, min, max, crit, alarm and label for each sensor. |
| 354 | */ |
Guenter Roeck | 85493e6 | 2013-11-27 18:54:31 -0800 | [diff] [blame] | 355 | n_attrs = 6 * n_sensors; |
Ben Hutchings | 55c5e0f8 | 2012-01-06 20:25:39 +0000 | [diff] [blame] | 356 | hwmon->attrs = kcalloc(n_attrs, sizeof(*hwmon->attrs), GFP_KERNEL); |
| 357 | if (!hwmon->attrs) { |
| 358 | rc = -ENOMEM; |
| 359 | goto fail; |
| 360 | } |
Guenter Roeck | 85493e6 | 2013-11-27 18:54:31 -0800 | [diff] [blame] | 361 | hwmon->group.attrs = kcalloc(n_attrs + 1, sizeof(struct attribute *), |
| 362 | GFP_KERNEL); |
| 363 | if (!hwmon->group.attrs) { |
| 364 | rc = -ENOMEM; |
Ben Hutchings | 55c5e0f8 | 2012-01-06 20:25:39 +0000 | [diff] [blame] | 365 | goto fail; |
| 366 | } |
| 367 | |
Ben Hutchings | d4fbdcf | 2013-08-08 11:14:20 +0100 | [diff] [blame] | 368 | for (i = 0, j = -1, type = -1; ; i++) { |
| 369 | enum efx_hwmon_type hwmon_type; |
Ben Hutchings | 55c5e0f8 | 2012-01-06 20:25:39 +0000 | [diff] [blame] | 370 | const char *hwmon_prefix; |
| 371 | unsigned hwmon_index; |
| 372 | u16 min1, max1, min2, max2; |
| 373 | |
| 374 | /* Find next sensor type or exit if there is none */ |
Ben Hutchings | d4fbdcf | 2013-08-08 11:14:20 +0100 | [diff] [blame] | 375 | do { |
Ben Hutchings | 55c5e0f8 | 2012-01-06 20:25:39 +0000 | [diff] [blame] | 376 | type++; |
Ben Hutchings | d4fbdcf | 2013-08-08 11:14:20 +0100 | [diff] [blame] | 377 | |
| 378 | if ((type % 32) == 0) { |
| 379 | page = type / 32; |
| 380 | j = -1; |
| 381 | if (page == n_pages) |
Guenter Roeck | 85493e6 | 2013-11-27 18:54:31 -0800 | [diff] [blame] | 382 | goto hwmon_register; |
Ben Hutchings | d4fbdcf | 2013-08-08 11:14:20 +0100 | [diff] [blame] | 383 | |
| 384 | MCDI_SET_DWORD(inbuf, SENSOR_INFO_EXT_IN_PAGE, |
| 385 | page); |
| 386 | rc = efx_mcdi_rpc(efx, MC_CMD_SENSOR_INFO, |
| 387 | inbuf, sizeof(inbuf), |
| 388 | outbuf, sizeof(outbuf), |
| 389 | &outlen); |
| 390 | if (rc) |
| 391 | goto fail; |
| 392 | if (outlen < MC_CMD_SENSOR_INFO_OUT_LENMIN) { |
| 393 | rc = -EIO; |
| 394 | goto fail; |
| 395 | } |
| 396 | |
| 397 | mask = (MCDI_DWORD(outbuf, |
| 398 | SENSOR_INFO_OUT_MASK) & |
| 399 | ~(1 << MC_CMD_SENSOR_PAGE0_NEXT)); |
| 400 | |
| 401 | /* Check again for short response */ |
| 402 | if (outlen < |
| 403 | MC_CMD_SENSOR_INFO_OUT_LEN(hweight32(mask))) { |
| 404 | rc = -EIO; |
| 405 | goto fail; |
| 406 | } |
| 407 | } |
| 408 | } while (!(mask & (1 << type % 32))); |
| 409 | j++; |
| 410 | |
| 411 | if (type < ARRAY_SIZE(efx_mcdi_sensor_type)) { |
| 412 | hwmon_type = efx_mcdi_sensor_type[type].hwmon_type; |
| 413 | |
| 414 | /* Skip sensors specific to a different port */ |
| 415 | if (hwmon_type != EFX_HWMON_UNKNOWN && |
| 416 | efx_mcdi_sensor_type[type].port >= 0 && |
| 417 | efx_mcdi_sensor_type[type].port != |
| 418 | efx_port_num(efx)) |
| 419 | continue; |
| 420 | } else { |
| 421 | hwmon_type = EFX_HWMON_UNKNOWN; |
Ben Hutchings | 55c5e0f8 | 2012-01-06 20:25:39 +0000 | [diff] [blame] | 422 | } |
| 423 | |
Ben Hutchings | d4fbdcf | 2013-08-08 11:14:20 +0100 | [diff] [blame] | 424 | switch (hwmon_type) { |
Ben Hutchings | 55c5e0f8 | 2012-01-06 20:25:39 +0000 | [diff] [blame] | 425 | case EFX_HWMON_TEMP: |
| 426 | hwmon_prefix = "temp"; |
| 427 | hwmon_index = ++n_temp; /* 1-based */ |
| 428 | break; |
| 429 | case EFX_HWMON_COOL: |
| 430 | /* This is likely to be a heatsink, but there |
| 431 | * is no convention for representing cooling |
| 432 | * devices other than fans. |
| 433 | */ |
| 434 | hwmon_prefix = "fan"; |
| 435 | hwmon_index = ++n_cool; /* 1-based */ |
| 436 | break; |
| 437 | default: |
| 438 | hwmon_prefix = "in"; |
| 439 | hwmon_index = n_in++; /* 0-based */ |
| 440 | break; |
Ben Hutchings | 38589cd | 2012-11-28 23:11:18 +0000 | [diff] [blame] | 441 | case EFX_HWMON_CURR: |
| 442 | hwmon_prefix = "curr"; |
| 443 | hwmon_index = ++n_curr; /* 1-based */ |
| 444 | break; |
| 445 | case EFX_HWMON_POWER: |
| 446 | hwmon_prefix = "power"; |
| 447 | hwmon_index = ++n_power; /* 1-based */ |
| 448 | break; |
Ben Hutchings | 55c5e0f8 | 2012-01-06 20:25:39 +0000 | [diff] [blame] | 449 | } |
| 450 | |
| 451 | min1 = MCDI_ARRAY_FIELD(outbuf, SENSOR_ENTRY, |
Ben Hutchings | d4fbdcf | 2013-08-08 11:14:20 +0100 | [diff] [blame] | 452 | SENSOR_INFO_ENTRY, j, MIN1); |
Ben Hutchings | 55c5e0f8 | 2012-01-06 20:25:39 +0000 | [diff] [blame] | 453 | max1 = MCDI_ARRAY_FIELD(outbuf, SENSOR_ENTRY, |
Ben Hutchings | d4fbdcf | 2013-08-08 11:14:20 +0100 | [diff] [blame] | 454 | SENSOR_INFO_ENTRY, j, MAX1); |
Ben Hutchings | 55c5e0f8 | 2012-01-06 20:25:39 +0000 | [diff] [blame] | 455 | min2 = MCDI_ARRAY_FIELD(outbuf, SENSOR_ENTRY, |
Ben Hutchings | d4fbdcf | 2013-08-08 11:14:20 +0100 | [diff] [blame] | 456 | SENSOR_INFO_ENTRY, j, MIN2); |
Ben Hutchings | 55c5e0f8 | 2012-01-06 20:25:39 +0000 | [diff] [blame] | 457 | max2 = MCDI_ARRAY_FIELD(outbuf, SENSOR_ENTRY, |
Ben Hutchings | d4fbdcf | 2013-08-08 11:14:20 +0100 | [diff] [blame] | 458 | SENSOR_INFO_ENTRY, j, MAX2); |
Ben Hutchings | 55c5e0f8 | 2012-01-06 20:25:39 +0000 | [diff] [blame] | 459 | |
| 460 | if (min1 != max1) { |
| 461 | snprintf(name, sizeof(name), "%s%u_input", |
| 462 | hwmon_prefix, hwmon_index); |
Guenter Roeck | 85493e6 | 2013-11-27 18:54:31 -0800 | [diff] [blame] | 463 | efx_mcdi_mon_add_attr( |
Ben Hutchings | 55c5e0f8 | 2012-01-06 20:25:39 +0000 | [diff] [blame] | 464 | efx, name, efx_mcdi_mon_show_value, i, type, 0); |
Ben Hutchings | 55c5e0f8 | 2012-01-06 20:25:39 +0000 | [diff] [blame] | 465 | |
Ben Hutchings | 38589cd | 2012-11-28 23:11:18 +0000 | [diff] [blame] | 466 | if (hwmon_type != EFX_HWMON_POWER) { |
| 467 | snprintf(name, sizeof(name), "%s%u_min", |
| 468 | hwmon_prefix, hwmon_index); |
Guenter Roeck | 85493e6 | 2013-11-27 18:54:31 -0800 | [diff] [blame] | 469 | efx_mcdi_mon_add_attr( |
Ben Hutchings | 38589cd | 2012-11-28 23:11:18 +0000 | [diff] [blame] | 470 | efx, name, efx_mcdi_mon_show_limit, |
| 471 | i, type, min1); |
Ben Hutchings | 38589cd | 2012-11-28 23:11:18 +0000 | [diff] [blame] | 472 | } |
Ben Hutchings | 55c5e0f8 | 2012-01-06 20:25:39 +0000 | [diff] [blame] | 473 | |
| 474 | snprintf(name, sizeof(name), "%s%u_max", |
| 475 | hwmon_prefix, hwmon_index); |
Guenter Roeck | 85493e6 | 2013-11-27 18:54:31 -0800 | [diff] [blame] | 476 | efx_mcdi_mon_add_attr( |
Ben Hutchings | 55c5e0f8 | 2012-01-06 20:25:39 +0000 | [diff] [blame] | 477 | efx, name, efx_mcdi_mon_show_limit, |
| 478 | i, type, max1); |
Ben Hutchings | 55c5e0f8 | 2012-01-06 20:25:39 +0000 | [diff] [blame] | 479 | |
| 480 | if (min2 != max2) { |
| 481 | /* Assume max2 is critical value. |
| 482 | * But we have no good way to expose min2. |
| 483 | */ |
| 484 | snprintf(name, sizeof(name), "%s%u_crit", |
| 485 | hwmon_prefix, hwmon_index); |
Guenter Roeck | 85493e6 | 2013-11-27 18:54:31 -0800 | [diff] [blame] | 486 | efx_mcdi_mon_add_attr( |
Ben Hutchings | 55c5e0f8 | 2012-01-06 20:25:39 +0000 | [diff] [blame] | 487 | efx, name, efx_mcdi_mon_show_limit, |
| 488 | i, type, max2); |
Ben Hutchings | 55c5e0f8 | 2012-01-06 20:25:39 +0000 | [diff] [blame] | 489 | } |
| 490 | } |
| 491 | |
| 492 | snprintf(name, sizeof(name), "%s%u_alarm", |
| 493 | hwmon_prefix, hwmon_index); |
Guenter Roeck | 85493e6 | 2013-11-27 18:54:31 -0800 | [diff] [blame] | 494 | efx_mcdi_mon_add_attr( |
Ben Hutchings | 55c5e0f8 | 2012-01-06 20:25:39 +0000 | [diff] [blame] | 495 | efx, name, efx_mcdi_mon_show_alarm, i, type, 0); |
Ben Hutchings | 55c5e0f8 | 2012-01-06 20:25:39 +0000 | [diff] [blame] | 496 | |
Ben Hutchings | d4fbdcf | 2013-08-08 11:14:20 +0100 | [diff] [blame] | 497 | if (type < ARRAY_SIZE(efx_mcdi_sensor_type) && |
| 498 | efx_mcdi_sensor_type[type].label) { |
Ben Hutchings | 55c5e0f8 | 2012-01-06 20:25:39 +0000 | [diff] [blame] | 499 | snprintf(name, sizeof(name), "%s%u_label", |
| 500 | hwmon_prefix, hwmon_index); |
Guenter Roeck | 85493e6 | 2013-11-27 18:54:31 -0800 | [diff] [blame] | 501 | efx_mcdi_mon_add_attr( |
Ben Hutchings | 55c5e0f8 | 2012-01-06 20:25:39 +0000 | [diff] [blame] | 502 | efx, name, efx_mcdi_mon_show_label, i, type, 0); |
Ben Hutchings | 55c5e0f8 | 2012-01-06 20:25:39 +0000 | [diff] [blame] | 503 | } |
| 504 | } |
| 505 | |
Guenter Roeck | 85493e6 | 2013-11-27 18:54:31 -0800 | [diff] [blame] | 506 | hwmon_register: |
| 507 | hwmon->groups[0] = &hwmon->group; |
| 508 | hwmon->device = hwmon_device_register_with_groups(&efx->pci_dev->dev, |
| 509 | KBUILD_MODNAME, NULL, |
| 510 | hwmon->groups); |
| 511 | if (IS_ERR(hwmon->device)) { |
| 512 | rc = PTR_ERR(hwmon->device); |
| 513 | goto fail; |
| 514 | } |
| 515 | |
| 516 | return 0; |
| 517 | |
Ben Hutchings | 55c5e0f8 | 2012-01-06 20:25:39 +0000 | [diff] [blame] | 518 | fail: |
| 519 | efx_mcdi_mon_remove(efx); |
| 520 | return rc; |
| 521 | } |
| 522 | |
| 523 | void efx_mcdi_mon_remove(struct efx_nic *efx) |
| 524 | { |
Ben Hutchings | e847b53 | 2012-09-13 01:11:24 +0100 | [diff] [blame] | 525 | struct efx_mcdi_mon *hwmon = efx_mcdi_mon(efx); |
Ben Hutchings | 55c5e0f8 | 2012-01-06 20:25:39 +0000 | [diff] [blame] | 526 | |
Ben Hutchings | 55c5e0f8 | 2012-01-06 20:25:39 +0000 | [diff] [blame] | 527 | if (hwmon->device) |
| 528 | hwmon_device_unregister(hwmon->device); |
Guenter Roeck | 85493e6 | 2013-11-27 18:54:31 -0800 | [diff] [blame] | 529 | kfree(hwmon->attrs); |
| 530 | kfree(hwmon->group.attrs); |
Ben Hutchings | 55c5e0f8 | 2012-01-06 20:25:39 +0000 | [diff] [blame] | 531 | efx_nic_free_buffer(efx, &hwmon->dma_buf); |
| 532 | } |
| 533 | |
| 534 | #endif /* CONFIG_SFC_MCDI_MON */ |