aboutsummaryrefslogtreecommitdiffstats
path: root/src/vlib/freebsd/pci.c
blob: a4e9eb2dda6cd816cc031bb8b0c94027aa55ef79 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
/*
 * SPDX-License-Identifier: Apache-2.0
 * Copyright (c) 2024 Tom Jones <thj@freebsd.org>
 *
 * This software was developed by Tom Jones <thj@freebsd.org> under sponsorship
 * from the FreeBSD Foundation.
 *
 */

#include <vlib/vlib.h>
#include <vlib/pci/pci.h>
#include <vlib/unix/unix.h>

#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <sys/eventfd.h>

#include <sys/pciio.h>

#include <fcntl.h>
#include <dirent.h>
#include <net/if.h>

extern vlib_pci_main_t freebsd_pci_main;

uword
vlib_pci_get_private_data (vlib_main_t *vm, vlib_pci_dev_handle_t h)
{
  return 0;
}

void
vlib_pci_set_private_data (vlib_main_t *vm, vlib_pci_dev_handle_t h,
			   uword private_data)
{
}

vlib_pci_addr_t *
vlib_pci_get_addr (vlib_main_t *vm, vlib_pci_dev_handle_t h)
{
  return NULL;
}

u32
vlib_pci_get_numa_node (vlib_main_t *vm, vlib_pci_dev_handle_t h)
{
  return 0;
}

u32
vlib_pci_get_num_msix_interrupts (vlib_main_t *vm, vlib_pci_dev_handle_t h)
{
  return 0;
}

/* Call to allocate/initialize the pci subsystem.
   This is not an init function so that users can explicitly enable
   pci only when it's needed. */
clib_error_t *pci_bus_init (vlib_main_t *vm);

vlib_pci_device_info_t *
vlib_pci_get_device_info (vlib_main_t *vm, vlib_pci_addr_t *addr,
			  clib_error_t **error)
{
  /* Populate a vlib_pci_device_info_t from the given address */
  clib_error_t *err = NULL;
  vlib_pci_device_info_t *di = NULL;

  int fd = -1;
  struct pci_conf_io pci;
  struct pci_conf match;
  struct pci_match_conf pattern;
  bzero (&match, sizeof (match));
  bzero (&pattern, sizeof (pattern));

  pattern.pc_sel.pc_domain = addr->domain;
  pattern.pc_sel.pc_bus = addr->bus;
  pattern.pc_sel.pc_dev = addr->slot;
  pattern.pc_sel.pc_func = addr->function;
  pattern.flags = PCI_GETCONF_MATCH_DOMAIN | PCI_GETCONF_MATCH_BUS |
		  PCI_GETCONF_MATCH_DEV | PCI_GETCONF_MATCH_FUNC;

  pci.pat_buf_len = sizeof (pattern);
  pci.num_patterns = 1;
  pci.patterns = &pattern;
  pci.match_buf_len = sizeof (match);
  pci.num_matches = 1;
  pci.matches = &match;
  pci.offset = 0;
  pci.generation = 0;
  pci.status = 0;

  fd = open ("/dev/pci", 0);
  if (fd == -1)
    {
      err = clib_error_return_unix (0, "open '/dev/pci'");
      goto error;
    }

  if (ioctl (fd, PCIOCGETCONF, &pci) == -1)
    {
      err = clib_error_return_unix (0, "reading PCIOCGETCONF");
      goto error;
    }

  di = clib_mem_alloc (sizeof (vlib_pci_device_info_t));
  clib_memset (di, 0, sizeof (vlib_pci_device_info_t));

  di->addr.as_u32 = addr->as_u32;
  di->numa_node = 0; /* TODO: Place holder until we have NUMA on FreeBSD */

  di->device_class = match.pc_class;
  di->vendor_id = match.pc_vendor;
  di->device_id = match.pc_device;
  di->revision = match.pc_revid;

  di->product_name = NULL;
  di->vpd_r = 0;
  di->vpd_w = 0;
  di->driver_name = format (0, "%s", &match.pd_name);
  di->iommu_group = -1;

  goto done;

error:
  vlib_pci_free_device_info (di);
  di = NULL;
done:
  if (error)
    *error = err;
  close (fd);
  return di;
}

clib_error_t *__attribute__ ((weak))
vlib_pci_get_device_root_bus (vlib_pci_addr_t *addr, vlib_pci_addr_t *root_bus)
{
  return NULL;
}

clib_error_t *
vlib_pci_bind_to_uio (vlib_main_t *vm, vlib_pci_addr_t *addr,
		      char *uio_drv_name, int force)
{
  clib_error_t *error = 0;

  if (error)
    {
      return error;
    }

  if (strncmp ("auto", uio_drv_name, 5) == 0)
    {
      /* TODO: We should confirm that nic_uio is loaded and return an error. */
      uio_drv_name = "nic_uio";
    }
  return error;
}

clib_error_t *
vlib_pci_register_intx_handler (vlib_main_t *vm, vlib_pci_dev_handle_t h,
				pci_intx_handler_function_t *intx_handler)
{
  return NULL;
}

clib_error_t *
vlib_pci_unregister_intx_handler (vlib_main_t *vm, vlib_pci_dev_handle_t h)
{
  return NULL;
}

clib_error_t *
vlib_pci_register_msix_handler (vlib_main_t *vm, vlib_pci_dev_handle_t h,
				u32 start, u32 count,
				pci_msix_handler_function_t *msix_handler)
{
  return NULL;
}

clib_error_t *
vlib_pci_unregister_msix_handler (vlib_main_t *vm, vlib_pci_dev_handle_t h,
				  u32 start, u32 count)
{
  return NULL;
}

clib_error_t *
vlib_pci_enable_msix_irq (vlib_main_t *vm, vlib_pci_dev_handle_t h, u16 start,
			  u16 count)
{
  return NULL;
}

uword
vlib_pci_get_msix_file_index (vlib_main_t *vm, vlib_pci_dev_handle_t h,
			      u16 index)
{
  return 0;
}

clib_error_t *
vlib_pci_disable_msix_irq (vlib_main_t *vm, vlib_pci_dev_handle_t h, u16 start,
			   u16 count)
{
  return NULL;
}

/* Configuration space read/write. */
clib_error_t *
vlib_pci_read_write_config (vlib_main_t *vm, vlib_pci_dev_handle_t h,
			    vlib_read_or_write_t read_or_write, uword address,
			    void *data, u32 n_bytes)
{
  return NULL;
}

clib_error_t *
vlib_pci_map_region (vlib_main_t *vm, vlib_pci_dev_handle_t h, u32 resource,
		     void **result)
{
  return NULL;
}

clib_error_t *
vlib_pci_map_region_fixed (vlib_main_t *vm, vlib_pci_dev_handle_t h,
			   u32 resource, u8 *addr, void **result)
{
  return NULL;
}

clib_error_t *
vlib_pci_io_region (vlib_main_t *vm, vlib_pci_dev_handle_t h, u32 resource)
{
  return NULL;
}

clib_error_t *
vlib_pci_read_write_io (vlib_main_t *vm, vlib_pci_dev_handle_t h,
			vlib_read_or_write_t read_or_write, uword offset,
			void *data, u32 length)
{
  return NULL;
}

clib_error_t *
vlib_pci_map_dma (vlib_main_t *vm, vlib_pci_dev_handle_t h, void *ptr)
{
  return NULL;
}

int
vlib_pci_supports_virtual_addr_dma (vlib_main_t *vm, vlib_pci_dev_handle_t h)
{
  return 0;
}

clib_error_t *
vlib_pci_device_open (vlib_main_t *vm, vlib_pci_addr_t *addr,
		      pci_device_id_t ids[], vlib_pci_dev_handle_t *handle)
{
  return NULL;
}

void
vlib_pci_device_close (vlib_main_t *vm, vlib_pci_dev_handle_t h)
{
}

void
init_device_from_registered (vlib_main_t *vm, vlib_pci_device_info_t *di)
{
}

static int
pci_addr_cmp (void *v1, void *v2)
{
  vlib_pci_addr_t *a1 = v1;
  vlib_pci_addr_t *a2 = v2;

  if (a1->domain > a2->domain)
    return 1;
  if (a1->domain < a2->domain)
    return -1;
  if (a1->bus > a2->bus)
    return 1;
  if (a1->bus < a2->bus)
    return -1;
  if (a1->slot > a2->slot)
    return 1;
  if (a1->slot < a2->slot)
    return -1;
  if (a1->function > a2->function)
    return 1;
  if (a1->function < a2->function)
    return -1;
  return 0;
}

vlib_pci_addr_t *
vlib_pci_get_all_dev_addrs ()
{
  vlib_pci_addr_t *addrs = 0;

  int fd = -1;
  struct pci_conf_io pci;
  struct pci_conf matches[32];
  bzero (matches, sizeof (matches));

  pci.pat_buf_len = 0;
  pci.num_patterns = 0;
  pci.patterns = NULL;
  pci.match_buf_len = sizeof (matches);
  pci.num_matches = 32;
  pci.matches = (struct pci_conf *) &matches;
  pci.offset = 0;
  pci.generation = 0;
  pci.status = 0;

  fd = open ("/dev/pci", 0);
  if (fd == -1)
    {
      clib_error_return_unix (0, "opening /dev/pci");
      return (NULL);
    }

  if (ioctl (fd, PCIOCGETCONF, &pci) == -1)
    {
      clib_error_return_unix (0, "reading pci config");
      close (fd);
      return (NULL);
    }

  for (int i = 0; i < pci.num_matches; i++)
    {
      struct pci_conf *m = &pci.matches[i];
      vlib_pci_addr_t addr;

      addr.domain = m->pc_sel.pc_domain;
      addr.bus = m->pc_sel.pc_bus;
      addr.slot = m->pc_sel.pc_dev;
      addr.function = m->pc_sel.pc_func;

      vec_add1 (addrs, addr);
    }

  vec_sort_with_function (addrs, pci_addr_cmp);
  close (fd);

  return addrs;
}

clib_error_t *
freebsd_pci_init (vlib_main_t *vm)
{
  vlib_pci_main_t *pm = &pci_main;
  vlib_pci_addr_t *addr = 0, *addrs;

  pm->vlib_main = vm;

  ASSERT (sizeof (vlib_pci_addr_t) == sizeof (u32));

  addrs = vlib_pci_get_all_dev_addrs ();
  vec_foreach (addr, addrs)
    {
      vlib_pci_device_info_t *d;
      if ((d = vlib_pci_get_device_info (vm, addr, 0)))
	{
	  init_device_from_registered (vm, d);
	  vlib_pci_free_device_info (d);
	}
    }

  return 0;
}

VLIB_INIT_FUNCTION (freebsd_pci_init) = {
  .runs_after = VLIB_INITS ("unix_input_init"),
};