4 * Copyright(c) 2014-2015 Chelsio Communications.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
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.
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.
34 /* This file should not be included directly. Include common.h instead. */
36 #ifndef __T4_ADAPTER_H__
37 #define __T4_ADAPTER_H__
41 #include "cxgbe_compat.h"
42 #include "t4_regs_values.h"
45 MAX_ETH_QSETS = 64, /* # of Ethernet Tx/Rx queue sets */
52 PORT_RSS_DONE = (1 << 0),
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 */
61 unsigned long flags; /* port related flags */
62 short int xact_addr_filt; /* index of exact MAC address filter */
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 */
71 u8 n_rx_qsets; /* # of rx qsets */
72 u8 n_tx_qsets; /* # of tx qsets */
73 u8 first_qset; /* index of first qset */
75 u16 *rss; /* rss table */
76 u8 rss_mode; /* rss mode */
77 u16 rss_size; /* size of VI's RSS table slice */
80 /* Enable or disable autonegotiation. If this is set to enable,
81 * the forced link modes above are completely ignored.
83 #define AUTONEG_DISABLE 0x00
84 #define AUTONEG_ENABLE 0x01
86 enum { /* adapter flags */
87 FULL_INIT_DONE = (1 << 0),
89 USING_MSIX = (1 << 2),
90 FW_QUEUE_BOUND = (1 << 3),
92 CFG_QUEUES = (1 << 5),
96 struct rx_sw_desc { /* SW state per Rx descriptor */
97 void *buf; /* struct page or mbuf */
101 struct sge_fl { /* SGE free-buffer queue state */
103 struct rx_sw_desc *sdesc; /* address of SW Rx descriptor ring */
105 dma_addr_t addr; /* bus address of HW ring start */
106 __be64 *desc; /* address of HW Rx descriptor ring */
108 void __iomem *bar2_addr; /* address of BAR2 Queue registers */
109 unsigned int bar2_qid; /* Queue ID for BAR2 Queue registers */
111 unsigned int cntxt_id; /* SGE relative QID for the free list */
112 unsigned int size; /* capacity of free list */
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 */
119 unsigned long alloc_failed; /* # of times buffer allocation failed */
120 unsigned long low; /* # of times momentarily starving */
123 #define MAX_MBUF_FRAGS (16384 / 512 + 2)
125 /* A packet gather list */
128 struct rte_mbuf *mbufs[MAX_MBUF_FRAGS];
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 */
136 typedef int (*rspq_handler_t)(struct sge_rspq *q, const __be64 *rsp,
137 const struct pkt_gl *gl);
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 */
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 */
148 void __iomem *bar2_addr; /* address of BAR2 Queue registers */
149 unsigned int bar2_qid; /* Queue ID for BAR2 Queue registers */
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 */
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 */
164 rspq_handler_t handler; /* associated handler for this response q */
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 */
175 struct sge_eth_rxq { /* a SW Ethernet Rx queue */
176 struct sge_rspq rspq;
178 struct sge_eth_rx_stats stats;
179 bool usembufs; /* one ingress packet per mbuf FL buffer */
180 } __rte_cache_aligned;
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.
193 #define ETH_COALESCE_PKT_NUM 15
194 #define ETH_COALESCE_PKT_PER_DESC 2
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];
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;
213 EQ_STOPPED = (1 << 0),
216 struct eth_coalesce {
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 */
231 uint64_t phys_addr; /* physical address of the ring */
233 void __iomem *bar2_addr; /* address of BAR2 Queue registers */
234 unsigned int bar2_qid; /* Queue ID for BAR2 Queue registers */
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 */
246 int db_disabled; /* doorbell state */
247 unsigned short db_pidx; /* doorbell producer index */
248 unsigned short db_pidx_inc; /* doorbell producer increment */
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 */
262 struct sge_eth_txq { /* state for an SGE Ethernet Tx queue */
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;
268 unsigned int flags; /* flags for state of the queue */
269 } __rte_cache_aligned;
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;
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 */
280 /* response queue interrupt parameters */
281 u16 timer_val[SGE_NTIMERS];
282 u8 counter_val[SGE_NCOUNTERS];
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 */
289 #define T4_OS_NEEDS_MBOX_LOCKING 1
292 * OS Lock/List primitives for those interfaces in the Common Code which
297 TAILQ_ENTRY(mbox_entry) next;
300 TAILQ_HEAD(mbox_list, mbox_entry);
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 */
309 /* support for single-threading access to adapter mailbox registers */
310 struct mbox_list mbox_list;
311 rte_spinlock_t mbox_lock;
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 */
319 int use_unpacked_mode; /* unpacked rx mode state */
322 #define CXGBE_PCI_REG(reg) (*((volatile uint32_t *)(reg)))
324 static inline uint64_t cxgbe_read_addr64(volatile void *addr)
326 uint64_t val = CXGBE_PCI_REG(addr);
327 uint64_t val2 = CXGBE_PCI_REG(((volatile uint8_t *)(addr) + 4));
329 val2 = (uint64_t)(val2 << 32);
334 static inline uint32_t cxgbe_read_addr(volatile void *addr)
336 return CXGBE_PCI_REG(addr);
339 #define CXGBE_PCI_REG_ADDR(adap, reg) \
340 ((volatile uint32_t *)((char *)(adap)->regs + (reg)))
342 #define CXGBE_READ_REG(adap, reg) \
343 cxgbe_read_addr(CXGBE_PCI_REG_ADDR((adap), (reg)))
345 #define CXGBE_READ_REG64(adap, reg) \
346 cxgbe_read_addr64(CXGBE_PCI_REG_ADDR((adap), (reg)))
348 #define CXGBE_PCI_REG_WRITE(reg, value) ({ \
349 CXGBE_PCI_REG((reg)) = (value); })
351 #define CXGBE_WRITE_REG(adap, reg, value) \
352 CXGBE_PCI_REG_WRITE(CXGBE_PCI_REG_ADDR((adap), (reg)), (value))
354 static inline uint64_t cxgbe_write_addr64(volatile void *addr, uint64_t val)
356 CXGBE_PCI_REG(addr) = val;
357 CXGBE_PCI_REG(((volatile uint8_t *)(addr) + 4)) = (val >> 32);
361 #define CXGBE_WRITE_REG64(adap, reg, value) \
362 cxgbe_write_addr64(CXGBE_PCI_REG_ADDR((adap), (reg)), (value))
365 * t4_read_reg - read a HW register
366 * @adapter: the adapter
367 * @reg_addr: the register address
369 * Returns the 32-bit value of the given HW register.
371 static inline u32 t4_read_reg(struct adapter *adapter, u32 reg_addr)
373 u32 val = CXGBE_READ_REG(adapter, reg_addr);
375 CXGBE_DEBUG_REG(adapter, "read register 0x%x value 0x%x\n", reg_addr,
381 * t4_write_reg - write a HW register
382 * @adapter: the adapter
383 * @reg_addr: the register address
384 * @val: the value to write
386 * Write a 32-bit value into the given HW register.
388 static inline void t4_write_reg(struct adapter *adapter, u32 reg_addr, u32 val)
390 CXGBE_DEBUG_REG(adapter, "setting register 0x%x to 0x%x\n", reg_addr,
392 CXGBE_WRITE_REG(adapter, reg_addr, val);
396 * t4_read_reg64 - read a 64-bit HW register
397 * @adapter: the adapter
398 * @reg_addr: the register address
400 * Returns the 64-bit value of the given HW register.
402 static inline u64 t4_read_reg64(struct adapter *adapter, u32 reg_addr)
404 u64 val = CXGBE_READ_REG64(adapter, reg_addr);
406 CXGBE_DEBUG_REG(adapter, "64-bit read register %#x value %#llx\n",
407 reg_addr, (unsigned long long)val);
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
417 * Write a 64-bit value into the given HW register.
419 static inline void t4_write_reg64(struct adapter *adapter, u32 reg_addr,
422 CXGBE_DEBUG_REG(adapter, "setting register %#x to %#llx\n", reg_addr,
423 (unsigned long long)val);
425 CXGBE_WRITE_REG64(adapter, reg_addr, val);
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
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.
437 static inline void t4_os_set_hw_addr(struct adapter *adapter, int port_idx,
440 struct port_info *pi = &adapter->port[port_idx];
442 ether_addr_copy((struct ether_addr *)hw_addr,
443 &pi->eth_dev->data->mac_addrs[0]);
447 * t4_os_lock_init - initialize spinlock
448 * @lock: the spinlock
450 static inline void t4_os_lock_init(rte_spinlock_t *lock)
452 rte_spinlock_init(lock);
456 * t4_os_lock - spin until lock is acquired
457 * @lock: the spinlock
459 static inline void t4_os_lock(rte_spinlock_t *lock)
461 rte_spinlock_lock(lock);
465 * t4_os_unlock - unlock a spinlock
466 * @lock: the spinlock
468 static inline void t4_os_unlock(rte_spinlock_t *lock)
470 rte_spinlock_unlock(lock);
474 * t4_os_init_list_head - initialize
475 * @head: head of list to initialize [to empty]
477 static inline void t4_os_init_list_head(struct mbox_list *head)
482 static inline struct mbox_entry *t4_os_list_first_entry(struct mbox_list *head)
484 return TAILQ_FIRST(head);
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
493 static inline void t4_os_atomic_add_tail(struct mbox_entry *entry,
494 struct mbox_list *head,
495 rte_spinlock_t *lock)
498 TAILQ_INSERT_TAIL(head, entry, next);
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
507 static inline void t4_os_atomic_list_del(struct mbox_entry *entry,
508 struct mbox_list *head,
509 rte_spinlock_t *lock)
512 TAILQ_REMOVE(head, entry, next);
517 * adap2pinfo - return the port_info of a port
519 * @idx: the port index
521 * Return the port_info structure for the port of the given index.
523 static inline struct port_info *adap2pinfo(struct adapter *adap, int idx)
525 return &adap->port[idx];
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))
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);
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,
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,
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);
565 #endif /* __T4_ADAPTER_H__ */