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