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);
70 static void dump_i2c_data(struct i2c_info *ii, uint8_t addr, uint8_t off,
78 const char *find_value(struct _nv *x, int value);
79 const char *find_zero_bit(struct _nv *x, int value, int sz);
81 /* SFF-8472 Rev. 11.4 table 3.4: Connector values */
82 static struct _nv conn[] = {
85 { 0x02, "Fibre Channel Style 1 copper" },
86 { 0x03, "Fibre Channel Style 2 copper" },
88 { 0x05, "Fibre Channel coaxial" },
89 { 0x06, "FiberJack" },
94 { 0x0B, "Optical pigtail" },
95 { 0x0C, "MPO Parallel Optic" },
97 { 0x21, "Copper pigtail" },
99 { 0x23, "No separate connector" }, /* SFF-8436 */
103 /* SFF-8472 Rev. 11.4 table 3.5: Transceiver codes */
104 /* 10G Ethernet/IB compliance codes, byte 3 */
105 static struct _nv eth_10g[] = {
106 { 0x80, "10G Base-ER" },
107 { 0x40, "10G Base-LRM" },
108 { 0x20, "10G Base-LR" },
109 { 0x10, "10G Base-SR" },
112 { 0x02, "1X Copper Active" },
113 { 0x01, "1X Copper Passive" },
117 /* Ethernet compliance codes, byte 6 */
118 static struct _nv eth_compat[] = {
120 { 0x40, "BASE-BX10" },
121 { 0x20, "100BASE-FX" },
122 { 0x10, "100BASE-LX/LX10" },
123 { 0x08, "1000BASE-T" },
124 { 0x04, "1000BASE-CX" },
125 { 0x02, "1000BASE-LX" },
126 { 0x01, "1000BASE-SX" },
130 /* FC link length, byte 7 */
131 static struct _nv fc_len[] = {
132 { 0x80, "very long distance" },
133 { 0x40, "short distance" },
134 { 0x20, "intermediate distance" },
135 { 0x10, "long distance" },
136 { 0x08, "medium distance" },
140 /* Channel/Cable technology, byte 7-8 */
141 static struct _nv cab_tech[] = {
142 { 0x0400, "Shortwave laser (SA)" },
143 { 0x0200, "Longwave laser (LC)" },
144 { 0x0100, "Electrical inter-enclosure (EL)" },
145 { 0x80, "Electrical intra-enclosure (EL)" },
146 { 0x40, "Shortwave laser (SN)" },
147 { 0x20, "Shortwave laser (SL)" },
148 { 0x10, "Longwave laser (LL)" },
149 { 0x08, "Active Cable" },
150 { 0x04, "Passive Cable" },
154 /* FC Transmission media, byte 9 */
155 static struct _nv fc_media[] = {
156 { 0x80, "Twin Axial Pair" },
157 { 0x40, "Twisted Pair" },
158 { 0x20, "Miniature Coax" },
159 { 0x10, "Viao Coax" },
160 { 0x08, "Miltimode, 62.5um" },
161 { 0x04, "Multimode, 50um" },
163 { 0x01, "Single Mode" },
167 /* FC Speed, byte 10 */
168 static struct _nv fc_speed[] = {
169 { 0x80, "1200 MBytes/sec" },
170 { 0x40, "800 MBytes/sec" },
171 { 0x20, "1600 MBytes/sec" },
172 { 0x10, "400 MBytes/sec" },
173 { 0x08, "3200 MBytes/sec" },
174 { 0x04, "200 MBytes/sec" },
175 { 0x01, "100 MBytes/sec" },
179 /* SFF-8436 Rev. 4.8 table 33: Specification compliance */
181 /* 10/40G Ethernet compliance codes, byte 128 + 3 */
182 static struct _nv eth_1040g[] = {
183 { 0x80, "Reserved" },
184 { 0x40, "10GBASE-LRM" },
185 { 0x20, "10GBASE-LR" },
186 { 0x10, "10GBASE-SR" },
187 { 0x08, "40GBASE-CR4" },
188 { 0x04, "40GBASE-SR4" },
189 { 0x02, "40GBASE-LR4" },
190 { 0x01, "40G Active Cable" },
195 find_value(struct _nv *x, int value)
197 for (; x->n != NULL; x++)
204 find_zero_bit(struct _nv *x, int value, int sz)
210 for (v = 1, m = 1 << (8 * sz); v < m; v *= 2) {
211 if ((value & v) == 0)
213 if ((s = find_value(x, value & v)) != NULL) {
223 convert_sff_identifier(char *buf, size_t size, uint8_t value)
228 if (value <= SFF_8024_ID_LAST)
229 x = sff_8024_id[value];
232 x = "Vendor specific";
237 snprintf(buf, size, "%s", x);
241 convert_sff_connector(char *buf, size_t size, uint8_t value)
245 if ((x = find_value(conn, value)) == NULL) {
246 if (value >= 0x0D && value <= 0x1F)
248 else if (value >= 0x24 && value <= 0x7F)
251 x = "Vendor specific";
254 snprintf(buf, size, "%s", x);
258 get_sfp_identifier(struct i2c_info *ii, char *buf, size_t size)
262 ii->f(ii, SFF_8472_BASE, SFF_8472_ID, 1, (caddr_t)&data);
263 convert_sff_identifier(buf, size, data);
267 get_sfp_connector(struct i2c_info *ii, char *buf, size_t size)
271 ii->f(ii, SFF_8472_BASE, SFF_8472_CONNECTOR, 1, (caddr_t)&data);
272 convert_sff_connector(buf, size, data);
276 get_qsfp_identifier(struct i2c_info *ii, char *buf, size_t size)
280 ii->f(ii, SFF_8436_BASE, SFF_8436_ID, 1, (caddr_t)&data);
281 convert_sff_identifier(buf, size, data);
285 get_qsfp_connector(struct i2c_info *ii, char *buf, size_t size)
289 ii->f(ii, SFF_8436_BASE, SFF_8436_CONNECTOR, 1, (caddr_t)&data);
290 convert_sff_connector(buf, size, data);
294 printf_sfp_transceiver_descr(struct i2c_info *ii, char *buf, size_t size)
297 const char *tech_class, *tech_len, *tech_tech, *tech_media, *tech_speed;
305 /* Read bytes 3-10 at once */
306 ii->f(ii, SFF_8472_BASE, SFF_8472_TRANS_START, 8, &xbuf[3]);
308 /* Check 10G ethernet first */
309 tech_class = find_zero_bit(eth_10g, xbuf[3], 1);
310 if (tech_class == NULL) {
311 /* No match. Try 1G */
312 tech_class = find_zero_bit(eth_compat, xbuf[6], 1);
315 tech_len = find_zero_bit(fc_len, xbuf[7], 1);
316 tech_tech = find_zero_bit(cab_tech, xbuf[7] << 8 | xbuf[8], 2);
317 tech_media = find_zero_bit(fc_media, xbuf[9], 1);
318 tech_speed = find_zero_bit(fc_speed, xbuf[10], 1);
320 printf("Class: %s\n", tech_class);
321 printf("Length: %s\n", tech_len);
322 printf("Tech: %s\n", tech_tech);
323 printf("Media: %s\n", tech_media);
324 printf("Speed: %s\n", tech_speed);
328 get_sfp_transceiver_class(struct i2c_info *ii, char *buf, size_t size)
330 const char *tech_class;
333 unsigned char qbuf[8];
334 ii->f(ii, SFF_8472_BASE, SFF_8472_TRANS_START, 8, (caddr_t)qbuf);
336 /* Check 10G Ethernet/IB first */
337 ii->f(ii, SFF_8472_BASE, SFF_8472_TRANS_START, 1, (caddr_t)&code);
338 tech_class = find_zero_bit(eth_10g, code, 1);
339 if (tech_class == NULL) {
340 /* No match. Try Ethernet 1G */
341 ii->f(ii, SFF_8472_BASE, SFF_8472_TRANS_START + 3,
343 tech_class = find_zero_bit(eth_compat, code, 1);
346 if (tech_class == NULL)
347 tech_class = "Unknown";
349 snprintf(buf, size, "%s", tech_class);
353 get_qsfp_transceiver_class(struct i2c_info *ii, char *buf, size_t size)
355 const char *tech_class;
358 /* Check 10/40G Ethernet class only */
359 ii->f(ii, SFF_8436_BASE, SFF_8436_CODE_E1040G, 1, (caddr_t)&code);
360 tech_class = find_zero_bit(eth_1040g, code, 1);
361 if (tech_class == NULL)
362 tech_class = "Unknown";
364 snprintf(buf, size, "%s", tech_class);
368 * Print SFF-8472/SFF-8436 string to supplied buffer.
369 * All (vendor-specific) strings are padded right with '0x20'.
372 convert_sff_name(char *buf, size_t size, char *xbuf)
376 for (p = &xbuf[16]; *(p - 1) == 0x20; p--)
379 snprintf(buf, size, "%s", xbuf);
383 convert_sff_date(char *buf, size_t size, char *xbuf)
386 snprintf(buf, size, "20%c%c-%c%c-%c%c", xbuf[0], xbuf[1],
387 xbuf[2], xbuf[3], xbuf[4], xbuf[5]);
391 get_sfp_vendor_name(struct i2c_info *ii, char *buf, size_t size)
395 memset(xbuf, 0, sizeof(xbuf));
396 ii->f(ii, SFF_8472_BASE, SFF_8472_VENDOR_START, 16, xbuf);
397 convert_sff_name(buf, size, xbuf);
401 get_sfp_vendor_pn(struct i2c_info *ii, char *buf, size_t size)
405 memset(xbuf, 0, sizeof(xbuf));
406 ii->f(ii, SFF_8472_BASE, SFF_8472_PN_START, 16, xbuf);
407 convert_sff_name(buf, size, xbuf);
411 get_sfp_vendor_sn(struct i2c_info *ii, char *buf, size_t size)
415 memset(xbuf, 0, sizeof(xbuf));
416 ii->f(ii, SFF_8472_BASE, SFF_8472_SN_START, 16, xbuf);
417 convert_sff_name(buf, size, xbuf);
421 get_sfp_vendor_date(struct i2c_info *ii, char *buf, size_t size)
425 memset(xbuf, 0, sizeof(xbuf));
426 /* Date code, see Table 3.8 for description */
427 ii->f(ii, SFF_8472_BASE, SFF_8472_DATE_START, 6, xbuf);
428 convert_sff_date(buf, size, xbuf);
432 get_qsfp_vendor_name(struct i2c_info *ii, char *buf, size_t size)
436 memset(xbuf, 0, sizeof(xbuf));
437 ii->f(ii, SFF_8436_BASE, SFF_8436_VENDOR_START, 16, xbuf);
438 convert_sff_name(buf, size, xbuf);
442 get_qsfp_vendor_pn(struct i2c_info *ii, char *buf, size_t size)
446 memset(xbuf, 0, sizeof(xbuf));
447 ii->f(ii, SFF_8436_BASE, SFF_8436_PN_START, 16, xbuf);
448 convert_sff_name(buf, size, xbuf);
452 get_qsfp_vendor_sn(struct i2c_info *ii, char *buf, size_t size)
456 memset(xbuf, 0, sizeof(xbuf));
457 ii->f(ii, SFF_8436_BASE, SFF_8436_SN_START, 16, xbuf);
458 convert_sff_name(buf, size, xbuf);
462 get_qsfp_vendor_date(struct i2c_info *ii, char *buf, size_t size)
466 memset(xbuf, 0, sizeof(xbuf));
467 ii->f(ii, SFF_8436_BASE, SFF_8436_DATE_START, 6, xbuf);
468 convert_sff_date(buf, size, xbuf);
472 print_sfp_vendor(struct i2c_info *ii, char *buf, size_t size)
476 memset(xbuf, 0, sizeof(xbuf));
478 get_qsfp_vendor_name(ii, xbuf, 20);
479 get_qsfp_vendor_pn(ii, &xbuf[20], 20);
480 get_qsfp_vendor_sn(ii, &xbuf[40], 20);
481 get_qsfp_vendor_date(ii, &xbuf[60], 20);
483 get_sfp_vendor_name(ii, xbuf, 20);
484 get_sfp_vendor_pn(ii, &xbuf[20], 20);
485 get_sfp_vendor_sn(ii, &xbuf[40], 20);
486 get_sfp_vendor_date(ii, &xbuf[60], 20);
489 snprintf(buf, size, "vendor: %s PN: %s SN: %s DATE: %s",
490 xbuf, &xbuf[20], &xbuf[40], &xbuf[60]);
494 * Converts internal templerature (SFF-8472, SFF-8436)
495 * 16-bit unsigned value to human-readable representation:
497 * Internally measured Module temperature are represented
498 * as a 16-bit signed twos complement value in increments of
499 * 1/256 degrees Celsius, yielding a total range of –128C to +128C
500 * that is considered valid between –40 and +125C.
504 convert_sff_temp(char *buf, size_t size, char *xbuf)
508 d = (double)(int8_t)xbuf[0];
509 d += (double)(uint8_t)xbuf[1] / 256;
511 snprintf(buf, size, "%.2f C", d);
515 * Retrieves supplied voltage (SFF-8472, SFF-8436).
516 * 16-bit usigned value, treated as range 0..+6.55 Volts
519 convert_sff_voltage(char *buf, size_t size, char *xbuf)
523 d = (double)(((uint8_t)xbuf[0] << 8) | (uint8_t)xbuf[1]);
524 snprintf(buf, size, "%.2f Volts", d / 10000);
528 * Converts value in @xbuf to both milliwats and dBm
529 * human representation.
532 convert_sff_power(struct i2c_info *ii, char *buf, size_t size, char *xbuf)
537 mW = ((uint8_t)xbuf[0] << 8) + (uint8_t)xbuf[1];
539 /* Convert mw to dbm */
540 dbm = 10.0 * log10(1.0 * mW / 10000);
543 * Assume internally-calibrated data.
544 * This is always true for SFF-8346, and explicitly
545 * checked for SFF-8472.
548 /* Table 3.9, bit 5 is set, internally calibrated */
549 snprintf(buf, size, "%d.%02d mW (%.2f dBm)",
550 mW / 10000, (mW % 10000) / 100, dbm);
554 get_sfp_temp(struct i2c_info *ii, char *buf, size_t size)
558 memset(xbuf, 0, sizeof(xbuf));
559 ii->f(ii, SFF_8472_DIAG, SFF_8472_TEMP, 2, xbuf);
560 convert_sff_temp(buf, size, xbuf);
564 get_sfp_voltage(struct i2c_info *ii, char *buf, size_t size)
568 memset(xbuf, 0, sizeof(xbuf));
569 ii->f(ii, SFF_8472_DIAG, SFF_8472_VCC, 2, xbuf);
570 convert_sff_voltage(buf, size, xbuf);
574 get_qsfp_temp(struct i2c_info *ii, char *buf, size_t size)
578 memset(xbuf, 0, sizeof(xbuf));
579 ii->f(ii, SFF_8436_BASE, SFF_8436_TEMP, 2, xbuf);
580 convert_sff_temp(buf, size, xbuf);
584 get_qsfp_voltage(struct i2c_info *ii, char *buf, size_t size)
588 memset(xbuf, 0, sizeof(xbuf));
589 ii->f(ii, SFF_8436_BASE, SFF_8436_VCC, 2, xbuf);
590 convert_sff_voltage(buf, size, xbuf);
594 get_sfp_rx_power(struct i2c_info *ii, char *buf, size_t size)
598 memset(xbuf, 0, sizeof(xbuf));
599 ii->f(ii, SFF_8472_DIAG, SFF_8472_RX_POWER, 2, xbuf);
600 convert_sff_power(ii, buf, size, xbuf);
604 get_sfp_tx_power(struct i2c_info *ii, char *buf, size_t size)
608 memset(xbuf, 0, sizeof(xbuf));
609 ii->f(ii, SFF_8472_DIAG, SFF_8472_TX_POWER, 2, xbuf);
610 convert_sff_power(ii, buf, size, xbuf);
614 get_qsfp_rx_power(struct i2c_info *ii, char *buf, size_t size, int chan)
618 memset(xbuf, 0, sizeof(xbuf));
619 ii->f(ii, SFF_8436_BASE, SFF_8436_RX_CH1_MSB + (chan - 1) * 2, 2, xbuf);
620 convert_sff_power(ii, buf, size, xbuf);
624 get_qsfp_tx_power(struct i2c_info *ii, char *buf, size_t size, int chan)
628 memset(xbuf, 0, sizeof(xbuf));
629 ii->f(ii, SFF_8436_BASE, SFF_8436_TX_CH1_MSB + (chan -1) * 2, 2, xbuf);
630 convert_sff_power(ii, buf, size, xbuf);
633 /* Generic handler */
635 read_i2c_generic(struct i2c_info *ii, uint8_t addr, uint8_t off, uint8_t len,
644 ii->ifr->ifr_data = (caddr_t)&req;
648 memset(&req, 0, sizeof(req));
654 l = (len > sizeof(req.data)) ? sizeof(req.data) : len;
656 if (ioctl(ii->s, SIOCGI2C, ii->ifr) != 0) {
661 memcpy(&buf[i], req.data, l);
671 dump_i2c_data(struct i2c_info *ii, uint8_t addr, uint8_t off, uint8_t len)
673 unsigned char buf[16];
677 memset(buf, 0, sizeof(buf));
678 read = (len > sizeof(buf)) ? sizeof(buf) : len;
679 ii->f(ii, addr, off, read, buf);
680 if (ii->error != 0) {
681 fprintf(stderr, "Error reading i2c info\n");
686 for (i = 0; i < read; i++)
687 printf("%02X ", buf[i]);
695 print_qsfp_status(struct i2c_info *ii, int verbose)
697 char buf[80], buf2[40], buf3[40];
701 /* Read diagnostic monitoring type */
702 ii->f(ii, SFF_8436_BASE, SFF_8436_DIAG_TYPE, 1, (caddr_t)&diag_type);
707 * Read monitoring data it is supplied.
708 * XXX: It is not exactly clear from standard
709 * how one can specify lack of measurements (passive cables case).
715 /* Transceiver type */
716 get_qsfp_identifier(ii, buf, sizeof(buf));
717 get_qsfp_transceiver_class(ii, buf2, sizeof(buf2));
718 get_qsfp_connector(ii, buf3, sizeof(buf3));
720 printf("\tplugged: %s %s (%s)\n", buf, buf2, buf3);
721 print_sfp_vendor(ii, buf, sizeof(buf));
723 printf("\t%s\n", buf);
725 /* Request current measurements if they are provided: */
726 if (ii->do_diag != 0) {
727 get_qsfp_temp(ii, buf, sizeof(buf));
728 get_qsfp_voltage(ii, buf2, sizeof(buf2));
729 printf("\tmodule temperature: %s voltage: %s\n", buf, buf2);
730 for (i = 1; i <= 4; i++) {
731 get_qsfp_rx_power(ii, buf, sizeof(buf), i);
732 get_qsfp_tx_power(ii, buf2, sizeof(buf2), i);
733 printf("\tlane %d: RX: %s TX: %s\n", i, buf, buf2);
738 printf("\n\tSFF8436 DUMP (0xA0 128..255 range):\n");
739 dump_i2c_data(ii, SFF_8436_BASE, 128, 128);
740 printf("\n\tSFF8436 DUMP (0xA0 0..81 range):\n");
741 dump_i2c_data(ii, SFF_8436_BASE, 0, 82);
746 print_sfp_status(struct i2c_info *ii, int verbose)
748 char buf[80], buf2[40], buf3[40];
749 uint8_t diag_type, flags;
751 /* Read diagnostic monitoring type */
752 ii->f(ii, SFF_8472_BASE, SFF_8472_DIAG_TYPE, 1, (caddr_t)&diag_type);
757 * Read monitoring data IFF it is supplied AND is
758 * internally calibrated
760 flags = SFF_8472_DDM_DONE | SFF_8472_DDM_INTERNAL;
761 if ((diag_type & flags) == flags)
764 /* Transceiver type */
765 get_sfp_identifier(ii, buf, sizeof(buf));
766 get_sfp_transceiver_class(ii, buf2, sizeof(buf2));
767 get_sfp_connector(ii, buf3, sizeof(buf3));
769 printf("\tplugged: %s %s (%s)\n", buf, buf2, buf3);
770 print_sfp_vendor(ii, buf, sizeof(buf));
772 printf("\t%s\n", buf);
775 printf_sfp_transceiver_descr(ii, buf, sizeof(buf));
777 * Request current measurements iff they are provided:
779 if (ii->do_diag != 0) {
780 get_sfp_temp(ii, buf, sizeof(buf));
781 get_sfp_voltage(ii, buf2, sizeof(buf2));
782 printf("\tmodule temperature: %s Voltage: %s\n", buf, buf2);
783 get_sfp_rx_power(ii, buf, sizeof(buf));
784 get_sfp_tx_power(ii, buf2, sizeof(buf2));
785 printf("\tRX: %s TX: %s\n", buf, buf2);
789 printf("\n\tSFF8472 DUMP (0xA0 0..127 range):\n");
790 dump_i2c_data(ii, SFF_8472_BASE, 0, 128);
795 sfp_status(int s, struct ifreq *ifr, int verbose)
800 memset(&ii, 0, sizeof(ii));
801 /* Prepare necessary into to pass to NIC handler */
804 ii.f = read_i2c_generic;
807 * Try to read byte 0 from i2c:
808 * Both SFF-8472 and SFF-8436 use it as
809 * 'identification byte'.
810 * Stop reading status on zero as value -
811 * this might happen in case of empty transceiver slot.
814 ii.f(&ii, SFF_8472_BASE, SFF_8472_ID, 1, (caddr_t)&id_byte);
815 if (ii.error != 0 || id_byte == 0)
819 case SFF_8024_ID_QSFP:
820 case SFF_8024_ID_QSFPPLUS:
821 print_qsfp_status(&ii, verbose);
824 print_sfp_status(&ii, verbose);