1 /****************************************************************************
2 * Driver for Solarflare Solarstorm network controllers and boards
3 * Copyright 2011 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 /* input voltage */
29 enum efx_hwmon_type hwmon_type;
31 } efx_mcdi_sensor_type[MC_CMD_SENSOR_ENTRY_MAXNUM] = {
32 #define SENSOR(name, label, hwmon_type, port) \
33 [MC_CMD_SENSOR_##name] = { label, hwmon_type, port }
34 SENSOR(CONTROLLER_TEMP, "Controller temp.", EFX_HWMON_TEMP, -1),
35 SENSOR(PHY_COMMON_TEMP, "PHY temp.", EFX_HWMON_TEMP, -1),
36 SENSOR(CONTROLLER_COOLING, "Controller cooling", EFX_HWMON_COOL, -1),
37 SENSOR(PHY0_TEMP, "PHY temp.", EFX_HWMON_TEMP, 0),
38 SENSOR(PHY0_COOLING, "PHY cooling", EFX_HWMON_COOL, 0),
39 SENSOR(PHY1_TEMP, "PHY temp.", EFX_HWMON_TEMP, 1),
40 SENSOR(PHY1_COOLING, "PHY cooling", EFX_HWMON_COOL, 1),
41 SENSOR(IN_1V0, "1.0V supply", EFX_HWMON_IN, -1),
42 SENSOR(IN_1V2, "1.2V supply", EFX_HWMON_IN, -1),
43 SENSOR(IN_1V8, "1.8V supply", EFX_HWMON_IN, -1),
44 SENSOR(IN_2V5, "2.5V supply", EFX_HWMON_IN, -1),
45 SENSOR(IN_3V3, "3.3V supply", EFX_HWMON_IN, -1),
46 SENSOR(IN_12V0, "12.0V supply", EFX_HWMON_IN, -1),
47 SENSOR(IN_1V2A, "1.2V analogue supply", EFX_HWMON_IN, -1),
48 SENSOR(IN_VREF, "ref. voltage", EFX_HWMON_IN, -1),
52 static const char *const sensor_status_names[] = {
53 [MC_CMD_SENSOR_STATE_OK] = "OK",
54 [MC_CMD_SENSOR_STATE_WARNING] = "Warning",
55 [MC_CMD_SENSOR_STATE_FATAL] = "Fatal",
56 [MC_CMD_SENSOR_STATE_BROKEN] = "Device failure",
57 [MC_CMD_SENSOR_STATE_NO_READING] = "No reading",
60 void efx_mcdi_sensor_event(struct efx_nic *efx, efx_qword_t *ev)
62 unsigned int type, state, value;
63 const char *name = NULL, *state_txt;
65 type = EFX_QWORD_FIELD(*ev, MCDI_EVENT_SENSOREVT_MONITOR);
66 state = EFX_QWORD_FIELD(*ev, MCDI_EVENT_SENSOREVT_STATE);
67 value = EFX_QWORD_FIELD(*ev, MCDI_EVENT_SENSOREVT_VALUE);
69 /* Deal gracefully with the board having more drivers than we
70 * know about, but do not expect new sensor states. */
71 if (type < ARRAY_SIZE(efx_mcdi_sensor_type))
72 name = efx_mcdi_sensor_type[type].label;
74 name = "No sensor name available";
75 EFX_BUG_ON_PARANOID(state >= ARRAY_SIZE(sensor_status_names));
76 state_txt = sensor_status_names[state];
78 netif_err(efx, hw, efx->net_dev,
79 "Sensor %d (%s) reports condition '%s' for raw value %d\n",
80 type, name, state_txt, value);
83 #ifdef CONFIG_SFC_MCDI_MON
85 struct efx_mcdi_mon_attribute {
86 struct device_attribute dev_attr;
89 unsigned int limit_value;
93 static int efx_mcdi_mon_update(struct efx_nic *efx)
95 struct efx_mcdi_mon *hwmon = efx_mcdi_mon(efx);
96 MCDI_DECLARE_BUF(inbuf, MC_CMD_READ_SENSORS_IN_LEN);
99 MCDI_SET_QWORD(inbuf, READ_SENSORS_IN_DMA_ADDR,
100 hwmon->dma_buf.dma_addr);
102 rc = efx_mcdi_rpc(efx, MC_CMD_READ_SENSORS,
103 inbuf, sizeof(inbuf), NULL, 0, NULL);
105 hwmon->last_update = jiffies;
109 static ssize_t efx_mcdi_mon_show_name(struct device *dev,
110 struct device_attribute *attr,
113 return sprintf(buf, "%s\n", KBUILD_MODNAME);
116 static int efx_mcdi_mon_get_entry(struct device *dev, unsigned int index,
119 struct efx_nic *efx = dev_get_drvdata(dev);
120 struct efx_mcdi_mon *hwmon = efx_mcdi_mon(efx);
123 BUILD_BUG_ON(MC_CMD_READ_SENSORS_OUT_LEN != 0);
125 mutex_lock(&hwmon->update_lock);
127 /* Use cached value if last update was < 1 s ago */
128 if (time_before(jiffies, hwmon->last_update + HZ))
131 rc = efx_mcdi_mon_update(efx);
133 /* Copy out the requested entry */
134 *entry = ((efx_dword_t *)hwmon->dma_buf.addr)[index];
136 mutex_unlock(&hwmon->update_lock);
141 static ssize_t efx_mcdi_mon_show_value(struct device *dev,
142 struct device_attribute *attr,
145 struct efx_mcdi_mon_attribute *mon_attr =
146 container_of(attr, struct efx_mcdi_mon_attribute, dev_attr);
148 unsigned int value, state;
151 rc = efx_mcdi_mon_get_entry(dev, mon_attr->index, &entry);
155 state = EFX_DWORD_FIELD(entry, MC_CMD_SENSOR_VALUE_ENTRY_TYPEDEF_STATE);
156 if (state == MC_CMD_SENSOR_STATE_NO_READING)
159 value = EFX_DWORD_FIELD(entry, MC_CMD_SENSOR_VALUE_ENTRY_TYPEDEF_VALUE);
161 /* Convert temperature from degrees to milli-degrees Celsius */
162 if (efx_mcdi_sensor_type[mon_attr->type].hwmon_type == EFX_HWMON_TEMP)
165 return sprintf(buf, "%u\n", value);
168 static ssize_t efx_mcdi_mon_show_limit(struct device *dev,
169 struct device_attribute *attr,
172 struct efx_mcdi_mon_attribute *mon_attr =
173 container_of(attr, struct efx_mcdi_mon_attribute, dev_attr);
176 value = mon_attr->limit_value;
178 /* Convert temperature from degrees to milli-degrees Celsius */
179 if (efx_mcdi_sensor_type[mon_attr->type].hwmon_type == EFX_HWMON_TEMP)
182 return sprintf(buf, "%u\n", value);
185 static ssize_t efx_mcdi_mon_show_alarm(struct device *dev,
186 struct device_attribute *attr,
189 struct efx_mcdi_mon_attribute *mon_attr =
190 container_of(attr, struct efx_mcdi_mon_attribute, dev_attr);
195 rc = efx_mcdi_mon_get_entry(dev, mon_attr->index, &entry);
199 state = EFX_DWORD_FIELD(entry, MC_CMD_SENSOR_VALUE_ENTRY_TYPEDEF_STATE);
200 return sprintf(buf, "%d\n", state != MC_CMD_SENSOR_STATE_OK);
203 static ssize_t efx_mcdi_mon_show_label(struct device *dev,
204 struct device_attribute *attr,
207 struct efx_mcdi_mon_attribute *mon_attr =
208 container_of(attr, struct efx_mcdi_mon_attribute, dev_attr);
209 return sprintf(buf, "%s\n",
210 efx_mcdi_sensor_type[mon_attr->type].label);
214 efx_mcdi_mon_add_attr(struct efx_nic *efx, const char *name,
215 ssize_t (*reader)(struct device *,
216 struct device_attribute *, char *),
217 unsigned int index, unsigned int type,
218 unsigned int limit_value)
220 struct efx_mcdi_mon *hwmon = efx_mcdi_mon(efx);
221 struct efx_mcdi_mon_attribute *attr = &hwmon->attrs[hwmon->n_attrs];
224 strlcpy(attr->name, name, sizeof(attr->name));
227 attr->limit_value = limit_value;
228 sysfs_attr_init(&attr->dev_attr.attr);
229 attr->dev_attr.attr.name = attr->name;
230 attr->dev_attr.attr.mode = S_IRUGO;
231 attr->dev_attr.show = reader;
232 rc = device_create_file(&efx->pci_dev->dev, &attr->dev_attr);
238 int efx_mcdi_mon_probe(struct efx_nic *efx)
240 struct efx_mcdi_mon *hwmon = efx_mcdi_mon(efx);
241 unsigned int n_attrs, n_temp = 0, n_cool = 0, n_in = 0;
242 MCDI_DECLARE_BUF(outbuf, MC_CMD_SENSOR_INFO_OUT_LENMAX);
248 BUILD_BUG_ON(MC_CMD_SENSOR_INFO_IN_LEN != 0);
250 rc = efx_mcdi_rpc(efx, MC_CMD_SENSOR_INFO, NULL, 0,
251 outbuf, sizeof(outbuf), &outlen);
254 if (outlen < MC_CMD_SENSOR_INFO_OUT_LENMIN)
257 /* Find out which sensors are present. Don't create a device
260 mask = MCDI_DWORD(outbuf, SENSOR_INFO_OUT_MASK);
264 /* Check again for short response */
265 if (outlen < MC_CMD_SENSOR_INFO_OUT_LEN(hweight32(mask)))
268 rc = efx_nic_alloc_buffer(efx, &hwmon->dma_buf,
269 4 * MC_CMD_SENSOR_ENTRY_MAXNUM, GFP_KERNEL);
273 mutex_init(&hwmon->update_lock);
274 efx_mcdi_mon_update(efx);
276 /* Allocate space for the maximum possible number of
277 * attributes for this set of sensors: name of the driver plus
278 * value, min, max, crit, alarm and label for each sensor.
280 n_attrs = 1 + 6 * hweight32(mask);
281 hwmon->attrs = kcalloc(n_attrs, sizeof(*hwmon->attrs), GFP_KERNEL);
287 hwmon->device = hwmon_device_register(&efx->pci_dev->dev);
288 if (IS_ERR(hwmon->device)) {
289 rc = PTR_ERR(hwmon->device);
293 rc = efx_mcdi_mon_add_attr(efx, "name", efx_mcdi_mon_show_name, 0, 0, 0);
297 for (i = 0, type = -1; ; i++) {
298 const char *hwmon_prefix;
299 unsigned hwmon_index;
300 u16 min1, max1, min2, max2;
302 /* Find next sensor type or exit if there is none */
304 while (!(mask & (1 << type))) {
310 /* Skip sensors specific to a different port */
311 if (efx_mcdi_sensor_type[type].hwmon_type != EFX_HWMON_UNKNOWN &&
312 efx_mcdi_sensor_type[type].port >= 0 &&
313 efx_mcdi_sensor_type[type].port != efx_port_num(efx))
316 switch (efx_mcdi_sensor_type[type].hwmon_type) {
318 hwmon_prefix = "temp";
319 hwmon_index = ++n_temp; /* 1-based */
322 /* This is likely to be a heatsink, but there
323 * is no convention for representing cooling
324 * devices other than fans.
326 hwmon_prefix = "fan";
327 hwmon_index = ++n_cool; /* 1-based */
331 hwmon_index = n_in++; /* 0-based */
335 min1 = MCDI_ARRAY_FIELD(outbuf, SENSOR_ENTRY,
336 SENSOR_INFO_ENTRY, i, MIN1);
337 max1 = MCDI_ARRAY_FIELD(outbuf, SENSOR_ENTRY,
338 SENSOR_INFO_ENTRY, i, MAX1);
339 min2 = MCDI_ARRAY_FIELD(outbuf, SENSOR_ENTRY,
340 SENSOR_INFO_ENTRY, i, MIN2);
341 max2 = MCDI_ARRAY_FIELD(outbuf, SENSOR_ENTRY,
342 SENSOR_INFO_ENTRY, i, MAX2);
345 snprintf(name, sizeof(name), "%s%u_input",
346 hwmon_prefix, hwmon_index);
347 rc = efx_mcdi_mon_add_attr(
348 efx, name, efx_mcdi_mon_show_value, i, type, 0);
352 snprintf(name, sizeof(name), "%s%u_min",
353 hwmon_prefix, hwmon_index);
354 rc = efx_mcdi_mon_add_attr(
355 efx, name, efx_mcdi_mon_show_limit,
360 snprintf(name, sizeof(name), "%s%u_max",
361 hwmon_prefix, hwmon_index);
362 rc = efx_mcdi_mon_add_attr(
363 efx, name, efx_mcdi_mon_show_limit,
369 /* Assume max2 is critical value.
370 * But we have no good way to expose min2.
372 snprintf(name, sizeof(name), "%s%u_crit",
373 hwmon_prefix, hwmon_index);
374 rc = efx_mcdi_mon_add_attr(
375 efx, name, efx_mcdi_mon_show_limit,
382 snprintf(name, sizeof(name), "%s%u_alarm",
383 hwmon_prefix, hwmon_index);
384 rc = efx_mcdi_mon_add_attr(
385 efx, name, efx_mcdi_mon_show_alarm, i, type, 0);
389 if (efx_mcdi_sensor_type[type].label) {
390 snprintf(name, sizeof(name), "%s%u_label",
391 hwmon_prefix, hwmon_index);
392 rc = efx_mcdi_mon_add_attr(
393 efx, name, efx_mcdi_mon_show_label, i, type, 0);
400 efx_mcdi_mon_remove(efx);
404 void efx_mcdi_mon_remove(struct efx_nic *efx)
406 struct efx_mcdi_mon *hwmon = efx_mcdi_mon(efx);
409 for (i = 0; i < hwmon->n_attrs; i++)
410 device_remove_file(&efx->pci_dev->dev,
411 &hwmon->attrs[i].dev_attr);
414 hwmon_device_unregister(hwmon->device);
415 efx_nic_free_buffer(efx, &hwmon->dma_buf);
418 #endif /* CONFIG_SFC_MCDI_MON */