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.
15 #include <sys/types.h>
23 #include <hurd/paths.h>
26 #define _SERVERS_BUS_PCI _SERVERS_BUS "/pci"
29 #define FILE_CONFIG_NAME "config"
30 #define FILE_ROM_NAME "rom"
32 /* Level in the fs tree */
42 /* Check whether there's a pci server */
44 hurd_detect(struct pci_access *a)
49 err = stat(_SERVERS_BUS_PCI, &st);
52 a->error("Could not open file `%s'", _SERVERS_BUS_PCI);
56 /* The node must be a directory and a translator */
57 return S_ISDIR(st.st_mode) && ((st.st_mode & S_ITRANS) == S_IROOT);
60 /* Empty callbacks, we don't need any special init or cleanup */
62 hurd_init(struct pci_access *a UNUSED)
67 hurd_cleanup(struct pci_access *a UNUSED)
71 /* Each device has its own server path. Allocate space for the port. */
73 hurd_init_dev(struct pci_dev *d)
75 d->aux = pci_malloc(d->access, sizeof(mach_port_t));
76 *((mach_port_t *) d->aux) = MACH_PORT_NULL;
79 /* Deallocate the port and free its space */
81 hurd_cleanup_dev(struct pci_dev *d)
83 mach_port_t device_port;
85 device_port = *((mach_port_t *) d->aux);
86 mach_port_deallocate(mach_task_self(), device_port);
92 device_port_lookup(struct pci_dev *d)
94 char server[NAME_MAX];
95 mach_port_t device_port = *((mach_port_t *) d->aux);
97 if (device_port != MACH_PORT_NULL)
100 snprintf(server, NAME_MAX, "%s/%04x/%02x/%02x/%01u/%s",
101 _SERVERS_BUS_PCI, d->domain, d->bus, d->dev, d->func,
103 device_port = file_name_lookup(server, 0, 0);
105 *((mach_port_t *) d->aux) = device_port;
109 /* Walk through the FS tree to see what is allowed for us */
111 enum_devices(const char *parent, struct pci_access *a, int domain, int bus,
112 int dev, int func, tree_level lev)
116 struct dirent *entry;
122 dir = opendir(parent);
125 if (errno == EPERM || errno == EACCES)
126 /* The client lacks the permissions to access this function, skip */
129 a->error("Cannot open directory: %s (%s)", parent, strerror(errno));
132 while ((entry = readdir(dir)) != 0)
134 snprintf(path, NAME_MAX, "%s/%s", parent, entry->d_name);
135 if (entry->d_type == DT_DIR)
137 if (!strncmp(entry->d_name, ".", NAME_MAX)
138 || !strncmp(entry->d_name, "..", NAME_MAX))
142 ret = strtol(entry->d_name, 0, 16);
145 if (closedir(dir) < 0)
146 a->warning("Cannot close directory: %s (%s)", parent,
148 a->error("Wrong directory name: %s (number expected) probably "
149 "not connected to an arbiter", entry->d_name);
153 * We found a valid directory.
154 * Update the address and switch to the next level.
171 if (closedir(dir) < 0)
172 a->warning("Cannot close directory: %s (%s)", parent,
174 a->error("Wrong directory tree, probably not connected to an arbiter");
177 enum_devices(path, a, domain, bus, dev, func, lev + 1);
181 if (strncmp(entry->d_name, FILE_CONFIG_NAME, NAME_MAX))
182 /* We are looking for the config file */
185 /* We found an available virtual device, add it to our list */
186 d = pci_alloc_dev(a);
192 /* Get the arbiter port */
193 if (device_port_lookup(d) == MACH_PORT_NULL)
195 if (closedir(dir) < 0)
196 a->warning("Cannot close directory: %s (%s)", parent,
198 a->error("Cannot find the PCI arbiter");
203 vd = pci_read_long(d, PCI_VENDOR_ID);
204 ht = pci_read_byte(d, PCI_HEADER_TYPE);
206 d->vendor_id = vd & 0xffff;
207 d->device_id = vd >> 16U;
208 d->known_fields = PCI_FILL_IDENT;
213 if (closedir(dir) < 0)
214 a->error("Cannot close directory: %s (%s)", parent, strerror(errno));
217 /* Enumerate devices */
219 hurd_scan(struct pci_access *a)
221 enum_devices(_SERVERS_BUS_PCI, a, -1, -1, -1, -1, LEVEL_DOMAIN);
225 * Read `len' bytes to `buf'.
227 * Returns error when the number of read bytes does not match `len'.
230 hurd_read(struct pci_dev *d, int pos, byte * buf, int len)
235 mach_port_t device_port;
238 return pci_generic_block_read(d, pos, buf, nread);
240 device_port = device_port_lookup(d);
241 if (device_port == MACH_PORT_NULL)
242 d->access->error("Cannot find the PCI arbiter");
246 err = pci_conf_read(device_port, pos, &data, &nread, len);
248 if (data != (char *) buf)
250 if (nread > (size_t) len) /* Sanity check for bogus server. */
252 vm_deallocate(mach_task_self(), (vm_address_t) data, nread);
256 memcpy(buf, data, nread);
257 vm_deallocate(mach_task_self(), (vm_address_t) data, nread);
260 return !err && nread == (size_t) len;
264 * Write `len' bytes from `buf'.
266 * Returns error when the number of written bytes does not match `len'.
269 hurd_write(struct pci_dev *d, int pos, byte * buf, int len)
273 mach_port_t device_port;
276 return pci_generic_block_write(d, pos, buf, len);
278 device_port = device_port_lookup(d);
279 if (device_port == MACH_PORT_NULL)
280 d->access->error("Cannot find the PCI arbiter");
283 err = pci_conf_write(device_port, pos, (char *) buf, len, &nwrote);
285 return !err && nwrote == (size_t) len;
288 /* Get requested info from the server */
291 hurd_fill_regions(struct pci_dev *d)
293 mach_port_t device_port = *((mach_port_t *) d->aux);
294 struct pci_bar regions[6];
295 char *buf = (char *) ®ions;
296 size_t size = sizeof(regions);
298 int err = pci_get_dev_regions(device_port, &buf, &size);
302 if ((char *) ®ions != buf)
304 /* Sanity check for bogus server. */
305 if (size > sizeof(regions))
307 vm_deallocate(mach_task_self(), (vm_address_t) buf, size);
311 memcpy(®ions, buf, size);
312 vm_deallocate(mach_task_self(), (vm_address_t) buf, size);
315 for (int i = 0; i < 6; i++)
317 if (regions[i].size == 0)
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;
325 d->size[i] = regions[i].size;
330 hurd_fill_rom(struct pci_dev *d)
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);
337 int err = pci_get_dev_rom(device_port, &buf, &size);
341 if ((char *) &rom != buf)
343 /* Sanity check for bogus server. */
344 if (size > sizeof(rom))
346 vm_deallocate(mach_task_self(), (vm_address_t) buf, size);
350 memcpy(&rom, buf, size);
351 vm_deallocate(mach_task_self(), (vm_address_t) buf, size);
354 d->rom_base_addr = rom.base_addr;
355 d->rom_size = rom.size;
359 hurd_fill_info(struct pci_dev *d, unsigned int flags)
361 unsigned int done = 0;
363 if (!d->access->buscentric)
365 if (flags & (PCI_FILL_BASES | PCI_FILL_SIZES))
367 hurd_fill_regions(d);
368 done |= PCI_FILL_BASES | PCI_FILL_SIZES;
370 if (flags & PCI_FILL_ROM_BASE)
373 done |= PCI_FILL_ROM_BASE;
377 return done | pci_generic_fill_info(d, flags & ~done);
380 struct pci_methods pm_hurd = {
382 "Hurd access using RPCs",