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 typedef int (read_i2c)(struct i2c_info *ii, uint8_t addr, uint8_t off, 53 uint8_t len, caddr_t buf); 54 55 struct i2c_info { 56 int s; 57 int error; 58 int bshift; 59 int qsfp; 60 int do_diag; 61 struct ifreq *ifr; 62 read_i2c *f; 63 char *textbuf; 64 size_t bufsize; 65 int cfd; 66 int port_id; 67 int chip_id; 68 }; 69 70 static void dump_i2c_data(struct i2c_info *ii, uint8_t addr, uint8_t off, 71 uint8_t len); 72 73 struct _nv { 74 int v; 75 const char *n; 76 }; 77 78 const char *find_value(struct _nv *x, int value); 79 const char *find_zero_bit(struct _nv *x, int value, int sz); 80 81 /* SFF-8472 Rev. 11.4 table 3.4: Connector values */ 82 static struct _nv conn[] = { 83 { 0x00, "Unknown" }, 84 { 0x01, "SC" }, 85 { 0x02, "Fibre Channel Style 1 copper" }, 86 { 0x03, "Fibre Channel Style 2 copper" }, 87 { 0x04, "BNC/TNC" }, 88 { 0x05, "Fibre Channel coaxial" }, 89 { 0x06, "FiberJack" }, 90 { 0x07, "LC" }, 91 { 0x08, "MT-RJ" }, 92 { 0x09, "MU" }, 93 { 0x0A, "SG" }, 94 { 0x0B, "Optical pigtail" }, 95 { 0x0C, "MPO Parallel Optic" }, 96 { 0x20, "HSSDC II" }, 97 { 0x21, "Copper pigtail" }, 98 { 0x22, "RJ45" }, 99 { 0x23, "No separate connector" }, /* SFF-8436 */ 100 { 0, NULL } 101 }; 102 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" }, 110 { 0x08, "1X SX" }, 111 { 0x04, "1X LX" }, 112 { 0x02, "1X Copper Active" }, 113 { 0x01, "1X Copper Passive" }, 114 { 0, NULL } 115 }; 116 117 /* Ethernet compliance codes, byte 6 */ 118 static struct _nv eth_compat[] = { 119 { 0x80, "BASE-PX" }, 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" }, 127 { 0, NULL } 128 }; 129 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" }, 137 { 0, NULL } 138 }; 139 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" }, 151 { 0, NULL } 152 }; 153 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" }, 162 { 0x02, "" }, 163 { 0x01, "Single Mode" }, 164 { 0, NULL } 165 }; 166 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" }, 176 { 0, NULL } 177 }; 178 179 /* SFF-8436 Rev. 4.8 table 33: Specification compliance */ 180 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" }, 191 { 0, NULL } 192 }; 193 194 const char * 195 find_value(struct _nv *x, int value) 196 { 197 for (; x->n != NULL; x++) 198 if (x->v == value) 199 return (x->n); 200 return (NULL); 201 } 202 203 const char * 204 find_zero_bit(struct _nv *x, int value, int sz) 205 { 206 int v, m; 207 const char *s; 208 209 v = 1; 210 for (v = 1, m = 1 << (8 * sz); v < m; v *= 2) { 211 if ((value & v) == 0) 212 continue; 213 if ((s = find_value(x, value & v)) != NULL) { 214 value &= ~v; 215 return (s); 216 } 217 } 218 219 return (NULL); 220 } 221 222 static void 223 convert_sff_identifier(char *buf, size_t size, uint8_t value) 224 { 225 const char *x; 226 227 x = NULL; 228 if (value <= SFF_8024_ID_LAST) 229 x = sff_8024_id[value]; 230 else { 231 if (value > 0x80) 232 x = "Vendor specific"; 233 else 234 x = "Reserved"; 235 } 236 237 snprintf(buf, size, "%s", x); 238 } 239 240 static void 241 convert_sff_connector(char *buf, size_t size, uint8_t value) 242 { 243 const char *x; 244 245 if ((x = find_value(conn, value)) == NULL) { 246 if (value >= 0x0D && value <= 0x1F) 247 x = "Unallocated"; 248 else if (value >= 0x24 && value <= 0x7F) 249 x = "Unallocated"; 250 else 251 x = "Vendor specific"; 252 } 253 254 snprintf(buf, size, "%s", x); 255 } 256 257 static void 258 get_sfp_identifier(struct i2c_info *ii, char *buf, size_t size) 259 { 260 uint8_t data; 261 262 ii->f(ii, SFF_8472_BASE, SFF_8472_ID, 1, (caddr_t)&data); 263 convert_sff_identifier(buf, size, data); 264 } 265 266 static void 267 get_sfp_connector(struct i2c_info *ii, char *buf, size_t size) 268 { 269 uint8_t data; 270 271 ii->f(ii, SFF_8472_BASE, SFF_8472_CONNECTOR, 1, (caddr_t)&data); 272 convert_sff_connector(buf, size, data); 273 } 274 275 static void 276 get_qsfp_identifier(struct i2c_info *ii, char *buf, size_t size) 277 { 278 uint8_t data; 279 280 ii->f(ii, SFF_8436_BASE, SFF_8436_ID, 1, (caddr_t)&data); 281 convert_sff_identifier(buf, size, data); 282 } 283 284 static void 285 get_qsfp_connector(struct i2c_info *ii, char *buf, size_t size) 286 { 287 uint8_t data; 288 289 ii->f(ii, SFF_8436_BASE, SFF_8436_CONNECTOR, 1, (caddr_t)&data); 290 convert_sff_connector(buf, size, data); 291 } 292 293 static void 294 printf_sfp_transceiver_descr(struct i2c_info *ii, char *buf, size_t size) 295 { 296 char xbuf[12]; 297 const char *tech_class, *tech_len, *tech_tech, *tech_media, *tech_speed; 298 299 tech_class = NULL; 300 tech_len = NULL; 301 tech_tech = NULL; 302 tech_media = NULL; 303 tech_speed = NULL; 304 305 /* Read bytes 3-10 at once */ 306 ii->f(ii, SFF_8472_BASE, SFF_8472_TRANS_START, 8, &xbuf[3]); 307 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); 313 } 314 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); 319 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); 325 } 326 327 static void 328 get_sfp_transceiver_class(struct i2c_info *ii, char *buf, size_t size) 329 { 330 const char *tech_class; 331 uint8_t code; 332 333 unsigned char qbuf[8]; 334 ii->f(ii, SFF_8472_BASE, SFF_8472_TRANS_START, 8, (caddr_t)qbuf); 335 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, 342 1, (caddr_t)&code); 343 tech_class = find_zero_bit(eth_compat, code, 1); 344 } 345 346 if (tech_class == NULL) 347 tech_class = "Unknown"; 348 349 snprintf(buf, size, "%s", tech_class); 350 } 351 352 static void 353 get_qsfp_transceiver_class(struct i2c_info *ii, char *buf, size_t size) 354 { 355 const char *tech_class; 356 uint8_t code; 357 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"; 363 364 snprintf(buf, size, "%s", tech_class); 365 } 366 367 /* 368 * Print SFF-8472/SFF-8436 string to supplied buffer. 369 * All (vendor-specific) strings are padded right with '0x20'. 370 */ 371 static void 372 convert_sff_name(char *buf, size_t size, char *xbuf) 373 { 374 char *p; 375 376 for (p = &xbuf[16]; *(p - 1) == 0x20; p--) 377 ; 378 *p = '\0'; 379 snprintf(buf, size, "%s", xbuf); 380 } 381 382 static void 383 convert_sff_date(char *buf, size_t size, char *xbuf) 384 { 385 386 snprintf(buf, size, "20%c%c-%c%c-%c%c", xbuf[0], xbuf[1], 387 xbuf[2], xbuf[3], xbuf[4], xbuf[5]); 388 } 389 390 static void 391 get_sfp_vendor_name(struct i2c_info *ii, char *buf, size_t size) 392 { 393 char xbuf[17]; 394 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); 398 } 399 400 static void 401 get_sfp_vendor_pn(struct i2c_info *ii, char *buf, size_t size) 402 { 403 char xbuf[17]; 404 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); 408 } 409 410 static void 411 get_sfp_vendor_sn(struct i2c_info *ii, char *buf, size_t size) 412 { 413 char xbuf[17]; 414 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); 418 } 419 420 static void 421 get_sfp_vendor_date(struct i2c_info *ii, char *buf, size_t size) 422 { 423 char xbuf[6]; 424 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); 429 } 430 431 static void 432 get_qsfp_vendor_name(struct i2c_info *ii, char *buf, size_t size) 433 { 434 char xbuf[17]; 435 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); 439 } 440 441 static void 442 get_qsfp_vendor_pn(struct i2c_info *ii, char *buf, size_t size) 443 { 444 char xbuf[17]; 445 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); 449 } 450 451 static void 452 get_qsfp_vendor_sn(struct i2c_info *ii, char *buf, size_t size) 453 { 454 char xbuf[17]; 455 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); 459 } 460 461 static void 462 get_qsfp_vendor_date(struct i2c_info *ii, char *buf, size_t size) 463 { 464 char xbuf[6]; 465 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); 469 } 470 471 static void 472 print_sfp_vendor(struct i2c_info *ii, char *buf, size_t size) 473 { 474 char xbuf[80]; 475 476 memset(xbuf, 0, sizeof(xbuf)); 477 if (ii->qsfp != 0) { 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); 482 } else { 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); 487 } 488 489 snprintf(buf, size, "vendor: %s PN: %s SN: %s DATE: %s", 490 xbuf, &xbuf[20], &xbuf[40], &xbuf[60]); 491 } 492 493 /* 494 * Converts internal templerature (SFF-8472, SFF-8436) 495 * 16-bit unsigned value to human-readable representation: 496 * 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. 501 * 502 */ 503 static void 504 convert_sff_temp(char *buf, size_t size, char *xbuf) 505 { 506 double d; 507 508 d = (double)(int8_t)xbuf[0]; 509 d += (double)(uint8_t)xbuf[1] / 256; 510 511 snprintf(buf, size, "%.2f C", d); 512 } 513 514 /* 515 * Retrieves supplied voltage (SFF-8472, SFF-8436). 516 * 16-bit usigned value, treated as range 0..+6.55 Volts 517 */ 518 static void 519 convert_sff_voltage(char *buf, size_t size, char *xbuf) 520 { 521 double d; 522 523 d = (double)(((uint8_t)xbuf[0] << 8) | (uint8_t)xbuf[1]); 524 snprintf(buf, size, "%.2f Volts", d / 10000); 525 } 526 527 /* 528 * Converts value in @xbuf to both milliwats and dBm 529 * human representation. 530 */ 531 static void 532 convert_sff_power(struct i2c_info *ii, char *buf, size_t size, char *xbuf) 533 { 534 uint16_t mW; 535 double dbm; 536 537 mW = ((uint8_t)xbuf[0] << 8) + (uint8_t)xbuf[1]; 538 539 /* Convert mw to dbm */ 540 dbm = 10.0 * log10(1.0 * mW / 10000); 541 542 /* 543 * Assume internally-calibrated data. 544 * This is always true for SFF-8346, and explicitly 545 * checked for SFF-8472. 546 */ 547 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); 551 } 552 553 static void 554 get_sfp_temp(struct i2c_info *ii, char *buf, size_t size) 555 { 556 char xbuf[2]; 557 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); 561 } 562 563 static void 564 get_sfp_voltage(struct i2c_info *ii, char *buf, size_t size) 565 { 566 char xbuf[2]; 567 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); 571 } 572 573 static void 574 get_qsfp_temp(struct i2c_info *ii, char *buf, size_t size) 575 { 576 char xbuf[2]; 577 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); 581 } 582 583 static void 584 get_qsfp_voltage(struct i2c_info *ii, char *buf, size_t size) 585 { 586 char xbuf[2]; 587 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); 591 } 592 593 static void 594 get_sfp_rx_power(struct i2c_info *ii, char *buf, size_t size) 595 { 596 char xbuf[2]; 597 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); 601 } 602 603 static void 604 get_sfp_tx_power(struct i2c_info *ii, char *buf, size_t size) 605 { 606 char xbuf[2]; 607 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); 611 } 612 613 static void 614 get_qsfp_rx_power(struct i2c_info *ii, char *buf, size_t size, int chan) 615 { 616 char xbuf[2]; 617 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); 621 } 622 623 static void 624 get_qsfp_tx_power(struct i2c_info *ii, char *buf, size_t size, int chan) 625 { 626 char xbuf[2]; 627 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); 631 } 632 633 /* Generic handler */ 634 static int 635 read_i2c_generic(struct i2c_info *ii, uint8_t addr, uint8_t off, uint8_t len, 636 caddr_t buf) 637 { 638 struct ifi2creq req; 639 int i, l; 640 641 if (ii->error != 0) 642 return (ii->error); 643 644 ii->ifr->ifr_data = (caddr_t)&req; 645 646 i = 0; 647 l = 0; 648 memset(&req, 0, sizeof(req)); 649 req.dev_addr = addr; 650 req.offset = off; 651 req.len = len; 652 653 while (len > 0) { 654 l = (len > sizeof(req.data)) ? sizeof(req.data) : len; 655 req.len = l; 656 if (ioctl(ii->s, SIOCGI2C, ii->ifr) != 0) { 657 ii->error = errno; 658 return (errno); 659 } 660 661 memcpy(&buf[i], req.data, l); 662 len -= l; 663 i += l; 664 req.offset += l; 665 } 666 667 return (0); 668 } 669 670 static void 671 dump_i2c_data(struct i2c_info *ii, uint8_t addr, uint8_t off, uint8_t len) 672 { 673 unsigned char buf[16]; 674 int i, read; 675 676 while (len > 0) { 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"); 682 return; 683 } 684 685 printf("\t"); 686 for (i = 0; i < read; i++) 687 printf("%02X ", buf[i]); 688 printf("\n"); 689 len -= read; 690 off += read; 691 } 692 } 693 694 static void 695 print_qsfp_status(struct i2c_info *ii, int verbose) 696 { 697 char buf[80], buf2[40], buf3[40]; 698 uint8_t diag_type; 699 int i; 700 701 /* Read diagnostic monitoring type */ 702 ii->f(ii, SFF_8436_BASE, SFF_8436_DIAG_TYPE, 1, (caddr_t)&diag_type); 703 if (ii->error != 0) 704 return; 705 706 /* 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). 710 */ 711 if (diag_type != 0) 712 ii->do_diag = 1; 713 ii->qsfp = 1; 714 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)); 719 if (ii->error == 0) 720 printf("\tplugged: %s %s (%s)\n", buf, buf2, buf3); 721 print_sfp_vendor(ii, buf, sizeof(buf)); 722 if (ii->error == 0) 723 printf("\t%s\n", buf); 724 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); 734 } 735 } 736 737 if (verbose > 2) { 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); 742 } 743 } 744 745 static void 746 print_sfp_status(struct i2c_info *ii, int verbose) 747 { 748 char buf[80], buf2[40], buf3[40]; 749 uint8_t diag_type, flags; 750 751 /* Read diagnostic monitoring type */ 752 ii->f(ii, SFF_8472_BASE, SFF_8472_DIAG_TYPE, 1, (caddr_t)&diag_type); 753 if (ii->error != 0) 754 return; 755 756 /* 757 * Read monitoring data IFF it is supplied AND is 758 * internally calibrated 759 */ 760 flags = SFF_8472_DDM_DONE | SFF_8472_DDM_INTERNAL; 761 if ((diag_type & flags) == flags) 762 ii->do_diag = 1; 763 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)); 768 if (ii->error == 0) 769 printf("\tplugged: %s %s (%s)\n", buf, buf2, buf3); 770 print_sfp_vendor(ii, buf, sizeof(buf)); 771 if (ii->error == 0) 772 printf("\t%s\n", buf); 773 774 if (verbose > 5) 775 printf_sfp_transceiver_descr(ii, buf, sizeof(buf)); 776 /* 777 * Request current measurements iff they are provided: 778 */ 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); 786 } 787 788 if (verbose > 2) { 789 printf("\n\tSFF8472 DUMP (0xA0 0..127 range):\n"); 790 dump_i2c_data(ii, SFF_8472_BASE, 0, 128); 791 } 792 } 793 794 void 795 sfp_status(int s, struct ifreq *ifr, int verbose) 796 { 797 struct i2c_info ii; 798 uint8_t id_byte; 799 800 memset(&ii, 0, sizeof(ii)); 801 /* Prepare necessary into to pass to NIC handler */ 802 ii.s = s; 803 ii.ifr = ifr; 804 ii.f = read_i2c_generic; 805 806 /* 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. 812 */ 813 id_byte = 0; 814 ii.f(&ii, SFF_8472_BASE, SFF_8472_ID, 1, (caddr_t)&id_byte); 815 if (ii.error != 0 || id_byte == 0) 816 return; 817 818 switch (id_byte) { 819 case SFF_8024_ID_QSFP: 820 case SFF_8024_ID_QSFPPLUS: 821 print_qsfp_status(&ii, verbose); 822 break; 823 default: 824 print_sfp_status(&ii, verbose); 825 }; 826 } 827 828