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