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