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 int efx_mcdi_mon_get_entry(struct device *dev, unsigned int index,
145 struct efx_nic *efx = dev_get_drvdata(dev->parent);
146 struct efx_mcdi_mon *hwmon = efx_mcdi_mon(efx);
149 BUILD_BUG_ON(MC_CMD_READ_SENSORS_OUT_LEN != 0);
151 mutex_lock(&hwmon->update_lock);
153 /* Use cached value if last update was < 1 s ago */
154 if (time_before(jiffies, hwmon->last_update + HZ))
157 rc = efx_mcdi_mon_update(efx);
159 /* Copy out the requested entry */
160 *entry = ((efx_dword_t *)hwmon->dma_buf.addr)[index];
162 mutex_unlock(&hwmon->update_lock);
167 static ssize_t efx_mcdi_mon_show_value(struct device *dev,
168 struct device_attribute *attr,
171 struct efx_mcdi_mon_attribute *mon_attr =
172 container_of(attr, struct efx_mcdi_mon_attribute, dev_attr);
174 unsigned int value, state;
177 rc = efx_mcdi_mon_get_entry(dev, mon_attr->index, &entry);
181 state = EFX_DWORD_FIELD(entry, MC_CMD_SENSOR_VALUE_ENTRY_TYPEDEF_STATE);
182 if (state == MC_CMD_SENSOR_STATE_NO_READING)
185 value = EFX_DWORD_FIELD(entry, MC_CMD_SENSOR_VALUE_ENTRY_TYPEDEF_VALUE);
187 switch (mon_attr->hwmon_type) {
189 /* Convert temperature from degrees to milli-degrees Celsius */
192 case EFX_HWMON_POWER:
193 /* Convert power from watts to microwatts */
197 /* No conversion needed */
201 return sprintf(buf, "%u\n", value);
204 static ssize_t efx_mcdi_mon_show_limit(struct device *dev,
205 struct device_attribute *attr,
208 struct efx_mcdi_mon_attribute *mon_attr =
209 container_of(attr, struct efx_mcdi_mon_attribute, dev_attr);
212 value = mon_attr->limit_value;
214 switch (mon_attr->hwmon_type) {
216 /* Convert temperature from degrees to milli-degrees Celsius */
219 case EFX_HWMON_POWER:
220 /* Convert power from watts to microwatts */
224 /* No conversion needed */
228 return sprintf(buf, "%u\n", value);
231 static ssize_t efx_mcdi_mon_show_alarm(struct device *dev,
232 struct device_attribute *attr,
235 struct efx_mcdi_mon_attribute *mon_attr =
236 container_of(attr, struct efx_mcdi_mon_attribute, dev_attr);
241 rc = efx_mcdi_mon_get_entry(dev, mon_attr->index, &entry);
245 state = EFX_DWORD_FIELD(entry, MC_CMD_SENSOR_VALUE_ENTRY_TYPEDEF_STATE);
246 return sprintf(buf, "%d\n", state != MC_CMD_SENSOR_STATE_OK);
249 static ssize_t efx_mcdi_mon_show_label(struct device *dev,
250 struct device_attribute *attr,
253 struct efx_mcdi_mon_attribute *mon_attr =
254 container_of(attr, struct efx_mcdi_mon_attribute, dev_attr);
255 return sprintf(buf, "%s\n",
256 efx_mcdi_sensor_type[mon_attr->type].label);
260 efx_mcdi_mon_add_attr(struct efx_nic *efx, const char *name,
261 ssize_t (*reader)(struct device *,
262 struct device_attribute *, char *),
263 unsigned int index, unsigned int type,
264 unsigned int limit_value)
266 struct efx_mcdi_mon *hwmon = efx_mcdi_mon(efx);
267 struct efx_mcdi_mon_attribute *attr = &hwmon->attrs[hwmon->n_attrs];
269 strlcpy(attr->name, name, sizeof(attr->name));
272 if (type < ARRAY_SIZE(efx_mcdi_sensor_type))
273 attr->hwmon_type = efx_mcdi_sensor_type[type].hwmon_type;
275 attr->hwmon_type = EFX_HWMON_UNKNOWN;
276 attr->limit_value = limit_value;
277 sysfs_attr_init(&attr->dev_attr.attr);
278 attr->dev_attr.attr.name = attr->name;
279 attr->dev_attr.attr.mode = S_IRUGO;
280 attr->dev_attr.show = reader;
281 hwmon->group.attrs[hwmon->n_attrs++] = &attr->dev_attr.attr;
284 int efx_mcdi_mon_probe(struct efx_nic *efx)
286 unsigned int n_temp = 0, n_cool = 0, n_in = 0, n_curr = 0, n_power = 0;
287 struct efx_mcdi_mon *hwmon = efx_mcdi_mon(efx);
288 MCDI_DECLARE_BUF(inbuf, MC_CMD_SENSOR_INFO_EXT_IN_LEN);
289 MCDI_DECLARE_BUF(outbuf, MC_CMD_SENSOR_INFO_OUT_LENMAX);
290 unsigned int n_pages, n_sensors, n_attrs, page;
296 /* Find out how many sensors are present */
300 MCDI_SET_DWORD(inbuf, SENSOR_INFO_EXT_IN_PAGE, page);
302 rc = efx_mcdi_rpc(efx, MC_CMD_SENSOR_INFO, inbuf, sizeof(inbuf),
303 outbuf, sizeof(outbuf), &outlen);
306 if (outlen < MC_CMD_SENSOR_INFO_OUT_LENMIN)
309 mask = MCDI_DWORD(outbuf, SENSOR_INFO_OUT_MASK);
310 n_sensors += hweight32(mask & ~(1 << MC_CMD_SENSOR_PAGE0_NEXT));
312 } while (mask & (1 << MC_CMD_SENSOR_PAGE0_NEXT));
315 /* Don't create a device if there are none */
319 rc = efx_nic_alloc_buffer(
320 efx, &hwmon->dma_buf,
321 n_sensors * MC_CMD_SENSOR_VALUE_ENTRY_TYPEDEF_LEN,
326 mutex_init(&hwmon->update_lock);
327 efx_mcdi_mon_update(efx);
329 /* Allocate space for the maximum possible number of
330 * attributes for this set of sensors:
331 * value, min, max, crit, alarm and label for each sensor.
333 n_attrs = 6 * n_sensors;
334 hwmon->attrs = kcalloc(n_attrs, sizeof(*hwmon->attrs), GFP_KERNEL);
339 hwmon->group.attrs = kcalloc(n_attrs + 1, sizeof(struct attribute *),
341 if (!hwmon->group.attrs) {
346 for (i = 0, j = -1, type = -1; ; i++) {
347 enum efx_hwmon_type hwmon_type;
348 const char *hwmon_prefix;
349 unsigned hwmon_index;
350 u16 min1, max1, min2, max2;
352 /* Find next sensor type or exit if there is none */
356 if ((type % 32) == 0) {
362 MCDI_SET_DWORD(inbuf, SENSOR_INFO_EXT_IN_PAGE,
364 rc = efx_mcdi_rpc(efx, MC_CMD_SENSOR_INFO,
365 inbuf, sizeof(inbuf),
366 outbuf, sizeof(outbuf),
370 if (outlen < MC_CMD_SENSOR_INFO_OUT_LENMIN) {
375 mask = (MCDI_DWORD(outbuf,
376 SENSOR_INFO_OUT_MASK) &
377 ~(1 << MC_CMD_SENSOR_PAGE0_NEXT));
379 /* Check again for short response */
381 MC_CMD_SENSOR_INFO_OUT_LEN(hweight32(mask))) {
386 } while (!(mask & (1 << type % 32)));
389 if (type < ARRAY_SIZE(efx_mcdi_sensor_type)) {
390 hwmon_type = efx_mcdi_sensor_type[type].hwmon_type;
392 /* Skip sensors specific to a different port */
393 if (hwmon_type != EFX_HWMON_UNKNOWN &&
394 efx_mcdi_sensor_type[type].port >= 0 &&
395 efx_mcdi_sensor_type[type].port !=
399 hwmon_type = EFX_HWMON_UNKNOWN;
402 switch (hwmon_type) {
404 hwmon_prefix = "temp";
405 hwmon_index = ++n_temp; /* 1-based */
408 /* This is likely to be a heatsink, but there
409 * is no convention for representing cooling
410 * devices other than fans.
412 hwmon_prefix = "fan";
413 hwmon_index = ++n_cool; /* 1-based */
417 hwmon_index = n_in++; /* 0-based */
420 hwmon_prefix = "curr";
421 hwmon_index = ++n_curr; /* 1-based */
423 case EFX_HWMON_POWER:
424 hwmon_prefix = "power";
425 hwmon_index = ++n_power; /* 1-based */
429 min1 = MCDI_ARRAY_FIELD(outbuf, SENSOR_ENTRY,
430 SENSOR_INFO_ENTRY, j, MIN1);
431 max1 = MCDI_ARRAY_FIELD(outbuf, SENSOR_ENTRY,
432 SENSOR_INFO_ENTRY, j, MAX1);
433 min2 = MCDI_ARRAY_FIELD(outbuf, SENSOR_ENTRY,
434 SENSOR_INFO_ENTRY, j, MIN2);
435 max2 = MCDI_ARRAY_FIELD(outbuf, SENSOR_ENTRY,
436 SENSOR_INFO_ENTRY, j, MAX2);
439 snprintf(name, sizeof(name), "%s%u_input",
440 hwmon_prefix, hwmon_index);
441 efx_mcdi_mon_add_attr(
442 efx, name, efx_mcdi_mon_show_value, i, type, 0);
444 if (hwmon_type != EFX_HWMON_POWER) {
445 snprintf(name, sizeof(name), "%s%u_min",
446 hwmon_prefix, hwmon_index);
447 efx_mcdi_mon_add_attr(
448 efx, name, efx_mcdi_mon_show_limit,
452 snprintf(name, sizeof(name), "%s%u_max",
453 hwmon_prefix, hwmon_index);
454 efx_mcdi_mon_add_attr(
455 efx, name, efx_mcdi_mon_show_limit,
459 /* Assume max2 is critical value.
460 * But we have no good way to expose min2.
462 snprintf(name, sizeof(name), "%s%u_crit",
463 hwmon_prefix, hwmon_index);
464 efx_mcdi_mon_add_attr(
465 efx, name, efx_mcdi_mon_show_limit,
470 snprintf(name, sizeof(name), "%s%u_alarm",
471 hwmon_prefix, hwmon_index);
472 efx_mcdi_mon_add_attr(
473 efx, name, efx_mcdi_mon_show_alarm, i, type, 0);
475 if (type < ARRAY_SIZE(efx_mcdi_sensor_type) &&
476 efx_mcdi_sensor_type[type].label) {
477 snprintf(name, sizeof(name), "%s%u_label",
478 hwmon_prefix, hwmon_index);
479 efx_mcdi_mon_add_attr(
480 efx, name, efx_mcdi_mon_show_label, i, type, 0);
485 hwmon->groups[0] = &hwmon->group;
486 hwmon->device = hwmon_device_register_with_groups(&efx->pci_dev->dev,
487 KBUILD_MODNAME, NULL,
489 if (IS_ERR(hwmon->device)) {
490 rc = PTR_ERR(hwmon->device);
497 efx_mcdi_mon_remove(efx);
501 void efx_mcdi_mon_remove(struct efx_nic *efx)
503 struct efx_mcdi_mon *hwmon = efx_mcdi_mon(efx);
506 hwmon_device_unregister(hwmon->device);
508 kfree(hwmon->group.attrs);
509 efx_nic_free_buffer(efx, &hwmon->dma_buf);
512 #endif /* CONFIG_SFC_MCDI_MON */