blob: e1bc4172e8baefb6c838edde47e04594a0fd43e9 [file] [log] [blame]
Damjan Mariona42cd342016-04-13 18:03:20 +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/*
16 * pci.h: PCI definitions.
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#ifndef included_vlib_pci_h
41#define included_vlib_pci_h
42
43#include <vlib/vlib.h>
44#include <vlib/pci/pci_config.h>
45
Damjan Marioncef87f12017-10-05 15:32:41 +020046/* *INDENT-OFF* */
Dave Barach9b8ffd92016-07-08 08:13:45 -040047typedef CLIB_PACKED (union
Dave Barach9b8ffd92016-07-08 08:13:45 -040048{
Damjan Marioncef87f12017-10-05 15:32:41 +020049 struct
50 {
51 u16 domain;
52 u8 bus;
53 u8 slot: 5;
54 u8 function:3;
55 };
56 u32 as_u32;
57}) vlib_pci_addr_t;
58/* *INDENT-ON* */
Damjan Mariona42cd342016-04-13 18:03:20 +020059
Damjan Marioncef87f12017-10-05 15:32:41 +020060typedef struct vlib_pci_device_info
61{
62 /* addr */
63 vlib_pci_addr_t addr;
Damjan Marion5a206ea2016-05-12 22:11:03 +020064
65 /* Numa Node */
66 int numa_node;
67
Damjan Marionb1b1a142016-08-23 00:53:22 +020068 /* Device data */
69 u16 device_class;
70 u16 vendor_id;
71 u16 device_id;
72
Damjan Marion5a206ea2016-05-12 22:11:03 +020073 /* Vital Product Data */
Dave Barach9b8ffd92016-07-08 08:13:45 -040074 u8 *product_name;
75 u8 *vpd_r;
76 u8 *vpd_w;
Damjan Marion5a206ea2016-05-12 22:11:03 +020077
Damjan Marioncef87f12017-10-05 15:32:41 +020078 /* Driver name */
79 u8 *driver_name;
Damjan Marion5a206ea2016-05-12 22:11:03 +020080
Damjan Marioncef87f12017-10-05 15:32:41 +020081 /* First 64 bytes of configuration space. */
82 union
83 {
84 pci_config_type0_regs_t config0;
85 pci_config_type1_regs_t config1;
86 u8 config_data[256];
87 };
88
89 /* IOMMU Group */
90 int iommu_group;
91
92} vlib_pci_device_info_t;
93
94typedef u32 vlib_pci_dev_handle_t;
95
96vlib_pci_device_info_t *vlib_pci_get_device_info (vlib_pci_addr_t * addr,
97 clib_error_t ** error);
98vlib_pci_addr_t *vlib_pci_get_all_dev_addrs ();
99vlib_pci_addr_t *vlib_pci_get_addr (vlib_pci_dev_handle_t h);
100uword vlib_pci_get_private_data (vlib_pci_dev_handle_t h);
101void vlib_pci_set_private_data (vlib_pci_dev_handle_t h, uword private_data);
102
103static inline void
104vlib_pci_free_device_info (vlib_pci_device_info_t * di)
105{
106 if (!di)
107 return;
108 vec_free (di->product_name);
109 vec_free (di->vpd_r);
110 vec_free (di->vpd_w);
111 vec_free (di->driver_name);
112 clib_mem_free (di);
113}
Damjan Mariona42cd342016-04-13 18:03:20 +0200114
Dave Barach9b8ffd92016-07-08 08:13:45 -0400115typedef struct
116{
Damjan Mariona42cd342016-04-13 18:03:20 +0200117 u16 vendor_id, device_id;
118} pci_device_id_t;
119
Dave Barach9b8ffd92016-07-08 08:13:45 -0400120typedef struct _pci_device_registration
121{
Damjan Mariona42cd342016-04-13 18:03:20 +0200122 /* Driver init function. */
Damjan Marioncef87f12017-10-05 15:32:41 +0200123 clib_error_t *(*init_function) (vlib_main_t * vm,
124 vlib_pci_dev_handle_t handle);
Damjan Mariona42cd342016-04-13 18:03:20 +0200125
Damjan Marion5a206ea2016-05-12 22:11:03 +0200126 /* Interrupt handler */
Damjan Marioncef87f12017-10-05 15:32:41 +0200127 void (*interrupt_handler) (vlib_pci_dev_handle_t handle);
Damjan Mariona42cd342016-04-13 18:03:20 +0200128
129 /* List of registrations */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400130 struct _pci_device_registration *next_registration;
Damjan Mariona42cd342016-04-13 18:03:20 +0200131
132 /* Vendor/device ids supported by this driver. */
133 pci_device_id_t supported_devices[];
134} pci_device_registration_t;
135
Damjan Marion5a206ea2016-05-12 22:11:03 +0200136/* Pool of PCI devices. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400137typedef struct
138{
139 vlib_main_t *vlib_main;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400140 pci_device_registration_t *pci_device_registrations;
Damjan Marion5a206ea2016-05-12 22:11:03 +0200141} vlib_pci_main_t;
142
143extern vlib_pci_main_t pci_main;
144
Damjan Mariona42cd342016-04-13 18:03:20 +0200145#define PCI_REGISTER_DEVICE(x,...) \
146 __VA_ARGS__ pci_device_registration_t x; \
147static void __vlib_add_pci_device_registration_##x (void) \
148 __attribute__((__constructor__)) ; \
149static void __vlib_add_pci_device_registration_##x (void) \
150{ \
Damjan Marion5a206ea2016-05-12 22:11:03 +0200151 vlib_pci_main_t * pm = &pci_main; \
152 x.next_registration = pm->pci_device_registrations; \
153 pm->pci_device_registrations = &x; \
Damjan Mariona42cd342016-04-13 18:03:20 +0200154} \
Damjan Marion5a206ea2016-05-12 22:11:03 +0200155__VA_ARGS__ pci_device_registration_t x
Damjan Mariona42cd342016-04-13 18:03:20 +0200156
Damjan Marioncef87f12017-10-05 15:32:41 +0200157clib_error_t *vlib_pci_bind_to_uio (vlib_pci_addr_t * addr,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400158 char *uio_driver_name);
Damjan Mariona42cd342016-04-13 18:03:20 +0200159
160/* Configuration space read/write. */
Damjan Marioncef87f12017-10-05 15:32:41 +0200161clib_error_t *vlib_pci_read_write_config (vlib_pci_dev_handle_t handle,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400162 vlib_read_or_write_t read_or_write,
163 uword address,
164 void *data, u32 n_bytes);
Damjan Mariona42cd342016-04-13 18:03:20 +0200165
166#define _(t) \
167static inline clib_error_t * \
Damjan Marioncef87f12017-10-05 15:32:41 +0200168vlib_pci_read_config_##t (vlib_pci_dev_handle_t h, \
Damjan Marion5a206ea2016-05-12 22:11:03 +0200169 uword address, t * data) \
Damjan Mariona42cd342016-04-13 18:03:20 +0200170{ \
Damjan Marioncef87f12017-10-05 15:32:41 +0200171 return vlib_pci_read_write_config (h, VLIB_READ,address, data, \
Damjan Marion5a206ea2016-05-12 22:11:03 +0200172 sizeof (data[0])); \
Damjan Mariona42cd342016-04-13 18:03:20 +0200173}
174
Dave Barach9b8ffd92016-07-08 08:13:45 -0400175_(u32);
176_(u16);
177_(u8);
Damjan Mariona42cd342016-04-13 18:03:20 +0200178
179#undef _
180
181#define _(t) \
182static inline clib_error_t * \
Damjan Marioncef87f12017-10-05 15:32:41 +0200183vlib_pci_write_config_##t (vlib_pci_dev_handle_t h, uword address, \
Damjan Marion5a206ea2016-05-12 22:11:03 +0200184 t * data) \
Damjan Mariona42cd342016-04-13 18:03:20 +0200185{ \
Damjan Marioncef87f12017-10-05 15:32:41 +0200186 return vlib_pci_read_write_config (h, VLIB_WRITE, \
Damjan Mariona42cd342016-04-13 18:03:20 +0200187 address, data, sizeof (data[0])); \
188}
189
Dave Barach9b8ffd92016-07-08 08:13:45 -0400190_(u32);
191_(u16);
192_(u8);
Damjan Mariona42cd342016-04-13 18:03:20 +0200193
194#undef _
195
Damjan Marion5a206ea2016-05-12 22:11:03 +0200196static inline clib_error_t *
Damjan Marioncef87f12017-10-05 15:32:41 +0200197vlib_pci_intr_enable (vlib_pci_dev_handle_t h)
Damjan Marion5a206ea2016-05-12 22:11:03 +0200198{
199 u16 command;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400200 clib_error_t *err;
Damjan Marion5a206ea2016-05-12 22:11:03 +0200201
Damjan Marioncef87f12017-10-05 15:32:41 +0200202 err = vlib_pci_read_config_u16 (h, 4, &command);
Damjan Marion5a206ea2016-05-12 22:11:03 +0200203
204 if (err)
205 return err;
206
207 command &= ~PCI_COMMAND_INTX_DISABLE;
208
Damjan Marioncef87f12017-10-05 15:32:41 +0200209 return vlib_pci_write_config_u16 (h, 4, &command);
Damjan Marion5a206ea2016-05-12 22:11:03 +0200210}
211
212static inline clib_error_t *
Damjan Marioncef87f12017-10-05 15:32:41 +0200213vlib_pci_intr_disable (vlib_pci_dev_handle_t h)
Damjan Marion5a206ea2016-05-12 22:11:03 +0200214{
215 u16 command;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400216 clib_error_t *err;
Damjan Marion5a206ea2016-05-12 22:11:03 +0200217
Damjan Marioncef87f12017-10-05 15:32:41 +0200218 err = vlib_pci_read_config_u16 (h, 4, &command);
Damjan Marion5a206ea2016-05-12 22:11:03 +0200219
220 if (err)
221 return err;
222
223 command |= PCI_COMMAND_INTX_DISABLE;
224
Damjan Marioncef87f12017-10-05 15:32:41 +0200225 return vlib_pci_write_config_u16 (h, 4, &command);
Damjan Marion5a206ea2016-05-12 22:11:03 +0200226}
227
228static inline clib_error_t *
Damjan Marioncef87f12017-10-05 15:32:41 +0200229vlib_pci_bus_master_enable (vlib_pci_dev_handle_t h)
Damjan Marion5a206ea2016-05-12 22:11:03 +0200230{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400231 clib_error_t *err;
Damjan Marion5a206ea2016-05-12 22:11:03 +0200232 u16 command;
233
234 /* Set bus master enable (BME) */
Damjan Marioncef87f12017-10-05 15:32:41 +0200235 err = vlib_pci_read_config_u16 (h, 4, &command);
Damjan Marion5a206ea2016-05-12 22:11:03 +0200236
237 if (err)
238 return err;
239
Damjan Marion191a1a42017-07-10 15:38:21 +0200240 if (command & PCI_COMMAND_BUS_MASTER)
Damjan Marion5a206ea2016-05-12 22:11:03 +0200241 return 0;
242
243 command |= PCI_COMMAND_BUS_MASTER;
244
Damjan Marioncef87f12017-10-05 15:32:41 +0200245 return vlib_pci_write_config_u16 (h, 4, &command);
Damjan Marion5a206ea2016-05-12 22:11:03 +0200246}
Damjan Mariona42cd342016-04-13 18:03:20 +0200247
Damjan Marioncef87f12017-10-05 15:32:41 +0200248clib_error_t *vlib_pci_map_resource (vlib_pci_dev_handle_t h, u32 resource,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400249 void **result);
Damjan Marion5a206ea2016-05-12 22:11:03 +0200250
Damjan Marioncef87f12017-10-05 15:32:41 +0200251clib_error_t *vlib_pci_map_resource_fixed (vlib_pci_dev_handle_t h,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400252 u32 resource, u8 * addr,
253 void **result);
Damjan Mariona42cd342016-04-13 18:03:20 +0200254
Damjan Marion5a206ea2016-05-12 22:11:03 +0200255unformat_function_t unformat_vlib_pci_addr;
256format_function_t format_vlib_pci_addr;
Damjan Marion5a206ea2016-05-12 22:11:03 +0200257format_function_t format_vlib_pci_link_speed;
Damjan Marion7d221612016-12-26 11:39:42 +0100258format_function_t format_vlib_pci_vpd;
Damjan Mariona42cd342016-04-13 18:03:20 +0200259
260#endif /* included_vlib_pci_h */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400261
262/*
263 * fd.io coding-style-patch-verification: ON
264 *
265 * Local Variables:
266 * eval: (c-set-style "gnu")
267 * End:
268 */