fd-io-styleify pass
[vpp.git] / vlib / vlib / pci / pci.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  * pci.c: Linux user space PCI bus management.
17  *
18  * Copyright (c) 2008 Eliot Dresselhaus
19  *
20  * Permission is hereby granted, free of charge, to any person obtaining
21  * a copy of this software and associated documentation files (the
22  * "Software"), to deal in the Software without restriction, including
23  * without limitation the rights to use, copy, modify, merge, publish,
24  * distribute, sublicense, and/or sell copies of the Software, and to
25  * permit persons to whom the Software is furnished to do so, subject to
26  * the following conditions:
27  *
28  * The above copyright notice and this permission notice shall be
29  * included in all copies or substantial portions of the Software.
30  *
31  *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
32  *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
33  *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
34  *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
35  *  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
36  *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
37  *  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
38  */
39
40 #include <vlib/vlib.h>
41 #include <vlib/pci/pci.h>
42 #include <vlib/unix/unix.h>
43
44 #include <sys/types.h>
45 #include <sys/stat.h>
46 #include <fcntl.h>
47 #include <dirent.h>
48 #include <sys/ioctl.h>
49 #include <net/if.h>
50 #include <linux/ethtool.h>
51 #include <linux/sockios.h>
52
53 vlib_pci_main_t pci_main;
54
55 static clib_error_t *
56 show_pci_fn (vlib_main_t * vm,
57              unformat_input_t * input, vlib_cli_command_t * cmd)
58 {
59   vlib_pci_main_t *pm = &pci_main;
60   vlib_pci_device_t *d;
61   pci_config_header_t *c;
62   int show_all = 0;
63   u8 *s = 0;
64
65   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
66     {
67       if (unformat (input, "all"))
68         show_all = 1;
69       else
70         return clib_error_return (0, "unknown input `%U'",
71                                   format_unformat_error, input);
72     }
73
74   vlib_cli_output (vm, "%-13s%-7s%-12s%-15s%-20s%-40s",
75                    "Address", "Socket", "VID:PID", "Link Speed", "Driver",
76                    "Product Name");
77
78   /* *INDENT-OFF* */
79   pool_foreach (d, pm->pci_devs, ({
80     c = &d->config0.header;
81
82     if (c->device_class != PCI_CLASS_NETWORK_ETHERNET && !show_all)
83       continue;
84
85     vec_reset_length (s);
86
87     if (d->numa_node >= 0)
88       s = format (s, "  %d", d->numa_node);
89
90     vlib_cli_output (vm, "%-13U%-7v%04x:%04x   %-15U%-20s%-40v",
91                      format_vlib_pci_addr, &d->bus_address, s,
92                      c->vendor_id, c->device_id,
93                      format_vlib_pci_link_speed, d,
94                      d->driver_name ? (char *) d->driver_name : "",
95                      d->product_name);
96   }));
97 /* *INDENT-ON* */
98
99   vec_free (s);
100   return 0;
101 }
102
103 uword
104 unformat_vlib_pci_addr (unformat_input_t * input, va_list * args)
105 {
106   vlib_pci_addr_t *addr = va_arg (*args, vlib_pci_addr_t *);
107   u32 x[4];
108
109   if (!unformat (input, "%x:%x:%x.%x", &x[0], &x[1], &x[2], &x[3]))
110     return 0;
111
112   addr->domain = x[0];
113   addr->bus = x[1];
114   addr->slot = x[2];
115   addr->function = x[3];
116
117   return 1;
118 }
119
120 u8 *
121 format_vlib_pci_addr (u8 * s, va_list * va)
122 {
123   vlib_pci_addr_t *addr = va_arg (*va, vlib_pci_addr_t *);
124   return format (s, "%04x:%02x:%02x.%x", addr->domain, addr->bus,
125                  addr->slot, addr->function);
126 }
127
128 u8 *
129 format_vlib_pci_handle (u8 * s, va_list * va)
130 {
131   vlib_pci_addr_t *addr = va_arg (*va, vlib_pci_addr_t *);
132   return format (s, "%x/%x/%x", addr->bus, addr->slot, addr->function);
133 }
134
135 u8 *
136 format_vlib_pci_link_speed (u8 * s, va_list * va)
137 {
138   vlib_pci_device_t *d = va_arg (*va, vlib_pci_device_t *);
139   pcie_config_regs_t *r =
140     pci_config_find_capability (&d->config0, PCI_CAP_ID_PCIE);
141   int width;
142
143   if (!r)
144     return format (s, "unknown");
145
146   width = (r->link_status >> 4) & 0x3f;
147
148   if ((r->link_status & 0xf) == 1)
149     return format (s, "2.5 GT/s x%u", width);
150   if ((r->link_status & 0xf) == 2)
151     return format (s, "5.0 GT/s x%u", width);
152   if ((r->link_status & 0xf) == 3)
153     return format (s, "8.0 GT/s x%u", width);
154   return format (s, "unknown");
155 }
156
157
158 /* *INDENT-OFF* */
159 VLIB_CLI_COMMAND (show_pci_command, static) = {
160   .path = "show pci",
161   .short_help = "show pci [all]",
162   .function = show_pci_fn,
163 };
164 /* *INDENT-ON* */
165
166 clib_error_t *
167 pci_bus_init (vlib_main_t * vm)
168 {
169   return 0;
170 }
171
172 VLIB_INIT_FUNCTION (pci_bus_init);
173
174 /*
175  * fd.io coding-style-patch-verification: ON
176  *
177  * Local Variables:
178  * eval: (c-set-style "gnu")
179  * End:
180  */