xref: /freebsd/sbin/ifconfig/sfp.c (revision 5608fd23c27fa1e8ee595d7b678cbfd35d657fbe)
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/sff8472.h>
38 
39 #include <math.h>
40 #include <err.h>
41 #include <errno.h>
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <string.h>
45 #include <unistd.h>
46 
47 #include "ifconfig.h"
48 
49 struct i2c_info;
50 typedef int (read_i2c)(struct i2c_info *ii, uint8_t addr, uint8_t off,
51     uint8_t len, caddr_t buf);
52 
53 struct i2c_info {
54 	int s;
55 	int error;
56 	struct ifreq *ifr;
57 	read_i2c *f;
58 	uint8_t diag_type;
59 	char *textbuf;
60 	size_t bufsize;
61 };
62 
63 struct _nv {
64 	int v;
65 	const char *n;
66 };
67 
68 const char *find_value(struct _nv *x, int value);
69 const char *find_zero_bit(struct _nv *x, int value, int sz);
70 
71 /* SFF-8472 Rev. 11.4 table 3.4: Connector values */
72 static struct _nv conn[] = {
73 	{ 0x00, "Unknown" },
74 	{ 0x01, "SC" },
75 	{ 0x02, "Fibre Channel Style 1 copper" },
76 	{ 0x03, "Fibre Channel Style 2 copper" },
77 	{ 0x04, "BNC/TNC" },
78 	{ 0x05, "Fibre Channel coaxial" },
79 	{ 0x06, "FiberJack" },
80 	{ 0x07, "LC" },
81 	{ 0x08, "MT-RJ" },
82 	{ 0x09, "MU" },
83 	{ 0x0A, "SG" },
84 	{ 0x0B, "Optical pigtail" },
85 	{ 0x0C, "MPO Parallel Optic" },
86 	{ 0x20, "HSSDC II" },
87 	{ 0x21, "Copper pigtail" },
88 	{ 0x22, "RJ45" },
89 	{ 0, NULL }
90 };
91 
92 /* SFF-8472 Rev. 11.4 table 3.5: Transceiver codes */
93 /* 10G Ethernet compliance codes, byte 3 */
94 static struct _nv eth_10g[] = {
95 	{ 0x80, "10G Base-ER" },
96 	{ 0x40, "10G Base-LRM" },
97 	{ 0x20, "10G Base-LR" },
98 	{ 0x10, "10G Base-SR" },
99 	{ 0x08, "1X SX" },
100 	{ 0x04, "1X LX" },
101 	{ 0x02, "1X Copper Active" },
102 	{ 0x01, "1X Copper Passive" },
103 	{ 0, NULL }
104 };
105 
106 /* Ethernet compliance codes, byte 6 */
107 static struct _nv eth_compat[] = {
108 	{ 0x80, "BASE-PX" },
109 	{ 0x40, "BASE-BX10" },
110 	{ 0x20, "100BASE-FX" },
111 	{ 0x10, "100BASE-LX/LX10" },
112 	{ 0x08, "1000BASE-T" },
113 	{ 0x04, "1000BASE-CX" },
114 	{ 0x02, "1000BASE-LX" },
115 	{ 0x01, "1000BASE-SX" },
116 	{ 0, NULL }
117 };
118 
119 /* FC link length, byte 7 */
120 static struct _nv fc_len[] = {
121 	{ 0x80, "very long distance" },
122 	{ 0x40, "short distance" },
123 	{ 0x20, "intermediate distance" },
124 	{ 0x10, "long distance" },
125 	{ 0x08, "medium distance" },
126 	{ 0, NULL }
127 };
128 
129 /* Channel/Cable technology, byte 7-8 */
130 static struct _nv cab_tech[] = {
131 	{ 0x0400, "Shortwave laser (SA)" },
132 	{ 0x0200, "Longwave laser (LC)" },
133 	{ 0x0100, "Electrical inter-enclosure (EL)" },
134 	{ 0x80, "Electrical intra-enclosure (EL)" },
135 	{ 0x40, "Shortwave laser (SN)" },
136 	{ 0x20, "Shortwave laser (SL)" },
137 	{ 0x10, "Longwave laser (LL)" },
138 	{ 0x08, "Active Cable" },
139 	{ 0x04, "Passive Cable" },
140 	{ 0, NULL }
141 };
142 
143 /* FC Transmission media, byte 9 */
144 static struct _nv fc_media[] = {
145 	{ 0x80, "Twin Axial Pair" },
146 	{ 0x40, "Twisted Pair" },
147 	{ 0x20, "Miniature Coax" },
148 	{ 0x10, "Viao Coax" },
149 	{ 0x08, "Miltimode, 62.5um" },
150 	{ 0x04, "Multimode, 50um" },
151 	{ 0x02, "" },
152 	{ 0x01, "Single Mode" },
153 	{ 0, NULL }
154 };
155 
156 /* FC Speed, byte 10 */
157 static struct _nv fc_speed[] = {
158 	{ 0x80, "1200 MBytes/sec" },
159 	{ 0x40, "800 MBytes/sec" },
160 	{ 0x20, "1600 MBytes/sec" },
161 	{ 0x10, "400 MBytes/sec" },
162 	{ 0x08, "3200 MBytes/sec" },
163 	{ 0x04, "200 MBytes/sec" },
164 	{ 0x01, "100 MBytes/sec" },
165 	{ 0, NULL }
166 };
167 
168 const char *
169 find_value(struct _nv *x, int value)
170 {
171 	for (; x->n != NULL; x++)
172 		if (x->v == value)
173 			return (x->n);
174 	return (NULL);
175 }
176 
177 const char *
178 find_zero_bit(struct _nv *x, int value, int sz)
179 {
180 	int v, m;
181 	const char *s;
182 
183 	v = 1;
184 	for (v = 1, m = 1 << (8 * sz); v < m; v *= 2) {
185 		if ((value & v) == 0)
186 			continue;
187 		if ((s = find_value(x, value & v)) != NULL) {
188 			value &= ~v;
189 			return (s);
190 		}
191 	}
192 
193 	return (NULL);
194 }
195 
196 static void
197 get_sfp_identifier(struct i2c_info *ii, char *buf, size_t size)
198 {
199 	const char *x;
200 	uint8_t data;
201 
202 	ii->f(ii, SFF_8472_BASE, SFF_8472_ID, 1, (caddr_t)&data);
203 
204 	x = NULL;
205 	if (data <= SFF_8472_ID_LAST)
206 		x = sff_8472_id[data];
207 	else {
208 		if (data > 0x80)
209 			x = "Vendor specific";
210 		else
211 			x = "Reserved";
212 	}
213 
214 	snprintf(buf, size, "%s", x);
215 }
216 
217 static void
218 get_sfp_connector(struct i2c_info *ii, char *buf, size_t size)
219 {
220 	const char *x;
221 	uint8_t data;
222 
223 	ii->f(ii, SFF_8472_BASE, SFF_8472_CONNECTOR, 1, (caddr_t)&data);
224 
225 	if ((x = find_value(conn, data)) == NULL) {
226 		if (data >= 0x0D && data <= 0x1F)
227 			x = "Unallocated";
228 		else if (data >= 0x23 && data <= 0x7F)
229 			x = "Unallocated";
230 		else
231 			x = "Vendor specific";
232 	}
233 
234 	snprintf(buf, size, "%s", x);
235 }
236 
237 static void
238 printf_sfp_transceiver_descr(struct i2c_info *ii, char *buf, size_t size)
239 {
240 	char xbuf[12];
241 	const char *tech_class, *tech_len, *tech_tech, *tech_media, *tech_speed;
242 
243 	tech_class = NULL;
244 	tech_len = NULL;
245 	tech_tech = NULL;
246 	tech_media = NULL;
247 	tech_speed = NULL;
248 
249 	/* Read bytes 3-10 at once */
250 	ii->f(ii, SFF_8472_BASE, SFF_8472_TRANS_START, 8, &xbuf[3]);
251 
252 	/* Check 10G ethernet first */
253 	tech_class = find_zero_bit(eth_10g, xbuf[3], 1);
254 	if (tech_class == NULL) {
255 		/* No match. Try 1G */
256 		tech_class = find_zero_bit(eth_compat, xbuf[6], 1);
257 	}
258 
259 	tech_len = find_zero_bit(fc_len, xbuf[7], 1);
260 	tech_tech = find_zero_bit(cab_tech, xbuf[7] << 8 | xbuf[8], 2);
261 	tech_media = find_zero_bit(fc_media, xbuf[9], 1);
262 	tech_speed = find_zero_bit(fc_speed, xbuf[10], 1);
263 
264 	printf("Class: %s\n", tech_class);
265 	printf("Length: %s\n", tech_len);
266 	printf("Tech: %s\n", tech_tech);
267 	printf("Media: %s\n", tech_media);
268 	printf("Speed: %s\n", tech_speed);
269 }
270 
271 static void
272 get_sfp_transceiver_class(struct i2c_info *ii, char *buf, size_t size)
273 {
274 	const char *tech_class;
275 	uint8_t code;
276 
277 	/* Check 10G Ethernet/IB first */
278 	ii->f(ii, SFF_8472_BASE, SFF_8472_TRANS_START, 1, (caddr_t)&code);
279 	tech_class = find_zero_bit(eth_10g, code, 1);
280 	if (tech_class == NULL) {
281 		/* No match. Try Ethernet 1G */
282 		ii->f(ii, SFF_8472_BASE, SFF_8472_TRANS_START + 3,
283 		    1, (caddr_t)&code);
284 		tech_class = find_zero_bit(eth_compat, code, 1);
285 	}
286 
287 	if (tech_class == NULL)
288 		tech_class = "Unknown";
289 
290 	snprintf(buf, size, "%s", tech_class);
291 }
292 
293 
294 static void
295 get_sfp_vendor_name(struct i2c_info *ii, char *buf, size_t size)
296 {
297 	char xbuf[17], *p;
298 
299 	memset(xbuf, 0, sizeof(xbuf));
300 	/* ASCII String, right-padded with 0x20 */
301 	ii->f(ii, SFF_8472_BASE, SFF_8472_VENDOR_START, 16, xbuf);
302 	for (p = &xbuf[16]; *(p - 1) == 0x20; p--)
303 		;
304 	*p = '\0';
305 
306 	snprintf(buf, size, "%s", xbuf);
307 }
308 
309 static void
310 get_sfp_vendor_pn(struct i2c_info *ii, char *buf, size_t size)
311 {
312 	char xbuf[17], *p;
313 
314 	memset(xbuf, 0, sizeof(xbuf));
315 	/* ASCII String, right-padded with 0x20 */
316 	ii->f(ii, SFF_8472_BASE, SFF_8472_PN_START, 16, xbuf);
317 	for (p = &xbuf[16]; *(p - 1) == 0x20; p--)
318 		;
319 	*p = '\0';
320 
321 	snprintf(buf, size, "%s", xbuf);
322 }
323 
324 static void
325 get_sfp_vendor_sn(struct i2c_info *ii, char *buf, size_t size)
326 {
327 	char xbuf[17], *p;
328 
329 	memset(xbuf, 0, sizeof(xbuf));
330 	/* ASCII String, right-padded with 0x20 */
331 	ii->f(ii, SFF_8472_BASE, SFF_8472_SN_START, 16, xbuf);
332 	for (p = &xbuf[16]; *(p - 1) == 0x20; p--)
333 		;
334 	*p = '\0';
335 	snprintf(buf, size, "%s", xbuf);
336 }
337 
338 static void
339 get_sfp_vendor_date(struct i2c_info *ii, char *buf, size_t size)
340 {
341 	char xbuf[6];
342 
343 	memset(xbuf, 0, sizeof(xbuf));
344 	/* Date code, see Table 3.8 for description */
345 	ii->f(ii, SFF_8472_BASE, SFF_8472_DATE_START, 6, xbuf);
346 	snprintf(buf, size, "20%c%c-%c%c-%c%c", xbuf[0], xbuf[1],
347 	    xbuf[2], xbuf[3], xbuf[4], xbuf[5]);
348 }
349 
350 static void
351 print_sfp_vendor(struct i2c_info *ii, char *buf, size_t size)
352 {
353 	char xbuf[80];
354 
355 	memset(xbuf, 0, sizeof(xbuf));
356 	get_sfp_vendor_name(ii, xbuf, 20);
357 	get_sfp_vendor_pn(ii, &xbuf[20], 20);
358 	get_sfp_vendor_sn(ii, &xbuf[40], 20);
359 	get_sfp_vendor_date(ii, &xbuf[60], 20);
360 
361 	snprintf(buf, size, "vendor: %s PN: %s SN: %s DATE: %s",
362 	    xbuf, &xbuf[20],  &xbuf[40], &xbuf[60]);
363 }
364 
365 static void
366 get_sfp_temp(struct i2c_info *ii, char *buf, size_t size)
367 {
368 	char xbuf[2];
369 
370 	int8_t major;
371 	uint8_t minor;
372 	int k;
373 
374 	memset(xbuf, 0, sizeof(xbuf));
375 	ii->f(ii, SFF_8472_DIAG, SFF_8472_TEMP, 2, xbuf);
376 
377 	/* Convert temperature to string according to table 3.13 */
378 	major = (int8_t)xbuf[0];
379 	minor = (uint8_t)buf[1];
380 	k = minor * 1000 / 256;
381 
382 	snprintf(buf, size, "%d.%d C", major, k / 100);
383 }
384 
385 /*
386  * Converts value in @xbuf to both milliwats and dBm
387  * human representation.
388  */
389 static void
390 convert_power(struct i2c_info *ii, char *xbuf, char *buf, size_t size)
391 {
392 	uint16_t mW;
393 	double dbm;
394 
395 	mW = ((uint8_t)xbuf[0] << 8) + (uint8_t)xbuf[1];
396 
397 	/* Convert mw to dbm */
398 	dbm = 10.0 * log10(1.0 * mW / 10000);
399 
400 	/* Table 3.9, bit 5 is set, internally calibrated */
401 	if ((ii->diag_type & 0x20) != 0) {
402 		snprintf(buf, size, "%d.%02d mW (%.2f dBm)",
403 	    	    mW / 10000, (mW % 10000) / 100, dbm);
404 	}
405 }
406 
407 static void
408 get_sfp_rx_power(struct i2c_info *ii, char *buf, size_t size)
409 {
410 	char xbuf[2];
411 
412 	memset(xbuf, 0, sizeof(xbuf));
413 	ii->f(ii, SFF_8472_DIAG, SFF_8472_RX_POWER, 2, xbuf);
414 	convert_power(ii, xbuf, buf, size);
415 }
416 
417 static void
418 get_sfp_tx_power(struct i2c_info *ii, char *buf, size_t size)
419 {
420 	char xbuf[2];
421 
422 	memset(xbuf, 0, sizeof(xbuf));
423 	ii->f(ii, SFF_8472_DIAG, SFF_8472_TX_POWER, 2, xbuf);
424 	convert_power(ii, xbuf, buf, size);
425 }
426 
427 /* Intel ixgbe-specific structures and handlers */
428 struct ixgbe_i2c_req {
429 	uint8_t dev_addr;
430 	uint8_t	offset;
431 	uint8_t len;
432 	uint8_t data[8];
433 };
434 #define	SIOCGI2C	SIOCGIFGENERIC
435 
436 static int
437 read_i2c_ixgbe(struct i2c_info *ii, uint8_t addr, uint8_t off, uint8_t len,
438     caddr_t buf)
439 {
440 	struct ixgbe_i2c_req ixreq;
441 	int i;
442 
443 	if (ii->error != 0)
444 		return (ii->error);
445 
446 	ii->ifr->ifr_data = (caddr_t)&ixreq;
447 
448 	memset(&ixreq, 0, sizeof(ixreq));
449 	ixreq.dev_addr = addr;
450 
451 	for (i = 0; i < len; i += 1) {
452 		ixreq.offset = off + i;
453 		ixreq.len = 1;
454 		ixreq.data[0] = '\0';
455 
456 		if (ioctl(ii->s, SIOCGI2C, ii->ifr) != 0) {
457 			ii->error = errno;
458 			return (errno);
459 		}
460 		memcpy(&buf[i], ixreq.data, 1);
461 	}
462 
463 	return (0);
464 }
465 
466 void
467 sfp_status(int s, struct ifreq *ifr, int verbose)
468 {
469 	struct i2c_info ii;
470 	char buf[80], buf2[40], buf3[40];
471 
472 	/*
473 	 * Check if we have i2c support for particular driver.
474 	 * TODO: Determine driver by original name.
475 	 */
476 	memset(&ii, 0, sizeof(ii));
477 	if (strncmp(ifr->ifr_name, "ix", 2) == 0) {
478 		ii.f = read_i2c_ixgbe;
479 	} else
480 		return;
481 
482 	/* Prepare necessary into to pass to NIC handler */
483 	ii.s = s;
484 	ii.ifr = ifr;
485 
486 	/* Read diagnostic monitoring type */
487 	ii.f(&ii, SFF_8472_BASE, SFF_8472_DIAG_TYPE, 1, (caddr_t)&ii.diag_type);
488 
489 	/* Transceiver type */
490 	get_sfp_identifier(&ii, buf, sizeof(buf));
491 	get_sfp_transceiver_class(&ii, buf2, sizeof(buf2));
492 	get_sfp_connector(&ii, buf3, sizeof(buf3));
493 	if (ii.error == 0)
494 		printf("\ti2c: %s %s (%s)\n", buf, buf2, buf3);
495 	if (verbose > 2)
496 		printf_sfp_transceiver_descr(&ii, buf, sizeof(buf));
497 	print_sfp_vendor(&ii, buf, sizeof(buf));
498 	if (ii.error == 0)
499 		printf("\t%s\n", buf);
500 
501 	/*
502 	 * Request current measurements iff they are provided:
503 	 * Bit 6 must be set.
504 	 */
505 	if ((ii.diag_type & 0x40) != 0) {
506 		get_sfp_temp(&ii, buf, sizeof(buf));
507 		get_sfp_rx_power(&ii, buf2, sizeof(buf2));
508 		get_sfp_tx_power(&ii, buf3, sizeof(buf3));
509 		printf("\tTemp: %s RX: %s TX: %s\n", buf, buf2, buf3);
510 	}
511 }
512 
513