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