1 /*
2 * Copyright (C) 2017 Netronome Systems, Inc.
3 *
4 * This software is dual licensed under the GNU General License Version 2,
5 * June 1991 as shown in the file COPYING in the top-level directory of this
6 * source tree or the BSD 2-Clause License provided below. You have the
7 * option to license this software under the complete terms of either license.
8 *
9 * The BSD 2-Clause License:
10 *
11 * Redistribution and use in source and binary forms, with or
12 * without modification, are permitted provided that the following
13 * conditions are met:
14 *
15 * 1. Redistributions of source code must retain the above
16 * copyright notice, this list of conditions and the following
17 * disclaimer.
18 *
19 * 2. Redistributions in binary form must reproduce the above
20 * copyright notice, this list of conditions and the following
21 * disclaimer in the documentation and/or other materials
22 * provided with the distribution.
23 *
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
28 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
29 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
30 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31 * SOFTWARE.
32 */
33
34 #include <linux/kernel.h>
35 #include <linux/slab.h>
36
37 #include "nfp.h"
38 #include "nfp_nsp.h"
39
40 struct nsp_identify {
41 u8 version[40];
42 u8 flags;
43 u8 br_primary;
44 u8 br_secondary;
45 u8 br_nsp;
46 __le16 primary;
47 __le16 secondary;
48 __le16 nsp;
49 u8 reserved[6];
50 __le64 sensor_mask;
51 };
52
__nfp_nsp_identify(struct nfp_nsp * nsp)53 struct nfp_nsp_identify *__nfp_nsp_identify(struct nfp_nsp *nsp)
54 {
55 struct nfp_nsp_identify *nspi = NULL;
56 struct nsp_identify *ni;
57 int ret;
58
59 if (nfp_nsp_get_abi_ver_minor(nsp) < 15)
60 return NULL;
61
62 ni = kzalloc(sizeof(*ni), GFP_KERNEL);
63 if (!ni)
64 return NULL;
65
66 ret = nfp_nsp_read_identify(nsp, ni, sizeof(*ni));
67 if (ret < 0) {
68 nfp_err(nfp_nsp_cpp(nsp), "reading bsp version failed %d\n",
69 ret);
70 goto exit_free;
71 }
72
73 nspi = kzalloc(sizeof(*nspi), GFP_KERNEL);
74 if (!nspi)
75 goto exit_free;
76
77 memcpy(nspi->version, ni->version, sizeof(nspi->version));
78 nspi->version[sizeof(nspi->version) - 1] = '\0';
79 nspi->flags = ni->flags;
80 nspi->br_primary = ni->br_primary;
81 nspi->br_secondary = ni->br_secondary;
82 nspi->br_nsp = ni->br_nsp;
83 nspi->primary = le16_to_cpu(ni->primary);
84 nspi->secondary = le16_to_cpu(ni->secondary);
85 nspi->nsp = le16_to_cpu(ni->nsp);
86 nspi->sensor_mask = le64_to_cpu(ni->sensor_mask);
87
88 exit_free:
89 kfree(ni);
90 return nspi;
91 }
92
93 struct nfp_sensors {
94 __le32 chip_temp;
95 __le32 assembly_power;
96 __le32 assembly_12v_power;
97 __le32 assembly_3v3_power;
98 };
99
nfp_hwmon_read_sensor(struct nfp_cpp * cpp,enum nfp_nsp_sensor_id id,long * val)100 int nfp_hwmon_read_sensor(struct nfp_cpp *cpp, enum nfp_nsp_sensor_id id,
101 long *val)
102 {
103 struct nfp_sensors s;
104 struct nfp_nsp *nsp;
105 int ret;
106
107 nsp = nfp_nsp_open(cpp);
108 if (IS_ERR(nsp))
109 return PTR_ERR(nsp);
110
111 ret = nfp_nsp_read_sensors(nsp, BIT(id), &s, sizeof(s));
112 nfp_nsp_close(nsp);
113
114 if (ret < 0)
115 return ret;
116
117 switch (id) {
118 case NFP_SENSOR_CHIP_TEMPERATURE:
119 *val = le32_to_cpu(s.chip_temp);
120 break;
121 case NFP_SENSOR_ASSEMBLY_POWER:
122 *val = le32_to_cpu(s.assembly_power);
123 break;
124 case NFP_SENSOR_ASSEMBLY_12V_POWER:
125 *val = le32_to_cpu(s.assembly_12v_power);
126 break;
127 case NFP_SENSOR_ASSEMBLY_3V3_POWER:
128 *val = le32_to_cpu(s.assembly_3v3_power);
129 break;
130 default:
131 return -EINVAL;
132 }
133 return 0;
134 }
135