2 * Copyright (c) 2016 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
16 #include <vnet/devices/nic/sfp.h>
18 static u8 * format_space_terminated (u8 * s, va_list * args)
20 u32 l = va_arg (*args, u32);
21 u8 * v = va_arg (*args, u8 *);
24 for (p = v + l - 1; p >= v && p[0] == ' '; p--)
26 vec_add (s, v, clib_min (p - v + 1, l));
30 static u8 * format_sfp_id (u8 * s, va_list * args)
32 u32 id = va_arg (*args, u32);
36 #define _(f) case SFP_ID_##f: t = #f; break;
40 return format (s, "unknown 0x%x", id);
42 return format (s, "%s", t);
45 static u8 * format_sfp_compatibility (u8 * s, va_list * args)
47 u32 c = va_arg (*args, u32);
51 #define _(a,b,f) case SFP_COMPATIBILITY_##f: t = #f; break;
52 foreach_sfp_compatibility
55 return format (s, "unknown 0x%x", c);
57 return format (s, "%s", t);
60 u32 sfp_is_comatible (sfp_eeprom_t * e, sfp_compatibility_t c)
62 static struct { u8 byte, bit; } t[] = {
63 #define _(a,b,f) { .byte = a, .bit = b, },
64 foreach_sfp_compatibility
68 ASSERT (c < ARRAY_LEN (t));
69 return (e->compatibility[t[c].byte] & (1 << t[c].bit)) != 0;
72 u8 * format_sfp_eeprom (u8 * s, va_list * args)
74 sfp_eeprom_t * e = va_arg (*args, sfp_eeprom_t *);
75 uword indent = format_get_indent (s);
78 if (e->id != SFP_ID_sfp)
79 s = format (s, "id %U, ", format_sfp_id, e->id);
81 s = format (s, "compatibility:");
82 for (i = 0; i < SFP_N_COMPATIBILITY; i++)
83 if (sfp_is_comatible (e, i))
84 s = format (s, " %U", format_sfp_compatibility, i);
86 s = format (s, "\n%Uvendor: %U, part %U",
87 format_white_space, indent,
88 format_space_terminated, sizeof (e->vendor_name), e->vendor_name,
89 format_space_terminated, sizeof (e->vendor_part_number), e->vendor_part_number);
90 s = format (s, "\n%Urevision: %U, serial: %U, date code: %U",
91 format_white_space, indent,
92 format_space_terminated, sizeof (e->vendor_revision), e->vendor_revision,
93 format_space_terminated, sizeof (e->vendor_serial_number), e->vendor_serial_number,
94 format_space_terminated, sizeof (e->vendor_date_code), e->vendor_date_code);