Author: melifaro
Date: Sat Aug 15 17:52:55 2015
New Revision: 286810
URL: https://svnweb.freebsd.org/changeset/base/286810

Log:
  MFC r270064,r270068,r270069,r270115,r270129,r270287,r270822,r271014,
    r271524,r273541,r282967,r283009,r283364.
  
  Add support for reading i2c SFP/SFP+ data from NIC driver and
  presenting most interesting fields via ifconfig -v.
  This version supports Intel ixgbe driver only.
  
  Tested on:      Cisco,Intel,Mellanox,ModuleTech,Molex transceivers
  
  * Add new net/sff8436.h containing constants used to access
    QSFP+ data via i2c inteface. These constants has been taken
    from SFF-8436 "QSFP+ 10 Gbs 4X PLUGGABLE TRANSCEIVER" standard
    rev 4.8.
  * Add support for printing QSFP+ information from 40G NICs
    such as Chelsio T5.
  
  Example:
  cxl1: flags=8843<UP,BROADCAST,RUNNING,SIMPLEX,MULTICAST> metric 0 mtu 1500
          options=ec07bb<RXCSUM,TXCSUM,VLAN_MTU,VLAN_HWTAGGING,JUMBO_MTU,.....>
          ether 00:07:43:28:ad:08
          nd6 options=29<PERFORMNUD,IFDISABLED,AUTO_LINKLOCAL>
          media: Ethernet 40Gbase-LR4 <full-duplex>
          status: active
          plugged: QSFP+ 40GBASE-LR4 (MPO Parallel Optic)
          vendor: OEM PN: OP-QSFP-40G-LR4 SN: 20140318001 DATE: 2014-03-18
          module temperature: 64.06 C voltage: 3.26 Volts
          lane 1: RX: 0.47 mW (-3.21 dBm) TX: 2.78 mW (4.46 dBm)
          lane 2: RX: 0.20 mW (-6.94 dBm) TX: 2.80 mW (4.47 dBm)
          lane 3: RX: 0.18 mW (-7.38 dBm) TX: 2.79 mW (4.47 dBm)
          lane 4: RX: 0.90 mW (-0.45 dBm) TX: 2.80 mW (4.48 dBm)
  
  Tested on:      Chelsio T5
  Tested on:      Mellanox/Huawei passive/active cables/transceivers.
  
  Sponsored by:   Yandex LLC

Added:
  stable/10/sbin/ifconfig/sfp.c
     - copied, changed from r270064, head/sbin/ifconfig/sfp.c
  stable/10/sys/net/sff8436.h
     - copied, changed from r270287, head/sys/net/sff8436.h
Modified:
  stable/10/sbin/ifconfig/Makefile
  stable/10/sbin/ifconfig/ifconfig.c
  stable/10/sbin/ifconfig/ifconfig.h
  stable/10/sys/dev/ixgbe/if_ix.c
  stable/10/sys/dev/ixgbe/ixgbe.h
  stable/10/sys/net/if.h
  stable/10/sys/net/sff8472.h
  stable/10/sys/sys/sockio.h
Directory Properties:
  stable/10/   (props changed)

Modified: stable/10/sbin/ifconfig/Makefile
==============================================================================
--- stable/10/sbin/ifconfig/Makefile    Sat Aug 15 16:13:28 2015        
(r286809)
+++ stable/10/sbin/ifconfig/Makefile    Sat Aug 15 17:52:55 2015        
(r286810)
@@ -35,6 +35,10 @@ SRCS+=       ifvxlan.c               # VXLAN support
 SRCS+= ifgre.c                 # GRE keys etc
 SRCS+= ifgif.c                 # GIF reversed header workaround
 
+SRCS+= sfp.c                   # SFP/SFP+ information
+DPADD+=        ${LIBM}
+LDADD+=        -lm
+
 SRCS+= ifieee80211.c regdomain.c # SIOC[GS]IEEE80211 support
 DPADD+=        ${LIBBSDXML} ${LIBSBUF}
 LDADD+=        -lbsdxml -lsbuf

Modified: stable/10/sbin/ifconfig/ifconfig.c
==============================================================================
--- stable/10/sbin/ifconfig/ifconfig.c  Sat Aug 15 16:13:28 2015        
(r286809)
+++ stable/10/sbin/ifconfig/ifconfig.c  Sat Aug 15 17:52:55 2015        
(r286810)
@@ -1025,6 +1025,9 @@ status(const struct afswtch *afp, const 
        if (ioctl(s, SIOCGIFSTATUS, &ifs) == 0) 
                printf("%s", ifs.ascii);
 
+       if (verbose > 0)
+               sfp_status(s, &ifr, verbose);
+
        close(s);
        return;
 }

Modified: stable/10/sbin/ifconfig/ifconfig.h
==============================================================================
--- stable/10/sbin/ifconfig/ifconfig.h  Sat Aug 15 16:13:28 2015        
(r286809)
+++ stable/10/sbin/ifconfig/ifconfig.h  Sat Aug 15 17:52:55 2015        
(r286810)
@@ -144,6 +144,8 @@ void        ifmaybeload(const char *name);
 typedef void clone_callback_func(int, struct ifreq *);
 void   clone_setdefcallback(const char *, clone_callback_func *);
 
+void   sfp_status(int s, struct ifreq *ifr, int verbose);
+
 /*
  * XXX expose this so modules that neeed to know of any pending
  * operations on ifmedia can avoid cmd line ordering confusion.

Copied and modified: stable/10/sbin/ifconfig/sfp.c (from r270064, 
head/sbin/ifconfig/sfp.c)
==============================================================================
--- head/sbin/ifconfig/sfp.c    Sat Aug 16 19:13:52 2014        (r270064, copy 
source)
+++ stable/10/sbin/ifconfig/sfp.c       Sat Aug 15 17:52:55 2015        
(r286810)
@@ -34,10 +34,13 @@ static const char rcsid[] =
 #include <sys/socket.h>
 
 #include <net/if.h>
+#include <net/sff8436.h>
+#include <net/sff8472.h>
 
 #include <math.h>
 #include <err.h>
 #include <errno.h>
+#include <fcntl.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -45,41 +48,19 @@ static const char rcsid[] =
 
 #include "ifconfig.h"
 
-/* 2wire addresses */
-#define        SFP_ADDR_MSA    0xA0    /* Identification data */
-#define        SFP_ADDR_DDM    0xA2    /* digital monitoring interface */
-
-/* Definitions from Table 3.1 */
-#define SFP_MSA_IDENTIFIER     0       /* Type of transceiver (T. 3.2), 1B */
-#define        SFP_MSA_CONNECTOR       2       /* Connector type (T. 3.3), 1B 
*/
-
-#define        SFP_MSA_TRANSCEIVER_CLASS       3       /* Ethernet/Sonet/IB 
code, 1B */
-
-#define        SFP_MSA_VENDOR_NAME     20      /* ASCII vendor name, 16B */
-#define        SFP_MSA_VENDOR_PN       40      /* ASCII vendor partnum, 16B */
-#define        SFP_MSA_VENDOR_SN       68      /* ASCII vendor serialnum, 16B 
*/
-#define        SFP_MSA_VENDOR_DATE     84      /* Vendor's date code, 8B */
-#define        SFP_MSA_DMONTYPE        92      /* Type of disagnostic 
monitoring, 1B */
-
-/* Definitions from table 3.17 */
-#define        SFP_DDM_TEMP            96      /* Module temperature, 2B */
-#define        SFP_DDM_TXPOWER         102     /* Measured TX output power, 2B 
*/
-#define        SFP_DDM_RXPOWER         104     /* Measured RX input power, 2B 
*/
-
-struct i2c_info;
-typedef int (read_i2c)(struct i2c_info *ii, uint8_t addr, uint8_t off,
-    uint8_t len, caddr_t buf);
-
 struct i2c_info {
-       int s;
-       int error;
-       struct ifreq *ifr;
-       read_i2c *f;
-       uint8_t diag_type;
-       char *textbuf;
-       size_t bufsize;
+       int fd;                 /* fd to issue SIOCGI2C */
+       int error;              /* Store first error */
+       int qsfp;               /* True if transceiver is QSFP */
+       int do_diag;            /* True if we need to request DDM */
+       struct ifreq *ifr;      /* Pointer to pre-filled ifreq */
 };
 
+static int read_i2c(struct i2c_info *ii, uint8_t addr, uint8_t off,
+    uint8_t len, uint8_t *buf);
+static void dump_i2c_data(struct i2c_info *ii, uint8_t addr, uint8_t off,
+    uint8_t len);
+
 struct _nv {
        int v;
        const char *n;
@@ -88,25 +69,6 @@ struct _nv {
 const char *find_value(struct _nv *x, int value);
 const char *find_zero_bit(struct _nv *x, int value, int sz);
 
-
-/* SFF-8472 Rev. 11.4 table 3.2: Identifier values */
-static struct _nv ids[] = {
-       { 0x00, "Unknown" },
-       { 0x01, "GBIC" },
-       { 0x02, "SFF" },
-       { 0x03, "SFP/SFP+" },
-       { 0x04, "300 pin XBI" },
-       { 0x05, "Xenpak" },
-       { 0x06, "XFP" },
-       { 0x07, "XFF" },
-       { 0x08, "XFP-E" },
-       { 0x09, "XPak" },
-       { 0x0A, "X2" },
-       { 0x0B, "DWDM-SFP/DWDM-SFP+" },
-       { 0x0C, "QSFP" },
-       { 0, NULL, },
-};
-
 /* SFF-8472 Rev. 11.4 table 3.4: Connector values */
 static struct _nv conn[] = {
        { 0x00, "Unknown" },
@@ -125,77 +87,12 @@ static struct _nv conn[] = {
        { 0x20, "HSSDC II" },
        { 0x21, "Copper pigtail" },
        { 0x22, "RJ45" },
+       { 0x23, "No separate connector" }, /* SFF-8436 */
        { 0, NULL }
 };
 
-const char *
-find_value(struct _nv *x, int value)
-{
-       for (; x->n != NULL; x++)
-               if (x->v == value)
-                       return (x->n);
-       return (NULL);
-}
-
-const char *
-find_zero_bit(struct _nv *x, int value, int sz)
-{
-       int v, m;
-       const char *s;
-
-       v = 1;
-       for (v = 1, m = 1 << (8 * sz); v < m; v *= 2) {
-               if ((value & v) == 0)
-                       continue;
-               if ((s = find_value(x, value & v)) != NULL) {
-                       value &= ~v;
-                       return (s);
-               }
-       }
-
-       return (NULL);
-}
-
-static void
-get_sfp_identifier(struct i2c_info *ii, char *buf, size_t size)
-{
-       const char *x;
-       uint8_t data;
-
-       ii->f(ii, SFP_ADDR_MSA, SFP_MSA_IDENTIFIER, 1, (caddr_t)&data);
-
-       if ((x = find_value(ids, data)) == NULL) {
-               if (data > 0x80)
-                       x = "Vendor specific";
-               else
-                       x = "Reserved";
-       }
-
-       snprintf(buf, size, "%s", x);
-}
-
-static void
-get_sfp_connector(struct i2c_info *ii, char *buf, size_t size)
-{
-       const char *x;
-       uint8_t data;
-
-       ii->f(ii, SFP_ADDR_MSA, SFP_MSA_CONNECTOR, 1, (caddr_t)&data);
-
-       if ((x = find_value(conn, data)) == NULL) {
-               if (data >= 0x0D && data <= 0x1F)
-                       x = "Unallocated";
-               else if (data >= 0x23 && data <= 0x7F)
-                       x = "Unallocated";
-               else
-                       x = "Vendor specific";
-       }
-
-       snprintf(buf, size, "%s", x);
-}
-
 /* SFF-8472 Rev. 11.4 table 3.5: Transceiver codes */
-/* 10G Ethernet compliance codes, byte 3 */
+/* 10G Ethernet/IB compliance codes, byte 3 */
 static struct _nv eth_10g[] = {
        { 0x80, "10G Base-ER" },
        { 0x40, "10G Base-LRM" },
@@ -270,6 +167,145 @@ static struct _nv fc_speed[] = {
        { 0, NULL }
 };
 
+/* SFF-8436 Rev. 4.8 table 33: Specification compliance  */
+
+/* 10/40G Ethernet compliance codes, byte 128 + 3 */
+static struct _nv eth_1040g[] = {
+       { 0x80, "Reserved" },
+       { 0x40, "10GBASE-LRM" },
+       { 0x20, "10GBASE-LR" },
+       { 0x10, "10GBASE-SR" },
+       { 0x08, "40GBASE-CR4" },
+       { 0x04, "40GBASE-SR4" },
+       { 0x02, "40GBASE-LR4" },
+       { 0x01, "40G Active Cable" },
+       { 0, NULL }
+};
+
+/* SFF-8636 Rev. 2.5 table 6.3: Revision compliance */
+static struct _nv rev_compl[] = {
+       { 0x1, "SFF-8436 rev <=4.8" },
+       { 0x2, "SFF-8436 rev <=4.8" },
+       { 0x3, "SFF-8636 rev <=1.3" },
+       { 0x4, "SFF-8636 rev <=1.4" },
+       { 0x5, "SFF-8636 rev <=1.5" },
+       { 0x6, "SFF-8636 rev <=2.0" },
+       { 0x7, "SFF-8636 rev <=2.5" },
+       { 0x0, "Unspecified" }
+};
+
+const char *
+find_value(struct _nv *x, int value)
+{
+       for (; x->n != NULL; x++)
+               if (x->v == value)
+                       return (x->n);
+       return (NULL);
+}
+
+const char *
+find_zero_bit(struct _nv *x, int value, int sz)
+{
+       int v, m;
+       const char *s;
+
+       v = 1;
+       for (v = 1, m = 1 << (8 * sz); v < m; v *= 2) {
+               if ((value & v) == 0)
+                       continue;
+               if ((s = find_value(x, value & v)) != NULL) {
+                       value &= ~v;
+                       return (s);
+               }
+       }
+
+       return (NULL);
+}
+
+static void
+convert_sff_identifier(char *buf, size_t size, uint8_t value)
+{
+       const char *x;
+
+       x = NULL;
+       if (value <= SFF_8024_ID_LAST)
+               x = sff_8024_id[value];
+       else {
+               if (value > 0x80)
+                       x = "Vendor specific";
+               else
+                       x = "Reserved";
+       }
+
+       snprintf(buf, size, "%s", x);
+}
+
+static void
+convert_sff_connector(char *buf, size_t size, uint8_t value)
+{
+       const char *x;
+
+       if ((x = find_value(conn, value)) == NULL) {
+               if (value >= 0x0D && value <= 0x1F)
+                       x = "Unallocated";
+               else if (value >= 0x24 && value <= 0x7F)
+                       x = "Unallocated";
+               else
+                       x = "Vendor specific";
+       }
+
+       snprintf(buf, size, "%s", x);
+}
+
+static void
+convert_sff_rev_compliance(char *buf, size_t size, uint8_t value)
+{
+       const char *x;
+
+       if (value > 0x07)
+               x = "Unallocated";
+       else
+               x = find_value(rev_compl, value);
+
+       snprintf(buf, size, "%s", x);
+}
+
+static void
+get_sfp_identifier(struct i2c_info *ii, char *buf, size_t size)
+{
+       uint8_t data;
+
+       read_i2c(ii, SFF_8472_BASE, SFF_8472_ID, 1, &data);
+       convert_sff_identifier(buf, size, data);
+}
+
+static void
+get_sfp_connector(struct i2c_info *ii, char *buf, size_t size)
+{
+       uint8_t data;
+
+       read_i2c(ii, SFF_8472_BASE, SFF_8472_CONNECTOR, 1, &data);
+       convert_sff_connector(buf, size, data);
+}
+
+static void
+get_qsfp_identifier(struct i2c_info *ii, char *buf, size_t size)
+{
+       uint8_t data;
+
+       read_i2c(ii, SFF_8436_BASE, SFF_8436_ID, 1, &data);
+       convert_sff_identifier(buf, size, data);
+}
+
+static void
+get_qsfp_connector(struct i2c_info *ii, char *buf, size_t size)
+{
+       uint8_t data;
+
+       read_i2c(ii, SFF_8436_BASE, SFF_8436_CONNECTOR, 1, &data);
+       convert_sff_connector(buf, size, data);
+}
+
 static void
 printf_sfp_transceiver_descr(struct i2c_info *ii, char *buf, size_t size)
 {
@@ -283,9 +319,9 @@ printf_sfp_transceiver_descr(struct i2c_
        tech_speed = NULL;
 
        /* Read bytes 3-10 at once */
-       ii->f(ii, SFP_ADDR_MSA, 3, 8, &xbuf[3]);
+       read_i2c(ii, SFF_8472_BASE, SFF_8472_TRANS_START, 8, &xbuf[3]);
 
-       /* Check 10G first */
+       /* Check 10G ethernet first */
        tech_class = find_zero_bit(eth_10g, xbuf[3], 1);
        if (tech_class == NULL) {
                /* No match. Try 1G */
@@ -310,12 +346,16 @@ get_sfp_transceiver_class(struct i2c_inf
        const char *tech_class;
        uint8_t code;
 
+       unsigned char qbuf[8];
+       read_i2c(ii, SFF_8472_BASE, SFF_8472_TRANS_START, 8, (uint8_t *)qbuf);
+
        /* Check 10G Ethernet/IB first */
-       ii->f(ii, SFP_ADDR_MSA, SFP_MSA_TRANSCEIVER_CLASS, 1, (caddr_t)&code);
+       read_i2c(ii, SFF_8472_BASE, SFF_8472_TRANS_START, 1, &code);
        tech_class = find_zero_bit(eth_10g, code, 1);
        if (tech_class == NULL) {
                /* No match. Try Ethernet 1G */
-               ii->f(ii, SFP_ADDR_MSA, 6, 1, (caddr_t)&code);
+               read_i2c(ii, SFF_8472_BASE, SFF_8472_TRANS_START + 3,
+                   1, (caddr_t)&code);
                tech_class = find_zero_bit(eth_compat, code, 1);
        }
 
@@ -325,49 +365,72 @@ get_sfp_transceiver_class(struct i2c_inf
        snprintf(buf, size, "%s", tech_class);
 }
 
+static void
+get_qsfp_transceiver_class(struct i2c_info *ii, char *buf, size_t size)
+{
+       const char *tech_class;
+       uint8_t code;
+
+       /* Check 10/40G Ethernet class only */
+       read_i2c(ii, SFF_8436_BASE, SFF_8436_CODE_E1040G, 1, &code);
+       tech_class = find_zero_bit(eth_1040g, code, 1);
+       if (tech_class == NULL)
+               tech_class = "Unknown";
 
+       snprintf(buf, size, "%s", tech_class);
+}
+
+/*
+ * Print SFF-8472/SFF-8436 string to supplied buffer.
+ * All (vendor-specific) strings are padded right with '0x20'.
+ */
 static void
-get_sfp_vendor_name(struct i2c_info *ii, char *buf, size_t size)
+convert_sff_name(char *buf, size_t size, char *xbuf)
 {
-       char xbuf[17], *p;
+       char *p;
 
-       memset(xbuf, 0, sizeof(xbuf));
-       /* ASCII String, right-padded with 0x20 */
-       ii->f(ii, SFP_ADDR_MSA, SFP_MSA_VENDOR_NAME, 16, xbuf);
        for (p = &xbuf[16]; *(p - 1) == 0x20; p--)
                ;
        *p = '\0';
-
        snprintf(buf, size, "%s", xbuf);
 }
 
 static void
-get_sfp_vendor_pn(struct i2c_info *ii, char *buf, size_t size)
+convert_sff_date(char *buf, size_t size, char *xbuf)
+{
+
+       snprintf(buf, size, "20%c%c-%c%c-%c%c", xbuf[0], xbuf[1],
+           xbuf[2], xbuf[3], xbuf[4], xbuf[5]);
+}
+
+static void
+get_sfp_vendor_name(struct i2c_info *ii, char *buf, size_t size)
 {
-       char xbuf[17], *p;
+       char xbuf[17];
 
        memset(xbuf, 0, sizeof(xbuf));
-       /* ASCII String, right-padded with 0x20 */
-       ii->f(ii, SFP_ADDR_MSA, SFP_MSA_VENDOR_PN, 16, xbuf);
-       for (p = &xbuf[16]; *(p - 1) == 0x20; p--)
-               ;
-       *p = '\0';
+       read_i2c(ii, SFF_8472_BASE, SFF_8472_VENDOR_START, 16, (uint8_t *)xbuf);
+       convert_sff_name(buf, size, xbuf);
+}
 
-       snprintf(buf, size, "%s", xbuf);
+static void
+get_sfp_vendor_pn(struct i2c_info *ii, char *buf, size_t size)
+{
+       char xbuf[17];
+
+       memset(xbuf, 0, sizeof(xbuf));
+       read_i2c(ii, SFF_8472_BASE, SFF_8472_PN_START, 16, (uint8_t *)xbuf);
+       convert_sff_name(buf, size, xbuf);
 }
 
 static void
 get_sfp_vendor_sn(struct i2c_info *ii, char *buf, size_t size)
 {
-       char xbuf[17], *p;
+       char xbuf[17];
 
        memset(xbuf, 0, sizeof(xbuf));
-       /* ASCII String, right-padded with 0x20 */
-       ii->f(ii, SFP_ADDR_MSA, SFP_MSA_VENDOR_SN, 16, xbuf);
-       for (p = &xbuf[16]; *(p - 1) == 0x20; p--)
-               ;
-       *p = '\0';
-       snprintf(buf, size, "%s", xbuf);
+       read_i2c(ii, SFF_8472_BASE, SFF_8472_SN_START, 16, (uint8_t *)xbuf);
+       convert_sff_name(buf, size, xbuf);
 }
 
 static void
@@ -377,9 +440,48 @@ get_sfp_vendor_date(struct i2c_info *ii,
 
        memset(xbuf, 0, sizeof(xbuf));
        /* Date code, see Table 3.8 for description */
-       ii->f(ii, SFP_ADDR_MSA, SFP_MSA_VENDOR_DATE, 6, xbuf);
-       snprintf(buf, size, "20%c%c-%c%c-%c%c", xbuf[0], xbuf[1],
-           xbuf[2], xbuf[3], xbuf[4], xbuf[5]);
+       read_i2c(ii, SFF_8472_BASE, SFF_8472_DATE_START, 6, (uint8_t *)xbuf);
+       convert_sff_date(buf, size, xbuf);
+}
+
+static void
+get_qsfp_vendor_name(struct i2c_info *ii, char *buf, size_t size)
+{
+       char xbuf[17];
+
+       memset(xbuf, 0, sizeof(xbuf));
+       read_i2c(ii, SFF_8436_BASE, SFF_8436_VENDOR_START, 16, (uint8_t *)xbuf);
+       convert_sff_name(buf, size, xbuf);
+}
+
+static void
+get_qsfp_vendor_pn(struct i2c_info *ii, char *buf, size_t size)
+{
+       char xbuf[17];
+
+       memset(xbuf, 0, sizeof(xbuf));
+       read_i2c(ii, SFF_8436_BASE, SFF_8436_PN_START, 16, (uint8_t *)xbuf);
+       convert_sff_name(buf, size, xbuf);
+}
+
+static void
+get_qsfp_vendor_sn(struct i2c_info *ii, char *buf, size_t size)
+{
+       char xbuf[17];
+
+       memset(xbuf, 0, sizeof(xbuf));
+       read_i2c(ii, SFF_8436_BASE, SFF_8436_SN_START, 16, (uint8_t *)xbuf);
+       convert_sff_name(buf, size, xbuf);
+}
+
+static void
+get_qsfp_vendor_date(struct i2c_info *ii, char *buf, size_t size)
+{
+       char xbuf[6];
+
+       memset(xbuf, 0, sizeof(xbuf));
+       read_i2c(ii, SFF_8436_BASE, SFF_8436_DATE_START, 6, (uint8_t *)xbuf);
+       convert_sff_date(buf, size, xbuf);
 }
 
 static void
@@ -388,156 +490,393 @@ print_sfp_vendor(struct i2c_info *ii, ch
        char xbuf[80];
 
        memset(xbuf, 0, sizeof(xbuf));
-       get_sfp_vendor_name(ii, xbuf, 20);
-       get_sfp_vendor_pn(ii, &xbuf[20], 20);
-       get_sfp_vendor_sn(ii, &xbuf[40], 20);
-       get_sfp_vendor_date(ii, &xbuf[60], 20);
+       if (ii->qsfp != 0) {
+               get_qsfp_vendor_name(ii, xbuf, 20);
+               get_qsfp_vendor_pn(ii, &xbuf[20], 20);
+               get_qsfp_vendor_sn(ii, &xbuf[40], 20);
+               get_qsfp_vendor_date(ii, &xbuf[60], 20);
+       } else {
+               get_sfp_vendor_name(ii, xbuf, 20);
+               get_sfp_vendor_pn(ii, &xbuf[20], 20);
+               get_sfp_vendor_sn(ii, &xbuf[40], 20);
+               get_sfp_vendor_date(ii, &xbuf[60], 20);
+       }
 
        snprintf(buf, size, "vendor: %s PN: %s SN: %s DATE: %s",
            xbuf, &xbuf[20],  &xbuf[40], &xbuf[60]);
 }
 
+/*
+ * Converts internal templerature (SFF-8472, SFF-8436)
+ * 16-bit unsigned value to human-readable representation:
+ * 
+ * Internally measured Module temperature are represented
+ * as a 16-bit signed twos complement value in increments of
+ * 1/256 degrees Celsius, yielding a total range of –128C to +128C
+ * that is considered valid between –40 and +125C.
+ *
+ */
 static void
-get_sfp_temp(struct i2c_info *ii, char *buf, size_t size)
+convert_sff_temp(char *buf, size_t size, uint8_t *xbuf)
 {
-       char xbuf[2];
+       double d;
 
-       int8_t major;
-       uint8_t minor;
-       int k;
+       d = (double)xbuf[0];
+       d += (double)xbuf[1] / 256;
 
-       memset(xbuf, 0, sizeof(xbuf));
-       ii->f(ii, SFP_ADDR_DDM, SFP_DDM_TEMP, 2, xbuf);
+       snprintf(buf, size, "%.2f C", d);
+}
 
-       /* Convert temperature to string according to table 3.13 */
-       major = (int8_t)xbuf[0];
-       minor = (uint8_t)buf[1];
-       k = minor * 1000 / 256;
+/*
+ * Retrieves supplied voltage (SFF-8472, SFF-8436).
+ * 16-bit usigned value, treated as range 0..+6.55 Volts
+ */
+static void
+convert_sff_voltage(char *buf, size_t size, uint8_t *xbuf)
+{
+       double d;
 
-       snprintf(buf, size, "%d.%d C", major, k / 100);
+       d = (double)((xbuf[0] << 8) | xbuf[1]);
+       snprintf(buf, size, "%.2f Volts", d / 10000);
 }
 
+/*
+ * Converts value in @xbuf to both milliwats and dBm
+ * human representation.
+ */
 static void
-convert_power(struct i2c_info *ii, char *xbuf, char *buf, size_t size)
+convert_sff_power(struct i2c_info *ii, char *buf, size_t size, uint8_t *xbuf)
 {
        uint16_t mW;
        double dbm;
 
-       mW = ((uint8_t)xbuf[0] << 8) + (uint8_t)xbuf[1];
+       mW = (xbuf[0] << 8) + xbuf[1];
 
        /* Convert mw to dbm */
        dbm = 10.0 * log10(1.0 * mW / 10000);
 
+       /*
+        * Assume internally-calibrated data.
+        * This is always true for SFF-8346, and explicitly
+        * checked for SFF-8472.
+        */
+
        /* Table 3.9, bit 5 is set, internally calibrated */
-       if ((ii->diag_type & 0x20) != 0) {
-               snprintf(buf, size, "%d.%02d mW (%.2f dBm)",
-                   mW / 10000, (mW % 10000) / 100, dbm);
-       }
+       snprintf(buf, size, "%d.%02d mW (%.2f dBm)",
+           mW / 10000, (mW % 10000) / 100, dbm);
+}
+
+static void
+get_sfp_temp(struct i2c_info *ii, char *buf, size_t size)
+{
+       uint8_t xbuf[2];
+
+       memset(xbuf, 0, sizeof(xbuf));
+       read_i2c(ii, SFF_8472_DIAG, SFF_8472_TEMP, 2, xbuf);
+       convert_sff_temp(buf, size, xbuf);
+}
+
+static void
+get_sfp_voltage(struct i2c_info *ii, char *buf, size_t size)
+{
+       uint8_t xbuf[2];
+
+       memset(xbuf, 0, sizeof(xbuf));
+       read_i2c(ii, SFF_8472_DIAG, SFF_8472_VCC, 2, xbuf);
+       convert_sff_voltage(buf, size, xbuf);
+}
+
+static void
+get_qsfp_temp(struct i2c_info *ii, char *buf, size_t size)
+{
+       uint8_t xbuf[2];
+
+       memset(xbuf, 0, sizeof(xbuf));
+       read_i2c(ii, SFF_8436_BASE, SFF_8436_TEMP, 2, xbuf);
+       convert_sff_temp(buf, size, xbuf);
+}
+
+static void
+get_qsfp_voltage(struct i2c_info *ii, char *buf, size_t size)
+{
+       uint8_t xbuf[2];
+
+       memset(xbuf, 0, sizeof(xbuf));
+       read_i2c(ii, SFF_8436_BASE, SFF_8436_VCC, 2, xbuf);
+       convert_sff_voltage(buf, size, xbuf);
 }
 
 static void
 get_sfp_rx_power(struct i2c_info *ii, char *buf, size_t size)
 {
-       char xbuf[2];
+       uint8_t xbuf[2];
 
        memset(xbuf, 0, sizeof(xbuf));
-       ii->f(ii, SFP_ADDR_DDM, SFP_DDM_RXPOWER, 2, xbuf);
-       convert_power(ii, xbuf, buf, size);
+       read_i2c(ii, SFF_8472_DIAG, SFF_8472_RX_POWER, 2, xbuf);
+       convert_sff_power(ii, buf, size, xbuf);
 }
 
 static void
 get_sfp_tx_power(struct i2c_info *ii, char *buf, size_t size)
 {
-       char xbuf[2];
+       uint8_t xbuf[2];
 
        memset(xbuf, 0, sizeof(xbuf));
-       ii->f(ii, SFP_ADDR_DDM, SFP_DDM_TXPOWER, 2, xbuf);
-       convert_power(ii, xbuf, buf, size);
+       read_i2c(ii, SFF_8472_DIAG, SFF_8472_TX_POWER, 2, xbuf);
+       convert_sff_power(ii, buf, size, xbuf);
 }
 
-/* Intel ixgbe-specific structures and handlers */
-struct ixgbe_i2c_req {
-       uint8_t dev_addr;
-       uint8_t offset;
-       uint8_t len;
-       uint8_t data[8];
-};
-#define        SIOCGI2C        SIOCGIFGENERIC
+static void
+get_qsfp_rx_power(struct i2c_info *ii, char *buf, size_t size, int chan)
+{
+       uint8_t xbuf[2];
+
+       memset(xbuf, 0, sizeof(xbuf));
+       read_i2c(ii, SFF_8436_BASE, SFF_8436_RX_CH1_MSB + (chan-1)*2, 2, xbuf);
+       convert_sff_power(ii, buf, size, xbuf);
+}
+
+static void
+get_qsfp_tx_power(struct i2c_info *ii, char *buf, size_t size, int chan)
+{
+       uint8_t xbuf[2];
 
+       memset(xbuf, 0, sizeof(xbuf));
+       read_i2c(ii, SFF_8436_BASE, SFF_8436_TX_CH1_MSB + (chan-1)*2, 2, xbuf);
+       convert_sff_power(ii, buf, size, xbuf);
+}
+
+static void
+get_qsfp_rev_compliance(struct i2c_info *ii, char *buf, size_t size)
+{
+       uint8_t xbuf;
+
+       xbuf = 0;
+       read_i2c(ii, SFF_8436_BASE, SFF_8436_STATUS, 1, &xbuf);
+       convert_sff_rev_compliance(buf, size, xbuf);
+}
+
+static uint32_t 
+get_qsfp_br(struct i2c_info *ii)
+{
+       uint8_t xbuf;
+       uint32_t rate;
+
+       xbuf = 0;
+       read_i2c(ii, SFF_8436_BASE, SFF_8436_BITRATE, 1, &xbuf);
+       rate = xbuf * 100;
+       if (xbuf == 0xFF) {
+               read_i2c(ii, SFF_8436_BASE, SFF_8636_BITRATE, 1, &xbuf);
+               rate = xbuf * 250;
+       }
+
+       return (rate);
+}
+
+/*
+ * Reads i2c data from opened kernel socket.
+ */
 static int
-read_i2c_ixgbe(struct i2c_info *ii, uint8_t addr, uint8_t off, uint8_t len,
-    caddr_t buf)
+read_i2c(struct i2c_info *ii, uint8_t addr, uint8_t off, uint8_t len,
+    uint8_t *buf)
 {
-       struct ixgbe_i2c_req ixreq;
-       int i;
+       struct ifi2creq req;
+       int i, l;
 
        if (ii->error != 0)
                return (ii->error);
 
-       ii->ifr->ifr_data = (caddr_t)&ixreq;
-
-       memset(&ixreq, 0, sizeof(ixreq));
-       ixreq.dev_addr = addr;
-
-       for (i = 0; i < len; i += 1) {
-               ixreq.offset = off + i;
-               ixreq.len = 1;
+       ii->ifr->ifr_data = (caddr_t)&req;
 
-               if (ioctl(ii->s, SIOCGI2C, ii->ifr) != 0) {
+       i = 0;
+       l = 0;
+       memset(&req, 0, sizeof(req));
+       req.dev_addr = addr;
+       req.offset = off;
+       req.len = len;
+
+       while (len > 0) {
+               l = (len > sizeof(req.data)) ? sizeof(req.data) : len;
+               req.len = l;
+               if (ioctl(ii->fd, SIOCGI2C, ii->ifr) != 0) {
                        ii->error = errno;
                        return (errno);
                }
-               memcpy(&buf[i], ixreq.data, 1);
+
+               memcpy(&buf[i], req.data, l);
+               len -= l;
+               i += l;
+               req.offset += l;
        }
 
        return (0);
 }
 
-void
-sfp_status(int s, struct ifreq *ifr, int verbose)
+static void
+dump_i2c_data(struct i2c_info *ii, uint8_t addr, uint8_t off, uint8_t len)
+{
+       unsigned char buf[16];
+       int i, read;
+
+       while (len > 0) {
+               memset(buf, 0, sizeof(buf));
+               read = (len > sizeof(buf)) ? sizeof(buf) : len;
+               read_i2c(ii, addr, off, read, buf);
+               if (ii->error != 0) {
+                       fprintf(stderr, "Error reading i2c info\n");
+                       return;
+               }
+
+               printf("\t");
+               for (i = 0; i < read; i++)
+                       printf("%02X ", buf[i]);
+               printf("\n");
+               len -= read;
+               off += read;
+       }
+}
+
+static void
+print_qsfp_status(struct i2c_info *ii, int verbose)
 {
-       struct i2c_info ii;
        char buf[80], buf2[40], buf3[40];
+       uint8_t diag_type;
+       uint32_t bitrate;
+       int i;
+
+       /* Read diagnostic monitoring type */
+       read_i2c(ii, SFF_8436_BASE, SFF_8436_DIAG_TYPE, 1, (caddr_t)&diag_type);
+       if (ii->error != 0)
+               return;
 
        /*
-        * Check if we have i2c support for particular driver.
-        * TODO: Determine driver by original name.
+        * Read monitoring data it is supplied.
+        * XXX: It is not exactly clear from standard
+        * how one can specify lack of measurements (passive cables case).
         */
-       memset(&ii, 0, sizeof(ii));
-       if (strncmp(ifr->ifr_name, "ix", 2) == 0) {
-               ii.f = read_i2c_ixgbe;
-       } else
-               return;
+       if (diag_type != 0)
+               ii->do_diag = 1;
+       ii->qsfp = 1;
 
-       /* Prepare necessary into to pass to NIC handler */
-       ii.s = s;
-       ii.ifr = ifr;
+       /* Transceiver type */
+       get_qsfp_identifier(ii, buf, sizeof(buf));
+       get_qsfp_transceiver_class(ii, buf2, sizeof(buf2));
+       get_qsfp_connector(ii, buf3, sizeof(buf3));
+       if (ii->error == 0)
+               printf("\tplugged: %s %s (%s)\n", buf, buf2, buf3);
+       print_sfp_vendor(ii, buf, sizeof(buf));
+       if (ii->error == 0)
+               printf("\t%s\n", buf);
+
+       if (verbose > 1) {
+               get_qsfp_rev_compliance(ii, buf, sizeof(buf));
+               if (ii->error == 0)
+                       printf("\tcompliance level: %s\n", buf);
+
+               bitrate = get_qsfp_br(ii);
+               if (ii->error == 0 && bitrate > 0)
+                       printf("\tnominal bitrate: %u Mbps\n", bitrate);
+       }
+
+       /* Request current measurements if they are provided: */
+       if (ii->do_diag != 0) {
+               get_qsfp_temp(ii, buf, sizeof(buf));
+               get_qsfp_voltage(ii, buf2, sizeof(buf2));
+               printf("\tmodule temperature: %s voltage: %s\n", buf, buf2);
+               for (i = 1; i <= 4; i++) {
+                       get_qsfp_rx_power(ii, buf, sizeof(buf), i);
+                       get_qsfp_tx_power(ii, buf2, sizeof(buf2), i);
+                       printf("\tlane %d: RX: %s TX: %s\n", i, buf, buf2);
+               }
+       }
+
+       if (verbose > 2) {
+               printf("\n\tSFF8436 DUMP (0xA0 128..255 range):\n");
+               dump_i2c_data(ii, SFF_8436_BASE, 128, 128);
+               printf("\n\tSFF8436 DUMP (0xA0 0..81 range):\n");
+               dump_i2c_data(ii, SFF_8436_BASE, 0, 82);
+       }
+}
+
+static void
+print_sfp_status(struct i2c_info *ii, int verbose)
+{
+       char buf[80], buf2[40], buf3[40];
+       uint8_t diag_type, flags;
 
        /* Read diagnostic monitoring type */
-       ii.f(&ii, SFP_ADDR_MSA, SFP_MSA_DMONTYPE, 1, (caddr_t)&ii.diag_type);
+       read_i2c(ii, SFF_8472_BASE, SFF_8472_DIAG_TYPE, 1, (caddr_t)&diag_type);
+       if (ii->error != 0)
+               return;
+
+       /*
+        * Read monitoring data IFF it is supplied AND is
+        * internally calibrated
+        */
+       flags = SFF_8472_DDM_DONE | SFF_8472_DDM_INTERNAL;
+       if ((diag_type & flags) == flags)
+               ii->do_diag = 1;
 
        /* Transceiver type */
-       get_sfp_identifier(&ii, buf, sizeof(buf));
-       get_sfp_transceiver_class(&ii, buf2, sizeof(buf2));
-       get_sfp_connector(&ii, buf3, sizeof(buf3));
-       if (ii.error == 0)
-               printf("\ti2c: %s %s (%s)\n", buf, buf2, buf3);
-       if (verbose > 2)
-               printf_sfp_transceiver_descr(&ii, buf, sizeof(buf));
-       print_sfp_vendor(&ii, buf, sizeof(buf));
-       if (ii.error == 0)
+       get_sfp_identifier(ii, buf, sizeof(buf));
+       get_sfp_transceiver_class(ii, buf2, sizeof(buf2));
+       get_sfp_connector(ii, buf3, sizeof(buf3));
+       if (ii->error == 0)
+               printf("\tplugged: %s %s (%s)\n", buf, buf2, buf3);
+       print_sfp_vendor(ii, buf, sizeof(buf));
+       if (ii->error == 0)
                printf("\t%s\n", buf);
+
+       if (verbose > 5)
+               printf_sfp_transceiver_descr(ii, buf, sizeof(buf));
        /*
-        * Request current measurements iff they are implemented:
-        * Bit 6 must be set.
+        * Request current measurements iff they are provided:
         */
-       if ((ii.diag_type & 0x40) != 0) {
-               get_sfp_temp(&ii, buf, sizeof(buf));
-               get_sfp_rx_power(&ii, buf2, sizeof(buf2));
-               get_sfp_tx_power(&ii, buf3, sizeof(buf3));
-               printf("\tTemp: %s RX: %s TX: %s\n", buf, buf2, buf3);
+       if (ii->do_diag != 0) {
+               get_sfp_temp(ii, buf, sizeof(buf));
+               get_sfp_voltage(ii, buf2, sizeof(buf2));
+               printf("\tmodule temperature: %s Voltage: %s\n", buf, buf2);
+               get_sfp_rx_power(ii, buf, sizeof(buf));
+               get_sfp_tx_power(ii, buf2, sizeof(buf2));
+               printf("\tRX: %s TX: %s\n", buf, buf2);
        }
 
-       close(s);
+       if (verbose > 2) {
+               printf("\n\tSFF8472 DUMP (0xA0 0..127 range):\n");
+               dump_i2c_data(ii, SFF_8472_BASE, 0, 128);
+       }
+}
+
+void
+sfp_status(int s, struct ifreq *ifr, int verbose)
+{
+       struct i2c_info ii;
+       uint8_t id_byte;
+
+       /* Prepare necessary into pass to i2c reader */
+       memset(&ii, 0, sizeof(ii));
+       ii.fd = s;
+       ii.ifr = ifr;
+
+       /*
+        * Try to read byte 0 from i2c:
+        * Both SFF-8472 and SFF-8436 use it as
+        * 'identification byte'.
+        * Stop reading status on zero as value - 
+        * this might happen in case of empty transceiver slot.
+        */
+       id_byte = 0;
+       read_i2c(&ii, SFF_8472_BASE, SFF_8472_ID, 1, (caddr_t)&id_byte);
+       if (ii.error != 0 || id_byte == 0)
+               return;
+
+       switch (id_byte) {
+       case SFF_8024_ID_QSFP:

*** DIFF OUTPUT TRUNCATED AT 1000 LINES ***
_______________________________________________
svn-src-all@freebsd.org mailing list
https://lists.freebsd.org/mailman/listinfo/svn-src-all
To unsubscribe, send any mail to "svn-src-all-unsubscr...@freebsd.org"

Reply via email to