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));
78 /* Deallocate the port and free its space */
80 hurd_cleanup_dev(struct pci_dev *d)
82 mach_port_t device_port;
84 device_port = *((mach_port_t *) d->aux);
85 mach_port_deallocate(mach_task_self(), device_port);
90 /* Walk through the FS tree to see what is allowed for us */
92 enum_devices(const char *parent, struct pci_access *a, int domain, int bus,
93 int dev, int func, tree_level lev)
99 char server[NAME_MAX];
102 mach_port_t device_port;
105 dir = opendir(parent);
108 if (errno == EPERM || errno == EACCES)
109 /* The client lacks the permissions to access this function, skip */
112 a->error("Cannot open directory: %s (%s)", parent, strerror(errno));
115 while ((entry = readdir(dir)) != 0)
117 snprintf(path, NAME_MAX, "%s/%s", parent, entry->d_name);
118 if (entry->d_type == DT_DIR)
120 if (!strncmp(entry->d_name, ".", NAME_MAX)
121 || !strncmp(entry->d_name, "..", NAME_MAX))
125 ret = strtol(entry->d_name, 0, 16);
128 if (closedir(dir) < 0)
129 a->warning("Cannot close directory: %s (%s)", parent,
131 a->error("Wrong directory name: %s (number expected) probably \
132 not connected to an arbiter", entry->d_name);
136 * We found a valid directory.
137 * Update the address and switch to the next level.
154 if (closedir(dir) < 0)
155 a->warning("Cannot close directory: %s (%s)", parent,
157 a->error("Wrong directory tree, probably not connected \
161 enum_devices(path, a, domain, bus, dev, func, lev + 1);
165 if (strncmp(entry->d_name, FILE_CONFIG_NAME, NAME_MAX))
166 /* We are looking for the config file */
169 /* We found an available virtual device, add it to our list */
170 snprintf(server, NAME_MAX, "%s/%04x/%02x/%02x/%01u/%s",
171 _SERVERS_BUS_PCI, domain, bus, dev, func, entry->d_name);
172 device_port = file_name_lookup(server, 0, 0);
173 if (device_port == MACH_PORT_NULL)
175 if (closedir(dir) < 0)
176 a->warning("Cannot close directory: %s (%s)", parent,
178 a->error("Cannot open %s", server);
181 d = pci_alloc_dev(a);
182 *((mach_port_t *) d->aux) = device_port;
188 vd = pci_read_long(d, PCI_VENDOR_ID);
189 ht = pci_read_byte(d, PCI_HEADER_TYPE);
191 d->vendor_id = vd & 0xffff;
192 d->device_id = vd >> 16U;
193 d->known_fields = PCI_FILL_IDENT;
198 if (closedir(dir) < 0)
199 a->error("Cannot close directory: %s (%s)", parent, strerror(errno));
202 /* Enumerate devices */
204 hurd_scan(struct pci_access *a)
206 enum_devices(_SERVERS_BUS_PCI, a, -1, -1, -1, -1, LEVEL_DOMAIN);
210 * Read `len' bytes to `buf'.
212 * Returns error when the number of read bytes does not match `len'.
215 hurd_read(struct pci_dev *d, int pos, byte * buf, int len)
220 mach_port_t device_port;
223 device_port = *((mach_port_t *) d->aux);
225 err = !pci_generic_block_read(d, pos, buf, nread);
229 err = pci_conf_read(device_port, pos, &data, &nread, len);
231 if (data != (char *) buf)
233 if (nread > (size_t) len) /* Sanity check for bogus server. */
235 vm_deallocate(mach_task_self(), (vm_address_t) data, nread);
239 memcpy(buf, data, nread);
240 vm_deallocate(mach_task_self(), (vm_address_t) data, nread);
246 return nread == (size_t) len;
250 * Write `len' bytes from `buf'.
252 * Returns error when the number of written bytes does not match `len'.
255 hurd_write(struct pci_dev *d, int pos, byte * buf, int len)
259 mach_port_t device_port;
262 device_port = *((mach_port_t *) d->aux);
264 err = !pci_generic_block_write(d, pos, buf, len);
266 err = pci_conf_write(device_port, pos, (char *) buf, len, &nwrote);
270 return nwrote == (size_t) len;
273 /* Get requested info from the server */
276 hurd_fill_regions(struct pci_dev *d)
278 mach_port_t device_port = *((mach_port_t *) d->aux);
279 struct pci_bar regions[6];
280 char *buf = (char *) ®ions;
281 size_t size = sizeof(regions);
283 int err = pci_get_dev_regions(device_port, &buf, &size);
287 if ((char *) ®ions != buf)
289 /* Sanity check for bogus server. */
290 if (size > sizeof(regions))
292 vm_deallocate(mach_task_self(), (vm_address_t) buf, size);
296 memcpy(®ions, buf, size);
297 vm_deallocate(mach_task_self(), (vm_address_t) buf, size);
300 for (int i = 0; i < 6; i++)
302 if (regions[i].size == 0)
305 d->base_addr[i] = regions[i].base_addr;
306 d->base_addr[i] |= regions[i].is_IO;
307 d->base_addr[i] |= regions[i].is_64 << 2;
308 d->base_addr[i] |= regions[i].is_prefetchable << 3;
310 if (flags & PCI_FILL_SIZES)
311 d->size[i] = regions[i].size;
316 hurd_fill_rom(struct pci_dev *d)
318 struct pci_xrom_bar rom;
319 mach_port_t device_port = *((mach_port_t *) d->aux);
320 char *buf = (char *) &rom;
321 size_t size = sizeof(rom);
323 int err = pci_get_dev_rom(device_port, &buf, &size);
327 if ((char *) &rom != buf)
329 /* Sanity check for bogus server. */
330 if (size > sizeof(rom))
332 vm_deallocate(mach_task_self(), (vm_address_t) buf, size);
336 memcpy(&rom, buf, size);
337 vm_deallocate(mach_task_self(), (vm_address_t) buf, size);
340 d->rom_base_addr = rom.base_addr;
341 d->rom_size = rom.size;
345 hurd_fill_info(struct pci_dev *d, unsigned int flags)
347 unsigned int done = 0;
349 if (!d->access->buscentric)
351 if (flags & (PCI_FILL_BASES | PCI_FILL_SIZES))
353 hurd_fill_regions(d);
354 done |= PCI_FILL_BASES | PCI_FILL_SIZES;
356 if (flags & PCI_FILL_ROM_BASE)
359 done |= PCI_FILL_ROM_BASE;
363 return done | pci_generic_fill_info(d, flags & ~done);
366 struct pci_methods pm_hurd = {
368 "Hurd access using RPCs",