i40e_bm.h 14.2 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
                void *desc;
                void *data;
                size_t data_len;
                size_t data_capacity;

80
81
                virtual void prepared();
                virtual void processed();
82
83
84
85
86
87
88
89
90
91
92
93
94

            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
    public:
137
        std::string qname;
138
    protected:
139
140
141
142
143
        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
144
145
146
147
148
149
150
        uint64_t base;
        uint32_t len;
        uint32_t &reg_head;
        uint32_t &reg_tail;

        bool enabled;
        size_t desc_len;
151
152

        void ctxs_init();
Antoine Kaufmann's avatar
Antoine Kaufmann committed
153
154

        void trigger_fetch();
155
156
        void trigger_process();
        void trigger_writeback();
Antoine Kaufmann's avatar
Antoine Kaufmann committed
157

158
159
160
        // 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();
161
162
163
164
165
        virtual uint32_t max_writeback_capacity();
        virtual uint32_t max_active_capacity();

        virtual desc_ctx &desc_ctx_create() = 0;

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

169
170
171
172
173
174
        // 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
175
    public:
176
177
        queue_base(const std::string &qname_, uint32_t &reg_head_,
                uint32_t &reg_tail_);
178
        virtual void reset();
Antoine Kaufmann's avatar
Antoine Kaufmann committed
179
        void reg_updated();
180
        bool is_enabled();
Antoine Kaufmann's avatar
Antoine Kaufmann committed
181
182
183
184
};

class queue_admin_tx : public queue_base {
    protected:
185
186
187
188
189
        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
190

191
                virtual void data_written(uint64_t addr, size_t len);
192

193
194
195
196
197
198
199
200
                // 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);
201

202
203
            public:
                admin_desc_ctx(queue_admin_tx &queue_, i40e_bm &dev);
204

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

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

        virtual desc_ctx &desc_ctx_create();
Antoine Kaufmann's avatar
Antoine Kaufmann committed
214
215
216
217
218
219
    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
220
221
222
223
224
225
// host memory cache
class host_mem_cache {
    protected:
        static const uint16_t MAX_SEGMENTS = 0x1000;

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

        i40e_bm &dev;
        segment segs[MAX_SEGMENTS];
234

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

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

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

249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
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();

265
        virtual void interrupt();
266
267
268
269
270
271
272
        virtual void initialize() = 0;

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

        uint32_t reg_dummy_head;

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

class lan_queue_tx : public lan_queue_base {
    protected:
290
291
292
293
294
295
296
297
298
299
300
301
302
        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();
303
                virtual void processed();
304
305
306
        };


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

Antoine Kaufmann's avatar
Antoine Kaufmann committed
320
        uint8_t pktbuf[MTU];
321
        std::deque<tx_desc_ctx *> ready_segments;
Antoine Kaufmann's avatar
Antoine Kaufmann committed
322

323
324
325
326
        bool hwb;
        uint64_t hwb_addr;

        virtual void initialize();
327
        virtual desc_ctx &desc_ctx_create();
328

329
330
331
332
        virtual void do_writeback(uint32_t first_idx, uint32_t first_pos,
                uint32_t cnt);
        bool trigger_tx_packet();
        void trigger_tx();
333

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

class lan_queue_rx : public lan_queue_base {
    protected:
343
344
345
346
347
348
349
350
351
        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);
352
353
        };

354
355
356
357
358
        uint16_t dbuff_size;
        uint16_t hbuff_size;
        uint16_t rxmax;
        bool crc_strip;

359
        std::deque<rx_desc_ctx *> dcache;
360

361
        virtual void initialize();
362
        virtual desc_ctx &desc_ctx_create();
363

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

372
373
374
// rx tx management
class lan {
    protected:
375
376
377
378
        friend class lan_queue_base;
        friend class lan_queue_tx;
        friend class lan_queue_rx;

379
380
        i40e_bm &dev;
        const size_t num_qs;
381
382
383
        lan_queue_rx **rxqs;
        lan_queue_tx **txqs;

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

Antoine Kaufmann's avatar
Antoine Kaufmann committed
392
393
394
395
396
397
398
399
400
401
402
403
404
405
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;
406
    friend class host_mem_cache;
407
408
    friend class lan;
    friend class lan_queue_base;
409
410
    friend class lan_queue_rx;
    friend class lan_queue_tx;
411
    friend class shadow_ram;
Antoine Kaufmann's avatar
Antoine Kaufmann committed
412
413
414
415
416
417

    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;
418
    static const uint32_t NUM_VSIS = 384;
419
    static const uint16_t MAX_MTU = 2048;
Antoine Kaufmann's avatar
Antoine Kaufmann committed
420
421
422
423
424
425

    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
426
        uint32_t pfint_icr0;
Antoine Kaufmann's avatar
Antoine Kaufmann committed
427
428
429
430
431
432
433
434
435
436
437

        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];
438
        uint32_t qtx_tail[NUM_QUEUES];
Antoine Kaufmann's avatar
Antoine Kaufmann committed
439
440
        uint32_t qint_rqctl[NUM_QUEUES];
        uint32_t qrx_ena[NUM_QUEUES];
441
        uint32_t qrx_tail[NUM_QUEUES];
Antoine Kaufmann's avatar
Antoine Kaufmann committed
442

443
444
445
446
447
        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
448
449
450
451
452
453
454
        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
455
456
457
458
459
460
461
462
463
        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
464
465

        uint32_t glqf_hkey[13];
Antoine Kaufmann's avatar
Antoine Kaufmann committed
466
467
468
469
470
471
472
473
474
475
    };

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
476
    virtual uint32_t reg_read32(uint8_t bar, uint64_t addr);
Antoine Kaufmann's avatar
Antoine Kaufmann committed
477
478
    virtual void reg_write(uint8_t bar, uint64_t addr, const void *src,
            size_t len);
Antoine Kaufmann's avatar
Antoine Kaufmann committed
479
    virtual void reg_write32(uint8_t bar, uint64_t addr, uint32_t val);
Antoine Kaufmann's avatar
Antoine Kaufmann committed
480
481
482
483
484
485
    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
486
    host_mem_cache hmc;
Antoine Kaufmann's avatar
Antoine Kaufmann committed
487
    shadow_ram shram;
488
    lan lanmgr;
Antoine Kaufmann's avatar
Antoine Kaufmann committed
489
490
491
492
493
494
495
496
497
498
499

    /** 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);

500
    void reset(bool indicate_done);
Antoine Kaufmann's avatar
Antoine Kaufmann committed
501
502
};

503
504
505
// places the tcp checksum in the packet (assuming ipv4)
void xsum_tcp(void *tcphdr, size_t l4len);

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