]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/net/ethernet/sfc/mcdi_mon.c
sfc: Return an error code when a sensor is busy.
[karo-tx-linux.git] / drivers / net / ethernet / sfc / mcdi_mon.c
1 /****************************************************************************
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
20 enum 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
27 static const struct {
28         const char *label;
29         enum efx_hwmon_type hwmon_type;
30         int port;
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),
49 #undef SENSOR
50 };
51
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",
58 };
59
60 void 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
85 struct efx_mcdi_mon_attribute {
86         struct device_attribute dev_attr;
87         unsigned int index;
88         unsigned int type;
89         unsigned int limit_value;
90         char name[12];
91 };
92
93 static int efx_mcdi_mon_update(struct efx_nic *efx)
94 {
95         struct efx_mcdi_mon *hwmon = efx_mcdi_mon(efx);
96         MCDI_DECLARE_BUF(inbuf, MC_CMD_READ_SENSORS_IN_LEN);
97         int rc;
98
99         MCDI_SET_QWORD(inbuf, READ_SENSORS_IN_DMA_ADDR,
100                        hwmon->dma_buf.dma_addr);
101
102         rc = efx_mcdi_rpc(efx, MC_CMD_READ_SENSORS,
103                           inbuf, sizeof(inbuf), NULL, 0, NULL);
104         if (rc == 0)
105                 hwmon->last_update = jiffies;
106         return rc;
107 }
108
109 static ssize_t efx_mcdi_mon_show_name(struct device *dev,
110                                       struct device_attribute *attr,
111                                       char *buf)
112 {
113         return sprintf(buf, "%s\n", KBUILD_MODNAME);
114 }
115
116 static int efx_mcdi_mon_get_entry(struct device *dev, unsigned int index,
117                                   efx_dword_t *entry)
118 {
119         struct efx_nic *efx = dev_get_drvdata(dev);
120         struct efx_mcdi_mon *hwmon = efx_mcdi_mon(efx);
121         int rc;
122
123         BUILD_BUG_ON(MC_CMD_READ_SENSORS_OUT_LEN != 0);
124
125         mutex_lock(&hwmon->update_lock);
126
127         /* Use cached value if last update was < 1 s ago */
128         if (time_before(jiffies, hwmon->last_update + HZ))
129                 rc = 0;
130         else
131                 rc = efx_mcdi_mon_update(efx);
132
133         /* Copy out the requested entry */
134         *entry = ((efx_dword_t *)hwmon->dma_buf.addr)[index];
135
136         mutex_unlock(&hwmon->update_lock);
137
138         return rc;
139 }
140
141 static ssize_t efx_mcdi_mon_show_value(struct device *dev,
142                                        struct device_attribute *attr,
143                                        char *buf)
144 {
145         struct efx_mcdi_mon_attribute *mon_attr =
146                 container_of(attr, struct efx_mcdi_mon_attribute, dev_attr);
147         efx_dword_t entry;
148         unsigned int value, state;
149         int rc;
150
151         rc = efx_mcdi_mon_get_entry(dev, mon_attr->index, &entry);
152         if (rc)
153                 return rc;
154
155         state = EFX_DWORD_FIELD(entry, MC_CMD_SENSOR_VALUE_ENTRY_TYPEDEF_STATE);
156         if (state == MC_CMD_SENSOR_STATE_NO_READING)
157                 return -EBUSY;
158
159         value = EFX_DWORD_FIELD(entry, MC_CMD_SENSOR_VALUE_ENTRY_TYPEDEF_VALUE);
160
161         /* Convert temperature from degrees to milli-degrees Celsius */
162         if (efx_mcdi_sensor_type[mon_attr->type].hwmon_type == EFX_HWMON_TEMP)
163                 value *= 1000;
164
165         return sprintf(buf, "%u\n", value);
166 }
167
168 static ssize_t efx_mcdi_mon_show_limit(struct device *dev,
169                                        struct device_attribute *attr,
170                                        char *buf)
171 {
172         struct efx_mcdi_mon_attribute *mon_attr =
173                 container_of(attr, struct efx_mcdi_mon_attribute, dev_attr);
174         unsigned int value;
175
176         value = mon_attr->limit_value;
177
178         /* Convert temperature from degrees to milli-degrees Celsius */
179         if (efx_mcdi_sensor_type[mon_attr->type].hwmon_type == EFX_HWMON_TEMP)
180                 value *= 1000;
181
182         return sprintf(buf, "%u\n", value);
183 }
184
185 static ssize_t efx_mcdi_mon_show_alarm(struct device *dev,
186                                        struct device_attribute *attr,
187                                        char *buf)
188 {
189         struct efx_mcdi_mon_attribute *mon_attr =
190                 container_of(attr, struct efx_mcdi_mon_attribute, dev_attr);
191         efx_dword_t entry;
192         int state;
193         int rc;
194
195         rc = efx_mcdi_mon_get_entry(dev, mon_attr->index, &entry);
196         if (rc)
197                 return rc;
198
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);
201 }
202
203 static ssize_t efx_mcdi_mon_show_label(struct device *dev,
204                                        struct device_attribute *attr,
205                                        char *buf)
206 {
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);
211 }
212
213 static int
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)
219 {
220         struct efx_mcdi_mon *hwmon = efx_mcdi_mon(efx);
221         struct efx_mcdi_mon_attribute *attr = &hwmon->attrs[hwmon->n_attrs];
222         int rc;
223
224         strlcpy(attr->name, name, sizeof(attr->name));
225         attr->index = index;
226         attr->type = type;
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);
233         if (rc == 0)
234                 ++hwmon->n_attrs;
235         return rc;
236 }
237
238 int efx_mcdi_mon_probe(struct efx_nic *efx)
239 {
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);
243         size_t outlen;
244         char name[12];
245         u32 mask;
246         int rc, i, type;
247
248         BUILD_BUG_ON(MC_CMD_SENSOR_INFO_IN_LEN != 0);
249
250         rc = efx_mcdi_rpc(efx, MC_CMD_SENSOR_INFO, NULL, 0,
251                           outbuf, sizeof(outbuf), &outlen);
252         if (rc)
253                 return rc;
254         if (outlen < MC_CMD_SENSOR_INFO_OUT_LENMIN)
255                 return -EIO;
256
257         /* Find out which sensors are present.  Don't create a device
258          * if there are none.
259          */
260         mask = MCDI_DWORD(outbuf, SENSOR_INFO_OUT_MASK);
261         if (mask == 0)
262                 return 0;
263
264         /* Check again for short response */
265         if (outlen < MC_CMD_SENSOR_INFO_OUT_LEN(hweight32(mask)))
266                 return -EIO;
267
268         rc = efx_nic_alloc_buffer(efx, &hwmon->dma_buf,
269                                   4 * MC_CMD_SENSOR_ENTRY_MAXNUM, GFP_KERNEL);
270         if (rc)
271                 return rc;
272
273         mutex_init(&hwmon->update_lock);
274         efx_mcdi_mon_update(efx);
275
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.
279          */
280         n_attrs = 1 + 6 * hweight32(mask);
281         hwmon->attrs = kcalloc(n_attrs, sizeof(*hwmon->attrs), GFP_KERNEL);
282         if (!hwmon->attrs) {
283                 rc = -ENOMEM;
284                 goto fail;
285         }
286
287         hwmon->device = hwmon_device_register(&efx->pci_dev->dev);
288         if (IS_ERR(hwmon->device)) {
289                 rc = PTR_ERR(hwmon->device);
290                 goto fail;
291         }
292
293         rc = efx_mcdi_mon_add_attr(efx, "name", efx_mcdi_mon_show_name, 0, 0, 0);
294         if (rc)
295                 goto fail;
296
297         for (i = 0, type = -1; ; i++) {
298                 const char *hwmon_prefix;
299                 unsigned hwmon_index;
300                 u16 min1, max1, min2, max2;
301
302                 /* Find next sensor type or exit if there is none */
303                 type++;
304                 while (!(mask & (1 << type))) {
305                         type++;
306                         if (type == 32)
307                                 return 0;
308                 }
309
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))
314                         continue;
315
316                 switch (efx_mcdi_sensor_type[type].hwmon_type) {
317                 case EFX_HWMON_TEMP:
318                         hwmon_prefix = "temp";
319                         hwmon_index = ++n_temp; /* 1-based */
320                         break;
321                 case EFX_HWMON_COOL:
322                         /* This is likely to be a heatsink, but there
323                          * is no convention for representing cooling
324                          * devices other than fans.
325                          */
326                         hwmon_prefix = "fan";
327                         hwmon_index = ++n_cool; /* 1-based */
328                         break;
329                 default:
330                         hwmon_prefix = "in";
331                         hwmon_index = n_in++; /* 0-based */
332                         break;
333                 }
334
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);
343
344                 if (min1 != max1) {
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);
349                         if (rc)
350                                 goto fail;
351
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,
356                                 i, type, min1);
357                         if (rc)
358                                 goto fail;
359
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,
364                                 i, type, max1);
365                         if (rc)
366                                 goto fail;
367
368                         if (min2 != max2) {
369                                 /* Assume max2 is critical value.
370                                  * But we have no good way to expose min2.
371                                  */
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,
376                                         i, type, max2);
377                                 if (rc)
378                                         goto fail;
379                         }
380                 }
381
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);
386                 if (rc)
387                         goto fail;
388
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);
394                         if (rc)
395                                 goto fail;
396                 }
397         }
398
399 fail:
400         efx_mcdi_mon_remove(efx);
401         return rc;
402 }
403
404 void efx_mcdi_mon_remove(struct efx_nic *efx)
405 {
406         struct efx_mcdi_mon *hwmon = efx_mcdi_mon(efx);
407         unsigned int i;
408
409         for (i = 0; i < hwmon->n_attrs; i++)
410                 device_remove_file(&efx->pci_dev->dev,
411                                    &hwmon->attrs[i].dev_attr);
412         kfree(hwmon->attrs);
413         if (hwmon->device)
414                 hwmon_device_unregister(hwmon->device);
415         efx_nic_free_buffer(efx, &hwmon->dma_buf);
416 }
417
418 #endif /* CONFIG_SFC_MCDI_MON */