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