2 * The PCI Library -- Hurd access via RPCs
4 * Copyright (c) 2017 Joan Lledó <jlledom@member.fsf.org>
6 * Can be freely distributed and used under the terms of the GNU GPL v2+.
8 * SPDX-License-Identifier: GPL-2.0-or-later
17 #include <sys/types.h>
25 #include <hurd/paths.h>
28 #define _SERVERS_BUS_PCI _SERVERS_BUS "/pci"
31 #define FILE_CONFIG_NAME "config"
32 #define FILE_ROM_NAME "rom"
34 /* Level in the fs tree */
44 /* Check whether there's a pci server */
46 hurd_detect(struct pci_access *a)
51 err = stat(_SERVERS_BUS_PCI, &st);
54 a->error("Could not open file `%s'", _SERVERS_BUS_PCI);
58 /* The node must be a directory and a translator */
59 return S_ISDIR(st.st_mode) && ((st.st_mode & S_ITRANS) == S_IROOT);
62 /* Empty callbacks, we don't need any special init or cleanup */
64 hurd_init(struct pci_access *a UNUSED)
69 hurd_cleanup(struct pci_access *a UNUSED)
73 /* Each device has its own server path. Allocate space for the port. */
75 hurd_init_dev(struct pci_dev *d)
77 d->backend_data = pci_malloc(d->access, sizeof(mach_port_t));
78 *((mach_port_t *) d->backend_data) = MACH_PORT_NULL;
81 /* Deallocate the port and free its space */
83 hurd_cleanup_dev(struct pci_dev *d)
85 mach_port_t device_port;
87 device_port = *((mach_port_t *) d->backend_data);
88 mach_port_deallocate(mach_task_self(), device_port);
90 pci_mfree(d->backend_data);
91 d->backend_data = NULL;
95 device_port_lookup(struct pci_dev *d)
97 char server[NAME_MAX];
98 mach_port_t device_port = *((mach_port_t *) d->backend_data);
100 if (device_port != MACH_PORT_NULL)
103 snprintf(server, NAME_MAX, "%s/%04x/%02x/%02x/%01u/%s",
104 _SERVERS_BUS_PCI, d->domain, d->bus, d->dev, d->func,
106 device_port = file_name_lookup(server, 0, 0);
108 if (device_port == MACH_PORT_NULL)
109 d->access->error("Cannot find the PCI arbiter");
111 *((mach_port_t *) d->backend_data) = device_port;
115 /* Walk through the FS tree to see what is allowed for us */
117 enum_devices(const char *parent, struct pci_access *a, int domain, int bus,
118 int dev, int func, tree_level lev)
122 struct dirent *entry;
126 dir = opendir(parent);
129 if (errno == EPERM || errno == EACCES)
130 /* The client lacks the permissions to access this function, skip */
133 a->error("Cannot open directory: %s (%s)", parent, strerror(errno));
136 while ((entry = readdir(dir)) != 0)
138 snprintf(path, NAME_MAX, "%s/%s", parent, entry->d_name);
139 if (entry->d_type == DT_DIR)
141 if (!strncmp(entry->d_name, ".", NAME_MAX)
142 || !strncmp(entry->d_name, "..", NAME_MAX))
146 ret = strtol(entry->d_name, 0, 16);
149 if (closedir(dir) < 0)
150 a->warning("Cannot close directory: %s (%s)", parent,
152 a->error("Wrong directory name: %s (number expected) probably "
153 "not connected to an arbiter", entry->d_name);
157 * We found a valid directory.
158 * Update the address and switch to the next level.
175 if (closedir(dir) < 0)
176 a->warning("Cannot close directory: %s (%s)", parent,
178 a->error("Wrong directory tree, probably not connected to an arbiter");
181 enum_devices(path, a, domain, bus, dev, func, lev + 1);
185 if (strncmp(entry->d_name, FILE_CONFIG_NAME, NAME_MAX))
186 /* We are looking for the config file */
189 /* We found an available virtual device, add it to our list */
190 d = pci_alloc_dev(a);
199 if (closedir(dir) < 0)
200 a->error("Cannot close directory: %s (%s)", parent, strerror(errno));
203 /* Enumerate devices */
205 hurd_scan(struct pci_access *a)
207 enum_devices(_SERVERS_BUS_PCI, a, -1, -1, -1, -1, LEVEL_DOMAIN);
211 * Read `len' bytes to `buf'.
213 * Returns error when the number of read bytes does not match `len'.
216 hurd_read(struct pci_dev *d, int pos, byte * buf, int len)
221 mach_port_t device_port = device_port_lookup(d);
224 return pci_generic_block_read(d, pos, buf, len);
227 err = pci_conf_read(device_port, pos, &data, &nread, len);
229 if (data != (char *) buf)
231 if (nread > (size_t) len) /* Sanity check for bogus server. */
233 vm_deallocate(mach_task_self(), (vm_address_t) data, nread);
237 memcpy(buf, data, nread);
238 vm_deallocate(mach_task_self(), (vm_address_t) data, nread);
241 return !err && nread == (size_t) len;
245 * Write `len' bytes from `buf'.
247 * Returns error when the number of written bytes does not match `len'.
250 hurd_write(struct pci_dev *d, int pos, byte * buf, int len)
254 mach_port_t device_port = device_port_lookup(d);
257 return pci_generic_block_write(d, pos, buf, len);
259 err = pci_conf_write(device_port, pos, (char *) buf, len, &nwrote);
261 return !err && nwrote == (size_t) len;
264 /* Get requested info from the server */
267 hurd_fill_regions(struct pci_dev *d)
269 mach_port_t device_port = device_port_lookup(d);
270 struct pci_bar regions[6];
271 char *buf = (char *) ®ions;
272 size_t size = sizeof(regions);
274 int err = pci_get_dev_regions(device_port, &buf, &size);
278 if ((char *) ®ions != buf)
280 /* Sanity check for bogus server. */
281 if (size > sizeof(regions))
283 vm_deallocate(mach_task_self(), (vm_address_t) buf, size);
287 memcpy(®ions, buf, size);
288 vm_deallocate(mach_task_self(), (vm_address_t) buf, size);
291 for (int i = 0; i < 6; i++)
293 if (regions[i].size == 0)
296 d->base_addr[i] = regions[i].base_addr;
297 d->base_addr[i] |= regions[i].is_IO;
298 d->base_addr[i] |= regions[i].is_64 << 2;
299 d->base_addr[i] |= regions[i].is_prefetchable << 3;
301 d->size[i] = regions[i].size;
308 hurd_fill_rom(struct pci_dev *d)
310 struct pci_xrom_bar rom;
311 mach_port_t device_port = device_port_lookup(d);
312 char *buf = (char *) &rom;
313 size_t size = sizeof(rom);
315 int err = pci_get_dev_rom(device_port, &buf, &size);
319 if ((char *) &rom != buf)
321 /* Sanity check for bogus server. */
322 if (size > sizeof(rom))
324 vm_deallocate(mach_task_self(), (vm_address_t) buf, size);
328 memcpy(&rom, buf, size);
329 vm_deallocate(mach_task_self(), (vm_address_t) buf, size);
332 d->rom_base_addr = rom.base_addr;
333 d->rom_size = rom.size;
339 hurd_fill_info(struct pci_dev *d, unsigned int flags)
341 if (!d->access->buscentric)
343 if (want_fill(d, flags, PCI_FILL_BASES | PCI_FILL_SIZES))
345 if (hurd_fill_regions(d))
346 clear_fill(d, PCI_FILL_BASES | PCI_FILL_SIZES);
348 if (want_fill(d, flags, PCI_FILL_ROM_BASE))
350 if (hurd_fill_rom(d))
351 clear_fill(d, PCI_FILL_ROM_BASE);
355 pci_generic_fill_info(d, flags);
358 struct pci_methods pm_hurd = {
360 "Hurd access using RPCs",