Fix native build on non x86_64 systems
[vpp.git] / vnet / vnet / devices / nic / sfp.c
1 /*
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:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
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.
14  */
15
16 #include <vnet/devices/nic/sfp.h>
17
18 static u8 * format_space_terminated (u8 * s, va_list * args)
19 {
20   u32 l = va_arg (*args, u32);
21   u8 * v = va_arg (*args, u8 *);
22   u8 * p;
23
24   for (p = v + l - 1; p >= v && p[0] == ' '; p--)
25     ;
26   vec_add (s, v, clib_min (p - v + 1, l));
27   return s;
28 }
29
30 static u8 * format_sfp_id (u8 * s, va_list * args)
31 {
32   u32 id = va_arg (*args, u32);
33   char * t = 0;
34   switch (id)
35     {
36 #define _(f) case SFP_ID_##f: t = #f; break;
37       foreach_sfp_id
38 #undef _
39     default:
40       return format (s, "unknown 0x%x", id);
41     }
42   return format (s, "%s", t);
43 }
44
45 static u8 * format_sfp_compatibility (u8 * s, va_list * args)
46 {
47   u32 c = va_arg (*args, u32);
48   char * t = 0;
49   switch (c)
50     {
51 #define _(a,b,f) case SFP_COMPATIBILITY_##f: t = #f; break;
52       foreach_sfp_compatibility
53 #undef _
54     default:
55       return format (s, "unknown 0x%x", c);
56     }
57   return format (s, "%s", t);
58 }
59
60 u32 sfp_is_comatible (sfp_eeprom_t * e, sfp_compatibility_t c)
61 {
62   static struct { u8 byte, bit; } t[] = {
63 #define _(a,b,f) { .byte = a, .bit = b, },
64     foreach_sfp_compatibility
65 #undef _
66   };
67
68   ASSERT (c < ARRAY_LEN (t));
69   return (e->compatibility[t[c].byte] & (1 << t[c].bit)) != 0;
70 }
71
72 u8 * format_sfp_eeprom (u8 * s, va_list * args)
73 {
74   sfp_eeprom_t * e = va_arg (*args, sfp_eeprom_t *);
75   uword indent = format_get_indent (s);
76   int i;
77
78   if (e->id != SFP_ID_sfp)
79     s = format (s, "id %U, ", format_sfp_id, e->id);
80
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);
85
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);
95
96   return s;
97 }