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 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 */
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,
69 const char *find_value(struct _nv *x, int value);
70 const char *find_zero_bit(struct _nv *x, int value, int sz);
72 /* SFF-8024 Rev. 4.6 Table 4-3: Connector Types */
73 static struct _nv conn[] = {
76 { 0x02, "Fibre Channel Style 1 copper" },
77 { 0x03, "Fibre Channel Style 2 copper" },
79 { 0x05, "Fibre Channel coaxial" },
80 { 0x06, "Fiber Jack" },
85 { 0x0B, "Optical pigtail" },
86 { 0x0C, "MPO 1x12 Parallel Optic" },
87 { 0x0D, "MPO 2x16 Parallel Optic" },
89 { 0x21, "Copper pigtail" },
91 { 0x23, "No separable connector" },
93 { 0x25, "CS optical connector" },
94 { 0x26, "Mini CS optical connector" },
95 { 0x27, "MPO 2x12 Parallel Optic" },
96 { 0x28, "MPO 1x16 Parallel Optic" },
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, "Extended" },
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" },
190 #define SFF_8636_EXT_COMPLIANCE 0x80
192 /* SFF-8024 Rev. 4.6 table 4-4: Extended Specification Compliance */
193 static struct _nv eth_extended_comp[] = {
194 { 0xFF, "Reserved" },
195 { 0x55, "128GFC LW" },
196 { 0x54, "128GFC SW" },
197 { 0x53, "128GFC EA" },
198 { 0x52, "64GFC LW" },
199 { 0x51, "64GFC SW" },
200 { 0x50, "64GFC EA" },
201 { 0x4F, "Reserved" },
202 { 0x4E, "Reserved" },
203 { 0x4D, "Reserved" },
204 { 0x4C, "Reserved" },
205 { 0x4B, "Reserved" },
206 { 0x4A, "Reserved" },
207 { 0x49, "Reserved" },
208 { 0x48, "Reserved" },
209 { 0x47, "Reserved" },
210 { 0x46, "200GBASE-LR4" },
211 { 0x45, "50GBASE-LR" },
212 { 0x44, "200G 1550nm PSM4" },
213 { 0x43, "200GBASE-FR4" },
214 { 0x42, "50GBASE-FR or 200GBASE-DR4" },
215 { 0x41, "50GBASE-SR/100GBASE-SR2/200GBASE-SR4" },
216 { 0x40, "50GBASE-CR/100GBASE-CR2/200GBASE-CR4" },
217 { 0x3F, "Reserved" },
218 { 0x3E, "Reserved" },
219 { 0x3D, "Reserved" },
220 { 0x3C, "Reserved" },
221 { 0x3B, "Reserved" },
222 { 0x3A, "Reserved" },
223 { 0x39, "Reserved" },
224 { 0x38, "Reserved" },
225 { 0x37, "Reserved" },
226 { 0x36, "Reserved" },
227 { 0x35, "Reserved" },
228 { 0x34, "Reserved" },
229 { 0x33, "50GAUI/100GAUI-2/200GAUI-4 AOC (BER <2.6e-4)" },
230 { 0x32, "50GAUI/100GAUI-2/200GAUI-4 ACC (BER <2.6e-4)" },
231 { 0x31, "50GAUI/100GAUI-2/200GAUI-4 AOC (BER <1e-6)" },
232 { 0x30, "50GAUI/100GAUI-2/200GAUI-4 ACC (BER <1e-6)" },
233 { 0x2F, "Reserved" },
234 { 0x2E, "Reserved" },
235 { 0x2D, "Reserved" },
236 { 0x2C, "Reserved" },
237 { 0x2B, "Reserved" },
238 { 0x2A, "Reserved" },
239 { 0x29, "Reserved" },
240 { 0x28, "Reserved" },
243 { 0x25, "100GBASE-DR" },
244 { 0x24, "4WDM-40 MSA" },
245 { 0x23, "4WDM-20 MSA" },
246 { 0x22, "4WDM-10 MSA" },
247 { 0x21, "100G PAM4 BiDi" },
248 { 0x20, "100G SWDM4" },
249 { 0x1F, "40G SWDM4" },
250 { 0x1E, "2.5GBASE-T" },
251 { 0x1D, "5GBASE-T" },
252 { 0x1C, "10GBASE-T Short Reach" },
253 { 0x1B, "100G 1550nm WDM" },
254 { 0x1A, "100GE-DWDM2" },
255 { 0x19, "100G ACC or 25GAUI C2M ACC" },
256 { 0x18, "100G AOC or 25GAUI C2M AOC" },
257 { 0x17, "100G CLR4" },
258 { 0x16, "10GBASE-T with SFI electrical interface" },
259 { 0x15, "G959.1 profile P1L1-2D2" },
260 { 0x14, "G959.1 profile P1S1-2D2" },
261 { 0x13, "G959.1 profile P1I1-2D1" },
262 { 0x12, "40G PSM4 Parallel SMF" },
263 { 0x11, "4 x 10GBASE-SR" },
264 { 0x10, "40GBASE-ER4" },
265 { 0x0F, "Reserved" },
266 { 0x0E, "Reserved" },
267 { 0x0D, "25GBASE-CR CA-25G-N" },
268 { 0x0C, "25GBASE-CR CA-25G-S" },
269 { 0x0B, "100GBASE-CR4 or 25GBASE-CR CA-L" },
270 { 0x0A, "Reserved" },
271 { 0x09, "Obsolete" },
272 { 0x08, "100G ACC (Active Copper Cable) or 25GAUI C2M ACC" },
273 { 0x07, "100G PSM4 Parallel SMF" },
274 { 0x06, "100G CWDM4" },
275 { 0x05, "100GBASE-SR10" },
276 { 0x04, "100GBASE-ER4 or 25GBASE-ER" },
277 { 0x03, "100GBASE-LR4 or 25GBASE-LR" },
278 { 0x02, "100GBASE-SR4 or 25GBASE-SR" },
279 { 0x01, "100G AOC (Active Optical Cable) or 25GAUI C2M AOC" },
280 { 0x00, "Unspecified" }
283 /* SFF-8636 Rev. 2.9 table 6.3: Revision compliance */
284 static struct _nv rev_compl[] = {
285 { 0x1, "SFF-8436 rev <=4.8" },
286 { 0x2, "SFF-8436 rev <=4.8" },
287 { 0x3, "SFF-8636 rev <=1.3" },
288 { 0x4, "SFF-8636 rev <=1.4" },
289 { 0x5, "SFF-8636 rev <=1.5" },
290 { 0x6, "SFF-8636 rev <=2.0" },
291 { 0x7, "SFF-8636 rev <=2.7" },
292 { 0x8, "SFF-8636 rev >=2.8" },
293 { 0x0, "Unspecified" }
297 find_value(struct _nv *x, int value)
299 for (; x->n != NULL; x++)
306 find_zero_bit(struct _nv *x, int value, int sz)
312 for (v = 1, m = 1 << (8 * sz); v < m; v *= 2) {
313 if ((value & v) == 0)
315 if ((s = find_value(x, value & v)) != NULL) {
325 convert_sff_identifier(char *buf, size_t size, uint8_t value)
330 if (value <= SFF_8024_ID_LAST)
331 x = sff_8024_id[value];
334 x = "Vendor specific";
339 snprintf(buf, size, "%s", x);
343 convert_sff_connector(char *buf, size_t size, uint8_t value)
347 if ((x = find_value(conn, value)) == NULL) {
348 if (value >= 0x0D && value <= 0x1F)
350 else if (value >= 0x24 && value <= 0x7F)
353 x = "Vendor specific";
356 snprintf(buf, size, "%s", x);
360 convert_sff_rev_compliance(char *buf, size_t size, uint8_t value)
367 x = find_value(rev_compl, value);
369 snprintf(buf, size, "%s", x);
373 get_sfp_identifier(struct i2c_info *ii, char *buf, size_t size)
377 read_i2c(ii, SFF_8472_BASE, SFF_8472_ID, 1, &data);
378 convert_sff_identifier(buf, size, data);
382 get_sfp_connector(struct i2c_info *ii, char *buf, size_t size)
386 read_i2c(ii, SFF_8472_BASE, SFF_8472_CONNECTOR, 1, &data);
387 convert_sff_connector(buf, size, data);
391 get_qsfp_identifier(struct i2c_info *ii, char *buf, size_t size)
395 read_i2c(ii, SFF_8436_BASE, SFF_8436_ID, 1, &data);
396 convert_sff_identifier(buf, size, data);
400 get_qsfp_connector(struct i2c_info *ii, char *buf, size_t size)
404 read_i2c(ii, SFF_8436_BASE, SFF_8436_CONNECTOR, 1, &data);
405 convert_sff_connector(buf, size, data);
409 printf_sfp_transceiver_descr(struct i2c_info *ii, char *buf, size_t size)
412 const char *tech_class, *tech_len, *tech_tech, *tech_media, *tech_speed;
420 /* Read bytes 3-10 at once */
421 read_i2c(ii, SFF_8472_BASE, SFF_8472_TRANS_START, 8, &xbuf[3]);
423 /* Check 10G ethernet first */
424 tech_class = find_zero_bit(eth_10g, xbuf[3], 1);
425 if (tech_class == NULL) {
426 /* No match. Try 1G */
427 tech_class = find_zero_bit(eth_compat, xbuf[6], 1);
430 tech_len = find_zero_bit(fc_len, xbuf[7], 1);
431 tech_tech = find_zero_bit(cab_tech, xbuf[7] << 8 | xbuf[8], 2);
432 tech_media = find_zero_bit(fc_media, xbuf[9], 1);
433 tech_speed = find_zero_bit(fc_speed, xbuf[10], 1);
435 printf("Class: %s\n", tech_class);
436 printf("Length: %s\n", tech_len);
437 printf("Tech: %s\n", tech_tech);
438 printf("Media: %s\n", tech_media);
439 printf("Speed: %s\n", tech_speed);
443 get_sfp_transceiver_class(struct i2c_info *ii, char *buf, size_t size)
445 const char *tech_class;
448 /* Use extended compliance code if it's valid */
449 read_i2c(ii, SFF_8472_BASE, SFF_8472_TRANS, 1, &code);
451 tech_class = find_value(eth_extended_comp, code);
453 /* Next, check 10G Ethernet/IB CCs */
454 read_i2c(ii, SFF_8472_BASE, SFF_8472_TRANS_START, 1, &code);
455 tech_class = find_zero_bit(eth_10g, code, 1);
456 if (tech_class == NULL) {
457 /* No match. Try Ethernet 1G */
458 read_i2c(ii, SFF_8472_BASE, SFF_8472_TRANS_START + 3,
460 tech_class = find_zero_bit(eth_compat, code, 1);
464 if (tech_class == NULL)
465 tech_class = "Unknown";
467 snprintf(buf, size, "%s", tech_class);
471 get_qsfp_transceiver_class(struct i2c_info *ii, char *buf, size_t size)
473 const char *tech_class;
476 read_i2c(ii, SFF_8436_BASE, SFF_8436_CODE_E1040100G, 1, &code);
478 /* Check for extended specification compliance */
479 if (code & SFF_8636_EXT_COMPLIANCE) {
480 read_i2c(ii, SFF_8436_BASE, SFF_8436_OPTIONS_START, 1, &code);
481 tech_class = find_value(eth_extended_comp, code);
483 /* Check 10/40G Ethernet class only */
484 tech_class = find_zero_bit(eth_1040g, code, 1);
486 if (tech_class == NULL)
487 tech_class = "Unknown";
489 snprintf(buf, size, "%s", tech_class);
493 * Print SFF-8472/SFF-8436 string to supplied buffer.
494 * All (vendor-specific) strings are padded right with '0x20'.
497 convert_sff_name(char *buf, size_t size, char *xbuf)
501 for (p = &xbuf[16]; *(p - 1) == 0x20; p--)
504 snprintf(buf, size, "%s", xbuf);
508 convert_sff_date(char *buf, size_t size, char *xbuf)
511 snprintf(buf, size, "20%c%c-%c%c-%c%c", xbuf[0], xbuf[1],
512 xbuf[2], xbuf[3], xbuf[4], xbuf[5]);
516 get_sfp_vendor_name(struct i2c_info *ii, char *buf, size_t size)
520 memset(xbuf, 0, sizeof(xbuf));
521 read_i2c(ii, SFF_8472_BASE, SFF_8472_VENDOR_START, 16, (uint8_t *)xbuf);
522 convert_sff_name(buf, size, xbuf);
526 get_sfp_vendor_pn(struct i2c_info *ii, char *buf, size_t size)
530 memset(xbuf, 0, sizeof(xbuf));
531 read_i2c(ii, SFF_8472_BASE, SFF_8472_PN_START, 16, (uint8_t *)xbuf);
532 convert_sff_name(buf, size, xbuf);
536 get_sfp_vendor_sn(struct i2c_info *ii, char *buf, size_t size)
540 memset(xbuf, 0, sizeof(xbuf));
541 read_i2c(ii, SFF_8472_BASE, SFF_8472_SN_START, 16, (uint8_t *)xbuf);
542 convert_sff_name(buf, size, xbuf);
546 get_sfp_vendor_date(struct i2c_info *ii, char *buf, size_t size)
550 memset(xbuf, 0, sizeof(xbuf));
551 /* Date code, see Table 3.8 for description */
552 read_i2c(ii, SFF_8472_BASE, SFF_8472_DATE_START, 6, (uint8_t *)xbuf);
553 convert_sff_date(buf, size, xbuf);
557 get_qsfp_vendor_name(struct i2c_info *ii, char *buf, size_t size)
561 memset(xbuf, 0, sizeof(xbuf));
562 read_i2c(ii, SFF_8436_BASE, SFF_8436_VENDOR_START, 16, (uint8_t *)xbuf);
563 convert_sff_name(buf, size, xbuf);
567 get_qsfp_vendor_pn(struct i2c_info *ii, char *buf, size_t size)
571 memset(xbuf, 0, sizeof(xbuf));
572 read_i2c(ii, SFF_8436_BASE, SFF_8436_PN_START, 16, (uint8_t *)xbuf);
573 convert_sff_name(buf, size, xbuf);
577 get_qsfp_vendor_sn(struct i2c_info *ii, char *buf, size_t size)
581 memset(xbuf, 0, sizeof(xbuf));
582 read_i2c(ii, SFF_8436_BASE, SFF_8436_SN_START, 16, (uint8_t *)xbuf);
583 convert_sff_name(buf, size, xbuf);
587 get_qsfp_vendor_date(struct i2c_info *ii, char *buf, size_t size)
591 memset(xbuf, 0, sizeof(xbuf));
592 read_i2c(ii, SFF_8436_BASE, SFF_8436_DATE_START, 6, (uint8_t *)xbuf);
593 convert_sff_date(buf, size, xbuf);
597 print_sfp_vendor(struct i2c_info *ii, char *buf, size_t size)
601 memset(xbuf, 0, sizeof(xbuf));
603 get_qsfp_vendor_name(ii, xbuf, 20);
604 get_qsfp_vendor_pn(ii, &xbuf[20], 20);
605 get_qsfp_vendor_sn(ii, &xbuf[40], 20);
606 get_qsfp_vendor_date(ii, &xbuf[60], 20);
608 get_sfp_vendor_name(ii, xbuf, 20);
609 get_sfp_vendor_pn(ii, &xbuf[20], 20);
610 get_sfp_vendor_sn(ii, &xbuf[40], 20);
611 get_sfp_vendor_date(ii, &xbuf[60], 20);
614 snprintf(buf, size, "vendor: %s PN: %s SN: %s DATE: %s",
615 xbuf, &xbuf[20], &xbuf[40], &xbuf[60]);
619 * Converts internal templerature (SFF-8472, SFF-8436)
620 * 16-bit unsigned value to human-readable representation:
622 * Internally measured Module temperature are represented
623 * as a 16-bit signed twos complement value in increments of
624 * 1/256 degrees Celsius, yielding a total range of –128C to +128C
625 * that is considered valid between –40 and +125C.
629 convert_sff_temp(char *buf, size_t size, uint8_t *xbuf)
634 d += (double)xbuf[1] / 256;
636 snprintf(buf, size, "%.2f C", d);
640 * Retrieves supplied voltage (SFF-8472, SFF-8436).
641 * 16-bit usigned value, treated as range 0..+6.55 Volts
644 convert_sff_voltage(char *buf, size_t size, uint8_t *xbuf)
648 d = (double)((xbuf[0] << 8) | xbuf[1]);
649 snprintf(buf, size, "%.2f Volts", d / 10000);
653 * Converts value in @xbuf to both milliwats and dBm
654 * human representation.
657 convert_sff_power(struct i2c_info *ii, char *buf, size_t size, uint8_t *xbuf)
662 mW = (xbuf[0] << 8) + xbuf[1];
664 /* Convert mw to dbm */
665 dbm = 10.0 * log10(1.0 * mW / 10000);
668 * Assume internally-calibrated data.
669 * This is always true for SFF-8346, and explicitly
670 * checked for SFF-8472.
673 /* Table 3.9, bit 5 is set, internally calibrated */
674 snprintf(buf, size, "%d.%02d mW (%.2f dBm)",
675 mW / 10000, (mW % 10000) / 100, dbm);
679 get_sfp_temp(struct i2c_info *ii, char *buf, size_t size)
683 memset(xbuf, 0, sizeof(xbuf));
684 read_i2c(ii, SFF_8472_DIAG, SFF_8472_TEMP, 2, xbuf);
685 convert_sff_temp(buf, size, xbuf);
689 get_sfp_voltage(struct i2c_info *ii, char *buf, size_t size)
693 memset(xbuf, 0, sizeof(xbuf));
694 read_i2c(ii, SFF_8472_DIAG, SFF_8472_VCC, 2, xbuf);
695 convert_sff_voltage(buf, size, xbuf);
699 get_qsfp_temp(struct i2c_info *ii, char *buf, size_t size)
703 memset(xbuf, 0, sizeof(xbuf));
704 read_i2c(ii, SFF_8436_BASE, SFF_8436_TEMP, 2, xbuf);
705 if ((xbuf[0] == 0xFF && xbuf[1] == 0xFF) || (xbuf[0] == 0 && xbuf[1] == 0))
707 convert_sff_temp(buf, size, xbuf);
712 get_qsfp_voltage(struct i2c_info *ii, char *buf, size_t size)
716 memset(xbuf, 0, sizeof(xbuf));
717 read_i2c(ii, SFF_8436_BASE, SFF_8436_VCC, 2, xbuf);
718 convert_sff_voltage(buf, size, xbuf);
722 get_sfp_rx_power(struct i2c_info *ii, char *buf, size_t size)
726 memset(xbuf, 0, sizeof(xbuf));
727 read_i2c(ii, SFF_8472_DIAG, SFF_8472_RX_POWER, 2, xbuf);
728 convert_sff_power(ii, buf, size, xbuf);
732 get_sfp_tx_power(struct i2c_info *ii, char *buf, size_t size)
736 memset(xbuf, 0, sizeof(xbuf));
737 read_i2c(ii, SFF_8472_DIAG, SFF_8472_TX_POWER, 2, xbuf);
738 convert_sff_power(ii, buf, size, xbuf);
742 get_qsfp_rx_power(struct i2c_info *ii, char *buf, size_t size, int chan)
746 memset(xbuf, 0, sizeof(xbuf));
747 read_i2c(ii, SFF_8436_BASE, SFF_8436_RX_CH1_MSB + (chan-1)*2, 2, xbuf);
748 convert_sff_power(ii, buf, size, xbuf);
752 get_qsfp_tx_power(struct i2c_info *ii, char *buf, size_t size, int chan)
756 memset(xbuf, 0, sizeof(xbuf));
757 read_i2c(ii, SFF_8436_BASE, SFF_8436_TX_CH1_MSB + (chan-1)*2, 2, xbuf);
758 convert_sff_power(ii, buf, size, xbuf);
762 get_qsfp_rev_compliance(struct i2c_info *ii, char *buf, size_t size)
767 read_i2c(ii, SFF_8436_BASE, SFF_8436_STATUS, 1, &xbuf);
768 convert_sff_rev_compliance(buf, size, xbuf);
772 get_qsfp_br(struct i2c_info *ii)
778 read_i2c(ii, SFF_8436_BASE, SFF_8436_BITRATE, 1, &xbuf);
781 read_i2c(ii, SFF_8436_BASE, SFF_8636_BITRATE, 1, &xbuf);
789 * Reads i2c data from opened kernel socket.
792 read_i2c(struct i2c_info *ii, uint8_t addr, uint8_t off, uint8_t len,
801 ii->ifr->ifr_data = (caddr_t)&req;
805 memset(&req, 0, sizeof(req));
811 l = MIN(sizeof(req.data), len);
813 if (ioctl(ii->fd, SIOCGI2C, ii->ifr) != 0) {
818 memcpy(&buf[i], req.data, l);
828 dump_i2c_data(struct i2c_info *ii, uint8_t addr, uint8_t off, uint8_t len)
830 unsigned char buf[16];
834 memset(buf, 0, sizeof(buf));
835 read = MIN(sizeof(buf), len);
836 read_i2c(ii, addr, off, read, buf);
837 if (ii->error != 0) {
838 fprintf(stderr, "Error reading i2c info\n");
843 for (i = 0; i < read; i++)
844 printf("%02X ", buf[i]);
852 print_qsfp_status(struct i2c_info *ii, int verbose)
854 char buf[80], buf2[40], buf3[40];
860 /* Transceiver type */
861 get_qsfp_identifier(ii, buf, sizeof(buf));
862 get_qsfp_transceiver_class(ii, buf2, sizeof(buf2));
863 get_qsfp_connector(ii, buf3, sizeof(buf3));
865 printf("\tplugged: %s %s (%s)\n", buf, buf2, buf3);
866 print_sfp_vendor(ii, buf, sizeof(buf));
868 printf("\t%s\n", buf);
871 get_qsfp_rev_compliance(ii, buf, sizeof(buf));
873 printf("\tcompliance level: %s\n", buf);
875 bitrate = get_qsfp_br(ii);
876 if (ii->error == 0 && bitrate > 0)
877 printf("\tnominal bitrate: %u Mbps\n", bitrate);
881 * The standards in this area are not clear when the
882 * additional measurements are present or not. Use a valid
883 * temperature reading as an indicator for the presence of
884 * voltage and TX/RX power measurements.
886 if (get_qsfp_temp(ii, buf, sizeof(buf)) == 0) {
887 get_qsfp_voltage(ii, buf2, sizeof(buf2));
888 printf("\tmodule temperature: %s voltage: %s\n", buf, buf2);
889 for (i = 1; i <= 4; i++) {
890 get_qsfp_rx_power(ii, buf, sizeof(buf), i);
891 get_qsfp_tx_power(ii, buf2, sizeof(buf2), i);
892 printf("\tlane %d: RX: %s TX: %s\n", i, buf, buf2);
897 printf("\n\tSFF8436 DUMP (0xA0 128..255 range):\n");
898 dump_i2c_data(ii, SFF_8436_BASE, 128, 128);
899 printf("\n\tSFF8436 DUMP (0xA0 0..81 range):\n");
900 dump_i2c_data(ii, SFF_8436_BASE, 0, 82);
905 print_sfp_status(struct i2c_info *ii, int verbose)
907 char buf[80], buf2[40], buf3[40];
908 uint8_t diag_type, flags;
910 /* Read diagnostic monitoring type */
911 read_i2c(ii, SFF_8472_BASE, SFF_8472_DIAG_TYPE, 1, (caddr_t)&diag_type);
916 * Read monitoring data IFF it is supplied AND is
917 * internally calibrated
919 flags = SFF_8472_DDM_DONE | SFF_8472_DDM_INTERNAL;
920 if ((diag_type & flags) == flags)
923 /* Transceiver type */
924 get_sfp_identifier(ii, buf, sizeof(buf));
925 get_sfp_transceiver_class(ii, buf2, sizeof(buf2));
926 get_sfp_connector(ii, buf3, sizeof(buf3));
928 printf("\tplugged: %s %s (%s)\n", buf, buf2, buf3);
929 print_sfp_vendor(ii, buf, sizeof(buf));
931 printf("\t%s\n", buf);
934 printf_sfp_transceiver_descr(ii, buf, sizeof(buf));
936 * Request current measurements iff they are provided:
938 if (ii->do_diag != 0) {
939 get_sfp_temp(ii, buf, sizeof(buf));
940 get_sfp_voltage(ii, buf2, sizeof(buf2));
941 printf("\tmodule temperature: %s Voltage: %s\n", buf, buf2);
942 get_sfp_rx_power(ii, buf, sizeof(buf));
943 get_sfp_tx_power(ii, buf2, sizeof(buf2));
944 printf("\tRX: %s TX: %s\n", buf, buf2);
948 printf("\n\tSFF8472 DUMP (0xA0 0..127 range):\n");
949 dump_i2c_data(ii, SFF_8472_BASE, 0, 128);
954 sfp_status(int s, struct ifreq *ifr, int verbose)
959 /* Prepare necessary into pass to i2c reader */
960 memset(&ii, 0, sizeof(ii));
965 * Try to read byte 0 from i2c:
966 * Both SFF-8472 and SFF-8436 use it as
967 * 'identification byte'.
968 * Stop reading status on zero as value -
969 * this might happen in case of empty transceiver slot.
972 read_i2c(&ii, SFF_8472_BASE, SFF_8472_ID, 1, (caddr_t)&id_byte);
973 if (ii.error != 0 || id_byte == 0)
977 case SFF_8024_ID_QSFP:
978 case SFF_8024_ID_QSFPPLUS:
979 case SFF_8024_ID_QSFP28:
980 print_qsfp_status(&ii, verbose);
983 print_sfp_status(&ii, verbose);