]> mj.ucw.cz Git - pciutils.git/blob - lib/hurd.c
a184bc563fd2eab9ee0521fcbb3b84a00b44aa0b
[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   *((mach_port_t *) d->aux) = device_port;
106   return device_port;
107 }
108
109 /* Walk through the FS tree to see what is allowed for us */
110 static void
111 enum_devices(const char *parent, struct pci_access *a, int domain, int bus,
112              int dev, int func, tree_level lev)
113 {
114   int ret;
115   DIR *dir;
116   struct dirent *entry;
117   char path[NAME_MAX];
118   uint32_t vd;
119   uint8_t ht;
120   struct pci_dev *d;
121
122   dir = opendir(parent);
123   if (!dir)
124     {
125       if (errno == EPERM || errno == EACCES)
126         /* The client lacks the permissions to access this function, skip */
127         return;
128       else
129         a->error("Cannot open directory: %s (%s)", parent, strerror(errno));
130     }
131
132   while ((entry = readdir(dir)) != 0)
133     {
134       snprintf(path, NAME_MAX, "%s/%s", parent, entry->d_name);
135       if (entry->d_type == DT_DIR)
136         {
137           if (!strncmp(entry->d_name, ".", NAME_MAX)
138               || !strncmp(entry->d_name, "..", NAME_MAX))
139             continue;
140
141           errno = 0;
142           ret = strtol(entry->d_name, 0, 16);
143           if (errno)
144             {
145               if (closedir(dir) < 0)
146                 a->warning("Cannot close directory: %s (%s)", parent,
147                            strerror(errno));
148               a->error("Wrong directory name: %s (number expected) probably "
149                        "not connected to an arbiter", entry->d_name);
150             }
151
152           /*
153            * We found a valid directory.
154            * Update the address and switch to the next level.
155            */
156           switch (lev)
157             {
158             case LEVEL_DOMAIN:
159               domain = ret;
160               break;
161             case LEVEL_BUS:
162               bus = ret;
163               break;
164             case LEVEL_DEV:
165               dev = ret;
166               break;
167             case LEVEL_FUNC:
168               func = ret;
169               break;
170             default:
171               if (closedir(dir) < 0)
172                 a->warning("Cannot close directory: %s (%s)", parent,
173                            strerror(errno));
174               a->error("Wrong directory tree, probably not connected to an arbiter");
175             }
176
177           enum_devices(path, a, domain, bus, dev, func, lev + 1);
178         }
179       else
180         {
181           if (strncmp(entry->d_name, FILE_CONFIG_NAME, NAME_MAX))
182             /* We are looking for the config file */
183             continue;
184
185           /* We found an available virtual device, add it to our list */
186           d = pci_alloc_dev(a);
187           d->domain = domain;
188           d->bus = bus;
189           d->dev = dev;
190           d->func = func;
191
192           /* Get the arbiter port */
193           if (device_port_lookup(d) == MACH_PORT_NULL)
194             {
195               if (closedir(dir) < 0)
196                 a->warning("Cannot close directory: %s (%s)", parent,
197                            strerror(errno));
198               a->error("Cannot find the PCI arbiter");
199             }
200
201           pci_link_dev(a, d);
202
203           vd = pci_read_long(d, PCI_VENDOR_ID);
204           ht = pci_read_byte(d, PCI_HEADER_TYPE);
205
206           d->vendor_id = vd & 0xffff;
207           d->device_id = vd >> 16U;
208           d->known_fields = PCI_FILL_IDENT;
209           d->hdrtype = ht;
210         }
211     }
212
213   if (closedir(dir) < 0)
214     a->error("Cannot close directory: %s (%s)", parent, strerror(errno));
215 }
216
217 /* Enumerate devices */
218 static void
219 hurd_scan(struct pci_access *a)
220 {
221   enum_devices(_SERVERS_BUS_PCI, a, -1, -1, -1, -1, LEVEL_DOMAIN);
222 }
223
224 /*
225  * Read `len' bytes to `buf'.
226  *
227  * Returns error when the number of read bytes does not match `len'.
228  */
229 static int
230 hurd_read(struct pci_dev *d, int pos, byte * buf, int len)
231 {
232   int err;
233   size_t nread;
234   char *data;
235   mach_port_t device_port;
236
237   if (len > 4)
238     return pci_generic_block_read(d, pos, buf, nread);
239
240   device_port = device_port_lookup(d);
241   if (device_port == MACH_PORT_NULL)
242     d->access->error("Cannot find the PCI arbiter");
243
244   data = (char *) buf;
245   nread = len;
246   err = pci_conf_read(device_port, pos, &data, &nread, len);
247
248   if (data != (char *) buf)
249     {
250       if (nread > (size_t) len) /* Sanity check for bogus server.  */
251         {
252           vm_deallocate(mach_task_self(), (vm_address_t) data, nread);
253           return 0;
254         }
255
256       memcpy(buf, data, nread);
257       vm_deallocate(mach_task_self(), (vm_address_t) data, nread);
258     }
259
260   return !err && nread == (size_t) len;
261 }
262
263 /*
264  * Write `len' bytes from `buf'.
265  *
266  * Returns error when the number of written bytes does not match `len'.
267  */
268 static int
269 hurd_write(struct pci_dev *d, int pos, byte * buf, int len)
270 {
271   int err;
272   size_t nwrote;
273   mach_port_t device_port;
274
275   if (len > 4)
276     return pci_generic_block_write(d, pos, buf, len);
277
278   device_port = device_port_lookup(d);
279   if (device_port == MACH_PORT_NULL)
280     d->access->error("Cannot find the PCI arbiter");
281
282   nwrote = len;
283   err = pci_conf_write(device_port, pos, (char *) buf, len, &nwrote);
284
285   return !err && nwrote == (size_t) len;
286 }
287
288 /* Get requested info from the server */
289
290 static void
291 hurd_fill_regions(struct pci_dev *d)
292 {
293   mach_port_t device_port = *((mach_port_t *) d->aux);
294   struct pci_bar regions[6];
295   char *buf = (char *) &regions;
296   size_t size = sizeof(regions);
297
298   int err = pci_get_dev_regions(device_port, &buf, &size);
299   if (err)
300     return;
301
302   if ((char *) &regions != buf)
303     {
304       /* Sanity check for bogus server.  */
305       if (size > sizeof(regions))
306         {
307           vm_deallocate(mach_task_self(), (vm_address_t) buf, size);
308           return;
309         }
310
311       memcpy(&regions, buf, size);
312       vm_deallocate(mach_task_self(), (vm_address_t) buf, size);
313     }
314
315   for (int i = 0; i < 6; i++)
316     {
317       if (regions[i].size == 0)
318         continue;
319
320       d->base_addr[i] = regions[i].base_addr;
321       d->base_addr[i] |= regions[i].is_IO;
322       d->base_addr[i] |= regions[i].is_64 << 2;
323       d->base_addr[i] |= regions[i].is_prefetchable << 3;
324
325       d->size[i] = regions[i].size;
326     }
327 }
328
329 static void
330 hurd_fill_rom(struct pci_dev *d)
331 {
332   struct pci_xrom_bar rom;
333   mach_port_t device_port = *((mach_port_t *) d->aux);
334   char *buf = (char *) &rom;
335   size_t size = sizeof(rom);
336
337   int err = pci_get_dev_rom(device_port, &buf, &size);
338   if (err)
339     return;
340
341   if ((char *) &rom != buf)
342     {
343       /* Sanity check for bogus server.  */
344       if (size > sizeof(rom))
345         {
346           vm_deallocate(mach_task_self(), (vm_address_t) buf, size);
347           return;
348         }
349
350       memcpy(&rom, buf, size);
351       vm_deallocate(mach_task_self(), (vm_address_t) buf, size);
352     }
353
354   d->rom_base_addr = rom.base_addr;
355   d->rom_size = rom.size;
356 }
357
358 static unsigned int
359 hurd_fill_info(struct pci_dev *d, unsigned int flags)
360 {
361   unsigned int done = 0;
362
363   if (!d->access->buscentric)
364     {
365       if (flags & (PCI_FILL_BASES | PCI_FILL_SIZES))
366         {
367           hurd_fill_regions(d);
368           done |= PCI_FILL_BASES | PCI_FILL_SIZES;
369         }
370       if (flags & PCI_FILL_ROM_BASE)
371         {
372           hurd_fill_rom(d);
373           done |= PCI_FILL_ROM_BASE;
374         }
375     }
376
377   return done | pci_generic_fill_info(d, flags & ~done);
378 }
379
380 struct pci_methods pm_hurd = {
381   "hurd",
382   "Hurd access using RPCs",
383   NULL,                         /* config */
384   hurd_detect,
385   hurd_init,
386   hurd_cleanup,
387   hurd_scan,
388   hurd_fill_info,
389   hurd_read,
390   hurd_write,
391   NULL,                         /* read_vpd */
392   hurd_init_dev,
393   hurd_cleanup_dev
394 };