mlxsw: core: Change BUG to WARN in hwmon code
[cascardo/linux.git] / drivers / net / ethernet / mellanox / mlxsw / core_hwmon.c
1 /*
2  * drivers/net/ethernet/mellanox/mlxsw/core_hwmon.c
3  * Copyright (c) 2015 Mellanox Technologies. All rights reserved.
4  * Copyright (c) 2015 Jiri Pirko <jiri@mellanox.com>
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. Neither the names of the copyright holders nor the names of its
15  *    contributors may be used to endorse or promote products derived from
16  *    this software without specific prior written permission.
17  *
18  * Alternatively, this software may be distributed under the terms of the
19  * GNU General Public License ("GPL") version 2 as published by the Free
20  * Software Foundation.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
23  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
26  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  */
34
35 #include <linux/kernel.h>
36 #include <linux/types.h>
37 #include <linux/device.h>
38 #include <linux/sysfs.h>
39 #include <linux/hwmon.h>
40 #include <linux/err.h>
41
42 #include "core.h"
43
44 #define MLXSW_HWMON_TEMP_SENSOR_MAX_COUNT 127
45 #define MLXSW_HWMON_ATTR_COUNT (MLXSW_HWMON_TEMP_SENSOR_MAX_COUNT * 4 + \
46                                 MLXSW_MFCR_TACHOS_MAX + MLXSW_MFCR_PWMS_MAX)
47
48 struct mlxsw_hwmon_attr {
49         struct device_attribute dev_attr;
50         struct mlxsw_hwmon *hwmon;
51         unsigned int type_index;
52         char name[16];
53 };
54
55 struct mlxsw_hwmon {
56         struct mlxsw_core *core;
57         const struct mlxsw_bus_info *bus_info;
58         struct device *hwmon_dev;
59         struct attribute_group group;
60         const struct attribute_group *groups[2];
61         struct attribute *attrs[MLXSW_HWMON_ATTR_COUNT + 1];
62         struct mlxsw_hwmon_attr hwmon_attrs[MLXSW_HWMON_ATTR_COUNT];
63         unsigned int attrs_count;
64 };
65
66 static ssize_t mlxsw_hwmon_temp_show(struct device *dev,
67                                      struct device_attribute *attr,
68                                      char *buf)
69 {
70         struct mlxsw_hwmon_attr *mlwsw_hwmon_attr =
71                         container_of(attr, struct mlxsw_hwmon_attr, dev_attr);
72         struct mlxsw_hwmon *mlxsw_hwmon = mlwsw_hwmon_attr->hwmon;
73         char mtmp_pl[MLXSW_REG_MTMP_LEN];
74         unsigned int temp;
75         int err;
76
77         mlxsw_reg_mtmp_pack(mtmp_pl, mlwsw_hwmon_attr->type_index,
78                             false, false);
79         err = mlxsw_reg_query(mlxsw_hwmon->core, MLXSW_REG(mtmp), mtmp_pl);
80         if (err) {
81                 dev_err(mlxsw_hwmon->bus_info->dev, "Failed to query temp sensor\n");
82                 return err;
83         }
84         mlxsw_reg_mtmp_unpack(mtmp_pl, &temp, NULL, NULL);
85         return sprintf(buf, "%u\n", temp);
86 }
87
88 static ssize_t mlxsw_hwmon_temp_max_show(struct device *dev,
89                                          struct device_attribute *attr,
90                                          char *buf)
91 {
92         struct mlxsw_hwmon_attr *mlwsw_hwmon_attr =
93                         container_of(attr, struct mlxsw_hwmon_attr, dev_attr);
94         struct mlxsw_hwmon *mlxsw_hwmon = mlwsw_hwmon_attr->hwmon;
95         char mtmp_pl[MLXSW_REG_MTMP_LEN];
96         unsigned int temp_max;
97         int err;
98
99         mlxsw_reg_mtmp_pack(mtmp_pl, mlwsw_hwmon_attr->type_index,
100                             false, false);
101         err = mlxsw_reg_query(mlxsw_hwmon->core, MLXSW_REG(mtmp), mtmp_pl);
102         if (err) {
103                 dev_err(mlxsw_hwmon->bus_info->dev, "Failed to query temp sensor\n");
104                 return err;
105         }
106         mlxsw_reg_mtmp_unpack(mtmp_pl, NULL, &temp_max, NULL);
107         return sprintf(buf, "%u\n", temp_max);
108 }
109
110 static ssize_t mlxsw_hwmon_fan_rpm_show(struct device *dev,
111                                         struct device_attribute *attr,
112                                         char *buf)
113 {
114         struct mlxsw_hwmon_attr *mlwsw_hwmon_attr =
115                         container_of(attr, struct mlxsw_hwmon_attr, dev_attr);
116         struct mlxsw_hwmon *mlxsw_hwmon = mlwsw_hwmon_attr->hwmon;
117         char mfsm_pl[MLXSW_REG_MFSM_LEN];
118         int err;
119
120         mlxsw_reg_mfsm_pack(mfsm_pl, mlwsw_hwmon_attr->type_index);
121         err = mlxsw_reg_query(mlxsw_hwmon->core, MLXSW_REG(mfsm), mfsm_pl);
122         if (err) {
123                 dev_err(mlxsw_hwmon->bus_info->dev, "Failed to query fan\n");
124                 return err;
125         }
126         return sprintf(buf, "%u\n", mlxsw_reg_mfsm_rpm_get(mfsm_pl));
127 }
128
129 static ssize_t mlxsw_hwmon_pwm_show(struct device *dev,
130                                     struct device_attribute *attr,
131                                     char *buf)
132 {
133         struct mlxsw_hwmon_attr *mlwsw_hwmon_attr =
134                         container_of(attr, struct mlxsw_hwmon_attr, dev_attr);
135         struct mlxsw_hwmon *mlxsw_hwmon = mlwsw_hwmon_attr->hwmon;
136         char mfsc_pl[MLXSW_REG_MFSC_LEN];
137         int err;
138
139         mlxsw_reg_mfsc_pack(mfsc_pl, mlwsw_hwmon_attr->type_index, 0);
140         err = mlxsw_reg_query(mlxsw_hwmon->core, MLXSW_REG(mfsc), mfsc_pl);
141         if (err) {
142                 dev_err(mlxsw_hwmon->bus_info->dev, "Failed to query PWM\n");
143                 return err;
144         }
145         return sprintf(buf, "%u\n",
146                        mlxsw_reg_mfsc_pwm_duty_cycle_get(mfsc_pl));
147 }
148
149 static ssize_t mlxsw_hwmon_pwm_store(struct device *dev,
150                                      struct device_attribute *attr,
151                                      const char *buf, size_t len)
152 {
153         struct mlxsw_hwmon_attr *mlwsw_hwmon_attr =
154                         container_of(attr, struct mlxsw_hwmon_attr, dev_attr);
155         struct mlxsw_hwmon *mlxsw_hwmon = mlwsw_hwmon_attr->hwmon;
156         char mfsc_pl[MLXSW_REG_MFSC_LEN];
157         unsigned long val;
158         int err;
159
160         err = kstrtoul(buf, 10, &val);
161         if (err)
162                 return err;
163         if (val > 255)
164                 return -EINVAL;
165
166         mlxsw_reg_mfsc_pack(mfsc_pl, mlwsw_hwmon_attr->type_index, val);
167         err = mlxsw_reg_write(mlxsw_hwmon->core, MLXSW_REG(mfsc), mfsc_pl);
168         if (err) {
169                 dev_err(mlxsw_hwmon->bus_info->dev, "Failed to write PWM\n");
170                 return err;
171         }
172         return err ? err : len;
173 }
174
175 enum mlxsw_hwmon_attr_type {
176         MLXSW_HWMON_ATTR_TYPE_TEMP,
177         MLXSW_HWMON_ATTR_TYPE_TEMP_MAX,
178         MLXSW_HWMON_ATTR_TYPE_FAN_RPM,
179         MLXSW_HWMON_ATTR_TYPE_PWM,
180 };
181
182 static void mlxsw_hwmon_attr_add(struct mlxsw_hwmon *mlxsw_hwmon,
183                                  enum mlxsw_hwmon_attr_type attr_type,
184                                  unsigned int type_index, unsigned int num) {
185         struct mlxsw_hwmon_attr *mlxsw_hwmon_attr;
186         unsigned int attr_index;
187
188         attr_index = mlxsw_hwmon->attrs_count;
189         mlxsw_hwmon_attr = &mlxsw_hwmon->hwmon_attrs[attr_index];
190
191         switch (attr_type) {
192         case MLXSW_HWMON_ATTR_TYPE_TEMP:
193                 mlxsw_hwmon_attr->dev_attr.show = mlxsw_hwmon_temp_show;
194                 mlxsw_hwmon_attr->dev_attr.attr.mode = S_IRUGO;
195                 snprintf(mlxsw_hwmon_attr->name, sizeof(mlxsw_hwmon_attr->name),
196                          "temp%u_input", num + 1);
197                 break;
198         case MLXSW_HWMON_ATTR_TYPE_TEMP_MAX:
199                 mlxsw_hwmon_attr->dev_attr.show = mlxsw_hwmon_temp_max_show;
200                 mlxsw_hwmon_attr->dev_attr.attr.mode = S_IRUGO;
201                 snprintf(mlxsw_hwmon_attr->name, sizeof(mlxsw_hwmon_attr->name),
202                          "temp%u_highest", num + 1);
203                 break;
204         case MLXSW_HWMON_ATTR_TYPE_FAN_RPM:
205                 mlxsw_hwmon_attr->dev_attr.show = mlxsw_hwmon_fan_rpm_show;
206                 mlxsw_hwmon_attr->dev_attr.attr.mode = S_IRUGO;
207                 snprintf(mlxsw_hwmon_attr->name, sizeof(mlxsw_hwmon_attr->name),
208                          "fan%u_input", num + 1);
209                 break;
210         case MLXSW_HWMON_ATTR_TYPE_PWM:
211                 mlxsw_hwmon_attr->dev_attr.show = mlxsw_hwmon_pwm_show;
212                 mlxsw_hwmon_attr->dev_attr.store = mlxsw_hwmon_pwm_store;
213                 mlxsw_hwmon_attr->dev_attr.attr.mode = S_IWUSR | S_IRUGO;
214                 snprintf(mlxsw_hwmon_attr->name, sizeof(mlxsw_hwmon_attr->name),
215                          "pwm%u", num + 1);
216                 break;
217         default:
218                 WARN_ON(1);
219         }
220
221         mlxsw_hwmon_attr->type_index = type_index;
222         mlxsw_hwmon_attr->hwmon = mlxsw_hwmon;
223         mlxsw_hwmon_attr->dev_attr.attr.name = mlxsw_hwmon_attr->name;
224         sysfs_attr_init(&mlxsw_hwmon_attr->dev_attr.attr);
225
226         mlxsw_hwmon->attrs[attr_index] = &mlxsw_hwmon_attr->dev_attr.attr;
227         mlxsw_hwmon->attrs_count++;
228 }
229
230 static int mlxsw_hwmon_temp_init(struct mlxsw_hwmon *mlxsw_hwmon)
231 {
232         char mtcap_pl[MLXSW_REG_MTCAP_LEN];
233         char mtmp_pl[MLXSW_REG_MTMP_LEN];
234         u8 sensor_count;
235         int i;
236         int err;
237
238         err = mlxsw_reg_query(mlxsw_hwmon->core, MLXSW_REG(mtcap), mtcap_pl);
239         if (err) {
240                 dev_err(mlxsw_hwmon->bus_info->dev, "Failed to get number of temp sensors\n");
241                 return err;
242         }
243         sensor_count = mlxsw_reg_mtcap_sensor_count_get(mtcap_pl);
244         for (i = 0; i < sensor_count; i++) {
245                 mlxsw_reg_mtmp_pack(mtmp_pl, 0, true, true);
246                 err = mlxsw_reg_write(mlxsw_hwmon->core,
247                                       MLXSW_REG(mtmp), mtmp_pl);
248                 if (err) {
249                         dev_err(mlxsw_hwmon->bus_info->dev, "Failed to setup temp sensor number %d\n",
250                                 i);
251                         return err;
252                 }
253                 mlxsw_hwmon_attr_add(mlxsw_hwmon,
254                                      MLXSW_HWMON_ATTR_TYPE_TEMP, i, i);
255                 mlxsw_hwmon_attr_add(mlxsw_hwmon,
256                                      MLXSW_HWMON_ATTR_TYPE_TEMP_MAX, i, i);
257         }
258         return 0;
259 }
260
261 static int mlxsw_hwmon_fans_init(struct mlxsw_hwmon *mlxsw_hwmon)
262 {
263         char mfcr_pl[MLXSW_REG_MFCR_LEN];
264         enum mlxsw_reg_mfcr_pwm_frequency freq;
265         unsigned int type_index;
266         unsigned int num;
267         u16 tacho_active;
268         u8 pwm_active;
269         int err;
270
271         err = mlxsw_reg_query(mlxsw_hwmon->core, MLXSW_REG(mfcr), mfcr_pl);
272         if (err) {
273                 dev_err(mlxsw_hwmon->bus_info->dev, "Failed to get to probe PWMs and Tachometers\n");
274                 return err;
275         }
276         mlxsw_reg_mfcr_unpack(mfcr_pl, &freq, &tacho_active, &pwm_active);
277         num = 0;
278         for (type_index = 0; type_index < MLXSW_MFCR_TACHOS_MAX; type_index++) {
279                 if (tacho_active & BIT(type_index))
280                         mlxsw_hwmon_attr_add(mlxsw_hwmon,
281                                              MLXSW_HWMON_ATTR_TYPE_FAN_RPM,
282                                              type_index, num++);
283         }
284         num = 0;
285         for (type_index = 0; type_index < MLXSW_MFCR_PWMS_MAX; type_index++) {
286                 if (pwm_active & BIT(type_index))
287                         mlxsw_hwmon_attr_add(mlxsw_hwmon,
288                                              MLXSW_HWMON_ATTR_TYPE_PWM,
289                                              type_index, num++);
290         }
291         return 0;
292 }
293
294 int mlxsw_hwmon_init(struct mlxsw_core *mlxsw_core,
295                      const struct mlxsw_bus_info *mlxsw_bus_info,
296                      struct mlxsw_hwmon **p_hwmon)
297 {
298         struct mlxsw_hwmon *mlxsw_hwmon;
299         struct device *hwmon_dev;
300         int err;
301
302         mlxsw_hwmon = kzalloc(sizeof(*mlxsw_hwmon), GFP_KERNEL);
303         if (!mlxsw_hwmon)
304                 return -ENOMEM;
305         mlxsw_hwmon->core = mlxsw_core;
306         mlxsw_hwmon->bus_info = mlxsw_bus_info;
307
308         err = mlxsw_hwmon_temp_init(mlxsw_hwmon);
309         if (err)
310                 goto err_temp_init;
311
312         err = mlxsw_hwmon_fans_init(mlxsw_hwmon);
313         if (err)
314                 goto err_fans_init;
315
316         mlxsw_hwmon->groups[0] = &mlxsw_hwmon->group;
317         mlxsw_hwmon->group.attrs = mlxsw_hwmon->attrs;
318
319         hwmon_dev = devm_hwmon_device_register_with_groups(mlxsw_bus_info->dev,
320                                                            "mlxsw",
321                                                            mlxsw_hwmon,
322                                                            mlxsw_hwmon->groups);
323         if (IS_ERR(hwmon_dev)) {
324                 err = PTR_ERR(hwmon_dev);
325                 goto err_hwmon_register;
326         }
327
328         mlxsw_hwmon->hwmon_dev = hwmon_dev;
329         *p_hwmon = mlxsw_hwmon;
330         return 0;
331
332 err_hwmon_register:
333 err_fans_init:
334 err_temp_init:
335         kfree(mlxsw_hwmon);
336         return err;
337 }
338
339 void mlxsw_hwmon_fini(struct mlxsw_hwmon *mlxsw_hwmon)
340 {
341         kfree(mlxsw_hwmon);
342 }