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