blob: 624740e3d52145e7a0a887d5081cf8ca7906b8b3 [file] [log] [blame]
Damjan Marionb4d89272016-05-12 22:14:45 +02001/*
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
Damjan Marion374e2c52017-03-09 20:38:15 +010016#include <vnet/ethernet/sfp.h>
Damjan Marionb4d89272016-05-12 22:14:45 +020017
Damjan Marion00a9dca2016-08-17 17:05:46 +020018static u8 *
19format_space_terminated (u8 * s, va_list * args)
Damjan Marionb4d89272016-05-12 22:14:45 +020020{
21 u32 l = va_arg (*args, u32);
Damjan Marion00a9dca2016-08-17 17:05:46 +020022 u8 *v = va_arg (*args, u8 *);
23 u8 *p;
Damjan Marionb4d89272016-05-12 22:14:45 +020024
25 for (p = v + l - 1; p >= v && p[0] == ' '; p--)
26 ;
27 vec_add (s, v, clib_min (p - v + 1, l));
28 return s;
29}
30
Damjan Marion00a9dca2016-08-17 17:05:46 +020031static u8 *
32format_sfp_id (u8 * s, va_list * args)
Damjan Marionb4d89272016-05-12 22:14:45 +020033{
34 u32 id = va_arg (*args, u32);
Damjan Marion00a9dca2016-08-17 17:05:46 +020035 char *t = 0;
Damjan Marionb4d89272016-05-12 22:14:45 +020036 switch (id)
37 {
38#define _(f) case SFP_ID_##f: t = #f; break;
39 foreach_sfp_id
40#undef _
41 default:
42 return format (s, "unknown 0x%x", id);
43 }
44 return format (s, "%s", t);
45}
46
Damjan Marion00a9dca2016-08-17 17:05:46 +020047static u8 *
48format_sfp_compatibility (u8 * s, va_list * args)
Damjan Marionb4d89272016-05-12 22:14:45 +020049{
50 u32 c = va_arg (*args, u32);
Damjan Marion00a9dca2016-08-17 17:05:46 +020051 char *t = 0;
Damjan Marionb4d89272016-05-12 22:14:45 +020052 switch (c)
53 {
54#define _(a,b,f) case SFP_COMPATIBILITY_##f: t = #f; break;
55 foreach_sfp_compatibility
56#undef _
57 default:
58 return format (s, "unknown 0x%x", c);
59 }
60 return format (s, "%s", t);
61}
62
Damjan Marion00a9dca2016-08-17 17:05:46 +020063u32
64sfp_is_comatible (sfp_eeprom_t * e, sfp_compatibility_t c)
Damjan Marionb4d89272016-05-12 22:14:45 +020065{
Damjan Marion00a9dca2016-08-17 17:05:46 +020066 static struct
67 {
68 u8 byte, bit;
69 } t[] =
70 {
Damjan Marionb4d89272016-05-12 22:14:45 +020071#define _(a,b,f) { .byte = a, .bit = b, },
72 foreach_sfp_compatibility
73#undef _
74 };
75
76 ASSERT (c < ARRAY_LEN (t));
77 return (e->compatibility[t[c].byte] & (1 << t[c].bit)) != 0;
78}
79
Damjan Marion00a9dca2016-08-17 17:05:46 +020080u8 *
81format_sfp_eeprom (u8 * s, va_list * args)
Damjan Marionb4d89272016-05-12 22:14:45 +020082{
Damjan Marion00a9dca2016-08-17 17:05:46 +020083 sfp_eeprom_t *e = va_arg (*args, sfp_eeprom_t *);
Damjan Marionb4d89272016-05-12 22:14:45 +020084 uword indent = format_get_indent (s);
85 int i;
86
87 if (e->id != SFP_ID_sfp)
88 s = format (s, "id %U, ", format_sfp_id, e->id);
89
90 s = format (s, "compatibility:");
91 for (i = 0; i < SFP_N_COMPATIBILITY; i++)
92 if (sfp_is_comatible (e, i))
93 s = format (s, " %U", format_sfp_compatibility, i);
94
95 s = format (s, "\n%Uvendor: %U, part %U",
96 format_white_space, indent,
Damjan Marion00a9dca2016-08-17 17:05:46 +020097 format_space_terminated, sizeof (e->vendor_name),
98 e->vendor_name, format_space_terminated,
99 sizeof (e->vendor_part_number), e->vendor_part_number);
100 s =
101 format (s, "\n%Urevision: %U, serial: %U, date code: %U",
102 format_white_space, indent, format_space_terminated,
103 sizeof (e->vendor_revision), e->vendor_revision,
104 format_space_terminated, sizeof (e->vendor_serial_number),
105 e->vendor_serial_number, format_space_terminated,
106 sizeof (e->vendor_date_code), e->vendor_date_code);
Damjan Marionb4d89272016-05-12 22:14:45 +0200107
108 return s;
109}
Damjan Marion00a9dca2016-08-17 17:05:46 +0200110
111/*
112 * fd.io coding-style-patch-verification: ON
113 *
114 * Local Variables:
115 * eval: (c-set-style "gnu")
116 * End:
117 */