]> mj.ucw.cz Git - pciutils.git/blob - lib/hurd.c
Hurd: Fix minor indentation problems
[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 \
171                        to an arbiter");
172             }
173
174           enum_devices(path, a, domain, bus, dev, func, lev + 1);
175         }
176       else
177         {
178           if (strncmp(entry->d_name, FILE_CONFIG_NAME, NAME_MAX))
179             /* We are looking for the config file */
180             continue;
181
182           /* We found an available virtual device, add it to our list */
183           d = pci_alloc_dev(a);
184           d->domain = domain;
185           d->bus = bus;
186           d->dev = dev;
187           d->func = func;
188
189           /* Get the arbiter port */
190           device_port_lookup(d);
191           if (d->aux == MACH_PORT_NULL)
192             {
193               if (closedir(dir) < 0)
194                 a->warning("Cannot close directory: %s (%s)", parent,
195                            strerror(errno));
196               a->error("Cannot find the PCI arbiter");
197             }
198
199           pci_link_dev(a, d);
200
201           vd = pci_read_long(d, PCI_VENDOR_ID);
202           ht = pci_read_byte(d, PCI_HEADER_TYPE);
203
204           d->vendor_id = vd & 0xffff;
205           d->device_id = vd >> 16U;
206           d->known_fields = PCI_FILL_IDENT;
207           d->hdrtype = ht;
208         }
209     }
210
211   if (closedir(dir) < 0)
212     a->error("Cannot close directory: %s (%s)", parent, strerror(errno));
213 }
214
215 /* Enumerate devices */
216 static void
217 hurd_scan(struct pci_access *a)
218 {
219   enum_devices(_SERVERS_BUS_PCI, a, -1, -1, -1, -1, LEVEL_DOMAIN);
220 }
221
222 /*
223  * Read `len' bytes to `buf'.
224  *
225  * Returns error when the number of read bytes does not match `len'.
226  */
227 static int
228 hurd_read(struct pci_dev *d, int pos, byte * buf, int len)
229 {
230   int err;
231   size_t nread;
232   char *data;
233   mach_port_t device_port;
234
235   nread = len;
236   if (*((mach_port_t *) d->aux) == 0)
237     {
238       /* We still don't have the port for this device */
239       device_port_lookup(d);
240       if (d->aux == MACH_PORT_NULL)
241         {
242           d->access->error("Cannot find the PCI arbiter");
243         }
244     }
245   device_port = *((mach_port_t *) d->aux);
246
247   if (len > 4)
248     err = !pci_generic_block_read(d, pos, buf, nread);
249   else
250     {
251       data = (char *) buf;
252       err = pci_conf_read(device_port, pos, &data, &nread, len);
253
254       if (data != (char *) buf)
255         {
256           if (nread > (size_t) len)     /* Sanity check for bogus server.  */
257             {
258               vm_deallocate(mach_task_self(), (vm_address_t) data, nread);
259               return 0;
260             }
261
262           memcpy(buf, data, nread);
263           vm_deallocate(mach_task_self(), (vm_address_t) data, nread);
264         }
265     }
266   if (err)
267     return 0;
268
269   return nread == (size_t) len;
270 }
271
272 /*
273  * Write `len' bytes from `buf'.
274  *
275  * Returns error when the number of written bytes does not match `len'.
276  */
277 static int
278 hurd_write(struct pci_dev *d, int pos, byte * buf, int len)
279 {
280   int err;
281   size_t nwrote;
282   mach_port_t device_port;
283
284   nwrote = len;
285   if (*((mach_port_t *) d->aux) == 0)
286     {
287       /* We still don't have the port for this device */
288       device_port_lookup(d);
289       if (d->aux == MACH_PORT_NULL)
290         {
291           d->access->error("Cannot find the PCI arbiter");
292         }
293     }
294   device_port = *((mach_port_t *) d->aux);
295
296   if (len > 4)
297     err = !pci_generic_block_write(d, pos, buf, len);
298   else
299     err = pci_conf_write(device_port, pos, (char *) buf, len, &nwrote);
300   if (err)
301     return 0;
302
303   return nwrote == (size_t) len;
304 }
305
306 /* Get requested info from the server */
307 static int
308 hurd_fill_info(struct pci_dev *d, int flags)
309 {
310   int err, i;
311   struct pci_bar regions[6];
312   struct pci_xrom_bar rom;
313   size_t size;
314   char *buf;
315   mach_port_t device_port;
316
317   device_port = *((mach_port_t *) d->aux);
318
319   if (flags & PCI_FILL_BASES)
320     {
321       buf = (char *) &regions;
322       size = sizeof(regions);
323
324       err = pci_get_dev_regions(device_port, &buf, &size);
325       if (err)
326         return err;
327
328       if ((char *) &regions != buf)
329         {
330           /* Sanity check for bogus server.  */
331           if (size > sizeof(regions))
332             {
333               vm_deallocate(mach_task_self(), (vm_address_t) buf, size);
334               return EGRATUITOUS;
335             }
336
337           memcpy(&regions, buf, size);
338           vm_deallocate(mach_task_self(), (vm_address_t) buf, size);
339         }
340
341       for (i = 0; i < 6; i++)
342         {
343           if (regions[i].size == 0)
344             continue;
345
346           d->base_addr[i] = regions[i].base_addr;
347           d->base_addr[i] |= regions[i].is_IO;
348           d->base_addr[i] |= regions[i].is_64 << 2;
349           d->base_addr[i] |= regions[i].is_prefetchable << 3;
350
351           if (flags & PCI_FILL_SIZES)
352             d->size[i] = regions[i].size;
353         }
354     }
355
356   if (flags & PCI_FILL_ROM_BASE)
357     {
358       /* Get rom info */
359       buf = (char *) &rom;
360       size = sizeof(rom);
361       err = pci_get_dev_rom(device_port, &buf, &size);
362       if (err)
363         return err;
364
365       if ((char *) &rom != buf)
366         {
367           /* Sanity check for bogus server.  */
368           if (size > sizeof(rom))
369             {
370               vm_deallocate(mach_task_self(), (vm_address_t) buf, size);
371               return EGRATUITOUS;
372             }
373
374           memcpy(&rom, buf, size);
375           vm_deallocate(mach_task_self(), (vm_address_t) buf, size);
376         }
377
378       d->rom_base_addr = rom.base_addr;
379       d->rom_size = rom.size;
380     }
381
382   return pci_generic_fill_info(d, flags);
383 }
384
385 struct pci_methods pm_hurd = {
386   "hurd",
387   "Hurd access using RPCs",
388   NULL,                         /* config */
389   hurd_detect,
390   hurd_init,
391   hurd_cleanup,
392   hurd_scan,
393   hurd_fill_info,
394   hurd_read,
395   hurd_write,
396   NULL,                         /* read_vpd */
397   hurd_init_dev,
398   hurd_cleanup_dev
399 };