eeb6b1ef198f8f34ec31fa49067b3c6891619560
[openwrt/openwrt.git] / target / linux / mvebu / patches-4.14 / 402-sfp-display-SFP-module-information.patch
1 From e76632d118659347d9261a4470d9f60bfbe0044c Mon Sep 17 00:00:00 2001
2 From: Russell King <rmk+kernel@arm.linux.org.uk>
3 Date: Sun, 13 Sep 2015 01:06:31 +0100
4 Subject: sfp: display SFP module information
5
6 Signed-off-by: Russell King <rmk+kernel@arm.linux.org.uk>
7 ---
8 drivers/net/phy/sfp.c | 254 +++++++++++++++++++++++++++++++++++++++++++++++++-
9 1 file changed, 253 insertions(+), 1 deletion(-)
10
11 --- a/drivers/net/phy/sfp.c
12 +++ b/drivers/net/phy/sfp.c
13 @@ -248,6 +248,184 @@ static unsigned int sfp_check(void *buf,
14 return check;
15 }
16
17 +static const char *sfp_link_len(char *buf, size_t size, unsigned int length,
18 + unsigned int multiplier)
19 +{
20 + if (length == 0)
21 + return "unsupported/unspecified";
22 +
23 + if (length == 255) {
24 + *buf++ = '>';
25 + size -= 1;
26 + length -= 1;
27 + }
28 +
29 + length *= multiplier;
30 +
31 + if (length >= 1000)
32 + snprintf(buf, size, "%u.%0*ukm",
33 + length / 1000,
34 + multiplier > 100 ? 1 :
35 + multiplier > 10 ? 2 : 3,
36 + length % 1000);
37 + else
38 + snprintf(buf, size, "%um", length);
39 +
40 + return buf;
41 +}
42 +
43 +struct bitfield {
44 + unsigned int mask;
45 + unsigned int val;
46 + const char *str;
47 +};
48 +
49 +static const struct bitfield sfp_options[] = {
50 + {
51 + .mask = SFP_OPTIONS_HIGH_POWER_LEVEL,
52 + .val = SFP_OPTIONS_HIGH_POWER_LEVEL,
53 + .str = "hpl",
54 + }, {
55 + .mask = SFP_OPTIONS_PAGING_A2,
56 + .val = SFP_OPTIONS_PAGING_A2,
57 + .str = "paginga2",
58 + }, {
59 + .mask = SFP_OPTIONS_RETIMER,
60 + .val = SFP_OPTIONS_RETIMER,
61 + .str = "retimer",
62 + }, {
63 + .mask = SFP_OPTIONS_COOLED_XCVR,
64 + .val = SFP_OPTIONS_COOLED_XCVR,
65 + .str = "cooled",
66 + }, {
67 + .mask = SFP_OPTIONS_POWER_DECL,
68 + .val = SFP_OPTIONS_POWER_DECL,
69 + .str = "powerdecl",
70 + }, {
71 + .mask = SFP_OPTIONS_RX_LINEAR_OUT,
72 + .val = SFP_OPTIONS_RX_LINEAR_OUT,
73 + .str = "rxlinear",
74 + }, {
75 + .mask = SFP_OPTIONS_RX_DECISION_THRESH,
76 + .val = SFP_OPTIONS_RX_DECISION_THRESH,
77 + .str = "rxthresh",
78 + }, {
79 + .mask = SFP_OPTIONS_TUNABLE_TX,
80 + .val = SFP_OPTIONS_TUNABLE_TX,
81 + .str = "tunabletx",
82 + }, {
83 + .mask = SFP_OPTIONS_RATE_SELECT,
84 + .val = SFP_OPTIONS_RATE_SELECT,
85 + .str = "ratesel",
86 + }, {
87 + .mask = SFP_OPTIONS_TX_DISABLE,
88 + .val = SFP_OPTIONS_TX_DISABLE,
89 + .str = "txdisable",
90 + }, {
91 + .mask = SFP_OPTIONS_TX_FAULT,
92 + .val = SFP_OPTIONS_TX_FAULT,
93 + .str = "txfault",
94 + }, {
95 + .mask = SFP_OPTIONS_LOS_INVERTED,
96 + .val = SFP_OPTIONS_LOS_INVERTED,
97 + .str = "los-",
98 + }, {
99 + .mask = SFP_OPTIONS_LOS_NORMAL,
100 + .val = SFP_OPTIONS_LOS_NORMAL,
101 + .str = "los+",
102 + }, { }
103 +};
104 +
105 +static const struct bitfield diagmon[] = {
106 + {
107 + .mask = SFP_DIAGMON_DDM,
108 + .val = SFP_DIAGMON_DDM,
109 + .str = "ddm",
110 + }, {
111 + .mask = SFP_DIAGMON_INT_CAL,
112 + .val = SFP_DIAGMON_INT_CAL,
113 + .str = "intcal",
114 + }, {
115 + .mask = SFP_DIAGMON_EXT_CAL,
116 + .val = SFP_DIAGMON_EXT_CAL,
117 + .str = "extcal",
118 + }, {
119 + .mask = SFP_DIAGMON_RXPWR_AVG,
120 + .val = SFP_DIAGMON_RXPWR_AVG,
121 + .str = "rxpwravg",
122 + }, { }
123 +};
124 +
125 +static const char *sfp_bitfield(char *out, size_t outsz, const struct bitfield *bits, unsigned int val)
126 +{
127 + char *p = out;
128 + int n;
129 +
130 + *p = '\0';
131 + while (bits->mask) {
132 + if ((val & bits->mask) == bits->val) {
133 + n = snprintf(p, outsz, "%s%s",
134 + out != p ? ", " : "",
135 + bits->str);
136 + if (n == outsz)
137 + break;
138 + p += n;
139 + outsz -= n;
140 + }
141 + bits++;
142 + }
143 +
144 + return out;
145 +}
146 +
147 +static const char *sfp_connector(unsigned int connector)
148 +{
149 + switch (connector) {
150 + case SFP_CONNECTOR_UNSPEC:
151 + return "unknown/unspecified";
152 + case SFP_CONNECTOR_SC:
153 + return "SC";
154 + case SFP_CONNECTOR_FIBERJACK:
155 + return "Fiberjack";
156 + case SFP_CONNECTOR_LC:
157 + return "LC";
158 + case SFP_CONNECTOR_MT_RJ:
159 + return "MT-RJ";
160 + case SFP_CONNECTOR_MU:
161 + return "MU";
162 + case SFP_CONNECTOR_SG:
163 + return "SG";
164 + case SFP_CONNECTOR_OPTICAL_PIGTAIL:
165 + return "Optical pigtail";
166 + case SFP_CONNECTOR_HSSDC_II:
167 + return "HSSDC II";
168 + case SFP_CONNECTOR_COPPER_PIGTAIL:
169 + return "Copper pigtail";
170 + default:
171 + return "unknown";
172 + }
173 +}
174 +
175 +static const char *sfp_encoding(unsigned int encoding)
176 +{
177 + switch (encoding) {
178 + case SFP_ENCODING_UNSPEC:
179 + return "unspecified";
180 + case SFP_ENCODING_8472_64B66B:
181 + return "64b66b";
182 + case SFP_ENCODING_8B10B:
183 + return "8b10b";
184 + case SFP_ENCODING_4B5B:
185 + return "4b5b";
186 + case SFP_ENCODING_NRZ:
187 + return "NRZ";
188 + case SFP_ENCODING_8472_MANCHESTER:
189 + return "MANCHESTER";
190 + default:
191 + return "unknown";
192 + }
193 +}
194 +
195 /* Helpers */
196 static void sfp_module_tx_disable(struct sfp *sfp)
197 {
198 @@ -416,6 +594,7 @@ static int sfp_sm_mod_probe(struct sfp *
199 char sn[17];
200 char date[9];
201 char rev[5];
202 + char options[80];
203 u8 check;
204 int err;
205
206 @@ -459,10 +638,83 @@ static int sfp_sm_mod_probe(struct sfp *
207 rev[4] = '\0';
208 memcpy(sn, sfp->id.ext.vendor_sn, 16);
209 sn[16] = '\0';
210 - memcpy(date, sfp->id.ext.datecode, 8);
211 + date[0] = sfp->id.ext.datecode[4];
212 + date[1] = sfp->id.ext.datecode[5];
213 + date[2] = '-';
214 + date[3] = sfp->id.ext.datecode[2];
215 + date[4] = sfp->id.ext.datecode[3];
216 + date[5] = '-';
217 + date[6] = sfp->id.ext.datecode[0];
218 + date[7] = sfp->id.ext.datecode[1];
219 date[8] = '\0';
220
221 dev_info(sfp->dev, "module %s %s rev %s sn %s dc %s\n", vendor, part, rev, sn, date);
222 + dev_info(sfp->dev, " %s connector, encoding %s, nominal bitrate %u.%uGbps +%u%% -%u%%\n",
223 + sfp_connector(sfp->id.base.connector),
224 + sfp_encoding(sfp->id.base.encoding),
225 + sfp->id.base.br_nominal / 10,
226 + sfp->id.base.br_nominal % 10,
227 + sfp->id.ext.br_max, sfp->id.ext.br_min);
228 + dev_info(sfp->dev, " 1000BaseSX%c 1000BaseLX%c 1000BaseCX%c 1000BaseT%c 100BaseTLX%c 1000BaseFX%c BaseBX10%c BasePX%c\n",
229 + sfp->id.base.e1000_base_sx ? '+' : '-',
230 + sfp->id.base.e1000_base_lx ? '+' : '-',
231 + sfp->id.base.e1000_base_cx ? '+' : '-',
232 + sfp->id.base.e1000_base_t ? '+' : '-',
233 + sfp->id.base.e100_base_lx ? '+' : '-',
234 + sfp->id.base.e100_base_fx ? '+' : '-',
235 + sfp->id.base.e_base_bx10 ? '+' : '-',
236 + sfp->id.base.e_base_px ? '+' : '-');
237 + dev_info(sfp->dev, " 10GBaseSR%c 10GBaseLR%c 10GBaseLRM%c 10GBaseER%c\n",
238 + sfp->id.base.e10g_base_sr ? '+' : '-',
239 + sfp->id.base.e10g_base_lr ? '+' : '-',
240 + sfp->id.base.e10g_base_lrm ? '+' : '-',
241 + sfp->id.base.e10g_base_er ? '+' : '-');
242 +
243 + if (!sfp->id.base.sfp_ct_passive && !sfp->id.base.sfp_ct_active &&
244 + !sfp->id.base.e1000_base_t) {
245 + char len_9um[16], len_om[16];
246 +
247 + dev_info(sfp->dev, " Wavelength %unm, fiber lengths:\n",
248 + be16_to_cpup(&sfp->id.base.optical_wavelength));
249 +
250 + if (sfp->id.base.link_len[0] == 255)
251 + strcpy(len_9um, ">254km");
252 + else if (sfp->id.base.link_len[1] && sfp->id.base.link_len[1] != 255)
253 + sprintf(len_9um, "%um",
254 + sfp->id.base.link_len[1] * 100);
255 + else if (sfp->id.base.link_len[0])
256 + sprintf(len_9um, "%ukm", sfp->id.base.link_len[0]);
257 + else if (sfp->id.base.link_len[1] == 255)
258 + strcpy(len_9um, ">25.4km");
259 + else
260 + strcpy(len_9um, "unsupported");
261 +
262 + dev_info(sfp->dev, " 9µm SM : %s\n", len_9um);
263 + dev_info(sfp->dev, " 62.5µm MM OM1: %s\n",
264 + sfp_link_len(len_om, sizeof(len_om),
265 + sfp->id.base.link_len[3], 10));
266 + dev_info(sfp->dev, " 50µm MM OM2: %s\n",
267 + sfp_link_len(len_om, sizeof(len_om),
268 + sfp->id.base.link_len[2], 10));
269 + dev_info(sfp->dev, " 50µm MM OM3: %s\n",
270 + sfp_link_len(len_om, sizeof(len_om),
271 + sfp->id.base.link_len[5], 10));
272 + dev_info(sfp->dev, " 50µm MM OM4: %s\n",
273 + sfp_link_len(len_om, sizeof(len_om),
274 + sfp->id.base.link_len[4], 10));
275 + } else {
276 + char len[16];
277 + dev_info(sfp->dev, " Copper length: %s\n",
278 + sfp_link_len(len, sizeof(len),
279 + sfp->id.base.link_len[4], 1));
280 + }
281 +
282 + dev_info(sfp->dev, " Options: %s\n",
283 + sfp_bitfield(options, sizeof(options), sfp_options,
284 + be16_to_cpu(sfp->id.ext.options)));
285 + dev_info(sfp->dev, " Diagnostics: %s\n",
286 + sfp_bitfield(options, sizeof(options), diagmon,
287 + sfp->id.ext.diagmon));
288
289 /* We only support SFP modules, not the legacy GBIC modules. */
290 if (sfp->id.base.phys_id != SFP_PHYS_ID_SFP ||