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