summaryrefslogtreecommitdiffstats
path: root/protocols/property.c
blob: e8fec6e1e0935e7f1c012581d102d8f6e6f7a69c (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
#include <sys/time.h>
#include <arpa/inet.h>
#include <assert.h>

#include "pci.h"
#include "tools.h"
#include "model.h"
#include "error.h"


int pcilib_property_registers_read(pcilib_t *ctx, pcilib_register_bank_context_t *bank_ctx, pcilib_register_addr_t addr, pcilib_register_value_t *regval) {
    int err;

    const pcilib_register_bank_description_t *b = bank_ctx->bank;
    int access = b->access / 8;

    pcilib_view_t view = addr / access;
    pcilib_value_t val = {0};

    if (addr % access) {
        pcilib_error("Can't perform unaligned access to property register (the address is 0x%lx and alligment requirement is %u)", addr, access);
        return PCILIB_ERROR_INVALID_ARGUMENT;
    }

    if ((view == PCILIB_VIEW_INVALID)||(view >= ctx->num_views))
        return PCILIB_ERROR_INVALID_ARGUMENT;

    if ((ctx->views[view]->flags&PCILIB_VIEW_FLAG_REGISTER) == 0) {
        pcilib_error("Accessing invalid register %u (associated view %u does not provide register functionality)", addr, view);
        return PCILIB_ERROR_INVALID_REQUEST;
    }

    if ((ctx->views[view]->mode&PCILIB_ACCESS_R) == 0) {
        pcilib_error("Read access is not allowed to register %u (view %u)", addr, view);
        return PCILIB_ERROR_NOTPERMITED;
    }

    err = pcilib_get_property(ctx, ctx->views[view]->name, &val);
    if (err) return err;

    *regval = pcilib_get_value_as_register_value(ctx, &val, &err);

    return err;
}


int pcilib_property_registers_write(pcilib_t *ctx, pcilib_register_bank_context_t *bank_ctx, pcilib_register_addr_t addr, pcilib_register_value_t regval) {
    int err;

    const pcilib_register_bank_description_t *b = bank_ctx->bank;
    int access = b->access / 8;

    pcilib_view_t view = addr / access;
    pcilib_value_t val = {0};

    if (addr % access) {
        pcilib_error("Can't perform unaligned access to property register (the address is 0x%lx and alligment requirement is %u)", addr, access);
        return PCILIB_ERROR_INVALID_ARGUMENT;
    }

    if ((view == PCILIB_VIEW_INVALID)||(view >= ctx->num_views))
        return PCILIB_ERROR_INVALID_ARGUMENT;


    if ((ctx->views[view]->flags&PCILIB_VIEW_FLAG_REGISTER) == 0) {
        pcilib_error("Accessing invalid register %u (associated view %u does not provide register functionality)", addr, view);
        return PCILIB_ERROR_INVALID_REQUEST;
    }

    if ((ctx->views[view]->mode&PCILIB_ACCESS_W) == 0) {
        pcilib_error("Write access is not allowed to register %u (view %u)", addr, view);
        return PCILIB_ERROR_NOTPERMITED;
    }


    err = pcilib_set_value_from_register_value(ctx, &val, regval);
    if (err) return err;

    return pcilib_set_property(ctx, ctx->views[view]->name, &val);
}