corundum_bm.cc 22 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
bool
DescRing::empty()
{
Jialin Li's avatar
Jialin Li committed
114
    return (this->_headPtr == this->_currTail);
Jialin Li's avatar
Jialin Li committed
115
116
}

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

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) {
Jialin Li's avatar
Jialin Li committed
188
189
    case DMA_TYPE_TX_CPL:
    case DMA_TYPE_RX_CPL: {
Jialin Li's avatar
Jialin Li committed
190
191
192
        // TODO: assume in order transmission
        assert(this->_headPtr == op->tag);
        this->_headPtr++;
Jialin Li's avatar
Jialin Li committed
193
194
195
        unsigned type = op->type == DMA_TYPE_TX_CPL ? EVENT_TYPE_TX_CPL :
                                                      EVENT_TYPE_RX_CPL;
        this->eventRing->issueEvent(type, 0);
Jialin Li's avatar
Jialin Li committed
196
197
        delete op;
        break;
Jialin Li's avatar
Jialin Li committed
198
    }
Jialin Li's avatar
Jialin Li committed
199
200
201
202
203
204
205
    default:
        fprintf(stderr, "Unknown DMA type %u\n", op->type);
        abort();
    }
}

void
Jialin Li's avatar
Jialin Li committed
206
CplRing::complete(unsigned index, size_t len, bool tx)
Jialin Li's avatar
Jialin Li committed
207
208
209
210
211
212
213
214
{
    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;
Jialin Li's avatar
Jialin Li committed
215
    op->type = tx ? DMA_TYPE_TX_CPL : DMA_TYPE_RX_CPL;
Jialin Li's avatar
Jialin Li committed
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
    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
231
232
233
234
235
236
237
238
{
}

TxRing::~TxRing()
{
}

void
239
TxRing::setHeadPtr(ptr_t ptr)
Jialin Li's avatar
Jialin Li committed
240
241
{
    DescRing::setHeadPtr(ptr);
242
243
    while (this->_currTail != this->_headPtr) {
        unsigned index = this->_currTail & this->_sizeMask;
Jialin Li's avatar
Jialin Li committed
244
245
246
247
248
249
250
        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;
251
        op->tag = this->_currTail;
Jialin Li's avatar
Jialin Li committed
252
253
        op->write = false;
        issue_dma_op(op);
254
        this->_currTail++;
Jialin Li's avatar
Jialin Li committed
255
256
257
258
259
260
261
262
    }
}

void
TxRing::dmaDone(DMAOp *op)
{
    switch (op->type) {
    case DMA_TYPE_DESC: {
Jialin Li's avatar
Jialin Li committed
263
        assert(!op->write);
Jialin Li's avatar
Jialin Li committed
264
265
266
267
268
269
270
271
272
        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:
Jialin Li's avatar
Jialin Li committed
273
        assert(!op->write);
Jialin Li's avatar
Jialin Li committed
274
275
        eth_send(op->data, op->len);
        // TODO: assume in order transmission
276
277
        assert(this->_tailPtr == op->tag);
        this->_tailPtr++;
Jialin Li's avatar
Jialin Li committed
278
        this->txCplRing->complete(op->tag, op->len, true);
Jialin Li's avatar
Jialin Li committed
279
280
281
282
283
284
        delete op;
        break;
    default:
        fprintf(stderr, "Unknown DMA type %u\n", op->type);
        abort();
    }
Jialin Li's avatar
Jialin Li committed
285
286
}

Jialin Li's avatar
Jialin Li committed
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
RxRing::RxRing(CplRing *cplRing)
    : rxCplRing(cplRing)
{
}

RxRing::~RxRing()
{
}

void
RxRing::dmaDone(DMAOp *op)
{
    switch (op->type) {
    case DMA_TYPE_DESC: {
        assert(!op->write);
        Desc *desc = (Desc *)op->data;
        op->type = DMA_TYPE_MEM;
        op->dma_addr = desc->addr;
        op->len = op->rx_data->len;
        memcpy((void *)op->data, (void *)op->rx_data->data, op->len);
        delete op->rx_data;
        op->write = true;
        issue_dma_op(op);
        break;
    }
    case DMA_TYPE_MEM:
        assert(op->write);
        // TODO: assume in order transmission
        assert(this->_tailPtr == op->tag);
        this->_tailPtr++;
        this->rxCplRing->complete(op->tag, op->len, false);
        delete op;
        break;
    default:
        fprintf(stderr, "Unknown DMA type %u\n", op->type);
        abort();
    }
}

void
RxRing::rx(RxData *rx_data)
{
    if (empty()) {
        delete rx_data;
        return;
    }
    addr_t dma_addr = this->_dmaAddr + (this->_currTail & this->_sizeMask) * 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;
    op->rx_data = rx_data;
    op->tag = this->_currTail;
    op->write = false;
    issue_dma_op(op);
    this->_currTail++;
}

Jialin Li's avatar
Jialin Li committed
347
348
349
Port::Port()
    : _id(0), _features(0), _mtu(0),
    _schedCount(0), _schedOffset(0), _schedStride(0),
350
351
    _schedType(0), _rssMask(0), _schedEnable(false),
    _queueEnable(false)
Jialin Li's avatar
Jialin Li committed
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
{
}

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;
}

472
473
474
475
476
477
478
479
480
481
482
483
void
Port::queueEnable()
{
    this->_queueEnable = true;
}

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

Jialin Li's avatar
Jialin Li committed
484
Corundum::Corundum()
Jialin Li's avatar
Jialin Li committed
485
    : txCplRing(&this->eventRing), rxCplRing(&this->eventRing),
Jialin Li's avatar
Jialin Li committed
486
    txRing(&this->txCplRing), rxRing(&this->rxCplRing)
Jialin Li's avatar
Jialin Li committed
487
{
Jialin Li's avatar
Jialin Li committed
488
489
490
491
492
493
494
495
496
    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
497
498
499
500
501
}

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

Jialin Li's avatar
Jialin Li committed
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
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;
Jialin Li's avatar
Jialin Li committed
529
530
531
532
        case PHC_REG_PTP_CUR_SEC_L:
            return 0x0;
        case PHC_REG_PTP_CUR_SEC_H:
            return 0x0;
Jialin Li's avatar
Jialin Li committed
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
        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
563
        case EVENT_QUEUE_HEAD_PTR_REG:
Jialin Li's avatar
Jialin Li committed
564
            return this->eventRing.headPtr();
Jialin Li's avatar
Jialin Li committed
565
566
        case TX_QUEUE_ACTIVE_LOG_SIZE_REG:
            return this->txRing.sizeLog();
Jialin Li's avatar
Jialin Li committed
567
568
569
570
        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
571
572
573
574
        case RX_QUEUE_TAIL_PTR_REG:
            return this->rxRing.tailPtr();
        case RX_CPL_QUEUE_HEAD_PTR_REG:
            return this->rxCplRing.headPtr();
Jialin Li's avatar
Jialin Li committed
575
576
577
578
579
580
581
582
583
584
585
586
587
588
        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
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
        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
616
            this->eventRing.setDMALower(val);
Jialin Li's avatar
Jialin Li committed
617
618
            break;
        case EVENT_QUEUE_BASE_ADDR_REG + 4:
Jialin Li's avatar
Jialin Li committed
619
            this->eventRing.setDMAUpper(val);
Jialin Li's avatar
Jialin Li committed
620
621
            break;
        case EVENT_QUEUE_ACTIVE_LOG_SIZE_REG:
Jialin Li's avatar
Jialin Li committed
622
            this->eventRing.setSizeLog(val);
Jialin Li's avatar
Jialin Li committed
623
624
            break;
        case EVENT_QUEUE_INTERRUPT_INDEX_REG:
Jialin Li's avatar
Jialin Li committed
625
            this->eventRing.setIndex(val);
Jialin Li's avatar
Jialin Li committed
626
627
            break;
        case EVENT_QUEUE_HEAD_PTR_REG:
Jialin Li's avatar
Jialin Li committed
628
            this->eventRing.setHeadPtr(val);
Jialin Li's avatar
Jialin Li committed
629
630
            break;
        case EVENT_QUEUE_TAIL_PTR_REG:
Jialin Li's avatar
Jialin Li committed
631
            this->eventRing.setTailPtr(val);
Jialin Li's avatar
Jialin Li committed
632
633
634
635
636
637
638
639
            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
640
            this->txRing.setSizeLog(val);
Jialin Li's avatar
Jialin Li committed
641
            break;
Jialin Li's avatar
Jialin Li committed
642
        case TX_QUEUE_CPL_QUEUE_INDEX_REG:
Jialin Li's avatar
Jialin Li committed
643
644
645
646
647
648
649
650
            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
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
        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;
715
716
717
718
719
720
721
        case PORT_QUEUE_ENABLE:
            if (val) {
                this->port.queueEnable();
            } else {
                this->port.queueDisable();
            }
            break;
Jialin Li's avatar
Jialin Li committed
722
723
724
725
726
727
        default:
            fprintf(stderr, "Unknown register write %lx\n", addr);
            abort();
    }
}

Jialin Li's avatar
Jialin Li committed
728
729
730
731
732
733
void
Corundum::rx(uint8_t port, RxData *rx_data)
{
    this->rxRing.rx(rx_data);
}

Jialin Li's avatar
Jialin Li committed
734
735
} //namespace corundum

Jialin Li's avatar
Jialin Li committed
736
737
using namespace corundum;

Jialin Li's avatar
Jialin Li committed
738
739
static volatile int exiting = 0;

Jialin Li's avatar
Jialin Li committed
740
741
static void
sigint_handler(int dummy)
Jialin Li's avatar
Jialin Li committed
742
743
744
745
{
    exiting = 1;
}

Jialin Li's avatar
Jialin Li committed
746
747
static volatile union cosim_pcie_proto_d2h *
d2h_alloc(void)
Jialin Li's avatar
Jialin Li committed
748
749
750
751
752
753
754
755
756
{
    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
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
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();
Jialin Li's avatar
Jialin Li committed
772
    //printf("issue dma op %p addr %lx len %u\n", op, op->dma_addr, op->len);
Jialin Li's avatar
Jialin Li committed
773
774
775
776
777
778

    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;
Jialin Li's avatar
Jialin Li committed
779
        memcpy((void *)write->data, (void *)op->data, op->len);
Jialin Li's avatar
Jialin Li committed
780
781
782
783
784
785
786
787
788
789
790
791
792
793
        // 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
794
795
796
797
static void
msi_issue(uint8_t vec)
{
    volatile union cosim_pcie_proto_d2h *msg = d2h_alloc();
Jialin Li's avatar
Jialin Li committed
798
    //printf("issue MSI interrupt vec %u\n", vec);
Jialin Li's avatar
Jialin Li committed
799
800
801
802
803
804
805
806
807
    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
808
static void
Jialin Li's avatar
Jialin Li committed
809
h2d_read(Corundum &nic, volatile struct cosim_pcie_proto_h2d_read *read)
Jialin Li's avatar
Jialin Li committed
810
{
Jialin Li's avatar
Jialin Li committed
811
    reg_t val = nic.readReg(read->offset);
Jialin Li's avatar
Jialin Li committed
812
    //printf("read(off=0x%lx, len=%u, val=0x%x)\n", read->offset, read->len, val);
Jialin Li's avatar
Jialin Li committed
813

Jialin Li's avatar
Jialin Li committed
814
815
816
817
818
819
    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
820
821
    memcpy((void *)rc->data, &val, read->len);
    rc->req_id = read->req_id;
Jialin Li's avatar
Jialin Li committed
822
823
824
825
826
827

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

Jialin Li's avatar
Jialin Li committed
828
static void
Jialin Li's avatar
Jialin Li committed
829
h2d_write(Corundum &nic, volatile struct cosim_pcie_proto_h2d_write *write)
Jialin Li's avatar
Jialin Li committed
830
{
Jialin Li's avatar
Jialin Li committed
831
    reg_t val = 0;
Jialin Li's avatar
Jialin Li committed
832
833
    memcpy(&val, (void *)write->data, write->len);

Jialin Li's avatar
Jialin Li committed
834
835
    volatile union cosim_pcie_proto_d2h *msg;
    volatile struct cosim_pcie_proto_d2h_writecomp *wc;
Jialin Li's avatar
Jialin Li committed
836

Jialin Li's avatar
Jialin Li committed
837
838
    msg = d2h_alloc();
    wc = &msg->writecomp;
Jialin Li's avatar
Jialin Li committed
839

Jialin Li's avatar
Jialin Li committed
840
    //printf("write(off=0x%lx, len=%u, val=0x%x)\n", write->offset, write->len, val);
Jialin Li's avatar
Jialin Li committed
841
842
    nic.writeReg(write->offset, val);
    wc->req_id = write->req_id;
Jialin Li's avatar
Jialin Li committed
843

Jialin Li's avatar
Jialin Li committed
844
845
846
    //WMB();
    wc->own_type = COSIM_PCIE_PROTO_D2H_MSG_WRITECOMP |
        COSIM_PCIE_PROTO_D2H_OWN_HOST;
Jialin Li's avatar
Jialin Li committed
847
848
}

Jialin Li's avatar
Jialin Li committed
849
static void h2d_readcomp(volatile struct cosim_pcie_proto_h2d_readcomp *rc)
Jialin Li's avatar
Jialin Li committed
850
{
Jialin Li's avatar
Jialin Li committed
851
852
853
    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
854
855
}

Jialin Li's avatar
Jialin Li committed
856
static void h2d_writecomp(volatile struct cosim_pcie_proto_h2d_writecomp *wc)
Jialin Li's avatar
Jialin Li committed
857
{
Jialin Li's avatar
Jialin Li committed
858
859
    DMAOp *op = (DMAOp *)(uintptr_t)wc->req_id;
    op->ring->dmaDone(op);
Jialin Li's avatar
Jialin Li committed
860
861
}

Jialin Li's avatar
Jialin Li committed
862
863
static void eth_recv(Corundum &nic,
                     volatile struct cosim_eth_proto_n2d_recv *recv)
Jialin Li's avatar
Jialin Li committed
864
{
Jialin Li's avatar
Jialin Li committed
865
866
867
868
869
    //printf("RX recv(port=%u, len=%u)\n", recv->port, recv->len);
    RxData *rx_data = new RxData;
    memcpy((void *)rx_data->data, (void *)recv->data, recv->len);
    rx_data->len = recv->len;
    nic.rx(recv->port, rx_data);
Jialin Li's avatar
Jialin Li committed
870
871
}

Jialin Li's avatar
Jialin Li committed
872
873
874
875
876
877
878
879
880
881
882
883
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
884
885
886
887
888
889
890
891
892
893
{
    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
894
            h2d_read(nic, &msg->read);
Jialin Li's avatar
Jialin Li committed
895
896
897
            break;

        case COSIM_PCIE_PROTO_H2D_MSG_WRITE:
Jialin Li's avatar
Jialin Li committed
898
            h2d_write(nic, &msg->write);
Jialin Li's avatar
Jialin Li committed
899
900
901
            break;

        case COSIM_PCIE_PROTO_H2D_MSG_READCOMP:
Jialin Li's avatar
Jialin Li committed
902
            h2d_readcomp(&msg->readcomp);
Jialin Li's avatar
Jialin Li committed
903
904
905
            break;

        case COSIM_PCIE_PROTO_H2D_MSG_WRITECOMP:
Jialin Li's avatar
Jialin Li committed
906
            h2d_writecomp(&msg->writecomp);
Jialin Li's avatar
Jialin Li committed
907
908
909
910
911
912
913
914
915
916
            break;

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

    nicif_h2d_done(msg);
    nicif_h2d_next();
}

Jialin Li's avatar
Jialin Li committed
917
static void poll_n2d(Corundum &nic)
Jialin Li's avatar
Jialin Li committed
918
919
920
921
922
923
924
925
926
927
{
    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
928
            eth_recv(nic, &msg->recv);
Jialin Li's avatar
Jialin Li committed
929
930
931
932
933
934
935
936
937
938
939
940
            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
941
942
    signal(SIGINT, sigint_handler);

Jialin Li's avatar
Jialin Li committed
943
944
945
946
947
948
949
950
951
952
953
954
955
    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
956
                "/tmp/cosim-eth", &sync_eth_en,
Jialin Li's avatar
Jialin Li committed
957
958
959
960
                "/dev/shm/dummy_nic_shm")) {
        return EXIT_FAILURE;
    }

Jialin Li's avatar
Jialin Li committed
961
    Corundum nic;
Jialin Li's avatar
Jialin Li committed
962
963

    while (!exiting) {
Jialin Li's avatar
Jialin Li committed
964
        poll_h2d(nic);
Jialin Li's avatar
Jialin Li committed
965
        poll_n2d(nic);
Jialin Li's avatar
Jialin Li committed
966
967
968
969
970
    }

    nicsim_cleanup();
    return 0;
}