File: sff-common.c

package info (click to toggle)
ethtool 1%3A6.19-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,004 kB
  • sloc: ansic: 66,691; sh: 97; makefile: 88; xml: 17
file content (189 lines) | stat: -rw-r--r-- 6,246 bytes parent folder | download | duplicates (2)
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
/*
 * sff-common.c: Implements SFF-8024 Rev 4.0 i.e. Specifcation
 * of pluggable I/O configuration
 *
 * Common utilities across SFF-8436/8636 and SFF-8472/8079
 * are defined in this file
 *
 * Copyright 2010 Solarflare Communications Inc.
 * Aurelien Guillaume <aurelien@iwi.me> (C) 2012
 * Copyright (C) 2014 Cumulus networks Inc.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Freeoftware Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 *  Vidya Sagar Ravipati <vidya@cumulusnetworks.com>
 *   This implementation is loosely based on current SFP parser
 *   and SFF-8024 Rev 4.0 spec (ftp://ftp.seagate.com/pub/sff/SFF-8024.PDF)
 *   by SFF Committee.
 */

#include <stdio.h>
#include <math.h>
#include "sff-common.h"

double convert_mw_to_dbm(double mw)
{
	return (10. * log10(mw / 1000.)) + 30.;
}

void sff_print_any_hex_field(const char *field_name,
			     const char *json_field_name, u8 value,
			     const char *desc)
{
	char desc_name[SFF_MAX_FIELD_LEN];

	if (is_json_context()) {
		print_uint(PRINT_JSON, json_field_name, "%u", value);
		if (desc) {
			snprintf(desc_name, SFF_MAX_FIELD_LEN,
				 "%s_description", json_field_name);
			print_string(PRINT_JSON, desc_name, "%s", desc);
		}
	} else {
		printf("\t%-41s : 0x%02x", field_name, value);
		if (desc)
			printf(" (%s)", desc);
		print_nl();
	}
}

void sff8024_show_encoding(const __u8 *id, int encoding_offset, int sff_type)
{
	char encoding_desc[64];

	switch (id[encoding_offset]) {
	case SFF8024_ENCODING_UNSPEC:
		strncpy(encoding_desc, "unspecified", 64);
		break;
	case SFF8024_ENCODING_8B10B:
		strncpy(encoding_desc, "8B/10B", 64);
		break;
	case SFF8024_ENCODING_4B5B:
		strncpy(encoding_desc, "4B/5B", 64);
		break;
	case SFF8024_ENCODING_NRZ:
		strncpy(encoding_desc, "NRZ", 64);
		break;
	case SFF8024_ENCODING_4h:
		if (sff_type == ETH_MODULE_SFF_8472)
			strncpy(encoding_desc, "Manchester", 64);
		else if (sff_type == ETH_MODULE_SFF_8636)
			strncpy(encoding_desc, "SONET Scrambled", 64);
		break;
	case SFF8024_ENCODING_5h:
		if (sff_type == ETH_MODULE_SFF_8472)
			strncpy(encoding_desc, "SONET Scrambled", 64);
		else if (sff_type == ETH_MODULE_SFF_8636)
			strncpy(encoding_desc, "64B/66B", 64);
		break;
	case SFF8024_ENCODING_6h:
		if (sff_type == ETH_MODULE_SFF_8472)
			strncpy(encoding_desc, "64B/66B", 64);
		else if (sff_type == ETH_MODULE_SFF_8636)
			strncpy(encoding_desc, "Manchester", 64);
		break;
	case SFF8024_ENCODING_256B:
		strncpy(encoding_desc,
			"256B/257B (transcoded FEC-enabled data)", 64);
		break;
	case SFF8024_ENCODING_PAM4:
		strncpy(encoding_desc, "PAM4", 64);
		break;
	default:
		strncpy(encoding_desc, "reserved or unknown", 64);
		break;
	}

	sff_print_any_hex_field("Encoding", "encoding", id[encoding_offset],
				encoding_desc);
}


void sff_show_thresholds_json(struct sff_diags sd)
{
	open_json_object("laser_bias_current");
	PRINT_BIAS_JSON("high_alarm_threshold", sd.bias_cur[HALRM]);
	PRINT_BIAS_JSON("low_alarm_threshold", sd.bias_cur[LALRM]);
	PRINT_BIAS_JSON("high_warning_threshold", sd.bias_cur[HWARN]);
	PRINT_BIAS_JSON("low_warning_threshold", sd.bias_cur[LWARN]);
	close_json_object();

	open_json_object("laser_output_power");
	PRINT_xX_PWR_JSON("high_alarm_threshold", sd.tx_power[HALRM]);
	PRINT_xX_PWR_JSON("low_alarm_threshold", sd.tx_power[LALRM]);
	PRINT_xX_PWR_JSON("high_warning_threshold", sd.tx_power[HWARN]);
	PRINT_xX_PWR_JSON("low_warning_threshold", sd.tx_power[LWARN]);
	close_json_object();

	open_json_object("module_temperature");
	PRINT_TEMP_JSON("high_alarm_threshold", sd.sfp_temp[HALRM]);
	PRINT_TEMP_JSON("low_alarm_threshold", sd.sfp_temp[LALRM]);
	PRINT_TEMP_JSON("high_warning_threshold", sd.sfp_temp[HWARN]);
	PRINT_TEMP_JSON("low_warning_threshold", sd.sfp_temp[LWARN]);
	close_json_object();

	open_json_object("module_voltage");
	PRINT_VCC_JSON("high_alarm_threshold", sd.sfp_voltage[HALRM]);
	PRINT_VCC_JSON("low_alarm_threshold", sd.sfp_voltage[LALRM]);
	PRINT_VCC_JSON("high_warning_threshold", sd.sfp_voltage[HWARN]);
	PRINT_VCC_JSON("low_warning_threshold", sd.sfp_voltage[LWARN]);
	close_json_object();

	open_json_object("laser_rx_power");
	PRINT_xX_PWR_JSON("high_alarm_threshold", sd.rx_power[HALRM]);
	PRINT_xX_PWR_JSON("low_alarm_threshold", sd.rx_power[LALRM]);
	PRINT_xX_PWR_JSON("high_warning_threshold", sd.rx_power[HWARN]);
	PRINT_xX_PWR_JSON("low_warning_threshold", sd.rx_power[LWARN]);
	close_json_object();
}

void sff_show_thresholds(struct sff_diags sd)
{
	PRINT_BIAS("Laser bias current high alarm threshold",
		   sd.bias_cur[HALRM]);
	PRINT_BIAS("Laser bias current low alarm threshold",
		   sd.bias_cur[LALRM]);
	PRINT_BIAS("Laser bias current high warning threshold",
		   sd.bias_cur[HWARN]);
	PRINT_BIAS("Laser bias current low warning threshold",
		   sd.bias_cur[LWARN]);

	PRINT_xX_PWR("Laser output power high alarm threshold",
		     sd.tx_power[HALRM]);
	PRINT_xX_PWR("Laser output power low alarm threshold",
		     sd.tx_power[LALRM]);
	PRINT_xX_PWR("Laser output power high warning threshold",
		     sd.tx_power[HWARN]);
	PRINT_xX_PWR("Laser output power low warning threshold",
		     sd.tx_power[LWARN]);

	PRINT_TEMP("Module temperature high alarm threshold",
		   sd.sfp_temp[HALRM]);
	PRINT_TEMP("Module temperature low alarm threshold",
		   sd.sfp_temp[LALRM]);
	PRINT_TEMP("Module temperature high warning threshold",
		   sd.sfp_temp[HWARN]);
	PRINT_TEMP("Module temperature low warning threshold",
		   sd.sfp_temp[LWARN]);

	PRINT_VCC("Module voltage high alarm threshold",
		  sd.sfp_voltage[HALRM]);
	PRINT_VCC("Module voltage low alarm threshold",
		  sd.sfp_voltage[LALRM]);
	PRINT_VCC("Module voltage high warning threshold",
		  sd.sfp_voltage[HWARN]);
	PRINT_VCC("Module voltage low warning threshold",
		  sd.sfp_voltage[LWARN]);

	PRINT_xX_PWR("Laser rx power high alarm threshold",
		     sd.rx_power[HALRM]);
	PRINT_xX_PWR("Laser rx power low alarm threshold",
		     sd.rx_power[LALRM]);
	PRINT_xX_PWR("Laser rx power high warning threshold",
		     sd.rx_power[HWARN]);
	PRINT_xX_PWR("Laser rx power low warning threshold",
		     sd.rx_power[LWARN]);
}