net/cxgbe: update Tx and Rx path for VF
[dpdk.git] / drivers / net / cxgbe / base / adapter.h
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2014-2017 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_bus_pci.h>
40 #include <rte_mbuf.h>
41 #include <rte_io.h>
42
43 #include "cxgbe_compat.h"
44 #include "t4_regs_values.h"
45
46 enum {
47         MAX_ETH_QSETS = 64,           /* # of Ethernet Tx/Rx queue sets */
48 };
49
50 struct adapter;
51 struct sge_rspq;
52
53 enum {
54         PORT_RSS_DONE = (1 << 0),
55 };
56
57 struct port_info {
58         struct adapter *adapter;        /* adapter that this port belongs to */
59         struct rte_eth_dev *eth_dev;    /* associated rte eth device */
60         struct port_stats stats_base;   /* port statistics base */
61         struct link_config link_cfg;    /* link configuration info */
62
63         unsigned long flags;            /* port related flags */
64         short int xact_addr_filt;       /* index of exact MAC address filter */
65
66         u16    viid;                    /* associated virtual interface id */
67         s8     mdio_addr;               /* address of the PHY */
68         u8     port_type;               /* firmware port type */
69         u8     mod_type;                /* firmware module type */
70         u8     port_id;                 /* physical port ID */
71         u8     pidx;                    /* port index for this PF */
72         u8     tx_chan;                 /* associated channel */
73
74         u8     n_rx_qsets;              /* # of rx qsets */
75         u8     n_tx_qsets;              /* # of tx qsets */
76         u8     first_qset;              /* index of first qset */
77
78         u16    *rss;                    /* rss table */
79         u8     rss_mode;                /* rss mode */
80         u16    rss_size;                /* size of VI's RSS table slice */
81         u64    rss_hf;                  /* RSS Hash Function */
82 };
83
84 /* Enable or disable autonegotiation.  If this is set to enable,
85  * the forced link modes above are completely ignored.
86  */
87 #define AUTONEG_DISABLE         0x00
88 #define AUTONEG_ENABLE          0x01
89
90 enum {                                 /* adapter flags */
91         FULL_INIT_DONE     = (1 << 0),
92         USING_MSI          = (1 << 1),
93         USING_MSIX         = (1 << 2),
94         FW_QUEUE_BOUND     = (1 << 3),
95         FW_OK              = (1 << 4),
96         CFG_QUEUES         = (1 << 5),
97         MASTER_PF          = (1 << 6),
98 };
99
100 struct rx_sw_desc {                /* SW state per Rx descriptor */
101         void *buf;                 /* struct page or mbuf */
102         dma_addr_t dma_addr;
103 };
104
105 struct sge_fl {                     /* SGE free-buffer queue state */
106         /* RO fields */
107         struct rx_sw_desc *sdesc;   /* address of SW Rx descriptor ring */
108
109         dma_addr_t addr;            /* bus address of HW ring start */
110         __be64 *desc;               /* address of HW Rx descriptor ring */
111
112         void __iomem *bar2_addr;    /* address of BAR2 Queue registers */
113         unsigned int bar2_qid;      /* Queue ID for BAR2 Queue registers */
114
115         unsigned int cntxt_id;      /* SGE relative QID for the free list */
116         unsigned int size;          /* capacity of free list */
117
118         unsigned int avail;         /* # of available Rx buffers */
119         unsigned int pend_cred;     /* new buffers since last FL DB ring */
120         unsigned int cidx;          /* consumer index */
121         unsigned int pidx;          /* producer index */
122
123         unsigned long alloc_failed; /* # of times buffer allocation failed */
124         unsigned long low;          /* # of times momentarily starving */
125 };
126
127 #define MAX_MBUF_FRAGS (16384 / 512 + 2)
128
129 /* A packet gather list */
130 struct pkt_gl {
131         union {
132                 struct rte_mbuf *mbufs[MAX_MBUF_FRAGS];
133         } /* UNNAMED */;
134         void *va;                         /* virtual address of first byte */
135         unsigned int nfrags;              /* # of fragments */
136         unsigned int tot_len;             /* total length of fragments */
137         bool usembufs;                    /* use mbufs for fragments */
138 };
139
140 typedef int (*rspq_handler_t)(struct sge_rspq *q, const __be64 *rsp,
141                               const struct pkt_gl *gl);
142
143 struct sge_rspq {                   /* state for an SGE response queue */
144         struct adapter *adapter;      /* adapter that this queue belongs to */
145         struct rte_eth_dev *eth_dev;  /* associated rte eth device */
146         struct rte_mempool  *mb_pool; /* associated mempool */
147
148         dma_addr_t phys_addr;       /* physical address of the ring */
149         __be64 *desc;               /* address of HW response ring */
150         const __be64 *cur_desc;     /* current descriptor in queue */
151
152         void __iomem *bar2_addr;    /* address of BAR2 Queue registers */
153         unsigned int bar2_qid;      /* Queue ID for BAR2 Queue registers */
154         struct sge_qstat *stat;
155
156         unsigned int cidx;          /* consumer index */
157         unsigned int gts_idx;       /* last gts write sent */
158         unsigned int iqe_len;       /* entry size */
159         unsigned int size;          /* capacity of response queue */
160         int offset;                 /* offset into current Rx buffer */
161
162         u8 gen;                     /* current generation bit */
163         u8 intr_params;             /* interrupt holdoff parameters */
164         u8 next_intr_params;        /* holdoff params for next interrupt */
165         u8 pktcnt_idx;              /* interrupt packet threshold */
166         u8 port_id;                 /* associated port-id */
167         u8 idx;                     /* queue index within its group */
168         u16 cntxt_id;               /* SGE relative QID for the response Q */
169         u16 abs_id;                 /* absolute SGE id for the response q */
170
171         rspq_handler_t handler;     /* associated handler for this response q */
172 };
173
174 struct sge_eth_rx_stats {       /* Ethernet rx queue statistics */
175         u64 pkts;               /* # of ethernet packets */
176         u64 rx_bytes;           /* # of ethernet bytes */
177         u64 rx_cso;             /* # of Rx checksum offloads */
178         u64 vlan_ex;            /* # of Rx VLAN extractions */
179         u64 rx_drops;           /* # of packets dropped due to no mem */
180 };
181
182 struct sge_eth_rxq {                /* a SW Ethernet Rx queue */
183         struct sge_rspq rspq;
184         struct sge_fl fl;
185         struct sge_eth_rx_stats stats;
186         bool usembufs;               /* one ingress packet per mbuf FL buffer */
187 } __rte_cache_aligned;
188
189 /*
190  * Currently there are two types of coalesce WR. Type 0 needs 48 bytes per
191  * packet (if one sgl is present) and type 1 needs 32 bytes. This means
192  * that type 0 can fit a maximum of 10 packets per WR and type 1 can fit
193  * 15 packets. We need to keep track of the mbuf pointers in a coalesce WR
194  * to be able to free those mbufs when we get completions back from the FW.
195  * Allocating the maximum number of pointers in every tx desc is a waste
196  * of memory resources so we only store 2 pointers per tx desc which should
197  * be enough since a tx desc can only fit 2 packets in the best case
198  * scenario where a packet needs 32 bytes.
199  */
200 #define ETH_COALESCE_PKT_NUM 15
201 #define ETH_COALESCE_VF_PKT_NUM 7
202 #define ETH_COALESCE_PKT_PER_DESC 2
203
204 struct tx_eth_coal_desc {
205         struct rte_mbuf *mbuf[ETH_COALESCE_PKT_PER_DESC];
206         struct ulptx_sgl *sgl[ETH_COALESCE_PKT_PER_DESC];
207         int idx;
208 };
209
210 struct tx_desc {
211         __be64 flit[8];
212 };
213
214 struct tx_sw_desc {                /* SW state per Tx descriptor */
215         struct rte_mbuf *mbuf;
216         struct ulptx_sgl *sgl;
217         struct tx_eth_coal_desc coalesce;
218 };
219
220 enum {
221         EQ_STOPPED = (1 << 0),
222 };
223
224 struct eth_coalesce {
225         unsigned char *ptr;
226         unsigned char type;
227         unsigned int idx;
228         unsigned int len;
229         unsigned int flits;
230         unsigned int max;
231         __u8 ethmacdst[ETHER_ADDR_LEN];
232         __u8 ethmacsrc[ETHER_ADDR_LEN];
233         __be16 ethtype;
234         __be16 vlantci;
235 };
236
237 struct sge_txq {
238         struct tx_desc *desc;       /* address of HW Tx descriptor ring */
239         struct tx_sw_desc *sdesc;   /* address of SW Tx descriptor ring */
240         struct sge_qstat *stat;     /* queue status entry */
241         struct eth_coalesce coalesce; /* coalesce info */
242
243         uint64_t phys_addr;         /* physical address of the ring */
244
245         void __iomem *bar2_addr;    /* address of BAR2 Queue registers */
246         unsigned int bar2_qid;      /* Queue ID for BAR2 Queue registers */
247
248         unsigned int cntxt_id;     /* SGE relative QID for the Tx Q */
249         unsigned int in_use;       /* # of in-use Tx descriptors */
250         unsigned int size;         /* # of descriptors */
251         unsigned int cidx;         /* SW consumer index */
252         unsigned int pidx;         /* producer index */
253         unsigned int dbidx;        /* last idx when db ring was done */
254         unsigned int equeidx;      /* last sent credit request */
255         unsigned int last_pidx;    /* last pidx recorded by tx monitor */
256         unsigned int last_coal_idx;/* last coal-idx recorded by tx monitor */
257         unsigned int abs_id;
258
259         int db_disabled;            /* doorbell state */
260         unsigned short db_pidx;     /* doorbell producer index */
261         unsigned short db_pidx_inc; /* doorbell producer increment */
262 };
263
264 struct sge_eth_tx_stats {       /* Ethernet tx queue statistics */
265         u64 pkts;               /* # of ethernet packets */
266         u64 tx_bytes;           /* # of ethernet bytes */
267         u64 tso;                /* # of TSO requests */
268         u64 tx_cso;             /* # of Tx checksum offloads */
269         u64 vlan_ins;           /* # of Tx VLAN insertions */
270         u64 mapping_err;        /* # of I/O MMU packet mapping errors */
271         u64 coal_wr;            /* # of coalesced wr */
272         u64 coal_pkts;          /* # of coalesced packets */
273 };
274
275 struct sge_eth_txq {                   /* state for an SGE Ethernet Tx queue */
276         struct sge_txq q;
277         struct rte_eth_dev *eth_dev;   /* port that this queue belongs to */
278         struct rte_eth_dev_data *data;
279         struct sge_eth_tx_stats stats; /* queue statistics */
280         rte_spinlock_t txq_lock;
281
282         unsigned int flags;            /* flags for state of the queue */
283 } __rte_cache_aligned;
284
285 struct sge {
286         struct sge_eth_txq ethtxq[MAX_ETH_QSETS];
287         struct sge_eth_rxq ethrxq[MAX_ETH_QSETS];
288         struct sge_rspq fw_evtq __rte_cache_aligned;
289
290         u16 max_ethqsets;           /* # of available Ethernet queue sets */
291         u32 stat_len;               /* length of status page at ring end */
292         u32 pktshift;               /* padding between CPL & packet data */
293
294         /* response queue interrupt parameters */
295         u16 timer_val[SGE_NTIMERS];
296         u8  counter_val[SGE_NCOUNTERS];
297
298         u32 fl_align;               /* response queue message alignment */
299         u32 fl_pg_order;            /* large page allocation size */
300         u32 fl_starve_thres;        /* Free List starvation threshold */
301 };
302
303 #define T4_OS_NEEDS_MBOX_LOCKING 1
304
305 /*
306  * OS Lock/List primitives for those interfaces in the Common Code which
307  * need this.
308  */
309
310 struct mbox_entry {
311         TAILQ_ENTRY(mbox_entry) next;
312 };
313
314 TAILQ_HEAD(mbox_list, mbox_entry);
315
316 struct adapter {
317         struct rte_pci_device *pdev;       /* associated rte pci device */
318         struct rte_eth_dev *eth_dev;       /* first port's rte eth device */
319         struct adapter_params params;      /* adapter parameters */
320         struct port_info *port[MAX_NPORTS];/* ports belonging to this adapter */
321         struct sge sge;                    /* associated SGE */
322
323         /* support for single-threading access to adapter mailbox registers */
324         struct mbox_list mbox_list;
325         rte_spinlock_t mbox_lock;
326
327         u8 *regs;              /* pointer to registers region */
328         u8 *bar2;              /* pointer to bar2 region */
329         unsigned long flags;   /* adapter flags */
330         unsigned int mbox;     /* associated mailbox */
331         unsigned int pf;       /* associated physical function id */
332
333         unsigned int vpd_busy;
334         unsigned int vpd_flag;
335
336         int use_unpacked_mode; /* unpacked rx mode state */
337 };
338
339 /**
340  * adap2pinfo - return the port_info of a port
341  * @adap: the adapter
342  * @idx: the port index
343  *
344  * Return the port_info structure for the port of the given index.
345  */
346 static inline struct port_info *adap2pinfo(const struct adapter *adap, int idx)
347 {
348         return adap->port[idx];
349 }
350
351 #define CXGBE_PCI_REG(reg) rte_read32(reg)
352
353 static inline uint64_t cxgbe_read_addr64(volatile void *addr)
354 {
355         uint64_t val = CXGBE_PCI_REG(addr);
356         uint64_t val2 = CXGBE_PCI_REG(((volatile uint8_t *)(addr) + 4));
357
358         val2 = (uint64_t)(val2 << 32);
359         val += val2;
360         return val;
361 }
362
363 static inline uint32_t cxgbe_read_addr(volatile void *addr)
364 {
365         return CXGBE_PCI_REG(addr);
366 }
367
368 #define CXGBE_PCI_REG_ADDR(adap, reg) \
369         ((volatile uint32_t *)((char *)(adap)->regs + (reg)))
370
371 #define CXGBE_READ_REG(adap, reg) \
372         cxgbe_read_addr(CXGBE_PCI_REG_ADDR((adap), (reg)))
373
374 #define CXGBE_READ_REG64(adap, reg) \
375         cxgbe_read_addr64(CXGBE_PCI_REG_ADDR((adap), (reg)))
376
377 #define CXGBE_PCI_REG_WRITE(reg, value) rte_write32((value), (reg))
378
379 #define CXGBE_PCI_REG_WRITE_RELAXED(reg, value) \
380         rte_write32_relaxed((value), (reg))
381
382 #define CXGBE_WRITE_REG(adap, reg, value) \
383         CXGBE_PCI_REG_WRITE(CXGBE_PCI_REG_ADDR((adap), (reg)), (value))
384
385 #define CXGBE_WRITE_REG_RELAXED(adap, reg, value) \
386         CXGBE_PCI_REG_WRITE_RELAXED(CXGBE_PCI_REG_ADDR((adap), (reg)), (value))
387
388 static inline uint64_t cxgbe_write_addr64(volatile void *addr, uint64_t val)
389 {
390         CXGBE_PCI_REG_WRITE(addr, val);
391         CXGBE_PCI_REG_WRITE(((volatile uint8_t *)(addr) + 4), (val >> 32));
392         return val;
393 }
394
395 #define CXGBE_WRITE_REG64(adap, reg, value) \
396         cxgbe_write_addr64(CXGBE_PCI_REG_ADDR((adap), (reg)), (value))
397
398 /**
399  * t4_read_reg - read a HW register
400  * @adapter: the adapter
401  * @reg_addr: the register address
402  *
403  * Returns the 32-bit value of the given HW register.
404  */
405 static inline u32 t4_read_reg(struct adapter *adapter, u32 reg_addr)
406 {
407         u32 val = CXGBE_READ_REG(adapter, reg_addr);
408
409         CXGBE_DEBUG_REG(adapter, "read register 0x%x value 0x%x\n", reg_addr,
410                         val);
411         return val;
412 }
413
414 /**
415  * t4_write_reg - write a HW register with barrier
416  * @adapter: the adapter
417  * @reg_addr: the register address
418  * @val: the value to write
419  *
420  * Write a 32-bit value into the given HW register.
421  */
422 static inline void t4_write_reg(struct adapter *adapter, u32 reg_addr, u32 val)
423 {
424         CXGBE_DEBUG_REG(adapter, "setting register 0x%x to 0x%x\n", reg_addr,
425                         val);
426         CXGBE_WRITE_REG(adapter, reg_addr, val);
427 }
428
429 /**
430  * t4_write_reg_relaxed - write a HW register with no barrier
431  * @adapter: the adapter
432  * @reg_addr: the register address
433  * @val: the value to write
434  *
435  * Write a 32-bit value into the given HW register.
436  */
437 static inline void t4_write_reg_relaxed(struct adapter *adapter, u32 reg_addr,
438                                         u32 val)
439 {
440         CXGBE_DEBUG_REG(adapter, "setting register 0x%x to 0x%x\n", reg_addr,
441                         val);
442         CXGBE_WRITE_REG_RELAXED(adapter, reg_addr, val);
443 }
444
445 /**
446  * t4_read_reg64 - read a 64-bit HW register
447  * @adapter: the adapter
448  * @reg_addr: the register address
449  *
450  * Returns the 64-bit value of the given HW register.
451  */
452 static inline u64 t4_read_reg64(struct adapter *adapter, u32 reg_addr)
453 {
454         u64 val = CXGBE_READ_REG64(adapter, reg_addr);
455
456         CXGBE_DEBUG_REG(adapter, "64-bit read register %#x value %#llx\n",
457                         reg_addr, (unsigned long long)val);
458         return val;
459 }
460
461 /**
462  * t4_write_reg64 - write a 64-bit HW register
463  * @adapter: the adapter
464  * @reg_addr: the register address
465  * @val: the value to write
466  *
467  * Write a 64-bit value into the given HW register.
468  */
469 static inline void t4_write_reg64(struct adapter *adapter, u32 reg_addr,
470                                   u64 val)
471 {
472         CXGBE_DEBUG_REG(adapter, "setting register %#x to %#llx\n", reg_addr,
473                         (unsigned long long)val);
474
475         CXGBE_WRITE_REG64(adapter, reg_addr, val);
476 }
477
478 #define PCI_STATUS              0x06    /* 16 bits */
479 #define PCI_STATUS_CAP_LIST     0x10    /* Support Capability List */
480 #define PCI_CAPABILITY_LIST     0x34
481 /* Offset of first capability list entry */
482 #define PCI_CAP_ID_EXP          0x10    /* PCI Express */
483 #define PCI_CAP_LIST_ID         0       /* Capability ID */
484 #define PCI_CAP_LIST_NEXT       1       /* Next capability in the list */
485 #define PCI_EXP_DEVCTL          0x0008  /* Device control */
486 #define PCI_EXP_DEVCTL2         40      /* Device Control 2 */
487 #define PCI_EXP_DEVCTL_EXT_TAG  0x0100  /* Extended Tag Field Enable */
488 #define PCI_EXP_DEVCTL_PAYLOAD  0x00E0  /* Max payload */
489 #define PCI_CAP_ID_VPD          0x03    /* Vital Product Data */
490 #define PCI_VPD_ADDR            2       /* Address to access (15 bits!) */
491 #define PCI_VPD_ADDR_F          0x8000  /* Write 0, 1 indicates completion */
492 #define PCI_VPD_DATA            4       /* 32-bits of data returned here */
493
494 /**
495  * t4_os_pci_write_cfg4 - 32-bit write to PCI config space
496  * @adapter: the adapter
497  * @addr: the register address
498  * @val: the value to write
499  *
500  * Write a 32-bit value into the given register in PCI config space.
501  */
502 static inline void t4_os_pci_write_cfg4(struct adapter *adapter, size_t addr,
503                                         off_t val)
504 {
505         u32 val32 = val;
506
507         if (rte_pci_write_config(adapter->pdev, &val32, sizeof(val32),
508                                      addr) < 0)
509                 dev_err(adapter, "Can't write to PCI config space\n");
510 }
511
512 /**
513  * t4_os_pci_read_cfg4 - read a 32-bit value from PCI config space
514  * @adapter: the adapter
515  * @addr: the register address
516  * @val: where to store the value read
517  *
518  * Read a 32-bit value from the given register in PCI config space.
519  */
520 static inline void t4_os_pci_read_cfg4(struct adapter *adapter, size_t addr,
521                                        u32 *val)
522 {
523         if (rte_pci_read_config(adapter->pdev, val, sizeof(*val),
524                                     addr) < 0)
525                 dev_err(adapter, "Can't read from PCI config space\n");
526 }
527
528 /**
529  * t4_os_pci_write_cfg2 - 16-bit write to PCI config space
530  * @adapter: the adapter
531  * @addr: the register address
532  * @val: the value to write
533  *
534  * Write a 16-bit value into the given register in PCI config space.
535  */
536 static inline void t4_os_pci_write_cfg2(struct adapter *adapter, size_t addr,
537                                         off_t val)
538 {
539         u16 val16 = val;
540
541         if (rte_pci_write_config(adapter->pdev, &val16, sizeof(val16),
542                                      addr) < 0)
543                 dev_err(adapter, "Can't write to PCI config space\n");
544 }
545
546 /**
547  * t4_os_pci_read_cfg2 - read a 16-bit value from PCI config space
548  * @adapter: the adapter
549  * @addr: the register address
550  * @val: where to store the value read
551  *
552  * Read a 16-bit value from the given register in PCI config space.
553  */
554 static inline void t4_os_pci_read_cfg2(struct adapter *adapter, size_t addr,
555                                        u16 *val)
556 {
557         if (rte_pci_read_config(adapter->pdev, val, sizeof(*val),
558                                     addr) < 0)
559                 dev_err(adapter, "Can't read from PCI config space\n");
560 }
561
562 /**
563  * t4_os_pci_read_cfg - read a 8-bit value from PCI config space
564  * @adapter: the adapter
565  * @addr: the register address
566  * @val: where to store the value read
567  *
568  * Read a 8-bit value from the given register in PCI config space.
569  */
570 static inline void t4_os_pci_read_cfg(struct adapter *adapter, size_t addr,
571                                       u8 *val)
572 {
573         if (rte_pci_read_config(adapter->pdev, val, sizeof(*val),
574                                     addr) < 0)
575                 dev_err(adapter, "Can't read from PCI config space\n");
576 }
577
578 /**
579  * t4_os_find_pci_capability - lookup a capability in the PCI capability list
580  * @adapter: the adapter
581  * @cap: the capability
582  *
583  * Return the address of the given capability within the PCI capability list.
584  */
585 static inline int t4_os_find_pci_capability(struct adapter *adapter, int cap)
586 {
587         u16 status;
588         int ttl = 48;
589         u8 pos = 0;
590         u8 id = 0;
591
592         t4_os_pci_read_cfg2(adapter, PCI_STATUS, &status);
593         if (!(status & PCI_STATUS_CAP_LIST)) {
594                 dev_err(adapter, "PCIe capability reading failed\n");
595                 return -1;
596         }
597
598         t4_os_pci_read_cfg(adapter, PCI_CAPABILITY_LIST, &pos);
599         while (ttl-- && pos >= 0x40) {
600                 pos &= ~3;
601                 t4_os_pci_read_cfg(adapter, (pos + PCI_CAP_LIST_ID), &id);
602
603                 if (id == 0xff)
604                         break;
605
606                 if (id == cap)
607                         return (int)pos;
608
609                 t4_os_pci_read_cfg(adapter, (pos + PCI_CAP_LIST_NEXT), &pos);
610         }
611         return 0;
612 }
613
614 /**
615  * t4_os_set_hw_addr - store a port's MAC address in SW
616  * @adapter: the adapter
617  * @port_idx: the port index
618  * @hw_addr: the Ethernet address
619  *
620  * Store the Ethernet address of the given port in SW.  Called by the
621  * common code when it retrieves a port's Ethernet address from EEPROM.
622  */
623 static inline void t4_os_set_hw_addr(struct adapter *adapter, int port_idx,
624                                      u8 hw_addr[])
625 {
626         struct port_info *pi = adap2pinfo(adapter, port_idx);
627
628         ether_addr_copy((struct ether_addr *)hw_addr,
629                         &pi->eth_dev->data->mac_addrs[0]);
630 }
631
632 /**
633  * t4_os_lock_init - initialize spinlock
634  * @lock: the spinlock
635  */
636 static inline void t4_os_lock_init(rte_spinlock_t *lock)
637 {
638         rte_spinlock_init(lock);
639 }
640
641 /**
642  * t4_os_lock - spin until lock is acquired
643  * @lock: the spinlock
644  */
645 static inline void t4_os_lock(rte_spinlock_t *lock)
646 {
647         rte_spinlock_lock(lock);
648 }
649
650 /**
651  * t4_os_unlock - unlock a spinlock
652  * @lock: the spinlock
653  */
654 static inline void t4_os_unlock(rte_spinlock_t *lock)
655 {
656         rte_spinlock_unlock(lock);
657 }
658
659 /**
660  * t4_os_trylock - try to get a lock
661  * @lock: the spinlock
662  */
663 static inline int t4_os_trylock(rte_spinlock_t *lock)
664 {
665         return rte_spinlock_trylock(lock);
666 }
667
668 /**
669  * t4_os_init_list_head - initialize
670  * @head: head of list to initialize [to empty]
671  */
672 static inline void t4_os_init_list_head(struct mbox_list *head)
673 {
674         TAILQ_INIT(head);
675 }
676
677 static inline struct mbox_entry *t4_os_list_first_entry(struct mbox_list *head)
678 {
679         return TAILQ_FIRST(head);
680 }
681
682 /**
683  * t4_os_atomic_add_tail - Enqueue list element atomically onto list
684  * @new: the entry to be addded to the queue
685  * @head: current head of the linked list
686  * @lock: lock to use to guarantee atomicity
687  */
688 static inline void t4_os_atomic_add_tail(struct mbox_entry *entry,
689                                          struct mbox_list *head,
690                                          rte_spinlock_t *lock)
691 {
692         t4_os_lock(lock);
693         TAILQ_INSERT_TAIL(head, entry, next);
694         t4_os_unlock(lock);
695 }
696
697 /**
698  * t4_os_atomic_list_del - Dequeue list element atomically from list
699  * @entry: the entry to be remove/dequeued from the list.
700  * @lock: the spinlock
701  */
702 static inline void t4_os_atomic_list_del(struct mbox_entry *entry,
703                                          struct mbox_list *head,
704                                          rte_spinlock_t *lock)
705 {
706         t4_os_lock(lock);
707         TAILQ_REMOVE(head, entry, next);
708         t4_os_unlock(lock);
709 }
710
711 void *t4_alloc_mem(size_t size);
712 void t4_free_mem(void *addr);
713 #define t4_os_alloc(_size)     t4_alloc_mem((_size))
714 #define t4_os_free(_ptr)       t4_free_mem((_ptr))
715
716 void t4_os_portmod_changed(const struct adapter *adap, int port_id);
717 void t4_os_link_changed(struct adapter *adap, int port_id, int link_stat);
718
719 void reclaim_completed_tx(struct sge_txq *q);
720 void t4_free_sge_resources(struct adapter *adap);
721 void t4_sge_tx_monitor_start(struct adapter *adap);
722 void t4_sge_tx_monitor_stop(struct adapter *adap);
723 int t4_eth_xmit(struct sge_eth_txq *txq, struct rte_mbuf *mbuf,
724                 uint16_t nb_pkts);
725 int t4_ethrx_handler(struct sge_rspq *q, const __be64 *rsp,
726                      const struct pkt_gl *gl);
727 int t4_sge_init(struct adapter *adap);
728 int t4vf_sge_init(struct adapter *adap);
729 int t4_sge_alloc_eth_txq(struct adapter *adap, struct sge_eth_txq *txq,
730                          struct rte_eth_dev *eth_dev, uint16_t queue_id,
731                          unsigned int iqid, int socket_id);
732 int t4_sge_alloc_rxq(struct adapter *adap, struct sge_rspq *rspq, bool fwevtq,
733                      struct rte_eth_dev *eth_dev, int intr_idx,
734                      struct sge_fl *fl, rspq_handler_t handler,
735                      int cong, struct rte_mempool *mp, int queue_id,
736                      int socket_id);
737 int t4_sge_eth_txq_start(struct sge_eth_txq *txq);
738 int t4_sge_eth_txq_stop(struct sge_eth_txq *txq);
739 void t4_sge_eth_txq_release(struct adapter *adap, struct sge_eth_txq *txq);
740 int t4_sge_eth_rxq_start(struct adapter *adap, struct sge_rspq *rq);
741 int t4_sge_eth_rxq_stop(struct adapter *adap, struct sge_rspq *rq);
742 void t4_sge_eth_rxq_release(struct adapter *adap, struct sge_eth_rxq *rxq);
743 void t4_sge_eth_clear_queues(struct port_info *pi);
744 int cxgb4_set_rspq_intr_params(struct sge_rspq *q, unsigned int us,
745                                unsigned int cnt);
746 int cxgbe_poll(struct sge_rspq *q, struct rte_mbuf **rx_pkts,
747                unsigned int budget, unsigned int *work_done);
748 int cxgbe_write_rss(const struct port_info *pi, const u16 *queues);
749 int cxgbe_write_rss_conf(const struct port_info *pi, uint64_t flags);
750
751 #endif /* __T4_ADAPTER_H__ */