i40e_bm.h 14.1 KB
Newer Older
Antoine Kaufmann's avatar
Antoine Kaufmann committed
1
2
#pragma once

3
#include <deque>
Antoine Kaufmann's avatar
Antoine Kaufmann committed
4
5
6
7
8
9
#include <stdint.h>
extern "C" {
#include <cosim_pcie_proto.h>
}
#include <nicbm.h>

10
11
12
13
14
15
//#define DEBUG_DEV
//#define DEBUG_ADMINQ
//#define DEBUG_LAN
//#define DEBUG_HMC
//#define DEBUG_QUEUES

16
struct i40e_aq_desc;
17
struct i40e_tx_desc;
18

Antoine Kaufmann's avatar
Antoine Kaufmann committed
19
20
21
namespace i40e {

class i40e_bm;
22
class lan;
Antoine Kaufmann's avatar
Antoine Kaufmann committed
23
24
25
26
27
28
29

class dma_base : public nicbm::DMAOp {
    public:
        /** i40e_bm will call this when dma is done */
        virtual void done() = 0;
};

30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/**
 * Base-class for descriptor queues (RX/TX, Admin RX/TX).
 *
 * Descriptor processing is split up into multiple phases:
 *
 *      - fetch: descriptor is read from host memory. This can be done in
 *        batches, while the batch sizes is limited by the minimum of
 *        MAX_ACTIVE_DESCS, max_active_capacity(), and max_fetch_capacity().
 *        Fetch is implemented by this base class.
 *
 *      - prepare: to be implemented in the sub class, but typically involves
 *        fetching buffer contents. Not guaranteed to happen in order. If
 *        overriden subclass must call desc_prepared() when done.
 *
 *      - process: to be implemented in the sub class. Guaranteed to be called
 *        in order. In case of tx, this actually sends the packet, in rx
 *        processing finishes when a packet for a descriptor has been received.
 *        subclass must call desc_processed() when done.
 *
 *      - write back: descriptor is written back to host-memory. Write-back
 *        capacity
 */
Antoine Kaufmann's avatar
Antoine Kaufmann committed
52
53
class queue_base {
    protected:
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
        static const uint32_t MAX_ACTIVE_DESCS = 128;

        class desc_ctx {
            friend class queue_base;
            public:
                enum state {
                    DESC_EMPTY,
                    DESC_FETCHING,
                    DESC_PREPARING,
                    DESC_PREPARED,
                    DESC_PROCESSING,
                    DESC_PROCESSED,
                    DESC_WRITING_BACK,
                    DESC_WRITTEN_BACK,
                };

Antoine Kaufmann's avatar
Antoine Kaufmann committed
70
71
72
            protected:
                queue_base &queue;
            public:
73
                enum state state;
Antoine Kaufmann's avatar
Antoine Kaufmann committed
74
                uint32_t index;
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
                void *desc;
                void *data;
                size_t data_len;
                size_t data_capacity;

                void prepared();
                void processed();

            protected:
                void data_fetch(uint64_t addr, size_t len);
                virtual void data_fetched(uint64_t addr, size_t len);
                void data_write(uint64_t addr, size_t len, const void *buf);
                virtual void data_written(uint64_t addr, size_t len);

            public:
                desc_ctx(queue_base &queue_);
                virtual ~desc_ctx();

                virtual void prepare();
                virtual void process() = 0;
Antoine Kaufmann's avatar
Antoine Kaufmann committed
95
96
        };

97
        class dma_fetch : public dma_base {
98
99
100
            protected:
                queue_base &queue;
            public:
101
102
103
                uint32_t pos;
                dma_fetch(queue_base &queue_, size_t len);
                virtual ~dma_fetch();
104
105
106
                virtual void done();
        };

Antoine Kaufmann's avatar
Antoine Kaufmann committed
107
108
109
110
        class dma_wb : public dma_base {
            protected:
                queue_base &queue;
            public:
111
                uint32_t pos;
Antoine Kaufmann's avatar
Antoine Kaufmann committed
112
113
114
115
116
                dma_wb(queue_base &queue_, size_t len);
                virtual ~dma_wb();
                virtual void done();
        };

117
118
119
120
121
122
123
124
125
126
        class dma_data_fetch : public dma_base {
            protected:
                desc_ctx &ctx;

            public:
                dma_data_fetch(desc_ctx &ctx_, size_t len, void *buffer);
                virtual ~dma_data_fetch();
                virtual void done();
        };

127
128
        class dma_data_wb : public dma_base {
            protected:
129
                desc_ctx &ctx;
130
            public:
131
                dma_data_wb(desc_ctx &ctx_, size_t len);
132
133
134
135
                virtual ~dma_data_wb();
                virtual void done();
        };

136
        std::string qname;
137
138
139
140
141
        desc_ctx *desc_ctxs[MAX_ACTIVE_DESCS];
        uint32_t active_first_pos;
        uint32_t active_first_idx;
        uint32_t active_cnt;

Antoine Kaufmann's avatar
Antoine Kaufmann committed
142
143
144
145
146
147
148
        uint64_t base;
        uint32_t len;
        uint32_t &reg_head;
        uint32_t &reg_tail;

        bool enabled;
        size_t desc_len;
149
150

        void ctxs_init();
Antoine Kaufmann's avatar
Antoine Kaufmann committed
151
152

        void trigger_fetch();
153
154
        void trigger_process();
        void trigger_writeback();
Antoine Kaufmann's avatar
Antoine Kaufmann committed
155

156
157
158
        // returns how many descriptors the queue can fetch max during the next
        // fetch: default UINT32_MAX, but can be overriden by child classes
        virtual uint32_t max_fetch_capacity();
159
160
161
162
163
        virtual uint32_t max_writeback_capacity();
        virtual uint32_t max_active_capacity();

        virtual desc_ctx &desc_ctx_create() = 0;

164
165
        // dummy function, needs to be overriden if interrupts are required
        virtual void interrupt();
Antoine Kaufmann's avatar
Antoine Kaufmann committed
166

167
168
169
170
171
172
        // this does the actual write-back. Can be overridden
        virtual void do_writeback(uint32_t first_idx, uint32_t first_pos,
                uint32_t cnt);

        // called by dma op when writeback has completed
        void writeback_done(uint32_t first_pos, uint32_t cnt);
Antoine Kaufmann's avatar
Antoine Kaufmann committed
173
    public:
174
175
        queue_base(const std::string &qname_, uint32_t &reg_head_,
                uint32_t &reg_tail_);
176
        virtual void reset();
Antoine Kaufmann's avatar
Antoine Kaufmann committed
177
        void reg_updated();
178
        bool is_enabled();
Antoine Kaufmann's avatar
Antoine Kaufmann committed
179
180
181
182
};

class queue_admin_tx : public queue_base {
    protected:
183
184
185
186
187
        class admin_desc_ctx : public desc_ctx {
            protected:
                queue_admin_tx &aq;
                i40e_bm &dev;
                struct i40e_aq_desc *d;
Antoine Kaufmann's avatar
Antoine Kaufmann committed
188

189
                virtual void data_written(uint64_t addr, size_t len);
190

191
192
193
194
195
196
197
198
                // prepare completion descriptor (fills flags, and return value)
                void desc_compl_prepare(uint16_t retval, uint16_t extra_flags);
                // complete direct response
                void desc_complete(uint16_t retval, uint16_t extra_flags = 0);
                // complete indirect response
                void desc_complete_indir(uint16_t retval, const void *data,
                        size_t len, uint16_t extra_flags = 0,
                        bool ignore_datalen=false);
199

200
201
            public:
                admin_desc_ctx(queue_admin_tx &queue_, i40e_bm &dev);
202

203
204
205
                virtual void prepare();
                virtual void process();
        };
Antoine Kaufmann's avatar
Antoine Kaufmann committed
206

207
        i40e_bm &dev;
Antoine Kaufmann's avatar
Antoine Kaufmann committed
208
209
        uint64_t &reg_base;
        uint32_t &reg_len;
210
211

        virtual desc_ctx &desc_ctx_create();
Antoine Kaufmann's avatar
Antoine Kaufmann committed
212
213
214
215
216
217
    public:
        queue_admin_tx(i40e_bm &dev_, uint64_t &reg_base_,
                uint32_t &reg_len_, uint32_t &reg_head_, uint32_t &reg_tail_);
        void reg_updated();
};

Antoine Kaufmann's avatar
Antoine Kaufmann committed
218
219
220
221
222
223
// host memory cache
class host_mem_cache {
    protected:
        static const uint16_t MAX_SEGMENTS = 0x1000;

        struct segment {
224
            uint64_t addr;
Antoine Kaufmann's avatar
Antoine Kaufmann committed
225
226
227
228
229
230
231
            uint16_t pgcount;
            bool valid;
            bool direct;
        };

        i40e_bm &dev;
        segment segs[MAX_SEGMENTS];
232

Antoine Kaufmann's avatar
Antoine Kaufmann committed
233
    public:
234
235
236
237
238
        class mem_op : public dma_base {
            public:
                bool failed;
        };

Antoine Kaufmann's avatar
Antoine Kaufmann committed
239
        host_mem_cache(i40e_bm &dev);
240
        void reset();
Antoine Kaufmann's avatar
Antoine Kaufmann committed
241
        void reg_updated(uint64_t addr);
242
243
244

        // issue a hmc memory operation (address is in the context
        void issue_mem_op(mem_op &op);
Antoine Kaufmann's avatar
Antoine Kaufmann committed
245
246
};

247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
class lan_queue_base : public queue_base {
    protected:
        class qctx_fetch : public host_mem_cache::mem_op {
            public:
                lan_queue_base &lq;

                qctx_fetch(lan_queue_base &lq_);
                virtual void done();
        };


        lan &lanmgr;

        void ctx_fetched();
        void ctx_written_back();

263
        virtual void interrupt();
264
265
266
267
268
269
270
        virtual void initialize() = 0;

    public:
        bool enabling;
        size_t idx;
        uint32_t &reg_ena;
        uint32_t &fpm_basereg;
271
        uint32_t &reg_intqctl;
272
273
274
275
276
        size_t ctx_size;
        void *ctx;

        uint32_t reg_dummy_head;

277
278
        lan_queue_base(lan &lanmgr_, const std::string &qtype, uint32_t &reg_tail,
                size_t idx_,
279
280
                uint32_t &reg_ena_, uint32_t &fpm_basereg, uint32_t &reg_intqctl,
                uint16_t ctx_size);
281
        virtual void reset();
282
283
284
285
286
287
        void enable();
        void disable();
};

class lan_queue_tx : public lan_queue_base {
    protected:
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
        static const uint16_t MTU = 2048;

        class tx_desc_ctx : public desc_ctx {
            protected:
                lan_queue_tx &tq;

            public:
                i40e_tx_desc *d;

                tx_desc_ctx(lan_queue_tx &queue_);

                virtual void prepare();
                virtual void process();
        };


304
305
306
307
        class dma_hwb : public dma_base {
            protected:
                lan_queue_tx &queue;
            public:
308
309
                uint32_t pos;
                uint32_t cnt;
310
                uint32_t next_head;
311
312
                dma_hwb(lan_queue_tx &queue_, uint32_t pos, uint32_t cnt,
                        uint32_t next_head);
313
314
315
316
                virtual ~dma_hwb();
                virtual void done();
        };

Antoine Kaufmann's avatar
Antoine Kaufmann committed
317
        uint8_t pktbuf[MTU];
318
        std::deque<tx_desc_ctx *> ready_segments;
Antoine Kaufmann's avatar
Antoine Kaufmann committed
319

320
321
322
323
        bool hwb;
        uint64_t hwb_addr;

        virtual void initialize();
324
        virtual desc_ctx &desc_ctx_create();
325

326
327
328
329
        virtual void do_writeback(uint32_t first_idx, uint32_t first_pos,
                uint32_t cnt);
        bool trigger_tx_packet();
        void trigger_tx();
330

331
332
    public:
        lan_queue_tx(lan &lanmgr_, uint32_t &reg_tail, size_t idx,
333
334
                uint32_t &reg_ena, uint32_t &fpm_basereg,
                uint32_t &reg_intqctl);
Antoine Kaufmann's avatar
Antoine Kaufmann committed
335
        virtual void reset();
336
337
338
339
};

class lan_queue_rx : public lan_queue_base {
    protected:
340
341
342
343
344
345
346
347
348
        class rx_desc_ctx : public desc_ctx {
            protected:
                lan_queue_rx &rq;
                virtual void data_written(uint64_t addr, size_t len);

            public:
                rx_desc_ctx(lan_queue_rx &queue_);
                virtual void process();
                void packet_received(const void *data, size_t len);
349
350
        };

351
352
353
354
355
        uint16_t dbuff_size;
        uint16_t hbuff_size;
        uint16_t rxmax;
        bool crc_strip;

356
        std::deque<rx_desc_ctx *> dcache;
357

358
        virtual void initialize();
359
        virtual desc_ctx &desc_ctx_create();
360

361
362
    public:
        lan_queue_rx(lan &lanmgr_, uint32_t &reg_tail, size_t idx,
363
364
                uint32_t &reg_ena, uint32_t &fpm_basereg,
                uint32_t &reg_intqctl);
365
        virtual void reset();
366
        void packet_received(const void *data, size_t len);
367
368
};

369
370
371
// rx tx management
class lan {
    protected:
372
373
374
375
        friend class lan_queue_base;
        friend class lan_queue_tx;
        friend class lan_queue_rx;

376
377
        i40e_bm &dev;
        const size_t num_qs;
378
379
380
        lan_queue_rx **rxqs;
        lan_queue_tx **txqs;

381
382
    public:
        lan(i40e_bm &dev, size_t num_qs);
383
        void reset();
384
385
        void qena_updated(uint16_t idx, bool rx);
        void tail_updated(uint16_t idx, bool rx);
386
        void packet_received(const void *data, size_t len);
387
};
Antoine Kaufmann's avatar
Antoine Kaufmann committed
388

Antoine Kaufmann's avatar
Antoine Kaufmann committed
389
390
391
392
393
394
395
396
397
398
399
400
401
402
class shadow_ram {
    protected:
        i40e_bm &dev;

    public:
        shadow_ram(i40e_bm &dev);
        void reg_updated();
        uint16_t read(uint16_t addr);
        void write(uint16_t addr, uint16_t val);
};

class i40e_bm : public nicbm::Runner::Device {
protected:
    friend class queue_admin_tx;
403
    friend class host_mem_cache;
404
405
    friend class lan;
    friend class lan_queue_base;
406
407
    friend class lan_queue_rx;
    friend class lan_queue_tx;
408
    friend class shadow_ram;
Antoine Kaufmann's avatar
Antoine Kaufmann committed
409
410
411
412
413
414

    static const unsigned BAR_REGS = 0;
    static const unsigned BAR_IO = 2;

    static const uint32_t NUM_QUEUES = 1536;
    static const uint32_t NUM_PFINTS = 512;
415
    static const uint32_t NUM_VSIS = 384;
416
    static const uint16_t MAX_MTU = 2048;
Antoine Kaufmann's avatar
Antoine Kaufmann committed
417
418
419
420
421
422

    struct i40e_regs {
        uint32_t glgen_rstctl;
        uint32_t gllan_rctl_0;
        uint32_t pfint_lnklst0;
        uint32_t pfint_icr0_ena;
Antoine Kaufmann's avatar
Antoine Kaufmann committed
423
        uint32_t pfint_icr0;
Antoine Kaufmann's avatar
Antoine Kaufmann committed
424
425
426
427
428
429
430
431
432
433
434

        uint32_t pfint_dyn_ctln[NUM_PFINTS - 1];
        uint32_t pfint_lnklstn[NUM_PFINTS - 1];
        uint32_t pfint_raten[NUM_PFINTS - 1];
        uint32_t gllan_txpre_qdis[12];

        uint32_t glnvm_srctl;
        uint32_t glnvm_srdata;

        uint32_t qint_tqctl[NUM_QUEUES];
        uint32_t qtx_ena[NUM_QUEUES];
435
        uint32_t qtx_tail[NUM_QUEUES];
Antoine Kaufmann's avatar
Antoine Kaufmann committed
436
437
        uint32_t qint_rqctl[NUM_QUEUES];
        uint32_t qrx_ena[NUM_QUEUES];
438
        uint32_t qrx_tail[NUM_QUEUES];
Antoine Kaufmann's avatar
Antoine Kaufmann committed
439

440
441
442
443
444
        uint32_t glhmc_lantxbase[16];
        uint32_t glhmc_lantxcnt[16];
        uint32_t glhmc_lanrxbase[16];
        uint32_t glhmc_lanrxcnt[16];

Antoine Kaufmann's avatar
Antoine Kaufmann committed
445
446
447
448
449
450
451
        uint32_t pfhmc_sdcmd;
        uint32_t pfhmc_sddatalow;
        uint32_t pfhmc_sddatahigh;
        uint32_t pfhmc_pdinv;
        uint32_t pfhmc_errorinfo;
        uint32_t pfhmc_errordata;

Antoine Kaufmann's avatar
Antoine Kaufmann committed
452
453
454
455
456
457
458
459
460
        uint64_t pf_atqba;
        uint32_t pf_atqlen;
        uint32_t pf_atqh;
        uint32_t pf_atqt;

        uint64_t pf_arqba;
        uint32_t pf_arqlen;
        uint32_t pf_arqh;
        uint32_t pf_arqt;
Antoine Kaufmann's avatar
Antoine Kaufmann committed
461
462

        uint32_t glqf_hkey[13];
Antoine Kaufmann's avatar
Antoine Kaufmann committed
463
464
465
466
467
468
469
470
471
472
    };

public:
    nicbm::Runner *runner;

    i40e_bm();
    ~i40e_bm();

    virtual void setup_intro(struct cosim_pcie_proto_dev_intro &di);
    virtual void reg_read(uint8_t bar, uint64_t addr, void *dest, size_t len);
Antoine Kaufmann's avatar
Antoine Kaufmann committed
473
    virtual uint32_t reg_read32(uint8_t bar, uint64_t addr);
Antoine Kaufmann's avatar
Antoine Kaufmann committed
474
475
    virtual void reg_write(uint8_t bar, uint64_t addr, const void *src,
            size_t len);
Antoine Kaufmann's avatar
Antoine Kaufmann committed
476
    virtual void reg_write32(uint8_t bar, uint64_t addr, uint32_t val);
Antoine Kaufmann's avatar
Antoine Kaufmann committed
477
478
479
480
481
482
    virtual void dma_complete(nicbm::DMAOp &op);
    virtual void eth_rx(uint8_t port, const void *data, size_t len);

protected:
    i40e_regs regs;
    queue_admin_tx pf_atq;
Antoine Kaufmann's avatar
Antoine Kaufmann committed
483
    host_mem_cache hmc;
Antoine Kaufmann's avatar
Antoine Kaufmann committed
484
    shadow_ram shram;
485
    lan lanmgr;
Antoine Kaufmann's avatar
Antoine Kaufmann committed
486
487
488
489
490
491
492
493
494
495
496

    /** Read from the I/O bar */
    virtual uint32_t reg_io_read(uint64_t addr);
    /** Write to the I/O bar */
    virtual void reg_io_write(uint64_t addr, uint32_t val);

    /** 32-bit read from the memory bar (should be the default) */
    virtual uint32_t reg_mem_read32(uint64_t addr);
    /** 32-bit write to the memory bar (should be the default) */
    virtual void reg_mem_write32(uint64_t addr, uint32_t val);

497
    void reset(bool indicate_done);
Antoine Kaufmann's avatar
Antoine Kaufmann committed
498
499
};

500
501
502
// places the tcp checksum in the packet (assuming ipv4)
void xsum_tcp(void *tcphdr, size_t l4len);

Antoine Kaufmann's avatar
Antoine Kaufmann committed
503
} // namespace corundum