summaryrefslogtreecommitdiff
path: root/src/hurd_pci.c
blob: 40c9925fb7b86a676fbe0ba88e5635f1b177bedb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
/*
 * Copyright (c) 2018, Damien Zammit
 * Copyright (c) 2017, Joan Lledó
 * Copyright (c) 2009, 2012 Samuel Thibault
 * Heavily inspired from the freebsd, netbsd, and openbsd backends
 * (C) Copyright Eric Anholt 2006
 * (C) Copyright IBM Corporation 2006
 * Copyright (c) 2008 Juan Romero Pardines
 * Copyright (c) 2008 Mark Kettenis
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#define _GNU_SOURCE
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <dirent.h>
#include <sys/mman.h>
#include <string.h>
#include <strings.h>
#include <mach.h>
#include <hurd.h>
#include <hurd/pci.h>
#include <hurd/paths.h>
#include <hurd/fs.h>
#include <device/device.h>

#include "x86_pci.h"
#include "pciaccess.h"
#include "pciaccess_private.h"

/*
 * Hurd PCI access using RPCs.
 *
 * Some functions are shared with the x86 module to avoid repeating code.
 */

/* Server path */
#define _SERVERS_BUS_PCI	_SERVERS_BUS "/pci"

/* File names */
#define FILE_CONFIG_NAME "config"
#define FILE_ROM_NAME "rom"

/* Level in the fs tree */
typedef enum {
    LEVEL_NONE,
    LEVEL_DOMAIN,
    LEVEL_BUS,
    LEVEL_DEV,
    LEVEL_FUNC
} tree_level;

struct pci_system_hurd {
    struct pci_system system;
};

static int
pci_device_hurd_probe(struct pci_device *dev)
{
    uint8_t irq;
    int err, i;
    struct pci_bar regions[6];
    struct pci_xrom_bar rom;
    struct pci_device_private *d;
    size_t size;
    char *buf;

    /* Many of the fields were filled in during initial device enumeration.
     * At this point, we need to fill in regions, rom_size, and irq.
     */

    err = pci_device_cfg_read_u8(dev, &irq, PCI_IRQ);
    if (err)
        return err;
    dev->irq = irq;

    /* Get regions */
    buf = (char *)&regions;
    size = sizeof(regions);
    d = (struct pci_device_private *)dev;
    err = pci_get_dev_regions(d->device_port, &buf, &size);
    if(err)
        return err;

    if((char*)&regions != buf)
    {
        /* Sanity check for bogus server.  */
        if(size > sizeof(regions))
        {
            vm_deallocate(mach_task_self(), (vm_address_t) buf, size);
            return EGRATUITOUS;
        }

        memcpy(&regions, buf, size);
        vm_deallocate(mach_task_self(), (vm_address_t) buf, size);
    }

    for(i=0; i<6; i++)
    {
        if(regions[i].size == 0)
            continue;

        dev->regions[i].base_addr = regions[i].base_addr;
        dev->regions[i].size = regions[i].size;
        dev->regions[i].is_IO = regions[i].is_IO;
        dev->regions[i].is_prefetchable = regions[i].is_prefetchable;
        dev->regions[i].is_64 = regions[i].is_64;
    }

    /* Get rom info */
    buf = (char *)&rom;
    size = sizeof(rom);
    err = pci_get_dev_rom(d->device_port, &buf, &size);
    if(err)
        return err;

    if((char*)&rom != buf)
    {
        /* Sanity check for bogus server.  */
        if(size > sizeof(rom))
        {
            vm_deallocate(mach_task_self(), (vm_address_t) buf, size);
            return EGRATUITOUS;
        }

        memcpy(&rom, buf, size);
        vm_deallocate(mach_task_self(), (vm_address_t) buf, size);
    }

    d->rom_base = rom.base_addr;
    dev->rom_size = rom.size;

    return 0;
}

/*
 * Read `nbytes' bytes from `reg' in device's configuretion space
 * and store them in `buf'.
 *
 * It's assumed that `nbytes' bytes are allocated in `buf'
 */
static int
pciclient_cfg_read(mach_port_t device_port, int reg, char *buf,
                   size_t * nbytes)
{
    int err;
    size_t nread;
    char *data;

    data = buf;
    nread = *nbytes;
    err = __pci_conf_read(device_port, reg, &data, &nread, *nbytes);
    if (err)
        return err;

    if (data != buf) {
        if (nread > *nbytes)	/* Sanity check for bogus server.  */ {
                vm_deallocate(mach_task_self(), (vm_address_t) data, nread);
                return EGRATUITOUS;
        }

        memcpy(buf, data, nread);
        vm_deallocate(mach_task_self(), (vm_address_t)data, nread);
    }

    *nbytes = nread;

    return 0;
}

/* Write `nbytes' bytes from `buf' to `reg' in device's configuration space */
static int
pciclient_cfg_write(mach_port_t device_port, int reg, char *buf,
                    size_t * nbytes)
{
    int err;
    size_t nwrote;

    err = __pci_conf_write(device_port, reg, buf, *nbytes, &nwrote);

    if (!err)
        *nbytes = nwrote;

    return err;
}

/*
 * Read up to `size' bytes from `dev' configuration space to `data' starting
 * at `offset'. Write the amount on read bytes in `bytes_read'.
 */
static int
pci_device_hurd_read(struct pci_device *dev, void *data,
    pciaddr_t offset, pciaddr_t size, pciaddr_t *bytes_read)
{
    int err;
    struct pci_device_private *d;

    *bytes_read = 0;
    d = (struct pci_device_private *)dev;
    while (size > 0) {
        size_t toread = 1 << (ffs(0x4 + (offset & 0x03)) - 1);
        if (toread > size)
            toread = size;

        err = pciclient_cfg_read(d->device_port, offset, (char*)data,
                                 &toread);
        if (err)
            return err;

        offset += toread;
        data = (char*)data + toread;
        size -= toread;
        *bytes_read += toread;
    }
    return 0;
}

/*
 * Write up to `size' bytes from `data' to `dev' configuration space starting
 * at `offset'. Write the amount on written bytes in `bytes_written'.
 */
static int
pci_device_hurd_write(struct pci_device *dev, const void *data,
    pciaddr_t offset, pciaddr_t size, pciaddr_t *bytes_written)
{
    int err;
    struct pci_device_private *d;

    *bytes_written = 0;
    d = (struct pci_device_private *)dev;
    while (size > 0) {
        size_t towrite = 4;
        if (towrite > size)
            towrite = size;
        if (towrite > 4 - (offset & 0x3))
            towrite = 4 - (offset & 0x3);

        err = pciclient_cfg_write(d->device_port, offset, (char*)data,
                                  &towrite);
        if (err)
            return err;

        offset += towrite;
        data = (const char*)data + towrite;
        size -= towrite;
        *bytes_written += towrite;
    }
    return 0;
}

/*
 * Copy the device's firmware in `buffer'
 */
static int
pci_device_hurd_read_rom(struct pci_device * dev, void * buffer)
{
    ssize_t rd;
    int romfd;
    char server[NAME_MAX];

    snprintf(server, NAME_MAX, "%s/%04x/%02x/%02x/%01u/%s", _SERVERS_BUS_PCI,
             dev->domain, dev->bus, dev->dev, dev->func, FILE_ROM_NAME);

    romfd = open(server, O_RDONLY | O_CLOEXEC);
    if (romfd == -1)
        return errno;

    rd = read(romfd, buffer, dev->rom_size);
    if (rd != dev->rom_size) {
        close(romfd);
        return errno;
    }

    close(romfd);

    return 0;
}

/*
 * Each device has its own server where send RPC's to.
 *
 * Deallocate the port before destroying the device.
 */
static void
pci_device_hurd_destroy_device(struct pci_device *dev)
{
    struct pci_device_private *d = (struct pci_device_private*) dev;

    mach_port_deallocate (mach_task_self (), d->device_port);
}

static struct dirent64 *
simple_readdir(mach_port_t port, uint32_t *first_entry)
{
    char *data;
    int nentries = 0;
    vm_size_t size;

    dir_readdir (port, &data, &size, *first_entry, 1, 0, &nentries);

    if (nentries == 0) {
        return NULL;
    }

    *first_entry = *first_entry + 1;
    return (struct dirent64 *)data;
}

/* Walk through the FS tree to see what is allowed for us */
static int
enum_devices(mach_port_t pci_port, const char *parent, int domain,
             int bus, int dev, int func, tree_level lev)
{
    int err, ret;
    struct dirent64 *entry = NULL;
    char path[NAME_MAX];
    char server[NAME_MAX];
    uint32_t reg, count = 0;
    size_t toread;
    mach_port_t cwd_port, device_port;
    struct pci_device_private *d, *devices;

    if (lev > LEVEL_FUNC + 1) {
        return 0;
    }
    cwd_port = file_name_lookup_under (pci_port, parent, O_DIRECTORY | O_RDWR | O_EXEC, 0);
    if (cwd_port == MACH_PORT_NULL) {
        return 0;
    }

    while ((entry = simple_readdir(cwd_port, &count)) != NULL) {
        snprintf(path, NAME_MAX, "%s/%s", parent, entry->d_name);
        if (entry->d_type == DT_DIR) {
            if (!strncmp(entry->d_name, ".", NAME_MAX)
                || !strncmp(entry->d_name, "..", NAME_MAX))
                continue;

            errno = 0;
            ret = strtol(entry->d_name, 0, 16);
            if (errno) {
                return errno;
            }

            /*
             * We found a valid directory.
             * Update the address and switch to the next level.
             */
            switch (lev) {
            case LEVEL_DOMAIN:
                domain = ret;
                break;
            case LEVEL_BUS:
                bus = ret;
                break;
            case LEVEL_DEV:
                dev = ret;
                break;
            case LEVEL_FUNC:
                func = ret;
                break;
            default:
                return 0;
            }

            err = enum_devices(pci_port, path, domain, bus, dev, func, lev+1);
            if (err && err != EPERM && err != EACCES) {
                return 0;
            }
        } else {
            if (strncmp(entry->d_name, FILE_CONFIG_NAME, NAME_MAX))
                /* We are looking for the config file */
                continue;

            /* We found an available virtual device, add it to our list */
            snprintf(server, NAME_MAX, "./%04x/%02x/%02x/%01u/%s",
                     domain, bus, dev, func,
                     entry->d_name);
            device_port = file_name_lookup_under(pci_port, server, O_RDWR, 0);
            if (device_port == MACH_PORT_NULL) {
                return 0;
            }

            toread = sizeof(reg);
            err = pciclient_cfg_read(device_port, PCI_VENDOR_ID, (char*)&reg,
                                     &toread);
            if (err) {
                mach_port_deallocate (mach_task_self (), device_port);
                return err;
            }
            if (toread != sizeof(reg)) {
                mach_port_deallocate (mach_task_self (), device_port);
                return -1;
            }

            devices = realloc(pci_sys->devices, (pci_sys->num_devices + 1)
                              * sizeof(struct pci_device_private));
            if (!devices) {
                mach_port_deallocate (mach_task_self (), device_port);
                return ENOMEM;
            }

            d = devices + pci_sys->num_devices;
            memset(d, 0, sizeof(struct pci_device_private));

            d->base.domain = domain;
            d->base.bus = bus;
            d->base.dev = dev;
            d->base.func = func;
            d->base.vendor_id = PCI_VENDOR(reg);
            d->base.device_id = PCI_DEVICE(reg);

            toread = sizeof(reg);
            err = pciclient_cfg_read(device_port, PCI_CLASS, (char*)&reg,
                                     &toread);
            if (err) {
                mach_port_deallocate (mach_task_self (), device_port);
                return err;
            }
            if (toread != sizeof(reg)) {
                mach_port_deallocate (mach_task_self (), device_port);
                return -1;
            }

            d->base.device_class = reg >> 8;
            d->base.revision = reg & 0xFF;

            toread = sizeof(reg);
            err = pciclient_cfg_read(device_port, PCI_SUB_VENDOR_ID,
                                     (char*)&reg, &toread);
            if (err) {
                mach_port_deallocate (mach_task_self (), device_port);
                return err;
            }
            if (toread != sizeof(reg)) {
                mach_port_deallocate (mach_task_self (), device_port);
                return -1;
            }

            d->base.subvendor_id = PCI_VENDOR(reg);
            d->base.subdevice_id = PCI_DEVICE(reg);

            d->device_port = device_port;

            pci_sys->devices = devices;
            pci_sys->num_devices++;
        }
    }

    return 0;
}

static const struct pci_system_methods hurd_pci_methods = {
    .destroy = pci_system_x86_destroy,
    .destroy_device = pci_device_hurd_destroy_device,
    .read_rom = pci_device_hurd_read_rom,
    .probe = pci_device_hurd_probe,
    .map_range = pci_device_x86_map_range,
    .unmap_range = pci_device_x86_unmap_range,
    .read = pci_device_hurd_read,
    .write = pci_device_hurd_write,
    .fill_capabilities = pci_fill_capabilities_generic,
    .open_legacy_io = pci_device_x86_open_legacy_io,
    .close_io = pci_device_x86_close_io,
    .read32 = pci_device_x86_read32,
    .read16 = pci_device_x86_read16,
    .read8 = pci_device_x86_read8,
    .write32 = pci_device_x86_write32,
    .write16 = pci_device_x86_write16,
    .write8 = pci_device_x86_write8,
    .map_legacy = pci_device_x86_map_legacy,
    .unmap_legacy = pci_device_x86_unmap_legacy,
};

/* Get the name of the server using libpciaccess if any */
extern char *netfs_server_name;
#pragma weak netfs_server_name

_pci_hidden int
pci_system_hurd_create(void)
{
    int err;
    struct pci_system_hurd *pci_sys_hurd;
    mach_port_t device_master, pci_port;
    mach_port_t root = MACH_PORT_NULL;

    if (&netfs_server_name && netfs_server_name
        && !strcmp(netfs_server_name, "pci-arbiter")) {
      /* We are a PCI arbiter, try the x86 way */
      err = pci_system_x86_create();
      if (!err)
          return 0;
    }

    /*
     * From this point on, we are either a client or a nested arbiter.
     * Both will connect to a master arbiter.
     */

    pci_sys_hurd = calloc(1, sizeof(struct pci_system_hurd));
    if (pci_sys_hurd == NULL) {
        x86_disable_io();
        return ENOMEM;
    }
    pci_sys = &pci_sys_hurd->system;

    pci_sys->methods = &hurd_pci_methods;

    pci_sys->num_devices = 0;

    if ((err = get_privileged_ports (NULL, &device_master)) || (device_master == MACH_PORT_NULL)) {
        pci_system_cleanup();
        return err;
    }

    err = device_open (device_master, D_READ|D_WRITE, "pci", &pci_port);
    if (!err) {
        root = file_name_lookup_under (pci_port, ".", O_DIRECTORY | O_RDWR | O_EXEC, 0);
    }

    if (!root) {
        root = file_name_lookup (_SERVERS_BUS_PCI, O_RDWR, 0);
    }

    if (!root) {
        pci_system_cleanup();
        return errno;
    }

    err = enum_devices (root, ".", -1, -1, -1, -1, LEVEL_DOMAIN);
    if (err) {
        pci_system_cleanup();
        return err;
    }

    return 0;
}