summaryrefslogtreecommitdiffstats
path: root/sbin/ifconfig/sfp.c
blob: 2e8039d8d363d1df40190bb830e7eb88311ba8a6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
/*-
 * Copyright (c) 2014 Alexander V. Chernikov. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#ifndef lint
static const char rcsid[] =
  "$FreeBSD$";
#endif /* not lint */

#include <sys/types.h>
#include <sys/param.h>
#include <sys/ioctl.h>
#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>
#include <unistd.h>

#include "ifconfig.h"

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;
	int bshift;
	int qsfp;
	int do_diag;
	struct ifreq *ifr;
	read_i2c *f;
	char *textbuf;
	size_t bufsize;
	int cfd;
	int port_id;
	int chip_id;
};

struct _nv {
	int v;
	const char *n;
};

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.4: Connector values */
static struct _nv conn[] = {
	{ 0x00, "Unknown" },
	{ 0x01, "SC" },
	{ 0x02, "Fibre Channel Style 1 copper" },
	{ 0x03, "Fibre Channel Style 2 copper" },
	{ 0x04, "BNC/TNC" },
	{ 0x05, "Fibre Channel coaxial" },
	{ 0x06, "FiberJack" },
	{ 0x07, "LC" },
	{ 0x08, "MT-RJ" },
	{ 0x09, "MU" },
	{ 0x0A, "SG" },
	{ 0x0B, "Optical pigtail" },
	{ 0x0C, "MPO Parallel Optic" },
	{ 0x20, "HSSDC II" },
	{ 0x21, "Copper pigtail" },
	{ 0x22, "RJ45" },
	{ 0x23, "No separate connector" }, /* SFF-8436 */
	{ 0, NULL }
};

/* SFF-8472 Rev. 11.4 table 3.5: Transceiver codes */
/* 10G Ethernet/IB compliance codes, byte 3 */
static struct _nv eth_10g[] = {
	{ 0x80, "10G Base-ER" },
	{ 0x40, "10G Base-LRM" },
	{ 0x20, "10G Base-LR" },
	{ 0x10, "10G Base-SR" },
	{ 0x08, "1X SX" },
	{ 0x04, "1X LX" },
	{ 0x02, "1X Copper Active" },
	{ 0x01, "1X Copper Passive" },
	{ 0, NULL }
};

/* Ethernet compliance codes, byte 6 */
static struct _nv eth_compat[] = {
	{ 0x80, "BASE-PX" },
	{ 0x40, "BASE-BX10" },
	{ 0x20, "100BASE-FX" },
	{ 0x10, "100BASE-LX/LX10" },
	{ 0x08, "1000BASE-T" },
	{ 0x04, "1000BASE-CX" },
	{ 0x02, "1000BASE-LX" },
	{ 0x01, "1000BASE-SX" },
	{ 0, NULL }
};

/* FC link length, byte 7 */
static struct _nv fc_len[] = {
	{ 0x80, "very long distance" },
	{ 0x40, "short distance" },
	{ 0x20, "intermediate distance" },
	{ 0x10, "long distance" },
	{ 0x08, "medium distance" },
	{ 0, NULL }
};

/* Channel/Cable technology, byte 7-8 */
static struct _nv cab_tech[] = {
	{ 0x0400, "Shortwave laser (SA)" },
	{ 0x0200, "Longwave laser (LC)" },
	{ 0x0100, "Electrical inter-enclosure (EL)" },
	{ 0x80, "Electrical intra-enclosure (EL)" },
	{ 0x40, "Shortwave laser (SN)" },
	{ 0x20, "Shortwave laser (SL)" },
	{ 0x10, "Longwave laser (LL)" },
	{ 0x08, "Active Cable" },
	{ 0x04, "Passive Cable" },
	{ 0, NULL }
};

/* FC Transmission media, byte 9 */
static struct _nv fc_media[] = {
	{ 0x80, "Twin Axial Pair" },
	{ 0x40, "Twisted Pair" },
	{ 0x20, "Miniature Coax" },
	{ 0x10, "Viao Coax" },
	{ 0x08, "Miltimode, 62.5um" },
	{ 0x04, "Multimode, 50um" },
	{ 0x02, "" },
	{ 0x01, "Single Mode" },
	{ 0, NULL }
};

/* FC Speed, byte 10 */
static struct _nv fc_speed[] = {
	{ 0x80, "1200 MBytes/sec" },
	{ 0x40, "800 MBytes/sec" },
	{ 0x20, "1600 MBytes/sec" },
	{ 0x10, "400 MBytes/sec" },
	{ 0x08, "3200 MBytes/sec" },
	{ 0x04, "200 MBytes/sec" },
	{ 0x01, "100 MBytes/sec" },
	{ 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 }
};

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
get_sfp_identifier(struct i2c_info *ii, char *buf, size_t size)
{
	uint8_t data;

	ii->f(ii, SFF_8472_BASE, SFF_8472_ID, 1, (caddr_t)&data);
	convert_sff_identifier(buf, size, data);
}

static void
get_sfp_connector(struct i2c_info *ii, char *buf, size_t size)
{
	uint8_t data;

	ii->f(ii, SFF_8472_BASE, SFF_8472_CONNECTOR, 1, (caddr_t)&data);
	convert_sff_connector(buf, size, data);
}

static void
get_qsfp_identifier(struct i2c_info *ii, char *buf, size_t size)
{
	uint8_t data;

	ii->f(ii, SFF_8436_BASE, SFF_8436_ID, 1, (caddr_t)&data);
	convert_sff_identifier(buf, size, data);
}

static void
get_qsfp_connector(struct i2c_info *ii, char *buf, size_t size)
{
	uint8_t data;

	ii->f(ii, SFF_8436_BASE, SFF_8436_CONNECTOR, 1, (caddr_t)&data);
	convert_sff_connector(buf, size, data);
}

static void
printf_sfp_transceiver_descr(struct i2c_info *ii, char *buf, size_t size)
{
	char xbuf[12];
	const char *tech_class, *tech_len, *tech_tech, *tech_media, *tech_speed;

	tech_class = NULL;
	tech_len = NULL;
	tech_tech = NULL;
	tech_media = NULL;
	tech_speed = NULL;

	/* Read bytes 3-10 at once */
	ii->f(ii, SFF_8472_BASE, SFF_8472_TRANS_START, 8, &xbuf[3]);

	/* Check 10G ethernet first */
	tech_class = find_zero_bit(eth_10g, xbuf[3], 1);
	if (tech_class == NULL) {
		/* No match. Try 1G */
		tech_class = find_zero_bit(eth_compat, xbuf[6], 1);
	}

	tech_len = find_zero_bit(fc_len, xbuf[7], 1);
	tech_tech = find_zero_bit(cab_tech, xbuf[7] << 8 | xbuf[8], 2);
	tech_media = find_zero_bit(fc_media, xbuf[9], 1);
	tech_speed = find_zero_bit(fc_speed, xbuf[10], 1);

	printf("Class: %s\n", tech_class);
	printf("Length: %s\n", tech_len);
	printf("Tech: %s\n", tech_tech);
	printf("Media: %s\n", tech_media);
	printf("Speed: %s\n", tech_speed);
}

static void
get_sfp_transceiver_class(struct i2c_info *ii, char *buf, size_t size)
{
	const char *tech_class;
	uint8_t code;

	/* Check 10G Ethernet/IB first */
	ii->f(ii, SFF_8472_BASE, SFF_8472_TRANS_START, 1, (caddr_t)&code);
	tech_class = find_zero_bit(eth_10g, code, 1);
	if (tech_class == NULL) {
		/* No match. Try Ethernet 1G */
		ii->f(ii, SFF_8472_BASE, SFF_8472_TRANS_START + 3,
		    1, (caddr_t)&code);
		tech_class = find_zero_bit(eth_compat, code, 1);
	}

	if (tech_class == NULL)
		tech_class = "Unknown";

	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 */
	ii->f(ii, SFF_8436_BASE, SFF_8436_CODE_E1040G, 1, (caddr_t)&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
convert_sff_name(char *buf, size_t size, char *xbuf)
{
	char *p;

	for (p = &xbuf[16]; *(p - 1) == 0x20; p--)
		;
	*p = '\0';
	snprintf(buf, size, "%s", xbuf);
}

static void
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];

	memset(xbuf, 0, sizeof(xbuf));
	ii->f(ii, SFF_8472_BASE, SFF_8472_VENDOR_START, 16, xbuf);
	convert_sff_name(buf, size, xbuf);
}

static void
get_sfp_vendor_pn(struct i2c_info *ii, char *buf, size_t size)
{
	char xbuf[17];

	memset(xbuf, 0, sizeof(xbuf));
	ii->f(ii, SFF_8472_BASE, SFF_8472_PN_START, 16, 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];

	memset(xbuf, 0, sizeof(xbuf));
	ii->f(ii, SFF_8472_BASE, SFF_8472_SN_START, 16, xbuf);
	convert_sff_name(buf, size, xbuf);
}

static void
get_sfp_vendor_date(struct i2c_info *ii, char *buf, size_t size)
{
	char xbuf[6];

	memset(xbuf, 0, sizeof(xbuf));
	/* Date code, see Table 3.8 for description */
	ii->f(ii, SFF_8472_BASE, SFF_8472_DATE_START, 6, 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));
	ii->f(ii, SFF_8436_BASE, SFF_8436_VENDOR_START, 16, 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));
	ii->f(ii, SFF_8436_BASE, SFF_8436_PN_START, 16, 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));
	ii->f(ii, SFF_8436_BASE, SFF_8436_SN_START, 16, 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));
	ii->f(ii, SFF_8436_BASE, SFF_8436_DATE_START, 6, xbuf);
	convert_sff_date(buf, size, xbuf);
}

static void
print_sfp_vendor(struct i2c_info *ii, char *buf, size_t size)
{
	char xbuf[80];

	memset(xbuf, 0, sizeof(xbuf));
	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
convert_sff_temp(char *buf, size_t size, char *xbuf)
{
	double d;

	d = (double)(int8_t)xbuf[0];
	d += (double)(uint8_t)xbuf[1] / 256;

	snprintf(buf, size, "%.2f C", d);
}

/*
 * 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, char *xbuf)
{
	double d;

	d = (double)(((uint8_t)xbuf[0] << 8) | (uint8_t)xbuf[1]);
	snprintf(buf, size, "%.2f Volts", d / 10000);
}

/*
 * Converts value in @xbuf to both milliwats and dBm
 * human representation.
 */
static void
convert_sff_power(struct i2c_info *ii, char *buf, size_t size, char *xbuf)
{
	uint16_t mW;
	double dbm;

	mW = ((uint8_t)xbuf[0] << 8) + (uint8_t)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 */
	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)
{
	char xbuf[2];

	memset(xbuf, 0, sizeof(xbuf));
	ii->f(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)
{
	char xbuf[2];

	memset(xbuf, 0, sizeof(xbuf));
	ii->f(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)
{
	char xbuf[2];

	memset(xbuf, 0, sizeof(xbuf));
	ii->f(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)
{
	char xbuf[2];

	memset(xbuf, 0, sizeof(xbuf));
	ii->f(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];

	memset(xbuf, 0, sizeof(xbuf));
	ii->f(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];

	memset(xbuf, 0, sizeof(xbuf));
	ii->f(ii, SFF_8472_DIAG, SFF_8472_TX_POWER, 2, xbuf);
	convert_sff_power(ii, buf, size, xbuf);
}

static void
get_qsfp_rx_power(struct i2c_info *ii, char *buf, size_t size, int chan)
{
	char xbuf[2];

	memset(xbuf, 0, sizeof(xbuf));
	ii->f(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)
{
	char xbuf[2];

	memset(xbuf, 0, sizeof(xbuf));
	ii->f(ii, SFF_8436_BASE, SFF_8436_TX_CH1_MSB + (chan -1) * 2, 2, xbuf);
	convert_sff_power(ii, buf, size, xbuf);
}

/* Generic handler */
static int
read_i2c_generic(struct i2c_info *ii, uint8_t addr, uint8_t off, uint8_t len,
    caddr_t buf)
{
	struct ifi2creq req;
	int i, l;

	if (ii->error != 0)
		return (ii->error);

	ii->ifr->ifr_data = (caddr_t)&req;

	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->s, SIOCGI2C, ii->ifr) != 0) {
			ii->error = errno;
			return (errno);
		}

		memcpy(&buf[i], req.data, l);
		len -= l;
		i += l;
		req.offset += l;
	}

	return (0);
}

static void
print_qsfp_status(struct i2c_info *ii, int verbose)
{
	char buf[80], buf2[40], buf3[40];
	uint8_t diag_type;
	int i;

	/* Read diagnostic monitoring type */
	ii->f(ii, SFF_8436_BASE, SFF_8436_DIAG_TYPE, 1, (caddr_t)&diag_type);
	if (ii->error != 0)
		return;

	/*
	 * Read monitoring data it is supplied.
	 * XXX: It is not exactly clear from standard
	 * how one can specify lack of measurements (passive cables case).
	 */
	if (diag_type != 0)
		ii->do_diag = 1;
	ii->qsfp = 1;

	/* 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);

	/* 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);
		}
	}
}

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, 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("\tplugged: %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)
		printf("\t%s\n", buf);

	/*
	 * Request current measurements iff they are provided:
	 */
	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);
	}
}

void
sfp_status(int s, struct ifreq *ifr, int verbose)
{
	struct i2c_info ii;
	uint8_t id_byte;

	memset(&ii, 0, sizeof(ii));
	/* Prepare necessary into to pass to NIC handler */
	ii.s = s;
	ii.ifr = ifr;
	ii.f = read_i2c_generic;

	/*
	 * Try to read byte 0 from i2c:
	 * Both SFF-8472 and SFF-8436 use it as
	 * 'identification byte'
	 */
	id_byte = 0;
	ii.f(&ii, SFF_8472_BASE, SFF_8472_ID, 1, (caddr_t)&id_byte);
	if (ii.error != 0)
		return;

	switch (id_byte) {
	case SFF_8024_ID_QSFP:
	case SFF_8024_ID_QSFPPLUS:
		print_qsfp_status(&ii, verbose);
		break;
	default:
		print_sfp_status(&ii, verbose);
	};
}

OpenPOWER on IntegriCloud