1 /*- 2 * Copyright (c) 2014, Alexander V. Chernikov 3 * Copyright (c) 2020, Ryan Moeller <freqlabs@FreeBSD.org> 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 */ 26 27 #define _WANT_SFF_8024_ID 28 29 #include <sys/types.h> 30 #include <sys/param.h> 31 #include <sys/ioctl.h> 32 #include <sys/socket.h> 33 34 #include <net/if.h> 35 #include <net/sff8436.h> 36 #include <net/sff8472.h> 37 38 #include <math.h> 39 #include <err.h> 40 #include <errno.h> 41 #include <fcntl.h> 42 #include <stdbool.h> 43 #include <stdio.h> 44 #include <stdlib.h> 45 #include <string.h> 46 #include <unistd.h> 47 48 #include <libifconfig.h> 49 #include <libifconfig_internal.h> 50 #include <libifconfig_sfp.h> 51 #include <libifconfig_sfp_tables_internal.h> 52 53 #define SFF_8636_EXT_COMPLIANCE 0x80 54 55 struct i2c_info { 56 struct ifreq ifr; 57 ifconfig_handle_t *h; 58 int error; /* Store first error */ 59 enum sfp_id id; /* Module type */ 60 }; 61 62 static uint8_t 63 find_zero_bit(const struct sfp_enum_metadata *table, int value, int sz) 64 { 65 int v, m; 66 67 for (v = 1, m = 1 << (8 * sz); v < m; v <<= 1) { 68 if ((value & v) == 0) 69 continue; 70 if (find_metadata(table, value & v) != NULL) { 71 return (value & v); 72 } 73 } 74 return (0); 75 } 76 77 /* 78 * Reads i2c data from opened kernel socket. 79 */ 80 static int 81 read_i2c(struct i2c_info *ii, uint8_t addr, uint8_t off, uint8_t len, 82 uint8_t *buf) 83 { 84 struct ifi2creq req; 85 int i, l; 86 87 if (ii->error != 0) 88 return (ii->error); 89 90 ii->ifr.ifr_data = (caddr_t)&req; 91 92 i = 0; 93 l = 0; 94 memset(&req, 0, sizeof(req)); 95 req.dev_addr = addr; 96 req.offset = off; 97 req.len = len; 98 99 while (len > 0) { 100 l = MIN(sizeof(req.data), len); 101 req.len = l; 102 if (ifconfig_ioctlwrap(ii->h, AF_LOCAL, SIOCGI2C, 103 &ii->ifr) != 0) { 104 ii->error = errno; 105 return (errno); 106 } 107 108 memcpy(&buf[i], req.data, l); 109 len -= l; 110 i += l; 111 req.offset += l; 112 } 113 114 return (0); 115 } 116 117 static int 118 i2c_info_init(struct i2c_info *ii, ifconfig_handle_t *h, const char *name) 119 { 120 uint8_t id_byte; 121 122 memset(ii, 0, sizeof(*ii)); 123 strlcpy(ii->ifr.ifr_name, name, sizeof(ii->ifr.ifr_name)); 124 ii->h = h; 125 126 /* 127 * Try to read byte 0 from i2c: 128 * Both SFF-8472 and SFF-8436 use it as 129 * 'identification byte'. 130 * Stop reading status on zero as value - 131 * this might happen in case of empty transceiver slot. 132 */ 133 id_byte = 0; 134 read_i2c(ii, SFF_8472_BASE, SFF_8472_ID, 1, &id_byte); 135 if (ii->error != 0) 136 return (-1); 137 if (id_byte == 0) { 138 h->error.errtype = OTHER; 139 h->error.errcode = ENOENT; 140 return (-1); 141 } 142 ii->id = id_byte; 143 return (0); 144 } 145 146 static int 147 get_sfp_info(struct i2c_info *ii, struct ifconfig_sfp_info *sfp) 148 { 149 uint8_t code; 150 151 read_i2c(ii, SFF_8472_BASE, SFF_8472_ID, 1, &sfp->sfp_id); 152 read_i2c(ii, SFF_8472_BASE, SFF_8472_CONNECTOR, 1, &sfp->sfp_conn); 153 154 /* Use extended compliance code if it's valid */ 155 read_i2c(ii, SFF_8472_BASE, SFF_8472_TRANS, 1, &sfp->sfp_eth_ext); 156 if (sfp->sfp_eth_ext == 0) { 157 /* Next, check 10G Ethernet/IB CCs */ 158 read_i2c(ii, SFF_8472_BASE, SFF_8472_TRANS_START, 1, &code); 159 sfp->sfp_eth_10g = find_zero_bit(sfp_eth_10g_table, code, 1); 160 if (sfp->sfp_eth_10g == 0) { 161 /* No match. Try Ethernet 1G */ 162 read_i2c(ii, SFF_8472_BASE, SFF_8472_TRANS_START + 3, 163 1, &code); 164 sfp->sfp_eth = find_zero_bit(sfp_eth_table, code, 1); 165 } 166 } 167 168 return (ii->error); 169 } 170 171 static int 172 get_qsfp_info(struct i2c_info *ii, struct ifconfig_sfp_info *sfp) 173 { 174 uint8_t code; 175 176 read_i2c(ii, SFF_8436_BASE, SFF_8436_ID, 1, &sfp->sfp_id); 177 read_i2c(ii, SFF_8436_BASE, SFF_8436_CONNECTOR, 1, &sfp->sfp_conn); 178 179 read_i2c(ii, SFF_8436_BASE, SFF_8436_STATUS, 1, &sfp->sfp_rev); 180 181 /* Check for extended specification compliance */ 182 read_i2c(ii, SFF_8436_BASE, SFF_8436_CODE_E1040100G, 1, &code); 183 if (code & SFF_8636_EXT_COMPLIANCE) { 184 read_i2c(ii, SFF_8436_BASE, SFF_8436_OPTIONS_START, 1, 185 &sfp->sfp_eth_ext); 186 sfp->sfp_eth_1040g = code; 187 } else { 188 /* Check 10/40G Ethernet class only */ 189 sfp->sfp_eth_1040g = 190 find_zero_bit(sfp_eth_1040g_table, code, 1); 191 } 192 193 return (ii->error); 194 } 195 196 int 197 ifconfig_sfp_get_sfp_info(ifconfig_handle_t *h, 198 const char *name, struct ifconfig_sfp_info *sfp) 199 { 200 struct i2c_info ii; 201 char buf[8]; 202 203 memset(sfp, 0, sizeof(*sfp)); 204 205 if (i2c_info_init(&ii, h, name) != 0) 206 return (-1); 207 208 /* Read bytes 3-10 at once */ 209 read_i2c(&ii, SFF_8472_BASE, SFF_8472_TRANS_START, 8, buf); 210 if (ii.error != 0) 211 return (ii.error); 212 213 /* Check 10G ethernet first */ 214 sfp->sfp_eth_10g = find_zero_bit(sfp_eth_10g_table, buf[0], 1); 215 if (sfp->sfp_eth_10g == 0) { 216 /* No match. Try 1G */ 217 sfp->sfp_eth = find_zero_bit(sfp_eth_table, buf[3], 1); 218 } 219 sfp->sfp_fc_len = find_zero_bit(sfp_fc_len_table, buf[4], 1); 220 sfp->sfp_fc_media = find_zero_bit(sfp_fc_media_table, buf[6], 1); 221 sfp->sfp_fc_speed = find_zero_bit(sfp_fc_speed_table, buf[7], 1); 222 sfp->sfp_cab_tech = 223 find_zero_bit(sfp_cab_tech_table, (buf[4] << 8) | buf[5], 2); 224 225 if (ifconfig_sfp_id_is_qsfp(ii.id)) 226 return (get_qsfp_info(&ii, sfp)); 227 return (get_sfp_info(&ii, sfp)); 228 } 229 230 static size_t 231 channel_count(enum sfp_id id) 232 { 233 /* TODO: other ids */ 234 switch (id) { 235 case SFP_ID_UNKNOWN: 236 return (0); 237 case SFP_ID_QSFP: 238 case SFP_ID_QSFPPLUS: 239 case SFP_ID_QSFP28: 240 return (4); 241 default: 242 return (1); 243 } 244 } 245 246 size_t 247 ifconfig_sfp_channel_count(const struct ifconfig_sfp_info *sfp) 248 { 249 return (channel_count(sfp->sfp_id)); 250 } 251 252 /* 253 * Print SFF-8472/SFF-8436 string to supplied buffer. 254 * All (vendor-specific) strings are padded right with '0x20'. 255 */ 256 static void 257 get_sff_string(struct i2c_info *ii, uint8_t addr, uint8_t off, char *dst) 258 { 259 read_i2c(ii, addr, off, SFF_VENDOR_STRING_SIZE, dst); 260 dst += SFF_VENDOR_STRING_SIZE; 261 do { *dst-- = '\0'; } while (*dst == 0x20); 262 } 263 264 static void 265 get_sff_date(struct i2c_info *ii, uint8_t addr, uint8_t off, char *dst) 266 { 267 char buf[SFF_VENDOR_DATE_SIZE]; 268 269 read_i2c(ii, addr, off, SFF_VENDOR_DATE_SIZE, buf); 270 sprintf(dst, "20%c%c-%c%c-%c%c", buf[0], buf[1], buf[2], buf[3], 271 buf[4], buf[5]); 272 } 273 274 static int 275 get_sfp_vendor_info(struct i2c_info *ii, struct ifconfig_sfp_vendor_info *vi) 276 { 277 get_sff_string(ii, SFF_8472_BASE, SFF_8472_VENDOR_START, vi->name); 278 get_sff_string(ii, SFF_8472_BASE, SFF_8472_PN_START, vi->pn); 279 get_sff_string(ii, SFF_8472_BASE, SFF_8472_SN_START, vi->sn); 280 get_sff_date(ii, SFF_8472_BASE, SFF_8472_DATE_START, vi->date); 281 return (ii->error); 282 } 283 284 static int 285 get_qsfp_vendor_info(struct i2c_info *ii, struct ifconfig_sfp_vendor_info *vi) 286 { 287 get_sff_string(ii, SFF_8436_BASE, SFF_8436_VENDOR_START, vi->name); 288 get_sff_string(ii, SFF_8436_BASE, SFF_8436_PN_START, vi->pn); 289 get_sff_string(ii, SFF_8436_BASE, SFF_8436_SN_START, vi->sn); 290 get_sff_date(ii, SFF_8436_BASE, SFF_8436_DATE_START, vi->date); 291 return (ii->error); 292 } 293 294 int 295 ifconfig_sfp_get_sfp_vendor_info(ifconfig_handle_t *h, 296 const char *name, struct ifconfig_sfp_vendor_info *vi) 297 { 298 struct i2c_info ii; 299 300 memset(vi, 0, sizeof(*vi)); 301 302 if (i2c_info_init(&ii, h, name) != 0) 303 return (-1); 304 305 if (ifconfig_sfp_id_is_qsfp(ii.id)) 306 return (get_qsfp_vendor_info(&ii, vi)); 307 return (get_sfp_vendor_info(&ii, vi)); 308 } 309 310 /* 311 * Converts internal temperature (SFF-8472, SFF-8436) 312 * 16-bit unsigned value to human-readable representation: 313 * 314 * Internally measured Module temperature are represented 315 * as a 16-bit signed twos complement value in increments of 316 * 1/256 degrees Celsius, yielding a total range of –128C to +128C 317 * that is considered valid between –40 and +125C. 318 */ 319 static double 320 get_sff_temp(struct i2c_info *ii, uint8_t addr, uint8_t off) 321 { 322 double d; 323 uint8_t buf[2]; 324 325 read_i2c(ii, addr, off, 2, buf); 326 d = (double)buf[0]; 327 d += (double)buf[1] / 256; 328 return (d); 329 } 330 331 /* 332 * Retrieves supplied voltage (SFF-8472, SFF-8436). 333 * 16-bit usigned value, treated as range 0..+6.55 Volts 334 */ 335 static double 336 get_sff_voltage(struct i2c_info *ii, uint8_t addr, uint8_t off) 337 { 338 double d; 339 uint8_t buf[2]; 340 341 read_i2c(ii, addr, off, 2, buf); 342 d = (double)((buf[0] << 8) | buf[1]); 343 return (d / 10000); 344 } 345 346 /* 347 * The following conversions assume internally-calibrated data. 348 * This is always true for SFF-8346, and explicitly checked for SFF-8472. 349 */ 350 351 double 352 power_mW(uint16_t power) 353 { 354 /* Power is specified in units of 0.1 uW. */ 355 return (1.0 * power / 10000); 356 } 357 358 double 359 power_dBm(uint16_t power) 360 { 361 return (10.0 * log10(power_mW(power))); 362 } 363 364 double 365 bias_mA(uint16_t bias) 366 { 367 /* Bias current is specified in units of 2 uA. */ 368 return (1.0 * bias / 500); 369 } 370 371 static uint16_t 372 get_sff_channel(struct i2c_info *ii, uint8_t addr, uint8_t off) 373 { 374 uint8_t buf[2]; 375 376 read_i2c(ii, addr, off, 2, buf); 377 if (ii->error != 0) 378 return (0); 379 380 return ((buf[0] << 8) + buf[1]); 381 } 382 383 static int 384 get_sfp_status(struct i2c_info *ii, struct ifconfig_sfp_status *ss) 385 { 386 uint8_t diag_type, flags; 387 388 /* Read diagnostic monitoring type */ 389 read_i2c(ii, SFF_8472_BASE, SFF_8472_DIAG_TYPE, 1, (caddr_t)&diag_type); 390 if (ii->error != 0) 391 return (-1); 392 393 /* 394 * Read monitoring data IFF it is supplied AND is 395 * internally calibrated 396 */ 397 flags = SFF_8472_DDM_DONE | SFF_8472_DDM_INTERNAL; 398 if ((diag_type & flags) != flags) { 399 ii->h->error.errtype = OTHER; 400 ii->h->error.errcode = ENXIO; 401 return (-1); 402 } 403 404 ss->temp = get_sff_temp(ii, SFF_8472_DIAG, SFF_8472_TEMP); 405 ss->voltage = get_sff_voltage(ii, SFF_8472_DIAG, SFF_8472_VCC); 406 ss->channel = calloc(channel_count(ii->id), sizeof(*ss->channel)); 407 if (ss->channel == NULL) { 408 ii->h->error.errtype = OTHER; 409 ii->h->error.errcode = ENOMEM; 410 return (-1); 411 } 412 ss->channel[0].rx = get_sff_channel(ii, SFF_8472_DIAG, SFF_8472_RX_POWER); 413 ss->channel[0].tx = get_sff_channel(ii, SFF_8472_DIAG, SFF_8472_TX_BIAS); 414 return (ii->error); 415 } 416 417 static uint32_t 418 get_qsfp_bitrate(struct i2c_info *ii) 419 { 420 uint8_t code; 421 uint32_t rate; 422 423 code = 0; 424 read_i2c(ii, SFF_8436_BASE, SFF_8436_BITRATE, 1, &code); 425 rate = code * 100; 426 if (code == 0xFF) { 427 read_i2c(ii, SFF_8436_BASE, SFF_8636_BITRATE, 1, &code); 428 rate = code * 250; 429 } 430 431 return (rate); 432 } 433 434 static int 435 get_qsfp_status(struct i2c_info *ii, struct ifconfig_sfp_status *ss) 436 { 437 size_t channels; 438 439 ss->temp = get_sff_temp(ii, SFF_8436_BASE, SFF_8436_TEMP); 440 ss->voltage = get_sff_voltage(ii, SFF_8436_BASE, SFF_8436_VCC); 441 channels = channel_count(ii->id); 442 ss->channel = calloc(channels, sizeof(*ss->channel)); 443 if (ss->channel == NULL) { 444 ii->h->error.errtype = OTHER; 445 ii->h->error.errcode = ENOMEM; 446 return (-1); 447 } 448 for (size_t chan = 0; chan < channels; ++chan) { 449 uint8_t rxoffs = SFF_8436_RX_CH1_MSB + chan * sizeof(uint16_t); 450 uint8_t txoffs = SFF_8436_TX_CH1_MSB + chan * sizeof(uint16_t); 451 ss->channel[chan].rx = 452 get_sff_channel(ii, SFF_8436_BASE, rxoffs); 453 ss->channel[chan].tx = 454 get_sff_channel(ii, SFF_8436_BASE, txoffs); 455 } 456 ss->bitrate = get_qsfp_bitrate(ii); 457 return (ii->error); 458 } 459 460 int 461 ifconfig_sfp_get_sfp_status(ifconfig_handle_t *h, const char *name, 462 struct ifconfig_sfp_status *ss) 463 { 464 struct i2c_info ii; 465 466 memset(ss, 0, sizeof(*ss)); 467 468 if (i2c_info_init(&ii, h, name) != 0) 469 return (-1); 470 471 if (ifconfig_sfp_id_is_qsfp(ii.id)) 472 return (get_qsfp_status(&ii, ss)); 473 return (get_sfp_status(&ii, ss)); 474 } 475 476 void 477 ifconfig_sfp_free_sfp_status(struct ifconfig_sfp_status *ss) 478 { 479 if (ss != NULL) 480 free(ss->channel); 481 } 482 483 static const char * 484 sfp_id_string_alt(uint8_t value) 485 { 486 const char *id; 487 488 if (value <= SFF_8024_ID_LAST) 489 id = sff_8024_id[value]; 490 else if (value > 0x80) 491 id = "Vendor specific"; 492 else 493 id = "Reserved"; 494 495 return (id); 496 } 497 498 static const char * 499 sfp_conn_string_alt(uint8_t value) 500 { 501 const char *conn; 502 503 if (value >= 0x0D && value <= 0x1F) 504 conn = "Unallocated"; 505 else if (value >= 0x24 && value <= 0x7F) 506 conn = "Unallocated"; 507 else 508 conn = "Vendor specific"; 509 510 return (conn); 511 } 512 513 void 514 ifconfig_sfp_get_sfp_info_strings(const struct ifconfig_sfp_info *sfp, 515 struct ifconfig_sfp_info_strings *strings) 516 { 517 get_sfp_info_strings(sfp, strings); 518 if (strings->sfp_id == NULL) 519 strings->sfp_id = sfp_id_string_alt(sfp->sfp_id); 520 if (strings->sfp_conn == NULL) 521 strings->sfp_conn = sfp_conn_string_alt(sfp->sfp_conn); 522 if (strings->sfp_rev == NULL) 523 strings->sfp_rev = "Unallocated"; 524 } 525 526 const char * 527 ifconfig_sfp_physical_spec(const struct ifconfig_sfp_info *sfp, 528 const struct ifconfig_sfp_info_strings *strings) 529 { 530 switch (sfp->sfp_id) { 531 case SFP_ID_UNKNOWN: 532 break; 533 case SFP_ID_QSFP: 534 case SFP_ID_QSFPPLUS: 535 case SFP_ID_QSFP28: 536 if (sfp->sfp_eth_1040g & SFP_ETH_1040G_EXTENDED) 537 return (strings->sfp_eth_ext); 538 else if (sfp->sfp_eth_1040g) 539 return (strings->sfp_eth_1040g); 540 break; 541 default: 542 if (sfp->sfp_eth_ext) 543 return (strings->sfp_eth_ext); 544 else if (sfp->sfp_eth_10g) 545 return (strings->sfp_eth_10g); 546 else if (sfp->sfp_eth) 547 return (strings->sfp_eth); 548 break; 549 } 550 return ("Unknown"); 551 } 552 553 int 554 ifconfig_sfp_get_sfp_dump(ifconfig_handle_t *h, const char *name, 555 struct ifconfig_sfp_dump *dump) 556 { 557 struct i2c_info ii; 558 uint8_t *buf = dump->data; 559 560 memset(dump->data, 0, sizeof(dump->data)); 561 562 if (i2c_info_init(&ii, h, name) != 0) 563 return (-1); 564 565 if (ifconfig_sfp_id_is_qsfp(ii.id)) { 566 read_i2c(&ii, SFF_8436_BASE, QSFP_DUMP0_START, QSFP_DUMP0_SIZE, 567 buf + QSFP_DUMP0_START); 568 read_i2c(&ii, SFF_8436_BASE, QSFP_DUMP1_START, QSFP_DUMP1_SIZE, 569 buf + QSFP_DUMP1_START); 570 } else { 571 read_i2c(&ii, SFF_8472_BASE, SFP_DUMP_START, SFP_DUMP_SIZE, 572 buf + SFP_DUMP_START); 573 } 574 575 return (ii.error != 0 ? -1 : 0); 576 } 577 578 size_t 579 ifconfig_sfp_dump_region_count(const struct ifconfig_sfp_dump *dp) 580 { 581 uint8_t id_byte = dp->data[0]; 582 583 switch ((enum sfp_id)id_byte) { 584 case SFP_ID_UNKNOWN: 585 return (0); 586 case SFP_ID_QSFP: 587 case SFP_ID_QSFPPLUS: 588 case SFP_ID_QSFP28: 589 return (2); 590 default: 591 return (1); 592 } 593 } 594