blob: d7d45662d684100d903d0a109e2cd0f302b75936 [file] [log] [blame]
Ben Hutchings55c5e0f82012-01-06 20:25:39 +00001/****************************************************************************
2 * Driver for Solarflare Solarstorm network controllers and boards
3 * Copyright 2011 Solarflare Communications Inc.
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
20enum efx_hwmon_type {
21 EFX_HWMON_UNKNOWN,
22 EFX_HWMON_TEMP, /* temperature */
23 EFX_HWMON_COOL, /* cooling device, probably a heatsink */
24 EFX_HWMON_IN /* input voltage */
25};
26
27static const struct {
28 const char *label;
29 enum efx_hwmon_type hwmon_type;
30 int port;
Ben Hutchingsd4fbdcf2013-08-08 11:14:20 +010031} efx_mcdi_sensor_type[] = {
Ben Hutchings55c5e0f82012-01-06 20:25:39 +000032#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),
Ben Hutchings2d0cc562012-02-17 00:10:45 +000040 SENSOR(PHY1_COOLING, "PHY cooling", EFX_HWMON_COOL, 1),
Ben Hutchings55c5e0f82012-01-06 20:25:39 +000041 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),
49#undef SENSOR
50};
51
52static 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",
Alexandre Rames8c4e7202013-07-03 09:47:34 +010057 [MC_CMD_SENSOR_STATE_NO_READING] = "No reading",
Ben Hutchings55c5e0f82012-01-06 20:25:39 +000058};
59
60void efx_mcdi_sensor_event(struct efx_nic *efx, efx_qword_t *ev)
61{
62 unsigned int type, state, value;
63 const char *name = NULL, *state_txt;
64
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);
68
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;
73 if (!name)
74 name = "No sensor name available";
75 EFX_BUG_ON_PARANOID(state >= ARRAY_SIZE(sensor_status_names));
76 state_txt = sensor_status_names[state];
77
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);
81}
82
83#ifdef CONFIG_SFC_MCDI_MON
84
85struct efx_mcdi_mon_attribute {
86 struct device_attribute dev_attr;
87 unsigned int index;
88 unsigned int type;
Ben Hutchingsd4fbdcf2013-08-08 11:14:20 +010089 enum efx_hwmon_type hwmon_type;
Ben Hutchings55c5e0f82012-01-06 20:25:39 +000090 unsigned int limit_value;
91 char name[12];
92};
93
94static int efx_mcdi_mon_update(struct efx_nic *efx)
95{
96 struct efx_mcdi_mon *hwmon = efx_mcdi_mon(efx);
Ben Hutchingsd4fbdcf2013-08-08 11:14:20 +010097 MCDI_DECLARE_BUF(inbuf, MC_CMD_READ_SENSORS_EXT_IN_LEN);
Ben Hutchings55c5e0f82012-01-06 20:25:39 +000098 int rc;
99
Ben Hutchingsd4fbdcf2013-08-08 11:14:20 +0100100 MCDI_SET_QWORD(inbuf, READ_SENSORS_EXT_IN_DMA_ADDR,
Ben Hutchings338f74d2012-10-10 23:20:17 +0100101 hwmon->dma_buf.dma_addr);
Ben Hutchingsd4fbdcf2013-08-08 11:14:20 +0100102 MCDI_SET_DWORD(inbuf, READ_SENSORS_EXT_IN_LENGTH, hwmon->dma_buf.len);
Ben Hutchings55c5e0f82012-01-06 20:25:39 +0000103
104 rc = efx_mcdi_rpc(efx, MC_CMD_READ_SENSORS,
105 inbuf, sizeof(inbuf), NULL, 0, NULL);
106 if (rc == 0)
107 hwmon->last_update = jiffies;
108 return rc;
109}
110
111static ssize_t efx_mcdi_mon_show_name(struct device *dev,
112 struct device_attribute *attr,
113 char *buf)
114{
115 return sprintf(buf, "%s\n", KBUILD_MODNAME);
116}
117
118static int efx_mcdi_mon_get_entry(struct device *dev, unsigned int index,
119 efx_dword_t *entry)
120{
121 struct efx_nic *efx = dev_get_drvdata(dev);
122 struct efx_mcdi_mon *hwmon = efx_mcdi_mon(efx);
123 int rc;
124
125 BUILD_BUG_ON(MC_CMD_READ_SENSORS_OUT_LEN != 0);
126
127 mutex_lock(&hwmon->update_lock);
128
129 /* Use cached value if last update was < 1 s ago */
130 if (time_before(jiffies, hwmon->last_update + HZ))
131 rc = 0;
132 else
133 rc = efx_mcdi_mon_update(efx);
134
135 /* Copy out the requested entry */
136 *entry = ((efx_dword_t *)hwmon->dma_buf.addr)[index];
137
138 mutex_unlock(&hwmon->update_lock);
139
140 return rc;
141}
142
143static ssize_t efx_mcdi_mon_show_value(struct device *dev,
144 struct device_attribute *attr,
145 char *buf)
146{
147 struct efx_mcdi_mon_attribute *mon_attr =
148 container_of(attr, struct efx_mcdi_mon_attribute, dev_attr);
149 efx_dword_t entry;
Alexandre Rames8c4e7202013-07-03 09:47:34 +0100150 unsigned int value, state;
Ben Hutchings55c5e0f82012-01-06 20:25:39 +0000151 int rc;
152
153 rc = efx_mcdi_mon_get_entry(dev, mon_attr->index, &entry);
154 if (rc)
155 return rc;
156
Alexandre Rames8c4e7202013-07-03 09:47:34 +0100157 state = EFX_DWORD_FIELD(entry, MC_CMD_SENSOR_VALUE_ENTRY_TYPEDEF_STATE);
158 if (state == MC_CMD_SENSOR_STATE_NO_READING)
159 return -EBUSY;
160
Ben Hutchings55c5e0f82012-01-06 20:25:39 +0000161 value = EFX_DWORD_FIELD(entry, MC_CMD_SENSOR_VALUE_ENTRY_TYPEDEF_VALUE);
162
163 /* Convert temperature from degrees to milli-degrees Celsius */
Ben Hutchingsd4fbdcf2013-08-08 11:14:20 +0100164 if (mon_attr->hwmon_type == EFX_HWMON_TEMP)
Ben Hutchings55c5e0f82012-01-06 20:25:39 +0000165 value *= 1000;
166
167 return sprintf(buf, "%u\n", value);
168}
169
170static ssize_t efx_mcdi_mon_show_limit(struct device *dev,
171 struct device_attribute *attr,
172 char *buf)
173{
174 struct efx_mcdi_mon_attribute *mon_attr =
175 container_of(attr, struct efx_mcdi_mon_attribute, dev_attr);
176 unsigned int value;
177
178 value = mon_attr->limit_value;
179
180 /* Convert temperature from degrees to milli-degrees Celsius */
Ben Hutchingsd4fbdcf2013-08-08 11:14:20 +0100181 if (mon_attr->hwmon_type == EFX_HWMON_TEMP)
Ben Hutchings55c5e0f82012-01-06 20:25:39 +0000182 value *= 1000;
183
184 return sprintf(buf, "%u\n", value);
185}
186
187static ssize_t efx_mcdi_mon_show_alarm(struct device *dev,
188 struct device_attribute *attr,
189 char *buf)
190{
191 struct efx_mcdi_mon_attribute *mon_attr =
192 container_of(attr, struct efx_mcdi_mon_attribute, dev_attr);
193 efx_dword_t entry;
194 int state;
195 int rc;
196
197 rc = efx_mcdi_mon_get_entry(dev, mon_attr->index, &entry);
198 if (rc)
199 return rc;
200
201 state = EFX_DWORD_FIELD(entry, MC_CMD_SENSOR_VALUE_ENTRY_TYPEDEF_STATE);
202 return sprintf(buf, "%d\n", state != MC_CMD_SENSOR_STATE_OK);
203}
204
205static ssize_t efx_mcdi_mon_show_label(struct device *dev,
206 struct device_attribute *attr,
207 char *buf)
208{
209 struct efx_mcdi_mon_attribute *mon_attr =
210 container_of(attr, struct efx_mcdi_mon_attribute, dev_attr);
211 return sprintf(buf, "%s\n",
212 efx_mcdi_sensor_type[mon_attr->type].label);
213}
214
215static int
216efx_mcdi_mon_add_attr(struct efx_nic *efx, const char *name,
217 ssize_t (*reader)(struct device *,
218 struct device_attribute *, char *),
219 unsigned int index, unsigned int type,
220 unsigned int limit_value)
221{
222 struct efx_mcdi_mon *hwmon = efx_mcdi_mon(efx);
223 struct efx_mcdi_mon_attribute *attr = &hwmon->attrs[hwmon->n_attrs];
224 int rc;
225
226 strlcpy(attr->name, name, sizeof(attr->name));
227 attr->index = index;
228 attr->type = type;
Ben Hutchingsd4fbdcf2013-08-08 11:14:20 +0100229 if (type < ARRAY_SIZE(efx_mcdi_sensor_type))
230 attr->hwmon_type = efx_mcdi_sensor_type[type].hwmon_type;
231 else
232 attr->hwmon_type = EFX_HWMON_UNKNOWN;
Ben Hutchings55c5e0f82012-01-06 20:25:39 +0000233 attr->limit_value = limit_value;
Michal Schmidta9ec6bd2012-07-19 07:04:45 +0000234 sysfs_attr_init(&attr->dev_attr.attr);
Ben Hutchings55c5e0f82012-01-06 20:25:39 +0000235 attr->dev_attr.attr.name = attr->name;
236 attr->dev_attr.attr.mode = S_IRUGO;
237 attr->dev_attr.show = reader;
238 rc = device_create_file(&efx->pci_dev->dev, &attr->dev_attr);
239 if (rc == 0)
240 ++hwmon->n_attrs;
241 return rc;
242}
243
244int efx_mcdi_mon_probe(struct efx_nic *efx)
245{
246 struct efx_mcdi_mon *hwmon = efx_mcdi_mon(efx);
Ben Hutchingsd4fbdcf2013-08-08 11:14:20 +0100247 unsigned int n_temp = 0, n_cool = 0, n_in = 0;
248 MCDI_DECLARE_BUF(inbuf, MC_CMD_SENSOR_INFO_EXT_IN_LEN);
Ben Hutchings59cfc472012-09-14 17:30:10 +0100249 MCDI_DECLARE_BUF(outbuf, MC_CMD_SENSOR_INFO_OUT_LENMAX);
Ben Hutchingsd4fbdcf2013-08-08 11:14:20 +0100250 unsigned int n_pages, n_sensors, n_attrs, page;
Ben Hutchings55c5e0f82012-01-06 20:25:39 +0000251 size_t outlen;
252 char name[12];
253 u32 mask;
Ben Hutchingsd4fbdcf2013-08-08 11:14:20 +0100254 int rc, i, j, type;
Ben Hutchings55c5e0f82012-01-06 20:25:39 +0000255
Ben Hutchingsd4fbdcf2013-08-08 11:14:20 +0100256 /* Find out how many sensors are present */
257 n_sensors = 0;
258 page = 0;
259 do {
260 MCDI_SET_DWORD(inbuf, SENSOR_INFO_EXT_IN_PAGE, page);
Ben Hutchings55c5e0f82012-01-06 20:25:39 +0000261
Ben Hutchingsd4fbdcf2013-08-08 11:14:20 +0100262 rc = efx_mcdi_rpc(efx, MC_CMD_SENSOR_INFO, inbuf, sizeof(inbuf),
263 outbuf, sizeof(outbuf), &outlen);
264 if (rc)
265 return rc;
266 if (outlen < MC_CMD_SENSOR_INFO_OUT_LENMIN)
267 return -EIO;
Ben Hutchings55c5e0f82012-01-06 20:25:39 +0000268
Ben Hutchingsd4fbdcf2013-08-08 11:14:20 +0100269 mask = MCDI_DWORD(outbuf, SENSOR_INFO_OUT_MASK);
270 n_sensors += hweight32(mask & ~(1 << MC_CMD_SENSOR_PAGE0_NEXT));
271 ++page;
272 } while (mask & (1 << MC_CMD_SENSOR_PAGE0_NEXT));
273 n_pages = page;
274
275 /* Don't create a device if there are none */
276 if (n_sensors == 0)
Ben Hutchings55c5e0f82012-01-06 20:25:39 +0000277 return 0;
278
Ben Hutchingsd4fbdcf2013-08-08 11:14:20 +0100279 rc = efx_nic_alloc_buffer(
280 efx, &hwmon->dma_buf,
281 n_sensors * MC_CMD_SENSOR_VALUE_ENTRY_TYPEDEF_LEN,
282 GFP_KERNEL);
Ben Hutchings55c5e0f82012-01-06 20:25:39 +0000283 if (rc)
284 return rc;
285
286 mutex_init(&hwmon->update_lock);
287 efx_mcdi_mon_update(efx);
288
289 /* Allocate space for the maximum possible number of
290 * attributes for this set of sensors: name of the driver plus
291 * value, min, max, crit, alarm and label for each sensor.
292 */
Ben Hutchingsd4fbdcf2013-08-08 11:14:20 +0100293 n_attrs = 1 + 6 * n_sensors;
Ben Hutchings55c5e0f82012-01-06 20:25:39 +0000294 hwmon->attrs = kcalloc(n_attrs, sizeof(*hwmon->attrs), GFP_KERNEL);
295 if (!hwmon->attrs) {
296 rc = -ENOMEM;
297 goto fail;
298 }
299
300 hwmon->device = hwmon_device_register(&efx->pci_dev->dev);
301 if (IS_ERR(hwmon->device)) {
302 rc = PTR_ERR(hwmon->device);
303 goto fail;
304 }
305
306 rc = efx_mcdi_mon_add_attr(efx, "name", efx_mcdi_mon_show_name, 0, 0, 0);
307 if (rc)
308 goto fail;
309
Ben Hutchingsd4fbdcf2013-08-08 11:14:20 +0100310 for (i = 0, j = -1, type = -1; ; i++) {
311 enum efx_hwmon_type hwmon_type;
Ben Hutchings55c5e0f82012-01-06 20:25:39 +0000312 const char *hwmon_prefix;
313 unsigned hwmon_index;
314 u16 min1, max1, min2, max2;
315
316 /* Find next sensor type or exit if there is none */
Ben Hutchingsd4fbdcf2013-08-08 11:14:20 +0100317 do {
Ben Hutchings55c5e0f82012-01-06 20:25:39 +0000318 type++;
Ben Hutchingsd4fbdcf2013-08-08 11:14:20 +0100319
320 if ((type % 32) == 0) {
321 page = type / 32;
322 j = -1;
323 if (page == n_pages)
324 return 0;
325
326 MCDI_SET_DWORD(inbuf, SENSOR_INFO_EXT_IN_PAGE,
327 page);
328 rc = efx_mcdi_rpc(efx, MC_CMD_SENSOR_INFO,
329 inbuf, sizeof(inbuf),
330 outbuf, sizeof(outbuf),
331 &outlen);
332 if (rc)
333 goto fail;
334 if (outlen < MC_CMD_SENSOR_INFO_OUT_LENMIN) {
335 rc = -EIO;
336 goto fail;
337 }
338
339 mask = (MCDI_DWORD(outbuf,
340 SENSOR_INFO_OUT_MASK) &
341 ~(1 << MC_CMD_SENSOR_PAGE0_NEXT));
342
343 /* Check again for short response */
344 if (outlen <
345 MC_CMD_SENSOR_INFO_OUT_LEN(hweight32(mask))) {
346 rc = -EIO;
347 goto fail;
348 }
349 }
350 } while (!(mask & (1 << type % 32)));
351 j++;
352
353 if (type < ARRAY_SIZE(efx_mcdi_sensor_type)) {
354 hwmon_type = efx_mcdi_sensor_type[type].hwmon_type;
355
356 /* Skip sensors specific to a different port */
357 if (hwmon_type != EFX_HWMON_UNKNOWN &&
358 efx_mcdi_sensor_type[type].port >= 0 &&
359 efx_mcdi_sensor_type[type].port !=
360 efx_port_num(efx))
361 continue;
362 } else {
363 hwmon_type = EFX_HWMON_UNKNOWN;
Ben Hutchings55c5e0f82012-01-06 20:25:39 +0000364 }
365
Ben Hutchingsd4fbdcf2013-08-08 11:14:20 +0100366 switch (hwmon_type) {
Ben Hutchings55c5e0f82012-01-06 20:25:39 +0000367 case EFX_HWMON_TEMP:
368 hwmon_prefix = "temp";
369 hwmon_index = ++n_temp; /* 1-based */
370 break;
371 case EFX_HWMON_COOL:
372 /* This is likely to be a heatsink, but there
373 * is no convention for representing cooling
374 * devices other than fans.
375 */
376 hwmon_prefix = "fan";
377 hwmon_index = ++n_cool; /* 1-based */
378 break;
379 default:
380 hwmon_prefix = "in";
381 hwmon_index = n_in++; /* 0-based */
382 break;
383 }
384
385 min1 = MCDI_ARRAY_FIELD(outbuf, SENSOR_ENTRY,
Ben Hutchingsd4fbdcf2013-08-08 11:14:20 +0100386 SENSOR_INFO_ENTRY, j, MIN1);
Ben Hutchings55c5e0f82012-01-06 20:25:39 +0000387 max1 = MCDI_ARRAY_FIELD(outbuf, SENSOR_ENTRY,
Ben Hutchingsd4fbdcf2013-08-08 11:14:20 +0100388 SENSOR_INFO_ENTRY, j, MAX1);
Ben Hutchings55c5e0f82012-01-06 20:25:39 +0000389 min2 = MCDI_ARRAY_FIELD(outbuf, SENSOR_ENTRY,
Ben Hutchingsd4fbdcf2013-08-08 11:14:20 +0100390 SENSOR_INFO_ENTRY, j, MIN2);
Ben Hutchings55c5e0f82012-01-06 20:25:39 +0000391 max2 = MCDI_ARRAY_FIELD(outbuf, SENSOR_ENTRY,
Ben Hutchingsd4fbdcf2013-08-08 11:14:20 +0100392 SENSOR_INFO_ENTRY, j, MAX2);
Ben Hutchings55c5e0f82012-01-06 20:25:39 +0000393
394 if (min1 != max1) {
395 snprintf(name, sizeof(name), "%s%u_input",
396 hwmon_prefix, hwmon_index);
397 rc = efx_mcdi_mon_add_attr(
398 efx, name, efx_mcdi_mon_show_value, i, type, 0);
399 if (rc)
400 goto fail;
401
402 snprintf(name, sizeof(name), "%s%u_min",
403 hwmon_prefix, hwmon_index);
404 rc = efx_mcdi_mon_add_attr(
405 efx, name, efx_mcdi_mon_show_limit,
406 i, type, min1);
407 if (rc)
408 goto fail;
409
410 snprintf(name, sizeof(name), "%s%u_max",
411 hwmon_prefix, hwmon_index);
412 rc = efx_mcdi_mon_add_attr(
413 efx, name, efx_mcdi_mon_show_limit,
414 i, type, max1);
415 if (rc)
416 goto fail;
417
418 if (min2 != max2) {
419 /* Assume max2 is critical value.
420 * But we have no good way to expose min2.
421 */
422 snprintf(name, sizeof(name), "%s%u_crit",
423 hwmon_prefix, hwmon_index);
424 rc = efx_mcdi_mon_add_attr(
425 efx, name, efx_mcdi_mon_show_limit,
426 i, type, max2);
427 if (rc)
428 goto fail;
429 }
430 }
431
432 snprintf(name, sizeof(name), "%s%u_alarm",
433 hwmon_prefix, hwmon_index);
434 rc = efx_mcdi_mon_add_attr(
435 efx, name, efx_mcdi_mon_show_alarm, i, type, 0);
436 if (rc)
437 goto fail;
438
Ben Hutchingsd4fbdcf2013-08-08 11:14:20 +0100439 if (type < ARRAY_SIZE(efx_mcdi_sensor_type) &&
440 efx_mcdi_sensor_type[type].label) {
Ben Hutchings55c5e0f82012-01-06 20:25:39 +0000441 snprintf(name, sizeof(name), "%s%u_label",
442 hwmon_prefix, hwmon_index);
443 rc = efx_mcdi_mon_add_attr(
444 efx, name, efx_mcdi_mon_show_label, i, type, 0);
445 if (rc)
446 goto fail;
447 }
448 }
449
450fail:
451 efx_mcdi_mon_remove(efx);
452 return rc;
453}
454
455void efx_mcdi_mon_remove(struct efx_nic *efx)
456{
Ben Hutchingse847b532012-09-13 01:11:24 +0100457 struct efx_mcdi_mon *hwmon = efx_mcdi_mon(efx);
Ben Hutchings55c5e0f82012-01-06 20:25:39 +0000458 unsigned int i;
459
460 for (i = 0; i < hwmon->n_attrs; i++)
461 device_remove_file(&efx->pci_dev->dev,
462 &hwmon->attrs[i].dev_attr);
463 kfree(hwmon->attrs);
464 if (hwmon->device)
465 hwmon_device_unregister(hwmon->device);
466 efx_nic_free_buffer(efx, &hwmon->dma_buf);
467}
468
469#endif /* CONFIG_SFC_MCDI_MON */