corundum_bm.cc 24.3 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
}

16
17
18
19
#define SYNC_PERIOD (500 * 1000ULL) // 100ns
#define PCI_LATENCY (1 * 1000 * 1000ULL) // 1us
#define ETH_LATENCY (1 * 1000 * 1000ULL) // 1us

Jialin Li's avatar
Jialin Li committed
20
static void issue_dma_op(corundum::DMAOp *op);
Jialin Li's avatar
Jialin Li committed
21
static void msi_issue(uint8_t vec);
Jialin Li's avatar
Jialin Li committed
22
23
static void eth_send(void *data, size_t len);

24
25
26
static uint64_t main_time = 0;
static struct nicsim_params nsparams;

Jialin Li's avatar
Jialin Li committed
27
28
29
namespace corundum {

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

DescRing::~DescRing()
{
}


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

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

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

59
ptr_t
Jialin Li's avatar
Jialin Li committed
60
61
62
63
64
DescRing::headPtr()
{
    return this->_headPtr;
}

65
ptr_t
Jialin Li's avatar
Jialin Li committed
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
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
96
    this->_sizeMask = this->_size - 1;
97
    this->cplDma.resize(this->_size, false);
Jialin Li's avatar
Jialin Li committed
98
99
100
101
102
}

void
DescRing::setIndex(unsigned index)
{
103
    assert(!(index & QUEUE_CONT_MASK));
Jialin Li's avatar
Jialin Li committed
104
105
106
    if (index & QUEUE_ARM_MASK) {
        this->armed = true;
    }
107
    this->_index = index & 0xFF;
Jialin Li's avatar
Jialin Li committed
108
109
110
}

void
111
DescRing::setHeadPtr(ptr_t ptr)
Jialin Li's avatar
Jialin Li committed
112
113
114
115
116
{
    this->_headPtr = ptr;
}

void
117
DescRing::setTailPtr(ptr_t ptr)
Jialin Li's avatar
Jialin Li committed
118
119
120
121
{
    this->_tailPtr = ptr;
}

Jialin Li's avatar
Jialin Li committed
122
123
124
bool
DescRing::empty()
{
Jialin Li's avatar
Jialin Li committed
125
    return (this->_headPtr == this->_currTail);
Jialin Li's avatar
Jialin Li committed
126
127
}

Jialin Li's avatar
Jialin Li committed
128
129
130
bool
DescRing::full()
{
Jialin Li's avatar
Jialin Li committed
131
    return (this->_currHead - this->_tailPtr >= this->_size);
Jialin Li's avatar
Jialin Li committed
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

bool
DescRing::updatePtr(ptr_t ptr, bool head)
{
    ptr_t curr_ptr = head ? this->_headPtr : this->_tailPtr;
    if (ptr != curr_ptr) {
        // out of order completion
        this->cplDma[ptr & this->_sizeMask] = true;
        return false;
    }
    /* Safe to update the pointer. Also check if any DMA is completed
     * out-of-order in front of us.
     */
    curr_ptr = ptr & this->_sizeMask;

    do {
        if (head) {
            this->_headPtr++;
        } else {
            this->_tailPtr++;
        }
        this->cplDma[curr_ptr] = false;
        curr_ptr = (curr_ptr + 1) & this->_sizeMask;
    } while (this->cplDma.at(curr_ptr));
    return true;
}

Jialin Li's avatar
Jialin Li committed
161
162
163
164
165
166
167
168
169
170
171
172
173
174
EventRing::EventRing()
{
}

EventRing::~EventRing()
{
}

void
EventRing::dmaDone(DMAOp *op)
{
    assert(op->write);
    switch (op->type) {
    case DMA_TYPE_EVENT:
175
176
177
        if (updatePtr((ptr_t)op->tag, true)) {
            msi_issue(0);
        }
Jialin Li's avatar
Jialin Li committed
178
179
180
181
182
183
184
185
186
187
188
189
        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);
Jialin Li's avatar
Jialin Li committed
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
    if (this->armed) {
        if (full()) {
            fprintf(stderr, "Event ring is rull\n");
            return;
        }
        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++;
        this->armed = false;
Jialin Li's avatar
Jialin Li committed
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
    }
}

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
228
229
    case DMA_TYPE_TX_CPL:
    case DMA_TYPE_RX_CPL: {
230
231
232
233
234
        if (updatePtr((ptr_t)op->tag, true)) {
            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
235
236
        delete op;
        break;
Jialin Li's avatar
Jialin Li committed
237
    }
Jialin Li's avatar
Jialin Li committed
238
239
240
241
242
243
244
    default:
        fprintf(stderr, "Unknown DMA type %u\n", op->type);
        abort();
    }
}

void
Jialin Li's avatar
Jialin Li committed
245
CplRing::complete(unsigned index, size_t len, bool tx)
Jialin Li's avatar
Jialin Li committed
246
{
Jialin Li's avatar
Jialin Li committed
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
    CplData data;
    data.index = index;
    data.len = len;
    data.tx = tx;
    this->pending.push_back(data);
    while (!full() && !this->pending.empty()) {
        CplData &data = this->pending.front();
        addr_t dma_addr = this->_dmaAddr + (this->_currHead & this->_sizeMask) * CPL_SIZE;
        /* Issue DMA write */
        DMAOp *op = new DMAOp;
        op->type = data.tx ? DMA_TYPE_TX_CPL : DMA_TYPE_RX_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 = data.index;
        cpl->len = data.len;
        this->pending.pop_front();
        issue_dma_op(op);
        this->_currHead++;
Jialin Li's avatar
Jialin Li committed
270
271
272
273
274
    }
}

TxRing::TxRing(CplRing *cplRing)
    : txCplRing(cplRing)
Jialin Li's avatar
Jialin Li committed
275
276
277
278
279
280
281
282
{
}

TxRing::~TxRing()
{
}

void
283
TxRing::setHeadPtr(ptr_t ptr)
Jialin Li's avatar
Jialin Li committed
284
285
{
    DescRing::setHeadPtr(ptr);
286
287
    while (this->_currTail != this->_headPtr) {
        unsigned index = this->_currTail & this->_sizeMask;
Jialin Li's avatar
Jialin Li committed
288
289
290
291
292
293
294
        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;
295
        op->tag = this->_currTail;
Jialin Li's avatar
Jialin Li committed
296
297
        op->write = false;
        issue_dma_op(op);
298
        this->_currTail++;
Jialin Li's avatar
Jialin Li committed
299
300
301
302
303
304
305
306
    }
}

void
TxRing::dmaDone(DMAOp *op)
{
    switch (op->type) {
    case DMA_TYPE_DESC: {
Jialin Li's avatar
Jialin Li committed
307
        assert(!op->write);
Jialin Li's avatar
Jialin Li committed
308
309
310
311
312
313
314
315
316
        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
317
        assert(!op->write);
Jialin Li's avatar
Jialin Li committed
318
        eth_send(op->data, op->len);
319
        updatePtr((ptr_t)op->tag, false);
Jialin Li's avatar
Jialin Li committed
320
        this->txCplRing->complete(op->tag, op->len, true);
Jialin Li's avatar
Jialin Li committed
321
322
323
324
325
326
        delete op;
        break;
    default:
        fprintf(stderr, "Unknown DMA type %u\n", op->type);
        abort();
    }
Jialin Li's avatar
Jialin Li committed
327
328
}

Jialin Li's avatar
Jialin Li committed
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
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);
356
        updatePtr((ptr_t)op->tag, false);
Jialin Li's avatar
Jialin Li committed
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
        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
387
388
389
Port::Port()
    : _id(0), _features(0), _mtu(0),
    _schedCount(0), _schedOffset(0), _schedStride(0),
390
391
    _schedType(0), _rssMask(0), _schedEnable(false),
    _queueEnable(false)
Jialin Li's avatar
Jialin Li committed
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
{
}

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

512
513
514
515
516
517
518
519
520
521
522
523
void
Port::queueEnable()
{
    this->_queueEnable = true;
}

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

Jialin Li's avatar
Jialin Li committed
524
Corundum::Corundum()
Jialin Li's avatar
Jialin Li committed
525
    : txCplRing(&this->eventRing), rxCplRing(&this->eventRing),
Jialin Li's avatar
Jialin Li committed
526
    txRing(&this->txCplRing), rxRing(&this->rxCplRing), features(0)
Jialin Li's avatar
Jialin Li committed
527
{
Jialin Li's avatar
Jialin Li committed
528
    this->port.setId(0);
Jialin Li's avatar
Jialin Li committed
529
    this->port.setFeatures(this->features);
Jialin Li's avatar
Jialin Li committed
530
531
532
533
534
535
536
    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
537
538
539
540
541
}

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

Jialin Li's avatar
Jialin Li committed
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
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
569
570
571
572
        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
573
574
575
        case IF_REG_IF_ID:
            return 0;
        case IF_REG_IF_FEATURES:
Jialin Li's avatar
Jialin Li committed
576
            return this->features;
Jialin Li's avatar
Jialin Li committed
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
        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
603
        case EVENT_QUEUE_HEAD_PTR_REG:
Jialin Li's avatar
Jialin Li committed
604
            return this->eventRing.headPtr();
Jialin Li's avatar
Jialin Li committed
605
606
        case TX_QUEUE_ACTIVE_LOG_SIZE_REG:
            return this->txRing.sizeLog();
Jialin Li's avatar
Jialin Li committed
607
608
609
610
        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
611
612
613
614
        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
615
616
617
618
619
620
621
622
623
624
625
626
627
628
        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
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
        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
656
            this->eventRing.setDMALower(val);
Jialin Li's avatar
Jialin Li committed
657
658
            break;
        case EVENT_QUEUE_BASE_ADDR_REG + 4:
Jialin Li's avatar
Jialin Li committed
659
            this->eventRing.setDMAUpper(val);
Jialin Li's avatar
Jialin Li committed
660
661
            break;
        case EVENT_QUEUE_ACTIVE_LOG_SIZE_REG:
Jialin Li's avatar
Jialin Li committed
662
            this->eventRing.setSizeLog(val);
Jialin Li's avatar
Jialin Li committed
663
664
            break;
        case EVENT_QUEUE_INTERRUPT_INDEX_REG:
Jialin Li's avatar
Jialin Li committed
665
            this->eventRing.setIndex(val);
Jialin Li's avatar
Jialin Li committed
666
667
            break;
        case EVENT_QUEUE_HEAD_PTR_REG:
Jialin Li's avatar
Jialin Li committed
668
            this->eventRing.setHeadPtr(val);
Jialin Li's avatar
Jialin Li committed
669
670
            break;
        case EVENT_QUEUE_TAIL_PTR_REG:
Jialin Li's avatar
Jialin Li committed
671
            this->eventRing.setTailPtr(val);
Jialin Li's avatar
Jialin Li committed
672
673
674
675
676
677
678
679
            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
680
            this->txRing.setSizeLog(val);
Jialin Li's avatar
Jialin Li committed
681
            break;
Jialin Li's avatar
Jialin Li committed
682
        case TX_QUEUE_CPL_QUEUE_INDEX_REG:
Jialin Li's avatar
Jialin Li committed
683
684
685
686
687
688
689
690
            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
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
        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;
755
756
757
758
759
760
761
        case PORT_QUEUE_ENABLE:
            if (val) {
                this->port.queueEnable();
            } else {
                this->port.queueDisable();
            }
            break;
Jialin Li's avatar
Jialin Li committed
762
763
764
765
766
767
        default:
            fprintf(stderr, "Unknown register write %lx\n", addr);
            abort();
    }
}

Jialin Li's avatar
Jialin Li committed
768
769
770
771
772
773
void
Corundum::rx(uint8_t port, RxData *rx_data)
{
    this->rxRing.rx(rx_data);
}

Jialin Li's avatar
Jialin Li committed
774
775
} //namespace corundum

Jialin Li's avatar
Jialin Li committed
776
777
using namespace corundum;

Jialin Li's avatar
Jialin Li committed
778
779
static volatile int exiting = 0;

780
static void sigint_handler(int dummy)
Jialin Li's avatar
Jialin Li committed
781
782
783
784
{
    exiting = 1;
}

785
786
787
788
789
static void sigusr1_handler(int dummy)
{
    fprintf(stderr, "main_time = %lu\n", main_time);
}

Jialin Li's avatar
Jialin Li committed
790
791
static volatile union cosim_pcie_proto_d2h *
d2h_alloc(void)
Jialin Li's avatar
Jialin Li committed
792
{
793
794
    volatile union cosim_pcie_proto_d2h *msg =
        nicsim_d2h_alloc(&nsparams, main_time);
Jialin Li's avatar
Jialin Li committed
795
796
797
798
799
800
801
    if (msg == NULL) {
        fprintf(stderr, "d2h_alloc: no entry available\n");
        abort();
    }
    return msg;
}

Jialin Li's avatar
Jialin Li committed
802
803
804
static volatile union cosim_eth_proto_d2n *
d2n_alloc(void)
{
805
806
    volatile union cosim_eth_proto_d2n *msg =
        nicsim_d2n_alloc(&nsparams, main_time);
Jialin Li's avatar
Jialin Li committed
807
808
809
810
811
812
813
814
815
816
817
    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
818
    //printf("issue dma op %p addr %lx len %u\n", op, op->dma_addr, op->len);
Jialin Li's avatar
Jialin Li committed
819
820
821
822
823
824

    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
825
        memcpy((void *)write->data, (void *)op->data, op->len);
Jialin Li's avatar
Jialin Li committed
826
827
828
829
830
831
832
833
834
835
836
837
838
839
        // 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
840
841
842
843
static void
msi_issue(uint8_t vec)
{
    volatile union cosim_pcie_proto_d2h *msg = d2h_alloc();
Jialin Li's avatar
Jialin Li committed
844
    //printf("issue MSI interrupt vec %u\n", vec);
Jialin Li's avatar
Jialin Li committed
845
846
847
848
849
850
851
852
853
    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
854
static void
Jialin Li's avatar
Jialin Li committed
855
h2d_read(Corundum &nic, volatile struct cosim_pcie_proto_h2d_read *read)
Jialin Li's avatar
Jialin Li committed
856
{
Jialin Li's avatar
Jialin Li committed
857
    reg_t val = nic.readReg(read->offset);
Jialin Li's avatar
Jialin Li committed
858
    //printf("read(off=0x%lx, len=%u, val=0x%x)\n", read->offset, read->len, val);
Jialin Li's avatar
Jialin Li committed
859

Jialin Li's avatar
Jialin Li committed
860
861
862
863
864
865
    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
866
867
    memcpy((void *)rc->data, &val, read->len);
    rc->req_id = read->req_id;
Jialin Li's avatar
Jialin Li committed
868
869
870
871
872
873

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

Jialin Li's avatar
Jialin Li committed
874
static void
Jialin Li's avatar
Jialin Li committed
875
h2d_write(Corundum &nic, volatile struct cosim_pcie_proto_h2d_write *write)
Jialin Li's avatar
Jialin Li committed
876
{
Jialin Li's avatar
Jialin Li committed
877
    reg_t val = 0;
Jialin Li's avatar
Jialin Li committed
878
879
    memcpy(&val, (void *)write->data, write->len);

Jialin Li's avatar
Jialin Li committed
880
881
    volatile union cosim_pcie_proto_d2h *msg;
    volatile struct cosim_pcie_proto_d2h_writecomp *wc;
Jialin Li's avatar
Jialin Li committed
882

Jialin Li's avatar
Jialin Li committed
883
884
    msg = d2h_alloc();
    wc = &msg->writecomp;
Jialin Li's avatar
Jialin Li committed
885

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

Jialin Li's avatar
Jialin Li committed
890
891
892
    //WMB();
    wc->own_type = COSIM_PCIE_PROTO_D2H_MSG_WRITECOMP |
        COSIM_PCIE_PROTO_D2H_OWN_HOST;
Jialin Li's avatar
Jialin Li committed
893
894
}

Jialin Li's avatar
Jialin Li committed
895
static void h2d_readcomp(volatile struct cosim_pcie_proto_h2d_readcomp *rc)
Jialin Li's avatar
Jialin Li committed
896
{
Jialin Li's avatar
Jialin Li committed
897
898
899
    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
900
901
}

Jialin Li's avatar
Jialin Li committed
902
static void h2d_writecomp(volatile struct cosim_pcie_proto_h2d_writecomp *wc)
Jialin Li's avatar
Jialin Li committed
903
{
Jialin Li's avatar
Jialin Li committed
904
905
    DMAOp *op = (DMAOp *)(uintptr_t)wc->req_id;
    op->ring->dmaDone(op);
Jialin Li's avatar
Jialin Li committed
906
907
}

Jialin Li's avatar
Jialin Li committed
908
909
static void eth_recv(Corundum &nic,
                     volatile struct cosim_eth_proto_n2d_recv *recv)
Jialin Li's avatar
Jialin Li committed
910
{
Jialin Li's avatar
Jialin Li committed
911
912
913
914
915
    //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
916
917
}

Jialin Li's avatar
Jialin Li committed
918
919
920
921
922
923
924
925
926
927
928
929
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
930
{
931
932
    volatile union cosim_pcie_proto_h2d *msg =
        nicif_h2d_poll(&nsparams, main_time);
Jialin Li's avatar
Jialin Li committed
933
934
935
936
937
938
939
940
    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
941
            h2d_read(nic, &msg->read);
Jialin Li's avatar
Jialin Li committed
942
943
944
            break;

        case COSIM_PCIE_PROTO_H2D_MSG_WRITE:
Jialin Li's avatar
Jialin Li committed
945
            h2d_write(nic, &msg->write);
Jialin Li's avatar
Jialin Li committed
946
947
948
            break;

        case COSIM_PCIE_PROTO_H2D_MSG_READCOMP:
Jialin Li's avatar
Jialin Li committed
949
            h2d_readcomp(&msg->readcomp);
Jialin Li's avatar
Jialin Li committed
950
951
952
            break;

        case COSIM_PCIE_PROTO_H2D_MSG_WRITECOMP:
Jialin Li's avatar
Jialin Li committed
953
            h2d_writecomp(&msg->writecomp);
Jialin Li's avatar
Jialin Li committed
954
955
            break;

Antoine Kaufmann's avatar
Antoine Kaufmann committed
956
957
958
        case COSIM_PCIE_PROTO_H2D_MSG_SYNC:
            break;

Jialin Li's avatar
Jialin Li committed
959
960
961
962
963
964
965
966
        default:
            fprintf(stderr, "poll_h2d: unsupported type=%u\n", type);
    }

    nicif_h2d_done(msg);
    nicif_h2d_next();
}

Jialin Li's avatar
Jialin Li committed
967
static void poll_n2d(Corundum &nic)
Jialin Li's avatar
Jialin Li committed
968
{
969
970
    volatile union cosim_eth_proto_n2d *msg =
        nicif_n2d_poll(&nsparams, main_time);
Jialin Li's avatar
Jialin Li committed
971
972
973
974
975
976
977
978
    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
979
            eth_recv(nic, &msg->recv);
Jialin Li's avatar
Jialin Li committed
980
981
            break;

Antoine Kaufmann's avatar
Antoine Kaufmann committed
982
983
984
        case COSIM_ETH_PROTO_N2D_MSG_SYNC:
            break;

Jialin Li's avatar
Jialin Li committed
985
986
987
988
989
990
991
992
993
994
        default:
            fprintf(stderr, "poll_n2d: unsupported type=%u", t);
    }

    nicif_n2d_done(msg);
    nicif_n2d_next();
}

int main(int argc, char *argv[])
{
995
996
997
998
999
1000
1001
1002
1003
1004
1005
    uint64_t next_ts;

    if (argc != 4 && argc != 5) {
        fprintf(stderr, "Usage: corundum_bm PCI-SOCKET ETH-SOCKET "
                "SHM [START-TICK]\n");
        return EXIT_FAILURE;
    }
    if (argc == 5)
        main_time = strtoull(argv[4], NULL, 0);


Jialin Li's avatar
Jialin Li committed
1006
    signal(SIGINT, sigint_handler);
1007
    signal(SIGUSR1, sigusr1_handler);
Jialin Li's avatar
Jialin Li committed
1008

Jialin Li's avatar
Jialin Li committed
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
    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;

1020
1021
1022
1023
1024
1025
1026
1027
1028
    nsparams.sync_pci = 1;
    nsparams.sync_eth = 1;
    nsparams.pci_socket_path = argv[1];
    nsparams.eth_socket_path = argv[2];
    nsparams.shm_path = argv[3];
    nsparams.pci_latency = PCI_LATENCY;
    nsparams.eth_latency = ETH_LATENCY;
    nsparams.sync_delay = SYNC_PERIOD;
    if (nicsim_init(&nsparams, &di)) {
Jialin Li's avatar
Jialin Li committed
1029
1030
        return EXIT_FAILURE;
    }
1031
1032
    fprintf(stderr, "sync_pci=%d sync_eth=%d\n", nsparams.sync_pci,
        nsparams.sync_eth);
Jialin Li's avatar
Jialin Li committed
1033

Jialin Li's avatar
Jialin Li committed
1034
    Corundum nic;
Jialin Li's avatar
Jialin Li committed
1035
1036

    while (!exiting) {
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
        while (nicsim_sync(&nsparams, main_time)) {
            fprintf(stderr, "warn: nicsim_sync failed (t=%llu)\n", main_time);
        }

        do {
            poll_h2d(nic);
            poll_n2d(nic);
            next_ts = netsim_next_timestamp(&nsparams);
        } while ((nsparams.sync_pci || nsparams.sync_eth) &&
            next_ts <= main_time && !exiting);
        main_time = next_ts;
Jialin Li's avatar
Jialin Li committed
1048
1049
    }

Antoine Kaufmann's avatar
Antoine Kaufmann committed
1050
    fprintf(stderr, "main_time: %lu\n", main_time);
Jialin Li's avatar
Jialin Li committed
1051
1052
1053
    nicsim_cleanup();
    return 0;
}