net/liquidio: support Rx stats
[dpdk.git] / drivers / net / liquidio / lio_struct.h
1 /*
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2017 Cavium, Inc.. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Cavium, Inc. nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER(S) OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #ifndef _LIO_STRUCT_H_
35 #define _LIO_STRUCT_H_
36
37 #include <stdio.h>
38 #include <stdint.h>
39 #include <sys/queue.h>
40
41 #include <rte_spinlock.h>
42 #include <rte_atomic.h>
43
44 #include "lio_hw_defs.h"
45
46 struct lio_stailq_node {
47         STAILQ_ENTRY(lio_stailq_node) entries;
48 };
49
50 STAILQ_HEAD(lio_stailq_head, lio_stailq_node);
51
52 struct lio_version {
53         uint16_t major;
54         uint16_t minor;
55         uint16_t micro;
56         uint16_t reserved;
57 };
58
59 /** Output Queue statistics. Each output queue has four stats fields. */
60 struct lio_droq_stats {
61         /** Number of packets received in this queue. */
62         uint64_t pkts_received;
63
64         /** Bytes received by this queue. */
65         uint64_t bytes_received;
66
67         /** Packets dropped due to no memory available. */
68         uint64_t dropped_nomem;
69
70         /** Packets dropped due to large number of pkts to process. */
71         uint64_t dropped_toomany;
72
73         /** Number of packets  sent to stack from this queue. */
74         uint64_t rx_pkts_received;
75
76         /** Number of Bytes sent to stack from this queue. */
77         uint64_t rx_bytes_received;
78
79         /** Num of Packets dropped due to receive path failures. */
80         uint64_t rx_dropped;
81
82         /** Num of vxlan packets received; */
83         uint64_t rx_vxlan;
84
85         /** Num of failures of lio_recv_buffer_alloc() */
86         uint64_t rx_alloc_failure;
87
88 };
89
90 /** The Descriptor Ring Output Queue structure.
91  *  This structure has all the information required to implement a
92  *  DROQ.
93  */
94 struct lio_droq {
95         /** A spinlock to protect access to this ring. */
96         rte_spinlock_t lock;
97
98         uint32_t q_no;
99
100         uint32_t pkt_count;
101
102         struct lio_device *lio_dev;
103
104         /** The 8B aligned descriptor ring starts at this address. */
105         struct lio_droq_desc *desc_ring;
106
107         /** Index in the ring where the driver should read the next packet */
108         uint32_t read_idx;
109
110         /** Index in the ring where Octeon will write the next packet */
111         uint32_t write_idx;
112
113         /** Index in the ring where the driver will refill the descriptor's
114          * buffer
115          */
116         uint32_t refill_idx;
117
118         /** Packets pending to be processed */
119         rte_atomic64_t pkts_pending;
120
121         /** Number of  descriptors in this ring. */
122         uint32_t max_count;
123
124         /** The number of descriptors pending refill. */
125         uint32_t refill_count;
126
127         uint32_t refill_threshold;
128
129         /** The 8B aligned info ptrs begin from this address. */
130         struct lio_droq_info *info_list;
131
132         /** The receive buffer list. This list has the virtual addresses of the
133          *  buffers.
134          */
135         struct lio_recv_buffer *recv_buf_list;
136
137         /** The size of each buffer pointed by the buffer pointer. */
138         uint32_t buffer_size;
139
140         /** Pointer to the mapped packet credit register.
141          *  Host writes number of info/buffer ptrs available to this register
142          */
143         void *pkts_credit_reg;
144
145         /** Pointer to the mapped packet sent register.
146          *  Octeon writes the number of packets DMA'ed to host memory
147          *  in this register.
148          */
149         void *pkts_sent_reg;
150
151         /** Statistics for this DROQ. */
152         struct lio_droq_stats stats;
153
154         /** DMA mapped address of the DROQ descriptor ring. */
155         size_t desc_ring_dma;
156
157         /** Info ptr list are allocated at this virtual address. */
158         size_t info_base_addr;
159
160         /** DMA mapped address of the info list */
161         size_t info_list_dma;
162
163         /** Allocated size of info list. */
164         uint32_t info_alloc_size;
165
166         /** Memory zone **/
167         const struct rte_memzone *desc_ring_mz;
168         const struct rte_memzone *info_mz;
169         struct rte_mempool *mpool;
170 };
171
172 /** Receive Header */
173 union octeon_rh {
174 #if RTE_BYTE_ORDER == RTE_BIG_ENDIAN
175         uint64_t rh64;
176         struct  {
177                 uint64_t opcode : 4;
178                 uint64_t subcode : 8;
179                 uint64_t len : 3; /** additional 64-bit words */
180                 uint64_t reserved : 17;
181                 uint64_t ossp : 32; /** opcode/subcode specific parameters */
182         } r;
183         struct  {
184                 uint64_t opcode : 4;
185                 uint64_t subcode : 8;
186                 uint64_t len : 3; /** additional 64-bit words */
187                 uint64_t extra : 28;
188                 uint64_t vlan : 12;
189                 uint64_t priority : 3;
190                 uint64_t csum_verified : 3; /** checksum verified. */
191                 uint64_t has_hwtstamp : 1; /** Has hardware timestamp.1 = yes.*/
192                 uint64_t encap_on : 1;
193                 uint64_t has_hash : 1; /** Has hash (rth or rss). 1 = yes. */
194         } r_dh;
195         struct {
196                 uint64_t opcode : 4;
197                 uint64_t subcode : 8;
198                 uint64_t len : 3; /** additional 64-bit words */
199                 uint64_t reserved : 8;
200                 uint64_t extra : 25;
201                 uint64_t gmxport : 16;
202         } r_nic_info;
203 #else
204         uint64_t rh64;
205         struct {
206                 uint64_t ossp : 32; /** opcode/subcode specific parameters */
207                 uint64_t reserved : 17;
208                 uint64_t len : 3; /** additional 64-bit words */
209                 uint64_t subcode : 8;
210                 uint64_t opcode : 4;
211         } r;
212         struct {
213                 uint64_t has_hash : 1; /** Has hash (rth or rss). 1 = yes. */
214                 uint64_t encap_on : 1;
215                 uint64_t has_hwtstamp : 1;  /** 1 = has hwtstamp */
216                 uint64_t csum_verified : 3; /** checksum verified. */
217                 uint64_t priority : 3;
218                 uint64_t vlan : 12;
219                 uint64_t extra : 28;
220                 uint64_t len : 3; /** additional 64-bit words */
221                 uint64_t subcode : 8;
222                 uint64_t opcode : 4;
223         } r_dh;
224         struct {
225                 uint64_t gmxport : 16;
226                 uint64_t extra : 25;
227                 uint64_t reserved : 8;
228                 uint64_t len : 3; /** additional 64-bit words */
229                 uint64_t subcode : 8;
230                 uint64_t opcode : 4;
231         } r_nic_info;
232 #endif
233 };
234
235 #define OCTEON_RH_SIZE (sizeof(union octeon_rh))
236
237 /** The txpciq info passed to host from the firmware */
238 union octeon_txpciq {
239         uint64_t txpciq64;
240
241         struct {
242 #if RTE_BYTE_ORDER == RTE_BIG_ENDIAN
243                 uint64_t q_no : 8;
244                 uint64_t port : 8;
245                 uint64_t pkind : 6;
246                 uint64_t use_qpg : 1;
247                 uint64_t qpg : 11;
248                 uint64_t aura_num : 10;
249                 uint64_t reserved : 20;
250 #else
251                 uint64_t reserved : 20;
252                 uint64_t aura_num : 10;
253                 uint64_t qpg : 11;
254                 uint64_t use_qpg : 1;
255                 uint64_t pkind : 6;
256                 uint64_t port : 8;
257                 uint64_t q_no : 8;
258 #endif
259         } s;
260 };
261
262 /** The instruction (input) queue.
263  *  The input queue is used to post raw (instruction) mode data or packet
264  *  data to Octeon device from the host. Each input queue for
265  *  a LIO device has one such structure to represent it.
266  */
267 struct lio_instr_queue {
268         /** A spinlock to protect access to the input ring.  */
269         rte_spinlock_t lock;
270
271         rte_spinlock_t post_lock;
272
273         struct lio_device *lio_dev;
274
275         uint32_t pkt_in_done;
276
277         rte_atomic64_t iq_flush_running;
278
279         /** Flag that indicates if the queue uses 64 byte commands. */
280         uint32_t iqcmd_64B:1;
281
282         /** Queue info. */
283         union octeon_txpciq txpciq;
284
285         uint32_t rsvd:17;
286
287         uint32_t status:8;
288
289         /** Maximum no. of instructions in this queue. */
290         uint32_t max_count;
291
292         /** Index in input ring where the driver should write the next packet */
293         uint32_t host_write_index;
294
295         /** Index in input ring where Octeon is expected to read the next
296          *  packet.
297          */
298         uint32_t lio_read_index;
299
300         /** This index aids in finding the window in the queue where Octeon
301          *  has read the commands.
302          */
303         uint32_t flush_index;
304
305         /** This field keeps track of the instructions pending in this queue. */
306         rte_atomic64_t instr_pending;
307
308         /** Pointer to the Virtual Base addr of the input ring. */
309         uint8_t *base_addr;
310
311         struct lio_request_list *request_list;
312
313         /** Octeon doorbell register for the ring. */
314         void *doorbell_reg;
315
316         /** Octeon instruction count register for this ring. */
317         void *inst_cnt_reg;
318
319         /** Number of instructions pending to be posted to Octeon. */
320         uint32_t fill_cnt;
321
322         /** DMA mapped base address of the input descriptor ring. */
323         uint64_t base_addr_dma;
324
325         /** Application context */
326         void *app_ctx;
327
328         /* network stack queue index */
329         int q_index;
330
331         /* Memory zone */
332         const struct rte_memzone *iq_mz;
333 };
334
335 /** This structure is used by driver to store information required
336  *  to free the mbuff when the packet has been fetched by Octeon.
337  *  Bytes offset below assume worst-case of a 64-bit system.
338  */
339 struct lio_buf_free_info {
340         /** Bytes 1-8. Pointer to network device private structure. */
341         struct lio_device *lio_dev;
342
343         /** Bytes 9-16. Pointer to mbuff. */
344         struct rte_mbuf *mbuf;
345
346         /** Bytes 17-24. Pointer to gather list. */
347         struct lio_gather *g;
348
349         /** Bytes 25-32. Physical address of mbuf->data or gather list. */
350         uint64_t dptr;
351
352         /** Bytes 33-47. Piggybacked soft command, if any */
353         struct lio_soft_command *sc;
354
355         /** Bytes 48-63. iq no */
356         uint64_t iq_no;
357 };
358
359 /* The Scatter-Gather List Entry. The scatter or gather component used with
360  * input instruction has this format.
361  */
362 struct lio_sg_entry {
363         /** The first 64 bit gives the size of data in each dptr. */
364         union {
365                 uint16_t size[4];
366                 uint64_t size64;
367         } u;
368
369         /** The 4 dptr pointers for this entry. */
370         uint64_t ptr[4];
371 };
372
373 #define LIO_SG_ENTRY_SIZE       (sizeof(struct lio_sg_entry))
374
375 /** Structure of a node in list of gather components maintained by
376  *  driver for each network device.
377  */
378 struct lio_gather {
379         /** List manipulation. Next and prev pointers. */
380         struct lio_stailq_node list;
381
382         /** Size of the gather component at sg in bytes. */
383         int sg_size;
384
385         /** Number of bytes that sg was adjusted to make it 8B-aligned. */
386         int adjust;
387
388         /** Gather component that can accommodate max sized fragment list
389          *  received from the IP layer.
390          */
391         struct lio_sg_entry *sg;
392 };
393
394 struct lio_rss_ctx {
395         uint16_t hash_key_size;
396         uint8_t  hash_key[LIO_RSS_MAX_KEY_SZ];
397         /* Ideally a factor of number of queues */
398         uint8_t  itable[LIO_RSS_MAX_TABLE_SZ];
399         uint8_t  itable_size;
400         uint8_t  ip;
401         uint8_t  tcp_hash;
402         uint8_t  ipv6;
403         uint8_t  ipv6_tcp_hash;
404         uint8_t  ipv6_ex;
405         uint8_t  ipv6_tcp_ex_hash;
406         uint8_t  hash_disable;
407 };
408
409 struct lio_io_enable {
410         uint64_t iq;
411         uint64_t oq;
412         uint64_t iq64B;
413 };
414
415 struct lio_fn_list {
416         void (*setup_iq_regs)(struct lio_device *, uint32_t);
417         void (*setup_oq_regs)(struct lio_device *, uint32_t);
418
419         int (*setup_mbox)(struct lio_device *);
420         void (*free_mbox)(struct lio_device *);
421
422         int (*setup_device_regs)(struct lio_device *);
423         int (*enable_io_queues)(struct lio_device *);
424         void (*disable_io_queues)(struct lio_device *);
425 };
426
427 struct lio_pf_vf_hs_word {
428 #if RTE_BYTE_ORDER == RTE_LITTLE_ENDIAN
429         /** PKIND value assigned for the DPI interface */
430         uint64_t pkind : 8;
431
432         /** OCTEON core clock multiplier */
433         uint64_t core_tics_per_us : 16;
434
435         /** OCTEON coprocessor clock multiplier */
436         uint64_t coproc_tics_per_us : 16;
437
438         /** app that currently running on OCTEON */
439         uint64_t app_mode : 8;
440
441         /** RESERVED */
442         uint64_t reserved : 16;
443
444 #elif RTE_BYTE_ORDER == RTE_BIG_ENDIAN
445
446         /** RESERVED */
447         uint64_t reserved : 16;
448
449         /** app that currently running on OCTEON */
450         uint64_t app_mode : 8;
451
452         /** OCTEON coprocessor clock multiplier */
453         uint64_t coproc_tics_per_us : 16;
454
455         /** OCTEON core clock multiplier */
456         uint64_t core_tics_per_us : 16;
457
458         /** PKIND value assigned for the DPI interface */
459         uint64_t pkind : 8;
460 #endif
461 };
462
463 struct lio_sriov_info {
464         /** Number of rings assigned to VF */
465         uint32_t rings_per_vf;
466
467         /** Number of VF devices enabled */
468         uint32_t num_vfs;
469 };
470
471 /* Head of a response list */
472 struct lio_response_list {
473         /** List structure to add delete pending entries to */
474         struct lio_stailq_head head;
475
476         /** A lock for this response list */
477         rte_spinlock_t lock;
478
479         rte_atomic64_t pending_req_count;
480 };
481
482 /* Structure to define the configuration attributes for each Input queue. */
483 struct lio_iq_config {
484         /* Max number of IQs available */
485         uint8_t max_iqs;
486
487         /** Pending list size (usually set to the sum of the size of all Input
488          *  queues)
489          */
490         uint32_t pending_list_size;
491
492         /** Command size - 32 or 64 bytes */
493         uint32_t instr_type;
494 };
495
496 /* Structure to define the configuration attributes for each Output queue. */
497 struct lio_oq_config {
498         /* Max number of OQs available */
499         uint8_t max_oqs;
500
501         /** If set, the Output queue uses info-pointer mode. (Default: 1 ) */
502         uint32_t info_ptr;
503
504         /** The number of buffers that were consumed during packet processing by
505          *  the driver on this Output queue before the driver attempts to
506          *  replenish the descriptor ring with new buffers.
507          */
508         uint32_t refill_threshold;
509 };
510
511 /* Structure to define the configuration. */
512 struct lio_config {
513         uint16_t card_type;
514         const char *card_name;
515
516         /** Input Queue attributes. */
517         struct lio_iq_config iq;
518
519         /** Output Queue attributes. */
520         struct lio_oq_config oq;
521
522         int num_nic_ports;
523
524         int num_def_tx_descs;
525
526         /* Num of desc for rx rings */
527         int num_def_rx_descs;
528
529         int def_rx_buf_size;
530 };
531
532 /** Status of a RGMII Link on Octeon as seen by core driver. */
533 union octeon_link_status {
534         uint64_t link_status64;
535
536         struct {
537 #if RTE_BYTE_ORDER == RTE_BIG_ENDIAN
538                 uint64_t duplex : 8;
539                 uint64_t mtu : 16;
540                 uint64_t speed : 16;
541                 uint64_t link_up : 1;
542                 uint64_t autoneg : 1;
543                 uint64_t if_mode : 5;
544                 uint64_t pause : 1;
545                 uint64_t flashing : 1;
546                 uint64_t reserved : 15;
547 #else
548                 uint64_t reserved : 15;
549                 uint64_t flashing : 1;
550                 uint64_t pause : 1;
551                 uint64_t if_mode : 5;
552                 uint64_t autoneg : 1;
553                 uint64_t link_up : 1;
554                 uint64_t speed : 16;
555                 uint64_t mtu : 16;
556                 uint64_t duplex : 8;
557 #endif
558         } s;
559 };
560
561 /** The rxpciq info passed to host from the firmware */
562 union octeon_rxpciq {
563         uint64_t rxpciq64;
564
565         struct {
566 #if RTE_BYTE_ORDER == RTE_BIG_ENDIAN
567                 uint64_t q_no : 8;
568                 uint64_t reserved : 56;
569 #else
570                 uint64_t reserved : 56;
571                 uint64_t q_no : 8;
572 #endif
573         } s;
574 };
575
576 /** Information for a OCTEON ethernet interface shared between core & host. */
577 struct octeon_link_info {
578         union octeon_link_status link;
579         uint64_t hw_addr;
580
581 #if RTE_BYTE_ORDER == RTE_BIG_ENDIAN
582         uint64_t gmxport : 16;
583         uint64_t macaddr_is_admin_assigned : 1;
584         uint64_t vlan_is_admin_assigned : 1;
585         uint64_t rsvd : 30;
586         uint64_t num_txpciq : 8;
587         uint64_t num_rxpciq : 8;
588 #else
589         uint64_t num_rxpciq : 8;
590         uint64_t num_txpciq : 8;
591         uint64_t rsvd : 30;
592         uint64_t vlan_is_admin_assigned : 1;
593         uint64_t macaddr_is_admin_assigned : 1;
594         uint64_t gmxport : 16;
595 #endif
596
597         union octeon_txpciq txpciq[LIO_MAX_IOQS_PER_IF];
598         union octeon_rxpciq rxpciq[LIO_MAX_IOQS_PER_IF];
599 };
600
601 /* -----------------------  THE LIO DEVICE  --------------------------- */
602 /** The lio device.
603  *  Each lio device has this structure to represent all its
604  *  components.
605  */
606 struct lio_device {
607         /** PCI device pointer */
608         struct rte_pci_device *pci_dev;
609
610         /** Octeon Chip type */
611         uint16_t chip_id;
612         uint16_t pf_num;
613         uint16_t vf_num;
614
615         /** This device's PCIe port used for traffic. */
616         uint16_t pcie_port;
617
618         /** The state of this device */
619         rte_atomic64_t status;
620
621         uint8_t intf_open;
622
623         struct octeon_link_info linfo;
624
625         uint8_t *hw_addr;
626
627         struct lio_fn_list fn_list;
628
629         uint32_t num_iqs;
630
631         /** Guards each glist */
632         rte_spinlock_t *glist_lock;
633         /** Array of gather component linked lists */
634         struct lio_stailq_head *glist_head;
635
636         /* The pool containing pre allocated buffers used for soft commands */
637         struct rte_mempool *sc_buf_pool;
638
639         /** The input instruction queues */
640         struct lio_instr_queue *instr_queue[LIO_MAX_POSSIBLE_INSTR_QUEUES];
641
642         /** The singly-linked tail queues of instruction response */
643         struct lio_response_list response_list;
644
645         uint32_t num_oqs;
646
647         /** The DROQ output queues  */
648         struct lio_droq *droq[LIO_MAX_POSSIBLE_OUTPUT_QUEUES];
649
650         struct lio_io_enable io_qmask;
651
652         struct lio_sriov_info sriov_info;
653
654         struct lio_pf_vf_hs_word pfvf_hsword;
655
656         /** Mail Box details of each lio queue. */
657         struct lio_mbox **mbox;
658
659         char dev_string[LIO_DEVICE_NAME_LEN]; /* Device print string */
660
661         const struct lio_config *default_config;
662
663         struct rte_eth_dev      *eth_dev;
664
665         uint64_t ifflags;
666         uint8_t max_rx_queues;
667         uint8_t max_tx_queues;
668         uint8_t nb_rx_queues;
669         uint8_t nb_tx_queues;
670         uint8_t port_configured;
671         struct lio_rss_ctx rss_state;
672         uint8_t port_id;
673 };
674 #endif /* _LIO_STRUCT_H_ */