cxgbe/base: add hardware API for Chelsio T5 series adapters
[dpdk.git] / drivers / net / cxgbe / base / adapter.h
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2014-2015 Chelsio Communications.
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 Chelsio Communications 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 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 /* This file should not be included directly.  Include common.h instead. */
35
36 #ifndef __T4_ADAPTER_H__
37 #define __T4_ADAPTER_H__
38
39 #include <rte_mbuf.h>
40
41 #include "cxgbe_compat.h"
42 #include "t4_regs_values.h"
43
44 enum {
45         MAX_ETH_QSETS = 64,           /* # of Ethernet Tx/Rx queue sets */
46 };
47
48 struct adapter;
49 struct sge_rspq;
50
51 enum {
52         PORT_RSS_DONE = (1 << 0),
53 };
54
55 struct port_info {
56         struct adapter *adapter;        /* adapter that this port belongs to */
57         struct rte_eth_dev *eth_dev;    /* associated rte eth device */
58         struct port_stats stats_base;   /* port statistics base */
59         struct link_config link_cfg;    /* link configuration info */
60
61         unsigned long flags;            /* port related flags */
62         short int xact_addr_filt;       /* index of exact MAC address filter */
63
64         u16    viid;                    /* associated virtual interface id */
65         s8     mdio_addr;               /* address of the PHY */
66         u8     port_type;               /* firmware port type */
67         u8     mod_type;                /* firmware module type */
68         u8     port_id;                 /* physical port ID */
69         u8     tx_chan;                 /* associated channel */
70
71         u8     n_rx_qsets;              /* # of rx qsets */
72         u8     n_tx_qsets;              /* # of tx qsets */
73         u8     first_qset;              /* index of first qset */
74
75         u16    *rss;                    /* rss table */
76         u8     rss_mode;                /* rss mode */
77         u16    rss_size;                /* size of VI's RSS table slice */
78 };
79
80 /* Enable or disable autonegotiation.  If this is set to enable,
81  * the forced link modes above are completely ignored.
82  */
83 #define AUTONEG_DISABLE         0x00
84 #define AUTONEG_ENABLE          0x01
85
86 enum {                                 /* adapter flags */
87         FULL_INIT_DONE     = (1 << 0),
88         USING_MSI          = (1 << 1),
89         USING_MSIX         = (1 << 2),
90         FW_QUEUE_BOUND     = (1 << 3),
91         FW_OK              = (1 << 4),
92         CFG_QUEUES         = (1 << 5),
93         MASTER_PF          = (1 << 6),
94 };
95
96 struct rx_sw_desc {                /* SW state per Rx descriptor */
97         void *buf;                 /* struct page or mbuf */
98         dma_addr_t dma_addr;
99 };
100
101 struct sge_fl {                     /* SGE free-buffer queue state */
102         /* RO fields */
103         struct rx_sw_desc *sdesc;   /* address of SW Rx descriptor ring */
104
105         dma_addr_t addr;            /* bus address of HW ring start */
106         __be64 *desc;               /* address of HW Rx descriptor ring */
107
108         void __iomem *bar2_addr;    /* address of BAR2 Queue registers */
109         unsigned int bar2_qid;      /* Queue ID for BAR2 Queue registers */
110
111         unsigned int cntxt_id;      /* SGE relative QID for the free list */
112         unsigned int size;          /* capacity of free list */
113
114         unsigned int avail;         /* # of available Rx buffers */
115         unsigned int pend_cred;     /* new buffers since last FL DB ring */
116         unsigned int cidx;          /* consumer index */
117         unsigned int pidx;          /* producer index */
118
119         unsigned long alloc_failed; /* # of times buffer allocation failed */
120         unsigned long low;          /* # of times momentarily starving */
121 };
122
123 #define MAX_MBUF_FRAGS (16384 / 512 + 2)
124
125 /* A packet gather list */
126 struct pkt_gl {
127         union {
128                 struct rte_mbuf *mbufs[MAX_MBUF_FRAGS];
129         } /* UNNAMED */;
130         void *va;                         /* virtual address of first byte */
131         unsigned int nfrags;              /* # of fragments */
132         unsigned int tot_len;             /* total length of fragments */
133         bool usembufs;                    /* use mbufs for fragments */
134 };
135
136 typedef int (*rspq_handler_t)(struct sge_rspq *q, const __be64 *rsp,
137                               const struct pkt_gl *gl);
138
139 struct sge_rspq {                   /* state for an SGE response queue */
140         struct adapter *adapter;      /* adapter that this queue belongs to */
141         struct rte_eth_dev *eth_dev;  /* associated rte eth device */
142         struct rte_mempool  *mb_pool; /* associated mempool */
143
144         dma_addr_t phys_addr;       /* physical address of the ring */
145         __be64 *desc;               /* address of HW response ring */
146         const __be64 *cur_desc;     /* current descriptor in queue */
147
148         void __iomem *bar2_addr;    /* address of BAR2 Queue registers */
149         unsigned int bar2_qid;      /* Queue ID for BAR2 Queue registers */
150
151         unsigned int cidx;          /* consumer index */
152         unsigned int iqe_len;       /* entry size */
153         unsigned int size;          /* capacity of response queue */
154         int offset;                 /* offset into current Rx buffer */
155
156         u8 gen;                     /* current generation bit */
157         u8 intr_params;             /* interrupt holdoff parameters */
158         u8 next_intr_params;        /* holdoff params for next interrupt */
159         u8 pktcnt_idx;              /* interrupt packet threshold */
160         u8 idx;                     /* queue index within its group */
161         u16 cntxt_id;               /* SGE relative QID for the response Q */
162         u16 abs_id;                 /* absolute SGE id for the response q */
163
164         rspq_handler_t handler;     /* associated handler for this response q */
165 };
166
167 struct sge_eth_rx_stats {       /* Ethernet rx queue statistics */
168         u64 pkts;               /* # of ethernet packets */
169         u64 rx_bytes;           /* # of ethernet bytes */
170         u64 rx_cso;             /* # of Rx checksum offloads */
171         u64 vlan_ex;            /* # of Rx VLAN extractions */
172         u64 rx_drops;           /* # of packets dropped due to no mem */
173 };
174
175 struct sge_eth_rxq {                /* a SW Ethernet Rx queue */
176         struct sge_rspq rspq;
177         struct sge_fl fl;
178         struct sge_eth_rx_stats stats;
179         bool usembufs;               /* one ingress packet per mbuf FL buffer */
180 } __rte_cache_aligned;
181
182 /*
183  * Currently there are two types of coalesce WR. Type 0 needs 48 bytes per
184  * packet (if one sgl is present) and type 1 needs 32 bytes. This means
185  * that type 0 can fit a maximum of 10 packets per WR and type 1 can fit
186  * 15 packets. We need to keep track of the mbuf pointers in a coalesce WR
187  * to be able to free those mbufs when we get completions back from the FW.
188  * Allocating the maximum number of pointers in every tx desc is a waste
189  * of memory resources so we only store 2 pointers per tx desc which should
190  * be enough since a tx desc can only fit 2 packets in the best case
191  * scenario where a packet needs 32 bytes.
192  */
193 #define ETH_COALESCE_PKT_NUM 15
194 #define ETH_COALESCE_PKT_PER_DESC 2
195
196 struct tx_eth_coal_desc {
197         struct rte_mbuf *mbuf[ETH_COALESCE_PKT_PER_DESC];
198         struct ulptx_sgl *sgl[ETH_COALESCE_PKT_PER_DESC];
199         int idx;
200 };
201
202 struct tx_desc {
203         __be64 flit[8];
204 };
205
206 struct tx_sw_desc {                /* SW state per Tx descriptor */
207         struct rte_mbuf *mbuf;
208         struct ulptx_sgl *sgl;
209         struct tx_eth_coal_desc coalesce;
210 };
211
212 enum {
213         EQ_STOPPED = (1 << 0),
214 };
215
216 struct eth_coalesce {
217         unsigned char *ptr;
218         unsigned char type;
219         unsigned int idx;
220         unsigned int len;
221         unsigned int flits;
222         unsigned int max;
223 };
224
225 struct sge_txq {
226         struct tx_desc *desc;       /* address of HW Tx descriptor ring */
227         struct tx_sw_desc *sdesc;   /* address of SW Tx descriptor ring */
228         struct sge_qstat *stat;     /* queue status entry */
229         struct eth_coalesce coalesce; /* coalesce info */
230
231         uint64_t phys_addr;         /* physical address of the ring */
232
233         void __iomem *bar2_addr;    /* address of BAR2 Queue registers */
234         unsigned int bar2_qid;      /* Queue ID for BAR2 Queue registers */
235
236         unsigned int cntxt_id;     /* SGE relative QID for the Tx Q */
237         unsigned int in_use;       /* # of in-use Tx descriptors */
238         unsigned int size;         /* # of descriptors */
239         unsigned int cidx;         /* SW consumer index */
240         unsigned int pidx;         /* producer index */
241         unsigned int dbidx;        /* last idx when db ring was done */
242         unsigned int equeidx;      /* last sent credit request */
243         unsigned int last_pidx;    /* last pidx recorded by tx monitor */
244         unsigned int last_coal_idx;/* last coal-idx recorded by tx monitor */
245
246         int db_disabled;            /* doorbell state */
247         unsigned short db_pidx;     /* doorbell producer index */
248         unsigned short db_pidx_inc; /* doorbell producer increment */
249 };
250
251 struct sge_eth_tx_stats {       /* Ethernet tx queue statistics */
252         u64 pkts;               /* # of ethernet packets */
253         u64 tx_bytes;           /* # of ethernet bytes */
254         u64 tso;                /* # of TSO requests */
255         u64 tx_cso;             /* # of Tx checksum offloads */
256         u64 vlan_ins;           /* # of Tx VLAN insertions */
257         u64 mapping_err;        /* # of I/O MMU packet mapping errors */
258         u64 coal_wr;            /* # of coalesced wr */
259         u64 coal_pkts;          /* # of coalesced packets */
260 };
261
262 struct sge_eth_txq {                   /* state for an SGE Ethernet Tx queue */
263         struct sge_txq q;
264         struct rte_eth_dev *eth_dev;   /* port that this queue belongs to */
265         struct sge_eth_tx_stats stats; /* queue statistics */
266         rte_spinlock_t txq_lock;
267
268         unsigned int flags;            /* flags for state of the queue */
269 } __rte_cache_aligned;
270
271 struct sge {
272         struct sge_eth_txq ethtxq[MAX_ETH_QSETS];
273         struct sge_eth_rxq ethrxq[MAX_ETH_QSETS];
274         struct sge_rspq fw_evtq __rte_cache_aligned;
275
276         u16 max_ethqsets;           /* # of available Ethernet queue sets */
277         u32 stat_len;               /* length of status page at ring end */
278         u32 pktshift;               /* padding between CPL & packet data */
279
280         /* response queue interrupt parameters */
281         u16 timer_val[SGE_NTIMERS];
282         u8  counter_val[SGE_NCOUNTERS];
283
284         u32 fl_align;               /* response queue message alignment */
285         u32 fl_pg_order;            /* large page allocation size */
286         u32 fl_starve_thres;        /* Free List starvation threshold */
287 };
288
289 #define T4_OS_NEEDS_MBOX_LOCKING 1
290
291 /*
292  * OS Lock/List primitives for those interfaces in the Common Code which
293  * need this.
294  */
295
296 struct mbox_entry {
297         TAILQ_ENTRY(mbox_entry) next;
298 };
299
300 TAILQ_HEAD(mbox_list, mbox_entry);
301
302 struct adapter {
303         struct rte_pci_device *pdev;       /* associated rte pci device */
304         struct rte_eth_dev *eth_dev;       /* first port's rte eth device */
305         struct adapter_params params;      /* adapter parameters */
306         struct port_info port[MAX_NPORTS]; /* ports belonging to this adapter */
307         struct sge sge;                    /* associated SGE */
308
309         /* support for single-threading access to adapter mailbox registers */
310         struct mbox_list mbox_list;
311         rte_spinlock_t mbox_lock;
312
313         u8 *regs;              /* pointer to registers region */
314         u8 *bar2;              /* pointer to bar2 region */
315         unsigned long flags;   /* adapter flags */
316         unsigned int mbox;     /* associated mailbox */
317         unsigned int pf;       /* associated physical function id */
318
319         int use_unpacked_mode; /* unpacked rx mode state */
320 };
321
322 #define CXGBE_PCI_REG(reg) (*((volatile uint32_t *)(reg)))
323
324 static inline uint64_t cxgbe_read_addr64(volatile void *addr)
325 {
326         uint64_t val = CXGBE_PCI_REG(addr);
327         uint64_t val2 = CXGBE_PCI_REG(((volatile uint8_t *)(addr) + 4));
328
329         val2 = (uint64_t)(val2 << 32);
330         val += val2;
331         return val;
332 }
333
334 static inline uint32_t cxgbe_read_addr(volatile void *addr)
335 {
336         return CXGBE_PCI_REG(addr);
337 }
338
339 #define CXGBE_PCI_REG_ADDR(adap, reg) \
340         ((volatile uint32_t *)((char *)(adap)->regs + (reg)))
341
342 #define CXGBE_READ_REG(adap, reg) \
343         cxgbe_read_addr(CXGBE_PCI_REG_ADDR((adap), (reg)))
344
345 #define CXGBE_READ_REG64(adap, reg) \
346         cxgbe_read_addr64(CXGBE_PCI_REG_ADDR((adap), (reg)))
347
348 #define CXGBE_PCI_REG_WRITE(reg, value) ({ \
349         CXGBE_PCI_REG((reg)) = (value); })
350
351 #define CXGBE_WRITE_REG(adap, reg, value) \
352         CXGBE_PCI_REG_WRITE(CXGBE_PCI_REG_ADDR((adap), (reg)), (value))
353
354 static inline uint64_t cxgbe_write_addr64(volatile void *addr, uint64_t val)
355 {
356         CXGBE_PCI_REG(addr) = val;
357         CXGBE_PCI_REG(((volatile uint8_t *)(addr) + 4)) = (val >> 32);
358         return val;
359 }
360
361 #define CXGBE_WRITE_REG64(adap, reg, value) \
362         cxgbe_write_addr64(CXGBE_PCI_REG_ADDR((adap), (reg)), (value))
363
364 /**
365  * t4_read_reg - read a HW register
366  * @adapter: the adapter
367  * @reg_addr: the register address
368  *
369  * Returns the 32-bit value of the given HW register.
370  */
371 static inline u32 t4_read_reg(struct adapter *adapter, u32 reg_addr)
372 {
373         u32 val = CXGBE_READ_REG(adapter, reg_addr);
374
375         CXGBE_DEBUG_REG(adapter, "read register 0x%x value 0x%x\n", reg_addr,
376                         val);
377         return val;
378 }
379
380 /**
381  * t4_write_reg - write a HW register
382  * @adapter: the adapter
383  * @reg_addr: the register address
384  * @val: the value to write
385  *
386  * Write a 32-bit value into the given HW register.
387  */
388 static inline void t4_write_reg(struct adapter *adapter, u32 reg_addr, u32 val)
389 {
390         CXGBE_DEBUG_REG(adapter, "setting register 0x%x to 0x%x\n", reg_addr,
391                         val);
392         CXGBE_WRITE_REG(adapter, reg_addr, val);
393 }
394
395 /**
396  * t4_read_reg64 - read a 64-bit HW register
397  * @adapter: the adapter
398  * @reg_addr: the register address
399  *
400  * Returns the 64-bit value of the given HW register.
401  */
402 static inline u64 t4_read_reg64(struct adapter *adapter, u32 reg_addr)
403 {
404         u64 val = CXGBE_READ_REG64(adapter, reg_addr);
405
406         CXGBE_DEBUG_REG(adapter, "64-bit read register %#x value %#llx\n",
407                         reg_addr, (unsigned long long)val);
408         return val;
409 }
410
411 /**
412  * t4_write_reg64 - write a 64-bit HW register
413  * @adapter: the adapter
414  * @reg_addr: the register address
415  * @val: the value to write
416  *
417  * Write a 64-bit value into the given HW register.
418  */
419 static inline void t4_write_reg64(struct adapter *adapter, u32 reg_addr,
420                                   u64 val)
421 {
422         CXGBE_DEBUG_REG(adapter, "setting register %#x to %#llx\n", reg_addr,
423                         (unsigned long long)val);
424
425         CXGBE_WRITE_REG64(adapter, reg_addr, val);
426 }
427
428 /**
429  * t4_os_set_hw_addr - store a port's MAC address in SW
430  * @adapter: the adapter
431  * @port_idx: the port index
432  * @hw_addr: the Ethernet address
433  *
434  * Store the Ethernet address of the given port in SW.  Called by the
435  * common code when it retrieves a port's Ethernet address from EEPROM.
436  */
437 static inline void t4_os_set_hw_addr(struct adapter *adapter, int port_idx,
438                                      u8 hw_addr[])
439 {
440         struct port_info *pi = &adapter->port[port_idx];
441
442         ether_addr_copy((struct ether_addr *)hw_addr,
443                         &pi->eth_dev->data->mac_addrs[0]);
444 }
445
446 /**
447  * t4_os_lock_init - initialize spinlock
448  * @lock: the spinlock
449  */
450 static inline void t4_os_lock_init(rte_spinlock_t *lock)
451 {
452         rte_spinlock_init(lock);
453 }
454
455 /**
456  * t4_os_lock - spin until lock is acquired
457  * @lock: the spinlock
458  */
459 static inline void t4_os_lock(rte_spinlock_t *lock)
460 {
461         rte_spinlock_lock(lock);
462 }
463
464 /**
465  * t4_os_unlock - unlock a spinlock
466  * @lock: the spinlock
467  */
468 static inline void t4_os_unlock(rte_spinlock_t *lock)
469 {
470         rte_spinlock_unlock(lock);
471 }
472
473 /**
474  * t4_os_init_list_head - initialize
475  * @head: head of list to initialize [to empty]
476  */
477 static inline void t4_os_init_list_head(struct mbox_list *head)
478 {
479         TAILQ_INIT(head);
480 }
481
482 static inline struct mbox_entry *t4_os_list_first_entry(struct mbox_list *head)
483 {
484         return TAILQ_FIRST(head);
485 }
486
487 /**
488  * t4_os_atomic_add_tail - Enqueue list element atomically onto list
489  * @new: the entry to be addded to the queue
490  * @head: current head of the linked list
491  * @lock: lock to use to guarantee atomicity
492  */
493 static inline void t4_os_atomic_add_tail(struct mbox_entry *entry,
494                                          struct mbox_list *head,
495                                          rte_spinlock_t *lock)
496 {
497         t4_os_lock(lock);
498         TAILQ_INSERT_TAIL(head, entry, next);
499         t4_os_unlock(lock);
500 }
501
502 /**
503  * t4_os_atomic_list_del - Dequeue list element atomically from list
504  * @entry: the entry to be remove/dequeued from the list.
505  * @lock: the spinlock
506  */
507 static inline void t4_os_atomic_list_del(struct mbox_entry *entry,
508                                          struct mbox_list *head,
509                                          rte_spinlock_t *lock)
510 {
511         t4_os_lock(lock);
512         TAILQ_REMOVE(head, entry, next);
513         t4_os_unlock(lock);
514 }
515
516 /**
517  * adap2pinfo - return the port_info of a port
518  * @adap: the adapter
519  * @idx: the port index
520  *
521  * Return the port_info structure for the port of the given index.
522  */
523 static inline struct port_info *adap2pinfo(struct adapter *adap, int idx)
524 {
525         return &adap->port[idx];
526 }
527
528 void *t4_alloc_mem(size_t size);
529 void t4_free_mem(void *addr);
530 #define t4_os_alloc(_size)     t4_alloc_mem((_size))
531 #define t4_os_free(_ptr)       t4_free_mem((_ptr))
532
533 void t4_os_portmod_changed(const struct adapter *adap, int port_id);
534 void t4_os_link_changed(struct adapter *adap, int port_id, int link_stat);
535
536 void reclaim_completed_tx(struct sge_txq *q);
537 void t4_free_sge_resources(struct adapter *adap);
538 void t4_sge_tx_monitor_start(struct adapter *adap);
539 void t4_sge_tx_monitor_stop(struct adapter *adap);
540 int t4_eth_xmit(struct sge_eth_txq *txq, struct rte_mbuf *mbuf);
541 int t4_ethrx_handler(struct sge_rspq *q, const __be64 *rsp,
542                      const struct pkt_gl *gl);
543 int t4_sge_init(struct adapter *adap);
544 int t4_sge_alloc_eth_txq(struct adapter *adap, struct sge_eth_txq *txq,
545                          struct rte_eth_dev *eth_dev, uint16_t queue_id,
546                          unsigned int iqid, int socket_id);
547 int t4_sge_alloc_rxq(struct adapter *adap, struct sge_rspq *rspq, bool fwevtq,
548                      struct rte_eth_dev *eth_dev, int intr_idx,
549                      struct sge_fl *fl, rspq_handler_t handler,
550                      int cong, struct rte_mempool *mp, int queue_id,
551                      int socket_id);
552 int t4_sge_eth_txq_start(struct sge_eth_txq *txq);
553 int t4_sge_eth_txq_stop(struct sge_eth_txq *txq);
554 void t4_sge_eth_txq_release(struct adapter *adap, struct sge_eth_txq *txq);
555 int t4_sge_eth_rxq_start(struct adapter *adap, struct sge_rspq *rq);
556 int t4_sge_eth_rxq_stop(struct adapter *adap, struct sge_rspq *rq);
557 void t4_sge_eth_rxq_release(struct adapter *adap, struct sge_eth_rxq *rxq);
558 void t4_sge_eth_clear_queues(struct port_info *pi);
559 int cxgb4_set_rspq_intr_params(struct sge_rspq *q, unsigned int us,
560                                unsigned int cnt);
561 int cxgbe_poll(struct sge_rspq *q, struct rte_mbuf **rx_pkts,
562                unsigned int budget, unsigned int *work_done);
563 int cxgb4_write_rss(const struct port_info *pi, const u16 *queues);
564
565 #endif /* __T4_ADAPTER_H__ */