corundum_bm.cc 19.9 KB
Newer Older
Jialin Li's avatar
Jialin Li committed
1
2
3
4
5
6
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <unistd.h>
#include <signal.h>
7
#include <cassert>
Jialin Li's avatar
Jialin Li committed
8

Jialin Li's avatar
Jialin Li committed
9
#include "corundum_bm.h"
Jialin Li's avatar
Jialin Li committed
10
11
12

extern "C" {
    #include <nicsim.h>
Jialin Li's avatar
Jialin Li committed
13
    #include <netsim.h>
Jialin Li's avatar
Jialin Li committed
14
15
}

Jialin Li's avatar
Jialin Li committed
16
static void issue_dma_op(corundum::DMAOp *op);
Jialin Li's avatar
Jialin Li committed
17
static void msi_issue(uint8_t vec);
Jialin Li's avatar
Jialin Li committed
18
19
static void eth_send(void *data, size_t len);

Jialin Li's avatar
Jialin Li committed
20
21
22
namespace corundum {

DescRing::DescRing()
23
    : _dmaAddr(0), _sizeLog(0), _size(0), _sizeMask(0),
Jialin Li's avatar
Jialin Li committed
24
25
    _index(0), _headPtr(0), _tailPtr(0),
    _currHead(0), _currTail(0), active(false)
Jialin Li's avatar
Jialin Li committed
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
{
}

DescRing::~DescRing()
{
}


addr_t
DescRing::dmaAddr()
{
    return this->_dmaAddr;
}

size_t
DescRing::sizeLog()
{
    return this->_sizeLog;
}

unsigned
DescRing::index()
{
    return this->_index;
}

52
ptr_t
Jialin Li's avatar
Jialin Li committed
53
54
55
56
57
DescRing::headPtr()
{
    return this->_headPtr;
}

58
ptr_t
Jialin Li's avatar
Jialin Li committed
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
DescRing::tailPtr()
{
    return this->_tailPtr;
}

void
DescRing::setDMALower(uint32_t addr)
{
    this->_dmaAddr &= 0xFFFFFFFF00000000;
    this->_dmaAddr |= (addr_t)addr;
}

void
DescRing::setDMAUpper(uint32_t addr)
{
    this->_dmaAddr &= 0xFFFFFFFF;
    this->_dmaAddr |= ((addr_t)addr << 32);
}

void
DescRing::setSizeLog(size_t size_log)
{
    if (size_log & QUEUE_ACTIVE_MASK) {
        this->active = true;
    } else {
        this->active = false;
    }

    this->_sizeLog = size_log & 0xFF;
    this->_size = 1 << this->_sizeLog;
Jialin Li's avatar
Jialin Li committed
89
    this->_sizeMask = this->_size - 1;
Jialin Li's avatar
Jialin Li committed
90
91
92
93
94
}

void
DescRing::setIndex(unsigned index)
{
95
96
    assert(!(index & QUEUE_CONT_MASK));
    this->_index = index & 0xFF;
Jialin Li's avatar
Jialin Li committed
97
98
99
}

void
100
DescRing::setHeadPtr(ptr_t ptr)
Jialin Li's avatar
Jialin Li committed
101
102
103
104
105
{
    this->_headPtr = ptr;
}

void
106
DescRing::setTailPtr(ptr_t ptr)
Jialin Li's avatar
Jialin Li committed
107
108
109
110
{
    this->_tailPtr = ptr;
}

Jialin Li's avatar
Jialin Li committed
111
112
113
114
115
116
117
118
119
120
121
122
void
DescRing::dmaDone(DMAOp *op)
{
    // No action by default
}

bool
DescRing::empty()
{
    return (this->_headPtr == this->_tailPtr);
}

Jialin Li's avatar
Jialin Li committed
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
bool
DescRing::full()
{
    return (this->_headPtr - this->_tailPtr >= this->_size);
}

EventRing::EventRing()
{
}

EventRing::~EventRing()
{
}

void
EventRing::dmaDone(DMAOp *op)
{
    assert(op->write);
    switch (op->type) {
    case DMA_TYPE_EVENT:
        // TODO: assume in order transmission
        assert(this->_headPtr == op->tag);
        this->_headPtr++;
        msi_issue(0);
        delete op;
        break;
    default:
        fprintf(stderr, "Unknown DMA type %u\n", op->type);
        abort();
    }
}

void
EventRing::issueEvent(unsigned type, unsigned source)
{
    assert(type == EVENT_TYPE_TX_CPL || type == EVENT_TYPE_RX_CPL);
    if (full()) {
        fprintf(stderr, "Event ring is rull\n");
        abort();
    }
    addr_t dma_addr = this->_dmaAddr + (this->_currHead & this->_sizeMask) * EVENT_SIZE;
    /* Issue DMA write */
    DMAOp *op = new DMAOp;
    op->type = DMA_TYPE_EVENT;
    op->dma_addr = dma_addr;
    op->len = EVENT_SIZE;
    op->ring = this;
    op->tag = this->_currHead;
    op->write = true;
    Event *event = (Event *)op->data;
    memset(event, 0, sizeof(Event));
    event->type = type;
    event->source = source;
    issue_dma_op(op);
    this->_currHead++;
}

CplRing::CplRing(EventRing *eventRing)
    : eventRing(eventRing)
{
}

CplRing::~CplRing()
{
}

void
CplRing::dmaDone(DMAOp *op)
{
    assert(op->write);
    switch (op->type) {
    case DMA_TYPE_CPL:
        // TODO: assume in order transmission
        assert(this->_headPtr == op->tag);
        this->_headPtr++;
        this->eventRing->issueEvent(EVENT_TYPE_TX_CPL, 0);
        delete op;
        break;
    default:
        fprintf(stderr, "Unknown DMA type %u\n", op->type);
        abort();
    }
}

void
CplRing::complete(unsigned index, size_t len)
{
    if (full()) {
        fprintf(stderr, "Completion ring is full\n");
        abort();
    }
    addr_t dma_addr = this->_dmaAddr + (this->_currHead & this->_sizeMask) * CPL_SIZE;
    /* Issue DMA write */
    DMAOp *op = new DMAOp;
    op->type = DMA_TYPE_CPL;
    op->dma_addr = dma_addr;
    op->len = CPL_SIZE;
    op->ring = this;
    op->tag = this->_currHead;
    op->write = true;
    Cpl *cpl = (Cpl *)op->data;
    memset(cpl, 0, sizeof(Cpl));
    cpl->index = index;
    cpl->len = len;
    issue_dma_op(op);
    this->_currHead++;
}

TxRing::TxRing(CplRing *cplRing)
    : txCplRing(cplRing)
Jialin Li's avatar
Jialin Li committed
233
234
235
236
237
238
239
240
{
}

TxRing::~TxRing()
{
}

void
241
TxRing::setHeadPtr(ptr_t ptr)
Jialin Li's avatar
Jialin Li committed
242
243
{
    DescRing::setHeadPtr(ptr);
244
245
    while (this->_currTail != this->_headPtr) {
        unsigned index = this->_currTail & this->_sizeMask;
Jialin Li's avatar
Jialin Li committed
246
247
248
249
250
251
252
        addr_t dma_addr = this->_dmaAddr + index * DESC_SIZE;
        /* Issue DMA read */
        DMAOp *op = new DMAOp;
        op->type = DMA_TYPE_DESC;
        op->dma_addr = dma_addr;
        op->len = DESC_SIZE;
        op->ring = this;
253
        op->tag = this->_currTail;
Jialin Li's avatar
Jialin Li committed
254
255
        op->write = false;
        issue_dma_op(op);
256
        this->_currTail++;
Jialin Li's avatar
Jialin Li committed
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
    }
}

void
TxRing::dmaDone(DMAOp *op)
{
    assert(!op->write);
    switch (op->type) {
    case DMA_TYPE_DESC: {
        Desc *desc = (Desc *)op->data;
        op->type = DMA_TYPE_MEM;
        op->dma_addr = desc->addr;
        op->len = desc->len;
        op->write = false;
        issue_dma_op(op);
        break;
    }
    case DMA_TYPE_MEM:
        eth_send(op->data, op->len);
        // TODO: assume in order transmission
277
278
        assert(this->_tailPtr == op->tag);
        this->_tailPtr++;
Jialin Li's avatar
Jialin Li committed
279
        this->txCplRing->complete(op->tag, op->len);
Jialin Li's avatar
Jialin Li committed
280
281
282
283
284
285
        delete op;
        break;
    default:
        fprintf(stderr, "Unknown DMA type %u\n", op->type);
        abort();
    }
Jialin Li's avatar
Jialin Li committed
286
287
}

Jialin Li's avatar
Jialin Li committed
288
289
290
Port::Port()
    : _id(0), _features(0), _mtu(0),
    _schedCount(0), _schedOffset(0), _schedStride(0),
291
292
    _schedType(0), _rssMask(0), _schedEnable(false),
    _queueEnable(false)
Jialin Li's avatar
Jialin Li committed
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
{
}

Port::~Port()
{
}

unsigned
Port::id()
{
    return this->_id;
}

unsigned
Port::features()
{
    return this->_features;
}

size_t
Port::mtu()
{
    return this->_mtu;
}

size_t
Port::schedCount()
{
    return this->_schedCount;
}

addr_t
Port::schedOffset()
{
    return this->_schedOffset;
}

addr_t
Port::schedStride()
{
    return this->_schedStride;
}

unsigned
Port::schedType()
{
    return this->_schedType;
}


unsigned
Port::rssMask()
{
    return this->_rssMask;
}

void
Port::setId(unsigned id)
{
    this->_id = id;
}

void
Port::setFeatures(unsigned features)
{
    this->_features = features & (IF_FEATURE_RSS     |
                                  IF_FEATURE_PTP_TS  |
                                  IF_FEATURE_TX_CSUM |
                                  IF_FEATURE_RX_CSUM |
                                  IF_FEATURE_RX_HASH);
}

void
Port::setMtu(size_t mtu)
{
    this->_mtu = mtu;
}

void
Port::setSchedCount(size_t count)
{
    this->_schedCount = count;
}

void
Port::setSchedOffset(addr_t offset)
{
    this->_schedOffset = offset;
}

void
Port::setSchedStride(addr_t stride)
{
    this->_schedStride = stride;
}

void
Port::setSchedType(unsigned type)
{
    this->_schedType = type;
}

void
Port::setRssMask(unsigned mask)
{
    this->_rssMask = mask;
}

void
Port::schedEnable()
{
    this->_schedEnable = true;
}

void
Port::schedDisable()
{
    this->_schedEnable = false;
}

413
414
415
416
417
418
419
420
421
422
423
424
void
Port::queueEnable()
{
    this->_queueEnable = true;
}

void
Port::queueDisable()
{
    this->_queueEnable = false;
}

Jialin Li's avatar
Jialin Li committed
425
Corundum::Corundum()
Jialin Li's avatar
Jialin Li committed
426
427
    : txCplRing(&this->eventRing), rxCplRing(&this->eventRing),
    txRing(&this->txCplRing)
Jialin Li's avatar
Jialin Li committed
428
{
Jialin Li's avatar
Jialin Li committed
429
430
431
432
433
434
435
436
437
    this->port.setId(0);
    this->port.setFeatures(0x711);
    this->port.setMtu(2048);
    this->port.setSchedCount(1);
    this->port.setSchedOffset(0x100000);
    this->port.setSchedStride(0x100000);
    this->port.setSchedType(0);
    this->port.setRssMask(0);
    this->port.schedDisable();
Jialin Li's avatar
Jialin Li committed
438
439
440
441
442
}

Corundum::~Corundum()
{
}
Jialin Li's avatar
Jialin Li committed
443

Jialin Li's avatar
Jialin Li committed
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
reg_t
Corundum::readReg(addr_t addr)
{
    switch (addr) {
        case REG_FW_ID:
            return 32;
        case REG_FW_VER:
            return 1;
        case REG_BOARD_ID:
            return 0x43215678;
        case REG_BOARD_VER:
            return 1;
        case REG_PHC_COUNT:
            return 1;
        case REG_PHC_OFFSET:
            return 0x200;
        case REG_PHC_STRIDE:
            return 0x80;
        case REG_IF_COUNT:
            return 1;
        case REG_IF_STRIDE:
            return 0x80000;
        case REG_IF_CSR_OFFSET:
            return 0x80000;
        case PHC_REG_FEATURES:
            return 0x1;
        case IF_REG_IF_ID:
            return 0;
        case IF_REG_IF_FEATURES:
            return 0x711;
        case IF_REG_EVENT_QUEUE_COUNT:
            return 1;
        case IF_REG_EVENT_QUEUE_OFFSET:
            return 0x100000;
        case IF_REG_TX_QUEUE_COUNT:
            return 1;
        case IF_REG_TX_QUEUE_OFFSET:
            return 0x200000;
        case IF_REG_TX_CPL_QUEUE_COUNT:
            return 1;
        case IF_REG_TX_CPL_QUEUE_OFFSET:
            return 0x400000;
        case IF_REG_RX_QUEUE_COUNT:
            return 1;
        case IF_REG_RX_QUEUE_OFFSET:
            return 0x600000;
        case IF_REG_RX_CPL_QUEUE_COUNT:
            return 1;
        case IF_REG_RX_CPL_QUEUE_OFFSET:
            return 0x700000;
        case IF_REG_PORT_COUNT:
            return 1;
        case IF_REG_PORT_OFFSET:
            return 0x800000;
        case IF_REG_PORT_STRIDE:
            return 0x200000;
Jialin Li's avatar
Jialin Li committed
500
        case EVENT_QUEUE_HEAD_PTR_REG:
Jialin Li's avatar
Jialin Li committed
501
            return this->eventRing.headPtr();
Jialin Li's avatar
Jialin Li committed
502
503
        case TX_QUEUE_ACTIVE_LOG_SIZE_REG:
            return this->txRing.sizeLog();
Jialin Li's avatar
Jialin Li committed
504
505
506
507
        case TX_QUEUE_TAIL_PTR_REG:
            return this->txRing.tailPtr();
        case TX_CPL_QUEUE_HEAD_PTR_REG:
            return this->txCplRing.headPtr();
Jialin Li's avatar
Jialin Li committed
508
509
510
511
512
513
514
515
516
517
518
519
520
521
        case PORT_REG_PORT_ID:
            return this->port.id();
        case PORT_REG_PORT_FEATURES:
            return this->port.features();
        case PORT_REG_PORT_MTU:
            return this->port.mtu();
        case PORT_REG_SCHED_COUNT:
            return this->port.schedCount();
        case PORT_REG_SCHED_OFFSET:
            return this->port.schedOffset();
        case PORT_REG_SCHED_STRIDE:
            return this->port.schedStride();
        case PORT_REG_SCHED_TYPE:
            return this->port.schedType();
Jialin Li's avatar
Jialin Li committed
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
        default:
            fprintf(stderr, "Unknown register read %lx\n", addr);
            abort();
    }
}

void
Corundum::writeReg(addr_t addr, reg_t val)
{
    switch (addr) {
        case REG_FW_ID:
        case REG_FW_VER:
        case REG_BOARD_ID:
        case REG_BOARD_VER:
        case REG_PHC_COUNT:
        case REG_PHC_OFFSET:
        case REG_PHC_STRIDE:
        case REG_IF_COUNT:
        case REG_IF_STRIDE:
        case REG_IF_CSR_OFFSET:
        case PHC_REG_FEATURES:
        case PHC_REG_PTP_SET_FNS:
        case PHC_REG_PTP_SET_NS:
        case PHC_REG_PTP_SET_SEC_L:
        case PHC_REG_PTP_SET_SEC_H:
            break;
        case EVENT_QUEUE_BASE_ADDR_REG:
Jialin Li's avatar
Jialin Li committed
549
            this->eventRing.setDMALower(val);
Jialin Li's avatar
Jialin Li committed
550
551
            break;
        case EVENT_QUEUE_BASE_ADDR_REG + 4:
Jialin Li's avatar
Jialin Li committed
552
            this->eventRing.setDMAUpper(val);
Jialin Li's avatar
Jialin Li committed
553
554
            break;
        case EVENT_QUEUE_ACTIVE_LOG_SIZE_REG:
Jialin Li's avatar
Jialin Li committed
555
            this->eventRing.setSizeLog(val);
Jialin Li's avatar
Jialin Li committed
556
557
            break;
        case EVENT_QUEUE_INTERRUPT_INDEX_REG:
Jialin Li's avatar
Jialin Li committed
558
            this->eventRing.setIndex(val);
Jialin Li's avatar
Jialin Li committed
559
560
            break;
        case EVENT_QUEUE_HEAD_PTR_REG:
Jialin Li's avatar
Jialin Li committed
561
            this->eventRing.setHeadPtr(val);
Jialin Li's avatar
Jialin Li committed
562
563
            break;
        case EVENT_QUEUE_TAIL_PTR_REG:
Jialin Li's avatar
Jialin Li committed
564
            this->eventRing.setTailPtr(val);
Jialin Li's avatar
Jialin Li committed
565
566
567
568
569
570
571
572
            break;
        case TX_QUEUE_BASE_ADDR_REG:
            this->txRing.setDMALower(val);
            break;
        case TX_QUEUE_BASE_ADDR_REG + 4:
            this->txRing.setDMAUpper(val);
            break;
        case TX_QUEUE_ACTIVE_LOG_SIZE_REG:
Jialin Li's avatar
Jialin Li committed
573
            this->txRing.setSizeLog(val);
Jialin Li's avatar
Jialin Li committed
574
            break;
Jialin Li's avatar
Jialin Li committed
575
        case TX_QUEUE_CPL_QUEUE_INDEX_REG:
Jialin Li's avatar
Jialin Li committed
576
577
578
579
580
581
582
583
            this->txRing.setIndex(val);
            break;
        case TX_QUEUE_HEAD_PTR_REG:
            this->txRing.setHeadPtr(val);
            break;
        case TX_QUEUE_TAIL_PTR_REG:
            this->txRing.setTailPtr(val);
            break;
Jialin Li's avatar
Jialin Li committed
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
        case TX_CPL_QUEUE_BASE_ADDR_REG:
            this->txCplRing.setDMALower(val);
            break;
        case TX_CPL_QUEUE_BASE_ADDR_REG + 4:
            this->txCplRing.setDMAUpper(val);
            break;
        case TX_CPL_QUEUE_ACTIVE_LOG_SIZE_REG:
            this->txCplRing.setSizeLog(val);
            break;
        case TX_CPL_QUEUE_INTERRUPT_INDEX_REG:
            this->txCplRing.setIndex(val);
            break;
        case TX_CPL_QUEUE_HEAD_PTR_REG:
            this->txCplRing.setHeadPtr(val);
            break;
        case TX_CPL_QUEUE_TAIL_PTR_REG:
            this->txCplRing.setTailPtr(val);
            break;
        case RX_QUEUE_BASE_ADDR_REG:
            this->rxRing.setDMALower(val);
            break;
        case RX_QUEUE_BASE_ADDR_REG + 4:
            this->rxRing.setDMAUpper(val);
            break;
        case RX_QUEUE_ACTIVE_LOG_SIZE_REG:
            this->rxRing.setSizeLog(val);
            break;
        case RX_QUEUE_CPL_QUEUE_INDEX_REG:
            this->rxRing.setIndex(val);
            break;
        case RX_QUEUE_HEAD_PTR_REG:
            this->rxRing.setHeadPtr(val);
            break;
        case RX_QUEUE_TAIL_PTR_REG:
            this->rxRing.setTailPtr(val);
            break;
        case RX_CPL_QUEUE_BASE_ADDR_REG:
            this->rxCplRing.setDMALower(val);
            break;
        case RX_CPL_QUEUE_BASE_ADDR_REG + 4:
            this->rxCplRing.setDMAUpper(val);
            break;
        case RX_CPL_QUEUE_ACTIVE_LOG_SIZE_REG:
            this->rxCplRing.setSizeLog(val);
            break;
        case RX_CPL_QUEUE_INTERRUPT_INDEX_REG:
            this->rxCplRing.setIndex(val);
            break;
        case RX_CPL_QUEUE_HEAD_PTR_REG:
            this->rxCplRing.setHeadPtr(val);
            break;
        case RX_CPL_QUEUE_TAIL_PTR_REG:
            this->rxCplRing.setTailPtr(val);
            break;
        case PORT_REG_SCHED_ENABLE:
            if (val) {
                this->port.schedEnable();
            } else {
                this->port.schedDisable();
            }
            break;
        case PORT_REG_RSS_MASK:
            this->port.setRssMask(val);
            break;
648
649
650
651
652
653
654
        case PORT_QUEUE_ENABLE:
            if (val) {
                this->port.queueEnable();
            } else {
                this->port.queueDisable();
            }
            break;
Jialin Li's avatar
Jialin Li committed
655
656
657
658
659
660
        default:
            fprintf(stderr, "Unknown register write %lx\n", addr);
            abort();
    }
}

Jialin Li's avatar
Jialin Li committed
661
662
} //namespace corundum

Jialin Li's avatar
Jialin Li committed
663
664
using namespace corundum;

Jialin Li's avatar
Jialin Li committed
665
666
static volatile int exiting = 0;

Jialin Li's avatar
Jialin Li committed
667
668
static void
sigint_handler(int dummy)
Jialin Li's avatar
Jialin Li committed
669
670
671
672
{
    exiting = 1;
}

Jialin Li's avatar
Jialin Li committed
673
674
static volatile union cosim_pcie_proto_d2h *
d2h_alloc(void)
Jialin Li's avatar
Jialin Li committed
675
676
677
678
679
680
681
682
683
{
    volatile union cosim_pcie_proto_d2h *msg = nicsim_d2h_alloc();
    if (msg == NULL) {
        fprintf(stderr, "d2h_alloc: no entry available\n");
        abort();
    }
    return msg;
}

Jialin Li's avatar
Jialin Li committed
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
static volatile union cosim_eth_proto_d2n *
d2n_alloc(void)
{
    volatile union cosim_eth_proto_d2n *msg = nicsim_d2n_alloc();
    if (msg == NULL) {
        fprintf(stderr, "d2n_alloc: no entry available\n");
        abort();
    }
    return msg;
}

static void
issue_dma_op(DMAOp *op)
{
    volatile union cosim_pcie_proto_d2h *msg = d2h_alloc();
    printf("issue dma op %p addr %lx len %u\n", op, op->dma_addr, op->len);

    if (op->write) {
        volatile struct cosim_pcie_proto_d2h_write *write = &msg->write;
        write->req_id = (uintptr_t)op;
        write->offset = op->dma_addr;
        write->len = op->len;
        memcpy((void *)write->data, op->data, op->len);
        // WMB();
        write->own_type = COSIM_PCIE_PROTO_D2H_MSG_WRITE |
            COSIM_PCIE_PROTO_D2H_OWN_HOST;
    } else {
        volatile struct cosim_pcie_proto_d2h_read *read = &msg->read;
        read->req_id = (uintptr_t)op;
        read->offset = op->dma_addr;
        read->len = op->len;
        // WMB();
        read->own_type = COSIM_PCIE_PROTO_D2H_MSG_READ |
            COSIM_PCIE_PROTO_D2H_OWN_HOST;
    }
}

Jialin Li's avatar
Jialin Li committed
721
722
723
724
725
726
727
728
729
730
731
732
733
734
static void
msi_issue(uint8_t vec)
{
    volatile union cosim_pcie_proto_d2h *msg = d2h_alloc();
    printf("issue MSI interrupt vec %u\n", vec);
    volatile struct cosim_pcie_proto_d2h_interrupt *intr = &msg->interrupt;
    intr->vector = vec;
    intr->inttype = COSIM_PCIE_PROTO_INT_MSI;

    // WMB();
    intr->own_type = COSIM_PCIE_PROTO_D2H_MSG_INTERRUPT |
        COSIM_PCIE_PROTO_D2H_OWN_HOST;
}

Jialin Li's avatar
Jialin Li committed
735
static void
Jialin Li's avatar
Jialin Li committed
736
h2d_read(Corundum &nic, volatile struct cosim_pcie_proto_h2d_read *read)
Jialin Li's avatar
Jialin Li committed
737
{
Jialin Li's avatar
Jialin Li committed
738
739
740
    reg_t val = nic.readReg(read->offset);
    printf("read(off=0x%lx, len=%u, val=0x%x)\n", read->offset, read->len, val);

Jialin Li's avatar
Jialin Li committed
741
742
743
744
745
746
    volatile union cosim_pcie_proto_d2h *msg;
    volatile struct cosim_pcie_proto_d2h_readcomp *rc;

    msg = d2h_alloc();
    rc = &msg->readcomp;

Jialin Li's avatar
Jialin Li committed
747
748
    memcpy((void *)rc->data, &val, read->len);
    rc->req_id = read->req_id;
Jialin Li's avatar
Jialin Li committed
749
750
751
752
753
754

    //WMB();
    rc->own_type = COSIM_PCIE_PROTO_D2H_MSG_READCOMP |
        COSIM_PCIE_PROTO_D2H_OWN_HOST;
}

Jialin Li's avatar
Jialin Li committed
755
static void
Jialin Li's avatar
Jialin Li committed
756
h2d_write(Corundum &nic, volatile struct cosim_pcie_proto_h2d_write *write)
Jialin Li's avatar
Jialin Li committed
757
{
Jialin Li's avatar
Jialin Li committed
758
    reg_t val = 0;
Jialin Li's avatar
Jialin Li committed
759
760
    memcpy(&val, (void *)write->data, write->len);

Jialin Li's avatar
Jialin Li committed
761
762
    volatile union cosim_pcie_proto_d2h *msg;
    volatile struct cosim_pcie_proto_d2h_writecomp *wc;
Jialin Li's avatar
Jialin Li committed
763

Jialin Li's avatar
Jialin Li committed
764
765
    msg = d2h_alloc();
    wc = &msg->writecomp;
Jialin Li's avatar
Jialin Li committed
766

Jialin Li's avatar
Jialin Li committed
767
768
769
    printf("write(off=0x%lx, len=%u, val=0x%x)\n", write->offset, write->len, val);
    nic.writeReg(write->offset, val);
    wc->req_id = write->req_id;
Jialin Li's avatar
Jialin Li committed
770

Jialin Li's avatar
Jialin Li committed
771
772
773
    //WMB();
    wc->own_type = COSIM_PCIE_PROTO_D2H_MSG_WRITECOMP |
        COSIM_PCIE_PROTO_D2H_OWN_HOST;
Jialin Li's avatar
Jialin Li committed
774
775
}

Jialin Li's avatar
Jialin Li committed
776
static void h2d_readcomp(volatile struct cosim_pcie_proto_h2d_readcomp *rc)
Jialin Li's avatar
Jialin Li committed
777
{
Jialin Li's avatar
Jialin Li committed
778
779
780
    DMAOp *op = (DMAOp *)(uintptr_t)rc->req_id;
    memcpy(op->data, (void *)rc->data, op->len);
    op->ring->dmaDone(op);
Jialin Li's avatar
Jialin Li committed
781
782
}

Jialin Li's avatar
Jialin Li committed
783
static void h2d_writecomp(volatile struct cosim_pcie_proto_h2d_writecomp *wc)
Jialin Li's avatar
Jialin Li committed
784
{
Jialin Li's avatar
Jialin Li committed
785
786
    DMAOp *op = (DMAOp *)(uintptr_t)wc->req_id;
    op->ring->dmaDone(op);
Jialin Li's avatar
Jialin Li committed
787
788
}

Jialin Li's avatar
Jialin Li committed
789
static void eth_recv(volatile struct cosim_eth_proto_n2d_recv *recv)
Jialin Li's avatar
Jialin Li committed
790
791
792
793
{
    printf("RX recv(port=%u, len=%u)\n", recv->port, recv->len);
}

Jialin Li's avatar
Jialin Li committed
794
795
796
797
798
799
800
801
802
803
804
805
static void eth_send(void *data, size_t len)
{
    volatile union cosim_eth_proto_d2n *msg = d2n_alloc();
    volatile struct cosim_eth_proto_d2n_send *send = &msg->send;
    send->port = 0; // single port
    send->len = len;
    memcpy((void *)send->data, data, len);
    send->own_type = COSIM_ETH_PROTO_D2N_MSG_SEND |
        COSIM_ETH_PROTO_D2N_OWN_NET;
}

static void poll_h2d(Corundum &nic)
Jialin Li's avatar
Jialin Li committed
806
807
808
809
810
811
812
813
814
815
{
    volatile union cosim_pcie_proto_h2d *msg = nicif_h2d_poll();
    uint8_t type;

    if (msg == NULL)
        return;

    type = msg->dummy.own_type & COSIM_PCIE_PROTO_H2D_MSG_MASK;
    switch (type) {
        case COSIM_PCIE_PROTO_H2D_MSG_READ:
Jialin Li's avatar
Jialin Li committed
816
            h2d_read(nic, &msg->read);
Jialin Li's avatar
Jialin Li committed
817
818
819
            break;

        case COSIM_PCIE_PROTO_H2D_MSG_WRITE:
Jialin Li's avatar
Jialin Li committed
820
            h2d_write(nic, &msg->write);
Jialin Li's avatar
Jialin Li committed
821
822
823
            break;

        case COSIM_PCIE_PROTO_H2D_MSG_READCOMP:
Jialin Li's avatar
Jialin Li committed
824
            h2d_readcomp(&msg->readcomp);
Jialin Li's avatar
Jialin Li committed
825
826
827
            break;

        case COSIM_PCIE_PROTO_H2D_MSG_WRITECOMP:
Jialin Li's avatar
Jialin Li committed
828
            h2d_writecomp(&msg->writecomp);
Jialin Li's avatar
Jialin Li committed
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
            break;

        default:
            fprintf(stderr, "poll_h2d: unsupported type=%u\n", type);
    }

    nicif_h2d_done(msg);
    nicif_h2d_next();
}

static void poll_n2d(void)
{
    volatile union cosim_eth_proto_n2d *msg = nicif_n2d_poll();
    uint8_t t;

    if (msg == NULL)
        return;

    t = msg->dummy.own_type & COSIM_ETH_PROTO_N2D_MSG_MASK;
    switch (t) {
        case COSIM_ETH_PROTO_N2D_MSG_RECV:
Jialin Li's avatar
Jialin Li committed
850
            eth_recv(&msg->recv);
Jialin Li's avatar
Jialin Li committed
851
852
853
854
855
856
857
858
859
860
861
862
            break;

        default:
            fprintf(stderr, "poll_n2d: unsupported type=%u", t);
    }

    nicif_n2d_done(msg);
    nicif_n2d_next();
}

int main(int argc, char *argv[])
{
Jialin Li's avatar
Jialin Li committed
863
864
    signal(SIGINT, sigint_handler);

Jialin Li's avatar
Jialin Li committed
865
866
867
868
869
870
871
872
873
874
875
876
877
    struct cosim_pcie_proto_dev_intro di;
    memset(&di, 0, sizeof(di));
    di.bars[0].len = 1 << 24;
    di.bars[0].flags = COSIM_PCIE_PROTO_BAR_64;
    di.pci_vendor_id = 0x5543;
    di.pci_device_id = 0x1001;
    di.pci_class = 0x02;
    di.pci_subclass = 0x00;
    di.pci_revision = 0x00;
    di.pci_msi_nvecs = 32;

    int sync_pci_en = 0, sync_eth_en = 0;
    if (nicsim_init(&di, "/tmp/cosim-pci", &sync_pci_en,
Jialin Li's avatar
Jialin Li committed
878
                "/tmp/cosim-eth", &sync_eth_en,
Jialin Li's avatar
Jialin Li committed
879
880
881
882
                "/dev/shm/dummy_nic_shm")) {
        return EXIT_FAILURE;
    }

Jialin Li's avatar
Jialin Li committed
883
    Corundum nic;
Jialin Li's avatar
Jialin Li committed
884
885

    while (!exiting) {
Jialin Li's avatar
Jialin Li committed
886
        poll_h2d(nic);
Jialin Li's avatar
Jialin Li committed
887
888
889
890
891
892
        poll_n2d();
    }

    nicsim_cleanup();
    return 0;
}