2 * Copyright (c) 2014, Alexander V. Chernikov
3 * Copyright (c) 2020, Ryan Moeller <freqlabs@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
29 #include <sys/types.h>
30 #include <sys/param.h>
31 #include <sys/ioctl.h>
32 #include <sys/socket.h>
35 #include <net/sff8436.h>
36 #include <net/sff8472.h>
48 #include <libifconfig.h>
49 #include <libifconfig_internal.h>
50 #include <libifconfig_sfp.h>
51 #include <libifconfig_sfp_tables_internal.h>
53 #define SFF_8636_EXT_COMPLIANCE 0x80
58 int error; /* Store first error */
59 enum sfp_id id; /* Module type */
63 find_zero_bit(const struct sfp_enum_metadata *table, int value, int sz)
67 for (v = 1, m = 1 << (8 * sz); v < m; v <<= 1) {
70 if (find_metadata(table, value & v) != NULL) {
78 * Reads i2c data from opened kernel socket.
81 read_i2c(struct i2c_info *ii, uint8_t addr, uint8_t off, uint8_t len,
90 ii->ifr.ifr_data = (caddr_t)&req;
94 memset(&req, 0, sizeof(req));
100 l = MIN(sizeof(req.data), len);
102 if (ifconfig_ioctlwrap(ii->h, AF_LOCAL, SIOCGI2C,
108 memcpy(&buf[i], req.data, l);
118 i2c_info_init(struct i2c_info *ii, ifconfig_handle_t *h, const char *name)
122 memset(ii, 0, sizeof(*ii));
123 strlcpy(ii->ifr.ifr_name, name, sizeof(ii->ifr.ifr_name));
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.
134 read_i2c(ii, SFF_8472_BASE, SFF_8472_ID, 1, &id_byte);
138 h->error.errtype = OTHER;
139 h->error.errcode = ENOENT;
147 get_sfp_info(struct i2c_info *ii, struct ifconfig_sfp_info *sfp)
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);
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,
164 sfp->sfp_eth = find_zero_bit(sfp_eth_table, code, 1);
172 get_qsfp_info(struct i2c_info *ii, struct ifconfig_sfp_info *sfp)
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);
179 read_i2c(ii, SFF_8436_BASE, SFF_8436_STATUS, 1, &sfp->sfp_rev);
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,
187 /* Check 10/40G Ethernet class only */
189 find_zero_bit(sfp_eth_1040g_table, code, 1);
196 ifconfig_sfp_get_sfp_info(ifconfig_handle_t *h,
197 const char *name, struct ifconfig_sfp_info *sfp)
202 memset(sfp, 0, sizeof(*sfp));
204 if (i2c_info_init(&ii, h, name) != 0)
207 /* Read bytes 3-10 at once */
208 read_i2c(&ii, SFF_8472_BASE, SFF_8472_TRANS_START, 8, buf);
212 /* Check 10G ethernet first */
213 sfp->sfp_eth_10g = find_zero_bit(sfp_eth_10g_table, buf[0], 1);
214 if (sfp->sfp_eth_10g == 0) {
215 /* No match. Try 1G */
216 sfp->sfp_eth = find_zero_bit(sfp_eth_table, buf[3], 1);
218 sfp->sfp_fc_len = find_zero_bit(sfp_fc_len_table, buf[4], 1);
219 sfp->sfp_fc_media = find_zero_bit(sfp_fc_media_table, buf[6], 1);
220 sfp->sfp_fc_speed = find_zero_bit(sfp_fc_speed_table, buf[7], 1);
222 find_zero_bit(sfp_cab_tech_table, (buf[4] << 8) | buf[5], 2);
224 if (ifconfig_sfp_id_is_qsfp(ii.id))
225 return (get_qsfp_info(&ii, sfp));
226 return (get_sfp_info(&ii, sfp));
230 channel_count(enum sfp_id id)
232 /* TODO: other ids */
237 case SFP_ID_QSFPPLUS:
246 ifconfig_sfp_channel_count(const struct ifconfig_sfp_info *sfp)
248 return (channel_count(sfp->sfp_id));
252 * Print SFF-8472/SFF-8436 string to supplied buffer.
253 * All (vendor-specific) strings are padded right with '0x20'.
256 get_sff_string(struct i2c_info *ii, uint8_t addr, uint8_t off, char *dst)
258 read_i2c(ii, addr, off, SFF_VENDOR_STRING_SIZE, dst);
259 dst += SFF_VENDOR_STRING_SIZE;
260 do { *dst-- = '\0'; } while (*dst == 0x20);
264 get_sff_date(struct i2c_info *ii, uint8_t addr, uint8_t off, char *dst)
266 char buf[SFF_VENDOR_DATE_SIZE];
268 read_i2c(ii, addr, off, SFF_VENDOR_DATE_SIZE, buf);
269 sprintf(dst, "20%c%c-%c%c-%c%c", buf[0], buf[1], buf[2], buf[3],
274 get_sfp_vendor_info(struct i2c_info *ii, struct ifconfig_sfp_vendor_info *vi)
276 get_sff_string(ii, SFF_8472_BASE, SFF_8472_VENDOR_START, vi->name);
277 get_sff_string(ii, SFF_8472_BASE, SFF_8472_PN_START, vi->pn);
278 get_sff_string(ii, SFF_8472_BASE, SFF_8472_SN_START, vi->sn);
279 get_sff_date(ii, SFF_8472_BASE, SFF_8472_DATE_START, vi->date);
284 get_qsfp_vendor_info(struct i2c_info *ii, struct ifconfig_sfp_vendor_info *vi)
286 get_sff_string(ii, SFF_8436_BASE, SFF_8436_VENDOR_START, vi->name);
287 get_sff_string(ii, SFF_8436_BASE, SFF_8436_PN_START, vi->pn);
288 get_sff_string(ii, SFF_8436_BASE, SFF_8436_SN_START, vi->sn);
289 get_sff_date(ii, SFF_8436_BASE, SFF_8436_DATE_START, vi->date);
294 ifconfig_sfp_get_sfp_vendor_info(ifconfig_handle_t *h,
295 const char *name, struct ifconfig_sfp_vendor_info *vi)
299 memset(vi, 0, sizeof(*vi));
301 if (i2c_info_init(&ii, h, name) != 0)
304 if (ifconfig_sfp_id_is_qsfp(ii.id))
305 return (get_qsfp_vendor_info(&ii, vi));
306 return (get_sfp_vendor_info(&ii, vi));
310 * Converts internal temperature (SFF-8472, SFF-8436)
311 * 16-bit unsigned value to human-readable representation:
313 * Internally measured Module temperature are represented
314 * as a 16-bit signed twos complement value in increments of
315 * 1/256 degrees Celsius, yielding a total range of –128C to +128C
316 * that is considered valid between –40 and +125C.
319 get_sff_temp(struct i2c_info *ii, uint8_t addr, uint8_t off)
324 read_i2c(ii, addr, off, 2, buf);
326 d += (double)buf[1] / 256;
331 * Retrieves supplied voltage (SFF-8472, SFF-8436).
332 * 16-bit usigned value, treated as range 0..+6.55 Volts
335 get_sff_voltage(struct i2c_info *ii, uint8_t addr, uint8_t off)
340 read_i2c(ii, addr, off, 2, buf);
341 d = (double)((buf[0] << 8) | buf[1]);
346 * The following conversions assume internally-calibrated data.
347 * This is always true for SFF-8346, and explicitly checked for SFF-8472.
351 power_mW(uint16_t power)
353 /* Power is specified in units of 0.1 uW. */
354 return (1.0 * power / 10000);
358 power_dBm(uint16_t power)
360 return (10.0 * log10(power_mW(power)));
364 bias_mA(uint16_t bias)
366 /* Bias current is specified in units of 2 uA. */
367 return (1.0 * bias / 500);
371 get_sff_channel(struct i2c_info *ii, uint8_t addr, uint8_t off)
375 read_i2c(ii, addr, off, 2, buf);
379 return ((buf[0] << 8) + buf[1]);
383 get_sfp_status(struct i2c_info *ii, struct ifconfig_sfp_status *ss)
385 uint8_t diag_type, flags;
387 /* Read diagnostic monitoring type */
388 read_i2c(ii, SFF_8472_BASE, SFF_8472_DIAG_TYPE, 1, (caddr_t)&diag_type);
393 * Read monitoring data IFF it is supplied AND is
394 * internally calibrated
396 flags = SFF_8472_DDM_DONE | SFF_8472_DDM_INTERNAL;
397 if ((diag_type & flags) != flags) {
398 ii->h->error.errtype = OTHER;
399 ii->h->error.errcode = ENXIO;
403 ss->temp = get_sff_temp(ii, SFF_8472_DIAG, SFF_8472_TEMP);
404 ss->voltage = get_sff_voltage(ii, SFF_8472_DIAG, SFF_8472_VCC);
405 ss->channel = calloc(channel_count(ii->id), sizeof(*ss->channel));
406 if (ss->channel == NULL) {
407 ii->h->error.errtype = OTHER;
408 ii->h->error.errcode = ENOMEM;
411 ss->channel[0].rx = get_sff_channel(ii, SFF_8472_DIAG, SFF_8472_RX_POWER);
412 ss->channel[0].tx = get_sff_channel(ii, SFF_8472_DIAG, SFF_8472_TX_BIAS);
417 get_qsfp_bitrate(struct i2c_info *ii)
423 read_i2c(ii, SFF_8436_BASE, SFF_8436_BITRATE, 1, &code);
426 read_i2c(ii, SFF_8436_BASE, SFF_8636_BITRATE, 1, &code);
434 get_qsfp_status(struct i2c_info *ii, struct ifconfig_sfp_status *ss)
438 ss->temp = get_sff_temp(ii, SFF_8436_BASE, SFF_8436_TEMP);
439 ss->voltage = get_sff_voltage(ii, SFF_8436_BASE, SFF_8436_VCC);
440 channels = channel_count(ii->id);
441 ss->channel = calloc(channels, sizeof(*ss->channel));
442 if (ss->channel == NULL) {
443 ii->h->error.errtype = OTHER;
444 ii->h->error.errcode = ENOMEM;
447 for (size_t chan = 0; chan < channels; ++chan) {
448 uint8_t rxoffs = SFF_8436_RX_CH1_MSB + chan * sizeof(uint16_t);
449 uint8_t txoffs = SFF_8436_TX_CH1_MSB + chan * sizeof(uint16_t);
450 ss->channel[chan].rx =
451 get_sff_channel(ii, SFF_8436_BASE, rxoffs);
452 ss->channel[chan].tx =
453 get_sff_channel(ii, SFF_8436_BASE, txoffs);
455 ss->bitrate = get_qsfp_bitrate(ii);
460 ifconfig_sfp_get_sfp_status(ifconfig_handle_t *h, const char *name,
461 struct ifconfig_sfp_status *ss)
465 memset(ss, 0, sizeof(*ss));
467 if (i2c_info_init(&ii, h, name) != 0)
470 if (ifconfig_sfp_id_is_qsfp(ii.id))
471 return (get_qsfp_status(&ii, ss));
472 return (get_sfp_status(&ii, ss));
476 ifconfig_sfp_free_sfp_status(struct ifconfig_sfp_status *ss)
483 sfp_id_string_alt(uint8_t value)
487 if (value <= SFF_8024_ID_LAST)
488 id = sff_8024_id[value];
489 else if (value > 0x80)
490 id = "Vendor specific";
498 sfp_conn_string_alt(uint8_t value)
502 if (value >= 0x0D && value <= 0x1F)
503 conn = "Unallocated";
504 else if (value >= 0x24 && value <= 0x7F)
505 conn = "Unallocated";
507 conn = "Vendor specific";
513 ifconfig_sfp_get_sfp_info_strings(const struct ifconfig_sfp_info *sfp,
514 struct ifconfig_sfp_info_strings *strings)
516 get_sfp_info_strings(sfp, strings);
517 if (strings->sfp_id == NULL)
518 strings->sfp_id = sfp_id_string_alt(sfp->sfp_id);
519 if (strings->sfp_conn == NULL)
520 strings->sfp_conn = sfp_conn_string_alt(sfp->sfp_conn);
521 if (strings->sfp_rev == NULL)
522 strings->sfp_rev = "Unallocated";
526 ifconfig_sfp_physical_spec(const struct ifconfig_sfp_info *sfp,
527 const struct ifconfig_sfp_info_strings *strings)
529 switch (sfp->sfp_id) {
533 case SFP_ID_QSFPPLUS:
535 if (sfp->sfp_eth_1040g & SFP_ETH_1040G_EXTENDED)
536 return (strings->sfp_eth_ext);
537 else if (sfp->sfp_eth_1040g)
538 return (strings->sfp_eth_1040g);
541 if (sfp->sfp_eth_ext)
542 return (strings->sfp_eth_ext);
543 else if (sfp->sfp_eth_10g)
544 return (strings->sfp_eth_10g);
545 else if (sfp->sfp_eth)
546 return (strings->sfp_eth);
553 ifconfig_sfp_get_sfp_dump(ifconfig_handle_t *h, const char *name,
554 struct ifconfig_sfp_dump *dump)
557 uint8_t *buf = dump->data;
559 memset(dump->data, 0, sizeof(dump->data));
561 if (i2c_info_init(&ii, h, name) != 0)
564 if (ifconfig_sfp_id_is_qsfp(ii.id)) {
565 read_i2c(&ii, SFF_8436_BASE, QSFP_DUMP0_START, QSFP_DUMP0_SIZE,
566 buf + QSFP_DUMP0_START);
567 read_i2c(&ii, SFF_8436_BASE, QSFP_DUMP1_START, QSFP_DUMP1_SIZE,
568 buf + QSFP_DUMP1_START);
570 read_i2c(&ii, SFF_8472_BASE, SFP_DUMP_START, SFP_DUMP_SIZE,
571 buf + SFP_DUMP_START);
574 return (ii.error != 0 ? -1 : 0);
578 ifconfig_sfp_dump_region_count(const struct ifconfig_sfp_dump *dp)
580 uint8_t id_byte = dp->data[0];
582 switch ((enum sfp_id)id_byte) {
586 case SFP_ID_QSFPPLUS: