xref: /freebsd/sbin/ifconfig/sfp.c (revision e17f5b1d)
1 /*-
2  * Copyright (c) 2014 Alexander V. Chernikov. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23  * SUCH DAMAGE.
24  */
25 
26 #ifndef lint
27 static const char rcsid[] =
28   "$FreeBSD$";
29 #endif /* not lint */
30 
31 #include <sys/types.h>
32 #include <sys/param.h>
33 #include <sys/ioctl.h>
34 #include <sys/socket.h>
35 
36 #include <net/if.h>
37 #include <net/sff8436.h>
38 #include <net/sff8472.h>
39 
40 #include <math.h>
41 #include <err.h>
42 #include <errno.h>
43 #include <fcntl.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #include <unistd.h>
48 
49 #include "ifconfig.h"
50 
51 struct i2c_info {
52 	int fd;			/* fd to issue SIOCGI2C */
53 	int error;		/* Store first error */
54 	int qsfp;		/* True if transceiver is QSFP */
55 	int do_diag;		/* True if we need to request DDM */
56 	struct ifreq *ifr;	/* Pointer to pre-filled ifreq */
57 };
58 
59 static int read_i2c(struct i2c_info *ii, uint8_t addr, uint8_t off,
60     uint8_t len, uint8_t *buf);
61 static void dump_i2c_data(struct i2c_info *ii, uint8_t addr, uint8_t off,
62     uint8_t len);
63 
64 struct _nv {
65 	int v;
66 	const char *n;
67 };
68 
69 const char *find_value(struct _nv *x, int value);
70 const char *find_zero_bit(struct _nv *x, int value, int sz);
71 
72 /* SFF-8024 Rev. 4.6 Table 4-3: Connector Types */
73 static struct _nv conn[] = {
74 	{ 0x00, "Unknown" },
75 	{ 0x01, "SC" },
76 	{ 0x02, "Fibre Channel Style 1 copper" },
77 	{ 0x03, "Fibre Channel Style 2 copper" },
78 	{ 0x04, "BNC/TNC" },
79 	{ 0x05, "Fibre Channel coaxial" },
80 	{ 0x06, "Fiber Jack" },
81 	{ 0x07, "LC" },
82 	{ 0x08, "MT-RJ" },
83 	{ 0x09, "MU" },
84 	{ 0x0A, "SG" },
85 	{ 0x0B, "Optical pigtail" },
86 	{ 0x0C, "MPO 1x12 Parallel Optic" },
87 	{ 0x0D, "MPO 2x16 Parallel Optic" },
88 	{ 0x20, "HSSDC II" },
89 	{ 0x21, "Copper pigtail" },
90 	{ 0x22, "RJ45" },
91 	{ 0x23, "No separable connector" },
92 	{ 0x24, "MXC 2x16" },
93 	{ 0x25, "CS optical connector" },
94 	{ 0x26, "Mini CS optical connector" },
95 	{ 0x27, "MPO 2x12 Parallel Optic" },
96 	{ 0x28, "MPO 1x16 Parallel Optic" },
97 	{ 0, NULL }
98 };
99 
100 /* SFF-8472 Rev. 11.4 table 3.5: Transceiver codes */
101 /* 10G Ethernet/IB compliance codes, byte 3 */
102 static struct _nv eth_10g[] = {
103 	{ 0x80, "10G Base-ER" },
104 	{ 0x40, "10G Base-LRM" },
105 	{ 0x20, "10G Base-LR" },
106 	{ 0x10, "10G Base-SR" },
107 	{ 0x08, "1X SX" },
108 	{ 0x04, "1X LX" },
109 	{ 0x02, "1X Copper Active" },
110 	{ 0x01, "1X Copper Passive" },
111 	{ 0, NULL }
112 };
113 
114 /* Ethernet compliance codes, byte 6 */
115 static struct _nv eth_compat[] = {
116 	{ 0x80, "BASE-PX" },
117 	{ 0x40, "BASE-BX10" },
118 	{ 0x20, "100BASE-FX" },
119 	{ 0x10, "100BASE-LX/LX10" },
120 	{ 0x08, "1000BASE-T" },
121 	{ 0x04, "1000BASE-CX" },
122 	{ 0x02, "1000BASE-LX" },
123 	{ 0x01, "1000BASE-SX" },
124 	{ 0, NULL }
125 };
126 
127 /* FC link length, byte 7 */
128 static struct _nv fc_len[] = {
129 	{ 0x80, "very long distance" },
130 	{ 0x40, "short distance" },
131 	{ 0x20, "intermediate distance" },
132 	{ 0x10, "long distance" },
133 	{ 0x08, "medium distance" },
134 	{ 0, NULL }
135 };
136 
137 /* Channel/Cable technology, byte 7-8 */
138 static struct _nv cab_tech[] = {
139 	{ 0x0400, "Shortwave laser (SA)" },
140 	{ 0x0200, "Longwave laser (LC)" },
141 	{ 0x0100, "Electrical inter-enclosure (EL)" },
142 	{ 0x80, "Electrical intra-enclosure (EL)" },
143 	{ 0x40, "Shortwave laser (SN)" },
144 	{ 0x20, "Shortwave laser (SL)" },
145 	{ 0x10, "Longwave laser (LL)" },
146 	{ 0x08, "Active Cable" },
147 	{ 0x04, "Passive Cable" },
148 	{ 0, NULL }
149 };
150 
151 /* FC Transmission media, byte 9 */
152 static struct _nv fc_media[] = {
153 	{ 0x80, "Twin Axial Pair" },
154 	{ 0x40, "Twisted Pair" },
155 	{ 0x20, "Miniature Coax" },
156 	{ 0x10, "Viao Coax" },
157 	{ 0x08, "Miltimode, 62.5um" },
158 	{ 0x04, "Multimode, 50um" },
159 	{ 0x02, "" },
160 	{ 0x01, "Single Mode" },
161 	{ 0, NULL }
162 };
163 
164 /* FC Speed, byte 10 */
165 static struct _nv fc_speed[] = {
166 	{ 0x80, "1200 MBytes/sec" },
167 	{ 0x40, "800 MBytes/sec" },
168 	{ 0x20, "1600 MBytes/sec" },
169 	{ 0x10, "400 MBytes/sec" },
170 	{ 0x08, "3200 MBytes/sec" },
171 	{ 0x04, "200 MBytes/sec" },
172 	{ 0x01, "100 MBytes/sec" },
173 	{ 0, NULL }
174 };
175 
176 /* SFF-8436 Rev. 4.8 table 33: Specification compliance  */
177 
178 /* 10/40G Ethernet compliance codes, byte 128 + 3 */
179 static struct _nv eth_1040g[] = {
180 	{ 0x80, "Extended" },
181 	{ 0x40, "10GBASE-LRM" },
182 	{ 0x20, "10GBASE-LR" },
183 	{ 0x10, "10GBASE-SR" },
184 	{ 0x08, "40GBASE-CR4" },
185 	{ 0x04, "40GBASE-SR4" },
186 	{ 0x02, "40GBASE-LR4" },
187 	{ 0x01, "40G Active Cable" },
188 	{ 0, NULL }
189 };
190 #define	SFF_8636_EXT_COMPLIANCE	0x80
191 
192 /* SFF-8024 Rev. 4.6 table 4-4: Extended Specification Compliance */
193 static struct _nv eth_extended_comp[] = {
194 	{ 0xFF, "Reserved" },
195 	{ 0x55, "128GFC LW" },
196 	{ 0x54, "128GFC SW" },
197 	{ 0x53, "128GFC EA" },
198 	{ 0x52, "64GFC LW" },
199 	{ 0x51, "64GFC SW" },
200 	{ 0x50, "64GFC EA" },
201 	{ 0x4F, "Reserved" },
202 	{ 0x4E, "Reserved" },
203 	{ 0x4D, "Reserved" },
204 	{ 0x4C, "Reserved" },
205 	{ 0x4B, "Reserved" },
206 	{ 0x4A, "Reserved" },
207 	{ 0x49, "Reserved" },
208 	{ 0x48, "Reserved" },
209 	{ 0x47, "Reserved" },
210 	{ 0x46, "200GBASE-LR4" },
211 	{ 0x45, "50GBASE-LR" },
212 	{ 0x44, "200G 1550nm PSM4" },
213 	{ 0x43, "200GBASE-FR4" },
214 	{ 0x42, "50GBASE-FR or 200GBASE-DR4" },
215 	{ 0x41, "50GBASE-SR/100GBASE-SR2/200GBASE-SR4" },
216 	{ 0x40, "50GBASE-CR/100GBASE-CR2/200GBASE-CR4" },
217 	{ 0x3F, "Reserved" },
218 	{ 0x3E, "Reserved" },
219 	{ 0x3D, "Reserved" },
220 	{ 0x3C, "Reserved" },
221 	{ 0x3B, "Reserved" },
222 	{ 0x3A, "Reserved" },
223 	{ 0x39, "Reserved" },
224 	{ 0x38, "Reserved" },
225 	{ 0x37, "Reserved" },
226 	{ 0x36, "Reserved" },
227 	{ 0x35, "Reserved" },
228 	{ 0x34, "Reserved" },
229 	{ 0x33, "50GAUI/100GAUI-2/200GAUI-4 AOC (BER <2.6e-4)" },
230 	{ 0x32, "50GAUI/100GAUI-2/200GAUI-4 ACC (BER <2.6e-4)" },
231 	{ 0x31, "50GAUI/100GAUI-2/200GAUI-4 AOC (BER <1e-6)" },
232 	{ 0x30, "50GAUI/100GAUI-2/200GAUI-4 ACC (BER <1e-6)" },
233 	{ 0x2F, "Reserved" },
234 	{ 0x2E, "Reserved" },
235 	{ 0x2D, "Reserved" },
236 	{ 0x2C, "Reserved" },
237 	{ 0x2B, "Reserved" },
238 	{ 0x2A, "Reserved" },
239 	{ 0x29, "Reserved" },
240 	{ 0x28, "Reserved" },
241 	{ 0x27, "100G-LR" },
242 	{ 0x26, "100G-FR" },
243 	{ 0x25, "100GBASE-DR" },
244 	{ 0x24, "4WDM-40 MSA" },
245 	{ 0x23, "4WDM-20 MSA" },
246 	{ 0x22, "4WDM-10 MSA" },
247 	{ 0x21, "100G PAM4 BiDi" },
248 	{ 0x20, "100G SWDM4" },
249 	{ 0x1F, "40G SWDM4" },
250 	{ 0x1E, "2.5GBASE-T" },
251 	{ 0x1D, "5GBASE-T" },
252 	{ 0x1C, "10GBASE-T Short Reach" },
253 	{ 0x1B, "100G 1550nm WDM" },
254 	{ 0x1A, "100GE-DWDM2" },
255 	{ 0x19, "100G ACC or 25GAUI C2M ACC" },
256 	{ 0x18, "100G AOC or 25GAUI C2M AOC" },
257 	{ 0x17, "100G CLR4" },
258 	{ 0x16, "10GBASE-T with SFI electrical interface" },
259 	{ 0x15, "G959.1 profile P1L1-2D2" },
260 	{ 0x14, "G959.1 profile P1S1-2D2" },
261 	{ 0x13, "G959.1 profile P1I1-2D1" },
262 	{ 0x12, "40G PSM4 Parallel SMF" },
263 	{ 0x11, "4 x 10GBASE-SR" },
264 	{ 0x10, "40GBASE-ER4" },
265 	{ 0x0F, "Reserved" },
266 	{ 0x0E, "Reserved" },
267 	{ 0x0D, "25GBASE-CR CA-25G-N" },
268 	{ 0x0C, "25GBASE-CR CA-25G-S" },
269 	{ 0x0B, "100GBASE-CR4 or 25GBASE-CR CA-L" },
270 	{ 0x0A, "Reserved" },
271 	{ 0x09, "Obsolete" },
272 	{ 0x08, "100G ACC (Active Copper Cable) or 25GAUI C2M ACC" },
273 	{ 0x07, "100G PSM4 Parallel SMF" },
274 	{ 0x06, "100G CWDM4" },
275 	{ 0x05, "100GBASE-SR10" },
276 	{ 0x04, "100GBASE-ER4 or 25GBASE-ER" },
277 	{ 0x03, "100GBASE-LR4 or 25GBASE-LR" },
278 	{ 0x02, "100GBASE-SR4 or 25GBASE-SR" },
279 	{ 0x01, "100G AOC (Active Optical Cable) or 25GAUI C2M AOC" },
280 	{ 0x00, "Unspecified" }
281 };
282 
283 /* SFF-8636 Rev. 2.9 table 6.3: Revision compliance */
284 static struct _nv rev_compl[] = {
285 	{ 0x1, "SFF-8436 rev <=4.8" },
286 	{ 0x2, "SFF-8436 rev <=4.8" },
287 	{ 0x3, "SFF-8636 rev <=1.3" },
288 	{ 0x4, "SFF-8636 rev <=1.4" },
289 	{ 0x5, "SFF-8636 rev <=1.5" },
290 	{ 0x6, "SFF-8636 rev <=2.0" },
291 	{ 0x7, "SFF-8636 rev <=2.7" },
292 	{ 0x8, "SFF-8636 rev >=2.8" },
293 	{ 0x0, "Unspecified" }
294 };
295 
296 const char *
297 find_value(struct _nv *x, int value)
298 {
299 	for (; x->n != NULL; x++)
300 		if (x->v == value)
301 			return (x->n);
302 	return (NULL);
303 }
304 
305 const char *
306 find_zero_bit(struct _nv *x, int value, int sz)
307 {
308 	int v, m;
309 	const char *s;
310 
311 	v = 1;
312 	for (v = 1, m = 1 << (8 * sz); v < m; v *= 2) {
313 		if ((value & v) == 0)
314 			continue;
315 		if ((s = find_value(x, value & v)) != NULL) {
316 			value &= ~v;
317 			return (s);
318 		}
319 	}
320 
321 	return (NULL);
322 }
323 
324 static void
325 convert_sff_identifier(char *buf, size_t size, uint8_t value)
326 {
327 	const char *x;
328 
329 	x = NULL;
330 	if (value <= SFF_8024_ID_LAST)
331 		x = sff_8024_id[value];
332 	else {
333 		if (value > 0x80)
334 			x = "Vendor specific";
335 		else
336 			x = "Reserved";
337 	}
338 
339 	snprintf(buf, size, "%s", x);
340 }
341 
342 static void
343 convert_sff_connector(char *buf, size_t size, uint8_t value)
344 {
345 	const char *x;
346 
347 	if ((x = find_value(conn, value)) == NULL) {
348 		if (value >= 0x0D && value <= 0x1F)
349 			x = "Unallocated";
350 		else if (value >= 0x24 && value <= 0x7F)
351 			x = "Unallocated";
352 		else
353 			x = "Vendor specific";
354 	}
355 
356 	snprintf(buf, size, "%s", x);
357 }
358 
359 static void
360 convert_sff_rev_compliance(char *buf, size_t size, uint8_t value)
361 {
362 	const char *x;
363 
364 	if (value > 0x07)
365 		x = "Unallocated";
366 	else
367 		x = find_value(rev_compl, value);
368 
369 	snprintf(buf, size, "%s", x);
370 }
371 
372 static void
373 get_sfp_identifier(struct i2c_info *ii, char *buf, size_t size)
374 {
375 	uint8_t data;
376 
377 	read_i2c(ii, SFF_8472_BASE, SFF_8472_ID, 1, &data);
378 	convert_sff_identifier(buf, size, data);
379 }
380 
381 static void
382 get_sfp_connector(struct i2c_info *ii, char *buf, size_t size)
383 {
384 	uint8_t data;
385 
386 	read_i2c(ii, SFF_8472_BASE, SFF_8472_CONNECTOR, 1, &data);
387 	convert_sff_connector(buf, size, data);
388 }
389 
390 static void
391 get_qsfp_identifier(struct i2c_info *ii, char *buf, size_t size)
392 {
393 	uint8_t data;
394 
395 	read_i2c(ii, SFF_8436_BASE, SFF_8436_ID, 1, &data);
396 	convert_sff_identifier(buf, size, data);
397 }
398 
399 static void
400 get_qsfp_connector(struct i2c_info *ii, char *buf, size_t size)
401 {
402 	uint8_t data;
403 
404 	read_i2c(ii, SFF_8436_BASE, SFF_8436_CONNECTOR, 1, &data);
405 	convert_sff_connector(buf, size, data);
406 }
407 
408 static void
409 printf_sfp_transceiver_descr(struct i2c_info *ii, char *buf, size_t size)
410 {
411 	char xbuf[12];
412 	const char *tech_class, *tech_len, *tech_tech, *tech_media, *tech_speed;
413 
414 	tech_class = NULL;
415 	tech_len = NULL;
416 	tech_tech = NULL;
417 	tech_media = NULL;
418 	tech_speed = NULL;
419 
420 	/* Read bytes 3-10 at once */
421 	read_i2c(ii, SFF_8472_BASE, SFF_8472_TRANS_START, 8, &xbuf[3]);
422 
423 	/* Check 10G ethernet first */
424 	tech_class = find_zero_bit(eth_10g, xbuf[3], 1);
425 	if (tech_class == NULL) {
426 		/* No match. Try 1G */
427 		tech_class = find_zero_bit(eth_compat, xbuf[6], 1);
428 	}
429 
430 	tech_len = find_zero_bit(fc_len, xbuf[7], 1);
431 	tech_tech = find_zero_bit(cab_tech, xbuf[7] << 8 | xbuf[8], 2);
432 	tech_media = find_zero_bit(fc_media, xbuf[9], 1);
433 	tech_speed = find_zero_bit(fc_speed, xbuf[10], 1);
434 
435 	printf("Class: %s\n", tech_class);
436 	printf("Length: %s\n", tech_len);
437 	printf("Tech: %s\n", tech_tech);
438 	printf("Media: %s\n", tech_media);
439 	printf("Speed: %s\n", tech_speed);
440 }
441 
442 static void
443 get_sfp_transceiver_class(struct i2c_info *ii, char *buf, size_t size)
444 {
445 	const char *tech_class;
446 	uint8_t code;
447 
448 	/* Use extended compliance code if it's valid */
449 	read_i2c(ii, SFF_8472_BASE, SFF_8472_TRANS, 1, &code);
450 	if (code != 0)
451 		tech_class = find_value(eth_extended_comp, code);
452 	else {
453 		/* Next, check 10G Ethernet/IB CCs */
454 		read_i2c(ii, SFF_8472_BASE, SFF_8472_TRANS_START, 1, &code);
455 		tech_class = find_zero_bit(eth_10g, code, 1);
456 		if (tech_class == NULL) {
457 			/* No match. Try Ethernet 1G */
458 			read_i2c(ii, SFF_8472_BASE, SFF_8472_TRANS_START + 3,
459 			    1, (caddr_t)&code);
460 			tech_class = find_zero_bit(eth_compat, code, 1);
461 		}
462 	}
463 
464 	if (tech_class == NULL)
465 		tech_class = "Unknown";
466 
467 	snprintf(buf, size, "%s", tech_class);
468 }
469 
470 static void
471 get_qsfp_transceiver_class(struct i2c_info *ii, char *buf, size_t size)
472 {
473 	const char *tech_class;
474 	uint8_t code;
475 
476 	read_i2c(ii, SFF_8436_BASE, SFF_8436_CODE_E1040100G, 1, &code);
477 
478 	/* Check for extended specification compliance */
479 	if (code & SFF_8636_EXT_COMPLIANCE) {
480 		read_i2c(ii, SFF_8436_BASE, SFF_8436_OPTIONS_START, 1, &code);
481 		tech_class = find_value(eth_extended_comp, code);
482 	} else
483 		/* Check 10/40G Ethernet class only */
484 		tech_class = find_zero_bit(eth_1040g, code, 1);
485 
486 	if (tech_class == NULL)
487 		tech_class = "Unknown";
488 
489 	snprintf(buf, size, "%s", tech_class);
490 }
491 
492 /*
493  * Print SFF-8472/SFF-8436 string to supplied buffer.
494  * All (vendor-specific) strings are padded right with '0x20'.
495  */
496 static void
497 convert_sff_name(char *buf, size_t size, char *xbuf)
498 {
499 	char *p;
500 
501 	for (p = &xbuf[16]; *(p - 1) == 0x20; p--)
502 		;
503 	*p = '\0';
504 	snprintf(buf, size, "%s", xbuf);
505 }
506 
507 static void
508 convert_sff_date(char *buf, size_t size, char *xbuf)
509 {
510 
511 	snprintf(buf, size, "20%c%c-%c%c-%c%c", xbuf[0], xbuf[1],
512 	    xbuf[2], xbuf[3], xbuf[4], xbuf[5]);
513 }
514 
515 static void
516 get_sfp_vendor_name(struct i2c_info *ii, char *buf, size_t size)
517 {
518 	char xbuf[17];
519 
520 	memset(xbuf, 0, sizeof(xbuf));
521 	read_i2c(ii, SFF_8472_BASE, SFF_8472_VENDOR_START, 16, (uint8_t *)xbuf);
522 	convert_sff_name(buf, size, xbuf);
523 }
524 
525 static void
526 get_sfp_vendor_pn(struct i2c_info *ii, char *buf, size_t size)
527 {
528 	char xbuf[17];
529 
530 	memset(xbuf, 0, sizeof(xbuf));
531 	read_i2c(ii, SFF_8472_BASE, SFF_8472_PN_START, 16, (uint8_t *)xbuf);
532 	convert_sff_name(buf, size, xbuf);
533 }
534 
535 static void
536 get_sfp_vendor_sn(struct i2c_info *ii, char *buf, size_t size)
537 {
538 	char xbuf[17];
539 
540 	memset(xbuf, 0, sizeof(xbuf));
541 	read_i2c(ii, SFF_8472_BASE, SFF_8472_SN_START, 16, (uint8_t *)xbuf);
542 	convert_sff_name(buf, size, xbuf);
543 }
544 
545 static void
546 get_sfp_vendor_date(struct i2c_info *ii, char *buf, size_t size)
547 {
548 	char xbuf[6];
549 
550 	memset(xbuf, 0, sizeof(xbuf));
551 	/* Date code, see Table 3.8 for description */
552 	read_i2c(ii, SFF_8472_BASE, SFF_8472_DATE_START, 6, (uint8_t *)xbuf);
553 	convert_sff_date(buf, size, xbuf);
554 }
555 
556 static void
557 get_qsfp_vendor_name(struct i2c_info *ii, char *buf, size_t size)
558 {
559 	char xbuf[17];
560 
561 	memset(xbuf, 0, sizeof(xbuf));
562 	read_i2c(ii, SFF_8436_BASE, SFF_8436_VENDOR_START, 16, (uint8_t *)xbuf);
563 	convert_sff_name(buf, size, xbuf);
564 }
565 
566 static void
567 get_qsfp_vendor_pn(struct i2c_info *ii, char *buf, size_t size)
568 {
569 	char xbuf[17];
570 
571 	memset(xbuf, 0, sizeof(xbuf));
572 	read_i2c(ii, SFF_8436_BASE, SFF_8436_PN_START, 16, (uint8_t *)xbuf);
573 	convert_sff_name(buf, size, xbuf);
574 }
575 
576 static void
577 get_qsfp_vendor_sn(struct i2c_info *ii, char *buf, size_t size)
578 {
579 	char xbuf[17];
580 
581 	memset(xbuf, 0, sizeof(xbuf));
582 	read_i2c(ii, SFF_8436_BASE, SFF_8436_SN_START, 16, (uint8_t *)xbuf);
583 	convert_sff_name(buf, size, xbuf);
584 }
585 
586 static void
587 get_qsfp_vendor_date(struct i2c_info *ii, char *buf, size_t size)
588 {
589 	char xbuf[6];
590 
591 	memset(xbuf, 0, sizeof(xbuf));
592 	read_i2c(ii, SFF_8436_BASE, SFF_8436_DATE_START, 6, (uint8_t *)xbuf);
593 	convert_sff_date(buf, size, xbuf);
594 }
595 
596 static void
597 print_sfp_vendor(struct i2c_info *ii, char *buf, size_t size)
598 {
599 	char xbuf[80];
600 
601 	memset(xbuf, 0, sizeof(xbuf));
602 	if (ii->qsfp != 0) {
603 		get_qsfp_vendor_name(ii, xbuf, 20);
604 		get_qsfp_vendor_pn(ii, &xbuf[20], 20);
605 		get_qsfp_vendor_sn(ii, &xbuf[40], 20);
606 		get_qsfp_vendor_date(ii, &xbuf[60], 20);
607 	} else {
608 		get_sfp_vendor_name(ii, xbuf, 20);
609 		get_sfp_vendor_pn(ii, &xbuf[20], 20);
610 		get_sfp_vendor_sn(ii, &xbuf[40], 20);
611 		get_sfp_vendor_date(ii, &xbuf[60], 20);
612 	}
613 
614 	snprintf(buf, size, "vendor: %s PN: %s SN: %s DATE: %s",
615 	    xbuf, &xbuf[20],  &xbuf[40], &xbuf[60]);
616 }
617 
618 /*
619  * Converts internal templerature (SFF-8472, SFF-8436)
620  * 16-bit unsigned value to human-readable representation:
621  *
622  * Internally measured Module temperature are represented
623  * as a 16-bit signed twos complement value in increments of
624  * 1/256 degrees Celsius, yielding a total range of –128C to +128C
625  * that is considered valid between –40 and +125C.
626  *
627  */
628 static void
629 convert_sff_temp(char *buf, size_t size, uint8_t *xbuf)
630 {
631 	double d;
632 
633 	d = (double)xbuf[0];
634 	d += (double)xbuf[1] / 256;
635 
636 	snprintf(buf, size, "%.2f C", d);
637 }
638 
639 /*
640  * Retrieves supplied voltage (SFF-8472, SFF-8436).
641  * 16-bit usigned value, treated as range 0..+6.55 Volts
642  */
643 static void
644 convert_sff_voltage(char *buf, size_t size, uint8_t *xbuf)
645 {
646 	double d;
647 
648 	d = (double)((xbuf[0] << 8) | xbuf[1]);
649 	snprintf(buf, size, "%.2f Volts", d / 10000);
650 }
651 
652 /*
653  * Converts value in @xbuf to both milliwats and dBm
654  * human representation.
655  */
656 static void
657 convert_sff_power(struct i2c_info *ii, char *buf, size_t size, uint8_t *xbuf)
658 {
659 	uint16_t mW;
660 	double dbm;
661 
662 	mW = (xbuf[0] << 8) + xbuf[1];
663 
664 	/* Convert mw to dbm */
665 	dbm = 10.0 * log10(1.0 * mW / 10000);
666 
667 	/*
668 	 * Assume internally-calibrated data.
669 	 * This is always true for SFF-8346, and explicitly
670 	 * checked for SFF-8472.
671 	 */
672 
673 	/* Table 3.9, bit 5 is set, internally calibrated */
674 	snprintf(buf, size, "%d.%02d mW (%.2f dBm)",
675     	    mW / 10000, (mW % 10000) / 100, dbm);
676 }
677 
678 static void
679 get_sfp_temp(struct i2c_info *ii, char *buf, size_t size)
680 {
681 	uint8_t xbuf[2];
682 
683 	memset(xbuf, 0, sizeof(xbuf));
684 	read_i2c(ii, SFF_8472_DIAG, SFF_8472_TEMP, 2, xbuf);
685 	convert_sff_temp(buf, size, xbuf);
686 }
687 
688 static void
689 get_sfp_voltage(struct i2c_info *ii, char *buf, size_t size)
690 {
691 	uint8_t xbuf[2];
692 
693 	memset(xbuf, 0, sizeof(xbuf));
694 	read_i2c(ii, SFF_8472_DIAG, SFF_8472_VCC, 2, xbuf);
695 	convert_sff_voltage(buf, size, xbuf);
696 }
697 
698 static int
699 get_qsfp_temp(struct i2c_info *ii, char *buf, size_t size)
700 {
701 	uint8_t xbuf[2];
702 
703 	memset(xbuf, 0, sizeof(xbuf));
704 	read_i2c(ii, SFF_8436_BASE, SFF_8436_TEMP, 2, xbuf);
705 	if ((xbuf[0] == 0xFF && xbuf[1] == 0xFF) || (xbuf[0] == 0 && xbuf[1] == 0))
706 		return (-1);
707 	convert_sff_temp(buf, size, xbuf);
708 	return (0);
709 }
710 
711 static void
712 get_qsfp_voltage(struct i2c_info *ii, char *buf, size_t size)
713 {
714 	uint8_t xbuf[2];
715 
716 	memset(xbuf, 0, sizeof(xbuf));
717 	read_i2c(ii, SFF_8436_BASE, SFF_8436_VCC, 2, xbuf);
718 	convert_sff_voltage(buf, size, xbuf);
719 }
720 
721 static void
722 get_sfp_rx_power(struct i2c_info *ii, char *buf, size_t size)
723 {
724 	uint8_t xbuf[2];
725 
726 	memset(xbuf, 0, sizeof(xbuf));
727 	read_i2c(ii, SFF_8472_DIAG, SFF_8472_RX_POWER, 2, xbuf);
728 	convert_sff_power(ii, buf, size, xbuf);
729 }
730 
731 static void
732 get_sfp_tx_power(struct i2c_info *ii, char *buf, size_t size)
733 {
734 	uint8_t xbuf[2];
735 
736 	memset(xbuf, 0, sizeof(xbuf));
737 	read_i2c(ii, SFF_8472_DIAG, SFF_8472_TX_POWER, 2, xbuf);
738 	convert_sff_power(ii, buf, size, xbuf);
739 }
740 
741 static void
742 get_qsfp_rx_power(struct i2c_info *ii, char *buf, size_t size, int chan)
743 {
744 	uint8_t xbuf[2];
745 
746 	memset(xbuf, 0, sizeof(xbuf));
747 	read_i2c(ii, SFF_8436_BASE, SFF_8436_RX_CH1_MSB + (chan-1)*2, 2, xbuf);
748 	convert_sff_power(ii, buf, size, xbuf);
749 }
750 
751 static void
752 get_qsfp_tx_power(struct i2c_info *ii, char *buf, size_t size, int chan)
753 {
754 	uint8_t xbuf[2];
755 
756 	memset(xbuf, 0, sizeof(xbuf));
757 	read_i2c(ii, SFF_8436_BASE, SFF_8436_TX_CH1_MSB + (chan-1)*2, 2, xbuf);
758 	convert_sff_power(ii, buf, size, xbuf);
759 }
760 
761 static void
762 get_qsfp_rev_compliance(struct i2c_info *ii, char *buf, size_t size)
763 {
764 	uint8_t xbuf;
765 
766 	xbuf = 0;
767 	read_i2c(ii, SFF_8436_BASE, SFF_8436_STATUS, 1, &xbuf);
768 	convert_sff_rev_compliance(buf, size, xbuf);
769 }
770 
771 static uint32_t
772 get_qsfp_br(struct i2c_info *ii)
773 {
774 	uint8_t xbuf;
775 	uint32_t rate;
776 
777 	xbuf = 0;
778 	read_i2c(ii, SFF_8436_BASE, SFF_8436_BITRATE, 1, &xbuf);
779 	rate = xbuf * 100;
780 	if (xbuf == 0xFF) {
781 		read_i2c(ii, SFF_8436_BASE, SFF_8636_BITRATE, 1, &xbuf);
782 		rate = xbuf * 250;
783 	}
784 
785 	return (rate);
786 }
787 
788 /*
789  * Reads i2c data from opened kernel socket.
790  */
791 static int
792 read_i2c(struct i2c_info *ii, uint8_t addr, uint8_t off, uint8_t len,
793     uint8_t *buf)
794 {
795 	struct ifi2creq req;
796 	int i, l;
797 
798 	if (ii->error != 0)
799 		return (ii->error);
800 
801 	ii->ifr->ifr_data = (caddr_t)&req;
802 
803 	i = 0;
804 	l = 0;
805 	memset(&req, 0, sizeof(req));
806 	req.dev_addr = addr;
807 	req.offset = off;
808 	req.len = len;
809 
810 	while (len > 0) {
811 		l = MIN(sizeof(req.data), len);
812 		req.len = l;
813 		if (ioctl(ii->fd, SIOCGI2C, ii->ifr) != 0) {
814 			ii->error = errno;
815 			return (errno);
816 		}
817 
818 		memcpy(&buf[i], req.data, l);
819 		len -= l;
820 		i += l;
821 		req.offset += l;
822 	}
823 
824 	return (0);
825 }
826 
827 static void
828 dump_i2c_data(struct i2c_info *ii, uint8_t addr, uint8_t off, uint8_t len)
829 {
830 	unsigned char buf[16];
831 	int i, read;
832 
833 	while (len > 0) {
834 		memset(buf, 0, sizeof(buf));
835 		read = MIN(sizeof(buf), len);
836 		read_i2c(ii, addr, off, read, buf);
837 		if (ii->error != 0) {
838 			fprintf(stderr, "Error reading i2c info\n");
839 			return;
840 		}
841 
842 		printf("\t");
843 		for (i = 0; i < read; i++)
844 			printf("%02X ", buf[i]);
845 		printf("\n");
846 		len -= read;
847 		off += read;
848 	}
849 }
850 
851 static void
852 print_qsfp_status(struct i2c_info *ii, int verbose)
853 {
854 	char buf[80], buf2[40], buf3[40];
855 	uint32_t bitrate;
856 	int i;
857 
858 	ii->qsfp = 1;
859 
860 	/* Transceiver type */
861 	get_qsfp_identifier(ii, buf, sizeof(buf));
862 	get_qsfp_transceiver_class(ii, buf2, sizeof(buf2));
863 	get_qsfp_connector(ii, buf3, sizeof(buf3));
864 	if (ii->error == 0)
865 		printf("\tplugged: %s %s (%s)\n", buf, buf2, buf3);
866 	print_sfp_vendor(ii, buf, sizeof(buf));
867 	if (ii->error == 0)
868 		printf("\t%s\n", buf);
869 
870 	if (verbose > 1) {
871 		get_qsfp_rev_compliance(ii, buf, sizeof(buf));
872 		if (ii->error == 0)
873 			printf("\tcompliance level: %s\n", buf);
874 
875 		bitrate = get_qsfp_br(ii);
876 		if (ii->error == 0 && bitrate > 0)
877 			printf("\tnominal bitrate: %u Mbps\n", bitrate);
878 	}
879 
880 	/*
881 	 * The standards in this area are not clear when the
882 	 * additional measurements are present or not. Use a valid
883 	 * temperature reading as an indicator for the presence of
884 	 * voltage and TX/RX power measurements.
885 	 */
886 	if (get_qsfp_temp(ii, buf, sizeof(buf)) == 0) {
887 		get_qsfp_voltage(ii, buf2, sizeof(buf2));
888 		printf("\tmodule temperature: %s voltage: %s\n", buf, buf2);
889 		for (i = 1; i <= 4; i++) {
890 			get_qsfp_rx_power(ii, buf, sizeof(buf), i);
891 			get_qsfp_tx_power(ii, buf2, sizeof(buf2), i);
892 			printf("\tlane %d: RX: %s TX: %s\n", i, buf, buf2);
893 		}
894 	}
895 
896 	if (verbose > 2) {
897 		printf("\n\tSFF8436 DUMP (0xA0 128..255 range):\n");
898 		dump_i2c_data(ii, SFF_8436_BASE, 128, 128);
899 		printf("\n\tSFF8436 DUMP (0xA0 0..81 range):\n");
900 		dump_i2c_data(ii, SFF_8436_BASE, 0, 82);
901 	}
902 }
903 
904 static void
905 print_sfp_status(struct i2c_info *ii, int verbose)
906 {
907 	char buf[80], buf2[40], buf3[40];
908 	uint8_t diag_type, flags;
909 
910 	/* Read diagnostic monitoring type */
911 	read_i2c(ii, SFF_8472_BASE, SFF_8472_DIAG_TYPE, 1, (caddr_t)&diag_type);
912 	if (ii->error != 0)
913 		return;
914 
915 	/*
916 	 * Read monitoring data IFF it is supplied AND is
917 	 * internally calibrated
918 	 */
919 	flags = SFF_8472_DDM_DONE | SFF_8472_DDM_INTERNAL;
920 	if ((diag_type & flags) == flags)
921 		ii->do_diag = 1;
922 
923 	/* Transceiver type */
924 	get_sfp_identifier(ii, buf, sizeof(buf));
925 	get_sfp_transceiver_class(ii, buf2, sizeof(buf2));
926 	get_sfp_connector(ii, buf3, sizeof(buf3));
927 	if (ii->error == 0)
928 		printf("\tplugged: %s %s (%s)\n", buf, buf2, buf3);
929 	print_sfp_vendor(ii, buf, sizeof(buf));
930 	if (ii->error == 0)
931 		printf("\t%s\n", buf);
932 
933 	if (verbose > 5)
934 		printf_sfp_transceiver_descr(ii, buf, sizeof(buf));
935 	/*
936 	 * Request current measurements iff they are provided:
937 	 */
938 	if (ii->do_diag != 0) {
939 		get_sfp_temp(ii, buf, sizeof(buf));
940 		get_sfp_voltage(ii, buf2, sizeof(buf2));
941 		printf("\tmodule temperature: %s Voltage: %s\n", buf, buf2);
942 		get_sfp_rx_power(ii, buf, sizeof(buf));
943 		get_sfp_tx_power(ii, buf2, sizeof(buf2));
944 		printf("\tRX: %s TX: %s\n", buf, buf2);
945 	}
946 
947 	if (verbose > 2) {
948 		printf("\n\tSFF8472 DUMP (0xA0 0..127 range):\n");
949 		dump_i2c_data(ii, SFF_8472_BASE, 0, 128);
950 	}
951 }
952 
953 void
954 sfp_status(int s, struct ifreq *ifr, int verbose)
955 {
956 	struct i2c_info ii;
957 	uint8_t id_byte;
958 
959 	/* Prepare necessary into pass to i2c reader */
960 	memset(&ii, 0, sizeof(ii));
961 	ii.fd = s;
962 	ii.ifr = ifr;
963 
964 	/*
965 	 * Try to read byte 0 from i2c:
966 	 * Both SFF-8472 and SFF-8436 use it as
967 	 * 'identification byte'.
968 	 * Stop reading status on zero as value -
969 	 * this might happen in case of empty transceiver slot.
970 	 */
971 	id_byte = 0;
972 	read_i2c(&ii, SFF_8472_BASE, SFF_8472_ID, 1, (caddr_t)&id_byte);
973 	if (ii.error != 0 || id_byte == 0)
974 		return;
975 
976 	switch (id_byte) {
977 	case SFF_8024_ID_QSFP:
978 	case SFF_8024_ID_QSFPPLUS:
979 	case SFF_8024_ID_QSFP28:
980 		print_qsfp_status(&ii, verbose);
981 		break;
982 	default:
983 		print_sfp_status(&ii, verbose);
984 	}
985 }
986 
987