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 void 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 convert_sff_temp(buf, size, xbuf); 636 } 637 638 static void 639 get_qsfp_voltage(struct i2c_info *ii, char *buf, size_t size) 640 { 641 uint8_t xbuf[2]; 642 643 memset(xbuf, 0, sizeof(xbuf)); 644 read_i2c(ii, SFF_8436_BASE, SFF_8436_VCC, 2, xbuf); 645 convert_sff_voltage(buf, size, xbuf); 646 } 647 648 static void 649 get_sfp_rx_power(struct i2c_info *ii, char *buf, size_t size) 650 { 651 uint8_t xbuf[2]; 652 653 memset(xbuf, 0, sizeof(xbuf)); 654 read_i2c(ii, SFF_8472_DIAG, SFF_8472_RX_POWER, 2, xbuf); 655 convert_sff_power(ii, buf, size, xbuf); 656 } 657 658 static void 659 get_sfp_tx_power(struct i2c_info *ii, char *buf, size_t size) 660 { 661 uint8_t xbuf[2]; 662 663 memset(xbuf, 0, sizeof(xbuf)); 664 read_i2c(ii, SFF_8472_DIAG, SFF_8472_TX_POWER, 2, xbuf); 665 convert_sff_power(ii, buf, size, xbuf); 666 } 667 668 static void 669 get_qsfp_rx_power(struct i2c_info *ii, char *buf, size_t size, int chan) 670 { 671 uint8_t xbuf[2]; 672 673 memset(xbuf, 0, sizeof(xbuf)); 674 read_i2c(ii, SFF_8436_BASE, SFF_8436_RX_CH1_MSB + (chan-1)*2, 2, xbuf); 675 convert_sff_power(ii, buf, size, xbuf); 676 } 677 678 static void 679 get_qsfp_tx_power(struct i2c_info *ii, char *buf, size_t size, int chan) 680 { 681 uint8_t xbuf[2]; 682 683 memset(xbuf, 0, sizeof(xbuf)); 684 read_i2c(ii, SFF_8436_BASE, SFF_8436_TX_CH1_MSB + (chan-1)*2, 2, xbuf); 685 convert_sff_power(ii, buf, size, xbuf); 686 } 687 688 static void 689 get_qsfp_rev_compliance(struct i2c_info *ii, char *buf, size_t size) 690 { 691 uint8_t xbuf; 692 693 xbuf = 0; 694 read_i2c(ii, SFF_8436_BASE, SFF_8436_STATUS, 1, &xbuf); 695 convert_sff_rev_compliance(buf, size, xbuf); 696 } 697 698 static uint32_t 699 get_qsfp_br(struct i2c_info *ii) 700 { 701 uint8_t xbuf; 702 uint32_t rate; 703 704 xbuf = 0; 705 read_i2c(ii, SFF_8436_BASE, SFF_8436_BITRATE, 1, &xbuf); 706 rate = xbuf * 100; 707 if (xbuf == 0xFF) { 708 read_i2c(ii, SFF_8436_BASE, SFF_8636_BITRATE, 1, &xbuf); 709 rate = xbuf * 250; 710 } 711 712 return (rate); 713 } 714 715 /* 716 * Reads i2c data from opened kernel socket. 717 */ 718 static int 719 read_i2c(struct i2c_info *ii, uint8_t addr, uint8_t off, uint8_t len, 720 uint8_t *buf) 721 { 722 struct ifi2creq req; 723 int i, l; 724 725 if (ii->error != 0) 726 return (ii->error); 727 728 ii->ifr->ifr_data = (caddr_t)&req; 729 730 i = 0; 731 l = 0; 732 memset(&req, 0, sizeof(req)); 733 req.dev_addr = addr; 734 req.offset = off; 735 req.len = len; 736 737 while (len > 0) { 738 l = (len > sizeof(req.data)) ? sizeof(req.data) : len; 739 req.len = l; 740 if (ioctl(ii->fd, SIOCGI2C, ii->ifr) != 0) { 741 ii->error = errno; 742 return (errno); 743 } 744 745 memcpy(&buf[i], req.data, l); 746 len -= l; 747 i += l; 748 req.offset += l; 749 } 750 751 return (0); 752 } 753 754 static void 755 dump_i2c_data(struct i2c_info *ii, uint8_t addr, uint8_t off, uint8_t len) 756 { 757 unsigned char buf[16]; 758 int i, read; 759 760 while (len > 0) { 761 memset(buf, 0, sizeof(buf)); 762 read = (len > sizeof(buf)) ? sizeof(buf) : len; 763 read_i2c(ii, addr, off, read, buf); 764 if (ii->error != 0) { 765 fprintf(stderr, "Error reading i2c info\n"); 766 return; 767 } 768 769 printf("\t"); 770 for (i = 0; i < read; i++) 771 printf("%02X ", buf[i]); 772 printf("\n"); 773 len -= read; 774 off += read; 775 } 776 } 777 778 static void 779 print_qsfp_status(struct i2c_info *ii, int verbose) 780 { 781 char buf[80], buf2[40], buf3[40]; 782 uint8_t diag_type; 783 uint32_t bitrate; 784 int i; 785 786 /* Read diagnostic monitoring type */ 787 read_i2c(ii, SFF_8436_BASE, SFF_8436_DIAG_TYPE, 1, (caddr_t)&diag_type); 788 if (ii->error != 0) 789 return; 790 791 /* 792 * Read monitoring data it is supplied. 793 * XXX: It is not exactly clear from standard 794 * how one can specify lack of measurements (passive cables case). 795 */ 796 if (diag_type != 0) 797 ii->do_diag = 1; 798 ii->qsfp = 1; 799 800 /* Transceiver type */ 801 get_qsfp_identifier(ii, buf, sizeof(buf)); 802 get_qsfp_transceiver_class(ii, buf2, sizeof(buf2)); 803 get_qsfp_connector(ii, buf3, sizeof(buf3)); 804 if (ii->error == 0) 805 printf("\tplugged: %s %s (%s)\n", buf, buf2, buf3); 806 print_sfp_vendor(ii, buf, sizeof(buf)); 807 if (ii->error == 0) 808 printf("\t%s\n", buf); 809 810 if (verbose > 1) { 811 get_qsfp_rev_compliance(ii, buf, sizeof(buf)); 812 if (ii->error == 0) 813 printf("\tcompliance level: %s\n", buf); 814 815 bitrate = get_qsfp_br(ii); 816 if (ii->error == 0 && bitrate > 0) 817 printf("\tnominal bitrate: %u Mbps\n", bitrate); 818 } 819 820 /* Request current measurements if they are provided: */ 821 if (ii->do_diag != 0) { 822 get_qsfp_temp(ii, buf, sizeof(buf)); 823 get_qsfp_voltage(ii, buf2, sizeof(buf2)); 824 printf("\tmodule temperature: %s voltage: %s\n", buf, buf2); 825 for (i = 1; i <= 4; i++) { 826 get_qsfp_rx_power(ii, buf, sizeof(buf), i); 827 get_qsfp_tx_power(ii, buf2, sizeof(buf2), i); 828 printf("\tlane %d: RX: %s TX: %s\n", i, buf, buf2); 829 } 830 } 831 832 if (verbose > 2) { 833 printf("\n\tSFF8436 DUMP (0xA0 128..255 range):\n"); 834 dump_i2c_data(ii, SFF_8436_BASE, 128, 128); 835 printf("\n\tSFF8436 DUMP (0xA0 0..81 range):\n"); 836 dump_i2c_data(ii, SFF_8436_BASE, 0, 82); 837 } 838 } 839 840 static void 841 print_sfp_status(struct i2c_info *ii, int verbose) 842 { 843 char buf[80], buf2[40], buf3[40]; 844 uint8_t diag_type, flags; 845 846 /* Read diagnostic monitoring type */ 847 read_i2c(ii, SFF_8472_BASE, SFF_8472_DIAG_TYPE, 1, (caddr_t)&diag_type); 848 if (ii->error != 0) 849 return; 850 851 /* 852 * Read monitoring data IFF it is supplied AND is 853 * internally calibrated 854 */ 855 flags = SFF_8472_DDM_DONE | SFF_8472_DDM_INTERNAL; 856 if ((diag_type & flags) == flags) 857 ii->do_diag = 1; 858 859 /* Transceiver type */ 860 get_sfp_identifier(ii, buf, sizeof(buf)); 861 get_sfp_transceiver_class(ii, buf2, sizeof(buf2)); 862 get_sfp_connector(ii, buf3, sizeof(buf3)); 863 if (ii->error == 0) 864 printf("\tplugged: %s %s (%s)\n", buf, buf2, buf3); 865 print_sfp_vendor(ii, buf, sizeof(buf)); 866 if (ii->error == 0) 867 printf("\t%s\n", buf); 868 869 if (verbose > 5) 870 printf_sfp_transceiver_descr(ii, buf, sizeof(buf)); 871 /* 872 * Request current measurements iff they are provided: 873 */ 874 if (ii->do_diag != 0) { 875 get_sfp_temp(ii, buf, sizeof(buf)); 876 get_sfp_voltage(ii, buf2, sizeof(buf2)); 877 printf("\tmodule temperature: %s Voltage: %s\n", buf, buf2); 878 get_sfp_rx_power(ii, buf, sizeof(buf)); 879 get_sfp_tx_power(ii, buf2, sizeof(buf2)); 880 printf("\tRX: %s TX: %s\n", buf, buf2); 881 } 882 883 if (verbose > 2) { 884 printf("\n\tSFF8472 DUMP (0xA0 0..127 range):\n"); 885 dump_i2c_data(ii, SFF_8472_BASE, 0, 128); 886 } 887 } 888 889 void 890 sfp_status(int s, struct ifreq *ifr, int verbose) 891 { 892 struct i2c_info ii; 893 uint8_t id_byte; 894 895 /* Prepare necessary into pass to i2c reader */ 896 memset(&ii, 0, sizeof(ii)); 897 ii.fd = s; 898 ii.ifr = ifr; 899 900 /* 901 * Try to read byte 0 from i2c: 902 * Both SFF-8472 and SFF-8436 use it as 903 * 'identification byte'. 904 * Stop reading status on zero as value - 905 * this might happen in case of empty transceiver slot. 906 */ 907 id_byte = 0; 908 read_i2c(&ii, SFF_8472_BASE, SFF_8472_ID, 1, (caddr_t)&id_byte); 909 if (ii.error != 0 || id_byte == 0) 910 return; 911 912 switch (id_byte) { 913 case SFF_8024_ID_QSFP: 914 case SFF_8024_ID_QSFPPLUS: 915 case SFF_8024_ID_QSFP28: 916 print_qsfp_status(&ii, verbose); 917 break; 918 default: 919 print_sfp_status(&ii, verbose); 920 }; 921 } 922 923