xref: /freebsd/sys/dev/sfxge/common/efx_mon.c (revision 895f86f15fbf6540071feb9328c3c50ed1f027b8)
1 /*-
2  * Copyright (c) 2007-2015 Solarflare Communications Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice,
9  *    this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright notice,
11  *    this list of conditions and the following disclaimer in the documentation
12  *    and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
16  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
18  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
21  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
22  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
24  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  *
26  * The views and conclusions contained in the software and documentation are
27  * those of the authors and should not be interpreted as representing official
28  * policies, either expressed or implied, of the FreeBSD Project.
29  */
30 
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
33 
34 #include "efsys.h"
35 #include "efx.h"
36 #include "efx_types.h"
37 #include "efx_regs.h"
38 #include "efx_impl.h"
39 
40 #if EFSYS_OPT_MON_NULL
41 #include "nullmon.h"
42 #endif
43 
44 #if EFSYS_OPT_MON_LM87
45 #include "lm87.h"
46 #endif
47 
48 #if EFSYS_OPT_MON_MAX6647
49 #include "max6647.h"
50 #endif
51 
52 #if EFSYS_OPT_MON_MCDI
53 #include "mcdi_mon.h"
54 #endif
55 
56 #if EFSYS_OPT_NAMES
57 
58 static const char	*__efx_mon_name[] = {
59 	"",
60 	"nullmon",
61 	"lm87",
62 	"max6647",
63 	"sfx90x0",
64 	"sfx91x0"
65 };
66 
67 		const char *
68 efx_mon_name(
69 	__in	efx_nic_t *enp)
70 {
71 	efx_nic_cfg_t *encp = &(enp->en_nic_cfg);
72 
73 	EFSYS_ASSERT3U(enp->en_magic, ==, EFX_NIC_MAGIC);
74 
75 	EFSYS_ASSERT(encp->enc_mon_type != EFX_MON_INVALID);
76 	EFSYS_ASSERT3U(encp->enc_mon_type, <, EFX_MON_NTYPES);
77 	return (__efx_mon_name[encp->enc_mon_type]);
78 }
79 
80 #endif	/* EFSYS_OPT_NAMES */
81 
82 #if EFSYS_OPT_MON_NULL
83 static efx_mon_ops_t	__efx_mon_null_ops = {
84 	nullmon_reset,			/* emo_reset */
85 	nullmon_reconfigure,		/* emo_reconfigure */
86 #if EFSYS_OPT_MON_STATS
87 	nullmon_stats_update		/* emo_stats_update */
88 #endif	/* EFSYS_OPT_MON_STATS */
89 };
90 #endif
91 
92 #if EFSYS_OPT_MON_LM87
93 static efx_mon_ops_t	__efx_mon_lm87_ops = {
94 	lm87_reset,			/* emo_reset */
95 	lm87_reconfigure,		/* emo_reconfigure */
96 #if EFSYS_OPT_MON_STATS
97 	lm87_stats_update		/* emo_stats_update */
98 #endif	/* EFSYS_OPT_MON_STATS */
99 };
100 #endif
101 
102 #if EFSYS_OPT_MON_MAX6647
103 static efx_mon_ops_t	__efx_mon_max6647_ops = {
104 	max6647_reset,			/* emo_reset */
105 	max6647_reconfigure,		/* emo_reconfigure */
106 #if EFSYS_OPT_MON_STATS
107 	max6647_stats_update		/* emo_stats_update */
108 #endif	/* EFSYS_OPT_MON_STATS */
109 };
110 #endif
111 
112 #if EFSYS_OPT_MON_MCDI
113 static efx_mon_ops_t	__efx_mon_mcdi_ops = {
114 	NULL,				/* emo_reset */
115 	NULL,				/* emo_reconfigure */
116 #if EFSYS_OPT_MON_STATS
117 	mcdi_mon_stats_update		/* emo_stats_update */
118 #endif	/* EFSYS_OPT_MON_STATS */
119 };
120 #endif
121 
122 static efx_mon_ops_t	*__efx_mon_ops[] = {
123 	NULL,
124 #if EFSYS_OPT_MON_NULL
125 	&__efx_mon_null_ops,
126 #else
127 	NULL,
128 #endif
129 #if EFSYS_OPT_MON_LM87
130 	&__efx_mon_lm87_ops,
131 #else
132 	NULL,
133 #endif
134 #if EFSYS_OPT_MON_MAX6647
135 	&__efx_mon_max6647_ops,
136 #else
137 	NULL,
138 #endif
139 #if EFSYS_OPT_MON_MCDI
140 	&__efx_mon_mcdi_ops,
141 #else
142 	NULL,
143 #endif
144 #if EFSYS_OPT_MON_MCDI
145 	&__efx_mon_mcdi_ops
146 #else
147 	NULL
148 #endif
149 };
150 
151 	__checkReturn	efx_rc_t
152 efx_mon_init(
153 	__in		efx_nic_t *enp)
154 {
155 	efx_nic_cfg_t *encp = &(enp->en_nic_cfg);
156 	efx_mon_t *emp = &(enp->en_mon);
157 	efx_mon_ops_t *emop;
158 	efx_rc_t rc;
159 
160 	EFSYS_ASSERT3U(enp->en_magic, ==, EFX_NIC_MAGIC);
161 	EFSYS_ASSERT3U(enp->en_mod_flags, &, EFX_MOD_PROBE);
162 
163 	if (enp->en_mod_flags & EFX_MOD_MON) {
164 		rc = EINVAL;
165 		goto fail1;
166 	}
167 
168 	enp->en_mod_flags |= EFX_MOD_MON;
169 
170 	emp->em_type = encp->enc_mon_type;
171 
172 	EFSYS_ASSERT(encp->enc_mon_type != EFX_MON_INVALID);
173 	EFSYS_ASSERT3U(emp->em_type, <, EFX_MON_NTYPES);
174 	if ((emop = (efx_mon_ops_t *)__efx_mon_ops[emp->em_type]) == NULL) {
175 		rc = ENOTSUP;
176 		goto fail2;
177 	}
178 
179 	if (emop->emo_reset != NULL) {
180 		if ((rc = emop->emo_reset(enp)) != 0)
181 			goto fail3;
182 	}
183 
184 	if (emop->emo_reconfigure != NULL) {
185 		if ((rc = emop->emo_reconfigure(enp)) != 0)
186 			goto fail4;
187 	}
188 
189 	emp->em_emop = emop;
190 	return (0);
191 
192 fail4:
193 	EFSYS_PROBE(fail5);
194 
195 	if (emop->emo_reset != NULL)
196 		(void) emop->emo_reset(enp);
197 
198 fail3:
199 	EFSYS_PROBE(fail4);
200 fail2:
201 	EFSYS_PROBE(fail3);
202 
203 	emp->em_type = EFX_MON_INVALID;
204 
205 	enp->en_mod_flags &= ~EFX_MOD_MON;
206 
207 fail1:
208 	EFSYS_PROBE1(fail1, efx_rc_t, rc);
209 
210 	return (rc);
211 }
212 
213 #if EFSYS_OPT_MON_STATS
214 
215 #if EFSYS_OPT_NAMES
216 
217 /* START MKCONFIG GENERATED MonitorStatNamesBlock b9328f15438c4d01 */
218 static const char 	*__mon_stat_name[] = {
219 	"value_2_5v",
220 	"value_vccp1",
221 	"value_vcc",
222 	"value_5v",
223 	"value_12v",
224 	"value_vccp2",
225 	"value_ext_temp",
226 	"value_int_temp",
227 	"value_ain1",
228 	"value_ain2",
229 	"controller_cooling",
230 	"ext_cooling",
231 	"1v",
232 	"1_2v",
233 	"1_8v",
234 	"3_3v",
235 	"1_2va",
236 	"vref",
237 	"vaoe",
238 	"aoe_temperature",
239 	"psu_aoe_temperature",
240 	"psu_temperature",
241 	"fan0",
242 	"fan1",
243 	"fan2",
244 	"fan3",
245 	"fan4",
246 	"vaoe_in",
247 	"iaoe",
248 	"iaoe_in",
249 	"nic_power",
250 	"0_9v",
251 	"i0_9v",
252 	"i1_2v",
253 	"0_9v_adc",
254 	"controller_temperature2",
255 	"vreg_temperature",
256 	"vreg_0_9v_temperature",
257 	"vreg_1_2v_temperature",
258 	"int_vptat",
259 	"controller_internal_adc_temperature",
260 	"ext_vptat",
261 	"controller_external_adc_temperature",
262 	"ambient_temperature",
263 	"airflow",
264 	"vdd08d_vss08d_csr",
265 	"vdd08d_vss08d_csr_extadc",
266 	"hotpoint_temperature",
267 	"phy_power_switch_port0",
268 	"phy_power_switch_port1",
269 	"mum_vcc",
270 	"0v9_a",
271 	"i0v9_a",
272 	"0v9_a_temp",
273 	"0v9_b",
274 	"i0v9_b",
275 	"0v9_b_temp",
276 	"ccom_avreg_1v2_supply",
277 	"ccom_avreg_1v2_supply_ext_adc",
278 	"ccom_avreg_1v8_supply",
279 	"ccom_avreg_1v8_supply_ext_adc",
280 	"controller_master_vptat",
281 	"controller_master_internal_temp",
282 	"controller_master_vptat_ext_adc",
283 	"controller_master_internal_temp_ext_adc",
284 	"controller_slave_vptat",
285 	"controller_slave_internal_temp",
286 	"controller_slave_vptat_ext_adc",
287 	"controller_slave_internal_temp_ext_adc",
288 };
289 
290 /* END MKCONFIG GENERATED MonitorStatNamesBlock */
291 
292 extern					const char *
293 efx_mon_stat_name(
294 	__in				efx_nic_t *enp,
295 	__in				efx_mon_stat_t id)
296 {
297 	_NOTE(ARGUNUSED(enp))
298 	EFSYS_ASSERT3U(enp->en_magic, ==, EFX_NIC_MAGIC);
299 
300 	EFSYS_ASSERT3U(id, <, EFX_MON_NSTATS);
301 	return (__mon_stat_name[id]);
302 }
303 
304 #endif	/* EFSYS_OPT_NAMES */
305 
306 	__checkReturn			efx_rc_t
307 efx_mon_stats_update(
308 	__in				efx_nic_t *enp,
309 	__in				efsys_mem_t *esmp,
310 	__inout_ecount(EFX_MON_NSTATS)	efx_mon_stat_value_t *values)
311 {
312 	efx_mon_t *emp = &(enp->en_mon);
313 	efx_mon_ops_t *emop = emp->em_emop;
314 
315 	EFSYS_ASSERT3U(enp->en_magic, ==, EFX_NIC_MAGIC);
316 	EFSYS_ASSERT3U(enp->en_mod_flags, &, EFX_MOD_MON);
317 
318 	return (emop->emo_stats_update(enp, esmp, values));
319 }
320 
321 #endif	/* EFSYS_OPT_MON_STATS */
322 
323 		void
324 efx_mon_fini(
325 	__in	efx_nic_t *enp)
326 {
327 	efx_mon_t *emp = &(enp->en_mon);
328 	efx_mon_ops_t *emop = emp->em_emop;
329 	efx_rc_t rc;
330 
331 	EFSYS_ASSERT3U(enp->en_magic, ==, EFX_NIC_MAGIC);
332 	EFSYS_ASSERT3U(enp->en_mod_flags, &, EFX_MOD_PROBE);
333 	EFSYS_ASSERT3U(enp->en_mod_flags, &, EFX_MOD_MON);
334 
335 	emp->em_emop = NULL;
336 
337 	if (emop->emo_reset != NULL) {
338 		rc = emop->emo_reset(enp);
339 		if (rc != 0)
340 			EFSYS_PROBE1(fail1, efx_rc_t, rc);
341 	}
342 
343 	emp->em_type = EFX_MON_INVALID;
344 
345 	enp->en_mod_flags &= ~EFX_MOD_MON;
346 }
347