2 * Copyright (c) 2014 Alexander V. Chernikov. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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
27 static const char rcsid[] =
31 #include <sys/types.h>
32 #include <sys/param.h>
33 #include <sys/ioctl.h>
34 #include <sys/socket.h>
37 #include <net/sff8436.h>
38 #include <net/sff8472.h>
52 typedef int (read_i2c)(struct i2c_info *ii, uint8_t addr, uint8_t off,
53 uint8_t len, caddr_t buf);
75 const char *find_value(struct _nv *x, int value);
76 const char *find_zero_bit(struct _nv *x, int value, int sz);
78 /* SFF-8472 Rev. 11.4 table 3.4: Connector values */
79 static struct _nv conn[] = {
82 { 0x02, "Fibre Channel Style 1 copper" },
83 { 0x03, "Fibre Channel Style 2 copper" },
85 { 0x05, "Fibre Channel coaxial" },
86 { 0x06, "FiberJack" },
91 { 0x0B, "Optical pigtail" },
92 { 0x0C, "MPO Parallel Optic" },
94 { 0x21, "Copper pigtail" },
96 { 0x23, "No separate connector" }, /* SFF-8436 */
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" },
109 { 0x02, "1X Copper Active" },
110 { 0x01, "1X Copper Passive" },
114 /* Ethernet compliance codes, byte 6 */
115 static struct _nv eth_compat[] = {
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" },
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" },
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" },
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" },
160 { 0x01, "Single Mode" },
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" },
176 /* SFF-8436 Rev. 4.8 table 33: Specification compliance */
178 /* 10/40G Ethernet compliance codes, byte 128 + 3 */
179 static struct _nv eth_1040g[] = {
180 { 0x80, "Reserved" },
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" },
192 find_value(struct _nv *x, int value)
194 for (; x->n != NULL; x++)
201 find_zero_bit(struct _nv *x, int value, int sz)
207 for (v = 1, m = 1 << (8 * sz); v < m; v *= 2) {
208 if ((value & v) == 0)
210 if ((s = find_value(x, value & v)) != NULL) {
220 convert_sff_identifier(char *buf, size_t size, uint8_t value)
225 if (value <= SFF_8024_ID_LAST)
226 x = sff_8024_id[value];
229 x = "Vendor specific";
234 snprintf(buf, size, "%s", x);
238 convert_sff_connector(char *buf, size_t size, uint8_t value)
242 if ((x = find_value(conn, value)) == NULL) {
243 if (value >= 0x0D && value <= 0x1F)
245 else if (value >= 0x24 && value <= 0x7F)
248 x = "Vendor specific";
251 snprintf(buf, size, "%s", x);
255 get_sfp_identifier(struct i2c_info *ii, char *buf, size_t size)
259 ii->f(ii, SFF_8472_BASE, SFF_8472_ID, 1, (caddr_t)&data);
260 convert_sff_identifier(buf, size, data);
264 get_sfp_connector(struct i2c_info *ii, char *buf, size_t size)
268 ii->f(ii, SFF_8472_BASE, SFF_8472_CONNECTOR, 1, (caddr_t)&data);
269 convert_sff_connector(buf, size, data);
273 get_qsfp_identifier(struct i2c_info *ii, char *buf, size_t size)
277 ii->f(ii, SFF_8436_BASE, SFF_8436_ID, 1, (caddr_t)&data);
278 convert_sff_identifier(buf, size, data);
282 get_qsfp_connector(struct i2c_info *ii, char *buf, size_t size)
286 ii->f(ii, SFF_8436_BASE, SFF_8436_CONNECTOR, 1, (caddr_t)&data);
287 convert_sff_connector(buf, size, data);
291 printf_sfp_transceiver_descr(struct i2c_info *ii, char *buf, size_t size)
294 const char *tech_class, *tech_len, *tech_tech, *tech_media, *tech_speed;
302 /* Read bytes 3-10 at once */
303 ii->f(ii, SFF_8472_BASE, SFF_8472_TRANS_START, 8, &xbuf[3]);
305 /* Check 10G ethernet first */
306 tech_class = find_zero_bit(eth_10g, xbuf[3], 1);
307 if (tech_class == NULL) {
308 /* No match. Try 1G */
309 tech_class = find_zero_bit(eth_compat, xbuf[6], 1);
312 tech_len = find_zero_bit(fc_len, xbuf[7], 1);
313 tech_tech = find_zero_bit(cab_tech, xbuf[7] << 8 | xbuf[8], 2);
314 tech_media = find_zero_bit(fc_media, xbuf[9], 1);
315 tech_speed = find_zero_bit(fc_speed, xbuf[10], 1);
317 printf("Class: %s\n", tech_class);
318 printf("Length: %s\n", tech_len);
319 printf("Tech: %s\n", tech_tech);
320 printf("Media: %s\n", tech_media);
321 printf("Speed: %s\n", tech_speed);
325 get_sfp_transceiver_class(struct i2c_info *ii, char *buf, size_t size)
327 const char *tech_class;
330 /* Check 10G Ethernet/IB first */
331 ii->f(ii, SFF_8472_BASE, SFF_8472_TRANS_START, 1, (caddr_t)&code);
332 tech_class = find_zero_bit(eth_10g, code, 1);
333 if (tech_class == NULL) {
334 /* No match. Try Ethernet 1G */
335 ii->f(ii, SFF_8472_BASE, SFF_8472_TRANS_START + 3,
337 tech_class = find_zero_bit(eth_compat, code, 1);
340 if (tech_class == NULL)
341 tech_class = "Unknown";
343 snprintf(buf, size, "%s", tech_class);
347 get_qsfp_transceiver_class(struct i2c_info *ii, char *buf, size_t size)
349 const char *tech_class;
352 /* Check 10/40G Ethernet class only */
353 ii->f(ii, SFF_8436_BASE, SFF_8436_CODE_E1040G, 1, (caddr_t)&code);
354 tech_class = find_zero_bit(eth_1040g, code, 1);
355 if (tech_class == NULL)
356 tech_class = "Unknown";
358 snprintf(buf, size, "%s", tech_class);
362 * Print SFF-8472/SFF-8436 string to supplied buffer.
363 * All (vendor-specific) strings are padded right with '0x20'.
366 convert_sff_name(char *buf, size_t size, char *xbuf)
370 for (p = &xbuf[16]; *(p - 1) == 0x20; p--)
373 snprintf(buf, size, "%s", xbuf);
377 convert_sff_date(char *buf, size_t size, char *xbuf)
380 snprintf(buf, size, "20%c%c-%c%c-%c%c", xbuf[0], xbuf[1],
381 xbuf[2], xbuf[3], xbuf[4], xbuf[5]);
385 get_sfp_vendor_name(struct i2c_info *ii, char *buf, size_t size)
389 memset(xbuf, 0, sizeof(xbuf));
390 ii->f(ii, SFF_8472_BASE, SFF_8472_VENDOR_START, 16, xbuf);
391 convert_sff_name(buf, size, xbuf);
395 get_sfp_vendor_pn(struct i2c_info *ii, char *buf, size_t size)
399 memset(xbuf, 0, sizeof(xbuf));
400 ii->f(ii, SFF_8472_BASE, SFF_8472_PN_START, 16, xbuf);
401 convert_sff_name(buf, size, xbuf);
405 get_sfp_vendor_sn(struct i2c_info *ii, char *buf, size_t size)
409 memset(xbuf, 0, sizeof(xbuf));
410 ii->f(ii, SFF_8472_BASE, SFF_8472_SN_START, 16, xbuf);
411 convert_sff_name(buf, size, xbuf);
415 get_sfp_vendor_date(struct i2c_info *ii, char *buf, size_t size)
419 memset(xbuf, 0, sizeof(xbuf));
420 /* Date code, see Table 3.8 for description */
421 ii->f(ii, SFF_8472_BASE, SFF_8472_DATE_START, 6, xbuf);
422 convert_sff_date(buf, size, xbuf);
426 get_qsfp_vendor_name(struct i2c_info *ii, char *buf, size_t size)
430 memset(xbuf, 0, sizeof(xbuf));
431 ii->f(ii, SFF_8436_BASE, SFF_8436_VENDOR_START, 16, xbuf);
432 convert_sff_name(buf, size, xbuf);
436 get_qsfp_vendor_pn(struct i2c_info *ii, char *buf, size_t size)
440 memset(xbuf, 0, sizeof(xbuf));
441 ii->f(ii, SFF_8436_BASE, SFF_8436_PN_START, 16, xbuf);
442 convert_sff_name(buf, size, xbuf);
446 get_qsfp_vendor_sn(struct i2c_info *ii, char *buf, size_t size)
450 memset(xbuf, 0, sizeof(xbuf));
451 ii->f(ii, SFF_8436_BASE, SFF_8436_SN_START, 16, xbuf);
452 convert_sff_name(buf, size, xbuf);
456 get_qsfp_vendor_date(struct i2c_info *ii, char *buf, size_t size)
460 memset(xbuf, 0, sizeof(xbuf));
461 ii->f(ii, SFF_8436_BASE, SFF_8436_DATE_START, 6, xbuf);
462 convert_sff_date(buf, size, xbuf);
466 print_sfp_vendor(struct i2c_info *ii, char *buf, size_t size)
470 memset(xbuf, 0, sizeof(xbuf));
472 get_qsfp_vendor_name(ii, xbuf, 20);
473 get_qsfp_vendor_pn(ii, &xbuf[20], 20);
474 get_qsfp_vendor_sn(ii, &xbuf[40], 20);
475 get_qsfp_vendor_date(ii, &xbuf[60], 20);
477 get_sfp_vendor_name(ii, xbuf, 20);
478 get_sfp_vendor_pn(ii, &xbuf[20], 20);
479 get_sfp_vendor_sn(ii, &xbuf[40], 20);
480 get_sfp_vendor_date(ii, &xbuf[60], 20);
483 snprintf(buf, size, "vendor: %s PN: %s SN: %s DATE: %s",
484 xbuf, &xbuf[20], &xbuf[40], &xbuf[60]);
488 * Converts internal templerature (SFF-8472, SFF-8436)
489 * 16-bit unsigned value to human-readable representation:
491 * Internally measured Module temperature are represented
492 * as a 16-bit signed twos complement value in increments of
493 * 1/256 degrees Celsius, yielding a total range of –128C to +128C
494 * that is considered valid between –40 and +125C.
498 convert_sff_temp(char *buf, size_t size, char *xbuf)
502 d = (double)(int8_t)xbuf[0];
503 d += (double)(uint8_t)xbuf[1] / 256;
505 snprintf(buf, size, "%.2f C", d);
509 * Retrieves supplied voltage (SFF-8472, SFF-8436).
510 * 16-bit usigned value, treated as range 0..+6.55 Volts
513 convert_sff_voltage(char *buf, size_t size, char *xbuf)
517 d = (double)(((uint8_t)xbuf[0] << 8) | (uint8_t)xbuf[1]);
518 snprintf(buf, size, "%.2f Volts", d / 10000);
522 * Converts value in @xbuf to both milliwats and dBm
523 * human representation.
526 convert_sff_power(struct i2c_info *ii, char *buf, size_t size, char *xbuf)
531 mW = ((uint8_t)xbuf[0] << 8) + (uint8_t)xbuf[1];
533 /* Convert mw to dbm */
534 dbm = 10.0 * log10(1.0 * mW / 10000);
537 * Assume internally-calibrated data.
538 * This is always true for SFF-8346, and explicitly
539 * checked for SFF-8472.
542 /* Table 3.9, bit 5 is set, internally calibrated */
543 snprintf(buf, size, "%d.%02d mW (%.2f dBm)",
544 mW / 10000, (mW % 10000) / 100, dbm);
548 get_sfp_temp(struct i2c_info *ii, char *buf, size_t size)
552 memset(xbuf, 0, sizeof(xbuf));
553 ii->f(ii, SFF_8472_DIAG, SFF_8472_TEMP, 2, xbuf);
554 convert_sff_temp(buf, size, xbuf);
558 get_sfp_voltage(struct i2c_info *ii, char *buf, size_t size)
562 memset(xbuf, 0, sizeof(xbuf));
563 ii->f(ii, SFF_8472_DIAG, SFF_8472_VCC, 2, xbuf);
564 convert_sff_voltage(buf, size, xbuf);
568 get_qsfp_temp(struct i2c_info *ii, char *buf, size_t size)
572 memset(xbuf, 0, sizeof(xbuf));
573 ii->f(ii, SFF_8436_BASE, SFF_8436_TEMP, 2, xbuf);
574 convert_sff_temp(buf, size, xbuf);
578 get_qsfp_voltage(struct i2c_info *ii, char *buf, size_t size)
582 memset(xbuf, 0, sizeof(xbuf));
583 ii->f(ii, SFF_8436_BASE, SFF_8436_VCC, 2, xbuf);
584 convert_sff_voltage(buf, size, xbuf);
588 get_sfp_rx_power(struct i2c_info *ii, char *buf, size_t size)
592 memset(xbuf, 0, sizeof(xbuf));
593 ii->f(ii, SFF_8472_DIAG, SFF_8472_RX_POWER, 2, xbuf);
594 convert_sff_power(ii, buf, size, xbuf);
598 get_sfp_tx_power(struct i2c_info *ii, char *buf, size_t size)
602 memset(xbuf, 0, sizeof(xbuf));
603 ii->f(ii, SFF_8472_DIAG, SFF_8472_TX_POWER, 2, xbuf);
604 convert_sff_power(ii, buf, size, xbuf);
608 get_qsfp_rx_power(struct i2c_info *ii, char *buf, size_t size, int chan)
612 memset(xbuf, 0, sizeof(xbuf));
613 ii->f(ii, SFF_8436_BASE, SFF_8436_RX_CH1_MSB + (chan - 1) * 2, 2, xbuf);
614 convert_sff_power(ii, buf, size, xbuf);
618 get_qsfp_tx_power(struct i2c_info *ii, char *buf, size_t size, int chan)
622 memset(xbuf, 0, sizeof(xbuf));
623 ii->f(ii, SFF_8436_BASE, SFF_8436_TX_CH1_MSB + (chan -1) * 2, 2, xbuf);
624 convert_sff_power(ii, buf, size, xbuf);
627 /* Generic handler */
629 read_i2c_generic(struct i2c_info *ii, uint8_t addr, uint8_t off, uint8_t len,
638 ii->ifr->ifr_data = (caddr_t)&req;
642 memset(&req, 0, sizeof(req));
648 l = (len > sizeof(req.data)) ? sizeof(req.data) : len;
650 if (ioctl(ii->s, SIOCGI2C, ii->ifr) != 0) {
655 memcpy(&buf[i], req.data, l);
665 print_qsfp_status(struct i2c_info *ii, int verbose)
667 char buf[80], buf2[40], buf3[40];
671 /* Read diagnostic monitoring type */
672 ii->f(ii, SFF_8436_BASE, SFF_8436_DIAG_TYPE, 1, (caddr_t)&diag_type);
677 * Read monitoring data it is supplied.
678 * XXX: It is not exactly clear from standard
679 * how one can specify lack of measurements (passive cables case).
685 /* Transceiver type */
686 get_qsfp_identifier(ii, buf, sizeof(buf));
687 get_qsfp_transceiver_class(ii, buf2, sizeof(buf2));
688 get_qsfp_connector(ii, buf3, sizeof(buf3));
690 printf("\tplugged: %s %s (%s)\n", buf, buf2, buf3);
691 print_sfp_vendor(ii, buf, sizeof(buf));
693 printf("\t%s\n", buf);
695 /* Request current measurements if they are provided: */
696 if (ii->do_diag != 0) {
697 get_qsfp_temp(ii, buf, sizeof(buf));
698 get_qsfp_voltage(ii, buf2, sizeof(buf2));
699 printf("\tmodule temperature: %s voltage: %s\n", buf, buf2);
700 for (i = 1; i <= 4; i++) {
701 get_qsfp_rx_power(ii, buf, sizeof(buf), i);
702 get_qsfp_tx_power(ii, buf2, sizeof(buf2), i);
703 printf("\tlane %d: RX: %s TX: %s\n", i, buf, buf2);
709 print_sfp_status(struct i2c_info *ii, int verbose)
711 char buf[80], buf2[40], buf3[40];
712 uint8_t diag_type, flags;
714 /* Read diagnostic monitoring type */
715 ii->f(ii, SFF_8472_BASE, SFF_8472_DIAG_TYPE, 1, (caddr_t)&diag_type);
720 * Read monitoring data IFF it is supplied AND is
721 * internally calibrated
723 flags = SFF_8472_DDM_DONE | SFF_8472_DDM_INTERNAL;
724 if ((diag_type & flags) == flags)
727 /* Transceiver type */
728 get_sfp_identifier(ii, buf, sizeof(buf));
729 get_sfp_transceiver_class(ii, buf2, sizeof(buf2));
730 get_sfp_connector(ii, buf3, sizeof(buf3));
732 printf("\tplugged: %s %s (%s)\n", buf, buf2, buf3);
734 printf_sfp_transceiver_descr(ii, buf, sizeof(buf));
735 print_sfp_vendor(ii, buf, sizeof(buf));
737 printf("\t%s\n", buf);
740 * Request current measurements iff they are provided:
742 if (ii->do_diag != 0) {
743 get_sfp_temp(ii, buf, sizeof(buf));
744 get_sfp_voltage(ii, buf2, sizeof(buf2));
745 printf("\tmodule temperature: %s Voltage: %s\n", buf, buf2);
746 get_sfp_rx_power(ii, buf, sizeof(buf));
747 get_sfp_tx_power(ii, buf2, sizeof(buf2));
748 printf("\tRX: %s TX: %s\n", buf, buf2);
753 sfp_status(int s, struct ifreq *ifr, int verbose)
757 memset(&ii, 0, sizeof(ii));
758 /* Prepare necessary into to pass to NIC handler */
763 * Check if we have i2c support for particular driver.
764 * TODO: Determine driver by original name.
766 if (strncmp(ifr->ifr_name, "ix", 2) == 0) {
767 ii.f = read_i2c_generic;
768 print_sfp_status(&ii, verbose);
769 } else if (strncmp(ifr->ifr_name, "cxl", 3) == 0) {
770 ii.port_id = atoi(&ifr->ifr_name[3]);
771 ii.f = read_i2c_generic;
773 print_qsfp_status(&ii, verbose);