aboutsummaryrefslogtreecommitdiff
path: root/lib/pci_caps.c
blob: f6a3abce174b9594af1780c97a26ab38b882d625 (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
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
/*
 * Copyright (c) 2021 Nutanix Inc. All rights reserved.
 *
 * Authors: Thanos Makatos <thanos@nutanix.com>
 *          Swapnil Ingle <swapnil.ingle@nutanix.com>
 *          Felipe Franciosi <felipe@nutanix.com>
 *          John Levon <john.levon@nutanix.com>
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *      * Redistributions of source code must retain the above copyright
 *        notice, this list of conditions and the following disclaimer.
 *      * Redistributions in binary form must reproduce the above copyright
 *        notice, this list of conditions and the following disclaimer in the
 *        documentation and/or other materials provided with the distribution.
 *      * Neither the name of Nutanix nor the names of its contributors may be
 *        used to endorse or promote products derived from this software without
 *        specific prior written permission.
 *
 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 *  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 *  ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
 *  DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 *  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 *  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 *  OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
 *  DAMAGE.
 *
 */

/*
 * Capability handling. We handle reads and writes to standard capabilities
 * ourselves, and optionally for vendor capabilities too. For each access (via
 * pci_config_space_access() -> pci_cap_access()), if we find that we're
 * reading from a particular capability offset:
 *
 * - if VFU_CAP_FLAG_CALLBACK is set, we call the config space region callback
 *   given by the user
 * - else we memcpy() the capability data back out to the client
 *
 * For writes:
 *
 * - if VFU_CAP_FLAG_READONLY is set, we fail the write
 * - if VFU_CAP_FLAG_CALLBACK is set, we call the config space region callback
 *   given by the user
 * - else we call the cap-specific callback to handle the write.
 *
 * Extended capabilities live in extended space (after the first 256 bytes), so
 * can never clash with a standard capability. An empty capability list is
 * signalled by a zeroed header at offset 256 (which the config space has by
 * default).
 */

#include <assert.h>
#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
#include <stddef.h>
#include <string.h>

#include "common.h"
#include "libvfio-user.h"
#include "pci_caps.h"
#include "pci.h"
#include "private.h"

/* All capabilities must be dword-aligned. */
#define CAP_ROUND (4)

static void *
cap_data(vfu_ctx_t *vfu_ctx, struct pci_cap *cap)
{
    return (void *)pci_config_space_ptr(vfu_ctx, cap->off);
}

static size_t
cap_size(vfu_ctx_t *vfu_ctx, void *data, bool extended)
{
    if (extended) {
        uint16_t id = ((struct pcie_ext_cap_hdr *)data)->id;

        switch (id) {
        case PCI_EXT_CAP_ID_DSN:
            return PCI_EXT_CAP_DSN_SIZEOF;
        case PCI_EXT_CAP_ID_VNDR:
            return ((struct pcie_ext_cap_vsc_hdr *)data)->len;
        default:
            vfu_log(vfu_ctx, LOG_ERR, "invalid cap id %u", id);
            abort();
        }
    } else {
        uint8_t id = ((struct cap_hdr *)data)->id;

        switch (id) {
        case PCI_CAP_ID_PM:
            return PCI_PM_SIZEOF;
        case PCI_CAP_ID_EXP:
            return VFIO_USER_PCI_CAP_EXP_SIZEOF;
        case PCI_CAP_ID_MSIX:
            return PCI_CAP_MSIX_SIZEOF;
        case PCI_CAP_ID_VNDR:
            return ((struct vsc *)data)->size;
        default:
            vfu_log(vfu_ctx, LOG_ERR, "invalid cap id %u", id);
            abort();
        }
    }
}

static ssize_t
handle_pmcs_write(vfu_ctx_t *vfu_ctx, struct pmcap *pm,
                  const struct pmcs *const pmcs)
{
    if (pm->pmcs.ps != pmcs->ps) {
        vfu_log(vfu_ctx, LOG_DEBUG, "power state set to %#x", pmcs->ps);
    }
    if (pm->pmcs.pmee != pmcs->pmee) {
        vfu_log(vfu_ctx, LOG_DEBUG, "PME enable set to %#x", pmcs->pmee);
    }
    if (pm->pmcs.dse != pmcs->dse) {
        vfu_log(vfu_ctx, LOG_DEBUG, "data select set to %#x", pmcs->dse);
    }
    if (pm->pmcs.pmes != pmcs->pmes) {
        vfu_log(vfu_ctx, LOG_DEBUG, "PME status set to %#x", pmcs->pmes);
    }
    pm->pmcs = *pmcs;
    return 0;
}

static ssize_t
cap_write_pm(vfu_ctx_t *vfu_ctx, struct pci_cap *cap, char * buf,
             size_t count, loff_t offset)
{
    struct pmcap *pm = cap_data(vfu_ctx, cap);

    switch (offset - cap->off) {
    case offsetof(struct pmcap, pc):
        if (count != sizeof(struct pc)) {
            return ERROR_INT(EINVAL);
        }
        vfu_log(vfu_ctx, LOG_ERR, "FIXME: write to pmcap::pc unimplemented");
        return ERROR_INT(ENOTSUP);
    case offsetof(struct pmcap, pmcs):
        if (count != sizeof(struct pmcs)) {
            return ERROR_INT(EINVAL);
        }
        handle_pmcs_write(vfu_ctx, pm, (struct pmcs *)buf);
        return sizeof(struct pmcs);
    case offsetof(struct pmcap, pmcsr_bse):
        if (count != 1) {
            return ERROR_INT(EINVAL);
        }
        vfu_log(vfu_ctx, LOG_ERR,
                "FIXME: write to pmcap::pmcsr_bse unimplemented");
        return ERROR_INT(ENOTSUP);
    case offsetof(struct pmcap, data):
        if (count != 1) {
            return ERROR_INT(EINVAL);
        }
        vfu_log(vfu_ctx, LOG_ERR, "FIXME: write to pmcap::data unimplemented");
        return ERROR_INT(ENOTSUP);
    }
    return ERROR_INT(EINVAL);
}

static ssize_t
handle_mxc_write(vfu_ctx_t *vfu_ctx, struct msixcap *msix,
                 const struct mxc *const mxc)
{
    assert(msix != NULL);
    assert(mxc != NULL);

    if (mxc->mxe != msix->mxc.mxe) {
        vfu_log(vfu_ctx, LOG_DEBUG, "%s MSI-X",
                mxc->mxe ? "enable" : "disable");
        msix->mxc.mxe = mxc->mxe;
    }

    if (mxc->fm != msix->mxc.fm) {
        if (mxc->fm) {
            vfu_log(vfu_ctx, LOG_DEBUG, "all MSI-X vectors masked");
        } else {
            vfu_log(vfu_ctx, LOG_DEBUG,
                   "vector's mask bit determines whether vector is masked");
        }
        msix->mxc.fm = mxc->fm;
    }

    return sizeof(struct mxc);
}

static ssize_t
cap_write_msix(vfu_ctx_t *vfu_ctx, struct pci_cap *cap, char *buf,
               size_t count, loff_t offset)
{
    struct msixcap *msix = cap_data(vfu_ctx, cap);

    if (count == sizeof(struct mxc)) {
        switch (offset - cap->off) {
        case offsetof(struct msixcap, mxc):
            return handle_mxc_write(vfu_ctx, msix, (struct mxc *)buf);
        default:
            vfu_log(vfu_ctx, LOG_ERR,
                    "invalid MSI-X write offset %ld", offset);
            return ERROR_INT(EINVAL);
        }
    }
    vfu_log(vfu_ctx, LOG_ERR, "invalid MSI-X write size %lu", count);
    return ERROR_INT(EINVAL);
}

static int
handle_px_pxdc_write(vfu_ctx_t *vfu_ctx, struct pxcap *px,
                     const union pxdc *const p)
{
    assert(px != NULL);
    assert(p != NULL);

    if (p->cere != px->pxdc.cere) {
        px->pxdc.cere = p->cere;
        vfu_log(vfu_ctx, LOG_DEBUG, "CERE %s", p->cere ? "enable" : "disable");
    }

    if (p->nfere != px->pxdc.nfere) {
        px->pxdc.nfere = p->nfere;
        vfu_log(vfu_ctx, LOG_DEBUG, "NFERE %s",
                p->nfere ? "enable" : "disable");
    }

    if (p->fere != px->pxdc.fere) {
        px->pxdc.fere = p->fere;
        vfu_log(vfu_ctx, LOG_DEBUG, "FERE %s", p->fere ? "enable" : "disable");
    }

    if (p->urre != px->pxdc.urre) {
        px->pxdc.urre = p->urre;
        vfu_log(vfu_ctx, LOG_DEBUG, "URRE %s", p->urre ? "enable" : "disable");
    }

    if (p->ero != px->pxdc.ero) {
        px->pxdc.ero = p->ero;
        vfu_log(vfu_ctx, LOG_DEBUG, "ERO %s", p->ero ? "enable" : "disable");
    }

    if (p->mps != px->pxdc.mps) {
        px->pxdc.mps = p->mps;
        vfu_log(vfu_ctx, LOG_DEBUG, "MPS set to %d", p->mps);
    }

    if (p->ete != px->pxdc.ete) {
        px->pxdc.ete = p->ete;
        vfu_log(vfu_ctx, LOG_DEBUG, "ETE %s", p->ete ? "enable" : "disable");
    }

    if (p->pfe != px->pxdc.pfe) {
        px->pxdc.pfe = p->pfe;
        vfu_log(vfu_ctx, LOG_DEBUG, "PFE %s", p->pfe ? "enable" : "disable");
    }

    if (p->appme != px->pxdc.appme) {
        px->pxdc.appme = p->appme;
        vfu_log(vfu_ctx, LOG_DEBUG, "APPME %s",
                p->appme ? "enable" : "disable");
    }

    if (p->ens != px->pxdc.ens) {
        px->pxdc.ens = p->ens;
        vfu_log(vfu_ctx, LOG_DEBUG, "ENS %s", p->ens ? "enable" : "disable");
    }

    if (p->mrrs != px->pxdc.mrrs) {
        px->pxdc.mrrs = p->mrrs;
        vfu_log(vfu_ctx, LOG_DEBUG, "MRRS set to %d", p->mrrs);
    }

    if (p->iflr) {
        if (px->pxdcap.flrc == 0) {
            vfu_log(vfu_ctx, LOG_ERR, "FLR capability is not supported");
            return ERROR_INT(EINVAL);
        }
        if (vfu_ctx->reset != NULL) {
            vfu_log(vfu_ctx, LOG_DEBUG, "initiate function level reset");
            return vfu_ctx->reset(vfu_ctx, VFU_RESET_PCI_FLR);
        } else {
            vfu_log(vfu_ctx, LOG_ERR, "FLR callback is not implemented");
        }
    }

    return 0;
}

static int
handle_px_write_2_bytes(vfu_ctx_t *vfu_ctx, struct pxcap *px, char *buf,
                        loff_t off)
{
    switch (off) {
    case offsetof(struct pxcap, pxdc):
        return handle_px_pxdc_write(vfu_ctx, px, (union pxdc *)buf);
    }
    return ERROR_INT(EINVAL);
}

static ssize_t
cap_write_px(vfu_ctx_t *vfu_ctx, struct pci_cap *cap, char *buf,
             size_t count, loff_t offset)
{
    struct pxcap *px = cap_data(vfu_ctx, cap);
    int err;

    switch (count) {
    case 2:
        err = handle_px_write_2_bytes(vfu_ctx, px, buf, offset - cap->off);
        break;
    default:
        err = ERROR_INT(EINVAL);
        break;
    }
    if (err != 0) {
        return err;
    }
    return count;
}

static ssize_t
cap_write_vendor(vfu_ctx_t *vfu_ctx, struct pci_cap *cap UNUSED, char *buf,
                 size_t count, loff_t offset)
{
    memcpy(pci_config_space_ptr(vfu_ctx, offset), buf, count);
    return count;
}

static ssize_t
ext_cap_write_dsn(vfu_ctx_t *vfu_ctx, struct pci_cap *cap, char *buf UNUSED,
                  size_t count UNUSED, loff_t offset UNUSED)
{
    vfu_log(vfu_ctx, LOG_ERR, "%s capability is read-only", cap->name);
    return ERROR_INT(EPERM);
}

static ssize_t
ext_cap_write_vendor(vfu_ctx_t *vfu_ctx, struct pci_cap *cap UNUSED, char *buf,
                     size_t count, loff_t offset)
{
    memcpy(pci_config_space_ptr(vfu_ctx, offset), buf, count);
    return count;
}

static bool
ranges_intersect(size_t off1, size_t size1, size_t off2, size_t size2)
{
    return (off1 < (off2 + size2) && (off1 + size1) >= off2);
}

struct pci_cap *
cap_find_by_offset(vfu_ctx_t *vfu_ctx, loff_t offset, size_t count)
{
    size_t i;

    for (i = 0; i < vfu_ctx->pci.nr_caps; i++) {
        struct pci_cap *cap = &vfu_ctx->pci.caps[i];
        if (ranges_intersect(offset, count, cap->off, cap->size)) {
            return cap;
        }
    }

    for (i = 0; i < vfu_ctx->pci.nr_ext_caps; i++) {
        struct pci_cap *cap = &vfu_ctx->pci.ext_caps[i];
        if (ranges_intersect(offset, count, cap->off, cap->size)) {
            return cap;
        }
    }
    return NULL;
}

ssize_t
pci_cap_access(vfu_ctx_t *vfu_ctx, char *buf, size_t count, loff_t offset,
               bool is_write)
{
    struct pci_cap *cap = cap_find_by_offset(vfu_ctx, offset, count);

    assert(cap != NULL);
    assert((size_t)offset >= cap->off);
    assert(count <= cap->size);

    if (is_write && (cap->flags & VFU_CAP_FLAG_READONLY)) {
        vfu_log(vfu_ctx, LOG_ERR, "write of %zu bytes to read-only capability "
                "%u (%s)", count, cap->id, cap->name);
        return ERROR_INT(EPERM);
    }

    if (cap->flags & VFU_CAP_FLAG_CALLBACK) {
        return pci_nonstd_access(vfu_ctx, buf, count, offset, is_write);
    }

    if (!is_write) {
        memcpy(buf, pci_config_space_ptr(vfu_ctx, offset), count);
        return count;
    }

    if (offset - cap->off < cap->hdr_size) {
        vfu_log(vfu_ctx, LOG_ERR,
                "disallowed write to header for cap %d (%s)",
                cap->id, cap->name);
        return ERROR_INT(EPERM);
    }

    return cap->cb(vfu_ctx, cap, buf, count, offset);
}

/*
 * Place the new capability after the previous (or after the standard header if
 * this is the first capability).
 *
 * If cap->off is already provided, place it directly, but first check it
 * doesn't overlap an existing capability, or the PCI header. We still also need
 * to link it into the list. There's no guarantee that the list is ordered by
 * offset after doing so.
 */
static int
cap_place(vfu_ctx_t *vfu_ctx, struct pci_cap *cap, void *data)
{
    vfu_pci_config_space_t *config_space;
    uint8_t *prevp = NULL;
    size_t offset;

    config_space = vfu_pci_get_config_space(vfu_ctx);

    prevp = &config_space->hdr.cap;

    if (cap->off != 0) {
        if (cap->off < PCI_STD_HEADER_SIZEOF) {
            vfu_log(vfu_ctx, LOG_ERR, "invalid offset %#lx for capability "
                    "%u (%s)", cap->off, cap->id, cap->name);
            return ERROR_INT(EINVAL);
        }

        if (cap_find_by_offset(vfu_ctx, cap->off, cap->size) != NULL) {
            vfu_log(vfu_ctx, LOG_ERR, "overlap found for capability "
                    "%u (%s)", cap->id, cap->name);
            return ERROR_INT(EINVAL);
        }

        while (*prevp != 0) {
            prevp = pci_config_space_ptr(vfu_ctx, *prevp + PCI_CAP_LIST_NEXT);
        }
    } else if (*prevp == 0) {
        cap->off = PCI_STD_HEADER_SIZEOF;
    } else {
        for (offset = *prevp; offset != 0; offset = *prevp) {
            size_t size;

            prevp = pci_config_space_ptr(vfu_ctx, offset + PCI_CAP_LIST_NEXT);

            if (*prevp == 0) {
                size = cap_size(vfu_ctx, pci_config_space_ptr(vfu_ctx, offset),
                                false);
                cap->off = ROUND_UP(offset + size, 4);
                break;
            }
        }
    }

    if (cap->off + cap->size > pci_config_space_size(vfu_ctx)) {
        vfu_log(vfu_ctx, LOG_ERR, "no config space left for capability "
                "%u (%s) of size %zu bytes at offset %#lx", cap->id,
                cap->name, cap->size, cap->off);
        return ERROR_INT(ENOSPC);
    }

    memcpy(cap_data(vfu_ctx, cap), data, cap->size);
    /* Make sure the previous cap's PCI_CAP_LIST_NEXT points to us. */
    *prevp = cap->off;
    /* Make sure our PCI_CAP_LIST_NEXT is zeroed. */
    *pci_config_space_ptr(vfu_ctx, cap->off + PCI_CAP_LIST_NEXT) = 0;
    return 0;
}

/*
 * Place the new extended capability after the previous (or at the beginning of
 * extended config space, replacing the initial zeroed capability).
 *
 * If cap->off is already provided, place it directly, but first check it
 * doesn't overlap an existing extended capability, and that the first one
 * replaces the initial zeroed capability. We also still need to link it into
 * the list.
 */
static int
ext_cap_place(vfu_ctx_t *vfu_ctx, struct pci_cap *cap, void *data)
{
    struct pcie_ext_cap_hdr *hdr = NULL;

    hdr = (void *)pci_config_space_ptr(vfu_ctx, PCI_CFG_SPACE_SIZE);

    if (cap->off != 0) {
        if (cap->off < PCI_CFG_SPACE_SIZE) {
            vfu_log(vfu_ctx, LOG_ERR, "invalid offset %#lx for capability "
                    "%u (%s)", cap->off, cap->id, cap->name);
            return ERROR_INT(EINVAL);
        }

        if (cap_find_by_offset(vfu_ctx, cap->off, cap->size) != NULL) {
            vfu_log(vfu_ctx, LOG_ERR, "overlap found for capability "
                    "%u (%s)", cap->id, cap->name);
            return ERROR_INT(EINVAL);
        }

        if (hdr->id == 0x0 && cap->off != PCI_CFG_SPACE_SIZE) {
            vfu_log(vfu_ctx, LOG_ERR, "first extended capability must be at "
                    "%#x", PCI_CFG_SPACE_SIZE);
            return ERROR_INT(EINVAL);
        }

        while (hdr->next != 0) {
            hdr = (void *)pci_config_space_ptr(vfu_ctx, hdr->next);
        }
    } else if (hdr->id == 0x0) {
        hdr = NULL;
        cap->off = PCI_CFG_SPACE_SIZE;
    } else {
        while (hdr->next != 0) {
            hdr = (void *)pci_config_space_ptr(vfu_ctx, hdr->next);
        }

        cap->off = ROUND_UP((uint8_t *)hdr + cap_size(vfu_ctx, hdr, true) -
                            pci_config_space_ptr(vfu_ctx, 0), CAP_ROUND);
    }

    if (cap->off + cap->size > pci_config_space_size(vfu_ctx)) {
        vfu_log(vfu_ctx, LOG_ERR, "no config space left for capability "
                "%u (%s) of size %zu bytes at offset %#lx", cap->id,
                cap->name, cap->size, cap->off);
        return ERROR_INT(ENOSPC);
    }

    memcpy(cap_data(vfu_ctx, cap), data, cap->size);

    /* Make sure the previous cap's next points to us. */
    if (hdr != NULL) {
        assert((cap->off & 0x3) == 0);
        hdr->next = cap->off;
    }

    hdr = (void *)pci_config_space_ptr(vfu_ctx, cap->off);
    hdr->next = 0;
    return 0;
}

EXPORT ssize_t
vfu_pci_add_capability(vfu_ctx_t *vfu_ctx, size_t pos, int flags, void *data)
{
    bool extended = (flags & VFU_CAP_FLAG_EXTENDED);
    struct pci_cap cap = { 0 };
    int ret;

    assert(vfu_ctx != NULL);

    if (flags & ~(VFU_CAP_FLAG_EXTENDED | VFU_CAP_FLAG_CALLBACK |
        VFU_CAP_FLAG_READONLY)) {
        return ERROR_INT(EINVAL);
    }

    if ((flags & VFU_CAP_FLAG_CALLBACK) &&
        vfu_ctx->reg_info[VFU_PCI_DEV_CFG_REGION_IDX].cb == NULL) {
        return ERROR_INT(EINVAL);
    }

    cap.off = pos;
    cap.flags = flags;
    cap.extended = extended;

    if (extended) {
        switch (vfu_ctx->pci.type) {
        case VFU_PCI_TYPE_PCI_X_2:
        case VFU_PCI_TYPE_EXPRESS:
            break;
        default:
            return ERROR_INT(EINVAL);
        }

        if (vfu_ctx->pci.nr_ext_caps == VFU_MAX_CAPS) {
            return ERROR_INT(ENOSPC);
        }

        cap.id = ((struct pcie_ext_cap_hdr *)data)->id;
        cap.hdr_size = sizeof(struct pcie_ext_cap_hdr);

        switch (cap.id) {
        case PCI_EXT_CAP_ID_DSN:
            cap.name = "Device Serial Number";
            cap.cb = ext_cap_write_dsn;
            break;
        case PCI_EXT_CAP_ID_VNDR:
            cap.name = "Vendor-Specific";
            cap.cb = ext_cap_write_vendor;
            cap.hdr_size = sizeof(struct pcie_ext_cap_vsc_hdr);
            break;
        default:
            vfu_log(vfu_ctx, LOG_ERR, "unsupported capability %#x", cap.id);
            return ERROR_INT(ENOTSUP);
        }

        cap.size = cap_size(vfu_ctx, data, extended);

        if (cap.off + cap.size >= pci_config_space_size(vfu_ctx)) {
            return ERROR_INT(EINVAL);
        }

        ret = ext_cap_place(vfu_ctx, &cap, data);

    } else {
        if (vfu_ctx->pci.nr_caps == VFU_MAX_CAPS) {
            return ERROR_INT(ENOSPC);
        }

        cap.id = ((struct cap_hdr *)data)->id;
        cap.hdr_size = sizeof(struct cap_hdr);

        switch (cap.id) {
        case PCI_CAP_ID_PM:
            cap.name = "Power Management";
            cap.cb = cap_write_pm;
            break;
        case PCI_CAP_ID_EXP:
            cap.name = "PCI Express";
            cap.cb = cap_write_px;
            break;
        case PCI_CAP_ID_MSIX:
            cap.name = "MSI-X";
            cap.cb = cap_write_msix;
            break;
        case PCI_CAP_ID_VNDR:
            cap.name = "Vendor-Specific";
            cap.cb = cap_write_vendor;
            cap.hdr_size = sizeof(struct vsc);
            break;
        default:
            vfu_log(vfu_ctx, LOG_ERR, "unsupported capability %#x", cap.id);
            return ERROR_INT(ENOTSUP);
        }

        cap.size = cap_size(vfu_ctx, data, extended);

        if (cap.off + cap.size >= pci_config_space_size(vfu_ctx)) {
            return ERROR_INT(EINVAL);
        }

        ret = cap_place(vfu_ctx, &cap, data);
    }

    if (ret != 0) {
        return ret;
    }

    vfu_log(vfu_ctx, LOG_DEBUG, "added PCI cap \"%s\" size=%#zx offset=%#zx",
            cap.name, cap.size, cap.off);

    if (extended) {
        memcpy(&vfu_ctx->pci.ext_caps[vfu_ctx->pci.nr_ext_caps],
               &cap, sizeof(cap));
        vfu_ctx->pci.nr_ext_caps++;
    } else {
        memcpy(&vfu_ctx->pci.caps[vfu_ctx->pci.nr_caps], &cap, sizeof(cap));
        vfu_ctx->pci.nr_caps++;
    }

    return cap.off;
}

static size_t
vfu_pci_find_next_ext_capability(vfu_ctx_t *vfu_ctx, size_t offset, int cap_id)
{
    struct pcie_ext_cap_hdr *hdr = NULL;

    if (offset + sizeof(*hdr) >= pci_config_space_size(vfu_ctx)) {
        errno = EINVAL;
        return 0;
    }

    if (offset == 0) {
        offset = PCI_CFG_SPACE_SIZE;
        hdr = (void *)pci_config_space_ptr(vfu_ctx, offset);
    } else {
        hdr = (void *)pci_config_space_ptr(vfu_ctx, offset);
        hdr = (void *)pci_config_space_ptr(vfu_ctx, hdr->next);
    }

    for (;;) {
        offset = (uint8_t *)hdr - pci_config_space_ptr(vfu_ctx, 0);

        if (offset + sizeof(*hdr) >= pci_config_space_size(vfu_ctx)) {
            errno = EINVAL;
            return 0;
        }

        if (hdr->id == cap_id) {
            return offset;
        }

        if (hdr->next == 0) {
            break;
        }

        hdr = (void *)pci_config_space_ptr(vfu_ctx, hdr->next);
    }

    errno = ENOENT;
    return 0;
}

EXPORT size_t
vfu_pci_find_next_capability(vfu_ctx_t *vfu_ctx, bool extended,
                             size_t offset, int cap_id)
{

    assert(vfu_ctx != NULL);

    if (extended) {
        return vfu_pci_find_next_ext_capability(vfu_ctx, offset, cap_id);
    }

    if (offset + PCI_CAP_LIST_NEXT >= pci_config_space_size(vfu_ctx)) {
        errno = EINVAL;
        return 0;
    }

    if (offset == 0) {
        offset = vfu_pci_get_config_space(vfu_ctx)->hdr.cap;
    } else {
        offset = *pci_config_space_ptr(vfu_ctx, offset + PCI_CAP_LIST_NEXT);
    }

    if (offset == 0) {
        errno = ENOENT;
        return 0;
    }

    for (;;) {
        uint8_t id, next;

        /* Sanity check. */
        if (offset + PCI_CAP_LIST_NEXT >= pci_config_space_size(vfu_ctx)) {
            errno = EINVAL;
            return 0;
        }

        id = *pci_config_space_ptr(vfu_ctx, offset + PCI_CAP_LIST_ID);
        next = *pci_config_space_ptr(vfu_ctx, offset + PCI_CAP_LIST_NEXT);

        if (id == cap_id) {
            return offset;
        }

        offset = next;

        if (offset == 0) {
            errno = ENOENT;
            return 0;
        }
    }
}

EXPORT size_t
vfu_pci_find_capability(vfu_ctx_t *vfu_ctx, bool extended, int cap_id)
{
    return vfu_pci_find_next_capability(vfu_ctx, extended, 0, cap_id);
}

/* ex: set tabstop=4 shiftwidth=4 softtabstop=4 expandtab: */