Skip to content

Commit eefbde7

Browse files
David Bruneczdavem330
authored andcommitted
nfp: add hwmon support
Add support for retrieving temperature and power sensor and limits via NSP. Signed-off-by: David Brunecz <[email protected]> Signed-off-by: Jakub Kicinski <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent a87853f commit eefbde7

File tree

8 files changed

+286
-7
lines changed

8 files changed

+286
-7
lines changed

drivers/net/ethernet/netronome/nfp/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ nfp-objs := \
1616
nfpcore/nfp_target.o \
1717
nfp_app.o \
1818
nfp_devlink.o \
19+
nfp_hwmon.o \
1920
nfp_main.o \
2021
nfp_net_common.o \
2122
nfp_net_ethtool.o \
Lines changed: 192 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,192 @@
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/bitops.h>
36+
#include <linux/hwmon.h>
37+
38+
#include "nfpcore/nfp_cpp.h"
39+
#include "nfpcore/nfp_nsp.h"
40+
#include "nfp_main.h"
41+
42+
#define NFP_TEMP_MAX (95 * 1000)
43+
#define NFP_TEMP_CRIT (105 * 1000)
44+
45+
#define NFP_POWER_MAX (25 * 1000 * 1000)
46+
47+
static int nfp_hwmon_sensor_id(enum hwmon_sensor_types type, int channel)
48+
{
49+
if (type == hwmon_temp)
50+
return NFP_SENSOR_CHIP_TEMPERATURE;
51+
if (type == hwmon_power)
52+
return NFP_SENSOR_ASSEMBLY_POWER + channel;
53+
return -EINVAL;
54+
}
55+
56+
static int
57+
nfp_hwmon_read(struct device *dev, enum hwmon_sensor_types type, u32 attr,
58+
int channel, long *val)
59+
{
60+
static const struct {
61+
enum hwmon_sensor_types type;
62+
u32 attr;
63+
long val;
64+
} const_vals[] = {
65+
{ hwmon_temp, hwmon_temp_max, NFP_TEMP_MAX },
66+
{ hwmon_temp, hwmon_temp_crit, NFP_TEMP_CRIT },
67+
{ hwmon_power, hwmon_power_max, NFP_POWER_MAX },
68+
};
69+
struct nfp_pf *pf = dev_get_drvdata(dev);
70+
enum nfp_nsp_sensor_id id;
71+
int err, i;
72+
73+
for (i = 0; i < ARRAY_SIZE(const_vals); i++)
74+
if (const_vals[i].type == type && const_vals[i].attr == attr) {
75+
*val = const_vals[i].val;
76+
return 0;
77+
}
78+
79+
err = nfp_hwmon_sensor_id(type, channel);
80+
if (err < 0)
81+
return err;
82+
id = err;
83+
84+
if (!(pf->nspi->sensor_mask & BIT(id)))
85+
return -EOPNOTSUPP;
86+
87+
if (type == hwmon_temp && attr == hwmon_temp_input)
88+
return nfp_hwmon_read_sensor(pf->cpp, id, val);
89+
if (type == hwmon_power && attr == hwmon_power_input)
90+
return nfp_hwmon_read_sensor(pf->cpp, id, val);
91+
92+
return -EINVAL;
93+
}
94+
95+
static umode_t
96+
nfp_hwmon_is_visible(const void *data, enum hwmon_sensor_types type, u32 attr,
97+
int channel)
98+
{
99+
if (type == hwmon_temp) {
100+
switch (attr) {
101+
case hwmon_temp_input:
102+
case hwmon_temp_crit:
103+
case hwmon_temp_max:
104+
return 0444;
105+
}
106+
} else if (type == hwmon_power) {
107+
switch (attr) {
108+
case hwmon_power_input:
109+
case hwmon_power_max:
110+
return 0444;
111+
}
112+
}
113+
return 0;
114+
}
115+
116+
static u32 nfp_chip_config[] = {
117+
HWMON_C_REGISTER_TZ,
118+
0
119+
};
120+
121+
static const struct hwmon_channel_info nfp_chip = {
122+
.type = hwmon_chip,
123+
.config = nfp_chip_config,
124+
};
125+
126+
static u32 nfp_temp_config[] = {
127+
HWMON_T_INPUT | HWMON_T_MAX | HWMON_T_CRIT,
128+
0
129+
};
130+
131+
static const struct hwmon_channel_info nfp_temp = {
132+
.type = hwmon_temp,
133+
.config = nfp_temp_config,
134+
};
135+
136+
static u32 nfp_power_config[] = {
137+
HWMON_P_INPUT | HWMON_P_MAX,
138+
HWMON_P_INPUT,
139+
HWMON_P_INPUT,
140+
0
141+
};
142+
143+
static const struct hwmon_channel_info nfp_power = {
144+
.type = hwmon_power,
145+
.config = nfp_power_config,
146+
};
147+
148+
static const struct hwmon_channel_info *nfp_hwmon_info[] = {
149+
&nfp_chip,
150+
&nfp_temp,
151+
&nfp_power,
152+
NULL
153+
};
154+
155+
static const struct hwmon_ops nfp_hwmon_ops = {
156+
.is_visible = nfp_hwmon_is_visible,
157+
.read = nfp_hwmon_read,
158+
};
159+
160+
static const struct hwmon_chip_info nfp_chip_info = {
161+
.ops = &nfp_hwmon_ops,
162+
.info = nfp_hwmon_info,
163+
};
164+
165+
int nfp_hwmon_register(struct nfp_pf *pf)
166+
{
167+
if (!IS_REACHABLE(CONFIG_HWMON))
168+
return 0;
169+
170+
if (!pf->nspi) {
171+
nfp_warn(pf->cpp, "not registering HWMON (no NSP info)\n");
172+
return 0;
173+
}
174+
if (!pf->nspi->sensor_mask) {
175+
nfp_info(pf->cpp,
176+
"not registering HWMON (NSP doesn't report sensors)\n");
177+
return 0;
178+
}
179+
180+
pf->hwmon_dev = hwmon_device_register_with_info(&pf->pdev->dev, "nfp",
181+
pf, &nfp_chip_info,
182+
NULL);
183+
return PTR_ERR_OR_ZERO(pf->hwmon_dev);
184+
}
185+
186+
void nfp_hwmon_unregister(struct nfp_pf *pf)
187+
{
188+
if (!IS_REACHABLE(CONFIG_HWMON) || !pf->hwmon_dev)
189+
return;
190+
191+
hwmon_device_unregister(pf->hwmon_dev);
192+
}

drivers/net/ethernet/netronome/nfp/nfp_main.c

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,6 @@ nfp_fw_load(struct pci_dev *pdev, struct nfp_pf *pf, struct nfp_nsp *nsp)
257257

258258
static int nfp_nsp_init(struct pci_dev *pdev, struct nfp_pf *pf)
259259
{
260-
struct nfp_nsp_identify *nspi;
261260
struct nfp_nsp *nsp;
262261
int err;
263262

@@ -274,11 +273,9 @@ static int nfp_nsp_init(struct pci_dev *pdev, struct nfp_pf *pf)
274273

275274
pf->eth_tbl = __nfp_eth_read_ports(pf->cpp, nsp);
276275

277-
nspi = __nfp_nsp_identify(nsp);
278-
if (nspi) {
279-
dev_info(&pdev->dev, "BSP: %s\n", nspi->version);
280-
kfree(nspi);
281-
}
276+
pf->nspi = __nfp_nsp_identify(nsp);
277+
if (pf->nspi)
278+
dev_info(&pdev->dev, "BSP: %s\n", pf->nspi->version);
282279

283280
err = nfp_fw_load(pdev, pf, nsp);
284281
if (err < 0) {
@@ -383,14 +380,23 @@ static int nfp_pci_probe(struct pci_dev *pdev,
383380
if (err)
384381
goto err_sriov_unlimit;
385382

383+
err = nfp_hwmon_register(pf);
384+
if (err) {
385+
dev_err(&pdev->dev, "Failed to register hwmon info\n");
386+
goto err_net_remove;
387+
}
388+
386389
return 0;
387390

391+
err_net_remove:
392+
nfp_net_pci_remove(pf);
388393
err_sriov_unlimit:
389394
pci_sriov_set_totalvfs(pf->pdev, 0);
390395
err_fw_unload:
391396
if (pf->fw_loaded)
392397
nfp_fw_unload(pf);
393398
kfree(pf->eth_tbl);
399+
kfree(pf->nspi);
394400
err_devlink_unreg:
395401
devlink_unregister(devlink);
396402
err_cpp_free:
@@ -412,6 +418,8 @@ static void nfp_pci_remove(struct pci_dev *pdev)
412418
struct nfp_pf *pf = pci_get_drvdata(pdev);
413419
struct devlink *devlink;
414420

421+
nfp_hwmon_unregister(pf);
422+
415423
devlink = priv_to_devlink(pf);
416424

417425
nfp_net_pci_remove(pf);
@@ -428,6 +436,7 @@ static void nfp_pci_remove(struct pci_dev *pdev)
428436
nfp_cpp_free(pf->cpp);
429437

430438
kfree(pf->eth_tbl);
439+
kfree(pf->nspi);
431440
mutex_destroy(&pf->lock);
432441
devlink_free(devlink);
433442
pci_release_regions(pdev);

drivers/net/ethernet/netronome/nfp/nfp_main.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,14 @@
4747
#include <linux/workqueue.h>
4848

4949
struct dentry;
50+
struct device;
5051
struct devlink_ops;
5152
struct pci_dev;
5253

5354
struct nfp_cpp;
5455
struct nfp_cpp_area;
5556
struct nfp_eth_table;
57+
struct nfp_nsp_identify;
5658

5759
/**
5860
* struct nfp_pf - NFP PF-specific device structure
@@ -67,6 +69,8 @@ struct nfp_eth_table;
6769
* @num_vfs: Number of SR-IOV VFs enabled
6870
* @fw_loaded: Is the firmware loaded?
6971
* @eth_tbl: NSP ETH table
72+
* @nspi: NSP identification info
73+
* @hwmon_dev: pointer to hwmon device
7074
* @ddir: Per-device debugfs directory
7175
* @max_data_vnics: Number of data vNICs app firmware supports
7276
* @num_vnics: Number of vNICs spawned
@@ -94,6 +98,9 @@ struct nfp_pf {
9498
bool fw_loaded;
9599

96100
struct nfp_eth_table *eth_tbl;
101+
struct nfp_nsp_identify *nspi;
102+
103+
struct device *hwmon_dev;
97104

98105
struct dentry *ddir;
99106

@@ -113,4 +120,7 @@ extern const struct devlink_ops nfp_devlink_ops;
113120
int nfp_net_pci_probe(struct nfp_pf *pf);
114121
void nfp_net_pci_remove(struct nfp_pf *pf);
115122

123+
int nfp_hwmon_register(struct nfp_pf *pf);
124+
void nfp_hwmon_unregister(struct nfp_pf *pf);
125+
116126
#endif /* NFP_MAIN_H */

drivers/net/ethernet/netronome/nfp/nfpcore/nfp.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ int nfp_nsp_read_eth_table(struct nfp_nsp *state, void *buf, unsigned int size);
6464
int nfp_nsp_write_eth_table(struct nfp_nsp *state,
6565
const void *buf, unsigned int size);
6666
int nfp_nsp_read_identify(struct nfp_nsp *state, void *buf, unsigned int size);
67+
int nfp_nsp_read_sensors(struct nfp_nsp *state, unsigned int sensor_mask,
68+
void *buf, unsigned int size);
6769

6870
/* Implemented in nfp_resource.c */
6971

drivers/net/ethernet/netronome/nfp/nfpcore/nfp_nsp.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ enum nfp_nsp_cmd {
9393
SPCODE_FW_LOAD = 6, /* Load fw from buffer, len in option */
9494
SPCODE_ETH_RESCAN = 7, /* Rescan ETHs, write ETH_TABLE to buf */
9595
SPCODE_ETH_CONTROL = 8, /* Update media config from buffer */
96+
SPCODE_NSP_SENSORS = 12, /* Read NSP sensor(s) */
9697
SPCODE_NSP_IDENTIFY = 13, /* Read NSP version */
9798
};
9899

@@ -506,3 +507,10 @@ int nfp_nsp_read_identify(struct nfp_nsp *state, void *buf, unsigned int size)
506507
return nfp_nsp_command_buf(state, SPCODE_NSP_IDENTIFY, size, NULL, 0,
507508
buf, size);
508509
}
510+
511+
int nfp_nsp_read_sensors(struct nfp_nsp *state, unsigned int sensor_mask,
512+
void *buf, unsigned int size)
513+
{
514+
return nfp_nsp_command_buf(state, SPCODE_NSP_SENSORS, sensor_mask,
515+
NULL, 0, buf, size);
516+
}

drivers/net/ethernet/netronome/nfp/nfpcore/nfp_nsp.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ int __nfp_eth_set_split(struct nfp_nsp *nsp, unsigned int lanes);
160160
* @primary: version of primarary bootloader
161161
* @secondary: version id of secondary bootloader
162162
* @nsp: version id of NSP
163+
* @sensor_mask: mask of present sensors available on NIC
163164
*/
164165
struct nfp_nsp_identify {
165166
char version[40];
@@ -170,8 +171,19 @@ struct nfp_nsp_identify {
170171
u16 primary;
171172
u16 secondary;
172173
u16 nsp;
174+
u64 sensor_mask;
173175
};
174176

175177
struct nfp_nsp_identify *__nfp_nsp_identify(struct nfp_nsp *nsp);
176178

179+
enum nfp_nsp_sensor_id {
180+
NFP_SENSOR_CHIP_TEMPERATURE,
181+
NFP_SENSOR_ASSEMBLY_POWER,
182+
NFP_SENSOR_ASSEMBLY_12V_POWER,
183+
NFP_SENSOR_ASSEMBLY_3V3_POWER,
184+
};
185+
186+
int nfp_hwmon_read_sensor(struct nfp_cpp *cpp, enum nfp_nsp_sensor_id id,
187+
long *val);
188+
177189
#endif

0 commit comments

Comments
 (0)