summaryrefslogtreecommitdiffstats
path: root/drivers/scsi/mpt3sas/mpt3sas_hwmon.c
blob: 297a8efecadbdbf05cd57767ff42c909fba97020 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
// SPDX-License-Identifier: GPL-2.0
/*
 * Hardware monitoring (hwmon) support for the LSI / Broadcom mpt3sas
 * SAS HBA driver. Exposes the IOC and board temperature sensors by
 * reading MPI IO Unit Page 7.
 */

#include <linux/err.h>
#include <linux/hwmon.h>
#include <linux/slab.h>

#include "mpt3sas_base.h"

struct mpt3sas_hwmon {
	struct MPT3SAS_ADAPTER *ioc;
	struct device *hwmon_dev;
	bool ioc_present;
	bool board_present;
};

/*
 * Convert a (raw, units) reading to millidegrees Celsius.
 * Returns -ENODATA when the sensor reports "not present" or
 * unknown units. Temperature values are interpreted as signed
 * two's-complement integers.
 *
 * The MPI2_IOUNITPAGE7_IOC_TEMP_* and MPI2_IOUNITPAGE7_BOARD_TEMP_*
 * defines in mpi2_cnfg.h share the same values; the IOC ones are
 * used for both channels.
 */
static int _hwmon_to_mdegc(s16 raw, u8 units, long *out)
{
	switch (units) {
	case MPI2_IOUNITPAGE7_IOC_TEMP_CELSIUS:
		*out = (long)raw * 1000;
		return 0;
	case MPI2_IOUNITPAGE7_IOC_TEMP_FAHRENHEIT:
		/* (F - 32) * 5 / 9, expressed in milli-units */
		*out = ((long)raw - 32) * 5000 / 9;
		return 0;
	default:
		return -ENODATA;
	}
}

static umode_t _hwmon_is_visible(const void *drvdata,
				 enum hwmon_sensor_types type,
				 u32 attr, int channel)
{
	const struct mpt3sas_hwmon *h = drvdata;

	if (type != hwmon_temp)
		return 0;
	if (attr != hwmon_temp_input && attr != hwmon_temp_label)
		return 0;
	if (channel == 0 && h->ioc_present)
		return 0444;
	if (channel == 1 && h->board_present)
		return 0444;
	return 0;
}

static int _hwmon_read(struct device *dev, enum hwmon_sensor_types type,
		       u32 attr, int channel, long *val)
{
	struct mpt3sas_hwmon *h = dev_get_drvdata(dev);
	Mpi2ConfigReply_t mpi_reply;
	Mpi2IOUnitPage7_t page;
	int r;

	if (type != hwmon_temp || attr != hwmon_temp_input)
		return -EOPNOTSUPP;

	r = mpt3sas_config_get_iounit_pg7(h->ioc, &mpi_reply, &page);
	if (r)
		return r;

	if (channel == 0)
		return _hwmon_to_mdegc((s16)le16_to_cpu(page.IOCTemperature),
				       page.IOCTemperatureUnits, val);
	if (channel == 1)
		return _hwmon_to_mdegc((s16)le16_to_cpu(page.BoardTemperature),
				       page.BoardTemperatureUnits, val);
	return -EOPNOTSUPP;
}

static const char * const mpt3sas_hwmon_temp_labels[] = {
	"IOC",
	"Board",
};

static int _hwmon_read_string(struct device *dev,
			      enum hwmon_sensor_types type,
			      u32 attr, int channel, const char **str)
{
	if (type != hwmon_temp || attr != hwmon_temp_label)
		return -EOPNOTSUPP;
	*str = mpt3sas_hwmon_temp_labels[channel];
	return 0;
}

static const struct hwmon_channel_info * const mpt3sas_hwmon_info[] = {
	HWMON_CHANNEL_INFO(temp,
			   HWMON_T_INPUT | HWMON_T_LABEL,
			   HWMON_T_INPUT | HWMON_T_LABEL),
	NULL,
};

static const struct hwmon_ops mpt3sas_hwmon_ops = {
	.is_visible	= _hwmon_is_visible,
	.read		= _hwmon_read,
	.read_string	= _hwmon_read_string,
};

static const struct hwmon_chip_info mpt3sas_hwmon_chip_info = {
	.ops	= &mpt3sas_hwmon_ops,
	.info	= mpt3sas_hwmon_info,
};

/**
 * mpt3sas_hwmon_register - register an hwmon device for the IOC
 * @ioc: per adapter object
 * Context: sleep.
 *
 * Succeeds without registering when no temperature sensors are present,
 * so cards without thermal monitoring do not expose an empty hwmon node.
 * Paired with mpt3sas_hwmon_unregister() from the driver's remove path.
 *
 * Return: 0 for success, non-zero for failure.
 */
int mpt3sas_hwmon_register(struct MPT3SAS_ADAPTER *ioc)
{
	struct device *parent = &ioc->pdev->dev;
	struct mpt3sas_hwmon *h;
	struct device *hwdev;
	Mpi2ConfigReply_t mpi_reply;
	Mpi2IOUnitPage7_t page;
	int r;

	r = mpt3sas_config_get_iounit_pg7(ioc, &mpi_reply, &page);
	if (r)
		return r;

	/*
	 * A page where both *TemperatureUnits are NOT_PRESENT covers
	 * two cases: cards that genuinely lack sensors, and firmware
	 * errors that left the page zero-filled (the accessor mirrors
	 * _config_request() behaviour). Either way: skip registration.
	 */
	if (page.IOCTemperatureUnits == MPI2_IOUNITPAGE7_IOC_TEMP_NOT_PRESENT &&
	    page.BoardTemperatureUnits == MPI2_IOUNITPAGE7_BOARD_TEMP_NOT_PRESENT)
		return 0;

	h = kzalloc_obj(*h);
	if (!h)
		return -ENOMEM;

	h->ioc = ioc;
	h->ioc_present = page.IOCTemperatureUnits != MPI2_IOUNITPAGE7_IOC_TEMP_NOT_PRESENT;
	h->board_present = page.BoardTemperatureUnits != MPI2_IOUNITPAGE7_BOARD_TEMP_NOT_PRESENT;

	hwdev = hwmon_device_register_with_info(parent, "mpt3sas", h,
						&mpt3sas_hwmon_chip_info,
						NULL);
	if (IS_ERR(hwdev)) {
		kfree(h);
		return PTR_ERR(hwdev);
	}

	h->hwmon_dev = hwdev;
	ioc->hwmon = h;
	return 0;
}

/**
 * mpt3sas_hwmon_unregister - tear down the hwmon device, if any
 * @ioc: per adapter object
 *
 * Safe to call when registration was skipped (no sensors) or
 * failed; in those cases ioc->hwmon is NULL and this is a no-op.
 */
void mpt3sas_hwmon_unregister(struct MPT3SAS_ADAPTER *ioc)
{
	struct mpt3sas_hwmon *h = ioc->hwmon;

	if (!h)
		return;
	hwmon_device_unregister(h->hwmon_dev);
	kfree(h);
	ioc->hwmon = NULL;
}