xref: /dpdk/drivers/net/cxgbe/sge.c (revision 29fd052d)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2014-2018 Chelsio Communications.
3  * All rights reserved.
4  */
5 
6 #include <sys/queue.h>
7 #include <stdio.h>
8 #include <errno.h>
9 #include <stdint.h>
10 #include <string.h>
11 #include <unistd.h>
12 #include <stdarg.h>
13 #include <inttypes.h>
14 #include <netinet/in.h>
15 
16 #include <rte_byteorder.h>
17 #include <rte_common.h>
18 #include <rte_cycles.h>
19 #include <rte_interrupts.h>
20 #include <rte_log.h>
21 #include <rte_debug.h>
22 #include <rte_pci.h>
23 #include <rte_branch_prediction.h>
24 #include <rte_memory.h>
25 #include <rte_memzone.h>
26 #include <rte_tailq.h>
27 #include <rte_eal.h>
28 #include <rte_alarm.h>
29 #include <rte_ether.h>
30 #include <ethdev_driver.h>
31 #include <rte_malloc.h>
32 #include <rte_random.h>
33 #include <rte_dev.h>
34 
35 #include "base/common.h"
36 #include "base/t4_regs.h"
37 #include "base/t4_msg.h"
38 #include "cxgbe.h"
39 
40 static inline void ship_tx_pkt_coalesce_wr(struct adapter *adap,
41 					   struct sge_eth_txq *txq);
42 
43 /*
44  * Max number of Rx buffers we replenish at a time.
45  */
46 #define MAX_RX_REFILL 64U
47 
48 #define NOMEM_TMR_IDX (SGE_NTIMERS - 1)
49 
50 /*
51  * Max Tx descriptor space we allow for an Ethernet packet to be inlined
52  * into a WR.
53  */
54 #define MAX_IMM_TX_PKT_LEN 256
55 
56 /*
57  * Max size of a WR sent through a control Tx queue.
58  */
59 #define MAX_CTRL_WR_LEN SGE_MAX_WR_LEN
60 
61 /*
62  * Rx buffer sizes for "usembufs" Free List buffers (one ingress packet
63  * per mbuf buffer).  We currently only support two sizes for 1500- and
64  * 9000-byte MTUs. We could easily support more but there doesn't seem to be
65  * much need for that ...
66  */
67 #define FL_MTU_SMALL 1500
68 #define FL_MTU_LARGE 9000
69 
70 static inline unsigned int fl_mtu_bufsize(struct adapter *adapter,
71 					  unsigned int mtu)
72 {
73 	struct sge *s = &adapter->sge;
74 
75 	return CXGBE_ALIGN(s->pktshift + RTE_ETHER_HDR_LEN + RTE_VLAN_HLEN + mtu,
76 			   s->fl_align);
77 }
78 
79 #define FL_MTU_SMALL_BUFSIZE(adapter) fl_mtu_bufsize(adapter, FL_MTU_SMALL)
80 #define FL_MTU_LARGE_BUFSIZE(adapter) fl_mtu_bufsize(adapter, FL_MTU_LARGE)
81 
82 /*
83  * Bits 0..3 of rx_sw_desc.dma_addr have special meaning.  The hardware uses
84  * these to specify the buffer size as an index into the SGE Free List Buffer
85  * Size register array.  We also use bit 4, when the buffer has been unmapped
86  * for DMA, but this is of course never sent to the hardware and is only used
87  * to prevent double unmappings.  All of the above requires that the Free List
88  * Buffers which we allocate have the bottom 5 bits free (0) -- i.e. are
89  * 32-byte or or a power of 2 greater in alignment.  Since the SGE's minimal
90  * Free List Buffer alignment is 32 bytes, this works out for us ...
91  */
92 enum {
93 	RX_BUF_FLAGS     = 0x1f,   /* bottom five bits are special */
94 	RX_BUF_SIZE      = 0x0f,   /* bottom three bits are for buf sizes */
95 	RX_UNMAPPED_BUF  = 0x10,   /* buffer is not mapped */
96 
97 	/*
98 	 * XXX We shouldn't depend on being able to use these indices.
99 	 * XXX Especially when some other Master PF has initialized the
100 	 * XXX adapter or we use the Firmware Configuration File.  We
101 	 * XXX should really search through the Host Buffer Size register
102 	 * XXX array for the appropriately sized buffer indices.
103 	 */
104 	RX_SMALL_PG_BUF  = 0x0,   /* small (PAGE_SIZE) page buffer */
105 	RX_LARGE_PG_BUF  = 0x1,   /* buffer large page buffer */
106 
107 	RX_SMALL_MTU_BUF = 0x2,   /* small MTU buffer */
108 	RX_LARGE_MTU_BUF = 0x3,   /* large MTU buffer */
109 };
110 
111 /**
112  * txq_avail - return the number of available slots in a Tx queue
113  * @q: the Tx queue
114  *
115  * Returns the number of descriptors in a Tx queue available to write new
116  * packets.
117  */
118 static inline unsigned int txq_avail(const struct sge_txq *q)
119 {
120 	return q->size - 1 - q->in_use;
121 }
122 
123 static int map_mbuf(struct rte_mbuf *mbuf, dma_addr_t *addr)
124 {
125 	struct rte_mbuf *m = mbuf;
126 
127 	for (; m; m = m->next, addr++) {
128 		*addr = m->buf_iova + rte_pktmbuf_headroom(m);
129 		if (*addr == 0)
130 			goto out_err;
131 	}
132 	return 0;
133 
134 out_err:
135 	return -ENOMEM;
136 }
137 
138 /**
139  * free_tx_desc - reclaims Tx descriptors and their buffers
140  * @q: the Tx queue to reclaim descriptors from
141  * @n: the number of descriptors to reclaim
142  *
143  * Reclaims Tx descriptors from an SGE Tx queue and frees the associated
144  * Tx buffers.  Called with the Tx queue lock held.
145  */
146 static void free_tx_desc(struct sge_txq *q, unsigned int n)
147 {
148 	struct tx_sw_desc *d;
149 	unsigned int cidx = 0;
150 
151 	d = &q->sdesc[cidx];
152 	while (n--) {
153 		if (d->mbuf) {                       /* an SGL is present */
154 			rte_pktmbuf_free(d->mbuf);
155 			d->mbuf = NULL;
156 		}
157 		if (d->coalesce.idx) {
158 			int i;
159 
160 			for (i = 0; i < d->coalesce.idx; i++) {
161 				rte_pktmbuf_free(d->coalesce.mbuf[i]);
162 				d->coalesce.mbuf[i] = NULL;
163 			}
164 			d->coalesce.idx = 0;
165 		}
166 		++d;
167 		if (++cidx == q->size) {
168 			cidx = 0;
169 			d = q->sdesc;
170 		}
171 		RTE_MBUF_PREFETCH_TO_FREE(&q->sdesc->mbuf->pool);
172 	}
173 }
174 
175 static void reclaim_tx_desc(struct sge_txq *q, unsigned int n)
176 {
177 	struct tx_sw_desc *d;
178 	unsigned int cidx = q->cidx;
179 
180 	d = &q->sdesc[cidx];
181 	while (n--) {
182 		if (d->mbuf) {                       /* an SGL is present */
183 			rte_pktmbuf_free(d->mbuf);
184 			d->mbuf = NULL;
185 		}
186 		++d;
187 		if (++cidx == q->size) {
188 			cidx = 0;
189 			d = q->sdesc;
190 		}
191 	}
192 	q->cidx = cidx;
193 }
194 
195 /**
196  * fl_cap - return the capacity of a free-buffer list
197  * @fl: the FL
198  *
199  * Returns the capacity of a free-buffer list.  The capacity is less than
200  * the size because one descriptor needs to be left unpopulated, otherwise
201  * HW will think the FL is empty.
202  */
203 static inline unsigned int fl_cap(const struct sge_fl *fl)
204 {
205 	return fl->size - 8;   /* 1 descriptor = 8 buffers */
206 }
207 
208 /**
209  * fl_starving - return whether a Free List is starving.
210  * @adapter: pointer to the adapter
211  * @fl: the Free List
212  *
213  * Tests specified Free List to see whether the number of buffers
214  * available to the hardware has fallen below our "starvation"
215  * threshold.
216  */
217 static inline bool fl_starving(const struct adapter *adapter,
218 			       const struct sge_fl *fl)
219 {
220 	const struct sge *s = &adapter->sge;
221 
222 	return fl->avail - fl->pend_cred <= s->fl_starve_thres;
223 }
224 
225 static inline unsigned int get_buf_size(struct adapter *adapter,
226 					const struct rx_sw_desc *d)
227 {
228 	unsigned int rx_buf_size_idx = d->dma_addr & RX_BUF_SIZE;
229 	unsigned int buf_size = 0;
230 
231 	switch (rx_buf_size_idx) {
232 	case RX_SMALL_MTU_BUF:
233 		buf_size = FL_MTU_SMALL_BUFSIZE(adapter);
234 		break;
235 
236 	case RX_LARGE_MTU_BUF:
237 		buf_size = FL_MTU_LARGE_BUFSIZE(adapter);
238 		break;
239 
240 	default:
241 		BUG_ON(1);
242 		/* NOT REACHED */
243 	}
244 
245 	return buf_size;
246 }
247 
248 /**
249  * free_rx_bufs - free the Rx buffers on an SGE free list
250  * @q: the SGE free list to free buffers from
251  * @n: how many buffers to free
252  *
253  * Release the next @n buffers on an SGE free-buffer Rx queue.   The
254  * buffers must be made inaccessible to HW before calling this function.
255  */
256 static void free_rx_bufs(struct sge_fl *q, int n)
257 {
258 	unsigned int cidx = q->cidx;
259 	struct rx_sw_desc *d;
260 
261 	d = &q->sdesc[cidx];
262 	while (n--) {
263 		if (d->buf) {
264 			rte_pktmbuf_free(d->buf);
265 			d->buf = NULL;
266 		}
267 		++d;
268 		if (++cidx == q->size) {
269 			cidx = 0;
270 			d = q->sdesc;
271 		}
272 		q->avail--;
273 	}
274 	q->cidx = cidx;
275 }
276 
277 /**
278  * unmap_rx_buf - unmap the current Rx buffer on an SGE free list
279  * @q: the SGE free list
280  *
281  * Unmap the current buffer on an SGE free-buffer Rx queue.   The
282  * buffer must be made inaccessible to HW before calling this function.
283  *
284  * This is similar to @free_rx_bufs above but does not free the buffer.
285  * Do note that the FL still loses any further access to the buffer.
286  */
287 static void unmap_rx_buf(struct sge_fl *q)
288 {
289 	if (++q->cidx == q->size)
290 		q->cidx = 0;
291 	q->avail--;
292 }
293 
294 static inline void ring_fl_db(struct adapter *adap, struct sge_fl *q)
295 {
296 	if (q->pend_cred >= 64) {
297 		u32 val = adap->params.arch.sge_fl_db;
298 
299 		if (is_t4(adap->params.chip))
300 			val |= V_PIDX(q->pend_cred / 8);
301 		else
302 			val |= V_PIDX_T5(q->pend_cred / 8);
303 
304 		/*
305 		 * Make sure all memory writes to the Free List queue are
306 		 * committed before we tell the hardware about them.
307 		 */
308 		wmb();
309 
310 		/*
311 		 * If we don't have access to the new User Doorbell (T5+), use
312 		 * the old doorbell mechanism; otherwise use the new BAR2
313 		 * mechanism.
314 		 */
315 		if (unlikely(!q->bar2_addr)) {
316 			u32 reg = is_pf4(adap) ? MYPF_REG(A_SGE_PF_KDOORBELL) :
317 						 T4VF_SGE_BASE_ADDR +
318 						 A_SGE_VF_KDOORBELL;
319 
320 			t4_write_reg_relaxed(adap, reg,
321 					     val | V_QID(q->cntxt_id));
322 		} else {
323 			writel_relaxed(val | V_QID(q->bar2_qid),
324 				       (void *)((uintptr_t)q->bar2_addr +
325 				       SGE_UDB_KDOORBELL));
326 
327 			/*
328 			 * This Write memory Barrier will force the write to
329 			 * the User Doorbell area to be flushed.
330 			 */
331 			wmb();
332 		}
333 		q->pend_cred &= 7;
334 	}
335 }
336 
337 static inline void set_rx_sw_desc(struct rx_sw_desc *sd, void *buf,
338 				  dma_addr_t mapping)
339 {
340 	sd->buf = buf;
341 	sd->dma_addr = mapping;      /* includes size low bits */
342 }
343 
344 /**
345  * refill_fl_usembufs - refill an SGE Rx buffer ring with mbufs
346  * @adap: the adapter
347  * @q: the ring to refill
348  * @n: the number of new buffers to allocate
349  *
350  * (Re)populate an SGE free-buffer queue with up to @n new packet buffers,
351  * allocated with the supplied gfp flags.  The caller must assure that
352  * @n does not exceed the queue's capacity.  If afterwards the queue is
353  * found critically low mark it as starving in the bitmap of starving FLs.
354  *
355  * Returns the number of buffers allocated.
356  */
357 static unsigned int refill_fl_usembufs(struct adapter *adap, struct sge_fl *q,
358 				       int n)
359 {
360 	struct sge_eth_rxq *rxq = container_of(q, struct sge_eth_rxq, fl);
361 	unsigned int cred = q->avail;
362 	__be64 *d = &q->desc[q->pidx];
363 	struct rx_sw_desc *sd = &q->sdesc[q->pidx];
364 	unsigned int buf_size_idx = RX_SMALL_MTU_BUF;
365 	struct rte_mbuf *buf_bulk[n];
366 	int ret, i;
367 	struct rte_pktmbuf_pool_private *mbp_priv;
368 
369 	/* Use jumbo mtu buffers if mbuf data room size can fit jumbo data. */
370 	mbp_priv = rte_mempool_get_priv(rxq->rspq.mb_pool);
371 	if ((mbp_priv->mbuf_data_room_size - RTE_PKTMBUF_HEADROOM) >= 9000)
372 		buf_size_idx = RX_LARGE_MTU_BUF;
373 
374 	ret = rte_mempool_get_bulk(rxq->rspq.mb_pool, (void *)buf_bulk, n);
375 	if (unlikely(ret != 0)) {
376 		dev_debug(adap, "%s: failed to allocated fl entries in bulk ..\n",
377 			  __func__);
378 		q->alloc_failed++;
379 		rxq->rspq.eth_dev->data->rx_mbuf_alloc_failed++;
380 		goto out;
381 	}
382 
383 	for (i = 0; i < n; i++) {
384 		struct rte_mbuf *mbuf = buf_bulk[i];
385 		dma_addr_t mapping;
386 
387 		if (!mbuf) {
388 			dev_debug(adap, "%s: mbuf alloc failed\n", __func__);
389 			q->alloc_failed++;
390 			rxq->rspq.eth_dev->data->rx_mbuf_alloc_failed++;
391 			goto out;
392 		}
393 
394 		rte_mbuf_refcnt_set(mbuf, 1);
395 		mbuf->data_off =
396 			(uint16_t)((char *)
397 				   RTE_PTR_ALIGN((char *)mbuf->buf_addr +
398 						 RTE_PKTMBUF_HEADROOM,
399 						 adap->sge.fl_align) -
400 				   (char *)mbuf->buf_addr);
401 		mbuf->next = NULL;
402 		mbuf->nb_segs = 1;
403 		mbuf->port = rxq->rspq.port_id;
404 
405 		mapping = (dma_addr_t)RTE_ALIGN(mbuf->buf_iova +
406 						mbuf->data_off,
407 						adap->sge.fl_align);
408 		mapping |= buf_size_idx;
409 		*d++ = cpu_to_be64(mapping);
410 		set_rx_sw_desc(sd, mbuf, mapping);
411 		sd++;
412 
413 		q->avail++;
414 		if (++q->pidx == q->size) {
415 			q->pidx = 0;
416 			sd = q->sdesc;
417 			d = q->desc;
418 		}
419 	}
420 
421 out:    cred = q->avail - cred;
422 	q->pend_cred += cred;
423 	ring_fl_db(adap, q);
424 
425 	if (unlikely(fl_starving(adap, q))) {
426 		/*
427 		 * Make sure data has been written to free list
428 		 */
429 		wmb();
430 		q->low++;
431 	}
432 
433 	return cred;
434 }
435 
436 /**
437  * refill_fl - refill an SGE Rx buffer ring with mbufs
438  * @adap: the adapter
439  * @q: the ring to refill
440  * @n: the number of new buffers to allocate
441  *
442  * (Re)populate an SGE free-buffer queue with up to @n new packet buffers,
443  * allocated with the supplied gfp flags.  The caller must assure that
444  * @n does not exceed the queue's capacity.  Returns the number of buffers
445  * allocated.
446  */
447 static unsigned int refill_fl(struct adapter *adap, struct sge_fl *q, int n)
448 {
449 	return refill_fl_usembufs(adap, q, n);
450 }
451 
452 static inline void __refill_fl(struct adapter *adap, struct sge_fl *fl)
453 {
454 	refill_fl(adap, fl, min(MAX_RX_REFILL, fl_cap(fl) - fl->avail));
455 }
456 
457 /*
458  * Return the number of reclaimable descriptors in a Tx queue.
459  */
460 static inline int reclaimable(const struct sge_txq *q)
461 {
462 	int hw_cidx = ntohs(q->stat->cidx);
463 
464 	hw_cidx -= q->cidx;
465 	if (hw_cidx < 0)
466 		return hw_cidx + q->size;
467 	return hw_cidx;
468 }
469 
470 /**
471  * reclaim_completed_tx - reclaims completed Tx descriptors
472  * @q: the Tx queue to reclaim completed descriptors from
473  *
474  * Reclaims Tx descriptors that the SGE has indicated it has processed.
475  */
476 void reclaim_completed_tx(struct sge_txq *q)
477 {
478 	unsigned int avail = reclaimable(q);
479 
480 	do {
481 		/* reclaim as much as possible */
482 		reclaim_tx_desc(q, avail);
483 		q->in_use -= avail;
484 		avail = reclaimable(q);
485 	} while (avail);
486 }
487 
488 /**
489  * sgl_len - calculates the size of an SGL of the given capacity
490  * @n: the number of SGL entries
491  *
492  * Calculates the number of flits needed for a scatter/gather list that
493  * can hold the given number of entries.
494  */
495 static inline unsigned int sgl_len(unsigned int n)
496 {
497 	/*
498 	 * A Direct Scatter Gather List uses 32-bit lengths and 64-bit PCI DMA
499 	 * addresses.  The DSGL Work Request starts off with a 32-bit DSGL
500 	 * ULPTX header, then Length0, then Address0, then, for 1 <= i <= N,
501 	 * repeated sequences of { Length[i], Length[i+1], Address[i],
502 	 * Address[i+1] } (this ensures that all addresses are on 64-bit
503 	 * boundaries).  If N is even, then Length[N+1] should be set to 0 and
504 	 * Address[N+1] is omitted.
505 	 *
506 	 * The following calculation incorporates all of the above.  It's
507 	 * somewhat hard to follow but, briefly: the "+2" accounts for the
508 	 * first two flits which include the DSGL header, Length0 and
509 	 * Address0; the "(3*(n-1))/2" covers the main body of list entries (3
510 	 * flits for every pair of the remaining N) +1 if (n-1) is odd; and
511 	 * finally the "+((n-1)&1)" adds the one remaining flit needed if
512 	 * (n-1) is odd ...
513 	 */
514 	n--;
515 	return (3 * n) / 2 + (n & 1) + 2;
516 }
517 
518 /**
519  * flits_to_desc - returns the num of Tx descriptors for the given flits
520  * @n: the number of flits
521  *
522  * Returns the number of Tx descriptors needed for the supplied number
523  * of flits.
524  */
525 static inline unsigned int flits_to_desc(unsigned int n)
526 {
527 	return DIV_ROUND_UP(n, 8);
528 }
529 
530 /**
531  * is_eth_imm - can an Ethernet packet be sent as immediate data?
532  * @m: the packet
533  *
534  * Returns whether an Ethernet packet is small enough to fit as
535  * immediate data. Return value corresponds to the headroom required.
536  */
537 static inline int is_eth_imm(const struct rte_mbuf *m)
538 {
539 	unsigned int hdrlen = (m->ol_flags & RTE_MBUF_F_TX_TCP_SEG) ?
540 			      sizeof(struct cpl_tx_pkt_lso_core) : 0;
541 
542 	hdrlen += sizeof(struct cpl_tx_pkt);
543 	if (m->pkt_len <= MAX_IMM_TX_PKT_LEN - hdrlen)
544 		return hdrlen;
545 
546 	return 0;
547 }
548 
549 /**
550  * calc_tx_flits - calculate the number of flits for a packet Tx WR
551  * @m: the packet
552  * @adap: adapter structure pointer
553  *
554  * Returns the number of flits needed for a Tx WR for the given Ethernet
555  * packet, including the needed WR and CPL headers.
556  */
557 static inline unsigned int calc_tx_flits(const struct rte_mbuf *m,
558 					 struct adapter *adap)
559 {
560 	size_t wr_size = is_pf4(adap) ? sizeof(struct fw_eth_tx_pkt_wr) :
561 					sizeof(struct fw_eth_tx_pkt_vm_wr);
562 	unsigned int flits;
563 	int hdrlen;
564 
565 	/*
566 	 * If the mbuf is small enough, we can pump it out as a work request
567 	 * with only immediate data.  In that case we just have to have the
568 	 * TX Packet header plus the mbuf data in the Work Request.
569 	 */
570 
571 	hdrlen = is_eth_imm(m);
572 	if (hdrlen)
573 		return DIV_ROUND_UP(m->pkt_len + hdrlen, sizeof(__be64));
574 
575 	/*
576 	 * Otherwise, we're going to have to construct a Scatter gather list
577 	 * of the mbuf body and fragments.  We also include the flits necessary
578 	 * for the TX Packet Work Request and CPL.  We always have a firmware
579 	 * Write Header (incorporated as part of the cpl_tx_pkt_lso and
580 	 * cpl_tx_pkt structures), followed by either a TX Packet Write CPL
581 	 * message or, if we're doing a Large Send Offload, an LSO CPL message
582 	 * with an embedded TX Packet Write CPL message.
583 	 */
584 	flits = sgl_len(m->nb_segs);
585 	if (m->tso_segsz)
586 		flits += (wr_size + sizeof(struct cpl_tx_pkt_lso_core) +
587 			  sizeof(struct cpl_tx_pkt_core)) / sizeof(__be64);
588 	else
589 		flits += (wr_size +
590 			  sizeof(struct cpl_tx_pkt_core)) / sizeof(__be64);
591 	return flits;
592 }
593 
594 /**
595  * write_sgl - populate a scatter/gather list for a packet
596  * @mbuf: the packet
597  * @q: the Tx queue we are writing into
598  * @sgl: starting location for writing the SGL
599  * @end: points right after the end of the SGL
600  * @start: start offset into mbuf main-body data to include in the SGL
601  * @addr: address of mapped region
602  *
603  * Generates a scatter/gather list for the buffers that make up a packet.
604  * The caller must provide adequate space for the SGL that will be written.
605  * The SGL includes all of the packet's page fragments and the data in its
606  * main body except for the first @start bytes.  @sgl must be 16-byte
607  * aligned and within a Tx descriptor with available space.  @end points
608  * write after the end of the SGL but does not account for any potential
609  * wrap around, i.e., @end > @sgl.
610  */
611 static void write_sgl(struct rte_mbuf *mbuf, struct sge_txq *q,
612 		      struct ulptx_sgl *sgl, u64 *end, unsigned int start,
613 		      const dma_addr_t *addr)
614 {
615 	unsigned int i, len;
616 	struct ulptx_sge_pair *to;
617 	struct rte_mbuf *m = mbuf;
618 	unsigned int nfrags = m->nb_segs;
619 	struct ulptx_sge_pair buf[nfrags / 2];
620 
621 	len = m->data_len - start;
622 	sgl->len0 = htonl(len);
623 	sgl->addr0 = rte_cpu_to_be_64(addr[0]);
624 
625 	sgl->cmd_nsge = htonl(V_ULPTX_CMD(ULP_TX_SC_DSGL) |
626 			      V_ULPTX_NSGE(nfrags));
627 	if (likely(--nfrags == 0))
628 		return;
629 	/*
630 	 * Most of the complexity below deals with the possibility we hit the
631 	 * end of the queue in the middle of writing the SGL.  For this case
632 	 * only we create the SGL in a temporary buffer and then copy it.
633 	 */
634 	to = (u8 *)end > (u8 *)q->stat ? buf : sgl->sge;
635 
636 	for (i = 0; nfrags >= 2; nfrags -= 2, to++) {
637 		m = m->next;
638 		to->len[0] = rte_cpu_to_be_32(m->data_len);
639 		to->addr[0] = rte_cpu_to_be_64(addr[++i]);
640 		m = m->next;
641 		to->len[1] = rte_cpu_to_be_32(m->data_len);
642 		to->addr[1] = rte_cpu_to_be_64(addr[++i]);
643 	}
644 	if (nfrags) {
645 		m = m->next;
646 		to->len[0] = rte_cpu_to_be_32(m->data_len);
647 		to->len[1] = rte_cpu_to_be_32(0);
648 		to->addr[0] = rte_cpu_to_be_64(addr[i + 1]);
649 	}
650 	if (unlikely((u8 *)end > (u8 *)q->stat)) {
651 		unsigned int part0 = RTE_PTR_DIFF((u8 *)q->stat,
652 						  (u8 *)sgl->sge);
653 		unsigned int part1;
654 
655 		if (likely(part0))
656 			memcpy(sgl->sge, buf, part0);
657 		part1 = RTE_PTR_DIFF((u8 *)end, (u8 *)q->stat);
658 		rte_memcpy(q->desc, RTE_PTR_ADD((u8 *)buf, part0), part1);
659 		end = RTE_PTR_ADD((void *)q->desc, part1);
660 	}
661 	if ((uintptr_t)end & 8)           /* 0-pad to multiple of 16 */
662 		*(u64 *)end = 0;
663 }
664 
665 #define IDXDIFF(head, tail, wrap) \
666 	((head) >= (tail) ? (head) - (tail) : (wrap) - (tail) + (head))
667 
668 #define Q_IDXDIFF(q, idx) IDXDIFF((q)->pidx, (q)->idx, (q)->size)
669 #define R_IDXDIFF(q, idx) IDXDIFF((q)->cidx, (q)->idx, (q)->size)
670 
671 #define PIDXDIFF(head, tail, wrap) \
672 	((tail) >= (head) ? (tail) - (head) : (wrap) - (head) + (tail))
673 #define P_IDXDIFF(q, idx) PIDXDIFF((q)->cidx, idx, (q)->size)
674 
675 /**
676  * ring_tx_db - ring a Tx queue's doorbell
677  * @adap: the adapter
678  * @q: the Tx queue
679  * @n: number of new descriptors to give to HW
680  *
681  * Ring the doorbell for a Tx queue.
682  */
683 static inline void ring_tx_db(struct adapter *adap, struct sge_txq *q)
684 {
685 	int n = Q_IDXDIFF(q, dbidx);
686 
687 	/*
688 	 * Make sure that all writes to the TX Descriptors are committed
689 	 * before we tell the hardware about them.
690 	 */
691 	rte_wmb();
692 
693 	/*
694 	 * If we don't have access to the new User Doorbell (T5+), use the old
695 	 * doorbell mechanism; otherwise use the new BAR2 mechanism.
696 	 */
697 	if (unlikely(!q->bar2_addr)) {
698 		u32 val = V_PIDX(n);
699 
700 		/*
701 		 * For T4 we need to participate in the Doorbell Recovery
702 		 * mechanism.
703 		 */
704 		if (!q->db_disabled)
705 			t4_write_reg(adap, MYPF_REG(A_SGE_PF_KDOORBELL),
706 				     V_QID(q->cntxt_id) | val);
707 		else
708 			q->db_pidx_inc += n;
709 		q->db_pidx = q->pidx;
710 	} else {
711 		u32 val = V_PIDX_T5(n);
712 
713 		/*
714 		 * T4 and later chips share the same PIDX field offset within
715 		 * the doorbell, but T5 and later shrank the field in order to
716 		 * gain a bit for Doorbell Priority.  The field was absurdly
717 		 * large in the first place (14 bits) so we just use the T5
718 		 * and later limits and warn if a Queue ID is too large.
719 		 */
720 		WARN_ON(val & F_DBPRIO);
721 
722 		writel(val | V_QID(q->bar2_qid),
723 		       (void *)((uintptr_t)q->bar2_addr + SGE_UDB_KDOORBELL));
724 
725 		/*
726 		 * This Write Memory Barrier will force the write to the User
727 		 * Doorbell area to be flushed.  This is needed to prevent
728 		 * writes on different CPUs for the same queue from hitting
729 		 * the adapter out of order.  This is required when some Work
730 		 * Requests take the Write Combine Gather Buffer path (user
731 		 * doorbell area offset [SGE_UDB_WCDOORBELL..+63]) and some
732 		 * take the traditional path where we simply increment the
733 		 * PIDX (User Doorbell area SGE_UDB_KDOORBELL) and have the
734 		 * hardware DMA read the actual Work Request.
735 		 */
736 		rte_wmb();
737 	}
738 	q->dbidx = q->pidx;
739 }
740 
741 /*
742  * Figure out what HW csum a packet wants and return the appropriate control
743  * bits.
744  */
745 static u64 hwcsum(enum chip_type chip, const struct rte_mbuf *m)
746 {
747 	int csum_type;
748 
749 	if (m->ol_flags & RTE_MBUF_F_TX_IP_CKSUM) {
750 		switch (m->ol_flags & RTE_MBUF_F_TX_L4_MASK) {
751 		case RTE_MBUF_F_TX_TCP_CKSUM:
752 			csum_type = TX_CSUM_TCPIP;
753 			break;
754 		case RTE_MBUF_F_TX_UDP_CKSUM:
755 			csum_type = TX_CSUM_UDPIP;
756 			break;
757 		default:
758 			goto nocsum;
759 		}
760 	} else {
761 		goto nocsum;
762 	}
763 
764 	if (likely(csum_type >= TX_CSUM_TCPIP)) {
765 		u64 hdr_len = V_TXPKT_IPHDR_LEN(m->l3_len);
766 		int eth_hdr_len = m->l2_len;
767 
768 		if (CHELSIO_CHIP_VERSION(chip) <= CHELSIO_T5)
769 			hdr_len |= V_TXPKT_ETHHDR_LEN(eth_hdr_len);
770 		else
771 			hdr_len |= V_T6_TXPKT_ETHHDR_LEN(eth_hdr_len);
772 		return V_TXPKT_CSUM_TYPE(csum_type) | hdr_len;
773 	}
774 nocsum:
775 	/*
776 	 * unknown protocol, disable HW csum
777 	 * and hope a bad packet is detected
778 	 */
779 	return F_TXPKT_L4CSUM_DIS;
780 }
781 
782 static inline void txq_advance(struct sge_txq *q, unsigned int n)
783 {
784 	q->in_use += n;
785 	q->pidx += n;
786 	if (q->pidx >= q->size)
787 		q->pidx -= q->size;
788 }
789 
790 #define MAX_COALESCE_LEN 64000
791 
792 static inline int wraps_around(struct sge_txq *q, int ndesc)
793 {
794 	return (q->pidx + ndesc) > q->size ? 1 : 0;
795 }
796 
797 static void tx_timer_cb(void *data)
798 {
799 	struct adapter *adap = (struct adapter *)data;
800 	struct sge_eth_txq *txq = &adap->sge.ethtxq[0];
801 	int i;
802 	unsigned int coal_idx;
803 
804 	/* monitor any pending tx */
805 	for (i = 0; i < adap->sge.max_ethqsets; i++, txq++) {
806 		if (t4_os_trylock(&txq->txq_lock)) {
807 			coal_idx = txq->q.coalesce.idx;
808 			if (coal_idx) {
809 				if (coal_idx == txq->q.last_coal_idx &&
810 				    txq->q.pidx == txq->q.last_pidx) {
811 					ship_tx_pkt_coalesce_wr(adap, txq);
812 				} else {
813 					txq->q.last_coal_idx = coal_idx;
814 					txq->q.last_pidx = txq->q.pidx;
815 				}
816 			}
817 			t4_os_unlock(&txq->txq_lock);
818 		}
819 	}
820 	rte_eal_alarm_set(50, tx_timer_cb, (void *)adap);
821 }
822 
823 /**
824  * ship_tx_pkt_coalesce_wr - finalizes and ships a coalesce WR
825  * @ adap: adapter structure
826  * @txq: tx queue
827  *
828  * writes the different fields of the pkts WR and sends it.
829  */
830 static inline void ship_tx_pkt_coalesce_wr(struct adapter *adap,
831 					   struct sge_eth_txq *txq)
832 {
833 	struct fw_eth_tx_pkts_vm_wr *vmwr;
834 	const size_t fw_hdr_copy_len = (sizeof(vmwr->ethmacdst) +
835 					sizeof(vmwr->ethmacsrc) +
836 					sizeof(vmwr->ethtype) +
837 					sizeof(vmwr->vlantci));
838 	struct fw_eth_tx_pkts_wr *wr;
839 	struct sge_txq *q = &txq->q;
840 	unsigned int ndesc;
841 	u32 wr_mid;
842 
843 	/* fill the pkts WR header */
844 	wr = (void *)&q->desc[q->pidx];
845 	wr->op_pkd = htonl(V_FW_WR_OP(FW_ETH_TX_PKTS2_WR));
846 	vmwr = (void *)&q->desc[q->pidx];
847 
848 	wr_mid = V_FW_WR_LEN16(DIV_ROUND_UP(q->coalesce.flits, 2));
849 	ndesc = flits_to_desc(q->coalesce.flits);
850 	wr->equiq_to_len16 = htonl(wr_mid);
851 	wr->plen = cpu_to_be16(q->coalesce.len);
852 	wr->npkt = q->coalesce.idx;
853 	wr->r3 = 0;
854 	if (is_pf4(adap)) {
855 		wr->op_pkd = htonl(V_FW_WR_OP(FW_ETH_TX_PKTS2_WR));
856 		wr->type = q->coalesce.type;
857 	} else {
858 		wr->op_pkd = htonl(V_FW_WR_OP(FW_ETH_TX_PKTS_VM_WR));
859 		vmwr->r4 = 0;
860 		memcpy((void *)vmwr->ethmacdst, (void *)q->coalesce.ethmacdst,
861 		       fw_hdr_copy_len);
862 	}
863 
864 	/* zero out coalesce structure members */
865 	memset((void *)&q->coalesce, 0, sizeof(struct eth_coalesce));
866 
867 	txq_advance(q, ndesc);
868 	txq->stats.coal_wr++;
869 	txq->stats.coal_pkts += wr->npkt;
870 
871 	if (Q_IDXDIFF(q, equeidx) >= q->size / 2) {
872 		q->equeidx = q->pidx;
873 		wr_mid |= F_FW_WR_EQUEQ;
874 		wr->equiq_to_len16 = htonl(wr_mid);
875 	}
876 	ring_tx_db(adap, q);
877 }
878 
879 /**
880  * should_tx_packet_coalesce - decides whether to coalesce an mbuf or not
881  * @txq: tx queue where the mbuf is sent
882  * @mbuf: mbuf to be sent
883  * @nflits: return value for number of flits needed
884  * @adap: adapter structure
885  *
886  * This function decides if a packet should be coalesced or not.
887  */
888 static inline int should_tx_packet_coalesce(struct sge_eth_txq *txq,
889 					    struct rte_mbuf *mbuf,
890 					    unsigned int *nflits,
891 					    struct adapter *adap)
892 {
893 	struct fw_eth_tx_pkts_vm_wr *wr;
894 	const size_t fw_hdr_copy_len = (sizeof(wr->ethmacdst) +
895 					sizeof(wr->ethmacsrc) +
896 					sizeof(wr->ethtype) +
897 					sizeof(wr->vlantci));
898 	struct sge_txq *q = &txq->q;
899 	unsigned int flits, ndesc;
900 	unsigned char type = 0;
901 	int credits, wr_size;
902 
903 	/* use coal WR type 1 when no frags are present */
904 	type = (mbuf->nb_segs == 1) ? 1 : 0;
905 	if (!is_pf4(adap)) {
906 		if (!type)
907 			return 0;
908 
909 		if (q->coalesce.idx && memcmp((void *)q->coalesce.ethmacdst,
910 					      rte_pktmbuf_mtod(mbuf, void *),
911 					      fw_hdr_copy_len))
912 			ship_tx_pkt_coalesce_wr(adap, txq);
913 	}
914 
915 	if (unlikely(type != q->coalesce.type && q->coalesce.idx))
916 		ship_tx_pkt_coalesce_wr(adap, txq);
917 
918 	/* calculate the number of flits required for coalescing this packet
919 	 * without the 2 flits of the WR header. These are added further down
920 	 * if we are just starting in new PKTS WR. sgl_len doesn't account for
921 	 * the possible 16 bytes alignment ULP TX commands so we do it here.
922 	 */
923 	flits = (sgl_len(mbuf->nb_segs) + 1) & ~1U;
924 	if (type == 0)
925 		flits += (sizeof(struct ulp_txpkt) +
926 			  sizeof(struct ulptx_idata)) / sizeof(__be64);
927 	flits += sizeof(struct cpl_tx_pkt_core) / sizeof(__be64);
928 	*nflits = flits;
929 
930 	/* If coalescing is on, the mbuf is added to a pkts WR */
931 	if (q->coalesce.idx) {
932 		ndesc = DIV_ROUND_UP(q->coalesce.flits + flits, 8);
933 		credits = txq_avail(q) - ndesc;
934 
935 		/* If we are wrapping or this is last mbuf then, send the
936 		 * already coalesced mbufs and let the non-coalesce pass
937 		 * handle the mbuf.
938 		 */
939 		if (unlikely(credits < 0 || wraps_around(q, ndesc))) {
940 			ship_tx_pkt_coalesce_wr(adap, txq);
941 			return 0;
942 		}
943 
944 		/* If the max coalesce len or the max WR len is reached
945 		 * ship the WR and keep coalescing on.
946 		 */
947 		if (unlikely((q->coalesce.len + mbuf->pkt_len >
948 						MAX_COALESCE_LEN) ||
949 			     (q->coalesce.flits + flits >
950 			      q->coalesce.max))) {
951 			ship_tx_pkt_coalesce_wr(adap, txq);
952 			goto new;
953 		}
954 		return 1;
955 	}
956 
957 new:
958 	/* start a new pkts WR, the WR header is not filled below */
959 	wr_size = is_pf4(adap) ? sizeof(struct fw_eth_tx_pkts_wr) :
960 				 sizeof(struct fw_eth_tx_pkts_vm_wr);
961 	flits += wr_size / sizeof(__be64);
962 	ndesc = flits_to_desc(q->coalesce.flits + flits);
963 	credits = txq_avail(q) - ndesc;
964 
965 	if (unlikely(credits < 0 || wraps_around(q, ndesc)))
966 		return 0;
967 	q->coalesce.flits += wr_size / sizeof(__be64);
968 	q->coalesce.type = type;
969 	q->coalesce.ptr = (unsigned char *)&q->desc[q->pidx] +
970 			   q->coalesce.flits * sizeof(__be64);
971 	if (!is_pf4(adap))
972 		memcpy((void *)q->coalesce.ethmacdst,
973 		       rte_pktmbuf_mtod(mbuf, void *), fw_hdr_copy_len);
974 	return 1;
975 }
976 
977 /**
978  * tx_do_packet_coalesce - add an mbuf to a coalesce WR
979  * @txq: sge_eth_txq used send the mbuf
980  * @mbuf: mbuf to be sent
981  * @flits: flits needed for this mbuf
982  * @adap: adapter structure
983  * @pi: port_info structure
984  * @addr: mapped address of the mbuf
985  *
986  * Adds an mbuf to be sent as part of a coalesce WR by filling a
987  * ulp_tx_pkt command, ulp_tx_sc_imm command, cpl message and
988  * ulp_tx_sc_dsgl command.
989  */
990 static inline int tx_do_packet_coalesce(struct sge_eth_txq *txq,
991 					struct rte_mbuf *mbuf,
992 					int flits, struct adapter *adap,
993 					const struct port_info *pi,
994 					dma_addr_t *addr, uint16_t nb_pkts)
995 {
996 	u64 cntrl, *end;
997 	struct sge_txq *q = &txq->q;
998 	struct ulp_txpkt *mc;
999 	struct ulptx_idata *sc_imm;
1000 	struct cpl_tx_pkt_core *cpl;
1001 	struct tx_sw_desc *sd;
1002 	unsigned int idx = q->coalesce.idx, len = mbuf->pkt_len;
1003 
1004 	if (q->coalesce.type == 0) {
1005 		mc = (struct ulp_txpkt *)q->coalesce.ptr;
1006 		mc->cmd_dest = htonl(V_ULPTX_CMD(4) | V_ULP_TXPKT_DEST(0) |
1007 				     V_ULP_TXPKT_FID(adap->sge.fw_evtq.cntxt_id) |
1008 				     F_ULP_TXPKT_RO);
1009 		mc->len = htonl(DIV_ROUND_UP(flits, 2));
1010 		sc_imm = (struct ulptx_idata *)(mc + 1);
1011 		sc_imm->cmd_more = htonl(V_ULPTX_CMD(ULP_TX_SC_IMM) |
1012 					 F_ULP_TX_SC_MORE);
1013 		sc_imm->len = htonl(sizeof(*cpl));
1014 		end = (u64 *)mc + flits;
1015 		cpl = (struct cpl_tx_pkt_core *)(sc_imm + 1);
1016 	} else {
1017 		end = (u64 *)q->coalesce.ptr + flits;
1018 		cpl = (struct cpl_tx_pkt_core *)q->coalesce.ptr;
1019 	}
1020 
1021 	/* update coalesce structure for this txq */
1022 	q->coalesce.flits += flits;
1023 	q->coalesce.ptr += flits * sizeof(__be64);
1024 	q->coalesce.len += mbuf->pkt_len;
1025 
1026 	/* fill the cpl message, same as in t4_eth_xmit, this should be kept
1027 	 * similar to t4_eth_xmit
1028 	 */
1029 	if (mbuf->ol_flags & RTE_MBUF_F_TX_IP_CKSUM) {
1030 		cntrl = hwcsum(adap->params.chip, mbuf) |
1031 			       F_TXPKT_IPCSUM_DIS;
1032 		txq->stats.tx_cso++;
1033 	} else {
1034 		cntrl = F_TXPKT_L4CSUM_DIS | F_TXPKT_IPCSUM_DIS;
1035 	}
1036 
1037 	if (mbuf->ol_flags & RTE_MBUF_F_TX_VLAN) {
1038 		txq->stats.vlan_ins++;
1039 		cntrl |= F_TXPKT_VLAN_VLD | V_TXPKT_VLAN(mbuf->vlan_tci);
1040 	}
1041 
1042 	cpl->ctrl0 = htonl(V_TXPKT_OPCODE(CPL_TX_PKT_XT));
1043 	if (is_pf4(adap))
1044 		cpl->ctrl0 |= htonl(V_TXPKT_INTF(pi->tx_chan) |
1045 				    V_TXPKT_PF(adap->pf));
1046 	else
1047 		cpl->ctrl0 |= htonl(V_TXPKT_INTF(pi->port_id));
1048 	cpl->pack = htons(0);
1049 	cpl->len = htons(len);
1050 	cpl->ctrl1 = cpu_to_be64(cntrl);
1051 	write_sgl(mbuf, q, (struct ulptx_sgl *)(cpl + 1), end, 0,  addr);
1052 	txq->stats.pkts++;
1053 	txq->stats.tx_bytes += len;
1054 
1055 	sd = &q->sdesc[q->pidx + (idx >> 1)];
1056 	if (!(idx & 1)) {
1057 		if (sd->coalesce.idx) {
1058 			int i;
1059 
1060 			for (i = 0; i < sd->coalesce.idx; i++) {
1061 				rte_pktmbuf_free(sd->coalesce.mbuf[i]);
1062 				sd->coalesce.mbuf[i] = NULL;
1063 			}
1064 		}
1065 	}
1066 
1067 	/* store pointers to the mbuf and the sgl used in free_tx_desc.
1068 	 * each tx desc can hold two pointers corresponding to the value
1069 	 * of ETH_COALESCE_PKT_PER_DESC
1070 	 */
1071 	sd->coalesce.mbuf[idx & 1] = mbuf;
1072 	sd->coalesce.sgl[idx & 1] = (struct ulptx_sgl *)(cpl + 1);
1073 	sd->coalesce.idx = (idx & 1) + 1;
1074 
1075 	/* Send the coalesced work request, only if max reached. However,
1076 	 * if lower latency is preferred over throughput, then don't wait
1077 	 * for coalescing the next Tx burst and send the packets now.
1078 	 */
1079 	q->coalesce.idx++;
1080 	if (q->coalesce.idx == adap->params.max_tx_coalesce_num ||
1081 	    (adap->devargs.tx_mode_latency && q->coalesce.idx >= nb_pkts))
1082 		ship_tx_pkt_coalesce_wr(adap, txq);
1083 
1084 	return 0;
1085 }
1086 
1087 /**
1088  * t4_eth_xmit - add a packet to an Ethernet Tx queue
1089  * @txq: the egress queue
1090  * @mbuf: the packet
1091  *
1092  * Add a packet to an SGE Ethernet Tx queue.  Runs with softirqs disabled.
1093  */
1094 int t4_eth_xmit(struct sge_eth_txq *txq, struct rte_mbuf *mbuf,
1095 		uint16_t nb_pkts)
1096 {
1097 	const struct port_info *pi;
1098 	struct cpl_tx_pkt_lso_core *lso;
1099 	struct adapter *adap;
1100 	struct rte_mbuf *m = mbuf;
1101 	struct fw_eth_tx_pkt_wr *wr;
1102 	struct fw_eth_tx_pkt_vm_wr *vmwr;
1103 	struct cpl_tx_pkt_core *cpl;
1104 	struct tx_sw_desc *d;
1105 	dma_addr_t addr[m->nb_segs];
1106 	unsigned int flits, ndesc, cflits;
1107 	int l3hdr_len, l4hdr_len, eth_xtra_len;
1108 	int len, last_desc;
1109 	int credits;
1110 	u32 wr_mid;
1111 	u64 cntrl, *end;
1112 	bool v6;
1113 	u32 max_pkt_len;
1114 
1115 	/* Reject xmit if queue is stopped */
1116 	if (unlikely(txq->flags & EQ_STOPPED))
1117 		return -(EBUSY);
1118 
1119 	/*
1120 	 * The chip min packet length is 10 octets but play safe and reject
1121 	 * anything shorter than an Ethernet header.
1122 	 */
1123 	if (unlikely(m->pkt_len < RTE_ETHER_HDR_LEN)) {
1124 out_free:
1125 		rte_pktmbuf_free(m);
1126 		return 0;
1127 	}
1128 
1129 	max_pkt_len = txq->data->mtu + RTE_ETHER_HDR_LEN + RTE_ETHER_CRC_LEN;
1130 	if ((!(m->ol_flags & RTE_MBUF_F_TX_TCP_SEG)) &&
1131 	    (unlikely(m->pkt_len > max_pkt_len)))
1132 		goto out_free;
1133 
1134 	pi = txq->data->dev_private;
1135 	adap = pi->adapter;
1136 
1137 	cntrl = F_TXPKT_L4CSUM_DIS | F_TXPKT_IPCSUM_DIS;
1138 	/* align the end of coalesce WR to a 512 byte boundary */
1139 	txq->q.coalesce.max = (8 - (txq->q.pidx & 7)) * 8;
1140 
1141 	if (!((m->ol_flags & RTE_MBUF_F_TX_TCP_SEG) ||
1142 			m->pkt_len > RTE_ETHER_MAX_LEN)) {
1143 		if (should_tx_packet_coalesce(txq, mbuf, &cflits, adap)) {
1144 			if (unlikely(map_mbuf(mbuf, addr) < 0)) {
1145 				dev_warn(adap, "%s: mapping err for coalesce\n",
1146 					 __func__);
1147 				txq->stats.mapping_err++;
1148 				goto out_free;
1149 			}
1150 			return tx_do_packet_coalesce(txq, mbuf, cflits, adap,
1151 						     pi, addr, nb_pkts);
1152 		} else {
1153 			return -EBUSY;
1154 		}
1155 	}
1156 
1157 	if (txq->q.coalesce.idx)
1158 		ship_tx_pkt_coalesce_wr(adap, txq);
1159 
1160 	flits = calc_tx_flits(m, adap);
1161 	ndesc = flits_to_desc(flits);
1162 	credits = txq_avail(&txq->q) - ndesc;
1163 
1164 	if (unlikely(credits < 0)) {
1165 		dev_debug(adap, "%s: Tx ring %u full; credits = %d\n",
1166 			  __func__, txq->q.cntxt_id, credits);
1167 		return -EBUSY;
1168 	}
1169 
1170 	if (unlikely(map_mbuf(m, addr) < 0)) {
1171 		txq->stats.mapping_err++;
1172 		goto out_free;
1173 	}
1174 
1175 	wr_mid = V_FW_WR_LEN16(DIV_ROUND_UP(flits, 2));
1176 	if (Q_IDXDIFF(&txq->q, equeidx)  >= 64) {
1177 		txq->q.equeidx = txq->q.pidx;
1178 		wr_mid |= F_FW_WR_EQUEQ;
1179 	}
1180 
1181 	wr = (void *)&txq->q.desc[txq->q.pidx];
1182 	vmwr = (void *)&txq->q.desc[txq->q.pidx];
1183 	wr->equiq_to_len16 = htonl(wr_mid);
1184 	if (is_pf4(adap)) {
1185 		wr->r3 = rte_cpu_to_be_64(0);
1186 		end = (u64 *)wr + flits;
1187 	} else {
1188 		const size_t fw_hdr_copy_len = (sizeof(vmwr->ethmacdst) +
1189 						sizeof(vmwr->ethmacsrc) +
1190 						sizeof(vmwr->ethtype) +
1191 						sizeof(vmwr->vlantci));
1192 
1193 		vmwr->r3[0] = rte_cpu_to_be_32(0);
1194 		vmwr->r3[1] = rte_cpu_to_be_32(0);
1195 		memcpy((void *)vmwr->ethmacdst, rte_pktmbuf_mtod(m, void *),
1196 		       fw_hdr_copy_len);
1197 		end = (u64 *)vmwr + flits;
1198 	}
1199 
1200 	len = 0;
1201 	len += sizeof(*cpl);
1202 
1203 	/* Coalescing skipped and we send through normal path */
1204 	if (!(m->ol_flags & RTE_MBUF_F_TX_TCP_SEG)) {
1205 		wr->op_immdlen = htonl(V_FW_WR_OP(is_pf4(adap) ?
1206 						  FW_ETH_TX_PKT_WR :
1207 						  FW_ETH_TX_PKT_VM_WR) |
1208 				       V_FW_WR_IMMDLEN(len));
1209 		if (is_pf4(adap))
1210 			cpl = (void *)(wr + 1);
1211 		else
1212 			cpl = (void *)(vmwr + 1);
1213 		if (m->ol_flags & RTE_MBUF_F_TX_IP_CKSUM) {
1214 			cntrl = hwcsum(adap->params.chip, m) |
1215 				F_TXPKT_IPCSUM_DIS;
1216 			txq->stats.tx_cso++;
1217 		}
1218 	} else {
1219 		if (is_pf4(adap))
1220 			lso = (void *)(wr + 1);
1221 		else
1222 			lso = (void *)(vmwr + 1);
1223 		v6 = (m->ol_flags & RTE_MBUF_F_TX_IPV6) != 0;
1224 		l3hdr_len = m->l3_len;
1225 		l4hdr_len = m->l4_len;
1226 		eth_xtra_len = m->l2_len - RTE_ETHER_HDR_LEN;
1227 		len += sizeof(*lso);
1228 		wr->op_immdlen = htonl(V_FW_WR_OP(is_pf4(adap) ?
1229 						  FW_ETH_TX_PKT_WR :
1230 						  FW_ETH_TX_PKT_VM_WR) |
1231 				       V_FW_WR_IMMDLEN(len));
1232 		lso->lso_ctrl = htonl(V_LSO_OPCODE(CPL_TX_PKT_LSO) |
1233 				      F_LSO_FIRST_SLICE | F_LSO_LAST_SLICE |
1234 				      V_LSO_IPV6(v6) |
1235 				      V_LSO_ETHHDR_LEN(eth_xtra_len / 4) |
1236 				      V_LSO_IPHDR_LEN(l3hdr_len / 4) |
1237 				      V_LSO_TCPHDR_LEN(l4hdr_len / 4));
1238 		lso->ipid_ofst = htons(0);
1239 		lso->mss = htons(m->tso_segsz);
1240 		lso->seqno_offset = htonl(0);
1241 		if (is_t4(adap->params.chip))
1242 			lso->len = htonl(m->pkt_len);
1243 		else
1244 			lso->len = htonl(V_LSO_T5_XFER_SIZE(m->pkt_len));
1245 		cpl = (void *)(lso + 1);
1246 
1247 		if (CHELSIO_CHIP_VERSION(adap->params.chip) <= CHELSIO_T5)
1248 			cntrl = V_TXPKT_ETHHDR_LEN(eth_xtra_len);
1249 		else
1250 			cntrl = V_T6_TXPKT_ETHHDR_LEN(eth_xtra_len);
1251 
1252 		cntrl |= V_TXPKT_CSUM_TYPE(v6 ? TX_CSUM_TCPIP6 :
1253 						TX_CSUM_TCPIP) |
1254 			 V_TXPKT_IPHDR_LEN(l3hdr_len);
1255 		txq->stats.tso++;
1256 		txq->stats.tx_cso += m->tso_segsz;
1257 	}
1258 
1259 	if (m->ol_flags & RTE_MBUF_F_TX_VLAN) {
1260 		txq->stats.vlan_ins++;
1261 		cntrl |= F_TXPKT_VLAN_VLD | V_TXPKT_VLAN(m->vlan_tci);
1262 	}
1263 
1264 	cpl->ctrl0 = htonl(V_TXPKT_OPCODE(CPL_TX_PKT_XT));
1265 	if (is_pf4(adap))
1266 		cpl->ctrl0 |= htonl(V_TXPKT_INTF(pi->tx_chan) |
1267 				    V_TXPKT_PF(adap->pf));
1268 	else
1269 		cpl->ctrl0 |= htonl(V_TXPKT_INTF(pi->port_id) |
1270 				    V_TXPKT_PF(0));
1271 
1272 	cpl->pack = htons(0);
1273 	cpl->len = htons(m->pkt_len);
1274 	cpl->ctrl1 = cpu_to_be64(cntrl);
1275 
1276 	txq->stats.pkts++;
1277 	txq->stats.tx_bytes += m->pkt_len;
1278 	last_desc = txq->q.pidx + ndesc - 1;
1279 	if (last_desc >= (int)txq->q.size)
1280 		last_desc -= txq->q.size;
1281 
1282 	d = &txq->q.sdesc[last_desc];
1283 	if (d->coalesce.idx) {
1284 		int i;
1285 
1286 		for (i = 0; i < d->coalesce.idx; i++) {
1287 			rte_pktmbuf_free(d->coalesce.mbuf[i]);
1288 			d->coalesce.mbuf[i] = NULL;
1289 		}
1290 		d->coalesce.idx = 0;
1291 	}
1292 	write_sgl(m, &txq->q, (struct ulptx_sgl *)(cpl + 1), end, 0,
1293 		  addr);
1294 	txq->q.sdesc[last_desc].mbuf = m;
1295 	txq->q.sdesc[last_desc].sgl = (struct ulptx_sgl *)(cpl + 1);
1296 	txq_advance(&txq->q, ndesc);
1297 	ring_tx_db(adap, &txq->q);
1298 	return 0;
1299 }
1300 
1301 /**
1302  * reclaim_completed_tx_imm - reclaim completed control-queue Tx descs
1303  * @q: the SGE control Tx queue
1304  *
1305  * This is a variant of reclaim_completed_tx() that is used for Tx queues
1306  * that send only immediate data (presently just the control queues) and
1307  * thus do not have any mbufs to release.
1308  */
1309 static inline void reclaim_completed_tx_imm(struct sge_txq *q)
1310 {
1311 	int hw_cidx = ntohs(q->stat->cidx);
1312 	int reclaim = hw_cidx - q->cidx;
1313 
1314 	if (reclaim < 0)
1315 		reclaim += q->size;
1316 
1317 	q->in_use -= reclaim;
1318 	q->cidx = hw_cidx;
1319 }
1320 
1321 /**
1322  * is_imm - check whether a packet can be sent as immediate data
1323  * @mbuf: the packet
1324  *
1325  * Returns true if a packet can be sent as a WR with immediate data.
1326  */
1327 static inline int is_imm(const struct rte_mbuf *mbuf)
1328 {
1329 	return mbuf->pkt_len <= MAX_CTRL_WR_LEN;
1330 }
1331 
1332 /**
1333  * inline_tx_mbuf: inline a packet's data into TX descriptors
1334  * @q: the TX queue where the packet will be inlined
1335  * @from: pointer to data portion of packet
1336  * @to: pointer after cpl where data has to be inlined
1337  * @len: length of data to inline
1338  *
1339  * Inline a packet's contents directly to TX descriptors, starting at
1340  * the given position within the TX DMA ring.
1341  * Most of the complexity of this operation is dealing with wrap arounds
1342  * in the middle of the packet we want to inline.
1343  */
1344 static void inline_tx_mbuf(const struct sge_txq *q, caddr_t from, caddr_t *to,
1345 			   int len)
1346 {
1347 	int left = RTE_PTR_DIFF(q->stat, *to);
1348 
1349 	if (likely((uintptr_t)*to + len <= (uintptr_t)q->stat)) {
1350 		rte_memcpy(*to, from, len);
1351 		*to = RTE_PTR_ADD(*to, len);
1352 	} else {
1353 		rte_memcpy(*to, from, left);
1354 		from = RTE_PTR_ADD(from, left);
1355 		left = len - left;
1356 		rte_memcpy((void *)q->desc, from, left);
1357 		*to = RTE_PTR_ADD((void *)q->desc, left);
1358 	}
1359 }
1360 
1361 /**
1362  * ctrl_xmit - send a packet through an SGE control Tx queue
1363  * @q: the control queue
1364  * @mbuf: the packet
1365  *
1366  * Send a packet through an SGE control Tx queue.  Packets sent through
1367  * a control queue must fit entirely as immediate data.
1368  */
1369 static int ctrl_xmit(struct sge_ctrl_txq *q, struct rte_mbuf *mbuf)
1370 {
1371 	unsigned int ndesc;
1372 	struct fw_wr_hdr *wr;
1373 	caddr_t dst;
1374 
1375 	if (unlikely(!is_imm(mbuf))) {
1376 		WARN_ON(1);
1377 		rte_pktmbuf_free(mbuf);
1378 		return -1;
1379 	}
1380 
1381 	reclaim_completed_tx_imm(&q->q);
1382 	ndesc = DIV_ROUND_UP(mbuf->pkt_len, sizeof(struct tx_desc));
1383 	t4_os_lock(&q->ctrlq_lock);
1384 
1385 	q->full = txq_avail(&q->q) < ndesc ? 1 : 0;
1386 	if (unlikely(q->full)) {
1387 		t4_os_unlock(&q->ctrlq_lock);
1388 		return -1;
1389 	}
1390 
1391 	wr = (struct fw_wr_hdr *)&q->q.desc[q->q.pidx];
1392 	dst = (void *)wr;
1393 	inline_tx_mbuf(&q->q, rte_pktmbuf_mtod(mbuf, caddr_t),
1394 		       &dst, mbuf->data_len);
1395 
1396 	txq_advance(&q->q, ndesc);
1397 	if (unlikely(txq_avail(&q->q) < 64))
1398 		wr->lo |= htonl(F_FW_WR_EQUEQ);
1399 
1400 	q->txp++;
1401 
1402 	ring_tx_db(q->adapter, &q->q);
1403 	t4_os_unlock(&q->ctrlq_lock);
1404 
1405 	rte_pktmbuf_free(mbuf);
1406 	return 0;
1407 }
1408 
1409 /**
1410  * t4_mgmt_tx - send a management message
1411  * @q: the control queue
1412  * @mbuf: the packet containing the management message
1413  *
1414  * Send a management message through control queue.
1415  */
1416 int t4_mgmt_tx(struct sge_ctrl_txq *q, struct rte_mbuf *mbuf)
1417 {
1418 	return ctrl_xmit(q, mbuf);
1419 }
1420 
1421 /**
1422  * alloc_ring - allocate resources for an SGE descriptor ring
1423  * @dev: the port associated with the queue
1424  * @z_name: memzone's name
1425  * @queue_id: queue index
1426  * @socket_id: preferred socket id for memory allocations
1427  * @nelem: the number of descriptors
1428  * @elem_size: the size of each descriptor
1429  * @stat_size: extra space in HW ring for status information
1430  * @sw_size: the size of the SW state associated with each ring element
1431  * @phys: the physical address of the allocated ring
1432  * @metadata: address of the array holding the SW state for the ring
1433  *
1434  * Allocates resources for an SGE descriptor ring, such as Tx queues,
1435  * free buffer lists, or response queues.  Each SGE ring requires
1436  * space for its HW descriptors plus, optionally, space for the SW state
1437  * associated with each HW entry (the metadata).  The function returns
1438  * three values: the virtual address for the HW ring (the return value
1439  * of the function), the bus address of the HW ring, and the address
1440  * of the SW ring.
1441  */
1442 static void *alloc_ring(struct rte_eth_dev *dev, const char *z_name,
1443 			uint16_t queue_id, int socket_id, size_t nelem,
1444 			size_t elem_size, size_t stat_size, size_t sw_size,
1445 			dma_addr_t *phys, void *metadata)
1446 {
1447 	size_t len = CXGBE_MAX_RING_DESC_SIZE * elem_size + stat_size;
1448 	char z_name_sw[RTE_MEMZONE_NAMESIZE];
1449 	const struct rte_memzone *tz;
1450 	void *s = NULL;
1451 
1452 	snprintf(z_name_sw, sizeof(z_name_sw), "eth_p%d_q%d_%s_sw_ring",
1453 		 dev->data->port_id, queue_id, z_name);
1454 
1455 	dev_debug(adapter, "%s: nelem = %zu; elem_size = %zu; sw_size = %zu; "
1456 		  "stat_size = %zu; queue_id = %u; socket_id = %d; z_name = %s;"
1457 		  " z_name_sw = %s\n", __func__, nelem, elem_size, sw_size,
1458 		  stat_size, queue_id, socket_id, z_name, z_name_sw);
1459 
1460 	/*
1461 	 * Allocate TX/RX ring hardware descriptors. A memzone large enough to
1462 	 * handle the maximum ring size is allocated in order to allow for
1463 	 * resizing in later calls to the queue setup function.
1464 	 */
1465 	tz = rte_eth_dma_zone_reserve(dev, z_name, queue_id, len, 4096,
1466 				      socket_id);
1467 	if (!tz)
1468 		return NULL;
1469 
1470 	memset(tz->addr, 0, len);
1471 	if (sw_size) {
1472 		s = rte_zmalloc_socket(z_name_sw, nelem * sw_size,
1473 				       RTE_CACHE_LINE_SIZE, socket_id);
1474 
1475 		if (!s) {
1476 			dev_err(adapter, "%s: failed to get sw_ring memory\n",
1477 				__func__);
1478 			return NULL;
1479 		}
1480 	}
1481 	if (metadata)
1482 		*(void **)metadata = s;
1483 
1484 	*phys = (uint64_t)tz->iova;
1485 	return tz->addr;
1486 }
1487 
1488 #define CXGB4_MSG_AN ((void *)1)
1489 
1490 /**
1491  * rspq_next - advance to the next entry in a response queue
1492  * @q: the queue
1493  *
1494  * Updates the state of a response queue to advance it to the next entry.
1495  */
1496 static inline void rspq_next(struct sge_rspq *q)
1497 {
1498 	q->cur_desc = (const __be64 *)((const char *)q->cur_desc + q->iqe_len);
1499 	if (unlikely(++q->cidx == q->size)) {
1500 		q->cidx = 0;
1501 		q->gen ^= 1;
1502 		q->cur_desc = q->desc;
1503 	}
1504 }
1505 
1506 static inline void cxgbe_set_mbuf_info(struct rte_mbuf *pkt, uint32_t ptype,
1507 				       uint64_t ol_flags)
1508 {
1509 	pkt->packet_type |= ptype;
1510 	pkt->ol_flags |= ol_flags;
1511 }
1512 
1513 static inline void cxgbe_fill_mbuf_info(struct adapter *adap,
1514 					const struct cpl_rx_pkt *cpl,
1515 					struct rte_mbuf *pkt)
1516 {
1517 	bool csum_ok;
1518 	u16 err_vec;
1519 
1520 	if (adap->params.tp.rx_pkt_encap)
1521 		err_vec = G_T6_COMPR_RXERR_VEC(ntohs(cpl->err_vec));
1522 	else
1523 		err_vec = ntohs(cpl->err_vec);
1524 
1525 	csum_ok = cpl->csum_calc && !err_vec;
1526 
1527 	if (cpl->vlan_ex)
1528 		cxgbe_set_mbuf_info(pkt, RTE_PTYPE_L2_ETHER_VLAN,
1529 				    RTE_MBUF_F_RX_VLAN | RTE_MBUF_F_RX_VLAN_STRIPPED);
1530 	else
1531 		cxgbe_set_mbuf_info(pkt, RTE_PTYPE_L2_ETHER, 0);
1532 
1533 	if (cpl->l2info & htonl(F_RXF_IP))
1534 		cxgbe_set_mbuf_info(pkt, RTE_PTYPE_L3_IPV4,
1535 				    csum_ok ? RTE_MBUF_F_RX_IP_CKSUM_GOOD :
1536 				    RTE_MBUF_F_RX_IP_CKSUM_BAD);
1537 	else if (cpl->l2info & htonl(F_RXF_IP6))
1538 		cxgbe_set_mbuf_info(pkt, RTE_PTYPE_L3_IPV6,
1539 				    csum_ok ? RTE_MBUF_F_RX_IP_CKSUM_GOOD :
1540 				    RTE_MBUF_F_RX_IP_CKSUM_BAD);
1541 
1542 	if (cpl->l2info & htonl(F_RXF_TCP))
1543 		cxgbe_set_mbuf_info(pkt, RTE_PTYPE_L4_TCP,
1544 				    csum_ok ? RTE_MBUF_F_RX_L4_CKSUM_GOOD :
1545 				    RTE_MBUF_F_RX_L4_CKSUM_BAD);
1546 	else if (cpl->l2info & htonl(F_RXF_UDP))
1547 		cxgbe_set_mbuf_info(pkt, RTE_PTYPE_L4_UDP,
1548 				    csum_ok ? RTE_MBUF_F_RX_L4_CKSUM_GOOD :
1549 				    RTE_MBUF_F_RX_L4_CKSUM_BAD);
1550 }
1551 
1552 /**
1553  * process_responses - process responses from an SGE response queue
1554  * @q: the ingress queue to process
1555  * @budget: how many responses can be processed in this round
1556  * @rx_pkts: mbuf to put the pkts
1557  *
1558  * Process responses from an SGE response queue up to the supplied budget.
1559  * Responses include received packets as well as control messages from FW
1560  * or HW.
1561  *
1562  * Additionally choose the interrupt holdoff time for the next interrupt
1563  * on this queue.  If the system is under memory shortage use a fairly
1564  * long delay to help recovery.
1565  */
1566 static int process_responses(struct sge_rspq *q, int budget,
1567 			     struct rte_mbuf **rx_pkts)
1568 {
1569 	int ret = 0, rsp_type;
1570 	int budget_left = budget;
1571 	const struct rsp_ctrl *rc;
1572 	struct sge_eth_rxq *rxq = container_of(q, struct sge_eth_rxq, rspq);
1573 
1574 	while (likely(budget_left)) {
1575 		if (q->cidx == ntohs(q->stat->pidx))
1576 			break;
1577 
1578 		rc = (const struct rsp_ctrl *)
1579 		     ((const char *)q->cur_desc + (q->iqe_len - sizeof(*rc)));
1580 
1581 		/*
1582 		 * Ensure response has been read
1583 		 */
1584 		rmb();
1585 		rsp_type = G_RSPD_TYPE(rc->u.type_gen);
1586 
1587 		if (likely(rsp_type == X_RSPD_TYPE_FLBUF)) {
1588 			struct sge *s = &q->adapter->sge;
1589 			unsigned int stat_pidx;
1590 			int stat_pidx_diff;
1591 
1592 			stat_pidx = ntohs(q->stat->pidx);
1593 			stat_pidx_diff = P_IDXDIFF(q, stat_pidx);
1594 			while (stat_pidx_diff && budget_left) {
1595 				const struct rx_sw_desc *rsd =
1596 					&rxq->fl.sdesc[rxq->fl.cidx];
1597 				const struct rss_header *rss_hdr =
1598 					(const void *)q->cur_desc;
1599 				const struct cpl_rx_pkt *cpl =
1600 					(const void *)&q->cur_desc[1];
1601 				struct rte_mbuf *pkt, *npkt;
1602 				u32 len, bufsz;
1603 
1604 				rc = (const struct rsp_ctrl *)
1605 				     ((const char *)q->cur_desc +
1606 				      (q->iqe_len - sizeof(*rc)));
1607 
1608 				rsp_type = G_RSPD_TYPE(rc->u.type_gen);
1609 				if (unlikely(rsp_type != X_RSPD_TYPE_FLBUF))
1610 					break;
1611 
1612 				len = ntohl(rc->pldbuflen_qid);
1613 				BUG_ON(!(len & F_RSPD_NEWBUF));
1614 				pkt = rsd->buf;
1615 				npkt = pkt;
1616 				len = G_RSPD_LEN(len);
1617 				pkt->pkt_len = len;
1618 
1619 				/* Chain mbufs into len if necessary */
1620 				while (len) {
1621 					struct rte_mbuf *new_pkt = rsd->buf;
1622 
1623 					bufsz = min(get_buf_size(q->adapter,
1624 								 rsd), len);
1625 					new_pkt->data_len = bufsz;
1626 					unmap_rx_buf(&rxq->fl);
1627 					len -= bufsz;
1628 					npkt->next = new_pkt;
1629 					npkt = new_pkt;
1630 					pkt->nb_segs++;
1631 					rsd = &rxq->fl.sdesc[rxq->fl.cidx];
1632 				}
1633 				npkt->next = NULL;
1634 				pkt->nb_segs--;
1635 
1636 				cxgbe_fill_mbuf_info(q->adapter, cpl, pkt);
1637 
1638 				if (!rss_hdr->filter_tid &&
1639 				    rss_hdr->hash_type) {
1640 					pkt->ol_flags |= RTE_MBUF_F_RX_RSS_HASH;
1641 					pkt->hash.rss =
1642 						ntohl(rss_hdr->hash_val);
1643 				}
1644 
1645 				if (cpl->vlan_ex)
1646 					pkt->vlan_tci = ntohs(cpl->vlan);
1647 
1648 				rte_pktmbuf_adj(pkt, s->pktshift);
1649 				rxq->stats.pkts++;
1650 				rxq->stats.rx_bytes += pkt->pkt_len;
1651 				rx_pkts[budget - budget_left] = pkt;
1652 
1653 				rspq_next(q);
1654 				budget_left--;
1655 				stat_pidx_diff--;
1656 			}
1657 			continue;
1658 		} else if (likely(rsp_type == X_RSPD_TYPE_CPL)) {
1659 			ret = q->handler(q, q->cur_desc, NULL);
1660 		} else {
1661 			ret = q->handler(q, (const __be64 *)rc, CXGB4_MSG_AN);
1662 		}
1663 
1664 		if (unlikely(ret)) {
1665 			/* couldn't process descriptor, back off for recovery */
1666 			q->next_intr_params = V_QINTR_TIMER_IDX(NOMEM_TMR_IDX);
1667 			break;
1668 		}
1669 
1670 		rspq_next(q);
1671 		budget_left--;
1672 	}
1673 
1674 	/*
1675 	 * If this is a Response Queue with an associated Free List and
1676 	 * there's room for another chunk of new Free List buffer pointers,
1677 	 * refill the Free List.
1678 	 */
1679 
1680 	if (q->offset >= 0 && fl_cap(&rxq->fl) - rxq->fl.avail >= 64)
1681 		__refill_fl(q->adapter, &rxq->fl);
1682 
1683 	return budget - budget_left;
1684 }
1685 
1686 int cxgbe_poll(struct sge_rspq *q, struct rte_mbuf **rx_pkts,
1687 	       unsigned int budget, unsigned int *work_done)
1688 {
1689 	struct sge_eth_rxq *rxq = container_of(q, struct sge_eth_rxq, rspq);
1690 	unsigned int cidx_inc;
1691 	unsigned int params;
1692 	u32 val;
1693 
1694 	if (unlikely(rxq->flags & IQ_STOPPED)) {
1695 		*work_done = 0;
1696 		return 0;
1697 	}
1698 
1699 	*work_done = process_responses(q, budget, rx_pkts);
1700 
1701 	if (*work_done) {
1702 		cidx_inc = R_IDXDIFF(q, gts_idx);
1703 
1704 		if (q->offset >= 0 && fl_cap(&rxq->fl) - rxq->fl.avail >= 64)
1705 			__refill_fl(q->adapter, &rxq->fl);
1706 
1707 		params = q->intr_params;
1708 		q->next_intr_params = params;
1709 		val = V_CIDXINC(cidx_inc) | V_SEINTARM(params);
1710 
1711 		if (unlikely(!q->bar2_addr)) {
1712 			u32 reg = is_pf4(q->adapter) ? MYPF_REG(A_SGE_PF_GTS) :
1713 						       T4VF_SGE_BASE_ADDR +
1714 						       A_SGE_VF_GTS;
1715 
1716 			t4_write_reg(q->adapter, reg,
1717 				     val | V_INGRESSQID((u32)q->cntxt_id));
1718 		} else {
1719 			writel(val | V_INGRESSQID(q->bar2_qid),
1720 			       (void *)((uintptr_t)q->bar2_addr + SGE_UDB_GTS));
1721 			/* This Write memory Barrier will force the
1722 			 * write to the User Doorbell area to be
1723 			 * flushed.
1724 			 */
1725 			wmb();
1726 		}
1727 		q->gts_idx = q->cidx;
1728 	}
1729 	return 0;
1730 }
1731 
1732 /**
1733  * bar2_address - return the BAR2 address for an SGE Queue's Registers
1734  * @adapter: the adapter
1735  * @qid: the SGE Queue ID
1736  * @qtype: the SGE Queue Type (Egress or Ingress)
1737  * @pbar2_qid: BAR2 Queue ID or 0 for Queue ID inferred SGE Queues
1738  *
1739  * Returns the BAR2 address for the SGE Queue Registers associated with
1740  * @qid.  If BAR2 SGE Registers aren't available, returns NULL.  Also
1741  * returns the BAR2 Queue ID to be used with writes to the BAR2 SGE
1742  * Queue Registers.  If the BAR2 Queue ID is 0, then "Inferred Queue ID"
1743  * Registers are supported (e.g. the Write Combining Doorbell Buffer).
1744  */
1745 static void __iomem *bar2_address(struct adapter *adapter, unsigned int qid,
1746 				  enum t4_bar2_qtype qtype,
1747 				  unsigned int *pbar2_qid)
1748 {
1749 	u64 bar2_qoffset;
1750 	int ret;
1751 
1752 	ret = t4_bar2_sge_qregs(adapter, qid, qtype, &bar2_qoffset, pbar2_qid);
1753 	if (ret)
1754 		return NULL;
1755 
1756 	return adapter->bar2 + bar2_qoffset;
1757 }
1758 
1759 int t4_sge_eth_rxq_start(struct adapter *adap, struct sge_eth_rxq *rxq)
1760 {
1761 	unsigned int fl_id = rxq->fl.size ? rxq->fl.cntxt_id : 0xffff;
1762 
1763 	rxq->flags &= ~IQ_STOPPED;
1764 	return t4_iq_start_stop(adap, adap->mbox, true, adap->pf, 0,
1765 				rxq->rspq.cntxt_id, fl_id, 0xffff);
1766 }
1767 
1768 int t4_sge_eth_rxq_stop(struct adapter *adap, struct sge_eth_rxq *rxq)
1769 {
1770 	unsigned int fl_id = rxq->fl.size ? rxq->fl.cntxt_id : 0xffff;
1771 
1772 	rxq->flags |= IQ_STOPPED;
1773 	return t4_iq_start_stop(adap, adap->mbox, false, adap->pf, 0,
1774 				rxq->rspq.cntxt_id, fl_id, 0xffff);
1775 }
1776 
1777 /*
1778  * @intr_idx: MSI/MSI-X vector if >=0, -(absolute qid + 1) if < 0
1779  * @cong: < 0 -> no congestion feedback, >= 0 -> congestion channel map
1780  */
1781 int t4_sge_alloc_rxq(struct adapter *adap, struct sge_rspq *iq, bool fwevtq,
1782 		     struct rte_eth_dev *eth_dev, int intr_idx,
1783 		     struct sge_fl *fl, rspq_handler_t hnd, int cong,
1784 		     struct rte_mempool *mp, int queue_id, int socket_id)
1785 {
1786 	int ret, flsz = 0;
1787 	struct fw_iq_cmd c;
1788 	struct sge *s = &adap->sge;
1789 	struct port_info *pi = eth_dev->data->dev_private;
1790 	unsigned int nb_refill;
1791 	u8 pciechan;
1792 
1793 	/* Size needs to be multiple of 16, including status entry. */
1794 	iq->size = cxgbe_roundup(iq->size, 16);
1795 
1796 	iq->desc = alloc_ring(eth_dev, fwevtq ? "fwq_ring" : "rx_ring",
1797 			      queue_id, socket_id, iq->size, iq->iqe_len,
1798 			      0, 0, &iq->phys_addr, NULL);
1799 	if (!iq->desc)
1800 		return -ENOMEM;
1801 
1802 	memset(&c, 0, sizeof(c));
1803 	c.op_to_vfn = htonl(V_FW_CMD_OP(FW_IQ_CMD) | F_FW_CMD_REQUEST |
1804 			    F_FW_CMD_WRITE | F_FW_CMD_EXEC);
1805 
1806 	if (is_pf4(adap)) {
1807 		pciechan = pi->tx_chan;
1808 		c.op_to_vfn |= htonl(V_FW_IQ_CMD_PFN(adap->pf) |
1809 				     V_FW_IQ_CMD_VFN(0));
1810 		if (cong >= 0)
1811 			c.iqns_to_fl0congen =
1812 				htonl(F_FW_IQ_CMD_IQFLINTCONGEN |
1813 				      V_FW_IQ_CMD_IQTYPE(cong ?
1814 							 FW_IQ_IQTYPE_NIC :
1815 							 FW_IQ_IQTYPE_OFLD) |
1816 				      F_FW_IQ_CMD_IQRO);
1817 	} else {
1818 		pciechan = pi->port_id;
1819 	}
1820 
1821 	c.alloc_to_len16 = htonl(F_FW_IQ_CMD_ALLOC | F_FW_IQ_CMD_IQSTART |
1822 				 (sizeof(c) / 16));
1823 	c.type_to_iqandstindex =
1824 		htonl(V_FW_IQ_CMD_TYPE(FW_IQ_TYPE_FL_INT_CAP) |
1825 		      V_FW_IQ_CMD_IQASYNCH(fwevtq) |
1826 		      V_FW_IQ_CMD_VIID(pi->viid) |
1827 		      V_FW_IQ_CMD_IQANDST(intr_idx < 0) |
1828 		      V_FW_IQ_CMD_IQANUD(X_UPDATEDELIVERY_STATUS_PAGE) |
1829 		      V_FW_IQ_CMD_IQANDSTINDEX(intr_idx >= 0 ? intr_idx :
1830 							       -intr_idx - 1));
1831 	c.iqdroprss_to_iqesize =
1832 		htons(V_FW_IQ_CMD_IQPCIECH(pciechan) |
1833 		      F_FW_IQ_CMD_IQGTSMODE |
1834 		      V_FW_IQ_CMD_IQINTCNTTHRESH(iq->pktcnt_idx) |
1835 		      V_FW_IQ_CMD_IQESIZE(ilog2(iq->iqe_len) - 4));
1836 	c.iqsize = htons(iq->size);
1837 	c.iqaddr = cpu_to_be64(iq->phys_addr);
1838 
1839 	if (fl) {
1840 		struct sge_eth_rxq *rxq = container_of(fl, struct sge_eth_rxq,
1841 						       fl);
1842 		unsigned int chip_ver = CHELSIO_CHIP_VERSION(adap->params.chip);
1843 
1844 		/*
1845 		 * Allocate the ring for the hardware free list (with space
1846 		 * for its status page) along with the associated software
1847 		 * descriptor ring.  The free list size needs to be a multiple
1848 		 * of the Egress Queue Unit and at least 2 Egress Units larger
1849 		 * than the SGE's Egress Congestion Threshold
1850 		 * (fl_starve_thres - 1).
1851 		 */
1852 		if (fl->size < s->fl_starve_thres - 1 + 2 * 8)
1853 			fl->size = s->fl_starve_thres - 1 + 2 * 8;
1854 		fl->size = cxgbe_roundup(fl->size, 8);
1855 
1856 		fl->desc = alloc_ring(eth_dev, "fl_ring", queue_id, socket_id,
1857 				      fl->size, sizeof(__be64), s->stat_len,
1858 				      sizeof(struct rx_sw_desc),
1859 				      &fl->addr, &fl->sdesc);
1860 		if (!fl->desc) {
1861 			ret = -ENOMEM;
1862 			goto err;
1863 		}
1864 
1865 		flsz = fl->size / 8 + s->stat_len / sizeof(struct tx_desc);
1866 		c.iqns_to_fl0congen |=
1867 			htonl(V_FW_IQ_CMD_FL0HOSTFCMODE(X_HOSTFCMODE_NONE) |
1868 			      (unlikely(rxq->usembufs) ?
1869 			       0 : F_FW_IQ_CMD_FL0PACKEN) |
1870 			      F_FW_IQ_CMD_FL0FETCHRO | F_FW_IQ_CMD_FL0DATARO |
1871 			      F_FW_IQ_CMD_FL0PADEN);
1872 		if (is_pf4(adap) && cong >= 0)
1873 			c.iqns_to_fl0congen |=
1874 				htonl(V_FW_IQ_CMD_FL0CNGCHMAP(cong) |
1875 				      F_FW_IQ_CMD_FL0CONGCIF |
1876 				      F_FW_IQ_CMD_FL0CONGEN);
1877 
1878 		/* In T6, for egress queue type FL there is internal overhead
1879 		 * of 16B for header going into FLM module.
1880 		 * Hence maximum allowed burst size will be 448 bytes.
1881 		 */
1882 		c.fl0dcaen_to_fl0cidxfthresh =
1883 			htons(V_FW_IQ_CMD_FL0FBMIN(chip_ver <= CHELSIO_T5 ?
1884 						   X_FETCHBURSTMIN_128B :
1885 						   X_FETCHBURSTMIN_64B) |
1886 			      V_FW_IQ_CMD_FL0FBMAX(chip_ver <= CHELSIO_T5 ?
1887 						   X_FETCHBURSTMAX_512B :
1888 						   X_FETCHBURSTMAX_256B));
1889 		c.fl0size = htons(flsz);
1890 		c.fl0addr = cpu_to_be64(fl->addr);
1891 	}
1892 
1893 	if (is_pf4(adap))
1894 		ret = t4_wr_mbox(adap, adap->mbox, &c, sizeof(c), &c);
1895 	else
1896 		ret = t4vf_wr_mbox(adap, &c, sizeof(c), &c);
1897 	if (ret)
1898 		goto err;
1899 
1900 	iq->cur_desc = iq->desc;
1901 	iq->cidx = 0;
1902 	iq->gts_idx = 0;
1903 	iq->gen = 1;
1904 	iq->next_intr_params = iq->intr_params;
1905 	iq->cntxt_id = ntohs(c.iqid);
1906 	iq->abs_id = ntohs(c.physiqid);
1907 	iq->bar2_addr = bar2_address(adap, iq->cntxt_id, T4_BAR2_QTYPE_INGRESS,
1908 				     &iq->bar2_qid);
1909 	iq->size--;                           /* subtract status entry */
1910 	iq->stat = (void *)&iq->desc[iq->size * 8];
1911 	iq->eth_dev = eth_dev;
1912 	iq->handler = hnd;
1913 	iq->port_id = pi->pidx;
1914 	iq->mb_pool = mp;
1915 
1916 	/* set offset to -1 to distinguish ingress queues without FL */
1917 	iq->offset = fl ? 0 : -1;
1918 
1919 	if (fl) {
1920 		fl->cntxt_id = ntohs(c.fl0id);
1921 		fl->avail = 0;
1922 		fl->pend_cred = 0;
1923 		fl->pidx = 0;
1924 		fl->cidx = 0;
1925 		fl->alloc_failed = 0;
1926 
1927 		/*
1928 		 * Note, we must initialize the BAR2 Free List User Doorbell
1929 		 * information before refilling the Free List!
1930 		 */
1931 		fl->bar2_addr = bar2_address(adap, fl->cntxt_id,
1932 					     T4_BAR2_QTYPE_EGRESS,
1933 					     &fl->bar2_qid);
1934 
1935 		nb_refill = refill_fl(adap, fl, fl_cap(fl));
1936 		if (nb_refill != fl_cap(fl)) {
1937 			ret = -ENOMEM;
1938 			dev_err(adap, "%s: mbuf alloc failed with error: %d\n",
1939 				__func__, ret);
1940 			goto refill_fl_err;
1941 		}
1942 	}
1943 
1944 	/*
1945 	 * For T5 and later we attempt to set up the Congestion Manager values
1946 	 * of the new RX Ethernet Queue.  This should really be handled by
1947 	 * firmware because it's more complex than any host driver wants to
1948 	 * get involved with and it's different per chip and this is almost
1949 	 * certainly wrong.  Formware would be wrong as well, but it would be
1950 	 * a lot easier to fix in one place ...  For now we do something very
1951 	 * simple (and hopefully less wrong).
1952 	 */
1953 	if (is_pf4(adap) && !is_t4(adap->params.chip) && cong >= 0) {
1954 		u8 cng_ch_bits_log = adap->params.arch.cng_ch_bits_log;
1955 		u32 param, val, ch_map = 0;
1956 		int i;
1957 
1958 		param = (V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_DMAQ) |
1959 			 V_FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_DMAQ_CONM_CTXT) |
1960 			 V_FW_PARAMS_PARAM_YZ(iq->cntxt_id));
1961 		if (cong == 0) {
1962 			val = V_CONMCTXT_CNGTPMODE(X_CONMCTXT_CNGTPMODE_QUEUE);
1963 		} else {
1964 			val = V_CONMCTXT_CNGTPMODE(
1965 					X_CONMCTXT_CNGTPMODE_CHANNEL);
1966 			for (i = 0; i < 4; i++) {
1967 				if (cong & (1 << i))
1968 					ch_map |= 1 << (i << cng_ch_bits_log);
1969 			}
1970 			val |= V_CONMCTXT_CNGCHMAP(ch_map);
1971 		}
1972 		ret = t4_set_params(adap, adap->mbox, adap->pf, 0, 1,
1973 				    &param, &val);
1974 		if (ret)
1975 			dev_warn(adap->pdev_dev, "Failed to set Congestion Manager Context for Ingress Queue %d: %d\n",
1976 				 iq->cntxt_id, -ret);
1977 	}
1978 
1979 	return 0;
1980 
1981 refill_fl_err:
1982 	t4_iq_free(adap, adap->mbox, adap->pf, 0, FW_IQ_TYPE_FL_INT_CAP,
1983 		   iq->cntxt_id, fl->cntxt_id, 0xffff);
1984 err:
1985 	iq->cntxt_id = 0;
1986 	iq->abs_id = 0;
1987 	if (iq->desc)
1988 		iq->desc = NULL;
1989 
1990 	if (fl && fl->desc) {
1991 		rte_free(fl->sdesc);
1992 		fl->cntxt_id = 0;
1993 		fl->sdesc = NULL;
1994 		fl->desc = NULL;
1995 	}
1996 	return ret;
1997 }
1998 
1999 static void init_txq(struct adapter *adap, struct sge_txq *q, unsigned int id,
2000 		     unsigned int abs_id)
2001 {
2002 	q->cntxt_id = id;
2003 	q->abs_id = abs_id;
2004 	q->bar2_addr = bar2_address(adap, q->cntxt_id, T4_BAR2_QTYPE_EGRESS,
2005 				    &q->bar2_qid);
2006 	q->cidx = 0;
2007 	q->pidx = 0;
2008 	q->dbidx = 0;
2009 	q->in_use = 0;
2010 	q->equeidx = 0;
2011 	q->coalesce.idx = 0;
2012 	q->coalesce.len = 0;
2013 	q->coalesce.flits = 0;
2014 	q->last_coal_idx = 0;
2015 	q->last_pidx = 0;
2016 	q->stat = (void *)&q->desc[q->size];
2017 }
2018 
2019 int t4_sge_eth_txq_start(struct sge_eth_txq *txq)
2020 {
2021 	/*
2022 	 *  TODO: For flow-control, queue may be stopped waiting to reclaim
2023 	 *  credits.
2024 	 *  Ensure queue is in EQ_STOPPED state before starting it.
2025 	 */
2026 	if (!(txq->flags & EQ_STOPPED))
2027 		return -(EBUSY);
2028 
2029 	txq->flags &= ~EQ_STOPPED;
2030 
2031 	return 0;
2032 }
2033 
2034 int t4_sge_eth_txq_stop(struct sge_eth_txq *txq)
2035 {
2036 	txq->flags |= EQ_STOPPED;
2037 
2038 	return 0;
2039 }
2040 
2041 int t4_sge_alloc_eth_txq(struct adapter *adap, struct sge_eth_txq *txq,
2042 			 struct rte_eth_dev *eth_dev, uint16_t queue_id,
2043 			 unsigned int iqid, int socket_id)
2044 {
2045 	int ret, nentries;
2046 	struct fw_eq_eth_cmd c;
2047 	struct sge *s = &adap->sge;
2048 	struct port_info *pi = eth_dev->data->dev_private;
2049 	u8 pciechan;
2050 
2051 	/* Add status entries */
2052 	nentries = txq->q.size + s->stat_len / sizeof(struct tx_desc);
2053 
2054 	txq->q.desc = alloc_ring(eth_dev, "tx_ring", queue_id, socket_id,
2055 				 txq->q.size, sizeof(struct tx_desc),
2056 				 s->stat_len, sizeof(struct tx_sw_desc),
2057 				 &txq->q.phys_addr, &txq->q.sdesc);
2058 	if (!txq->q.desc)
2059 		return -ENOMEM;
2060 
2061 	memset(&c, 0, sizeof(c));
2062 	c.op_to_vfn = htonl(V_FW_CMD_OP(FW_EQ_ETH_CMD) | F_FW_CMD_REQUEST |
2063 			    F_FW_CMD_WRITE | F_FW_CMD_EXEC);
2064 	if (is_pf4(adap)) {
2065 		pciechan = pi->tx_chan;
2066 		c.op_to_vfn |= htonl(V_FW_EQ_ETH_CMD_PFN(adap->pf) |
2067 				     V_FW_EQ_ETH_CMD_VFN(0));
2068 	} else {
2069 		pciechan = pi->port_id;
2070 	}
2071 
2072 	c.alloc_to_len16 = htonl(F_FW_EQ_ETH_CMD_ALLOC |
2073 				 F_FW_EQ_ETH_CMD_EQSTART | (sizeof(c) / 16));
2074 	c.autoequiqe_to_viid = htonl(F_FW_EQ_ETH_CMD_AUTOEQUEQE |
2075 				     V_FW_EQ_ETH_CMD_VIID(pi->viid));
2076 	c.fetchszm_to_iqid =
2077 		htonl(V_FW_EQ_ETH_CMD_HOSTFCMODE(X_HOSTFCMODE_NONE) |
2078 		      V_FW_EQ_ETH_CMD_PCIECHN(pciechan) |
2079 		      F_FW_EQ_ETH_CMD_FETCHRO | V_FW_EQ_ETH_CMD_IQID(iqid));
2080 	c.dcaen_to_eqsize =
2081 		htonl(V_FW_EQ_ETH_CMD_FBMIN(X_FETCHBURSTMIN_64B) |
2082 		      V_FW_EQ_ETH_CMD_FBMAX(X_FETCHBURSTMAX_512B) |
2083 		      V_FW_EQ_ETH_CMD_EQSIZE(nentries));
2084 	c.eqaddr = rte_cpu_to_be_64(txq->q.phys_addr);
2085 
2086 	if (is_pf4(adap))
2087 		ret = t4_wr_mbox(adap, adap->mbox, &c, sizeof(c), &c);
2088 	else
2089 		ret = t4vf_wr_mbox(adap, &c, sizeof(c), &c);
2090 	if (ret) {
2091 		rte_free(txq->q.sdesc);
2092 		txq->q.sdesc = NULL;
2093 		txq->q.desc = NULL;
2094 		return ret;
2095 	}
2096 
2097 	init_txq(adap, &txq->q, G_FW_EQ_ETH_CMD_EQID(ntohl(c.eqid_pkd)),
2098 		 G_FW_EQ_ETH_CMD_PHYSEQID(ntohl(c.physeqid_pkd)));
2099 	txq->stats.tso = 0;
2100 	txq->stats.pkts = 0;
2101 	txq->stats.tx_cso = 0;
2102 	txq->stats.coal_wr = 0;
2103 	txq->stats.vlan_ins = 0;
2104 	txq->stats.tx_bytes = 0;
2105 	txq->stats.coal_pkts = 0;
2106 	txq->stats.mapping_err = 0;
2107 	txq->flags |= EQ_STOPPED;
2108 	txq->eth_dev = eth_dev;
2109 	txq->data = eth_dev->data;
2110 	t4_os_lock_init(&txq->txq_lock);
2111 	return 0;
2112 }
2113 
2114 int t4_sge_alloc_ctrl_txq(struct adapter *adap, struct sge_ctrl_txq *txq,
2115 			  struct rte_eth_dev *eth_dev, uint16_t queue_id,
2116 			  unsigned int iqid, int socket_id)
2117 {
2118 	int ret, nentries;
2119 	struct fw_eq_ctrl_cmd c;
2120 	struct sge *s = &adap->sge;
2121 	struct port_info *pi = eth_dev->data->dev_private;
2122 
2123 	/* Add status entries */
2124 	nentries = txq->q.size + s->stat_len / sizeof(struct tx_desc);
2125 
2126 	txq->q.desc = alloc_ring(eth_dev, "ctrl_tx_ring", queue_id,
2127 				 socket_id, txq->q.size, sizeof(struct tx_desc),
2128 				 0, 0, &txq->q.phys_addr, NULL);
2129 	if (!txq->q.desc)
2130 		return -ENOMEM;
2131 
2132 	memset(&c, 0, sizeof(c));
2133 	c.op_to_vfn = htonl(V_FW_CMD_OP(FW_EQ_CTRL_CMD) | F_FW_CMD_REQUEST |
2134 			    F_FW_CMD_WRITE | F_FW_CMD_EXEC |
2135 			    V_FW_EQ_CTRL_CMD_PFN(adap->pf) |
2136 			    V_FW_EQ_CTRL_CMD_VFN(0));
2137 	c.alloc_to_len16 = htonl(F_FW_EQ_CTRL_CMD_ALLOC |
2138 				 F_FW_EQ_CTRL_CMD_EQSTART | (sizeof(c) / 16));
2139 	c.cmpliqid_eqid = htonl(V_FW_EQ_CTRL_CMD_CMPLIQID(0));
2140 	c.physeqid_pkd = htonl(0);
2141 	c.fetchszm_to_iqid =
2142 		htonl(V_FW_EQ_CTRL_CMD_HOSTFCMODE(X_HOSTFCMODE_NONE) |
2143 		      V_FW_EQ_CTRL_CMD_PCIECHN(pi->tx_chan) |
2144 		      F_FW_EQ_CTRL_CMD_FETCHRO | V_FW_EQ_CTRL_CMD_IQID(iqid));
2145 	c.dcaen_to_eqsize =
2146 		htonl(V_FW_EQ_CTRL_CMD_FBMIN(X_FETCHBURSTMIN_64B) |
2147 		      V_FW_EQ_CTRL_CMD_FBMAX(X_FETCHBURSTMAX_512B) |
2148 		      V_FW_EQ_CTRL_CMD_EQSIZE(nentries));
2149 	c.eqaddr = cpu_to_be64(txq->q.phys_addr);
2150 
2151 	ret = t4_wr_mbox(adap, adap->mbox, &c, sizeof(c), &c);
2152 	if (ret) {
2153 		txq->q.desc = NULL;
2154 		return ret;
2155 	}
2156 
2157 	init_txq(adap, &txq->q, G_FW_EQ_CTRL_CMD_EQID(ntohl(c.cmpliqid_eqid)),
2158 		 G_FW_EQ_CTRL_CMD_EQID(ntohl(c. physeqid_pkd)));
2159 	txq->adapter = adap;
2160 	txq->full = 0;
2161 	return 0;
2162 }
2163 
2164 static void free_txq(struct sge_txq *q)
2165 {
2166 	q->cntxt_id = 0;
2167 	q->sdesc = NULL;
2168 	q->desc = NULL;
2169 }
2170 
2171 static void free_rspq_fl(struct adapter *adap, struct sge_rspq *rq,
2172 			 struct sge_fl *fl)
2173 {
2174 	unsigned int fl_id = fl ? fl->cntxt_id : 0xffff;
2175 
2176 	t4_iq_free(adap, adap->mbox, adap->pf, 0, FW_IQ_TYPE_FL_INT_CAP,
2177 		   rq->cntxt_id, fl_id, 0xffff);
2178 	rq->cntxt_id = 0;
2179 	rq->abs_id = 0;
2180 	rq->desc = NULL;
2181 
2182 	if (fl) {
2183 		free_rx_bufs(fl, fl->avail);
2184 		rte_free(fl->sdesc);
2185 		fl->sdesc = NULL;
2186 		fl->cntxt_id = 0;
2187 		fl->desc = NULL;
2188 	}
2189 }
2190 
2191 /*
2192  * Clear all queues of the port
2193  *
2194  * Note:  This function must only be called after rx and tx path
2195  * of the port have been disabled.
2196  */
2197 void t4_sge_eth_clear_queues(struct port_info *pi)
2198 {
2199 	struct adapter *adap = pi->adapter;
2200 	struct sge_eth_rxq *rxq;
2201 	struct sge_eth_txq *txq;
2202 	int i;
2203 
2204 	rxq = &adap->sge.ethrxq[pi->first_rxqset];
2205 	for (i = 0; i < pi->n_rx_qsets; i++, rxq++) {
2206 		if (rxq->rspq.desc)
2207 			t4_sge_eth_rxq_stop(adap, rxq);
2208 	}
2209 
2210 	txq = &adap->sge.ethtxq[pi->first_txqset];
2211 	for (i = 0; i < pi->n_tx_qsets; i++, txq++) {
2212 		if (txq->q.desc) {
2213 			struct sge_txq *q = &txq->q;
2214 
2215 			t4_sge_eth_txq_stop(txq);
2216 			reclaim_completed_tx(q);
2217 			free_tx_desc(q, q->size);
2218 			q->equeidx = q->pidx;
2219 		}
2220 	}
2221 }
2222 
2223 void t4_sge_eth_rxq_release(struct adapter *adap, struct sge_eth_rxq *rxq)
2224 {
2225 	if (rxq->rspq.desc) {
2226 		t4_sge_eth_rxq_stop(adap, rxq);
2227 		free_rspq_fl(adap, &rxq->rspq, rxq->fl.size ? &rxq->fl : NULL);
2228 	}
2229 }
2230 
2231 void t4_sge_eth_txq_release(struct adapter *adap, struct sge_eth_txq *txq)
2232 {
2233 	if (txq->q.desc) {
2234 		t4_sge_eth_txq_stop(txq);
2235 		reclaim_completed_tx(&txq->q);
2236 		t4_eth_eq_free(adap, adap->mbox, adap->pf, 0, txq->q.cntxt_id);
2237 		free_tx_desc(&txq->q, txq->q.size);
2238 		rte_free(txq->q.sdesc);
2239 		free_txq(&txq->q);
2240 	}
2241 }
2242 
2243 void t4_sge_eth_release_queues(struct port_info *pi)
2244 {
2245 	struct adapter *adap = pi->adapter;
2246 	struct sge_eth_rxq *rxq;
2247 	struct sge_eth_txq *txq;
2248 	unsigned int i;
2249 
2250 	rxq = &adap->sge.ethrxq[pi->first_rxqset];
2251 	/* clean up Ethernet Tx/Rx queues */
2252 	for (i = 0; i < pi->n_rx_qsets; i++, rxq++) {
2253 		/* Free only the queues allocated */
2254 		if (rxq->rspq.desc) {
2255 			t4_sge_eth_rxq_release(adap, rxq);
2256 			rte_eth_dma_zone_free(rxq->rspq.eth_dev, "fl_ring", i);
2257 			rte_eth_dma_zone_free(rxq->rspq.eth_dev, "rx_ring", i);
2258 			rxq->rspq.eth_dev = NULL;
2259 		}
2260 	}
2261 
2262 	txq = &adap->sge.ethtxq[pi->first_txqset];
2263 	for (i = 0; i < pi->n_tx_qsets; i++, txq++) {
2264 		/* Free only the queues allocated */
2265 		if (txq->q.desc) {
2266 			t4_sge_eth_txq_release(adap, txq);
2267 			rte_eth_dma_zone_free(txq->eth_dev, "tx_ring", i);
2268 			txq->eth_dev = NULL;
2269 		}
2270 	}
2271 }
2272 
2273 void t4_sge_tx_monitor_start(struct adapter *adap)
2274 {
2275 	rte_eal_alarm_set(50, tx_timer_cb, (void *)adap);
2276 }
2277 
2278 void t4_sge_tx_monitor_stop(struct adapter *adap)
2279 {
2280 	rte_eal_alarm_cancel(tx_timer_cb, (void *)adap);
2281 }
2282 
2283 /**
2284  * t4_free_sge_resources - free SGE resources
2285  * @adap: the adapter
2286  *
2287  * Frees resources used by the SGE queue sets.
2288  */
2289 void t4_free_sge_resources(struct adapter *adap)
2290 {
2291 	unsigned int i;
2292 
2293 	/* clean up control Tx queues */
2294 	for (i = 0; i < ARRAY_SIZE(adap->sge.ctrlq); i++) {
2295 		struct sge_ctrl_txq *cq = &adap->sge.ctrlq[i];
2296 
2297 		if (cq->q.desc) {
2298 			reclaim_completed_tx_imm(&cq->q);
2299 			t4_ctrl_eq_free(adap, adap->mbox, adap->pf, 0,
2300 					cq->q.cntxt_id);
2301 			rte_eth_dma_zone_free(adap->eth_dev, "ctrl_tx_ring", i);
2302 			rte_mempool_free(cq->mb_pool);
2303 			free_txq(&cq->q);
2304 		}
2305 	}
2306 
2307 	/* clean up firmware event queue */
2308 	if (adap->sge.fw_evtq.desc) {
2309 		free_rspq_fl(adap, &adap->sge.fw_evtq, NULL);
2310 		rte_eth_dma_zone_free(adap->eth_dev, "fwq_ring", 0);
2311 	}
2312 }
2313 
2314 /**
2315  * t4_sge_init - initialize SGE
2316  * @adap: the adapter
2317  *
2318  * Performs SGE initialization needed every time after a chip reset.
2319  * We do not initialize any of the queues here, instead the driver
2320  * top-level must request those individually.
2321  *
2322  * Called in two different modes:
2323  *
2324  *  1. Perform actual hardware initialization and record hard-coded
2325  *     parameters which were used.  This gets used when we're the
2326  *     Master PF and the Firmware Configuration File support didn't
2327  *     work for some reason.
2328  *
2329  *  2. We're not the Master PF or initialization was performed with
2330  *     a Firmware Configuration File.  In this case we need to grab
2331  *     any of the SGE operating parameters that we need to have in
2332  *     order to do our job and make sure we can live with them ...
2333  */
2334 static int t4_sge_init_soft(struct adapter *adap)
2335 {
2336 	struct sge *s = &adap->sge;
2337 	u32 fl_small_pg, fl_large_pg, fl_small_mtu, fl_large_mtu;
2338 	u32 timer_value_0_and_1, timer_value_2_and_3, timer_value_4_and_5;
2339 	u32 ingress_rx_threshold;
2340 
2341 	/*
2342 	 * Verify that CPL messages are going to the Ingress Queue for
2343 	 * process_responses() and that only packet data is going to the
2344 	 * Free Lists.
2345 	 */
2346 	if ((t4_read_reg(adap, A_SGE_CONTROL) & F_RXPKTCPLMODE) !=
2347 	    V_RXPKTCPLMODE(X_RXPKTCPLMODE_SPLIT)) {
2348 		dev_err(adap, "bad SGE CPL MODE\n");
2349 		return -EINVAL;
2350 	}
2351 
2352 	/*
2353 	 * Validate the Host Buffer Register Array indices that we want to
2354 	 * use ...
2355 	 *
2356 	 * XXX Note that we should really read through the Host Buffer Size
2357 	 * XXX register array and find the indices of the Buffer Sizes which
2358 	 * XXX meet our needs!
2359 	 */
2360 #define READ_FL_BUF(x) \
2361 	t4_read_reg(adap, A_SGE_FL_BUFFER_SIZE0 + (x) * sizeof(u32))
2362 
2363 	fl_small_pg = READ_FL_BUF(RX_SMALL_PG_BUF);
2364 	fl_large_pg = READ_FL_BUF(RX_LARGE_PG_BUF);
2365 	fl_small_mtu = READ_FL_BUF(RX_SMALL_MTU_BUF);
2366 	fl_large_mtu = READ_FL_BUF(RX_LARGE_MTU_BUF);
2367 
2368 	/*
2369 	 * We only bother using the Large Page logic if the Large Page Buffer
2370 	 * is larger than our Page Size Buffer.
2371 	 */
2372 	if (fl_large_pg <= fl_small_pg)
2373 		fl_large_pg = 0;
2374 
2375 #undef READ_FL_BUF
2376 
2377 	/*
2378 	 * The Page Size Buffer must be exactly equal to our Page Size and the
2379 	 * Large Page Size Buffer should be 0 (per above) or a power of 2.
2380 	 */
2381 	if (fl_small_pg != CXGBE_PAGE_SIZE ||
2382 	    (fl_large_pg & (fl_large_pg - 1)) != 0) {
2383 		dev_err(adap, "bad SGE FL page buffer sizes [%d, %d]\n",
2384 			fl_small_pg, fl_large_pg);
2385 		return -EINVAL;
2386 	}
2387 	if (fl_large_pg)
2388 		s->fl_pg_order = ilog2(fl_large_pg) - PAGE_SHIFT;
2389 
2390 	if (adap->use_unpacked_mode) {
2391 		int err = 0;
2392 
2393 		if (fl_small_mtu < FL_MTU_SMALL_BUFSIZE(adap)) {
2394 			dev_err(adap, "bad SGE FL small MTU %d\n",
2395 				fl_small_mtu);
2396 			err = -EINVAL;
2397 		}
2398 		if (fl_large_mtu < FL_MTU_LARGE_BUFSIZE(adap)) {
2399 			dev_err(adap, "bad SGE FL large MTU %d\n",
2400 				fl_large_mtu);
2401 			err = -EINVAL;
2402 		}
2403 		if (err)
2404 			return err;
2405 	}
2406 
2407 	/*
2408 	 * Retrieve our RX interrupt holdoff timer values and counter
2409 	 * threshold values from the SGE parameters.
2410 	 */
2411 	timer_value_0_and_1 = t4_read_reg(adap, A_SGE_TIMER_VALUE_0_AND_1);
2412 	timer_value_2_and_3 = t4_read_reg(adap, A_SGE_TIMER_VALUE_2_AND_3);
2413 	timer_value_4_and_5 = t4_read_reg(adap, A_SGE_TIMER_VALUE_4_AND_5);
2414 	s->timer_val[0] = core_ticks_to_us(adap,
2415 					   G_TIMERVALUE0(timer_value_0_and_1));
2416 	s->timer_val[1] = core_ticks_to_us(adap,
2417 					   G_TIMERVALUE1(timer_value_0_and_1));
2418 	s->timer_val[2] = core_ticks_to_us(adap,
2419 					   G_TIMERVALUE2(timer_value_2_and_3));
2420 	s->timer_val[3] = core_ticks_to_us(adap,
2421 					   G_TIMERVALUE3(timer_value_2_and_3));
2422 	s->timer_val[4] = core_ticks_to_us(adap,
2423 					   G_TIMERVALUE4(timer_value_4_and_5));
2424 	s->timer_val[5] = core_ticks_to_us(adap,
2425 					   G_TIMERVALUE5(timer_value_4_and_5));
2426 
2427 	ingress_rx_threshold = t4_read_reg(adap, A_SGE_INGRESS_RX_THRESHOLD);
2428 	s->counter_val[0] = G_THRESHOLD_0(ingress_rx_threshold);
2429 	s->counter_val[1] = G_THRESHOLD_1(ingress_rx_threshold);
2430 	s->counter_val[2] = G_THRESHOLD_2(ingress_rx_threshold);
2431 	s->counter_val[3] = G_THRESHOLD_3(ingress_rx_threshold);
2432 
2433 	return 0;
2434 }
2435 
2436 int t4_sge_init(struct adapter *adap)
2437 {
2438 	struct sge *s = &adap->sge;
2439 	u32 sge_control, sge_conm_ctrl;
2440 	int ret, egress_threshold;
2441 
2442 	/*
2443 	 * Ingress Padding Boundary and Egress Status Page Size are set up by
2444 	 * t4_fixup_host_params().
2445 	 */
2446 	sge_control = t4_read_reg(adap, A_SGE_CONTROL);
2447 	s->pktshift = G_PKTSHIFT(sge_control);
2448 	s->stat_len = (sge_control & F_EGRSTATUSPAGESIZE) ? 128 : 64;
2449 	s->fl_align = t4_fl_pkt_align(adap);
2450 	ret = t4_sge_init_soft(adap);
2451 	if (ret < 0) {
2452 		dev_err(adap, "%s: t4_sge_init_soft failed, error %d\n",
2453 			__func__, -ret);
2454 		return ret;
2455 	}
2456 
2457 	/*
2458 	 * A FL with <= fl_starve_thres buffers is starving and a periodic
2459 	 * timer will attempt to refill it.  This needs to be larger than the
2460 	 * SGE's Egress Congestion Threshold.  If it isn't, then we can get
2461 	 * stuck waiting for new packets while the SGE is waiting for us to
2462 	 * give it more Free List entries.  (Note that the SGE's Egress
2463 	 * Congestion Threshold is in units of 2 Free List pointers.)  For T4,
2464 	 * there was only a single field to control this.  For T5 there's the
2465 	 * original field which now only applies to Unpacked Mode Free List
2466 	 * buffers and a new field which only applies to Packed Mode Free List
2467 	 * buffers.
2468 	 */
2469 	sge_conm_ctrl = t4_read_reg(adap, A_SGE_CONM_CTRL);
2470 	if (is_t4(adap->params.chip) || adap->use_unpacked_mode)
2471 		egress_threshold = G_EGRTHRESHOLD(sge_conm_ctrl);
2472 	else
2473 		egress_threshold = G_EGRTHRESHOLDPACKING(sge_conm_ctrl);
2474 	s->fl_starve_thres = 2 * egress_threshold + 1;
2475 
2476 	return 0;
2477 }
2478 
2479 int t4vf_sge_init(struct adapter *adap)
2480 {
2481 	struct sge_params *sge_params = &adap->params.sge;
2482 	u32 sge_ingress_queues_per_page;
2483 	u32 sge_egress_queues_per_page;
2484 	u32 sge_control, sge_control2;
2485 	u32 fl_small_pg, fl_large_pg;
2486 	u32 sge_ingress_rx_threshold;
2487 	u32 sge_timer_value_0_and_1;
2488 	u32 sge_timer_value_2_and_3;
2489 	u32 sge_timer_value_4_and_5;
2490 	u32 sge_congestion_control;
2491 	struct sge *s = &adap->sge;
2492 	unsigned int s_hps, s_qpp;
2493 	u32 sge_host_page_size;
2494 	u32 params[7], vals[7];
2495 	int v;
2496 
2497 	/* query basic params from fw */
2498 	params[0] = (V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_REG) |
2499 		     V_FW_PARAMS_PARAM_XYZ(A_SGE_CONTROL));
2500 	params[1] = (V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_REG) |
2501 		     V_FW_PARAMS_PARAM_XYZ(A_SGE_HOST_PAGE_SIZE));
2502 	params[2] = (V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_REG) |
2503 		     V_FW_PARAMS_PARAM_XYZ(A_SGE_FL_BUFFER_SIZE0));
2504 	params[3] = (V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_REG) |
2505 		     V_FW_PARAMS_PARAM_XYZ(A_SGE_FL_BUFFER_SIZE1));
2506 	params[4] = (V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_REG) |
2507 		     V_FW_PARAMS_PARAM_XYZ(A_SGE_TIMER_VALUE_0_AND_1));
2508 	params[5] = (V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_REG) |
2509 		     V_FW_PARAMS_PARAM_XYZ(A_SGE_TIMER_VALUE_2_AND_3));
2510 	params[6] = (V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_REG) |
2511 		     V_FW_PARAMS_PARAM_XYZ(A_SGE_TIMER_VALUE_4_AND_5));
2512 	v = t4vf_query_params(adap, 7, params, vals);
2513 	if (v != FW_SUCCESS)
2514 		return v;
2515 
2516 	sge_control = vals[0];
2517 	sge_host_page_size = vals[1];
2518 	fl_small_pg = vals[2];
2519 	fl_large_pg = vals[3];
2520 	sge_timer_value_0_and_1 = vals[4];
2521 	sge_timer_value_2_and_3 = vals[5];
2522 	sge_timer_value_4_and_5 = vals[6];
2523 
2524 	/*
2525 	 * Start by vetting the basic SGE parameters which have been set up by
2526 	 * the Physical Function Driver.
2527 	 */
2528 
2529 	/* We only bother using the Large Page logic if the Large Page Buffer
2530 	 * is larger than our Page Size Buffer.
2531 	 */
2532 	if (fl_large_pg <= fl_small_pg)
2533 		fl_large_pg = 0;
2534 
2535 	/* The Page Size Buffer must be exactly equal to our Page Size and the
2536 	 * Large Page Size Buffer should be 0 (per above) or a power of 2.
2537 	 */
2538 	if (fl_small_pg != CXGBE_PAGE_SIZE ||
2539 	    (fl_large_pg & (fl_large_pg - 1)) != 0) {
2540 		dev_err(adapter->pdev_dev, "bad SGE FL buffer sizes [%d, %d]\n",
2541 			fl_small_pg, fl_large_pg);
2542 		return -EINVAL;
2543 	}
2544 
2545 	if ((sge_control & F_RXPKTCPLMODE) !=
2546 	    V_RXPKTCPLMODE(X_RXPKTCPLMODE_SPLIT)) {
2547 		dev_err(adapter->pdev_dev, "bad SGE CPL MODE\n");
2548 		return -EINVAL;
2549 	}
2550 
2551 
2552 	/* Grab ingress packing boundary from SGE_CONTROL2 for */
2553 	params[0] = (V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_REG) |
2554 		     V_FW_PARAMS_PARAM_XYZ(A_SGE_CONTROL2));
2555 	v = t4vf_query_params(adap, 1, params, vals);
2556 	if (v != FW_SUCCESS) {
2557 		dev_err(adapter, "Unable to get SGE Control2; "
2558 			"probably old firmware.\n");
2559 		return v;
2560 	}
2561 	sge_control2 = vals[0];
2562 
2563 	params[0] = (V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_REG) |
2564 		     V_FW_PARAMS_PARAM_XYZ(A_SGE_INGRESS_RX_THRESHOLD));
2565 	params[1] = (V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_REG) |
2566 		     V_FW_PARAMS_PARAM_XYZ(A_SGE_CONM_CTRL));
2567 	v = t4vf_query_params(adap, 2, params, vals);
2568 	if (v != FW_SUCCESS)
2569 		return v;
2570 	sge_ingress_rx_threshold = vals[0];
2571 	sge_congestion_control = vals[1];
2572 	params[0] = (V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_REG) |
2573 		     V_FW_PARAMS_PARAM_XYZ(A_SGE_EGRESS_QUEUES_PER_PAGE_VF));
2574 	params[1] = (V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_REG) |
2575 		     V_FW_PARAMS_PARAM_XYZ(A_SGE_INGRESS_QUEUES_PER_PAGE_VF));
2576 	v = t4vf_query_params(adap, 2, params, vals);
2577 	if (v != FW_SUCCESS) {
2578 		dev_warn(adap, "Unable to get VF SGE Queues/Page; "
2579 			 "probably old firmware.\n");
2580 		return v;
2581 	}
2582 	sge_egress_queues_per_page = vals[0];
2583 	sge_ingress_queues_per_page = vals[1];
2584 
2585 	/*
2586 	 * We need the Queues/Page for our VF.  This is based on the
2587 	 * PF from which we're instantiated and is indexed in the
2588 	 * register we just read.
2589 	 */
2590 	s_hps = (S_HOSTPAGESIZEPF0 +
2591 		 (S_HOSTPAGESIZEPF1 - S_HOSTPAGESIZEPF0) * adap->pf);
2592 	sge_params->hps =
2593 		((sge_host_page_size >> s_hps) & M_HOSTPAGESIZEPF0);
2594 
2595 	s_qpp = (S_QUEUESPERPAGEPF0 +
2596 		 (S_QUEUESPERPAGEPF1 - S_QUEUESPERPAGEPF0) * adap->pf);
2597 	sge_params->eq_qpp =
2598 		((sge_egress_queues_per_page >> s_qpp)
2599 		 & M_QUEUESPERPAGEPF0);
2600 	sge_params->iq_qpp =
2601 		((sge_ingress_queues_per_page >> s_qpp)
2602 		 & M_QUEUESPERPAGEPF0);
2603 
2604 	/*
2605 	 * Now translate the queried parameters into our internal forms.
2606 	 */
2607 	if (fl_large_pg)
2608 		s->fl_pg_order = ilog2(fl_large_pg) - PAGE_SHIFT;
2609 	s->stat_len = ((sge_control & F_EGRSTATUSPAGESIZE)
2610 			? 128 : 64);
2611 	s->pktshift = G_PKTSHIFT(sge_control);
2612 	s->fl_align = t4vf_fl_pkt_align(adap, sge_control, sge_control2);
2613 
2614 	/*
2615 	 * A FL with <= fl_starve_thres buffers is starving and a periodic
2616 	 * timer will attempt to refill it.  This needs to be larger than the
2617 	 * SGE's Egress Congestion Threshold.  If it isn't, then we can get
2618 	 * stuck waiting for new packets while the SGE is waiting for us to
2619 	 * give it more Free List entries.  (Note that the SGE's Egress
2620 	 * Congestion Threshold is in units of 2 Free List pointers.)
2621 	 */
2622 	switch (CHELSIO_CHIP_VERSION(adap->params.chip)) {
2623 	case CHELSIO_T5:
2624 		s->fl_starve_thres =
2625 			G_EGRTHRESHOLDPACKING(sge_congestion_control);
2626 		break;
2627 	case CHELSIO_T6:
2628 	default:
2629 		s->fl_starve_thres =
2630 			G_T6_EGRTHRESHOLDPACKING(sge_congestion_control);
2631 		break;
2632 	}
2633 	s->fl_starve_thres = s->fl_starve_thres * 2 + 1;
2634 
2635 	/*
2636 	 * Save RX interrupt holdoff timer values and counter
2637 	 * threshold values from the SGE parameters.
2638 	 */
2639 	s->timer_val[0] = core_ticks_to_us(adap,
2640 			G_TIMERVALUE0(sge_timer_value_0_and_1));
2641 	s->timer_val[1] = core_ticks_to_us(adap,
2642 			G_TIMERVALUE1(sge_timer_value_0_and_1));
2643 	s->timer_val[2] = core_ticks_to_us(adap,
2644 			G_TIMERVALUE2(sge_timer_value_2_and_3));
2645 	s->timer_val[3] = core_ticks_to_us(adap,
2646 			G_TIMERVALUE3(sge_timer_value_2_and_3));
2647 	s->timer_val[4] = core_ticks_to_us(adap,
2648 			G_TIMERVALUE4(sge_timer_value_4_and_5));
2649 	s->timer_val[5] = core_ticks_to_us(adap,
2650 			G_TIMERVALUE5(sge_timer_value_4_and_5));
2651 	s->counter_val[0] = G_THRESHOLD_0(sge_ingress_rx_threshold);
2652 	s->counter_val[1] = G_THRESHOLD_1(sge_ingress_rx_threshold);
2653 	s->counter_val[2] = G_THRESHOLD_2(sge_ingress_rx_threshold);
2654 	s->counter_val[3] = G_THRESHOLD_3(sge_ingress_rx_threshold);
2655 	return 0;
2656 }
2657