eadc06bcfd0fe506655116fd760f3fc501c3a7a4
[dpdk.git] / drivers / net / txgbe / txgbe_rxtx.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2015-2020
3  */
4
5 #include <sys/queue.h>
6
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10
11 #include <rte_common.h>
12 #include <rte_ethdev.h>
13 #include <rte_ethdev_driver.h>
14 #include <rte_mbuf.h>
15
16 #include "txgbe_logs.h"
17 #include "base/txgbe.h"
18 #include "txgbe_ethdev.h"
19 #include "txgbe_rxtx.h"
20
21 static int
22 txgbe_is_vf(struct rte_eth_dev *dev)
23 {
24         struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
25
26         switch (hw->mac.type) {
27         case txgbe_mac_raptor_vf:
28                 return 1;
29         default:
30                 return 0;
31         }
32 }
33
34 uint64_t
35 txgbe_get_rx_queue_offloads(struct rte_eth_dev *dev __rte_unused)
36 {
37         return DEV_RX_OFFLOAD_VLAN_STRIP;
38 }
39
40 uint64_t
41 txgbe_get_rx_port_offloads(struct rte_eth_dev *dev)
42 {
43         uint64_t offloads;
44         struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
45         struct rte_eth_dev_sriov *sriov = &RTE_ETH_DEV_SRIOV(dev);
46
47         offloads = DEV_RX_OFFLOAD_IPV4_CKSUM  |
48                    DEV_RX_OFFLOAD_UDP_CKSUM   |
49                    DEV_RX_OFFLOAD_TCP_CKSUM   |
50                    DEV_RX_OFFLOAD_KEEP_CRC    |
51                    DEV_RX_OFFLOAD_JUMBO_FRAME |
52                    DEV_RX_OFFLOAD_VLAN_FILTER |
53                    DEV_RX_OFFLOAD_RSS_HASH |
54                    DEV_RX_OFFLOAD_SCATTER;
55
56         if (!txgbe_is_vf(dev))
57                 offloads |= (DEV_RX_OFFLOAD_VLAN_FILTER |
58                              DEV_RX_OFFLOAD_QINQ_STRIP |
59                              DEV_RX_OFFLOAD_VLAN_EXTEND);
60
61         /*
62          * RSC is only supported by PF devices in a non-SR-IOV
63          * mode.
64          */
65         if (hw->mac.type == txgbe_mac_raptor && !sriov->active)
66                 offloads |= DEV_RX_OFFLOAD_TCP_LRO;
67
68         if (hw->mac.type == txgbe_mac_raptor)
69                 offloads |= DEV_RX_OFFLOAD_MACSEC_STRIP;
70
71         offloads |= DEV_RX_OFFLOAD_OUTER_IPV4_CKSUM;
72
73         return offloads;
74 }
75
76 uint64_t
77 txgbe_get_tx_queue_offloads(struct rte_eth_dev *dev)
78 {
79         RTE_SET_USED(dev);
80
81         return 0;
82 }
83
84 uint64_t
85 txgbe_get_tx_port_offloads(struct rte_eth_dev *dev)
86 {
87         uint64_t tx_offload_capa;
88
89         tx_offload_capa =
90                 DEV_TX_OFFLOAD_VLAN_INSERT |
91                 DEV_TX_OFFLOAD_IPV4_CKSUM  |
92                 DEV_TX_OFFLOAD_UDP_CKSUM   |
93                 DEV_TX_OFFLOAD_TCP_CKSUM   |
94                 DEV_TX_OFFLOAD_SCTP_CKSUM  |
95                 DEV_TX_OFFLOAD_TCP_TSO     |
96                 DEV_TX_OFFLOAD_UDP_TSO     |
97                 DEV_TX_OFFLOAD_UDP_TNL_TSO      |
98                 DEV_TX_OFFLOAD_IP_TNL_TSO       |
99                 DEV_TX_OFFLOAD_VXLAN_TNL_TSO    |
100                 DEV_TX_OFFLOAD_GRE_TNL_TSO      |
101                 DEV_TX_OFFLOAD_IPIP_TNL_TSO     |
102                 DEV_TX_OFFLOAD_GENEVE_TNL_TSO   |
103                 DEV_TX_OFFLOAD_MULTI_SEGS;
104
105         if (!txgbe_is_vf(dev))
106                 tx_offload_capa |= DEV_TX_OFFLOAD_QINQ_INSERT;
107
108         tx_offload_capa |= DEV_TX_OFFLOAD_MACSEC_INSERT;
109
110         tx_offload_capa |= DEV_TX_OFFLOAD_OUTER_IPV4_CKSUM;
111
112         return tx_offload_capa;
113 }
114
115 void __rte_cold
116 txgbe_set_rx_function(struct rte_eth_dev *dev)
117 {
118         RTE_SET_USED(dev);
119 }
120
121 /**
122  * txgbe_get_rscctl_maxdesc
123  *
124  * @pool Memory pool of the Rx queue
125  */
126 static inline uint32_t
127 txgbe_get_rscctl_maxdesc(struct rte_mempool *pool)
128 {
129         struct rte_pktmbuf_pool_private *mp_priv = rte_mempool_get_priv(pool);
130
131         uint16_t maxdesc =
132                 RTE_IPV4_MAX_PKT_LEN /
133                         (mp_priv->mbuf_data_room_size - RTE_PKTMBUF_HEADROOM);
134
135         if (maxdesc >= 16)
136                 return TXGBE_RXCFG_RSCMAX_16;
137         else if (maxdesc >= 8)
138                 return TXGBE_RXCFG_RSCMAX_8;
139         else if (maxdesc >= 4)
140                 return TXGBE_RXCFG_RSCMAX_4;
141         else
142                 return TXGBE_RXCFG_RSCMAX_1;
143 }
144
145 /**
146  * txgbe_set_rsc - configure RSC related port HW registers
147  *
148  * Configures the port's RSC related registers.
149  *
150  * @dev port handle
151  *
152  * Returns 0 in case of success or a non-zero error code
153  */
154 static int
155 txgbe_set_rsc(struct rte_eth_dev *dev)
156 {
157         struct rte_eth_rxmode *rx_conf = &dev->data->dev_conf.rxmode;
158         struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
159         struct rte_eth_dev_info dev_info = { 0 };
160         bool rsc_capable = false;
161         uint16_t i;
162         uint32_t rdrxctl;
163         uint32_t rfctl;
164
165         /* Sanity check */
166         dev->dev_ops->dev_infos_get(dev, &dev_info);
167         if (dev_info.rx_offload_capa & DEV_RX_OFFLOAD_TCP_LRO)
168                 rsc_capable = true;
169
170         if (!rsc_capable && (rx_conf->offloads & DEV_RX_OFFLOAD_TCP_LRO)) {
171                 PMD_INIT_LOG(CRIT, "LRO is requested on HW that doesn't "
172                                    "support it");
173                 return -EINVAL;
174         }
175
176         /* RSC global configuration */
177
178         if ((rx_conf->offloads & DEV_RX_OFFLOAD_KEEP_CRC) &&
179              (rx_conf->offloads & DEV_RX_OFFLOAD_TCP_LRO)) {
180                 PMD_INIT_LOG(CRIT, "LRO can't be enabled when HW CRC "
181                                     "is disabled");
182                 return -EINVAL;
183         }
184
185         rfctl = rd32(hw, TXGBE_PSRCTL);
186         if (rsc_capable && (rx_conf->offloads & DEV_RX_OFFLOAD_TCP_LRO))
187                 rfctl &= ~TXGBE_PSRCTL_RSCDIA;
188         else
189                 rfctl |= TXGBE_PSRCTL_RSCDIA;
190         wr32(hw, TXGBE_PSRCTL, rfctl);
191
192         /* If LRO hasn't been requested - we are done here. */
193         if (!(rx_conf->offloads & DEV_RX_OFFLOAD_TCP_LRO))
194                 return 0;
195
196         /* Set PSRCTL.RSCACK bit */
197         rdrxctl = rd32(hw, TXGBE_PSRCTL);
198         rdrxctl |= TXGBE_PSRCTL_RSCACK;
199         wr32(hw, TXGBE_PSRCTL, rdrxctl);
200
201         /* Per-queue RSC configuration */
202         for (i = 0; i < dev->data->nb_rx_queues; i++) {
203                 struct txgbe_rx_queue *rxq = dev->data->rx_queues[i];
204                 uint32_t srrctl =
205                         rd32(hw, TXGBE_RXCFG(rxq->reg_idx));
206                 uint32_t psrtype =
207                         rd32(hw, TXGBE_POOLRSS(rxq->reg_idx));
208                 uint32_t eitr =
209                         rd32(hw, TXGBE_ITR(rxq->reg_idx));
210
211                 /*
212                  * txgbe PMD doesn't support header-split at the moment.
213                  */
214                 srrctl &= ~TXGBE_RXCFG_HDRLEN_MASK;
215                 srrctl |= TXGBE_RXCFG_HDRLEN(128);
216
217                 /*
218                  * TODO: Consider setting the Receive Descriptor Minimum
219                  * Threshold Size for an RSC case. This is not an obviously
220                  * beneficiary option but the one worth considering...
221                  */
222
223                 srrctl |= TXGBE_RXCFG_RSCENA;
224                 srrctl &= ~TXGBE_RXCFG_RSCMAX_MASK;
225                 srrctl |= txgbe_get_rscctl_maxdesc(rxq->mb_pool);
226                 psrtype |= TXGBE_POOLRSS_L4HDR;
227
228                 /*
229                  * RSC: Set ITR interval corresponding to 2K ints/s.
230                  *
231                  * Full-sized RSC aggregations for a 10Gb/s link will
232                  * arrive at about 20K aggregation/s rate.
233                  *
234                  * 2K inst/s rate will make only 10% of the
235                  * aggregations to be closed due to the interrupt timer
236                  * expiration for a streaming at wire-speed case.
237                  *
238                  * For a sparse streaming case this setting will yield
239                  * at most 500us latency for a single RSC aggregation.
240                  */
241                 eitr &= ~TXGBE_ITR_IVAL_MASK;
242                 eitr |= TXGBE_ITR_IVAL_10G(TXGBE_QUEUE_ITR_INTERVAL_DEFAULT);
243                 eitr |= TXGBE_ITR_WRDSA;
244
245                 wr32(hw, TXGBE_RXCFG(rxq->reg_idx), srrctl);
246                 wr32(hw, TXGBE_POOLRSS(rxq->reg_idx), psrtype);
247                 wr32(hw, TXGBE_ITR(rxq->reg_idx), eitr);
248
249                 /*
250                  * RSC requires the mapping of the queue to the
251                  * interrupt vector.
252                  */
253                 txgbe_set_ivar_map(hw, 0, rxq->reg_idx, i);
254         }
255
256         dev->data->lro = 1;
257
258         PMD_INIT_LOG(DEBUG, "enabling LRO mode");
259
260         return 0;
261 }
262
263 /*
264  * Initializes Receive Unit.
265  */
266 int __rte_cold
267 txgbe_dev_rx_init(struct rte_eth_dev *dev)
268 {
269         struct txgbe_hw *hw;
270         struct txgbe_rx_queue *rxq;
271         uint64_t bus_addr;
272         uint32_t fctrl;
273         uint32_t hlreg0;
274         uint32_t srrctl;
275         uint32_t rdrxctl;
276         uint32_t rxcsum;
277         uint16_t buf_size;
278         uint16_t i;
279         struct rte_eth_rxmode *rx_conf = &dev->data->dev_conf.rxmode;
280         int rc;
281
282         PMD_INIT_FUNC_TRACE();
283         hw = TXGBE_DEV_HW(dev);
284
285         /*
286          * Make sure receives are disabled while setting
287          * up the RX context (registers, descriptor rings, etc.).
288          */
289         wr32m(hw, TXGBE_MACRXCFG, TXGBE_MACRXCFG_ENA, 0);
290         wr32m(hw, TXGBE_PBRXCTL, TXGBE_PBRXCTL_ENA, 0);
291
292         /* Enable receipt of broadcasted frames */
293         fctrl = rd32(hw, TXGBE_PSRCTL);
294         fctrl |= TXGBE_PSRCTL_BCA;
295         wr32(hw, TXGBE_PSRCTL, fctrl);
296
297         /*
298          * Configure CRC stripping, if any.
299          */
300         hlreg0 = rd32(hw, TXGBE_SECRXCTL);
301         if (rx_conf->offloads & DEV_RX_OFFLOAD_KEEP_CRC)
302                 hlreg0 &= ~TXGBE_SECRXCTL_CRCSTRIP;
303         else
304                 hlreg0 |= TXGBE_SECRXCTL_CRCSTRIP;
305         wr32(hw, TXGBE_SECRXCTL, hlreg0);
306
307         /*
308          * Configure jumbo frame support, if any.
309          */
310         if (rx_conf->offloads & DEV_RX_OFFLOAD_JUMBO_FRAME) {
311                 wr32m(hw, TXGBE_FRMSZ, TXGBE_FRMSZ_MAX_MASK,
312                         TXGBE_FRMSZ_MAX(rx_conf->max_rx_pkt_len));
313         } else {
314                 wr32m(hw, TXGBE_FRMSZ, TXGBE_FRMSZ_MAX_MASK,
315                         TXGBE_FRMSZ_MAX(TXGBE_FRAME_SIZE_DFT));
316         }
317
318         /*
319          * If loopback mode is configured, set LPBK bit.
320          */
321         hlreg0 = rd32(hw, TXGBE_PSRCTL);
322         if (hw->mac.type == txgbe_mac_raptor &&
323             dev->data->dev_conf.lpbk_mode)
324                 hlreg0 |= TXGBE_PSRCTL_LBENA;
325         else
326                 hlreg0 &= ~TXGBE_PSRCTL_LBENA;
327
328         wr32(hw, TXGBE_PSRCTL, hlreg0);
329
330         /*
331          * Assume no header split and no VLAN strip support
332          * on any Rx queue first .
333          */
334         rx_conf->offloads &= ~DEV_RX_OFFLOAD_VLAN_STRIP;
335
336         /* Setup RX queues */
337         for (i = 0; i < dev->data->nb_rx_queues; i++) {
338                 rxq = dev->data->rx_queues[i];
339
340                 /*
341                  * Reset crc_len in case it was changed after queue setup by a
342                  * call to configure.
343                  */
344                 if (rx_conf->offloads & DEV_RX_OFFLOAD_KEEP_CRC)
345                         rxq->crc_len = RTE_ETHER_CRC_LEN;
346                 else
347                         rxq->crc_len = 0;
348
349                 /* Setup the Base and Length of the Rx Descriptor Rings */
350                 bus_addr = rxq->rx_ring_phys_addr;
351                 wr32(hw, TXGBE_RXBAL(rxq->reg_idx),
352                                 (uint32_t)(bus_addr & BIT_MASK32));
353                 wr32(hw, TXGBE_RXBAH(rxq->reg_idx),
354                                 (uint32_t)(bus_addr >> 32));
355                 wr32(hw, TXGBE_RXRP(rxq->reg_idx), 0);
356                 wr32(hw, TXGBE_RXWP(rxq->reg_idx), 0);
357
358                 srrctl = TXGBE_RXCFG_RNGLEN(rxq->nb_rx_desc);
359
360                 /* Set if packets are dropped when no descriptors available */
361                 if (rxq->drop_en)
362                         srrctl |= TXGBE_RXCFG_DROP;
363
364                 /*
365                  * Configure the RX buffer size in the PKTLEN field of
366                  * the RXCFG register of the queue.
367                  * The value is in 1 KB resolution. Valid values can be from
368                  * 1 KB to 16 KB.
369                  */
370                 buf_size = (uint16_t)(rte_pktmbuf_data_room_size(rxq->mb_pool) -
371                         RTE_PKTMBUF_HEADROOM);
372                 buf_size = ROUND_UP(buf_size, 0x1 << 10);
373                 srrctl |= TXGBE_RXCFG_PKTLEN(buf_size);
374
375                 wr32(hw, TXGBE_RXCFG(rxq->reg_idx), srrctl);
376
377                 /* It adds dual VLAN length for supporting dual VLAN */
378                 if (dev->data->dev_conf.rxmode.max_rx_pkt_len +
379                                             2 * TXGBE_VLAN_TAG_SIZE > buf_size)
380                         dev->data->scattered_rx = 1;
381                 if (rxq->offloads & DEV_RX_OFFLOAD_VLAN_STRIP)
382                         rx_conf->offloads |= DEV_RX_OFFLOAD_VLAN_STRIP;
383         }
384
385         if (rx_conf->offloads & DEV_RX_OFFLOAD_SCATTER)
386                 dev->data->scattered_rx = 1;
387
388         /*
389          * Setup the Checksum Register.
390          * Disable Full-Packet Checksum which is mutually exclusive with RSS.
391          * Enable IP/L4 checksum computation by hardware if requested to do so.
392          */
393         rxcsum = rd32(hw, TXGBE_PSRCTL);
394         rxcsum |= TXGBE_PSRCTL_PCSD;
395         if (rx_conf->offloads & DEV_RX_OFFLOAD_CHECKSUM)
396                 rxcsum |= TXGBE_PSRCTL_L4CSUM;
397         else
398                 rxcsum &= ~TXGBE_PSRCTL_L4CSUM;
399
400         wr32(hw, TXGBE_PSRCTL, rxcsum);
401
402         if (hw->mac.type == txgbe_mac_raptor) {
403                 rdrxctl = rd32(hw, TXGBE_SECRXCTL);
404                 if (rx_conf->offloads & DEV_RX_OFFLOAD_KEEP_CRC)
405                         rdrxctl &= ~TXGBE_SECRXCTL_CRCSTRIP;
406                 else
407                         rdrxctl |= TXGBE_SECRXCTL_CRCSTRIP;
408                 wr32(hw, TXGBE_SECRXCTL, rdrxctl);
409         }
410
411         rc = txgbe_set_rsc(dev);
412         if (rc)
413                 return rc;
414
415         txgbe_set_rx_function(dev);
416
417         return 0;
418 }
419
420 /*
421  * Initializes Transmit Unit.
422  */
423 void __rte_cold
424 txgbe_dev_tx_init(struct rte_eth_dev *dev)
425 {
426         struct txgbe_hw     *hw;
427         struct txgbe_tx_queue *txq;
428         uint64_t bus_addr;
429         uint16_t i;
430
431         PMD_INIT_FUNC_TRACE();
432         hw = TXGBE_DEV_HW(dev);
433
434         /* Setup the Base and Length of the Tx Descriptor Rings */
435         for (i = 0; i < dev->data->nb_tx_queues; i++) {
436                 txq = dev->data->tx_queues[i];
437
438                 bus_addr = txq->tx_ring_phys_addr;
439                 wr32(hw, TXGBE_TXBAL(txq->reg_idx),
440                                 (uint32_t)(bus_addr & BIT_MASK32));
441                 wr32(hw, TXGBE_TXBAH(txq->reg_idx),
442                                 (uint32_t)(bus_addr >> 32));
443                 wr32m(hw, TXGBE_TXCFG(txq->reg_idx), TXGBE_TXCFG_BUFLEN_MASK,
444                         TXGBE_TXCFG_BUFLEN(txq->nb_tx_desc));
445                 /* Setup the HW Tx Head and TX Tail descriptor pointers */
446                 wr32(hw, TXGBE_TXRP(txq->reg_idx), 0);
447                 wr32(hw, TXGBE_TXWP(txq->reg_idx), 0);
448         }
449 }
450