2 * Copyright (c) 2015 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 * pci.c: Linux user space PCI bus management.
18 * Copyright (c) 2008 Eliot Dresselhaus
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:
28 * The above copyright notice and this permission notice shall be
29 * included in all copies or substantial portions of the Software.
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.
40 #include <vlib/vlib.h>
41 #include <vlib/pci/pci.h>
42 #include <vlib/unix/unix.h>
43 #include <vlib/unix/pci.h>
45 #include <sys/types.h>
50 linux_pci_main_t linux_pci_main;
53 foreach_directory_file (char * dir_name,
54 clib_error_t * (* f) (void * arg, u8 * path_name, u8 * file_name),
60 clib_error_t * error = 0;
63 d = opendir (dir_name);
66 /* System has no PCI bus. */
69 return clib_error_return_unix (0, "open `%s'", dir_name);
80 if (e->d_type == DT_DIR
81 && (! strcmp (e->d_name, ".")
82 || ! strcmp (e->d_name, "..")))
87 if (e->d_type == DT_DIR)
91 s = format (s, "%s/%s", dir_name, e->d_name);
92 t = format (t, "%s", e->d_name);
93 error = f (arg, s, t);
107 static clib_error_t *
108 write_sys_fs (char * file_name, char * fmt, ...)
113 fd = open (file_name, O_WRONLY);
115 return clib_error_return_unix (0, "open `%s'", file_name);
119 s = va_format (0, fmt, &va);
122 if (write (fd, s, vec_len (s)) < 0)
123 return clib_error_return_unix (0, "write `%s'", file_name);
130 static clib_error_t *
131 scan_uio_dir (void * arg, u8 * path_name, u8 * file_name)
133 linux_pci_device_t * l = arg;
134 unformat_input_t input;
136 unformat_init_string (&input, (char *) file_name, vec_len (file_name));
138 if (! unformat (&input, "uio%d", &l->uio_minor))
141 unformat_free (&input);
145 static clib_error_t * linux_pci_uio_read_ready (unix_file_t * uf)
147 linux_pci_main_t * pm = &linux_pci_main;
148 vlib_main_t * vm = pm->vlib_main;
149 linux_pci_device_t * l;
150 u32 li = uf->private_data;
152 l = pool_elt_at_index (pm->pci_devices, li);
153 vlib_node_set_interrupt_pending (vm, l->device_input_node_index);
155 /* Let node know which device is interrupting. */
157 vlib_node_runtime_t * rt = vlib_node_get_runtime (vm, l->device_input_node_index);
158 rt->runtime_data[0] |= 1 << l->device_index;
161 return /* no error */ 0;
164 static clib_error_t *linux_pci_uio_error_ready (unix_file_t *uf)
166 u32 error_index = (u32) uf->private_data;
168 return clib_error_return (0, "pci device %d: error", error_index);
171 static uword pci_resource_size (uword os_handle, uword resource)
173 linux_pci_main_t * pm = &linux_pci_main;
174 linux_pci_device_t * p;
179 p = pool_elt_at_index (pm->pci_devices, os_handle);
181 file_name = format (0, "%v/resource%d%c", p->dev_dir_name, resource, 0);
182 if (stat ((char *) file_name, &b) >= 0)
184 vec_free (file_name);
188 void os_add_pci_disable_interrupts_reg (uword os_handle, u32 resource,
189 u32 reg_offset, u32 reg_value)
191 linux_pci_main_t * pm = &linux_pci_main;
192 linux_pci_device_t * l;
194 clib_error_t * error;
196 l = pool_elt_at_index (pm->pci_devices, os_handle);
197 ASSERT (resource == 0);
198 ASSERT (reg_offset < pci_resource_size (os_handle, resource));
199 file_name = (char *) format (0, "%s/disable_interrupt_regs%c", l->dev_dir_name, 0);
200 error = write_sys_fs (file_name, "%x %x", reg_offset, reg_value);
202 clib_error_report (error);
203 vec_free (file_name);
206 static void add_device (pci_device_t * dev, linux_pci_device_t * pdev)
208 linux_pci_main_t * pm = &linux_pci_main;
209 linux_pci_device_t * l;
210 pci_config_header_t * c;
212 clib_error_t * error;
214 c = &dev->config0.header;
216 pool_get (pm->pci_devices, l);
219 l->dev_dir_name = vec_dup (l->dev_dir_name);
221 /* Parse bus, dev, function from directory name. */
223 unformat_input_t input;
225 unformat_init_string (&input, (char *) l->dev_dir_name,
226 vec_len (l->dev_dir_name));
228 if (! unformat (&input, "/sys/bus/pci/devices/%x:%x:%x.%x",
229 &x[0], &x[1], &x[2], &x[3]))
232 unformat_free (&input);
234 l->bus_address.bus = x[1];
235 l->bus_address.slot_function = (x[2] << 3) | x[3];
236 dev->bus_address = l->bus_address;
239 dev->os_handle = l - pm->pci_devices;
241 error = write_sys_fs ("/sys/bus/pci/drivers/uio_pci_dma/new_id",
242 "%x %x", c->vendor_id, c->device_id);
244 clib_error_report (error);
245 error = write_sys_fs ("/sys/bus/pci/drivers/uio_pci_dma/bind",
246 "%04x:%02x:%02x.%x", x[0], x[1], x[2], x[3]);
247 /* Errors happen when re-binding so just ignore them. */
249 clib_error_free (error);
252 u8 * uio_dir = format (0, "%s/uio", l->dev_dir_name);
253 foreach_directory_file ((char *) uio_dir, scan_uio_dir, l, /* scan_dirs */ 1);
258 char * uio_name = (char *) format (0, "/dev/uio%d%c", l->uio_minor, 0);
259 l->uio_fd = open (uio_name, O_RDWR);
261 clib_unix_error ("open `%s'", uio_name);
266 unix_file_t template = {0};
267 unix_main_t * um = &unix_main;
269 template.read_function = linux_pci_uio_read_ready;
270 template.file_descriptor = l->uio_fd;
271 template.error_function = linux_pci_uio_error_ready;
272 template.private_data = l - pm->pci_devices;
274 /* To be filled in by driver. */
275 l->device_input_node_index = ~0;
278 l->unix_file_index = unix_file_add (um, &template);
282 static void linux_pci_device_free (linux_pci_device_t * l)
285 for (i = 0; i < vec_len (l->resource_fds); i++)
286 if (l->resource_fds[i] > 0)
287 close (l->resource_fds[i]);
288 if (l->config_fd > 0)
289 close (l->config_fd);
292 vec_free (l->resource_fds);
293 vec_free (l->dev_dir_name);
296 /* Configuration space read/write. */
298 os_read_write_pci_config (uword os_handle,
299 vlib_read_or_write_t read_or_write,
304 linux_pci_main_t * pm = &linux_pci_main;
305 linux_pci_device_t * p;
308 p = pool_elt_at_index (pm->pci_devices, os_handle);
310 if (address != lseek (p->config_fd, address, SEEK_SET))
311 return clib_error_return_unix (0, "seek offset %d", address);
313 if (read_or_write == VLIB_READ)
314 n = read (p->config_fd, data, n_bytes);
316 n = write (p->config_fd, data, n_bytes);
319 return clib_error_return_unix (0, "%s",
320 read_or_write == VLIB_READ
326 static clib_error_t *
327 os_map_pci_resource_internal (uword os_handle,
332 linux_pci_main_t * pm = &linux_pci_main;
333 linux_pci_device_t * p;
334 struct stat stat_buf;
337 clib_error_t * error;
338 int flags = MAP_SHARED;
341 p = pool_elt_at_index (pm->pci_devices, os_handle);
343 file_name = format (0, "%v/resource%d%c", p->dev_dir_name, resource, 0);
344 fd = open ((char *) file_name, O_RDWR);
347 error = clib_error_return_unix (0, "open `%s'", file_name);
351 if (fstat (fd, &stat_buf) < 0)
353 error = clib_error_return_unix (0, "fstat `%s'", file_name);
357 vec_validate (p->resource_fds, resource);
358 p->resource_fds[resource] = fd;
362 *result = mmap (addr,
363 /* size */ stat_buf.st_size,
364 PROT_READ | PROT_WRITE,
368 if (*result == (void *) -1)
370 error = clib_error_return_unix (0, "mmap `%s'", file_name);
380 vec_free (file_name);
385 os_map_pci_resource (uword os_handle,
389 return (os_map_pci_resource_internal (os_handle, resource, 0 /* addr */,
394 os_map_pci_resource_fixed (uword os_handle,
399 return (os_map_pci_resource_internal (os_handle, resource, addr, result));
402 void os_free_pci_device (uword os_handle)
404 linux_pci_main_t * pm = &linux_pci_main;
405 linux_pci_device_t * l;
407 l = pool_elt_at_index (pm->pci_devices, os_handle);
408 linux_pci_device_free (l);
409 pool_put (pm->pci_devices, l);
412 u8 * format_os_pci_handle (u8 * s, va_list * va)
414 linux_pci_main_t * pm = &linux_pci_main;
415 uword os_pci_handle = va_arg (*va, uword);
416 linux_pci_device_t * l;
418 l = pool_elt_at_index (pm->pci_devices, os_pci_handle);
419 return format (s, "%x/%x/%x", l->bus_address.bus,
420 (l->bus_address.slot_function >> 3),
421 (l->bus_address.slot_function & 0x7));
424 static inline pci_device_registration_t *
425 pci_device_next_registered (pci_device_registration_t * r)
429 /* Null vendor id marks end of initialized list. */
430 for (i = 0; r->supported_devices[i].vendor_id != 0; i++)
433 return clib_elf_section_data_next (r, i * sizeof (r->supported_devices[0]));
436 static inline u8 kernel_driver_installed (pci_device_registration_t *r)
441 link_name = format (0, "/sys/bus/pci/drivers/%s", r->kernel_driver);
442 if (stat ((char *)link_name, &b) >= 0)
443 r->kernel_driver_running++;
445 r->kernel_driver_running=0;
447 vec_free (link_name);
448 return r->kernel_driver_running;
451 static clib_error_t *
452 init_device_from_registered (vlib_main_t * vm,
454 linux_pci_device_t * pdev)
456 unix_main_t * um = vlib_unix_get_main();
457 pci_device_registration_t * r;
459 pci_config_header_t * c;
461 c = &dev->config0.header;
463 r = um->pci_device_registrations;
467 for (i = r->supported_devices; i->vendor_id != 0; i++)
468 if (i->vendor_id == c->vendor_id && i->device_id == c->device_id)
470 if (r->kernel_driver && kernel_driver_installed(r))
472 if (r->kernel_driver_running == 1)
474 clib_warning("PCI device type [%04x:%04x] is busy!\n"
475 "\tUninstall the associated linux kernel "
476 "driver: sudo rmmod %s",
477 c->vendor_id, c->device_id, r->kernel_driver);
481 add_device (dev, pdev);
482 return r->init_function (vm, dev);
484 r = r->next_registration;
486 /* No driver, close the PCI config-space FD */
487 close (pdev->config_fd);
491 static clib_error_t *
492 init_device (vlib_main_t * vm,
494 linux_pci_device_t * pdev)
496 return init_device_from_registered (vm, dev, pdev);
499 static clib_error_t *
500 scan_device (void * arg, u8 * dev_dir_name, u8 * ignored)
502 vlib_main_t * vm = arg;
505 clib_error_t * error = 0;
506 pci_device_t dev = {0};
507 linux_pci_device_t pdev = {0};
509 f = format (0, "%v/config%c", dev_dir_name, 0);
510 fd = open ((char *) f, O_RDWR);
512 /* Try read-only access if write fails. */
514 fd = open ((char *) f, O_RDONLY);
518 error = clib_error_return_unix (0, "open `%s'", f);
522 /* You can only read more that 64 bytes of config space as root; so we try to
523 read the full space but fall back to just the first 64 bytes. */
524 if (read (fd, &dev.config_data, sizeof (dev.config_data)) != sizeof (dev.config_data)
525 && read (fd, &dev.config0, sizeof (dev.config0)) != sizeof (dev.config0))
527 error = clib_error_return_unix (0, "read `%s'", f);
532 static pci_config_header_t all_ones;
533 if (all_ones.vendor_id == 0)
534 memset (&all_ones, ~0, sizeof (all_ones));
536 if (! memcmp (&dev.config0.header, &all_ones, sizeof (all_ones)))
538 error = clib_error_return (0, "invalid PCI config for `%s'", f);
543 if (dev.config0.header.header_type == 0)
544 pci_config_type0_little_to_host (&dev.config0);
546 pci_config_type1_little_to_host (&dev.config1);
549 pdev.dev_dir_name = dev_dir_name;
551 error = init_device (vm, &dev, &pdev);
558 clib_error_t * pci_bus_init (vlib_main_t * vm)
560 linux_pci_main_t * pm = &linux_pci_main;
561 clib_error_t * error;
565 if ((error = vlib_call_init_function (vm, unix_input_init)))
568 error = foreach_directory_file ("/sys/bus/pci/devices", scan_device, vm, /* scan_dirs */ 0);
570 /* Complain and continue. might not be root, etc. */
572 clib_error_report (error);
577 VLIB_INIT_FUNCTION (pci_bus_init);