Initial commit of vpp code.
[vpp.git] / vlib / vlib / unix / pci.c
1 /*
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:
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 #include <vlib/unix/pci.h>
44
45 #include <sys/types.h>
46 #include <sys/stat.h>
47 #include <fcntl.h>
48 #include <dirent.h>
49
50 linux_pci_main_t linux_pci_main;
51
52 static clib_error_t *
53 foreach_directory_file (char * dir_name,
54                         clib_error_t * (* f) (void * arg, u8 * path_name, u8 * file_name),
55                         void * arg,
56                         int scan_dirs)
57 {
58   DIR * d;
59   struct dirent * e;
60   clib_error_t * error = 0;
61   u8 * s, * t;
62
63   d = opendir (dir_name);
64   if (! d)
65     {
66       /* System has no PCI bus. */
67       if (errno == ENOENT)
68         return 0;
69       return clib_error_return_unix (0, "open `%s'", dir_name);
70     }
71
72   s = t = 0;
73   while (1)
74     {
75       e = readdir (d);
76       if (! e)
77         break;
78       if (scan_dirs)
79         {
80           if (e->d_type == DT_DIR
81               && (! strcmp (e->d_name, ".")
82                   || ! strcmp (e->d_name, "..")))
83             continue;
84         }
85       else
86         {
87           if (e->d_type == DT_DIR)
88             continue;
89         }
90
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);
94       _vec_len (s) = 0;
95       _vec_len (t) = 0;
96
97       if (error)
98         break;
99     }
100
101   vec_free (s);
102   closedir (d);
103
104   return error;
105 }
106
107 static clib_error_t *
108 write_sys_fs (char * file_name, char * fmt, ...)
109 {
110   u8 * s;
111   int fd;
112
113   fd = open (file_name, O_WRONLY);
114   if (fd < 0)
115     return clib_error_return_unix (0, "open `%s'", file_name);
116
117   va_list va;
118   va_start (va, fmt);
119   s = va_format (0, fmt, &va);
120   va_end (va);
121
122   if (write (fd, s, vec_len (s)) < 0)
123     return clib_error_return_unix (0, "write `%s'", file_name);
124
125   vec_free (s);
126   close (fd);
127   return 0;
128 }
129
130 static clib_error_t *
131 scan_uio_dir (void * arg, u8 * path_name, u8 * file_name)
132 {
133   linux_pci_device_t * l = arg;
134   unformat_input_t input;
135
136   unformat_init_string (&input, (char *) file_name, vec_len (file_name));
137
138   if (! unformat (&input, "uio%d", &l->uio_minor))
139     abort ();
140
141   unformat_free (&input);
142   return 0;
143 }
144
145 static clib_error_t * linux_pci_uio_read_ready (unix_file_t * uf)
146 {
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;
151
152   l = pool_elt_at_index (pm->pci_devices, li);
153   vlib_node_set_interrupt_pending (vm, l->device_input_node_index);
154
155   /* Let node know which device is interrupting. */
156   {
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;
159   }
160
161   return /* no error */ 0;
162 }
163
164 static clib_error_t *linux_pci_uio_error_ready (unix_file_t *uf)
165 {
166   u32 error_index = (u32) uf->private_data;
167
168   return clib_error_return (0, "pci device %d: error", error_index);
169 }
170
171 static uword pci_resource_size (uword os_handle, uword resource)
172 {
173   linux_pci_main_t * pm = &linux_pci_main;
174   linux_pci_device_t * p;
175   u8 * file_name;
176   struct stat b;
177   uword result = 0;
178
179   p = pool_elt_at_index (pm->pci_devices, os_handle);
180
181   file_name = format (0, "%v/resource%d%c", p->dev_dir_name, resource, 0);
182   if (stat ((char *) file_name, &b) >= 0)
183     result = b.st_size;
184   vec_free (file_name);
185   return result;
186 }
187
188 void os_add_pci_disable_interrupts_reg (uword os_handle, u32 resource,
189                                         u32 reg_offset, u32 reg_value)
190 {
191   linux_pci_main_t * pm = &linux_pci_main;
192   linux_pci_device_t * l;
193   char * file_name;
194   clib_error_t * error;
195
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);
201   if (error)
202     clib_error_report (error);
203   vec_free (file_name);
204 }
205
206 static void add_device (pci_device_t * dev, linux_pci_device_t * pdev)
207 {
208   linux_pci_main_t * pm = &linux_pci_main;
209   linux_pci_device_t * l;
210   pci_config_header_t * c;
211   u32 x[4];
212   clib_error_t * error;
213
214   c = &dev->config0.header;
215
216   pool_get (pm->pci_devices, l);
217   l[0] = pdev[0];
218
219   l->dev_dir_name = vec_dup (l->dev_dir_name);
220
221   /* Parse bus, dev, function from directory name. */
222   {
223     unformat_input_t input;
224
225     unformat_init_string (&input, (char *) l->dev_dir_name,
226                           vec_len (l->dev_dir_name));
227
228     if (! unformat (&input, "/sys/bus/pci/devices/%x:%x:%x.%x",
229                     &x[0], &x[1], &x[2], &x[3]))
230       abort ();
231
232     unformat_free (&input);
233
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;
237   }
238
239   dev->os_handle = l - pm->pci_devices;
240
241   error = write_sys_fs ("/sys/bus/pci/drivers/uio_pci_dma/new_id",
242                         "%x %x", c->vendor_id, c->device_id);
243   if (error)
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. */
248   if (error)
249     clib_error_free (error);
250
251   {
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);
254     vec_free (uio_dir);
255   }
256
257   {
258     char * uio_name = (char *) format (0, "/dev/uio%d%c", l->uio_minor, 0);
259     l->uio_fd = open (uio_name, O_RDWR);
260     if (l->uio_fd < 0)
261       clib_unix_error ("open `%s'", uio_name);
262     vec_free (uio_name);
263   }
264
265   {
266     unix_file_t template = {0};
267     unix_main_t * um = &unix_main;
268
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;
273
274     /* To be filled in by driver. */
275     l->device_input_node_index = ~0;
276     l->device_index = 0;
277
278     l->unix_file_index = unix_file_add (um, &template);
279   }
280 }
281
282 static void linux_pci_device_free (linux_pci_device_t * l)
283 {
284   int i;
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);
290   if (l->uio_fd > 0)
291     close (l->uio_fd);
292   vec_free (l->resource_fds);
293   vec_free (l->dev_dir_name);
294 }
295
296 /* Configuration space read/write. */
297 clib_error_t *
298 os_read_write_pci_config (uword os_handle,
299                           vlib_read_or_write_t read_or_write,
300                           uword address,
301                           void * data,
302                           u32 n_bytes)
303 {
304   linux_pci_main_t * pm = &linux_pci_main;
305   linux_pci_device_t * p;
306   int n;
307
308   p = pool_elt_at_index (pm->pci_devices, os_handle);
309
310   if (address != lseek (p->config_fd, address, SEEK_SET))
311     return clib_error_return_unix (0, "seek offset %d", address);
312
313   if (read_or_write == VLIB_READ)
314     n = read (p->config_fd, data, n_bytes);
315   else
316     n = write (p->config_fd, data, n_bytes);
317
318   if (n != n_bytes)
319     return clib_error_return_unix (0, "%s",
320                                    read_or_write == VLIB_READ
321                                    ? "read" : "write");
322
323   return 0;
324 }
325
326 static clib_error_t *
327 os_map_pci_resource_internal (uword os_handle,
328                               u32 resource,
329                               u8 *addr,
330                               void ** result)
331 {
332   linux_pci_main_t * pm = &linux_pci_main;
333   linux_pci_device_t * p;
334   struct stat stat_buf;
335   u8 * file_name;
336   int fd;
337   clib_error_t * error;
338   int flags = MAP_SHARED;
339
340   error = 0;
341   p = pool_elt_at_index (pm->pci_devices, os_handle);
342
343   file_name = format (0, "%v/resource%d%c", p->dev_dir_name, resource, 0);
344   fd = open ((char *) file_name, O_RDWR);
345   if (fd < 0)
346     {
347       error = clib_error_return_unix (0, "open `%s'", file_name);
348       goto done;
349     }
350
351   if (fstat (fd, &stat_buf) < 0)
352     {
353       error = clib_error_return_unix (0, "fstat `%s'", file_name);
354       goto done;
355     }
356
357   vec_validate (p->resource_fds, resource);
358   p->resource_fds[resource] = fd;
359   if (addr != 0)
360     flags |= MAP_FIXED;
361
362   *result = mmap (addr,
363                   /* size */ stat_buf.st_size,
364                   PROT_READ | PROT_WRITE,
365                   flags,
366                   /* file */ fd,
367                   /* offset */ 0);
368   if (*result == (void *) -1)
369     {
370       error = clib_error_return_unix (0, "mmap `%s'", file_name);
371       goto done;
372     }
373
374  done:
375   if (error)
376     {
377       if (fd > 0)
378         close (fd);
379     }
380   vec_free (file_name);
381   return error;
382 }
383
384 clib_error_t *
385 os_map_pci_resource (uword os_handle,
386                      u32 resource,
387                      void ** result)
388 {
389   return (os_map_pci_resource_internal (os_handle, resource, 0 /* addr */,
390                                         result));
391 }
392
393 clib_error_t *
394 os_map_pci_resource_fixed (uword os_handle,
395                            u32 resource,
396                            u8 *addr,
397                            void ** result)
398 {
399   return (os_map_pci_resource_internal (os_handle, resource, addr, result));
400 }
401
402 void os_free_pci_device (uword os_handle)
403 {
404   linux_pci_main_t * pm = &linux_pci_main;
405   linux_pci_device_t * l;
406
407   l = pool_elt_at_index (pm->pci_devices, os_handle);
408   linux_pci_device_free (l);
409   pool_put (pm->pci_devices, l);
410 }
411
412 u8 * format_os_pci_handle (u8 * s, va_list * va)
413 {
414   linux_pci_main_t * pm = &linux_pci_main;
415   uword os_pci_handle = va_arg (*va, uword);
416   linux_pci_device_t * l;
417
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));
422 }
423
424 static inline pci_device_registration_t *
425 pci_device_next_registered (pci_device_registration_t * r)
426 {
427   uword i;
428
429   /* Null vendor id marks end of initialized list. */
430   for (i = 0; r->supported_devices[i].vendor_id != 0; i++)
431     ;
432
433   return clib_elf_section_data_next (r, i * sizeof (r->supported_devices[0]));
434 }
435
436 static inline u8 kernel_driver_installed (pci_device_registration_t *r)
437 {
438   u8 * link_name;
439   struct stat b;
440
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++;
444   else
445     r->kernel_driver_running=0;
446
447   vec_free (link_name);
448   return r->kernel_driver_running;
449 }
450
451 static clib_error_t *
452 init_device_from_registered (vlib_main_t * vm,
453                              pci_device_t * dev,
454                              linux_pci_device_t * pdev)
455 {
456   unix_main_t * um = vlib_unix_get_main();
457   pci_device_registration_t * r;
458   pci_device_id_t * i;
459   pci_config_header_t * c;
460
461   c = &dev->config0.header;
462
463   r = um->pci_device_registrations;
464
465   while (r)
466     {
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)
469           {
470             if (r->kernel_driver && kernel_driver_installed(r))
471               {
472                 if (r->kernel_driver_running == 1)
473                   {
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);
478                   }
479                 continue;
480               }
481             add_device (dev, pdev);
482             return r->init_function (vm, dev);
483           }
484       r = r->next_registration;
485   }
486   /* No driver, close the PCI config-space FD */
487   close (pdev->config_fd);
488   return 0;
489 }
490
491 static clib_error_t *
492 init_device (vlib_main_t * vm,
493              pci_device_t * dev,
494              linux_pci_device_t * pdev)
495 {
496   return init_device_from_registered (vm, dev, pdev);
497 }
498
499 static clib_error_t *
500 scan_device (void * arg, u8 * dev_dir_name, u8 * ignored)
501 {
502   vlib_main_t * vm = arg;
503   int fd;
504   u8 * f;
505   clib_error_t * error = 0;
506   pci_device_t dev = {0};
507   linux_pci_device_t pdev = {0};
508
509   f = format (0, "%v/config%c", dev_dir_name, 0);
510   fd = open ((char *) f, O_RDWR);
511
512   /* Try read-only access if write fails. */
513   if (fd < 0)
514     fd = open ((char *) f, O_RDONLY);
515
516   if (fd < 0)
517     {
518       error = clib_error_return_unix (0, "open `%s'", f);
519       goto done;
520     }
521
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))
526     {
527       error = clib_error_return_unix (0, "read `%s'", f);
528       goto done;
529     }
530
531   {
532     static pci_config_header_t all_ones;
533     if (all_ones.vendor_id == 0)
534       memset (&all_ones, ~0, sizeof (all_ones));
535     
536     if (! memcmp (&dev.config0.header, &all_ones, sizeof (all_ones)))
537       {
538         error = clib_error_return (0, "invalid PCI config for `%s'", f);
539         goto done;
540       }
541   }
542
543   if (dev.config0.header.header_type == 0)
544     pci_config_type0_little_to_host (&dev.config0);
545   else
546     pci_config_type1_little_to_host (&dev.config1);
547
548   pdev.config_fd = fd;
549   pdev.dev_dir_name = dev_dir_name;
550
551   error = init_device (vm, &dev, &pdev);
552
553  done:
554   vec_free (f);
555   return error;
556 }
557
558 clib_error_t * pci_bus_init (vlib_main_t * vm)
559 {
560   linux_pci_main_t * pm = &linux_pci_main;
561   clib_error_t * error;
562
563   pm->vlib_main = vm;
564
565   if ((error = vlib_call_init_function (vm, unix_input_init)))
566     return error;
567
568   error = foreach_directory_file ("/sys/bus/pci/devices", scan_device, vm, /* scan_dirs */ 0);
569
570   /* Complain and continue. might not be root, etc. */
571   if (error)
572     clib_error_report (error);
573
574   return error;
575 }
576
577 VLIB_INIT_FUNCTION (pci_bus_init);