1 /*
2  * This file and its contents are supplied under the terms of the
3  * Common Development and Distribution License ("CDDL"), version 1.0.
4  * You may only use this file in accordance with the terms of version
5  * 1.0 of the CDDL.
6  *
7  * A full copy of the text of the CDDL should have accompanied this
8  * source.  A copy of the CDDL is also available via the Internet at
9  * http://www.illumos.org/license/CDDL.
10  */
11 
12 /*
13  * Copyright (c) 2017, Joyent, Inc.
14  */
15 
16 /*
17  * Print and tests SFF options values.
18  */
19 
20 #include <stdio.h>
21 #include <errno.h>
22 #include <strings.h>
23 #include <err.h>
24 #include <libsff.h>
25 
26 /*
27  * Pick up private sff header file with offsets from lib/libsff.
28  */
29 #include "sff.h"
30 
31 static void
lso_print_array(nvlist_t * nvl,const char * key)32 lso_print_array(nvlist_t *nvl, const char *key)
33 {
34 	int ret;
35 	uint_t i, count;
36 	char **vals;
37 
38 	if ((ret = nvlist_lookup_string_array(nvl, key, &vals, &count)) != 0) {
39 		errx(1, "TEST FAILED failed to find key %s: %s\n", key,
40 		    strerror(ret));
41 	}
42 
43 	(void) puts(key);
44 	for (i = 0; i < count; i++) {
45 		(void) printf("\t%d\t%s\n", i, vals[i]);
46 	}
47 }
48 
49 int
main(void)50 main(void)
51 {
52 	int ret;
53 	uint8_t buf[256];
54 	nvlist_t *nvl;
55 
56 	/*
57 	 * Set every shared bit for options then print them all out. Note we
58 	 * include reserved bits so that way if someone ends up adding something
59 	 * to one of the reserved fields, we end up printing it.
60 	 */
61 	bzero(buf, sizeof (buf));
62 	buf[SFF_8472_OPTIONS_HI] = 0xff;
63 	buf[SFF_8472_OPTIONS_LOW] = 0xff;
64 	buf[SFF_8472_ENHANCED_OPTIONS] = 0xff;
65 
66 	if ((ret = libsff_parse(buf, sizeof (buf), 0xa0, &nvl)) != 0) {
67 		errx(1, "TEST FAILED: failed to parse SFP options "
68 		    "values: %s\n", strerror(ret));
69 	}
70 
71 	lso_print_array(nvl, LIBSFF_KEY_OPTIONS);
72 	lso_print_array(nvl, LIBSFF_KEY_EXTENDED_OPTIONS);
73 
74 	nvlist_free(nvl);
75 
76 	/*
77 	 * Now for QSFP+
78 	 */
79 	(void) puts("\n\nQSFP\n");
80 	bzero(buf, sizeof (buf));
81 	buf[SFF_8472_IDENTIFIER] = SFF_8024_ID_QSFP;
82 	buf[SFF_8636_OPTIONS_HI] = 0xff;
83 	buf[SFF_8636_OPTIONS_MID] = 0xff;
84 	buf[SFF_8636_OPTIONS_LOW] = 0xff;
85 	buf[SFF_8636_ENHANCED_OPTIONS] = 0xff;
86 
87 	if ((ret = libsff_parse(buf, sizeof (buf), 0xa0, &nvl)) != 0) {
88 		errx(1, "TEST FAILED: failed to parse QSFP options "
89 		    "values: %s\n", strerror(ret));
90 	}
91 
92 	lso_print_array(nvl, LIBSFF_KEY_OPTIONS);
93 	lso_print_array(nvl, LIBSFF_KEY_ENHANCED_OPTIONS);
94 
95 	nvlist_free(nvl);
96 
97 	return (0);
98 }
99