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 #include <sys/queue.h>
42 #include <netinet/in.h>
44 #include <rte_byteorder.h>
45 #include <rte_common.h>
46 #include <rte_cycles.h>
47 #include <rte_interrupts.h>
49 #include <rte_debug.h>
51 #include <rte_atomic.h>
52 #include <rte_branch_prediction.h>
53 #include <rte_memory.h>
54 #include <rte_memzone.h>
55 #include <rte_tailq.h>
57 #include <rte_alarm.h>
58 #include <rte_ether.h>
59 #include <rte_ethdev.h>
60 #include <rte_atomic.h>
61 #include <rte_malloc.h>
62 #include <rte_random.h>
70 static inline void ship_tx_pkt_coalesce_wr(struct adapter *adap,
71 struct sge_eth_txq *txq);
74 * Max number of Rx buffers we replenish at a time.
76 #define MAX_RX_REFILL 64U
78 #define NOMEM_TMR_IDX (SGE_NTIMERS - 1)
81 * Max Tx descriptor space we allow for an Ethernet packet to be inlined
84 #define MAX_IMM_TX_PKT_LEN 256
87 * Rx buffer sizes for "usembufs" Free List buffers (one ingress packet
88 * per mbuf buffer). We currently only support two sizes for 1500- and
89 * 9000-byte MTUs. We could easily support more but there doesn't seem to be
90 * much need for that ...
92 #define FL_MTU_SMALL 1500
93 #define FL_MTU_LARGE 9000
95 static inline unsigned int fl_mtu_bufsize(struct adapter *adapter,
98 struct sge *s = &adapter->sge;
100 return CXGBE_ALIGN(s->pktshift + ETHER_HDR_LEN + VLAN_HLEN + mtu,
104 #define FL_MTU_SMALL_BUFSIZE(adapter) fl_mtu_bufsize(adapter, FL_MTU_SMALL)
105 #define FL_MTU_LARGE_BUFSIZE(adapter) fl_mtu_bufsize(adapter, FL_MTU_LARGE)
108 * Bits 0..3 of rx_sw_desc.dma_addr have special meaning. The hardware uses
109 * these to specify the buffer size as an index into the SGE Free List Buffer
110 * Size register array. We also use bit 4, when the buffer has been unmapped
111 * for DMA, but this is of course never sent to the hardware and is only used
112 * to prevent double unmappings. All of the above requires that the Free List
113 * Buffers which we allocate have the bottom 5 bits free (0) -- i.e. are
114 * 32-byte or or a power of 2 greater in alignment. Since the SGE's minimal
115 * Free List Buffer alignment is 32 bytes, this works out for us ...
118 RX_BUF_FLAGS = 0x1f, /* bottom five bits are special */
119 RX_BUF_SIZE = 0x0f, /* bottom three bits are for buf sizes */
120 RX_UNMAPPED_BUF = 0x10, /* buffer is not mapped */
123 * XXX We shouldn't depend on being able to use these indices.
124 * XXX Especially when some other Master PF has initialized the
125 * XXX adapter or we use the Firmware Configuration File. We
126 * XXX should really search through the Host Buffer Size register
127 * XXX array for the appropriately sized buffer indices.
129 RX_SMALL_PG_BUF = 0x0, /* small (PAGE_SIZE) page buffer */
130 RX_LARGE_PG_BUF = 0x1, /* buffer large page buffer */
132 RX_SMALL_MTU_BUF = 0x2, /* small MTU buffer */
133 RX_LARGE_MTU_BUF = 0x3, /* large MTU buffer */
137 * txq_avail - return the number of available slots in a Tx queue
140 * Returns the number of descriptors in a Tx queue available to write new
143 static inline unsigned int txq_avail(const struct sge_txq *q)
145 return q->size - 1 - q->in_use;
148 static int map_mbuf(struct rte_mbuf *mbuf, dma_addr_t *addr)
150 struct rte_mbuf *m = mbuf;
152 for (; m; m = m->next, addr++) {
153 *addr = m->buf_physaddr + rte_pktmbuf_headroom(m);
164 * free_tx_desc - reclaims Tx descriptors and their buffers
165 * @q: the Tx queue to reclaim descriptors from
166 * @n: the number of descriptors to reclaim
168 * Reclaims Tx descriptors from an SGE Tx queue and frees the associated
169 * Tx buffers. Called with the Tx queue lock held.
171 static void free_tx_desc(struct sge_txq *q, unsigned int n)
173 struct tx_sw_desc *d;
174 unsigned int cidx = 0;
178 if (d->mbuf) { /* an SGL is present */
179 rte_pktmbuf_free(d->mbuf);
182 if (d->coalesce.idx) {
185 for (i = 0; i < d->coalesce.idx; i++) {
186 rte_pktmbuf_free(d->coalesce.mbuf[i]);
187 d->coalesce.mbuf[i] = NULL;
192 if (++cidx == q->size) {
196 RTE_MBUF_PREFETCH_TO_FREE(&q->sdesc->mbuf->pool);
200 static void reclaim_tx_desc(struct sge_txq *q, unsigned int n)
202 struct tx_sw_desc *d;
203 unsigned int cidx = q->cidx;
207 if (d->mbuf) { /* an SGL is present */
208 rte_pktmbuf_free(d->mbuf);
212 if (++cidx == q->size) {
221 * fl_cap - return the capacity of a free-buffer list
224 * Returns the capacity of a free-buffer list. The capacity is less than
225 * the size because one descriptor needs to be left unpopulated, otherwise
226 * HW will think the FL is empty.
228 static inline unsigned int fl_cap(const struct sge_fl *fl)
230 return fl->size - 8; /* 1 descriptor = 8 buffers */
234 * fl_starving - return whether a Free List is starving.
235 * @adapter: pointer to the adapter
238 * Tests specified Free List to see whether the number of buffers
239 * available to the hardware has falled below our "starvation"
242 static inline bool fl_starving(const struct adapter *adapter,
243 const struct sge_fl *fl)
245 const struct sge *s = &adapter->sge;
247 return fl->avail - fl->pend_cred <= s->fl_starve_thres;
250 static inline unsigned int get_buf_size(struct adapter *adapter,
251 const struct rx_sw_desc *d)
253 unsigned int rx_buf_size_idx = d->dma_addr & RX_BUF_SIZE;
254 unsigned int buf_size = 0;
256 switch (rx_buf_size_idx) {
257 case RX_SMALL_MTU_BUF:
258 buf_size = FL_MTU_SMALL_BUFSIZE(adapter);
261 case RX_LARGE_MTU_BUF:
262 buf_size = FL_MTU_LARGE_BUFSIZE(adapter);
274 * free_rx_bufs - free the Rx buffers on an SGE free list
275 * @q: the SGE free list to free buffers from
276 * @n: how many buffers to free
278 * Release the next @n buffers on an SGE free-buffer Rx queue. The
279 * buffers must be made inaccessible to HW before calling this function.
281 static void free_rx_bufs(struct sge_fl *q, int n)
283 unsigned int cidx = q->cidx;
284 struct rx_sw_desc *d;
289 rte_pktmbuf_free(d->buf);
293 if (++cidx == q->size) {
303 * unmap_rx_buf - unmap the current Rx buffer on an SGE free list
304 * @q: the SGE free list
306 * Unmap the current buffer on an SGE free-buffer Rx queue. The
307 * buffer must be made inaccessible to HW before calling this function.
309 * This is similar to @free_rx_bufs above but does not free the buffer.
310 * Do note that the FL still loses any further access to the buffer.
312 static void unmap_rx_buf(struct sge_fl *q)
314 if (++q->cidx == q->size)
319 static inline void ring_fl_db(struct adapter *adap, struct sge_fl *q)
321 if (q->pend_cred >= 64) {
322 u32 val = adap->params.arch.sge_fl_db;
324 if (is_t4(adap->params.chip))
325 val |= V_PIDX(q->pend_cred / 8);
327 val |= V_PIDX_T5(q->pend_cred / 8);
330 * Make sure all memory writes to the Free List queue are
331 * committed before we tell the hardware about them.
336 * If we don't have access to the new User Doorbell (T5+), use
337 * the old doorbell mechanism; otherwise use the new BAR2
340 if (unlikely(!q->bar2_addr)) {
341 t4_write_reg_relaxed(adap, MYPF_REG(A_SGE_PF_KDOORBELL),
342 val | V_QID(q->cntxt_id));
344 writel_relaxed(val | V_QID(q->bar2_qid),
345 (void *)((uintptr_t)q->bar2_addr +
349 * This Write memory Barrier will force the write to
350 * the User Doorbell area to be flushed.
358 static inline void set_rx_sw_desc(struct rx_sw_desc *sd, void *buf,
362 sd->dma_addr = mapping; /* includes size low bits */
366 * refill_fl_usembufs - refill an SGE Rx buffer ring with mbufs
368 * @q: the ring to refill
369 * @n: the number of new buffers to allocate
371 * (Re)populate an SGE free-buffer queue with up to @n new packet buffers,
372 * allocated with the supplied gfp flags. The caller must assure that
373 * @n does not exceed the queue's capacity. If afterwards the queue is
374 * found critically low mark it as starving in the bitmap of starving FLs.
376 * Returns the number of buffers allocated.
378 static unsigned int refill_fl_usembufs(struct adapter *adap, struct sge_fl *q,
381 struct sge_eth_rxq *rxq = container_of(q, struct sge_eth_rxq, fl);
382 unsigned int cred = q->avail;
383 __be64 *d = &q->desc[q->pidx];
384 struct rx_sw_desc *sd = &q->sdesc[q->pidx];
385 unsigned int buf_size_idx = RX_SMALL_MTU_BUF;
386 struct rte_mbuf *buf_bulk[n];
388 struct rte_pktmbuf_pool_private *mbp_priv;
389 u8 jumbo_en = rxq->rspq.eth_dev->data->dev_conf.rxmode.jumbo_frame;
391 /* Use jumbo mtu buffers iff mbuf data room size can fit jumbo data. */
392 mbp_priv = rte_mempool_get_priv(rxq->rspq.mb_pool);
394 ((mbp_priv->mbuf_data_room_size - RTE_PKTMBUF_HEADROOM) >= 9000))
395 buf_size_idx = RX_LARGE_MTU_BUF;
397 ret = rte_mempool_get_bulk(rxq->rspq.mb_pool, (void *)buf_bulk, n);
398 if (unlikely(ret != 0)) {
399 dev_debug(adap, "%s: failed to allocated fl entries in bulk ..\n",
402 rxq->rspq.eth_dev->data->rx_mbuf_alloc_failed++;
406 for (i = 0; i < n; i++) {
407 struct rte_mbuf *mbuf = buf_bulk[i];
411 dev_debug(adap, "%s: mbuf alloc failed\n", __func__);
413 rxq->rspq.eth_dev->data->rx_mbuf_alloc_failed++;
417 rte_mbuf_refcnt_set(mbuf, 1);
418 mbuf->data_off = RTE_PKTMBUF_HEADROOM;
421 mbuf->port = rxq->rspq.port_id;
423 mapping = (dma_addr_t)RTE_ALIGN(mbuf->buf_physaddr +
426 mapping |= buf_size_idx;
427 *d++ = cpu_to_be64(mapping);
428 set_rx_sw_desc(sd, mbuf, mapping);
432 if (++q->pidx == q->size) {
439 out: cred = q->avail - cred;
440 q->pend_cred += cred;
443 if (unlikely(fl_starving(adap, q))) {
445 * Make sure data has been written to free list
455 * refill_fl - refill an SGE Rx buffer ring with mbufs
457 * @q: the ring to refill
458 * @n: the number of new buffers to allocate
460 * (Re)populate an SGE free-buffer queue with up to @n new packet buffers,
461 * allocated with the supplied gfp flags. The caller must assure that
462 * @n does not exceed the queue's capacity. Returns the number of buffers
465 static unsigned int refill_fl(struct adapter *adap, struct sge_fl *q, int n)
467 return refill_fl_usembufs(adap, q, n);
470 static inline void __refill_fl(struct adapter *adap, struct sge_fl *fl)
472 refill_fl(adap, fl, min(MAX_RX_REFILL, fl_cap(fl) - fl->avail));
476 * Return the number of reclaimable descriptors in a Tx queue.
478 static inline int reclaimable(const struct sge_txq *q)
480 int hw_cidx = ntohs(q->stat->cidx);
484 return hw_cidx + q->size;
489 * reclaim_completed_tx - reclaims completed Tx descriptors
490 * @q: the Tx queue to reclaim completed descriptors from
492 * Reclaims Tx descriptors that the SGE has indicated it has processed.
494 void reclaim_completed_tx(struct sge_txq *q)
496 unsigned int avail = reclaimable(q);
499 /* reclaim as much as possible */
500 reclaim_tx_desc(q, avail);
502 avail = reclaimable(q);
507 * sgl_len - calculates the size of an SGL of the given capacity
508 * @n: the number of SGL entries
510 * Calculates the number of flits needed for a scatter/gather list that
511 * can hold the given number of entries.
513 static inline unsigned int sgl_len(unsigned int n)
516 * A Direct Scatter Gather List uses 32-bit lengths and 64-bit PCI DMA
517 * addresses. The DSGL Work Request starts off with a 32-bit DSGL
518 * ULPTX header, then Length0, then Address0, then, for 1 <= i <= N,
519 * repeated sequences of { Length[i], Length[i+1], Address[i],
520 * Address[i+1] } (this ensures that all addresses are on 64-bit
521 * boundaries). If N is even, then Length[N+1] should be set to 0 and
522 * Address[N+1] is omitted.
524 * The following calculation incorporates all of the above. It's
525 * somewhat hard to follow but, briefly: the "+2" accounts for the
526 * first two flits which include the DSGL header, Length0 and
527 * Address0; the "(3*(n-1))/2" covers the main body of list entries (3
528 * flits for every pair of the remaining N) +1 if (n-1) is odd; and
529 * finally the "+((n-1)&1)" adds the one remaining flit needed if
533 return (3 * n) / 2 + (n & 1) + 2;
537 * flits_to_desc - returns the num of Tx descriptors for the given flits
538 * @n: the number of flits
540 * Returns the number of Tx descriptors needed for the supplied number
543 static inline unsigned int flits_to_desc(unsigned int n)
545 return DIV_ROUND_UP(n, 8);
549 * is_eth_imm - can an Ethernet packet be sent as immediate data?
552 * Returns whether an Ethernet packet is small enough to fit as
553 * immediate data. Return value corresponds to the headroom required.
555 static inline int is_eth_imm(const struct rte_mbuf *m)
557 unsigned int hdrlen = (m->ol_flags & PKT_TX_TCP_SEG) ?
558 sizeof(struct cpl_tx_pkt_lso_core) : 0;
560 hdrlen += sizeof(struct cpl_tx_pkt);
561 if (m->pkt_len <= MAX_IMM_TX_PKT_LEN - hdrlen)
568 * calc_tx_flits - calculate the number of flits for a packet Tx WR
571 * Returns the number of flits needed for a Tx WR for the given Ethernet
572 * packet, including the needed WR and CPL headers.
574 static inline unsigned int calc_tx_flits(const struct rte_mbuf *m)
580 * If the mbuf is small enough, we can pump it out as a work request
581 * with only immediate data. In that case we just have to have the
582 * TX Packet header plus the mbuf data in the Work Request.
585 hdrlen = is_eth_imm(m);
587 return DIV_ROUND_UP(m->pkt_len + hdrlen, sizeof(__be64));
590 * Otherwise, we're going to have to construct a Scatter gather list
591 * of the mbuf body and fragments. We also include the flits necessary
592 * for the TX Packet Work Request and CPL. We always have a firmware
593 * Write Header (incorporated as part of the cpl_tx_pkt_lso and
594 * cpl_tx_pkt structures), followed by either a TX Packet Write CPL
595 * message or, if we're doing a Large Send Offload, an LSO CPL message
596 * with an embeded TX Packet Write CPL message.
598 flits = sgl_len(m->nb_segs);
600 flits += (sizeof(struct fw_eth_tx_pkt_wr) +
601 sizeof(struct cpl_tx_pkt_lso_core) +
602 sizeof(struct cpl_tx_pkt_core)) / sizeof(__be64);
604 flits += (sizeof(struct fw_eth_tx_pkt_wr) +
605 sizeof(struct cpl_tx_pkt_core)) / sizeof(__be64);
610 * write_sgl - populate a scatter/gather list for a packet
612 * @q: the Tx queue we are writing into
613 * @sgl: starting location for writing the SGL
614 * @end: points right after the end of the SGL
615 * @start: start offset into mbuf main-body data to include in the SGL
616 * @addr: address of mapped region
618 * Generates a scatter/gather list for the buffers that make up a packet.
619 * The caller must provide adequate space for the SGL that will be written.
620 * The SGL includes all of the packet's page fragments and the data in its
621 * main body except for the first @start bytes. @sgl must be 16-byte
622 * aligned and within a Tx descriptor with available space. @end points
623 * write after the end of the SGL but does not account for any potential
624 * wrap around, i.e., @end > @sgl.
626 static void write_sgl(struct rte_mbuf *mbuf, struct sge_txq *q,
627 struct ulptx_sgl *sgl, u64 *end, unsigned int start,
628 const dma_addr_t *addr)
631 struct ulptx_sge_pair *to;
632 struct rte_mbuf *m = mbuf;
633 unsigned int nfrags = m->nb_segs;
634 struct ulptx_sge_pair buf[nfrags / 2];
636 len = m->data_len - start;
637 sgl->len0 = htonl(len);
638 sgl->addr0 = rte_cpu_to_be_64(addr[0]);
640 sgl->cmd_nsge = htonl(V_ULPTX_CMD(ULP_TX_SC_DSGL) |
641 V_ULPTX_NSGE(nfrags));
642 if (likely(--nfrags == 0))
645 * Most of the complexity below deals with the possibility we hit the
646 * end of the queue in the middle of writing the SGL. For this case
647 * only we create the SGL in a temporary buffer and then copy it.
649 to = (u8 *)end > (u8 *)q->stat ? buf : sgl->sge;
651 for (i = 0; nfrags >= 2; nfrags -= 2, to++) {
653 to->len[0] = rte_cpu_to_be_32(m->data_len);
654 to->addr[0] = rte_cpu_to_be_64(addr[++i]);
656 to->len[1] = rte_cpu_to_be_32(m->data_len);
657 to->addr[1] = rte_cpu_to_be_64(addr[++i]);
661 to->len[0] = rte_cpu_to_be_32(m->data_len);
662 to->len[1] = rte_cpu_to_be_32(0);
663 to->addr[0] = rte_cpu_to_be_64(addr[i + 1]);
665 if (unlikely((u8 *)end > (u8 *)q->stat)) {
666 unsigned int part0 = RTE_PTR_DIFF((u8 *)q->stat,
671 memcpy(sgl->sge, buf, part0);
672 part1 = RTE_PTR_DIFF((u8 *)end, (u8 *)q->stat);
673 rte_memcpy(q->desc, RTE_PTR_ADD((u8 *)buf, part0), part1);
674 end = RTE_PTR_ADD((void *)q->desc, part1);
676 if ((uintptr_t)end & 8) /* 0-pad to multiple of 16 */
680 #define IDXDIFF(head, tail, wrap) \
681 ((head) >= (tail) ? (head) - (tail) : (wrap) - (tail) + (head))
683 #define Q_IDXDIFF(q, idx) IDXDIFF((q)->pidx, (q)->idx, (q)->size)
684 #define R_IDXDIFF(q, idx) IDXDIFF((q)->cidx, (q)->idx, (q)->size)
687 * ring_tx_db - ring a Tx queue's doorbell
690 * @n: number of new descriptors to give to HW
692 * Ring the doorbel for a Tx queue.
694 static inline void ring_tx_db(struct adapter *adap, struct sge_txq *q)
696 int n = Q_IDXDIFF(q, dbidx);
699 * Make sure that all writes to the TX Descriptors are committed
700 * before we tell the hardware about them.
705 * If we don't have access to the new User Doorbell (T5+), use the old
706 * doorbell mechanism; otherwise use the new BAR2 mechanism.
708 if (unlikely(!q->bar2_addr)) {
712 * For T4 we need to participate in the Doorbell Recovery
716 t4_write_reg(adap, MYPF_REG(A_SGE_PF_KDOORBELL),
717 V_QID(q->cntxt_id) | val);
720 q->db_pidx = q->pidx;
722 u32 val = V_PIDX_T5(n);
725 * T4 and later chips share the same PIDX field offset within
726 * the doorbell, but T5 and later shrank the field in order to
727 * gain a bit for Doorbell Priority. The field was absurdly
728 * large in the first place (14 bits) so we just use the T5
729 * and later limits and warn if a Queue ID is too large.
731 WARN_ON(val & F_DBPRIO);
733 writel(val | V_QID(q->bar2_qid),
734 (void *)((uintptr_t)q->bar2_addr + SGE_UDB_KDOORBELL));
737 * This Write Memory Barrier will force the write to the User
738 * Doorbell area to be flushed. This is needed to prevent
739 * writes on different CPUs for the same queue from hitting
740 * the adapter out of order. This is required when some Work
741 * Requests take the Write Combine Gather Buffer path (user
742 * doorbell area offset [SGE_UDB_WCDOORBELL..+63]) and some
743 * take the traditional path where we simply increment the
744 * PIDX (User Doorbell area SGE_UDB_KDOORBELL) and have the
745 * hardware DMA read the actual Work Request.
753 * Figure out what HW csum a packet wants and return the appropriate control
756 static u64 hwcsum(enum chip_type chip, const struct rte_mbuf *m)
760 if (m->ol_flags & PKT_TX_IP_CKSUM) {
761 switch (m->ol_flags & PKT_TX_L4_MASK) {
762 case PKT_TX_TCP_CKSUM:
763 csum_type = TX_CSUM_TCPIP;
765 case PKT_TX_UDP_CKSUM:
766 csum_type = TX_CSUM_UDPIP;
775 if (likely(csum_type >= TX_CSUM_TCPIP)) {
776 u64 hdr_len = V_TXPKT_IPHDR_LEN(m->l3_len);
777 int eth_hdr_len = m->l2_len;
779 if (CHELSIO_CHIP_VERSION(chip) <= CHELSIO_T5)
780 hdr_len |= V_TXPKT_ETHHDR_LEN(eth_hdr_len);
782 hdr_len |= V_T6_TXPKT_ETHHDR_LEN(eth_hdr_len);
783 return V_TXPKT_CSUM_TYPE(csum_type) | hdr_len;
787 * unknown protocol, disable HW csum
788 * and hope a bad packet is detected
790 return F_TXPKT_L4CSUM_DIS;
793 static inline void txq_advance(struct sge_txq *q, unsigned int n)
797 if (q->pidx >= q->size)
801 #define MAX_COALESCE_LEN 64000
803 static inline int wraps_around(struct sge_txq *q, int ndesc)
805 return (q->pidx + ndesc) > q->size ? 1 : 0;
808 static void tx_timer_cb(void *data)
810 struct adapter *adap = (struct adapter *)data;
811 struct sge_eth_txq *txq = &adap->sge.ethtxq[0];
813 unsigned int coal_idx;
815 /* monitor any pending tx */
816 for (i = 0; i < adap->sge.max_ethqsets; i++, txq++) {
817 if (t4_os_trylock(&txq->txq_lock)) {
818 coal_idx = txq->q.coalesce.idx;
820 if (coal_idx == txq->q.last_coal_idx &&
821 txq->q.pidx == txq->q.last_pidx) {
822 ship_tx_pkt_coalesce_wr(adap, txq);
824 txq->q.last_coal_idx = coal_idx;
825 txq->q.last_pidx = txq->q.pidx;
828 t4_os_unlock(&txq->txq_lock);
831 rte_eal_alarm_set(50, tx_timer_cb, (void *)adap);
835 * ship_tx_pkt_coalesce_wr - finalizes and ships a coalesce WR
836 * @ adap: adapter structure
839 * writes the different fields of the pkts WR and sends it.
841 static inline void ship_tx_pkt_coalesce_wr(struct adapter *adap,
842 struct sge_eth_txq *txq)
845 struct sge_txq *q = &txq->q;
846 struct fw_eth_tx_pkts_wr *wr;
849 /* fill the pkts WR header */
850 wr = (void *)&q->desc[q->pidx];
851 wr->op_pkd = htonl(V_FW_WR_OP(FW_ETH_TX_PKTS_WR));
853 wr_mid = V_FW_WR_LEN16(DIV_ROUND_UP(q->coalesce.flits, 2));
854 ndesc = flits_to_desc(q->coalesce.flits);
855 wr->equiq_to_len16 = htonl(wr_mid);
856 wr->plen = cpu_to_be16(q->coalesce.len);
857 wr->npkt = q->coalesce.idx;
859 wr->type = q->coalesce.type;
861 /* zero out coalesce structure members */
863 q->coalesce.flits = 0;
866 txq_advance(q, ndesc);
867 txq->stats.coal_wr++;
868 txq->stats.coal_pkts += wr->npkt;
870 if (Q_IDXDIFF(q, equeidx) >= q->size / 2) {
871 q->equeidx = q->pidx;
872 wr_mid |= F_FW_WR_EQUEQ;
873 wr->equiq_to_len16 = htonl(wr_mid);
879 * should_tx_packet_coalesce - decides wether to coalesce an mbuf or not
880 * @txq: tx queue where the mbuf is sent
881 * @mbuf: mbuf to be sent
882 * @nflits: return value for number of flits needed
883 * @adap: adapter structure
885 * This function decides if a packet should be coalesced or not.
887 static inline int should_tx_packet_coalesce(struct sge_eth_txq *txq,
888 struct rte_mbuf *mbuf,
889 unsigned int *nflits,
890 struct adapter *adap)
892 struct sge_txq *q = &txq->q;
893 unsigned int flits, ndesc;
894 unsigned char type = 0;
897 /* use coal WR type 1 when no frags are present */
898 type = (mbuf->nb_segs == 1) ? 1 : 0;
900 if (unlikely(type != q->coalesce.type && q->coalesce.idx))
901 ship_tx_pkt_coalesce_wr(adap, txq);
903 /* calculate the number of flits required for coalescing this packet
904 * without the 2 flits of the WR header. These are added further down
905 * if we are just starting in new PKTS WR. sgl_len doesn't account for
906 * the possible 16 bytes alignment ULP TX commands so we do it here.
908 flits = (sgl_len(mbuf->nb_segs) + 1) & ~1U;
910 flits += (sizeof(struct ulp_txpkt) +
911 sizeof(struct ulptx_idata)) / sizeof(__be64);
912 flits += sizeof(struct cpl_tx_pkt_core) / sizeof(__be64);
915 /* If coalescing is on, the mbuf is added to a pkts WR */
916 if (q->coalesce.idx) {
917 ndesc = DIV_ROUND_UP(q->coalesce.flits + flits, 8);
918 credits = txq_avail(q) - ndesc;
920 /* If we are wrapping or this is last mbuf then, send the
921 * already coalesced mbufs and let the non-coalesce pass
924 if (unlikely(credits < 0 || wraps_around(q, ndesc))) {
925 ship_tx_pkt_coalesce_wr(adap, txq);
929 /* If the max coalesce len or the max WR len is reached
930 * ship the WR and keep coalescing on.
932 if (unlikely((q->coalesce.len + mbuf->pkt_len >
934 (q->coalesce.flits + flits >
936 ship_tx_pkt_coalesce_wr(adap, txq);
943 /* start a new pkts WR, the WR header is not filled below */
944 flits += sizeof(struct fw_eth_tx_pkts_wr) / sizeof(__be64);
945 ndesc = flits_to_desc(q->coalesce.flits + flits);
946 credits = txq_avail(q) - ndesc;
948 if (unlikely(credits < 0 || wraps_around(q, ndesc)))
950 q->coalesce.flits += 2;
951 q->coalesce.type = type;
952 q->coalesce.ptr = (unsigned char *)&q->desc[q->pidx] +
958 * tx_do_packet_coalesce - add an mbuf to a coalesce WR
959 * @txq: sge_eth_txq used send the mbuf
960 * @mbuf: mbuf to be sent
961 * @flits: flits needed for this mbuf
962 * @adap: adapter structure
963 * @pi: port_info structure
964 * @addr: mapped address of the mbuf
966 * Adds an mbuf to be sent as part of a coalesce WR by filling a
967 * ulp_tx_pkt command, ulp_tx_sc_imm command, cpl message and
968 * ulp_tx_sc_dsgl command.
970 static inline int tx_do_packet_coalesce(struct sge_eth_txq *txq,
971 struct rte_mbuf *mbuf,
972 int flits, struct adapter *adap,
973 const struct port_info *pi,
977 struct sge_txq *q = &txq->q;
978 struct ulp_txpkt *mc;
979 struct ulptx_idata *sc_imm;
980 struct cpl_tx_pkt_core *cpl;
981 struct tx_sw_desc *sd;
982 unsigned int idx = q->coalesce.idx, len = mbuf->pkt_len;
984 if (q->coalesce.type == 0) {
985 mc = (struct ulp_txpkt *)q->coalesce.ptr;
986 mc->cmd_dest = htonl(V_ULPTX_CMD(4) | V_ULP_TXPKT_DEST(0) |
987 V_ULP_TXPKT_FID(adap->sge.fw_evtq.cntxt_id) |
989 mc->len = htonl(DIV_ROUND_UP(flits, 2));
990 sc_imm = (struct ulptx_idata *)(mc + 1);
991 sc_imm->cmd_more = htonl(V_ULPTX_CMD(ULP_TX_SC_IMM) |
993 sc_imm->len = htonl(sizeof(*cpl));
994 end = (u64 *)mc + flits;
995 cpl = (struct cpl_tx_pkt_core *)(sc_imm + 1);
997 end = (u64 *)q->coalesce.ptr + flits;
998 cpl = (struct cpl_tx_pkt_core *)q->coalesce.ptr;
1001 /* update coalesce structure for this txq */
1002 q->coalesce.flits += flits;
1003 q->coalesce.ptr += flits * sizeof(__be64);
1004 q->coalesce.len += mbuf->pkt_len;
1006 /* fill the cpl message, same as in t4_eth_xmit, this should be kept
1007 * similar to t4_eth_xmit
1009 if (mbuf->ol_flags & PKT_TX_IP_CKSUM) {
1010 cntrl = hwcsum(adap->params.chip, mbuf) |
1012 txq->stats.tx_cso++;
1014 cntrl = F_TXPKT_L4CSUM_DIS | F_TXPKT_IPCSUM_DIS;
1017 if (mbuf->ol_flags & PKT_TX_VLAN_PKT) {
1018 txq->stats.vlan_ins++;
1019 cntrl |= F_TXPKT_VLAN_VLD | V_TXPKT_VLAN(mbuf->vlan_tci);
1022 cpl->ctrl0 = htonl(V_TXPKT_OPCODE(CPL_TX_PKT_XT) |
1023 V_TXPKT_INTF(pi->tx_chan) |
1024 V_TXPKT_PF(adap->pf));
1025 cpl->pack = htons(0);
1026 cpl->len = htons(len);
1027 cpl->ctrl1 = cpu_to_be64(cntrl);
1028 write_sgl(mbuf, q, (struct ulptx_sgl *)(cpl + 1), end, 0, addr);
1030 txq->stats.tx_bytes += len;
1032 sd = &q->sdesc[q->pidx + (idx >> 1)];
1034 if (sd->coalesce.idx) {
1037 for (i = 0; i < sd->coalesce.idx; i++) {
1038 rte_pktmbuf_free(sd->coalesce.mbuf[i]);
1039 sd->coalesce.mbuf[i] = NULL;
1044 /* store pointers to the mbuf and the sgl used in free_tx_desc.
1045 * each tx desc can hold two pointers corresponding to the value
1046 * of ETH_COALESCE_PKT_PER_DESC
1048 sd->coalesce.mbuf[idx & 1] = mbuf;
1049 sd->coalesce.sgl[idx & 1] = (struct ulptx_sgl *)(cpl + 1);
1050 sd->coalesce.idx = (idx & 1) + 1;
1052 /* send the coaelsced work request if max reached */
1053 if (++q->coalesce.idx == ETH_COALESCE_PKT_NUM)
1054 ship_tx_pkt_coalesce_wr(adap, txq);
1059 * t4_eth_xmit - add a packet to an Ethernet Tx queue
1060 * @txq: the egress queue
1063 * Add a packet to an SGE Ethernet Tx queue. Runs with softirqs disabled.
1065 int t4_eth_xmit(struct sge_eth_txq *txq, struct rte_mbuf *mbuf)
1067 const struct port_info *pi;
1068 struct cpl_tx_pkt_lso_core *lso;
1069 struct adapter *adap;
1070 struct rte_mbuf *m = mbuf;
1071 struct fw_eth_tx_pkt_wr *wr;
1072 struct cpl_tx_pkt_core *cpl;
1073 struct tx_sw_desc *d;
1074 dma_addr_t addr[m->nb_segs];
1075 unsigned int flits, ndesc, cflits;
1076 int l3hdr_len, l4hdr_len, eth_xtra_len;
1082 u32 max_pkt_len = txq->eth_dev->data->dev_conf.rxmode.max_rx_pkt_len;
1084 /* Reject xmit if queue is stopped */
1085 if (unlikely(txq->flags & EQ_STOPPED))
1089 * The chip min packet length is 10 octets but play safe and reject
1090 * anything shorter than an Ethernet header.
1092 if (unlikely(m->pkt_len < ETHER_HDR_LEN)) {
1094 rte_pktmbuf_free(m);
1098 if ((!(m->ol_flags & PKT_TX_TCP_SEG)) &&
1099 (unlikely(m->pkt_len > max_pkt_len)))
1102 pi = (struct port_info *)txq->eth_dev->data->dev_private;
1105 cntrl = F_TXPKT_L4CSUM_DIS | F_TXPKT_IPCSUM_DIS;
1106 /* align the end of coalesce WR to a 512 byte boundary */
1107 txq->q.coalesce.max = (8 - (txq->q.pidx & 7)) * 8;
1109 if (!((m->ol_flags & PKT_TX_TCP_SEG) || (m->pkt_len > ETHER_MAX_LEN))) {
1110 if (should_tx_packet_coalesce(txq, mbuf, &cflits, adap)) {
1111 if (unlikely(map_mbuf(mbuf, addr) < 0)) {
1112 dev_warn(adap, "%s: mapping err for coalesce\n",
1114 txq->stats.mapping_err++;
1117 rte_prefetch0((volatile void *)addr);
1118 return tx_do_packet_coalesce(txq, mbuf, cflits, adap,
1125 if (txq->q.coalesce.idx)
1126 ship_tx_pkt_coalesce_wr(adap, txq);
1128 flits = calc_tx_flits(m);
1129 ndesc = flits_to_desc(flits);
1130 credits = txq_avail(&txq->q) - ndesc;
1132 if (unlikely(credits < 0)) {
1133 dev_debug(adap, "%s: Tx ring %u full; credits = %d\n",
1134 __func__, txq->q.cntxt_id, credits);
1138 if (unlikely(map_mbuf(m, addr) < 0)) {
1139 txq->stats.mapping_err++;
1143 wr_mid = V_FW_WR_LEN16(DIV_ROUND_UP(flits, 2));
1144 if (Q_IDXDIFF(&txq->q, equeidx) >= 64) {
1145 txq->q.equeidx = txq->q.pidx;
1146 wr_mid |= F_FW_WR_EQUEQ;
1149 wr = (void *)&txq->q.desc[txq->q.pidx];
1150 wr->equiq_to_len16 = htonl(wr_mid);
1151 wr->r3 = rte_cpu_to_be_64(0);
1152 end = (u64 *)wr + flits;
1155 len += sizeof(*cpl);
1157 /* Coalescing skipped and we send through normal path */
1158 if (!(m->ol_flags & PKT_TX_TCP_SEG)) {
1159 wr->op_immdlen = htonl(V_FW_WR_OP(FW_ETH_TX_PKT_WR) |
1160 V_FW_WR_IMMDLEN(len));
1161 cpl = (void *)(wr + 1);
1162 if (m->ol_flags & PKT_TX_IP_CKSUM) {
1163 cntrl = hwcsum(adap->params.chip, m) |
1165 txq->stats.tx_cso++;
1168 lso = (void *)(wr + 1);
1169 v6 = (m->ol_flags & PKT_TX_IPV6) != 0;
1170 l3hdr_len = m->l3_len;
1171 l4hdr_len = m->l4_len;
1172 eth_xtra_len = m->l2_len - ETHER_HDR_LEN;
1173 len += sizeof(*lso);
1174 wr->op_immdlen = htonl(V_FW_WR_OP(FW_ETH_TX_PKT_WR) |
1175 V_FW_WR_IMMDLEN(len));
1176 lso->lso_ctrl = htonl(V_LSO_OPCODE(CPL_TX_PKT_LSO) |
1177 F_LSO_FIRST_SLICE | F_LSO_LAST_SLICE |
1179 V_LSO_ETHHDR_LEN(eth_xtra_len / 4) |
1180 V_LSO_IPHDR_LEN(l3hdr_len / 4) |
1181 V_LSO_TCPHDR_LEN(l4hdr_len / 4));
1182 lso->ipid_ofst = htons(0);
1183 lso->mss = htons(m->tso_segsz);
1184 lso->seqno_offset = htonl(0);
1185 if (is_t4(adap->params.chip))
1186 lso->len = htonl(m->pkt_len);
1188 lso->len = htonl(V_LSO_T5_XFER_SIZE(m->pkt_len));
1189 cpl = (void *)(lso + 1);
1191 if (CHELSIO_CHIP_VERSION(adap->params.chip) <= CHELSIO_T5)
1192 cntrl = V_TXPKT_ETHHDR_LEN(eth_xtra_len);
1194 cntrl = V_T6_TXPKT_ETHHDR_LEN(eth_xtra_len);
1196 cntrl |= V_TXPKT_CSUM_TYPE(v6 ? TX_CSUM_TCPIP6 :
1198 V_TXPKT_IPHDR_LEN(l3hdr_len);
1200 txq->stats.tx_cso += m->tso_segsz;
1203 if (m->ol_flags & PKT_TX_VLAN_PKT) {
1204 txq->stats.vlan_ins++;
1205 cntrl |= F_TXPKT_VLAN_VLD | V_TXPKT_VLAN(m->vlan_tci);
1208 cpl->ctrl0 = htonl(V_TXPKT_OPCODE(CPL_TX_PKT_XT) |
1209 V_TXPKT_INTF(pi->tx_chan) |
1210 V_TXPKT_PF(adap->pf));
1211 cpl->pack = htons(0);
1212 cpl->len = htons(m->pkt_len);
1213 cpl->ctrl1 = cpu_to_be64(cntrl);
1216 txq->stats.tx_bytes += m->pkt_len;
1217 last_desc = txq->q.pidx + ndesc - 1;
1218 if (last_desc >= (int)txq->q.size)
1219 last_desc -= txq->q.size;
1221 d = &txq->q.sdesc[last_desc];
1222 if (d->coalesce.idx) {
1225 for (i = 0; i < d->coalesce.idx; i++) {
1226 rte_pktmbuf_free(d->coalesce.mbuf[i]);
1227 d->coalesce.mbuf[i] = NULL;
1229 d->coalesce.idx = 0;
1231 write_sgl(m, &txq->q, (struct ulptx_sgl *)(cpl + 1), end, 0,
1233 txq->q.sdesc[last_desc].mbuf = m;
1234 txq->q.sdesc[last_desc].sgl = (struct ulptx_sgl *)(cpl + 1);
1235 txq_advance(&txq->q, ndesc);
1236 ring_tx_db(adap, &txq->q);
1241 * alloc_ring - allocate resources for an SGE descriptor ring
1242 * @dev: the PCI device's core device
1243 * @nelem: the number of descriptors
1244 * @elem_size: the size of each descriptor
1245 * @sw_size: the size of the SW state associated with each ring element
1246 * @phys: the physical address of the allocated ring
1247 * @metadata: address of the array holding the SW state for the ring
1248 * @stat_size: extra space in HW ring for status information
1249 * @node: preferred node for memory allocations
1251 * Allocates resources for an SGE descriptor ring, such as Tx queues,
1252 * free buffer lists, or response queues. Each SGE ring requires
1253 * space for its HW descriptors plus, optionally, space for the SW state
1254 * associated with each HW entry (the metadata). The function returns
1255 * three values: the virtual address for the HW ring (the return value
1256 * of the function), the bus address of the HW ring, and the address
1259 static void *alloc_ring(size_t nelem, size_t elem_size,
1260 size_t sw_size, dma_addr_t *phys, void *metadata,
1261 size_t stat_size, __rte_unused uint16_t queue_id,
1262 int socket_id, const char *z_name,
1263 const char *z_name_sw)
1265 size_t len = CXGBE_MAX_RING_DESC_SIZE * elem_size + stat_size;
1266 const struct rte_memzone *tz;
1269 dev_debug(adapter, "%s: nelem = %zu; elem_size = %zu; sw_size = %zu; "
1270 "stat_size = %zu; queue_id = %u; socket_id = %d; z_name = %s;"
1271 " z_name_sw = %s\n", __func__, nelem, elem_size, sw_size,
1272 stat_size, queue_id, socket_id, z_name, z_name_sw);
1274 tz = rte_memzone_lookup(z_name);
1276 dev_debug(adapter, "%s: tz exists...returning existing..\n",
1282 * Allocate TX/RX ring hardware descriptors. A memzone large enough to
1283 * handle the maximum ring size is allocated in order to allow for
1284 * resizing in later calls to the queue setup function.
1286 tz = rte_memzone_reserve_aligned(z_name, len, socket_id, 0, 4096);
1291 memset(tz->addr, 0, len);
1293 s = rte_zmalloc_socket(z_name_sw, nelem * sw_size,
1294 RTE_CACHE_LINE_SIZE, socket_id);
1297 dev_err(adapter, "%s: failed to get sw_ring memory\n",
1303 *(void **)metadata = s;
1305 *phys = (uint64_t)tz->phys_addr;
1310 * t4_pktgl_to_mbuf_usembufs - build an mbuf from a packet gather list
1311 * @gl: the gather list
1313 * Builds an mbuf from the given packet gather list. Returns the mbuf or
1314 * %NULL if mbuf allocation failed.
1316 static struct rte_mbuf *t4_pktgl_to_mbuf_usembufs(const struct pkt_gl *gl)
1319 * If there's only one mbuf fragment, just return that.
1321 if (likely(gl->nfrags == 1))
1322 return gl->mbufs[0];
1328 * t4_pktgl_to_mbuf - build an mbuf from a packet gather list
1329 * @gl: the gather list
1331 * Builds an mbuf from the given packet gather list. Returns the mbuf or
1332 * %NULL if mbuf allocation failed.
1334 static struct rte_mbuf *t4_pktgl_to_mbuf(const struct pkt_gl *gl)
1336 return t4_pktgl_to_mbuf_usembufs(gl);
1340 * t4_ethrx_handler - process an ingress ethernet packet
1341 * @q: the response queue that received the packet
1342 * @rsp: the response queue descriptor holding the RX_PKT message
1343 * @si: the gather list of packet fragments
1345 * Process an ingress ethernet packet and deliver it to the stack.
1347 int t4_ethrx_handler(struct sge_rspq *q, const __be64 *rsp,
1348 const struct pkt_gl *si)
1350 struct rte_mbuf *mbuf;
1351 const struct cpl_rx_pkt *pkt;
1352 const struct rss_header *rss_hdr;
1354 struct sge_eth_rxq *rxq = container_of(q, struct sge_eth_rxq, rspq);
1356 rss_hdr = (const void *)rsp;
1357 pkt = (const void *)&rsp[1];
1358 csum_ok = pkt->csum_calc && !pkt->err_vec;
1360 mbuf = t4_pktgl_to_mbuf(si);
1361 if (unlikely(!mbuf)) {
1362 rxq->stats.rx_drops++;
1366 mbuf->port = pkt->iff;
1367 if (pkt->l2info & htonl(F_RXF_IP)) {
1368 mbuf->packet_type = RTE_PTYPE_L3_IPV4;
1369 if (unlikely(!csum_ok))
1370 mbuf->ol_flags |= PKT_RX_IP_CKSUM_BAD;
1372 if ((pkt->l2info & htonl(F_RXF_UDP | F_RXF_TCP)) && !csum_ok)
1373 mbuf->ol_flags |= PKT_RX_L4_CKSUM_BAD;
1374 } else if (pkt->l2info & htonl(F_RXF_IP6)) {
1375 mbuf->packet_type = RTE_PTYPE_L3_IPV6;
1378 mbuf->port = pkt->iff;
1380 if (!rss_hdr->filter_tid && rss_hdr->hash_type) {
1381 mbuf->ol_flags |= PKT_RX_RSS_HASH;
1382 mbuf->hash.rss = ntohl(rss_hdr->hash_val);
1386 mbuf->ol_flags |= PKT_RX_VLAN_PKT;
1387 mbuf->vlan_tci = ntohs(pkt->vlan);
1390 rxq->stats.rx_bytes += mbuf->pkt_len;
1396 * is_new_response - check if a response is newly written
1397 * @r: the response descriptor
1398 * @q: the response queue
1400 * Returns true if a response descriptor contains a yet unprocessed
1403 static inline bool is_new_response(const struct rsp_ctrl *r,
1404 const struct sge_rspq *q)
1406 return (r->u.type_gen >> S_RSPD_GEN) == q->gen;
1409 #define CXGB4_MSG_AN ((void *)1)
1412 * rspq_next - advance to the next entry in a response queue
1415 * Updates the state of a response queue to advance it to the next entry.
1417 static inline void rspq_next(struct sge_rspq *q)
1419 q->cur_desc = (const __be64 *)((const char *)q->cur_desc + q->iqe_len);
1420 if (unlikely(++q->cidx == q->size)) {
1423 q->cur_desc = q->desc;
1428 * process_responses - process responses from an SGE response queue
1429 * @q: the ingress queue to process
1430 * @budget: how many responses can be processed in this round
1431 * @rx_pkts: mbuf to put the pkts
1433 * Process responses from an SGE response queue up to the supplied budget.
1434 * Responses include received packets as well as control messages from FW
1437 * Additionally choose the interrupt holdoff time for the next interrupt
1438 * on this queue. If the system is under memory shortage use a fairly
1439 * long delay to help recovery.
1441 static int process_responses(struct sge_rspq *q, int budget,
1442 struct rte_mbuf **rx_pkts)
1444 int ret = 0, rsp_type;
1445 int budget_left = budget;
1446 const struct rsp_ctrl *rc;
1447 struct sge_eth_rxq *rxq = container_of(q, struct sge_eth_rxq, rspq);
1449 while (likely(budget_left)) {
1450 rc = (const struct rsp_ctrl *)
1451 ((const char *)q->cur_desc + (q->iqe_len - sizeof(*rc)));
1453 if (!is_new_response(rc, q))
1457 * Ensure response has been read
1460 rsp_type = G_RSPD_TYPE(rc->u.type_gen);
1462 if (likely(rsp_type == X_RSPD_TYPE_FLBUF)) {
1463 const struct rx_sw_desc *rsd =
1464 &rxq->fl.sdesc[rxq->fl.cidx];
1465 const struct rss_header *rss_hdr =
1466 (const void *)q->cur_desc;
1467 const struct cpl_rx_pkt *cpl =
1468 (const void *)&q->cur_desc[1];
1469 bool csum_ok = cpl->csum_calc && !cpl->err_vec;
1470 struct rte_mbuf *pkt, *npkt;
1473 len = ntohl(rc->pldbuflen_qid);
1474 BUG_ON(!(len & F_RSPD_NEWBUF));
1477 len = G_RSPD_LEN(len);
1480 /* Chain mbufs into len if necessary */
1482 struct rte_mbuf *new_pkt = rsd->buf;
1484 bufsz = min(get_buf_size(q->adapter, rsd), len);
1485 new_pkt->data_len = bufsz;
1486 unmap_rx_buf(&rxq->fl);
1488 npkt->next = new_pkt;
1491 rsd = &rxq->fl.sdesc[rxq->fl.cidx];
1496 if (cpl->l2info & htonl(F_RXF_IP)) {
1497 pkt->packet_type = RTE_PTYPE_L3_IPV4;
1498 if (unlikely(!csum_ok))
1499 pkt->ol_flags |= PKT_RX_IP_CKSUM_BAD;
1502 htonl(F_RXF_UDP | F_RXF_TCP)) && !csum_ok)
1503 pkt->ol_flags |= PKT_RX_L4_CKSUM_BAD;
1504 } else if (cpl->l2info & htonl(F_RXF_IP6)) {
1505 pkt->packet_type = RTE_PTYPE_L3_IPV6;
1508 if (!rss_hdr->filter_tid && rss_hdr->hash_type) {
1509 pkt->ol_flags |= PKT_RX_RSS_HASH;
1510 pkt->hash.rss = ntohl(rss_hdr->hash_val);
1514 pkt->ol_flags |= PKT_RX_VLAN_PKT;
1515 pkt->vlan_tci = ntohs(cpl->vlan);
1518 rxq->stats.rx_bytes += pkt->pkt_len;
1519 rx_pkts[budget - budget_left] = pkt;
1520 } else if (likely(rsp_type == X_RSPD_TYPE_CPL)) {
1521 ret = q->handler(q, q->cur_desc, NULL);
1523 ret = q->handler(q, (const __be64 *)rc, CXGB4_MSG_AN);
1526 if (unlikely(ret)) {
1527 /* couldn't process descriptor, back off for recovery */
1528 q->next_intr_params = V_QINTR_TIMER_IDX(NOMEM_TMR_IDX);
1535 if (R_IDXDIFF(q, gts_idx) >= 64) {
1536 unsigned int cidx_inc = R_IDXDIFF(q, gts_idx);
1537 unsigned int params;
1540 if (fl_cap(&rxq->fl) - rxq->fl.avail >= 64)
1541 __refill_fl(q->adapter, &rxq->fl);
1542 params = V_QINTR_TIMER_IDX(X_TIMERREG_UPDATE_CIDX);
1543 q->next_intr_params = params;
1544 val = V_CIDXINC(cidx_inc) | V_SEINTARM(params);
1546 if (unlikely(!q->bar2_addr))
1547 t4_write_reg(q->adapter, MYPF_REG(A_SGE_PF_GTS),
1549 V_INGRESSQID((u32)q->cntxt_id));
1551 writel(val | V_INGRESSQID(q->bar2_qid),
1552 (void *)((uintptr_t)q->bar2_addr +
1555 * This Write memory Barrier will force the
1556 * write to the User Doorbell area to be
1561 q->gts_idx = q->cidx;
1566 * If this is a Response Queue with an associated Free List and
1567 * there's room for another chunk of new Free List buffer pointers,
1568 * refill the Free List.
1571 if (q->offset >= 0 && fl_cap(&rxq->fl) - rxq->fl.avail >= 64)
1572 __refill_fl(q->adapter, &rxq->fl);
1574 return budget - budget_left;
1577 int cxgbe_poll(struct sge_rspq *q, struct rte_mbuf **rx_pkts,
1578 unsigned int budget, unsigned int *work_done)
1582 *work_done = process_responses(q, budget, rx_pkts);
1587 * bar2_address - return the BAR2 address for an SGE Queue's Registers
1588 * @adapter: the adapter
1589 * @qid: the SGE Queue ID
1590 * @qtype: the SGE Queue Type (Egress or Ingress)
1591 * @pbar2_qid: BAR2 Queue ID or 0 for Queue ID inferred SGE Queues
1593 * Returns the BAR2 address for the SGE Queue Registers associated with
1594 * @qid. If BAR2 SGE Registers aren't available, returns NULL. Also
1595 * returns the BAR2 Queue ID to be used with writes to the BAR2 SGE
1596 * Queue Registers. If the BAR2 Queue ID is 0, then "Inferred Queue ID"
1597 * Registers are supported (e.g. the Write Combining Doorbell Buffer).
1599 static void __iomem *bar2_address(struct adapter *adapter, unsigned int qid,
1600 enum t4_bar2_qtype qtype,
1601 unsigned int *pbar2_qid)
1606 ret = t4_bar2_sge_qregs(adapter, qid, qtype, &bar2_qoffset, pbar2_qid);
1610 return adapter->bar2 + bar2_qoffset;
1613 int t4_sge_eth_rxq_start(struct adapter *adap, struct sge_rspq *rq)
1615 struct sge_eth_rxq *rxq = container_of(rq, struct sge_eth_rxq, rspq);
1616 unsigned int fl_id = rxq->fl.size ? rxq->fl.cntxt_id : 0xffff;
1618 return t4_iq_start_stop(adap, adap->mbox, true, adap->pf, 0,
1619 rq->cntxt_id, fl_id, 0xffff);
1622 int t4_sge_eth_rxq_stop(struct adapter *adap, struct sge_rspq *rq)
1624 struct sge_eth_rxq *rxq = container_of(rq, struct sge_eth_rxq, rspq);
1625 unsigned int fl_id = rxq->fl.size ? rxq->fl.cntxt_id : 0xffff;
1627 return t4_iq_start_stop(adap, adap->mbox, false, adap->pf, 0,
1628 rq->cntxt_id, fl_id, 0xffff);
1632 * @intr_idx: MSI/MSI-X vector if >=0, -(absolute qid + 1) if < 0
1633 * @cong: < 0 -> no congestion feedback, >= 0 -> congestion channel map
1635 int t4_sge_alloc_rxq(struct adapter *adap, struct sge_rspq *iq, bool fwevtq,
1636 struct rte_eth_dev *eth_dev, int intr_idx,
1637 struct sge_fl *fl, rspq_handler_t hnd, int cong,
1638 struct rte_mempool *mp, int queue_id, int socket_id)
1642 struct sge *s = &adap->sge;
1643 struct port_info *pi = (struct port_info *)(eth_dev->data->dev_private);
1644 char z_name[RTE_MEMZONE_NAMESIZE];
1645 char z_name_sw[RTE_MEMZONE_NAMESIZE];
1646 unsigned int nb_refill;
1648 /* Size needs to be multiple of 16, including status entry. */
1649 iq->size = cxgbe_roundup(iq->size, 16);
1651 snprintf(z_name, sizeof(z_name), "%s_%s_%d_%d",
1652 eth_dev->data->drv_name,
1653 fwevtq ? "fwq_ring" : "rx_ring",
1654 eth_dev->data->port_id, queue_id);
1655 snprintf(z_name_sw, sizeof(z_name_sw), "%s_sw_ring", z_name);
1657 iq->desc = alloc_ring(iq->size, iq->iqe_len, 0, &iq->phys_addr, NULL, 0,
1658 queue_id, socket_id, z_name, z_name_sw);
1662 memset(&c, 0, sizeof(c));
1663 c.op_to_vfn = htonl(V_FW_CMD_OP(FW_IQ_CMD) | F_FW_CMD_REQUEST |
1664 F_FW_CMD_WRITE | F_FW_CMD_EXEC |
1665 V_FW_IQ_CMD_PFN(adap->pf) | V_FW_IQ_CMD_VFN(0));
1666 c.alloc_to_len16 = htonl(F_FW_IQ_CMD_ALLOC | F_FW_IQ_CMD_IQSTART |
1668 c.type_to_iqandstindex =
1669 htonl(V_FW_IQ_CMD_TYPE(FW_IQ_TYPE_FL_INT_CAP) |
1670 V_FW_IQ_CMD_IQASYNCH(fwevtq) |
1671 V_FW_IQ_CMD_VIID(pi->viid) |
1672 V_FW_IQ_CMD_IQANDST(intr_idx < 0) |
1673 V_FW_IQ_CMD_IQANUD(X_UPDATEDELIVERY_INTERRUPT) |
1674 V_FW_IQ_CMD_IQANDSTINDEX(intr_idx >= 0 ? intr_idx :
1676 c.iqdroprss_to_iqesize =
1677 htons(V_FW_IQ_CMD_IQPCIECH(pi->tx_chan) |
1678 F_FW_IQ_CMD_IQGTSMODE |
1679 V_FW_IQ_CMD_IQINTCNTTHRESH(iq->pktcnt_idx) |
1680 V_FW_IQ_CMD_IQESIZE(ilog2(iq->iqe_len) - 4));
1681 c.iqsize = htons(iq->size);
1682 c.iqaddr = cpu_to_be64(iq->phys_addr);
1684 c.iqns_to_fl0congen = htonl(F_FW_IQ_CMD_IQFLINTCONGEN);
1687 struct sge_eth_rxq *rxq = container_of(fl, struct sge_eth_rxq,
1689 unsigned int chip_ver = CHELSIO_CHIP_VERSION(adap->params.chip);
1692 * Allocate the ring for the hardware free list (with space
1693 * for its status page) along with the associated software
1694 * descriptor ring. The free list size needs to be a multiple
1695 * of the Egress Queue Unit and at least 2 Egress Units larger
1696 * than the SGE's Egress Congrestion Threshold
1697 * (fl_starve_thres - 1).
1699 if (fl->size < s->fl_starve_thres - 1 + 2 * 8)
1700 fl->size = s->fl_starve_thres - 1 + 2 * 8;
1701 fl->size = cxgbe_roundup(fl->size, 8);
1703 snprintf(z_name, sizeof(z_name), "%s_%s_%d_%d",
1704 eth_dev->data->drv_name,
1705 fwevtq ? "fwq_ring" : "fl_ring",
1706 eth_dev->data->port_id, queue_id);
1707 snprintf(z_name_sw, sizeof(z_name_sw), "%s_sw_ring", z_name);
1709 fl->desc = alloc_ring(fl->size, sizeof(__be64),
1710 sizeof(struct rx_sw_desc),
1711 &fl->addr, &fl->sdesc, s->stat_len,
1712 queue_id, socket_id, z_name, z_name_sw);
1717 flsz = fl->size / 8 + s->stat_len / sizeof(struct tx_desc);
1718 c.iqns_to_fl0congen |=
1719 htonl(V_FW_IQ_CMD_FL0HOSTFCMODE(X_HOSTFCMODE_NONE) |
1720 (unlikely(rxq->usembufs) ?
1721 0 : F_FW_IQ_CMD_FL0PACKEN) |
1722 F_FW_IQ_CMD_FL0FETCHRO | F_FW_IQ_CMD_FL0DATARO |
1723 F_FW_IQ_CMD_FL0PADEN);
1725 c.iqns_to_fl0congen |=
1726 htonl(V_FW_IQ_CMD_FL0CNGCHMAP(cong) |
1727 F_FW_IQ_CMD_FL0CONGCIF |
1728 F_FW_IQ_CMD_FL0CONGEN);
1730 /* In T6, for egress queue type FL there is internal overhead
1731 * of 16B for header going into FLM module.
1732 * Hence maximum allowed burst size will be 448 bytes.
1734 c.fl0dcaen_to_fl0cidxfthresh =
1735 htons(V_FW_IQ_CMD_FL0FBMIN(chip_ver <= CHELSIO_T5 ?
1736 X_FETCHBURSTMIN_128B :
1737 X_FETCHBURSTMIN_64B) |
1738 V_FW_IQ_CMD_FL0FBMAX(chip_ver <= CHELSIO_T5 ?
1739 X_FETCHBURSTMAX_512B :
1740 X_FETCHBURSTMAX_256B));
1741 c.fl0size = htons(flsz);
1742 c.fl0addr = cpu_to_be64(fl->addr);
1745 ret = t4_wr_mbox(adap, adap->mbox, &c, sizeof(c), &c);
1749 iq->cur_desc = iq->desc;
1753 iq->next_intr_params = iq->intr_params;
1754 iq->cntxt_id = ntohs(c.iqid);
1755 iq->abs_id = ntohs(c.physiqid);
1756 iq->bar2_addr = bar2_address(adap, iq->cntxt_id, T4_BAR2_QTYPE_INGRESS,
1758 iq->size--; /* subtract status entry */
1759 iq->eth_dev = eth_dev;
1761 iq->port_id = pi->port_id;
1764 /* set offset to -1 to distinguish ingress queues without FL */
1765 iq->offset = fl ? 0 : -1;
1768 fl->cntxt_id = ntohs(c.fl0id);
1773 fl->alloc_failed = 0;
1776 * Note, we must initialize the BAR2 Free List User Doorbell
1777 * information before refilling the Free List!
1779 fl->bar2_addr = bar2_address(adap, fl->cntxt_id,
1780 T4_BAR2_QTYPE_EGRESS,
1783 nb_refill = refill_fl(adap, fl, fl_cap(fl));
1784 if (nb_refill != fl_cap(fl)) {
1786 dev_err(adap, "%s: mbuf alloc failed with error: %d\n",
1793 * For T5 and later we attempt to set up the Congestion Manager values
1794 * of the new RX Ethernet Queue. This should really be handled by
1795 * firmware because it's more complex than any host driver wants to
1796 * get involved with and it's different per chip and this is almost
1797 * certainly wrong. Formware would be wrong as well, but it would be
1798 * a lot easier to fix in one place ... For now we do something very
1799 * simple (and hopefully less wrong).
1801 if (!is_t4(adap->params.chip) && cong >= 0) {
1805 param = (V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_DMAQ) |
1806 V_FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_DMAQ_CONM_CTXT) |
1807 V_FW_PARAMS_PARAM_YZ(iq->cntxt_id));
1809 val = V_CONMCTXT_CNGTPMODE(X_CONMCTXT_CNGTPMODE_QUEUE);
1811 val = V_CONMCTXT_CNGTPMODE(
1812 X_CONMCTXT_CNGTPMODE_CHANNEL);
1813 for (i = 0; i < 4; i++) {
1814 if (cong & (1 << i))
1815 val |= V_CONMCTXT_CNGCHMAP(1 <<
1819 ret = t4_set_params(adap, adap->mbox, adap->pf, 0, 1,
1822 dev_warn(adap->pdev_dev, "Failed to set Congestion Manager Context for Ingress Queue %d: %d\n",
1823 iq->cntxt_id, -ret);
1829 t4_iq_free(adap, adap->mbox, adap->pf, 0, FW_IQ_TYPE_FL_INT_CAP,
1830 iq->cntxt_id, fl->cntxt_id, 0xffff);
1839 if (fl && fl->desc) {
1840 rte_free(fl->sdesc);
1848 static void init_txq(struct adapter *adap, struct sge_txq *q, unsigned int id)
1851 q->bar2_addr = bar2_address(adap, q->cntxt_id, T4_BAR2_QTYPE_EGRESS,
1858 q->coalesce.idx = 0;
1859 q->coalesce.len = 0;
1860 q->coalesce.flits = 0;
1861 q->last_coal_idx = 0;
1863 q->stat = (void *)&q->desc[q->size];
1866 int t4_sge_eth_txq_start(struct sge_eth_txq *txq)
1869 * TODO: For flow-control, queue may be stopped waiting to reclaim
1871 * Ensure queue is in EQ_STOPPED state before starting it.
1873 if (!(txq->flags & EQ_STOPPED))
1876 txq->flags &= ~EQ_STOPPED;
1881 int t4_sge_eth_txq_stop(struct sge_eth_txq *txq)
1883 txq->flags |= EQ_STOPPED;
1888 int t4_sge_alloc_eth_txq(struct adapter *adap, struct sge_eth_txq *txq,
1889 struct rte_eth_dev *eth_dev, uint16_t queue_id,
1890 unsigned int iqid, int socket_id)
1893 struct fw_eq_eth_cmd c;
1894 struct sge *s = &adap->sge;
1895 struct port_info *pi = (struct port_info *)(eth_dev->data->dev_private);
1896 char z_name[RTE_MEMZONE_NAMESIZE];
1897 char z_name_sw[RTE_MEMZONE_NAMESIZE];
1899 /* Add status entries */
1900 nentries = txq->q.size + s->stat_len / sizeof(struct tx_desc);
1902 snprintf(z_name, sizeof(z_name), "%s_%s_%d_%d",
1903 eth_dev->data->drv_name, "tx_ring",
1904 eth_dev->data->port_id, queue_id);
1905 snprintf(z_name_sw, sizeof(z_name_sw), "%s_sw_ring", z_name);
1907 txq->q.desc = alloc_ring(txq->q.size, sizeof(struct tx_desc),
1908 sizeof(struct tx_sw_desc), &txq->q.phys_addr,
1909 &txq->q.sdesc, s->stat_len, queue_id,
1910 socket_id, z_name, z_name_sw);
1914 memset(&c, 0, sizeof(c));
1915 c.op_to_vfn = htonl(V_FW_CMD_OP(FW_EQ_ETH_CMD) | F_FW_CMD_REQUEST |
1916 F_FW_CMD_WRITE | F_FW_CMD_EXEC |
1917 V_FW_EQ_ETH_CMD_PFN(adap->pf) |
1918 V_FW_EQ_ETH_CMD_VFN(0));
1919 c.alloc_to_len16 = htonl(F_FW_EQ_ETH_CMD_ALLOC |
1920 F_FW_EQ_ETH_CMD_EQSTART | (sizeof(c) / 16));
1921 c.autoequiqe_to_viid = htonl(F_FW_EQ_ETH_CMD_AUTOEQUEQE |
1922 V_FW_EQ_ETH_CMD_VIID(pi->viid));
1923 c.fetchszm_to_iqid =
1924 htonl(V_FW_EQ_ETH_CMD_HOSTFCMODE(X_HOSTFCMODE_NONE) |
1925 V_FW_EQ_ETH_CMD_PCIECHN(pi->tx_chan) |
1926 F_FW_EQ_ETH_CMD_FETCHRO | V_FW_EQ_ETH_CMD_IQID(iqid));
1928 htonl(V_FW_EQ_ETH_CMD_FBMIN(X_FETCHBURSTMIN_64B) |
1929 V_FW_EQ_ETH_CMD_FBMAX(X_FETCHBURSTMAX_512B) |
1930 V_FW_EQ_ETH_CMD_EQSIZE(nentries));
1931 c.eqaddr = rte_cpu_to_be_64(txq->q.phys_addr);
1933 ret = t4_wr_mbox(adap, adap->mbox, &c, sizeof(c), &c);
1935 rte_free(txq->q.sdesc);
1936 txq->q.sdesc = NULL;
1941 init_txq(adap, &txq->q, G_FW_EQ_ETH_CMD_EQID(ntohl(c.eqid_pkd)));
1943 txq->stats.pkts = 0;
1944 txq->stats.tx_cso = 0;
1945 txq->stats.coal_wr = 0;
1946 txq->stats.vlan_ins = 0;
1947 txq->stats.tx_bytes = 0;
1948 txq->stats.coal_pkts = 0;
1949 txq->stats.mapping_err = 0;
1950 txq->flags |= EQ_STOPPED;
1951 txq->eth_dev = eth_dev;
1952 t4_os_lock_init(&txq->txq_lock);
1956 static void free_txq(struct sge_txq *q)
1963 static void free_rspq_fl(struct adapter *adap, struct sge_rspq *rq,
1966 unsigned int fl_id = fl ? fl->cntxt_id : 0xffff;
1968 t4_iq_free(adap, adap->mbox, adap->pf, 0, FW_IQ_TYPE_FL_INT_CAP,
1969 rq->cntxt_id, fl_id, 0xffff);
1975 free_rx_bufs(fl, fl->avail);
1976 rte_free(fl->sdesc);
1984 * Clear all queues of the port
1986 * Note: This function must only be called after rx and tx path
1987 * of the port have been disabled.
1989 void t4_sge_eth_clear_queues(struct port_info *pi)
1992 struct adapter *adap = pi->adapter;
1993 struct sge_eth_rxq *rxq = &adap->sge.ethrxq[pi->first_qset];
1994 struct sge_eth_txq *txq = &adap->sge.ethtxq[pi->first_qset];
1996 for (i = 0; i < pi->n_rx_qsets; i++, rxq++) {
1998 t4_sge_eth_rxq_stop(adap, &rxq->rspq);
2000 for (i = 0; i < pi->n_tx_qsets; i++, txq++) {
2002 struct sge_txq *q = &txq->q;
2004 t4_sge_eth_txq_stop(txq);
2005 reclaim_completed_tx(q);
2006 free_tx_desc(q, q->size);
2007 q->equeidx = q->pidx;
2012 void t4_sge_eth_rxq_release(struct adapter *adap, struct sge_eth_rxq *rxq)
2014 if (rxq->rspq.desc) {
2015 t4_sge_eth_rxq_stop(adap, &rxq->rspq);
2016 free_rspq_fl(adap, &rxq->rspq, rxq->fl.size ? &rxq->fl : NULL);
2020 void t4_sge_eth_txq_release(struct adapter *adap, struct sge_eth_txq *txq)
2023 t4_sge_eth_txq_stop(txq);
2024 reclaim_completed_tx(&txq->q);
2025 t4_eth_eq_free(adap, adap->mbox, adap->pf, 0, txq->q.cntxt_id);
2026 free_tx_desc(&txq->q, txq->q.size);
2027 rte_free(txq->q.sdesc);
2032 void t4_sge_tx_monitor_start(struct adapter *adap)
2034 rte_eal_alarm_set(50, tx_timer_cb, (void *)adap);
2037 void t4_sge_tx_monitor_stop(struct adapter *adap)
2039 rte_eal_alarm_cancel(tx_timer_cb, (void *)adap);
2043 * t4_free_sge_resources - free SGE resources
2044 * @adap: the adapter
2046 * Frees resources used by the SGE queue sets.
2048 void t4_free_sge_resources(struct adapter *adap)
2051 struct sge_eth_rxq *rxq = &adap->sge.ethrxq[0];
2052 struct sge_eth_txq *txq = &adap->sge.ethtxq[0];
2054 /* clean up Ethernet Tx/Rx queues */
2055 for (i = 0; i < adap->sge.max_ethqsets; i++, rxq++, txq++) {
2056 /* Free only the queues allocated */
2057 if (rxq->rspq.desc) {
2058 t4_sge_eth_rxq_release(adap, rxq);
2059 rxq->rspq.eth_dev = NULL;
2062 t4_sge_eth_txq_release(adap, txq);
2063 txq->eth_dev = NULL;
2067 if (adap->sge.fw_evtq.desc)
2068 free_rspq_fl(adap, &adap->sge.fw_evtq, NULL);
2072 * t4_sge_init - initialize SGE
2073 * @adap: the adapter
2075 * Performs SGE initialization needed every time after a chip reset.
2076 * We do not initialize any of the queues here, instead the driver
2077 * top-level must request those individually.
2079 * Called in two different modes:
2081 * 1. Perform actual hardware initialization and record hard-coded
2082 * parameters which were used. This gets used when we're the
2083 * Master PF and the Firmware Configuration File support didn't
2084 * work for some reason.
2086 * 2. We're not the Master PF or initialization was performed with
2087 * a Firmware Configuration File. In this case we need to grab
2088 * any of the SGE operating parameters that we need to have in
2089 * order to do our job and make sure we can live with them ...
2091 static int t4_sge_init_soft(struct adapter *adap)
2093 struct sge *s = &adap->sge;
2094 u32 fl_small_pg, fl_large_pg, fl_small_mtu, fl_large_mtu;
2095 u32 timer_value_0_and_1, timer_value_2_and_3, timer_value_4_and_5;
2096 u32 ingress_rx_threshold;
2099 * Verify that CPL messages are going to the Ingress Queue for
2100 * process_responses() and that only packet data is going to the
2103 if ((t4_read_reg(adap, A_SGE_CONTROL) & F_RXPKTCPLMODE) !=
2104 V_RXPKTCPLMODE(X_RXPKTCPLMODE_SPLIT)) {
2105 dev_err(adap, "bad SGE CPL MODE\n");
2110 * Validate the Host Buffer Register Array indices that we want to
2113 * XXX Note that we should really read through the Host Buffer Size
2114 * XXX register array and find the indices of the Buffer Sizes which
2115 * XXX meet our needs!
2117 #define READ_FL_BUF(x) \
2118 t4_read_reg(adap, A_SGE_FL_BUFFER_SIZE0 + (x) * sizeof(u32))
2120 fl_small_pg = READ_FL_BUF(RX_SMALL_PG_BUF);
2121 fl_large_pg = READ_FL_BUF(RX_LARGE_PG_BUF);
2122 fl_small_mtu = READ_FL_BUF(RX_SMALL_MTU_BUF);
2123 fl_large_mtu = READ_FL_BUF(RX_LARGE_MTU_BUF);
2126 * We only bother using the Large Page logic if the Large Page Buffer
2127 * is larger than our Page Size Buffer.
2129 if (fl_large_pg <= fl_small_pg)
2135 * The Page Size Buffer must be exactly equal to our Page Size and the
2136 * Large Page Size Buffer should be 0 (per above) or a power of 2.
2138 if (fl_small_pg != CXGBE_PAGE_SIZE ||
2139 (fl_large_pg & (fl_large_pg - 1)) != 0) {
2140 dev_err(adap, "bad SGE FL page buffer sizes [%d, %d]\n",
2141 fl_small_pg, fl_large_pg);
2145 s->fl_pg_order = ilog2(fl_large_pg) - PAGE_SHIFT;
2147 if (adap->use_unpacked_mode) {
2150 if (fl_small_mtu < FL_MTU_SMALL_BUFSIZE(adap)) {
2151 dev_err(adap, "bad SGE FL small MTU %d\n",
2155 if (fl_large_mtu < FL_MTU_LARGE_BUFSIZE(adap)) {
2156 dev_err(adap, "bad SGE FL large MTU %d\n",
2165 * Retrieve our RX interrupt holdoff timer values and counter
2166 * threshold values from the SGE parameters.
2168 timer_value_0_and_1 = t4_read_reg(adap, A_SGE_TIMER_VALUE_0_AND_1);
2169 timer_value_2_and_3 = t4_read_reg(adap, A_SGE_TIMER_VALUE_2_AND_3);
2170 timer_value_4_and_5 = t4_read_reg(adap, A_SGE_TIMER_VALUE_4_AND_5);
2171 s->timer_val[0] = core_ticks_to_us(adap,
2172 G_TIMERVALUE0(timer_value_0_and_1));
2173 s->timer_val[1] = core_ticks_to_us(adap,
2174 G_TIMERVALUE1(timer_value_0_and_1));
2175 s->timer_val[2] = core_ticks_to_us(adap,
2176 G_TIMERVALUE2(timer_value_2_and_3));
2177 s->timer_val[3] = core_ticks_to_us(adap,
2178 G_TIMERVALUE3(timer_value_2_and_3));
2179 s->timer_val[4] = core_ticks_to_us(adap,
2180 G_TIMERVALUE4(timer_value_4_and_5));
2181 s->timer_val[5] = core_ticks_to_us(adap,
2182 G_TIMERVALUE5(timer_value_4_and_5));
2184 ingress_rx_threshold = t4_read_reg(adap, A_SGE_INGRESS_RX_THRESHOLD);
2185 s->counter_val[0] = G_THRESHOLD_0(ingress_rx_threshold);
2186 s->counter_val[1] = G_THRESHOLD_1(ingress_rx_threshold);
2187 s->counter_val[2] = G_THRESHOLD_2(ingress_rx_threshold);
2188 s->counter_val[3] = G_THRESHOLD_3(ingress_rx_threshold);
2193 int t4_sge_init(struct adapter *adap)
2195 struct sge *s = &adap->sge;
2196 u32 sge_control, sge_conm_ctrl;
2197 int ret, egress_threshold;
2200 * Ingress Padding Boundary and Egress Status Page Size are set up by
2201 * t4_fixup_host_params().
2203 sge_control = t4_read_reg(adap, A_SGE_CONTROL);
2204 s->pktshift = G_PKTSHIFT(sge_control);
2205 s->stat_len = (sge_control & F_EGRSTATUSPAGESIZE) ? 128 : 64;
2206 s->fl_align = t4_fl_pkt_align(adap);
2207 ret = t4_sge_init_soft(adap);
2209 dev_err(adap, "%s: t4_sge_init_soft failed, error %d\n",
2215 * A FL with <= fl_starve_thres buffers is starving and a periodic
2216 * timer will attempt to refill it. This needs to be larger than the
2217 * SGE's Egress Congestion Threshold. If it isn't, then we can get
2218 * stuck waiting for new packets while the SGE is waiting for us to
2219 * give it more Free List entries. (Note that the SGE's Egress
2220 * Congestion Threshold is in units of 2 Free List pointers.) For T4,
2221 * there was only a single field to control this. For T5 there's the
2222 * original field which now only applies to Unpacked Mode Free List
2223 * buffers and a new field which only applies to Packed Mode Free List
2226 sge_conm_ctrl = t4_read_reg(adap, A_SGE_CONM_CTRL);
2227 if (is_t4(adap->params.chip) || adap->use_unpacked_mode)
2228 egress_threshold = G_EGRTHRESHOLD(sge_conm_ctrl);
2230 egress_threshold = G_EGRTHRESHOLDPACKING(sge_conm_ctrl);
2231 s->fl_starve_thres = 2 * egress_threshold + 1;