]> mj.ucw.cz Git - pciutils.git/blob - lib/hurd.c
CXL3.0: Add DVSEC CXLCtrl3 and missing CXLCtl2
[pciutils.git] / lib / hurd.c
1 /*
2  *      The PCI Library -- Hurd access via RPCs
3  *
4  *      Copyright (c) 2017 Joan Lledó <jlledom@member.fsf.org>
5  *
6  *      Can be freely distributed and used under the terms of the GNU GPL.
7  */
8
9 #define _GNU_SOURCE
10
11 #include "internal.h"
12
13 #include <stdlib.h>
14 #include <stdio.h>
15 #include <sys/types.h>
16 #include <sys/stat.h>
17 #include <unistd.h>
18 #include <dirent.h>
19 #include <fcntl.h>
20 #include <string.h>
21 #include <hurd.h>
22 #include <hurd/pci.h>
23 #include <hurd/paths.h>
24
25 /* Server path */
26 #define _SERVERS_BUS_PCI        _SERVERS_BUS "/pci"
27
28 /* File names */
29 #define FILE_CONFIG_NAME "config"
30 #define FILE_ROM_NAME "rom"
31
32 /* Level in the fs tree */
33 typedef enum
34 {
35   LEVEL_NONE,
36   LEVEL_DOMAIN,
37   LEVEL_BUS,
38   LEVEL_DEV,
39   LEVEL_FUNC
40 } tree_level;
41
42 /* Check whether there's a pci server */
43 static int
44 hurd_detect(struct pci_access *a)
45 {
46   int err;
47   struct stat st;
48
49   err = stat(_SERVERS_BUS_PCI, &st);
50   if (err)
51     {
52       a->error("Could not open file `%s'", _SERVERS_BUS_PCI);
53       return 0;
54     }
55
56   /* The node must be a directory and a translator */
57   return S_ISDIR(st.st_mode) && ((st.st_mode & S_ITRANS) == S_IROOT);
58 }
59
60 /* Empty callbacks, we don't need any special init or cleanup */
61 static void
62 hurd_init(struct pci_access *a UNUSED)
63 {
64 }
65
66 static void
67 hurd_cleanup(struct pci_access *a UNUSED)
68 {
69 }
70
71 /* Each device has its own server path. Allocate space for the port. */
72 static void
73 hurd_init_dev(struct pci_dev *d)
74 {
75   d->aux = pci_malloc(d->access, sizeof(mach_port_t));
76   *((mach_port_t *) d->aux) = MACH_PORT_NULL;
77 }
78
79 /* Deallocate the port and free its space */
80 static void
81 hurd_cleanup_dev(struct pci_dev *d)
82 {
83   mach_port_t device_port;
84
85   device_port = *((mach_port_t *) d->aux);
86   mach_port_deallocate(mach_task_self(), device_port);
87
88   pci_mfree(d->aux);
89 }
90
91 static mach_port_t
92 device_port_lookup(struct pci_dev *d)
93 {
94   char server[NAME_MAX];
95   mach_port_t device_port = *((mach_port_t *) d->aux);
96
97   if (device_port != MACH_PORT_NULL)
98     return device_port;
99
100   snprintf(server, NAME_MAX, "%s/%04x/%02x/%02x/%01u/%s",
101     _SERVERS_BUS_PCI, d->domain, d->bus, d->dev, d->func,
102     FILE_CONFIG_NAME);
103   device_port = file_name_lookup(server, 0, 0);
104
105   if (device_port == MACH_PORT_NULL)
106     d->access->error("Cannot find the PCI arbiter");
107
108   *((mach_port_t *) d->aux) = device_port;
109   return device_port;
110 }
111
112 /* Walk through the FS tree to see what is allowed for us */
113 static void
114 enum_devices(const char *parent, struct pci_access *a, int domain, int bus,
115              int dev, int func, tree_level lev)
116 {
117   int ret;
118   DIR *dir;
119   struct dirent *entry;
120   char path[NAME_MAX];
121   struct pci_dev *d;
122
123   dir = opendir(parent);
124   if (!dir)
125     {
126       if (errno == EPERM || errno == EACCES)
127         /* The client lacks the permissions to access this function, skip */
128         return;
129       else
130         a->error("Cannot open directory: %s (%s)", parent, strerror(errno));
131     }
132
133   while ((entry = readdir(dir)) != 0)
134     {
135       snprintf(path, NAME_MAX, "%s/%s", parent, entry->d_name);
136       if (entry->d_type == DT_DIR)
137         {
138           if (!strncmp(entry->d_name, ".", NAME_MAX)
139               || !strncmp(entry->d_name, "..", NAME_MAX))
140             continue;
141
142           errno = 0;
143           ret = strtol(entry->d_name, 0, 16);
144           if (errno)
145             {
146               if (closedir(dir) < 0)
147                 a->warning("Cannot close directory: %s (%s)", parent,
148                            strerror(errno));
149               a->error("Wrong directory name: %s (number expected) probably "
150                        "not connected to an arbiter", entry->d_name);
151             }
152
153           /*
154            * We found a valid directory.
155            * Update the address and switch to the next level.
156            */
157           switch (lev)
158             {
159             case LEVEL_DOMAIN:
160               domain = ret;
161               break;
162             case LEVEL_BUS:
163               bus = ret;
164               break;
165             case LEVEL_DEV:
166               dev = ret;
167               break;
168             case LEVEL_FUNC:
169               func = ret;
170               break;
171             default:
172               if (closedir(dir) < 0)
173                 a->warning("Cannot close directory: %s (%s)", parent,
174                            strerror(errno));
175               a->error("Wrong directory tree, probably not connected to an arbiter");
176             }
177
178           enum_devices(path, a, domain, bus, dev, func, lev + 1);
179         }
180       else
181         {
182           if (strncmp(entry->d_name, FILE_CONFIG_NAME, NAME_MAX))
183             /* We are looking for the config file */
184             continue;
185
186           /* We found an available virtual device, add it to our list */
187           d = pci_alloc_dev(a);
188           d->domain = domain;
189           d->bus = bus;
190           d->dev = dev;
191           d->func = func;
192           pci_link_dev(a, d);
193         }
194     }
195
196   if (closedir(dir) < 0)
197     a->error("Cannot close directory: %s (%s)", parent, strerror(errno));
198 }
199
200 /* Enumerate devices */
201 static void
202 hurd_scan(struct pci_access *a)
203 {
204   enum_devices(_SERVERS_BUS_PCI, a, -1, -1, -1, -1, LEVEL_DOMAIN);
205 }
206
207 /*
208  * Read `len' bytes to `buf'.
209  *
210  * Returns error when the number of read bytes does not match `len'.
211  */
212 static int
213 hurd_read(struct pci_dev *d, int pos, byte * buf, int len)
214 {
215   int err;
216   size_t nread;
217   char *data;
218   mach_port_t device_port = device_port_lookup(d);
219
220   if (len > 4)
221     return pci_generic_block_read(d, pos, buf, len);
222
223   data = (char *) buf;
224   err = pci_conf_read(device_port, pos, &data, &nread, len);
225
226   if (data != (char *) buf)
227     {
228       if (nread > (size_t) len) /* Sanity check for bogus server.  */
229         {
230           vm_deallocate(mach_task_self(), (vm_address_t) data, nread);
231           return 0;
232         }
233
234       memcpy(buf, data, nread);
235       vm_deallocate(mach_task_self(), (vm_address_t) data, nread);
236     }
237
238   return !err && nread == (size_t) len;
239 }
240
241 /*
242  * Write `len' bytes from `buf'.
243  *
244  * Returns error when the number of written bytes does not match `len'.
245  */
246 static int
247 hurd_write(struct pci_dev *d, int pos, byte * buf, int len)
248 {
249   int err;
250   size_t nwrote;
251   mach_port_t device_port = device_port_lookup(d);
252
253   if (len > 4)
254     return pci_generic_block_write(d, pos, buf, len);
255
256   err = pci_conf_write(device_port, pos, (char *) buf, len, &nwrote);
257
258   return !err && nwrote == (size_t) len;
259 }
260
261 /* Get requested info from the server */
262
263 static int
264 hurd_fill_regions(struct pci_dev *d)
265 {
266   mach_port_t device_port = device_port_lookup(d);
267   struct pci_bar regions[6];
268   char *buf = (char *) &regions;
269   size_t size = sizeof(regions);
270
271   int err = pci_get_dev_regions(device_port, &buf, &size);
272   if (err)
273     return 0;
274
275   if ((char *) &regions != buf)
276     {
277       /* Sanity check for bogus server.  */
278       if (size > sizeof(regions))
279         {
280           vm_deallocate(mach_task_self(), (vm_address_t) buf, size);
281           return 0;
282         }
283
284       memcpy(&regions, buf, size);
285       vm_deallocate(mach_task_self(), (vm_address_t) buf, size);
286     }
287
288   for (int i = 0; i < 6; i++)
289     {
290       if (regions[i].size == 0)
291         continue;
292
293       d->base_addr[i] = regions[i].base_addr;
294       d->base_addr[i] |= regions[i].is_IO;
295       d->base_addr[i] |= regions[i].is_64 << 2;
296       d->base_addr[i] |= regions[i].is_prefetchable << 3;
297
298       d->size[i] = regions[i].size;
299     }
300
301   return 1;
302 }
303
304 static int
305 hurd_fill_rom(struct pci_dev *d)
306 {
307   struct pci_xrom_bar rom;
308   mach_port_t device_port = device_port_lookup(d);
309   char *buf = (char *) &rom;
310   size_t size = sizeof(rom);
311
312   int err = pci_get_dev_rom(device_port, &buf, &size);
313   if (err)
314     return 0;
315
316   if ((char *) &rom != buf)
317     {
318       /* Sanity check for bogus server.  */
319       if (size > sizeof(rom))
320         {
321           vm_deallocate(mach_task_self(), (vm_address_t) buf, size);
322           return 0;
323         }
324
325       memcpy(&rom, buf, size);
326       vm_deallocate(mach_task_self(), (vm_address_t) buf, size);
327     }
328
329   d->rom_base_addr = rom.base_addr;
330   d->rom_size = rom.size;
331
332   return 1;
333 }
334
335 static void
336 hurd_fill_info(struct pci_dev *d, unsigned int flags)
337 {
338   if (!d->access->buscentric)
339     {
340       if (want_fill(d, flags, PCI_FILL_BASES | PCI_FILL_SIZES))
341         {
342           if (hurd_fill_regions(d))
343             clear_fill(d, PCI_FILL_BASES | PCI_FILL_SIZES);
344         }
345       if (want_fill(d, flags, PCI_FILL_ROM_BASE))
346         {
347           if (hurd_fill_rom(d))
348             clear_fill(d, PCI_FILL_ROM_BASE);
349         }
350     }
351
352   pci_generic_fill_info(d, flags);
353 }
354
355 struct pci_methods pm_hurd = {
356   "hurd",
357   "Hurd access using RPCs",
358   NULL,                         /* config */
359   hurd_detect,
360   hurd_init,
361   hurd_cleanup,
362   hurd_scan,
363   hurd_fill_info,
364   hurd_read,
365   hurd_write,
366   NULL,                         /* read_vpd */
367   hurd_init_dev,
368   hurd_cleanup_dev
369 };