net/ice: support device and queue ops
[dpdk.git] / drivers / net / ice / ice_rxtx.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2018 Intel Corporation
3  */
4
5 #include <rte_ethdev_driver.h>
6 #include <rte_net.h>
7
8 #include "ice_rxtx.h"
9
10 #define ICE_TD_CMD ICE_TX_DESC_CMD_EOP
11
12 #define ICE_TX_CKSUM_OFFLOAD_MASK (              \
13                 PKT_TX_IP_CKSUM |                \
14                 PKT_TX_L4_MASK |                 \
15                 PKT_TX_TCP_SEG |                 \
16                 PKT_TX_OUTER_IP_CKSUM)
17
18 #define ICE_RX_ERR_BITS 0x3f
19
20 static enum ice_status
21 ice_program_hw_rx_queue(struct ice_rx_queue *rxq)
22 {
23         struct ice_vsi *vsi = rxq->vsi;
24         struct ice_hw *hw = ICE_VSI_TO_HW(vsi);
25         struct rte_eth_dev *dev = ICE_VSI_TO_ETH_DEV(rxq->vsi);
26         struct ice_rlan_ctx rx_ctx;
27         enum ice_status err;
28         uint16_t buf_size, len;
29         struct rte_eth_rxmode *rxmode = &dev->data->dev_conf.rxmode;
30         uint32_t regval;
31
32         /**
33          * The kernel driver uses flex descriptor. It sets the register
34          * to flex descriptor mode.
35          * DPDK uses legacy descriptor. It should set the register back
36          * to the default value, then uses legacy descriptor mode.
37          */
38         regval = (0x01 << QRXFLXP_CNTXT_RXDID_PRIO_S) &
39                  QRXFLXP_CNTXT_RXDID_PRIO_M;
40         ICE_WRITE_REG(hw, QRXFLXP_CNTXT(rxq->reg_idx), regval);
41
42         /* Set buffer size as the head split is disabled. */
43         buf_size = (uint16_t)(rte_pktmbuf_data_room_size(rxq->mp) -
44                               RTE_PKTMBUF_HEADROOM);
45         rxq->rx_hdr_len = 0;
46         rxq->rx_buf_len = RTE_ALIGN(buf_size, (1 << ICE_RLAN_CTX_DBUF_S));
47         len = ICE_SUPPORT_CHAIN_NUM * rxq->rx_buf_len;
48         rxq->max_pkt_len = RTE_MIN(len,
49                                    dev->data->dev_conf.rxmode.max_rx_pkt_len);
50
51         if (rxmode->offloads & DEV_RX_OFFLOAD_JUMBO_FRAME) {
52                 if (rxq->max_pkt_len <= ETHER_MAX_LEN ||
53                     rxq->max_pkt_len > ICE_FRAME_SIZE_MAX) {
54                         PMD_DRV_LOG(ERR, "maximum packet length must "
55                                     "be larger than %u and smaller than %u,"
56                                     "as jumbo frame is enabled",
57                                     (uint32_t)ETHER_MAX_LEN,
58                                     (uint32_t)ICE_FRAME_SIZE_MAX);
59                         return -EINVAL;
60                 }
61         } else {
62                 if (rxq->max_pkt_len < ETHER_MIN_LEN ||
63                     rxq->max_pkt_len > ETHER_MAX_LEN) {
64                         PMD_DRV_LOG(ERR, "maximum packet length must be "
65                                     "larger than %u and smaller than %u, "
66                                     "as jumbo frame is disabled",
67                                     (uint32_t)ETHER_MIN_LEN,
68                                     (uint32_t)ETHER_MAX_LEN);
69                         return -EINVAL;
70                 }
71         }
72
73         memset(&rx_ctx, 0, sizeof(rx_ctx));
74
75         rx_ctx.base = rxq->rx_ring_phys_addr / ICE_QUEUE_BASE_ADDR_UNIT;
76         rx_ctx.qlen = rxq->nb_rx_desc;
77         rx_ctx.dbuf = rxq->rx_buf_len >> ICE_RLAN_CTX_DBUF_S;
78         rx_ctx.hbuf = rxq->rx_hdr_len >> ICE_RLAN_CTX_HBUF_S;
79         rx_ctx.dtype = 0; /* No Header Split mode */
80 #ifndef RTE_LIBRTE_ICE_16BYTE_RX_DESC
81         rx_ctx.dsize = 1; /* 32B descriptors */
82 #endif
83         rx_ctx.rxmax = rxq->max_pkt_len;
84         /* TPH: Transaction Layer Packet (TLP) processing hints */
85         rx_ctx.tphrdesc_ena = 1;
86         rx_ctx.tphwdesc_ena = 1;
87         rx_ctx.tphdata_ena = 1;
88         rx_ctx.tphhead_ena = 1;
89         /* Low Receive Queue Threshold defined in 64 descriptors units.
90          * When the number of free descriptors goes below the lrxqthresh,
91          * an immediate interrupt is triggered.
92          */
93         rx_ctx.lrxqthresh = 2;
94         /*default use 32 byte descriptor, vlan tag extract to L2TAG2(1st)*/
95         rx_ctx.l2tsel = 1;
96         rx_ctx.showiv = 0;
97
98         err = ice_clear_rxq_ctx(hw, rxq->reg_idx);
99         if (err) {
100                 PMD_DRV_LOG(ERR, "Failed to clear Lan Rx queue (%u) context",
101                             rxq->queue_id);
102                 return -EINVAL;
103         }
104         err = ice_write_rxq_ctx(hw, &rx_ctx, rxq->reg_idx);
105         if (err) {
106                 PMD_DRV_LOG(ERR, "Failed to write Lan Rx queue (%u) context",
107                             rxq->queue_id);
108                 return -EINVAL;
109         }
110
111         buf_size = (uint16_t)(rte_pktmbuf_data_room_size(rxq->mp) -
112                               RTE_PKTMBUF_HEADROOM);
113
114         rxq->qrx_tail = hw->hw_addr + QRX_TAIL(rxq->reg_idx);
115
116         /* Init the Rx tail register*/
117         ICE_PCI_REG_WRITE(rxq->qrx_tail, rxq->nb_rx_desc - 1);
118
119         return 0;
120 }
121
122 /* Allocate mbufs for all descriptors in rx queue */
123 static int
124 ice_alloc_rx_queue_mbufs(struct ice_rx_queue *rxq)
125 {
126         struct ice_rx_entry *rxe = rxq->sw_ring;
127         uint64_t dma_addr;
128         uint16_t i;
129
130         for (i = 0; i < rxq->nb_rx_desc; i++) {
131                 volatile union ice_rx_desc *rxd;
132                 struct rte_mbuf *mbuf = rte_mbuf_raw_alloc(rxq->mp);
133
134                 if (unlikely(!mbuf)) {
135                         PMD_DRV_LOG(ERR, "Failed to allocate mbuf for RX");
136                         return -ENOMEM;
137                 }
138
139                 rte_mbuf_refcnt_set(mbuf, 1);
140                 mbuf->next = NULL;
141                 mbuf->data_off = RTE_PKTMBUF_HEADROOM;
142                 mbuf->nb_segs = 1;
143                 mbuf->port = rxq->port_id;
144
145                 dma_addr =
146                         rte_cpu_to_le_64(rte_mbuf_data_iova_default(mbuf));
147
148                 rxd = &rxq->rx_ring[i];
149                 rxd->read.pkt_addr = dma_addr;
150                 rxd->read.hdr_addr = 0;
151 #ifndef RTE_LIBRTE_ICE_16BYTE_RX_DESC
152                 rxd->read.rsvd1 = 0;
153                 rxd->read.rsvd2 = 0;
154 #endif
155                 rxe[i].mbuf = mbuf;
156         }
157
158         return 0;
159 }
160
161 /* Free all mbufs for descriptors in rx queue */
162 static void
163 ice_rx_queue_release_mbufs(struct ice_rx_queue *rxq)
164 {
165         uint16_t i;
166
167         if (!rxq || !rxq->sw_ring) {
168                 PMD_DRV_LOG(DEBUG, "Pointer to sw_ring is NULL");
169                 return;
170         }
171
172         for (i = 0; i < rxq->nb_rx_desc; i++) {
173                 if (rxq->sw_ring[i].mbuf) {
174                         rte_pktmbuf_free_seg(rxq->sw_ring[i].mbuf);
175                         rxq->sw_ring[i].mbuf = NULL;
176                 }
177         }
178 #ifdef RTE_LIBRTE_ICE_RX_ALLOW_BULK_ALLOC
179                 if (rxq->rx_nb_avail == 0)
180                         return;
181                 for (i = 0; i < rxq->rx_nb_avail; i++) {
182                         struct rte_mbuf *mbuf;
183
184                         mbuf = rxq->rx_stage[rxq->rx_next_avail + i];
185                         rte_pktmbuf_free_seg(mbuf);
186                 }
187                 rxq->rx_nb_avail = 0;
188 #endif /* RTE_LIBRTE_ICE_RX_ALLOW_BULK_ALLOC */
189 }
190
191 /* turn on or off rx queue
192  * @q_idx: queue index in pf scope
193  * @on: turn on or off the queue
194  */
195 static int
196 ice_switch_rx_queue(struct ice_hw *hw, uint16_t q_idx, bool on)
197 {
198         uint32_t reg;
199         uint16_t j;
200
201         /* QRX_CTRL = QRX_ENA */
202         reg = ICE_READ_REG(hw, QRX_CTRL(q_idx));
203
204         if (on) {
205                 if (reg & QRX_CTRL_QENA_STAT_M)
206                         return 0; /* Already on, skip */
207                 reg |= QRX_CTRL_QENA_REQ_M;
208         } else {
209                 if (!(reg & QRX_CTRL_QENA_STAT_M))
210                         return 0; /* Already off, skip */
211                 reg &= ~QRX_CTRL_QENA_REQ_M;
212         }
213
214         /* Write the register */
215         ICE_WRITE_REG(hw, QRX_CTRL(q_idx), reg);
216         /* Check the result. It is said that QENA_STAT
217          * follows the QENA_REQ not more than 10 use.
218          * TODO: need to change the wait counter later
219          */
220         for (j = 0; j < ICE_CHK_Q_ENA_COUNT; j++) {
221                 rte_delay_us(ICE_CHK_Q_ENA_INTERVAL_US);
222                 reg = ICE_READ_REG(hw, QRX_CTRL(q_idx));
223                 if (on) {
224                         if ((reg & QRX_CTRL_QENA_REQ_M) &&
225                             (reg & QRX_CTRL_QENA_STAT_M))
226                                 break;
227                 } else {
228                         if (!(reg & QRX_CTRL_QENA_REQ_M) &&
229                             !(reg & QRX_CTRL_QENA_STAT_M))
230                                 break;
231                 }
232         }
233
234         /* Check if it is timeout */
235         if (j >= ICE_CHK_Q_ENA_COUNT) {
236                 PMD_DRV_LOG(ERR, "Failed to %s rx queue[%u]",
237                             (on ? "enable" : "disable"), q_idx);
238                 return -ETIMEDOUT;
239         }
240
241         return 0;
242 }
243
244 static inline int
245 #ifdef RTE_LIBRTE_ICE_RX_ALLOW_BULK_ALLOC
246 ice_check_rx_burst_bulk_alloc_preconditions(struct ice_rx_queue *rxq)
247 #else
248 ice_check_rx_burst_bulk_alloc_preconditions
249         (__rte_unused struct ice_rx_queue *rxq)
250 #endif
251 {
252         int ret = 0;
253
254 #ifdef RTE_LIBRTE_ICE_RX_ALLOW_BULK_ALLOC
255         if (!(rxq->rx_free_thresh >= ICE_RX_MAX_BURST)) {
256                 PMD_INIT_LOG(DEBUG, "Rx Burst Bulk Alloc Preconditions: "
257                              "rxq->rx_free_thresh=%d, "
258                              "ICE_RX_MAX_BURST=%d",
259                              rxq->rx_free_thresh, ICE_RX_MAX_BURST);
260                 ret = -EINVAL;
261         } else if (!(rxq->rx_free_thresh < rxq->nb_rx_desc)) {
262                 PMD_INIT_LOG(DEBUG, "Rx Burst Bulk Alloc Preconditions: "
263                              "rxq->rx_free_thresh=%d, "
264                              "rxq->nb_rx_desc=%d",
265                              rxq->rx_free_thresh, rxq->nb_rx_desc);
266                 ret = -EINVAL;
267         } else if (rxq->nb_rx_desc % rxq->rx_free_thresh != 0) {
268                 PMD_INIT_LOG(DEBUG, "Rx Burst Bulk Alloc Preconditions: "
269                              "rxq->nb_rx_desc=%d, "
270                              "rxq->rx_free_thresh=%d",
271                              rxq->nb_rx_desc, rxq->rx_free_thresh);
272                 ret = -EINVAL;
273         }
274 #else
275         ret = -EINVAL;
276 #endif
277
278         return ret;
279 }
280
281 /* reset fields in ice_rx_queue back to default */
282 static void
283 ice_reset_rx_queue(struct ice_rx_queue *rxq)
284 {
285         unsigned int i;
286         uint16_t len;
287
288         if (!rxq) {
289                 PMD_DRV_LOG(DEBUG, "Pointer to rxq is NULL");
290                 return;
291         }
292
293 #ifdef RTE_LIBRTE_ICE_RX_ALLOW_BULK_ALLOC
294         if (ice_check_rx_burst_bulk_alloc_preconditions(rxq) == 0)
295                 len = (uint16_t)(rxq->nb_rx_desc + ICE_RX_MAX_BURST);
296         else
297 #endif /* RTE_LIBRTE_ICE_RX_ALLOW_BULK_ALLOC */
298                 len = rxq->nb_rx_desc;
299
300         for (i = 0; i < len * sizeof(union ice_rx_desc); i++)
301                 ((volatile char *)rxq->rx_ring)[i] = 0;
302
303 #ifdef RTE_LIBRTE_ICE_RX_ALLOW_BULK_ALLOC
304         memset(&rxq->fake_mbuf, 0x0, sizeof(rxq->fake_mbuf));
305         for (i = 0; i < ICE_RX_MAX_BURST; ++i)
306                 rxq->sw_ring[rxq->nb_rx_desc + i].mbuf = &rxq->fake_mbuf;
307
308         rxq->rx_nb_avail = 0;
309         rxq->rx_next_avail = 0;
310         rxq->rx_free_trigger = (uint16_t)(rxq->rx_free_thresh - 1);
311 #endif /* RTE_LIBRTE_ICE_RX_ALLOW_BULK_ALLOC */
312
313         rxq->rx_tail = 0;
314         rxq->nb_rx_hold = 0;
315         rxq->pkt_first_seg = NULL;
316         rxq->pkt_last_seg = NULL;
317 }
318
319 int
320 ice_rx_queue_start(struct rte_eth_dev *dev, uint16_t rx_queue_id)
321 {
322         struct ice_rx_queue *rxq;
323         int err;
324         struct ice_hw *hw = ICE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
325
326         PMD_INIT_FUNC_TRACE();
327
328         if (rx_queue_id >= dev->data->nb_rx_queues) {
329                 PMD_DRV_LOG(ERR, "RX queue %u is out of range %u",
330                             rx_queue_id, dev->data->nb_rx_queues);
331                 return -EINVAL;
332         }
333
334         rxq = dev->data->rx_queues[rx_queue_id];
335         if (!rxq || !rxq->q_set) {
336                 PMD_DRV_LOG(ERR, "RX queue %u not available or setup",
337                             rx_queue_id);
338                 return -EINVAL;
339         }
340
341         err = ice_program_hw_rx_queue(rxq);
342         if (err) {
343                 PMD_DRV_LOG(ERR, "fail to program RX queue %u",
344                             rx_queue_id);
345                 return -EIO;
346         }
347
348         err = ice_alloc_rx_queue_mbufs(rxq);
349         if (err) {
350                 PMD_DRV_LOG(ERR, "Failed to allocate RX queue mbuf");
351                 return -ENOMEM;
352         }
353
354         rte_wmb();
355
356         /* Init the RX tail register. */
357         ICE_PCI_REG_WRITE(rxq->qrx_tail, rxq->nb_rx_desc - 1);
358
359         err = ice_switch_rx_queue(hw, rxq->reg_idx, TRUE);
360         if (err) {
361                 PMD_DRV_LOG(ERR, "Failed to switch RX queue %u on",
362                             rx_queue_id);
363
364                 ice_rx_queue_release_mbufs(rxq);
365                 ice_reset_rx_queue(rxq);
366                 return -EINVAL;
367         }
368
369         dev->data->rx_queue_state[rx_queue_id] =
370                 RTE_ETH_QUEUE_STATE_STARTED;
371
372         return 0;
373 }
374
375 int
376 ice_rx_queue_stop(struct rte_eth_dev *dev, uint16_t rx_queue_id)
377 {
378         struct ice_rx_queue *rxq;
379         int err;
380         struct ice_hw *hw = ICE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
381
382         if (rx_queue_id < dev->data->nb_rx_queues) {
383                 rxq = dev->data->rx_queues[rx_queue_id];
384
385                 err = ice_switch_rx_queue(hw, rxq->reg_idx, FALSE);
386                 if (err) {
387                         PMD_DRV_LOG(ERR, "Failed to switch RX queue %u off",
388                                     rx_queue_id);
389                         return -EINVAL;
390                 }
391                 ice_rx_queue_release_mbufs(rxq);
392                 ice_reset_rx_queue(rxq);
393                 dev->data->rx_queue_state[rx_queue_id] =
394                         RTE_ETH_QUEUE_STATE_STOPPED;
395         }
396
397         return 0;
398 }
399
400 int
401 ice_tx_queue_start(struct rte_eth_dev *dev, uint16_t tx_queue_id)
402 {
403         struct ice_tx_queue *txq;
404         int err;
405         struct ice_vsi *vsi;
406         struct ice_hw *hw;
407         struct ice_aqc_add_tx_qgrp txq_elem;
408         struct ice_tlan_ctx tx_ctx;
409
410         PMD_INIT_FUNC_TRACE();
411
412         if (tx_queue_id >= dev->data->nb_tx_queues) {
413                 PMD_DRV_LOG(ERR, "TX queue %u is out of range %u",
414                             tx_queue_id, dev->data->nb_tx_queues);
415                 return -EINVAL;
416         }
417
418         txq = dev->data->tx_queues[tx_queue_id];
419         if (!txq || !txq->q_set) {
420                 PMD_DRV_LOG(ERR, "TX queue %u is not available or setup",
421                             tx_queue_id);
422                 return -EINVAL;
423         }
424
425         vsi = txq->vsi;
426         hw = ICE_VSI_TO_HW(vsi);
427
428         memset(&txq_elem, 0, sizeof(txq_elem));
429         memset(&tx_ctx, 0, sizeof(tx_ctx));
430         txq_elem.num_txqs = 1;
431         txq_elem.txqs[0].txq_id = rte_cpu_to_le_16(txq->reg_idx);
432
433         tx_ctx.base = txq->tx_ring_phys_addr / ICE_QUEUE_BASE_ADDR_UNIT;
434         tx_ctx.qlen = txq->nb_tx_desc;
435         tx_ctx.pf_num = hw->pf_id;
436         tx_ctx.vmvf_type = ICE_TLAN_CTX_VMVF_TYPE_PF;
437         tx_ctx.src_vsi = vsi->vsi_id;
438         tx_ctx.port_num = hw->port_info->lport;
439         tx_ctx.tso_ena = 1; /* tso enable */
440         tx_ctx.tso_qnum = txq->reg_idx; /* index for tso state structure */
441         tx_ctx.legacy_int = 1; /* Legacy or Advanced Host Interface */
442
443         ice_set_ctx((uint8_t *)&tx_ctx, txq_elem.txqs[0].txq_ctx,
444                     ice_tlan_ctx_info);
445
446         txq->qtx_tail = hw->hw_addr + QTX_COMM_DBELL(txq->reg_idx);
447
448         /* Init the Tx tail register*/
449         ICE_PCI_REG_WRITE(txq->qtx_tail, 0);
450
451         err = ice_ena_vsi_txq(hw->port_info, vsi->idx, 0, 1, &txq_elem,
452                               sizeof(txq_elem), NULL);
453         if (err) {
454                 PMD_DRV_LOG(ERR, "Failed to add lan txq");
455                 return -EIO;
456         }
457         /* store the schedule node id */
458         txq->q_teid = txq_elem.txqs[0].q_teid;
459
460         dev->data->tx_queue_state[tx_queue_id] = RTE_ETH_QUEUE_STATE_STARTED;
461         return 0;
462 }
463
464 /* Free all mbufs for descriptors in tx queue */
465 static void
466 ice_tx_queue_release_mbufs(struct ice_tx_queue *txq)
467 {
468         uint16_t i;
469
470         if (!txq || !txq->sw_ring) {
471                 PMD_DRV_LOG(DEBUG, "Pointer to txq or sw_ring is NULL");
472                 return;
473         }
474
475         for (i = 0; i < txq->nb_tx_desc; i++) {
476                 if (txq->sw_ring[i].mbuf) {
477                         rte_pktmbuf_free_seg(txq->sw_ring[i].mbuf);
478                         txq->sw_ring[i].mbuf = NULL;
479                 }
480         }
481 }
482
483 static void
484 ice_reset_tx_queue(struct ice_tx_queue *txq)
485 {
486         struct ice_tx_entry *txe;
487         uint16_t i, prev, size;
488
489         if (!txq) {
490                 PMD_DRV_LOG(DEBUG, "Pointer to txq is NULL");
491                 return;
492         }
493
494         txe = txq->sw_ring;
495         size = sizeof(struct ice_tx_desc) * txq->nb_tx_desc;
496         for (i = 0; i < size; i++)
497                 ((volatile char *)txq->tx_ring)[i] = 0;
498
499         prev = (uint16_t)(txq->nb_tx_desc - 1);
500         for (i = 0; i < txq->nb_tx_desc; i++) {
501                 volatile struct ice_tx_desc *txd = &txq->tx_ring[i];
502
503                 txd->cmd_type_offset_bsz =
504                         rte_cpu_to_le_64(ICE_TX_DESC_DTYPE_DESC_DONE);
505                 txe[i].mbuf =  NULL;
506                 txe[i].last_id = i;
507                 txe[prev].next_id = i;
508                 prev = i;
509         }
510
511         txq->tx_next_dd = (uint16_t)(txq->tx_rs_thresh - 1);
512         txq->tx_next_rs = (uint16_t)(txq->tx_rs_thresh - 1);
513
514         txq->tx_tail = 0;
515         txq->nb_tx_used = 0;
516
517         txq->last_desc_cleaned = (uint16_t)(txq->nb_tx_desc - 1);
518         txq->nb_tx_free = (uint16_t)(txq->nb_tx_desc - 1);
519 }
520
521 int
522 ice_tx_queue_stop(struct rte_eth_dev *dev, uint16_t tx_queue_id)
523 {
524         struct ice_tx_queue *txq;
525         struct ice_hw *hw = ICE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
526         enum ice_status status;
527         uint16_t q_ids[1];
528         uint32_t q_teids[1];
529
530         if (tx_queue_id >= dev->data->nb_tx_queues) {
531                 PMD_DRV_LOG(ERR, "TX queue %u is out of range %u",
532                             tx_queue_id, dev->data->nb_tx_queues);
533                 return -EINVAL;
534         }
535
536         txq = dev->data->tx_queues[tx_queue_id];
537         if (!txq) {
538                 PMD_DRV_LOG(ERR, "TX queue %u is not available",
539                             tx_queue_id);
540                 return -EINVAL;
541         }
542
543         q_ids[0] = txq->reg_idx;
544         q_teids[0] = txq->q_teid;
545
546         status = ice_dis_vsi_txq(hw->port_info, 1, q_ids, q_teids,
547                                  ICE_NO_RESET, 0, NULL);
548         if (status != ICE_SUCCESS) {
549                 PMD_DRV_LOG(DEBUG, "Failed to disable Lan Tx queue");
550                 return -EINVAL;
551         }
552
553         ice_tx_queue_release_mbufs(txq);
554         ice_reset_tx_queue(txq);
555         dev->data->tx_queue_state[tx_queue_id] = RTE_ETH_QUEUE_STATE_STOPPED;
556
557         return 0;
558 }
559
560 int
561 ice_rx_queue_setup(struct rte_eth_dev *dev,
562                    uint16_t queue_idx,
563                    uint16_t nb_desc,
564                    unsigned int socket_id,
565                    const struct rte_eth_rxconf *rx_conf,
566                    struct rte_mempool *mp)
567 {
568         struct ice_pf *pf = ICE_DEV_PRIVATE_TO_PF(dev->data->dev_private);
569         struct ice_adapter *ad =
570                 ICE_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
571         struct ice_vsi *vsi = pf->main_vsi;
572         struct ice_rx_queue *rxq;
573         const struct rte_memzone *rz;
574         uint32_t ring_size;
575         uint16_t len;
576         int use_def_burst_func = 1;
577
578         if (nb_desc % ICE_ALIGN_RING_DESC != 0 ||
579             nb_desc > ICE_MAX_RING_DESC ||
580             nb_desc < ICE_MIN_RING_DESC) {
581                 PMD_INIT_LOG(ERR, "Number (%u) of receive descriptors is "
582                              "invalid", nb_desc);
583                 return -EINVAL;
584         }
585
586         /* Free memory if needed */
587         if (dev->data->rx_queues[queue_idx]) {
588                 ice_rx_queue_release(dev->data->rx_queues[queue_idx]);
589                 dev->data->rx_queues[queue_idx] = NULL;
590         }
591
592         /* Allocate the rx queue data structure */
593         rxq = rte_zmalloc_socket(NULL,
594                                  sizeof(struct ice_rx_queue),
595                                  RTE_CACHE_LINE_SIZE,
596                                  socket_id);
597         if (!rxq) {
598                 PMD_INIT_LOG(ERR, "Failed to allocate memory for "
599                              "rx queue data structure");
600                 return -ENOMEM;
601         }
602         rxq->mp = mp;
603         rxq->nb_rx_desc = nb_desc;
604         rxq->rx_free_thresh = rx_conf->rx_free_thresh;
605         rxq->queue_id = queue_idx;
606
607         rxq->reg_idx = vsi->base_queue + queue_idx;
608         rxq->port_id = dev->data->port_id;
609         if (dev->data->dev_conf.rxmode.offloads & DEV_RX_OFFLOAD_KEEP_CRC)
610                 rxq->crc_len = ETHER_CRC_LEN;
611         else
612                 rxq->crc_len = 0;
613
614         rxq->drop_en = rx_conf->rx_drop_en;
615         rxq->vsi = vsi;
616         rxq->rx_deferred_start = rx_conf->rx_deferred_start;
617
618         /* Allocate the maximun number of RX ring hardware descriptor. */
619         len = ICE_MAX_RING_DESC;
620
621 #ifdef RTE_LIBRTE_ICE_RX_ALLOW_BULK_ALLOC
622         /**
623          * Allocating a little more memory because vectorized/bulk_alloc Rx
624          * functions doesn't check boundaries each time.
625          */
626         len += ICE_RX_MAX_BURST;
627 #endif
628
629         /* Allocate the maximum number of RX ring hardware descriptor. */
630         ring_size = sizeof(union ice_rx_desc) * len;
631         ring_size = RTE_ALIGN(ring_size, ICE_DMA_MEM_ALIGN);
632         rz = rte_eth_dma_zone_reserve(dev, "rx_ring", queue_idx,
633                                       ring_size, ICE_RING_BASE_ALIGN,
634                                       socket_id);
635         if (!rz) {
636                 ice_rx_queue_release(rxq);
637                 PMD_INIT_LOG(ERR, "Failed to reserve DMA memory for RX");
638                 return -ENOMEM;
639         }
640
641         /* Zero all the descriptors in the ring. */
642         memset(rz->addr, 0, ring_size);
643
644         rxq->rx_ring_phys_addr = rz->phys_addr;
645         rxq->rx_ring = (union ice_rx_desc *)rz->addr;
646
647 #ifdef RTE_LIBRTE_ICE_RX_ALLOW_BULK_ALLOC
648         len = (uint16_t)(nb_desc + ICE_RX_MAX_BURST);
649 #else
650         len = nb_desc;
651 #endif
652
653         /* Allocate the software ring. */
654         rxq->sw_ring = rte_zmalloc_socket(NULL,
655                                           sizeof(struct ice_rx_entry) * len,
656                                           RTE_CACHE_LINE_SIZE,
657                                           socket_id);
658         if (!rxq->sw_ring) {
659                 ice_rx_queue_release(rxq);
660                 PMD_INIT_LOG(ERR, "Failed to allocate memory for SW ring");
661                 return -ENOMEM;
662         }
663
664         ice_reset_rx_queue(rxq);
665         rxq->q_set = TRUE;
666         dev->data->rx_queues[queue_idx] = rxq;
667
668         use_def_burst_func = ice_check_rx_burst_bulk_alloc_preconditions(rxq);
669
670         if (!use_def_burst_func) {
671 #ifdef RTE_LIBRTE_ICE_RX_ALLOW_BULK_ALLOC
672                 PMD_INIT_LOG(DEBUG, "Rx Burst Bulk Alloc Preconditions are "
673                              "satisfied. Rx Burst Bulk Alloc function will be "
674                              "used on port=%d, queue=%d.",
675                              rxq->port_id, rxq->queue_id);
676 #endif /* RTE_LIBRTE_ICE_RX_ALLOW_BULK_ALLOC */
677         } else {
678                 PMD_INIT_LOG(DEBUG, "Rx Burst Bulk Alloc Preconditions are "
679                              "not satisfied, Scattered Rx is requested, "
680                              "or RTE_LIBRTE_ICE_RX_ALLOW_BULK_ALLOC is "
681                              "not enabled on port=%d, queue=%d.",
682                              rxq->port_id, rxq->queue_id);
683                 ad->rx_bulk_alloc_allowed = false;
684         }
685
686         return 0;
687 }
688
689 void
690 ice_rx_queue_release(void *rxq)
691 {
692         struct ice_rx_queue *q = (struct ice_rx_queue *)rxq;
693
694         if (!q) {
695                 PMD_DRV_LOG(DEBUG, "Pointer to rxq is NULL");
696                 return;
697         }
698
699         ice_rx_queue_release_mbufs(q);
700         rte_free(q->sw_ring);
701         rte_free(q);
702 }
703
704 int
705 ice_tx_queue_setup(struct rte_eth_dev *dev,
706                    uint16_t queue_idx,
707                    uint16_t nb_desc,
708                    unsigned int socket_id,
709                    const struct rte_eth_txconf *tx_conf)
710 {
711         struct ice_pf *pf = ICE_DEV_PRIVATE_TO_PF(dev->data->dev_private);
712         struct ice_vsi *vsi = pf->main_vsi;
713         struct ice_tx_queue *txq;
714         const struct rte_memzone *tz;
715         uint32_t ring_size;
716         uint16_t tx_rs_thresh, tx_free_thresh;
717         uint64_t offloads;
718
719         offloads = tx_conf->offloads | dev->data->dev_conf.txmode.offloads;
720
721         if (nb_desc % ICE_ALIGN_RING_DESC != 0 ||
722             nb_desc > ICE_MAX_RING_DESC ||
723             nb_desc < ICE_MIN_RING_DESC) {
724                 PMD_INIT_LOG(ERR, "Number (%u) of transmit descriptors is "
725                              "invalid", nb_desc);
726                 return -EINVAL;
727         }
728
729         /**
730          * The following two parameters control the setting of the RS bit on
731          * transmit descriptors. TX descriptors will have their RS bit set
732          * after txq->tx_rs_thresh descriptors have been used. The TX
733          * descriptor ring will be cleaned after txq->tx_free_thresh
734          * descriptors are used or if the number of descriptors required to
735          * transmit a packet is greater than the number of free TX descriptors.
736          *
737          * The following constraints must be satisfied:
738          *  - tx_rs_thresh must be greater than 0.
739          *  - tx_rs_thresh must be less than the size of the ring minus 2.
740          *  - tx_rs_thresh must be less than or equal to tx_free_thresh.
741          *  - tx_rs_thresh must be a divisor of the ring size.
742          *  - tx_free_thresh must be greater than 0.
743          *  - tx_free_thresh must be less than the size of the ring minus 3.
744          *
745          * One descriptor in the TX ring is used as a sentinel to avoid a H/W
746          * race condition, hence the maximum threshold constraints. When set
747          * to zero use default values.
748          */
749         tx_rs_thresh = (uint16_t)(tx_conf->tx_rs_thresh ?
750                                   tx_conf->tx_rs_thresh :
751                                   ICE_DEFAULT_TX_RSBIT_THRESH);
752         tx_free_thresh = (uint16_t)(tx_conf->tx_free_thresh ?
753                                     tx_conf->tx_free_thresh :
754                                     ICE_DEFAULT_TX_FREE_THRESH);
755         if (tx_rs_thresh >= (nb_desc - 2)) {
756                 PMD_INIT_LOG(ERR, "tx_rs_thresh must be less than the "
757                              "number of TX descriptors minus 2. "
758                              "(tx_rs_thresh=%u port=%d queue=%d)",
759                              (unsigned int)tx_rs_thresh,
760                              (int)dev->data->port_id,
761                              (int)queue_idx);
762                 return -EINVAL;
763         }
764         if (tx_free_thresh >= (nb_desc - 3)) {
765                 PMD_INIT_LOG(ERR, "tx_rs_thresh must be less than the "
766                              "tx_free_thresh must be less than the "
767                              "number of TX descriptors minus 3. "
768                              "(tx_free_thresh=%u port=%d queue=%d)",
769                              (unsigned int)tx_free_thresh,
770                              (int)dev->data->port_id,
771                              (int)queue_idx);
772                 return -EINVAL;
773         }
774         if (tx_rs_thresh > tx_free_thresh) {
775                 PMD_INIT_LOG(ERR, "tx_rs_thresh must be less than or "
776                              "equal to tx_free_thresh. (tx_free_thresh=%u"
777                              " tx_rs_thresh=%u port=%d queue=%d)",
778                              (unsigned int)tx_free_thresh,
779                              (unsigned int)tx_rs_thresh,
780                              (int)dev->data->port_id,
781                              (int)queue_idx);
782                 return -EINVAL;
783         }
784         if ((nb_desc % tx_rs_thresh) != 0) {
785                 PMD_INIT_LOG(ERR, "tx_rs_thresh must be a divisor of the "
786                              "number of TX descriptors. (tx_rs_thresh=%u"
787                              " port=%d queue=%d)",
788                              (unsigned int)tx_rs_thresh,
789                              (int)dev->data->port_id,
790                              (int)queue_idx);
791                 return -EINVAL;
792         }
793         if (tx_rs_thresh > 1 && tx_conf->tx_thresh.wthresh != 0) {
794                 PMD_INIT_LOG(ERR, "TX WTHRESH must be set to 0 if "
795                              "tx_rs_thresh is greater than 1. "
796                              "(tx_rs_thresh=%u port=%d queue=%d)",
797                              (unsigned int)tx_rs_thresh,
798                              (int)dev->data->port_id,
799                              (int)queue_idx);
800                 return -EINVAL;
801         }
802
803         /* Free memory if needed. */
804         if (dev->data->tx_queues[queue_idx]) {
805                 ice_tx_queue_release(dev->data->tx_queues[queue_idx]);
806                 dev->data->tx_queues[queue_idx] = NULL;
807         }
808
809         /* Allocate the TX queue data structure. */
810         txq = rte_zmalloc_socket(NULL,
811                                  sizeof(struct ice_tx_queue),
812                                  RTE_CACHE_LINE_SIZE,
813                                  socket_id);
814         if (!txq) {
815                 PMD_INIT_LOG(ERR, "Failed to allocate memory for "
816                              "tx queue structure");
817                 return -ENOMEM;
818         }
819
820         /* Allocate TX hardware ring descriptors. */
821         ring_size = sizeof(struct ice_tx_desc) * ICE_MAX_RING_DESC;
822         ring_size = RTE_ALIGN(ring_size, ICE_DMA_MEM_ALIGN);
823         tz = rte_eth_dma_zone_reserve(dev, "tx_ring", queue_idx,
824                                       ring_size, ICE_RING_BASE_ALIGN,
825                                       socket_id);
826         if (!tz) {
827                 ice_tx_queue_release(txq);
828                 PMD_INIT_LOG(ERR, "Failed to reserve DMA memory for TX");
829                 return -ENOMEM;
830         }
831
832         txq->nb_tx_desc = nb_desc;
833         txq->tx_rs_thresh = tx_rs_thresh;
834         txq->tx_free_thresh = tx_free_thresh;
835         txq->pthresh = tx_conf->tx_thresh.pthresh;
836         txq->hthresh = tx_conf->tx_thresh.hthresh;
837         txq->wthresh = tx_conf->tx_thresh.wthresh;
838         txq->queue_id = queue_idx;
839
840         txq->reg_idx = vsi->base_queue + queue_idx;
841         txq->port_id = dev->data->port_id;
842         txq->offloads = offloads;
843         txq->vsi = vsi;
844         txq->tx_deferred_start = tx_conf->tx_deferred_start;
845
846         txq->tx_ring_phys_addr = tz->phys_addr;
847         txq->tx_ring = (struct ice_tx_desc *)tz->addr;
848
849         /* Allocate software ring */
850         txq->sw_ring =
851                 rte_zmalloc_socket(NULL,
852                                    sizeof(struct ice_tx_entry) * nb_desc,
853                                    RTE_CACHE_LINE_SIZE,
854                                    socket_id);
855         if (!txq->sw_ring) {
856                 ice_tx_queue_release(txq);
857                 PMD_INIT_LOG(ERR, "Failed to allocate memory for SW TX ring");
858                 return -ENOMEM;
859         }
860
861         ice_reset_tx_queue(txq);
862         txq->q_set = TRUE;
863         dev->data->tx_queues[queue_idx] = txq;
864
865         return 0;
866 }
867
868 void
869 ice_tx_queue_release(void *txq)
870 {
871         struct ice_tx_queue *q = (struct ice_tx_queue *)txq;
872
873         if (!q) {
874                 PMD_DRV_LOG(DEBUG, "Pointer to TX queue is NULL");
875                 return;
876         }
877
878         ice_tx_queue_release_mbufs(q);
879         rte_free(q->sw_ring);
880         rte_free(q);
881 }
882
883 void
884 ice_clear_queues(struct rte_eth_dev *dev)
885 {
886         uint16_t i;
887
888         PMD_INIT_FUNC_TRACE();
889
890         for (i = 0; i < dev->data->nb_tx_queues; i++) {
891                 ice_tx_queue_release_mbufs(dev->data->tx_queues[i]);
892                 ice_reset_tx_queue(dev->data->tx_queues[i]);
893         }
894
895         for (i = 0; i < dev->data->nb_rx_queues; i++) {
896                 ice_rx_queue_release_mbufs(dev->data->rx_queues[i]);
897                 ice_reset_rx_queue(dev->data->rx_queues[i]);
898         }
899 }
900
901 void
902 ice_free_queues(struct rte_eth_dev *dev)
903 {
904         uint16_t i;
905
906         PMD_INIT_FUNC_TRACE();
907
908         for (i = 0; i < dev->data->nb_rx_queues; i++) {
909                 if (!dev->data->rx_queues[i])
910                         continue;
911                 ice_rx_queue_release(dev->data->rx_queues[i]);
912                 dev->data->rx_queues[i] = NULL;
913         }
914         dev->data->nb_rx_queues = 0;
915
916         for (i = 0; i < dev->data->nb_tx_queues; i++) {
917                 if (!dev->data->tx_queues[i])
918                         continue;
919                 ice_tx_queue_release(dev->data->tx_queues[i]);
920                 dev->data->tx_queues[i] = NULL;
921         }
922         dev->data->nb_tx_queues = 0;
923 }