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