1 /****************************************************************************
2 * Driver for Solarflare network controllers and boards
3 * Copyright 2011-2013 Solarflare Communications Inc.
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.
10 #include <linux/bitops.h>
11 #include <linux/slab.h>
12 #include <linux/hwmon.h>
13 #include <linux/stat.h>
15 #include "net_driver.h"
17 #include "mcdi_pcol.h"
22 EFX_HWMON_TEMP, /* temperature */
23 EFX_HWMON_COOL, /* cooling device, probably a heatsink */
24 EFX_HWMON_IN, /* voltage */
25 EFX_HWMON_CURR, /* current */
26 EFX_HWMON_POWER, /* power */
31 enum efx_hwmon_type hwmon_type;
33 } efx_mcdi_sensor_type[] = {
34 #define SENSOR(name, label, hwmon_type, port) \
35 [MC_CMD_SENSOR_##name] = { label, EFX_HWMON_ ## hwmon_type, port }
36 SENSOR(CONTROLLER_TEMP, "Controller ext. temp.", TEMP, -1),
37 SENSOR(PHY_COMMON_TEMP, "PHY temp.", TEMP, -1),
38 SENSOR(CONTROLLER_COOLING, "Controller cooling", COOL, -1),
39 SENSOR(PHY0_TEMP, "PHY temp.", TEMP, 0),
40 SENSOR(PHY0_COOLING, "PHY cooling", COOL, 0),
41 SENSOR(PHY1_TEMP, "PHY temp.", TEMP, 1),
42 SENSOR(PHY1_COOLING, "PHY cooling", COOL, 1),
43 SENSOR(IN_1V0, "1.0V supply", IN, -1),
44 SENSOR(IN_1V2, "1.2V supply", IN, -1),
45 SENSOR(IN_1V8, "1.8V supply", IN, -1),
46 SENSOR(IN_2V5, "2.5V supply", IN, -1),
47 SENSOR(IN_3V3, "3.3V supply", IN, -1),
48 SENSOR(IN_12V0, "12.0V supply", IN, -1),
49 SENSOR(IN_1V2A, "1.2V analogue supply", IN, -1),
50 SENSOR(IN_VREF, "ref. voltage", IN, -1),
51 SENSOR(OUT_VAOE, "AOE power supply", IN, -1),
52 SENSOR(AOE_TEMP, "AOE temp.", TEMP, -1),
53 SENSOR(PSU_AOE_TEMP, "AOE PSU temp.", TEMP, -1),
54 SENSOR(PSU_TEMP, "Controller PSU temp.", TEMP, -1),
55 SENSOR(FAN_0, NULL, COOL, -1),
56 SENSOR(FAN_1, NULL, COOL, -1),
57 SENSOR(FAN_2, NULL, COOL, -1),
58 SENSOR(FAN_3, NULL, COOL, -1),
59 SENSOR(FAN_4, NULL, COOL, -1),
60 SENSOR(IN_VAOE, "AOE input supply", IN, -1),
61 SENSOR(OUT_IAOE, "AOE output current", CURR, -1),
62 SENSOR(IN_IAOE, "AOE input current", CURR, -1),
63 SENSOR(NIC_POWER, "Board power use", POWER, -1),
64 SENSOR(IN_0V9, "0.9V supply", IN, -1),
65 SENSOR(IN_I0V9, "0.9V input current", CURR, -1),
66 SENSOR(IN_I1V2, "1.2V input current", CURR, -1),
67 SENSOR(IN_0V9_ADC, "0.9V supply (at ADC)", IN, -1),
68 SENSOR(CONTROLLER_2_TEMP, "Controller ext. temp. 2", TEMP, -1),
69 SENSOR(VREG_INTERNAL_TEMP, "Voltage regulator temp.", TEMP, -1),
70 SENSOR(VREG_0V9_TEMP, "0.9V regulator temp.", TEMP, -1),
71 SENSOR(VREG_1V2_TEMP, "1.2V regulator temp.", TEMP, -1),
72 SENSOR(CONTROLLER_VPTAT, "Controller int. temp. raw", IN, -1),
73 SENSOR(CONTROLLER_INTERNAL_TEMP, "Controller int. temp.", TEMP, -1),
74 SENSOR(CONTROLLER_VPTAT_EXTADC,
75 "Controller int. temp. raw (at ADC)", IN, -1),
76 SENSOR(CONTROLLER_INTERNAL_TEMP_EXTADC,
77 "Controller int. temp. (via ADC)", TEMP, -1),
78 SENSOR(AMBIENT_TEMP, "Ambient temp.", TEMP, -1),
79 SENSOR(AIRFLOW, "Air flow raw", IN, -1),
83 static const char *const sensor_status_names[] = {
84 [MC_CMD_SENSOR_STATE_OK] = "OK",
85 [MC_CMD_SENSOR_STATE_WARNING] = "Warning",
86 [MC_CMD_SENSOR_STATE_FATAL] = "Fatal",
87 [MC_CMD_SENSOR_STATE_BROKEN] = "Device failure",
88 [MC_CMD_SENSOR_STATE_NO_READING] = "No reading",
91 void efx_mcdi_sensor_event(struct efx_nic *efx, efx_qword_t *ev)
93 unsigned int type, state, value;
94 const char *name = NULL, *state_txt;
96 type = EFX_QWORD_FIELD(*ev, MCDI_EVENT_SENSOREVT_MONITOR);
97 state = EFX_QWORD_FIELD(*ev, MCDI_EVENT_SENSOREVT_STATE);
98 value = EFX_QWORD_FIELD(*ev, MCDI_EVENT_SENSOREVT_VALUE);
100 /* Deal gracefully with the board having more drivers than we
101 * know about, but do not expect new sensor states. */
102 if (type < ARRAY_SIZE(efx_mcdi_sensor_type))
103 name = efx_mcdi_sensor_type[type].label;
105 name = "No sensor name available";
106 EFX_BUG_ON_PARANOID(state >= ARRAY_SIZE(sensor_status_names));
107 state_txt = sensor_status_names[state];
109 netif_err(efx, hw, efx->net_dev,
110 "Sensor %d (%s) reports condition '%s' for raw value %d\n",
111 type, name, state_txt, value);
114 #ifdef CONFIG_SFC_MCDI_MON
116 struct efx_mcdi_mon_attribute {
117 struct device_attribute dev_attr;
120 enum efx_hwmon_type hwmon_type;
121 unsigned int limit_value;
125 static int efx_mcdi_mon_update(struct efx_nic *efx)
127 struct efx_mcdi_mon *hwmon = efx_mcdi_mon(efx);
128 MCDI_DECLARE_BUF(inbuf, MC_CMD_READ_SENSORS_EXT_IN_LEN);
131 MCDI_SET_QWORD(inbuf, READ_SENSORS_EXT_IN_DMA_ADDR,
132 hwmon->dma_buf.dma_addr);
133 MCDI_SET_DWORD(inbuf, READ_SENSORS_EXT_IN_LENGTH, hwmon->dma_buf.len);
135 rc = efx_mcdi_rpc(efx, MC_CMD_READ_SENSORS,
136 inbuf, sizeof(inbuf), NULL, 0, NULL);
138 hwmon->last_update = jiffies;
142 static ssize_t efx_mcdi_mon_show_name(struct device *dev,
143 struct device_attribute *attr,
146 return sprintf(buf, "%s\n", KBUILD_MODNAME);
149 static int efx_mcdi_mon_get_entry(struct device *dev, unsigned int index,
152 struct efx_nic *efx = dev_get_drvdata(dev);
153 struct efx_mcdi_mon *hwmon = efx_mcdi_mon(efx);
156 BUILD_BUG_ON(MC_CMD_READ_SENSORS_OUT_LEN != 0);
158 mutex_lock(&hwmon->update_lock);
160 /* Use cached value if last update was < 1 s ago */
161 if (time_before(jiffies, hwmon->last_update + HZ))
164 rc = efx_mcdi_mon_update(efx);
166 /* Copy out the requested entry */
167 *entry = ((efx_dword_t *)hwmon->dma_buf.addr)[index];
169 mutex_unlock(&hwmon->update_lock);
174 static ssize_t efx_mcdi_mon_show_value(struct device *dev,
175 struct device_attribute *attr,
178 struct efx_mcdi_mon_attribute *mon_attr =
179 container_of(attr, struct efx_mcdi_mon_attribute, dev_attr);
181 unsigned int value, state;
184 rc = efx_mcdi_mon_get_entry(dev, mon_attr->index, &entry);
188 state = EFX_DWORD_FIELD(entry, MC_CMD_SENSOR_VALUE_ENTRY_TYPEDEF_STATE);
189 if (state == MC_CMD_SENSOR_STATE_NO_READING)
192 value = EFX_DWORD_FIELD(entry, MC_CMD_SENSOR_VALUE_ENTRY_TYPEDEF_VALUE);
194 switch (mon_attr->hwmon_type) {
196 /* Convert temperature from degrees to milli-degrees Celsius */
199 case EFX_HWMON_POWER:
200 /* Convert power from watts to microwatts */
204 /* No conversion needed */
208 return sprintf(buf, "%u\n", value);
211 static ssize_t efx_mcdi_mon_show_limit(struct device *dev,
212 struct device_attribute *attr,
215 struct efx_mcdi_mon_attribute *mon_attr =
216 container_of(attr, struct efx_mcdi_mon_attribute, dev_attr);
219 value = mon_attr->limit_value;
221 switch (mon_attr->hwmon_type) {
223 /* Convert temperature from degrees to milli-degrees Celsius */
226 case EFX_HWMON_POWER:
227 /* Convert power from watts to microwatts */
231 /* No conversion needed */
235 return sprintf(buf, "%u\n", value);
238 static ssize_t efx_mcdi_mon_show_alarm(struct device *dev,
239 struct device_attribute *attr,
242 struct efx_mcdi_mon_attribute *mon_attr =
243 container_of(attr, struct efx_mcdi_mon_attribute, dev_attr);
248 rc = efx_mcdi_mon_get_entry(dev, mon_attr->index, &entry);
252 state = EFX_DWORD_FIELD(entry, MC_CMD_SENSOR_VALUE_ENTRY_TYPEDEF_STATE);
253 return sprintf(buf, "%d\n", state != MC_CMD_SENSOR_STATE_OK);
256 static ssize_t efx_mcdi_mon_show_label(struct device *dev,
257 struct device_attribute *attr,
260 struct efx_mcdi_mon_attribute *mon_attr =
261 container_of(attr, struct efx_mcdi_mon_attribute, dev_attr);
262 return sprintf(buf, "%s\n",
263 efx_mcdi_sensor_type[mon_attr->type].label);
267 efx_mcdi_mon_add_attr(struct efx_nic *efx, const char *name,
268 ssize_t (*reader)(struct device *,
269 struct device_attribute *, char *),
270 unsigned int index, unsigned int type,
271 unsigned int limit_value)
273 struct efx_mcdi_mon *hwmon = efx_mcdi_mon(efx);
274 struct efx_mcdi_mon_attribute *attr = &hwmon->attrs[hwmon->n_attrs];
277 strlcpy(attr->name, name, sizeof(attr->name));
280 if (type < ARRAY_SIZE(efx_mcdi_sensor_type))
281 attr->hwmon_type = efx_mcdi_sensor_type[type].hwmon_type;
283 attr->hwmon_type = EFX_HWMON_UNKNOWN;
284 attr->limit_value = limit_value;
285 sysfs_attr_init(&attr->dev_attr.attr);
286 attr->dev_attr.attr.name = attr->name;
287 attr->dev_attr.attr.mode = S_IRUGO;
288 attr->dev_attr.show = reader;
289 rc = device_create_file(&efx->pci_dev->dev, &attr->dev_attr);
295 int efx_mcdi_mon_probe(struct efx_nic *efx)
297 unsigned int n_temp = 0, n_cool = 0, n_in = 0, n_curr = 0, n_power = 0;
298 struct efx_mcdi_mon *hwmon = efx_mcdi_mon(efx);
299 MCDI_DECLARE_BUF(inbuf, MC_CMD_SENSOR_INFO_EXT_IN_LEN);
300 MCDI_DECLARE_BUF(outbuf, MC_CMD_SENSOR_INFO_OUT_LENMAX);
301 unsigned int n_pages, n_sensors, n_attrs, page;
307 /* Find out how many sensors are present */
311 MCDI_SET_DWORD(inbuf, SENSOR_INFO_EXT_IN_PAGE, page);
313 rc = efx_mcdi_rpc(efx, MC_CMD_SENSOR_INFO, inbuf, sizeof(inbuf),
314 outbuf, sizeof(outbuf), &outlen);
317 if (outlen < MC_CMD_SENSOR_INFO_OUT_LENMIN)
320 mask = MCDI_DWORD(outbuf, SENSOR_INFO_OUT_MASK);
321 n_sensors += hweight32(mask & ~(1 << MC_CMD_SENSOR_PAGE0_NEXT));
323 } while (mask & (1 << MC_CMD_SENSOR_PAGE0_NEXT));
326 /* Don't create a device if there are none */
330 rc = efx_nic_alloc_buffer(
331 efx, &hwmon->dma_buf,
332 n_sensors * MC_CMD_SENSOR_VALUE_ENTRY_TYPEDEF_LEN,
337 mutex_init(&hwmon->update_lock);
338 efx_mcdi_mon_update(efx);
340 /* Allocate space for the maximum possible number of
341 * attributes for this set of sensors: name of the driver plus
342 * value, min, max, crit, alarm and label for each sensor.
344 n_attrs = 1 + 6 * n_sensors;
345 hwmon->attrs = kcalloc(n_attrs, sizeof(*hwmon->attrs), GFP_KERNEL);
351 hwmon->device = hwmon_device_register(&efx->pci_dev->dev);
352 if (IS_ERR(hwmon->device)) {
353 rc = PTR_ERR(hwmon->device);
357 rc = efx_mcdi_mon_add_attr(efx, "name", efx_mcdi_mon_show_name, 0, 0, 0);
361 for (i = 0, j = -1, type = -1; ; i++) {
362 enum efx_hwmon_type hwmon_type;
363 const char *hwmon_prefix;
364 unsigned hwmon_index;
365 u16 min1, max1, min2, max2;
367 /* Find next sensor type or exit if there is none */
371 if ((type % 32) == 0) {
377 MCDI_SET_DWORD(inbuf, SENSOR_INFO_EXT_IN_PAGE,
379 rc = efx_mcdi_rpc(efx, MC_CMD_SENSOR_INFO,
380 inbuf, sizeof(inbuf),
381 outbuf, sizeof(outbuf),
385 if (outlen < MC_CMD_SENSOR_INFO_OUT_LENMIN) {
390 mask = (MCDI_DWORD(outbuf,
391 SENSOR_INFO_OUT_MASK) &
392 ~(1 << MC_CMD_SENSOR_PAGE0_NEXT));
394 /* Check again for short response */
396 MC_CMD_SENSOR_INFO_OUT_LEN(hweight32(mask))) {
401 } while (!(mask & (1 << type % 32)));
404 if (type < ARRAY_SIZE(efx_mcdi_sensor_type)) {
405 hwmon_type = efx_mcdi_sensor_type[type].hwmon_type;
407 /* Skip sensors specific to a different port */
408 if (hwmon_type != EFX_HWMON_UNKNOWN &&
409 efx_mcdi_sensor_type[type].port >= 0 &&
410 efx_mcdi_sensor_type[type].port !=
414 hwmon_type = EFX_HWMON_UNKNOWN;
417 switch (hwmon_type) {
419 hwmon_prefix = "temp";
420 hwmon_index = ++n_temp; /* 1-based */
423 /* This is likely to be a heatsink, but there
424 * is no convention for representing cooling
425 * devices other than fans.
427 hwmon_prefix = "fan";
428 hwmon_index = ++n_cool; /* 1-based */
432 hwmon_index = n_in++; /* 0-based */
435 hwmon_prefix = "curr";
436 hwmon_index = ++n_curr; /* 1-based */
438 case EFX_HWMON_POWER:
439 hwmon_prefix = "power";
440 hwmon_index = ++n_power; /* 1-based */
444 min1 = MCDI_ARRAY_FIELD(outbuf, SENSOR_ENTRY,
445 SENSOR_INFO_ENTRY, j, MIN1);
446 max1 = MCDI_ARRAY_FIELD(outbuf, SENSOR_ENTRY,
447 SENSOR_INFO_ENTRY, j, MAX1);
448 min2 = MCDI_ARRAY_FIELD(outbuf, SENSOR_ENTRY,
449 SENSOR_INFO_ENTRY, j, MIN2);
450 max2 = MCDI_ARRAY_FIELD(outbuf, SENSOR_ENTRY,
451 SENSOR_INFO_ENTRY, j, MAX2);
454 snprintf(name, sizeof(name), "%s%u_input",
455 hwmon_prefix, hwmon_index);
456 rc = efx_mcdi_mon_add_attr(
457 efx, name, efx_mcdi_mon_show_value, i, type, 0);
461 if (hwmon_type != EFX_HWMON_POWER) {
462 snprintf(name, sizeof(name), "%s%u_min",
463 hwmon_prefix, hwmon_index);
464 rc = efx_mcdi_mon_add_attr(
465 efx, name, efx_mcdi_mon_show_limit,
471 snprintf(name, sizeof(name), "%s%u_max",
472 hwmon_prefix, hwmon_index);
473 rc = efx_mcdi_mon_add_attr(
474 efx, name, efx_mcdi_mon_show_limit,
480 /* Assume max2 is critical value.
481 * But we have no good way to expose min2.
483 snprintf(name, sizeof(name), "%s%u_crit",
484 hwmon_prefix, hwmon_index);
485 rc = efx_mcdi_mon_add_attr(
486 efx, name, efx_mcdi_mon_show_limit,
493 snprintf(name, sizeof(name), "%s%u_alarm",
494 hwmon_prefix, hwmon_index);
495 rc = efx_mcdi_mon_add_attr(
496 efx, name, efx_mcdi_mon_show_alarm, i, type, 0);
500 if (type < ARRAY_SIZE(efx_mcdi_sensor_type) &&
501 efx_mcdi_sensor_type[type].label) {
502 snprintf(name, sizeof(name), "%s%u_label",
503 hwmon_prefix, hwmon_index);
504 rc = efx_mcdi_mon_add_attr(
505 efx, name, efx_mcdi_mon_show_label, i, type, 0);
512 efx_mcdi_mon_remove(efx);
516 void efx_mcdi_mon_remove(struct efx_nic *efx)
518 struct efx_mcdi_mon *hwmon = efx_mcdi_mon(efx);
521 for (i = 0; i < hwmon->n_attrs; i++)
522 device_remove_file(&efx->pci_dev->dev,
523 &hwmon->attrs[i].dev_attr);
526 hwmon_device_unregister(hwmon->device);
527 efx_nic_free_buffer(efx, &hwmon->dma_buf);
530 #endif /* CONFIG_SFC_MCDI_MON */