20c1cf820c1b57a3ae9f1656f3340f00f098f3fa
[dpdk.git] / drivers / net / qede / qede_rxtx.c
1 /*
2  * Copyright (c) 2016 QLogic Corporation.
3  * All rights reserved.
4  * www.qlogic.com
5  *
6  * See LICENSE.qede_pmd for copyright and licensing details.
7  */
8
9 #include "qede_rxtx.h"
10
11 static bool gro_disable = 1;    /* mod_param */
12
13 static inline struct
14 rte_mbuf *qede_rxmbuf_alloc(struct rte_mempool *mp)
15 {
16         struct rte_mbuf *m;
17
18         m = __rte_mbuf_raw_alloc(mp);
19         __rte_mbuf_sanity_check(m, 0);
20
21         return m;
22 }
23
24 static inline int qede_alloc_rx_buffer(struct qede_rx_queue *rxq)
25 {
26         struct rte_mbuf *new_mb = NULL;
27         struct eth_rx_bd *rx_bd;
28         dma_addr_t mapping;
29         uint16_t idx = rxq->sw_rx_prod & NUM_RX_BDS(rxq);
30
31         new_mb = qede_rxmbuf_alloc(rxq->mb_pool);
32         if (unlikely(!new_mb)) {
33                 PMD_RX_LOG(ERR, rxq,
34                            "Failed to allocate rx buffer "
35                            "sw_rx_prod %u sw_rx_cons %u mp entries %u free %u",
36                            idx, rxq->sw_rx_cons & NUM_RX_BDS(rxq),
37                            rte_mempool_count(rxq->mb_pool),
38                            rte_mempool_free_count(rxq->mb_pool));
39                 return -ENOMEM;
40         }
41         rxq->sw_rx_ring[idx].mbuf = new_mb;
42         rxq->sw_rx_ring[idx].page_offset = 0;
43         mapping = rte_mbuf_data_dma_addr_default(new_mb);
44         /* Advance PROD and get BD pointer */
45         rx_bd = (struct eth_rx_bd *)ecore_chain_produce(&rxq->rx_bd_ring);
46         rx_bd->addr.hi = rte_cpu_to_le_32(U64_HI(mapping));
47         rx_bd->addr.lo = rte_cpu_to_le_32(U64_LO(mapping));
48         rxq->sw_rx_prod++;
49         return 0;
50 }
51
52 static void qede_rx_queue_release_mbufs(struct qede_rx_queue *rxq)
53 {
54         uint16_t i;
55
56         if (rxq->sw_rx_ring != NULL) {
57                 for (i = 0; i < rxq->nb_rx_desc; i++) {
58                         if (rxq->sw_rx_ring[i].mbuf != NULL) {
59                                 rte_pktmbuf_free(rxq->sw_rx_ring[i].mbuf);
60                                 rxq->sw_rx_ring[i].mbuf = NULL;
61                         }
62                 }
63         }
64 }
65
66 void qede_rx_queue_release(void *rx_queue)
67 {
68         struct qede_rx_queue *rxq = rx_queue;
69
70         if (rxq != NULL) {
71                 qede_rx_queue_release_mbufs(rxq);
72                 rte_free(rxq->sw_rx_ring);
73                 rxq->sw_rx_ring = NULL;
74                 rte_free(rxq);
75                 rx_queue = NULL;
76         }
77 }
78
79 int
80 qede_rx_queue_setup(struct rte_eth_dev *dev, uint16_t queue_idx,
81                     uint16_t nb_desc, unsigned int socket_id,
82                     const struct rte_eth_rxconf *rx_conf,
83                     struct rte_mempool *mp)
84 {
85         struct qede_dev *qdev = dev->data->dev_private;
86         struct ecore_dev *edev = &qdev->edev;
87         struct rte_eth_dev_data *eth_data = dev->data;
88         struct qede_rx_queue *rxq;
89         uint16_t pkt_len = (uint16_t)dev->data->dev_conf.rxmode.max_rx_pkt_len;
90         size_t size;
91         uint16_t data_size;
92         int rc;
93         int i;
94
95         PMD_INIT_FUNC_TRACE(edev);
96
97         /* Note: Ring size/align is controlled by struct rte_eth_desc_lim */
98         if (!rte_is_power_of_2(nb_desc)) {
99                 DP_ERR(edev, "Ring size %u is not power of 2\n",
100                           nb_desc);
101                 return -EINVAL;
102         }
103
104         /* Free memory prior to re-allocation if needed... */
105         if (dev->data->rx_queues[queue_idx] != NULL) {
106                 qede_rx_queue_release(dev->data->rx_queues[queue_idx]);
107                 dev->data->rx_queues[queue_idx] = NULL;
108         }
109
110         /* First allocate the rx queue data structure */
111         rxq = rte_zmalloc_socket("qede_rx_queue", sizeof(struct qede_rx_queue),
112                                  RTE_CACHE_LINE_SIZE, socket_id);
113
114         if (!rxq) {
115                 DP_ERR(edev, "Unable to allocate memory for rxq on socket %u",
116                           socket_id);
117                 return -ENOMEM;
118         }
119
120         rxq->qdev = qdev;
121         rxq->mb_pool = mp;
122         rxq->nb_rx_desc = nb_desc;
123         rxq->queue_id = queue_idx;
124         rxq->port_id = dev->data->port_id;
125
126         /* Sanity check */
127         data_size = (uint16_t)rte_pktmbuf_data_room_size(mp) -
128                                 RTE_PKTMBUF_HEADROOM;
129
130         if (pkt_len > data_size) {
131                 DP_ERR(edev, "MTU %u should not exceed dataroom %u\n",
132                        pkt_len, data_size);
133                 rte_free(rxq);
134                 return -EINVAL;
135         }
136
137         qdev->mtu = pkt_len;
138         rxq->rx_buf_size = pkt_len + QEDE_ETH_OVERHEAD;
139
140         DP_INFO(edev, "MTU = %u ; RX buffer = %u\n",
141                 qdev->mtu, rxq->rx_buf_size);
142
143         if (pkt_len > ETHER_MAX_LEN) {
144                 dev->data->dev_conf.rxmode.jumbo_frame = 1;
145                 DP_NOTICE(edev, false, "jumbo frame enabled\n");
146         } else {
147                 dev->data->dev_conf.rxmode.jumbo_frame = 0;
148         }
149
150         /* Allocate the parallel driver ring for Rx buffers */
151         size = sizeof(*rxq->sw_rx_ring) * rxq->nb_rx_desc;
152         rxq->sw_rx_ring = rte_zmalloc_socket("sw_rx_ring", size,
153                                              RTE_CACHE_LINE_SIZE, socket_id);
154         if (!rxq->sw_rx_ring) {
155                 DP_NOTICE(edev, false,
156                           "Unable to alloc memory for sw_rx_ring on socket %u\n",
157                           socket_id);
158                 rte_free(rxq);
159                 rxq = NULL;
160                 return -ENOMEM;
161         }
162
163         /* Allocate FW Rx ring  */
164         rc = qdev->ops->common->chain_alloc(edev,
165                                             ECORE_CHAIN_USE_TO_CONSUME_PRODUCE,
166                                             ECORE_CHAIN_MODE_NEXT_PTR,
167                                             ECORE_CHAIN_CNT_TYPE_U16,
168                                             rxq->nb_rx_desc,
169                                             sizeof(struct eth_rx_bd),
170                                             &rxq->rx_bd_ring);
171
172         if (rc != ECORE_SUCCESS) {
173                 DP_NOTICE(edev, false,
174                           "Unable to alloc memory for rxbd ring on socket %u\n",
175                           socket_id);
176                 rte_free(rxq->sw_rx_ring);
177                 rxq->sw_rx_ring = NULL;
178                 rte_free(rxq);
179                 rxq = NULL;
180         }
181
182         /* Allocate FW completion ring */
183         rc = qdev->ops->common->chain_alloc(edev,
184                                             ECORE_CHAIN_USE_TO_CONSUME,
185                                             ECORE_CHAIN_MODE_PBL,
186                                             ECORE_CHAIN_CNT_TYPE_U16,
187                                             rxq->nb_rx_desc,
188                                             sizeof(union eth_rx_cqe),
189                                             &rxq->rx_comp_ring);
190
191         if (rc != ECORE_SUCCESS) {
192                 DP_NOTICE(edev, false,
193                           "Unable to alloc memory for cqe ring on socket %u\n",
194                           socket_id);
195                 /* TBD: Freeing RX BD ring */
196                 rte_free(rxq->sw_rx_ring);
197                 rxq->sw_rx_ring = NULL;
198                 rte_free(rxq);
199         }
200
201         /* Allocate buffers for the Rx ring */
202         for (i = 0; i < rxq->nb_rx_desc; i++) {
203                 rc = qede_alloc_rx_buffer(rxq);
204                 if (rc) {
205                         DP_NOTICE(edev, false,
206                                   "RX buffer allocation failed at idx=%d\n", i);
207                         goto err4;
208                 }
209         }
210
211         dev->data->rx_queues[queue_idx] = rxq;
212         if (!qdev->rx_queues)
213                 qdev->rx_queues = (struct qede_rx_queue **)dev->data->rx_queues;
214
215         DP_INFO(edev, "rxq %d num_desc %u rx_buf_size=%u socket %u\n",
216                   queue_idx, nb_desc, qdev->mtu, socket_id);
217
218         return 0;
219 err4:
220         qede_rx_queue_release(rxq);
221         return -ENOMEM;
222 }
223
224 static void qede_tx_queue_release_mbufs(struct qede_tx_queue *txq)
225 {
226         unsigned int i;
227
228         PMD_TX_LOG(DEBUG, txq, "releasing %u mbufs\n", txq->nb_tx_desc);
229
230         if (txq->sw_tx_ring != NULL) {
231                 for (i = 0; i < txq->nb_tx_desc; i++) {
232                         if (txq->sw_tx_ring[i].mbuf != NULL) {
233                                 rte_pktmbuf_free(txq->sw_tx_ring[i].mbuf);
234                                 txq->sw_tx_ring[i].mbuf = NULL;
235                         }
236                 }
237         }
238 }
239
240 void qede_tx_queue_release(void *tx_queue)
241 {
242         struct qede_tx_queue *txq = tx_queue;
243
244         if (txq != NULL) {
245                 qede_tx_queue_release_mbufs(txq);
246                 if (txq->sw_tx_ring) {
247                         rte_free(txq->sw_tx_ring);
248                         txq->sw_tx_ring = NULL;
249                 }
250                 rte_free(txq);
251         }
252         tx_queue = NULL;
253 }
254
255 int
256 qede_tx_queue_setup(struct rte_eth_dev *dev,
257                     uint16_t queue_idx,
258                     uint16_t nb_desc,
259                     unsigned int socket_id,
260                     const struct rte_eth_txconf *tx_conf)
261 {
262         struct qede_dev *qdev = dev->data->dev_private;
263         struct ecore_dev *edev = &qdev->edev;
264         struct qede_tx_queue *txq;
265         int rc;
266
267         PMD_INIT_FUNC_TRACE(edev);
268
269         if (!rte_is_power_of_2(nb_desc)) {
270                 DP_ERR(edev, "Ring size %u is not power of 2\n",
271                        nb_desc);
272                 return -EINVAL;
273         }
274
275         /* Free memory prior to re-allocation if needed... */
276         if (dev->data->tx_queues[queue_idx] != NULL) {
277                 qede_tx_queue_release(dev->data->tx_queues[queue_idx]);
278                 dev->data->tx_queues[queue_idx] = NULL;
279         }
280
281         txq = rte_zmalloc_socket("qede_tx_queue", sizeof(struct qede_tx_queue),
282                                  RTE_CACHE_LINE_SIZE, socket_id);
283
284         if (txq == NULL) {
285                 DP_ERR(edev,
286                        "Unable to allocate memory for txq on socket %u",
287                        socket_id);
288                 return -ENOMEM;
289         }
290
291         txq->nb_tx_desc = nb_desc;
292         txq->qdev = qdev;
293         txq->port_id = dev->data->port_id;
294
295         rc = qdev->ops->common->chain_alloc(edev,
296                                             ECORE_CHAIN_USE_TO_CONSUME_PRODUCE,
297                                             ECORE_CHAIN_MODE_PBL,
298                                             ECORE_CHAIN_CNT_TYPE_U16,
299                                             txq->nb_tx_desc,
300                                             sizeof(union eth_tx_bd_types),
301                                             &txq->tx_pbl);
302         if (rc != ECORE_SUCCESS) {
303                 DP_ERR(edev,
304                        "Unable to allocate memory for txbd ring on socket %u",
305                        socket_id);
306                 qede_tx_queue_release(txq);
307                 return -ENOMEM;
308         }
309
310         /* Allocate software ring */
311         txq->sw_tx_ring = rte_zmalloc_socket("txq->sw_tx_ring",
312                                              (sizeof(struct qede_tx_entry) *
313                                               txq->nb_tx_desc),
314                                              RTE_CACHE_LINE_SIZE, socket_id);
315
316         if (!txq->sw_tx_ring) {
317                 DP_ERR(edev,
318                        "Unable to allocate memory for txbd ring on socket %u",
319                        socket_id);
320                 qede_tx_queue_release(txq);
321                 return -ENOMEM;
322         }
323
324         txq->queue_id = queue_idx;
325
326         txq->nb_tx_avail = txq->nb_tx_desc;
327
328         txq->tx_free_thresh =
329             tx_conf->tx_free_thresh ? tx_conf->tx_free_thresh :
330             (txq->nb_tx_desc - QEDE_DEFAULT_TX_FREE_THRESH);
331
332         dev->data->tx_queues[queue_idx] = txq;
333         if (!qdev->tx_queues)
334                 qdev->tx_queues = (struct qede_tx_queue **)dev->data->tx_queues;
335
336         txq->txq_counter = 0;
337
338         DP_INFO(edev,
339                   "txq %u num_desc %u tx_free_thresh %u socket %u\n",
340                   queue_idx, nb_desc, txq->tx_free_thresh, socket_id);
341
342         return 0;
343 }
344
345 /* This function inits fp content and resets the SB, RXQ and TXQ arrays */
346 static void qede_init_fp(struct qede_dev *qdev)
347 {
348         struct qede_fastpath *fp;
349         int rss_id, txq_index, tc;
350
351         memset((void *)qdev->fp_array, 0, (QEDE_RSS_CNT(qdev) *
352                                            sizeof(*qdev->fp_array)));
353         memset((void *)qdev->sb_array, 0, (QEDE_RSS_CNT(qdev) *
354                                            sizeof(*qdev->sb_array)));
355         for_each_rss(rss_id) {
356                 fp = &qdev->fp_array[rss_id];
357
358                 fp->qdev = qdev;
359                 fp->rss_id = rss_id;
360
361                 /* Point rxq to generic rte queues that was created
362                  * as part of queue creation.
363                  */
364                 fp->rxq = qdev->rx_queues[rss_id];
365                 fp->sb_info = &qdev->sb_array[rss_id];
366
367                 for (tc = 0; tc < qdev->num_tc; tc++) {
368                         txq_index = tc * QEDE_RSS_CNT(qdev) + rss_id;
369                         fp->txqs[tc] = qdev->tx_queues[txq_index];
370                         fp->txqs[tc]->queue_id = txq_index;
371                         /* Updating it to main structure */
372                         snprintf(fp->name, sizeof(fp->name), "%s-fp-%d",
373                                  "qdev", rss_id);
374                 }
375         }
376
377         qdev->gro_disable = gro_disable;
378 }
379
380 void qede_free_fp_arrays(struct qede_dev *qdev)
381 {
382         /* It asseumes qede_free_mem_load() is called before */
383         if (qdev->fp_array != NULL) {
384                 rte_free(qdev->fp_array);
385                 qdev->fp_array = NULL;
386         }
387
388         if (qdev->sb_array != NULL) {
389                 rte_free(qdev->sb_array);
390                 qdev->sb_array = NULL;
391         }
392 }
393
394 int qede_alloc_fp_array(struct qede_dev *qdev)
395 {
396         struct qede_fastpath *fp;
397         struct ecore_dev *edev = &qdev->edev;
398         int i;
399
400         qdev->fp_array = rte_calloc("fp", QEDE_RSS_CNT(qdev),
401                                     sizeof(*qdev->fp_array),
402                                     RTE_CACHE_LINE_SIZE);
403
404         if (!qdev->fp_array) {
405                 DP_ERR(edev, "fp array allocation failed\n");
406                 return -ENOMEM;
407         }
408
409         qdev->sb_array = rte_calloc("sb", QEDE_RSS_CNT(qdev),
410                                     sizeof(*qdev->sb_array),
411                                     RTE_CACHE_LINE_SIZE);
412
413         if (!qdev->sb_array) {
414                 DP_ERR(edev, "sb array allocation failed\n");
415                 rte_free(qdev->fp_array);
416                 return -ENOMEM;
417         }
418
419         return 0;
420 }
421
422 /* This function allocates fast-path status block memory */
423 static int
424 qede_alloc_mem_sb(struct qede_dev *qdev, struct ecore_sb_info *sb_info,
425                   uint16_t sb_id)
426 {
427         struct ecore_dev *edev = &qdev->edev;
428         struct status_block *sb_virt;
429         dma_addr_t sb_phys;
430         int rc;
431
432         sb_virt = OSAL_DMA_ALLOC_COHERENT(edev, &sb_phys, sizeof(*sb_virt));
433
434         if (!sb_virt) {
435                 DP_ERR(edev, "Status block allocation failed\n");
436                 return -ENOMEM;
437         }
438
439         rc = qdev->ops->common->sb_init(edev, sb_info,
440                                         sb_virt, sb_phys, sb_id,
441                                         QED_SB_TYPE_L2_QUEUE);
442         if (rc) {
443                 DP_ERR(edev, "Status block initialization failed\n");
444                 /* TBD: No dma_free_coherent possible */
445                 return rc;
446         }
447
448         return 0;
449 }
450
451 static int qede_alloc_mem_fp(struct qede_dev *qdev, struct qede_fastpath *fp)
452 {
453         return qede_alloc_mem_sb(qdev, fp->sb_info, fp->rss_id);
454 }
455
456 static void qede_shrink_txq(struct qede_dev *qdev, uint16_t num_rss)
457 {
458         /* @@@TBD - this should also re-set the qed interrupts */
459 }
460
461 /* This function allocates all qede memory at NIC load. */
462 static int qede_alloc_mem_load(struct qede_dev *qdev)
463 {
464         int rc = 0, rss_id;
465         struct ecore_dev *edev = &qdev->edev;
466
467         for (rss_id = 0; rss_id < QEDE_RSS_CNT(qdev); rss_id++) {
468                 struct qede_fastpath *fp = &qdev->fp_array[rss_id];
469
470                 rc = qede_alloc_mem_fp(qdev, fp);
471                 if (rc)
472                         break;
473         }
474
475         if (rss_id != QEDE_RSS_CNT(qdev)) {
476                 /* Failed allocating memory for all the queues */
477                 if (!rss_id) {
478                         DP_ERR(edev,
479                                "Failed to alloc memory for leading queue\n");
480                         rc = -ENOMEM;
481                 } else {
482                         DP_NOTICE(edev, false,
483                                   "Failed to allocate memory for all of "
484                                   "RSS queues\n"
485                                   "Desired: %d queues, allocated: %d queues\n",
486                                   QEDE_RSS_CNT(qdev), rss_id);
487                         qede_shrink_txq(qdev, rss_id);
488                 }
489                 qdev->num_rss = rss_id;
490         }
491
492         return 0;
493 }
494
495 static inline void
496 qede_update_rx_prod(struct qede_dev *edev, struct qede_rx_queue *rxq)
497 {
498         uint16_t bd_prod = ecore_chain_get_prod_idx(&rxq->rx_bd_ring);
499         uint16_t cqe_prod = ecore_chain_get_prod_idx(&rxq->rx_comp_ring);
500         struct eth_rx_prod_data rx_prods = { 0 };
501
502         /* Update producers */
503         rx_prods.bd_prod = rte_cpu_to_le_16(bd_prod);
504         rx_prods.cqe_prod = rte_cpu_to_le_16(cqe_prod);
505
506         /* Make sure that the BD and SGE data is updated before updating the
507          * producers since FW might read the BD/SGE right after the producer
508          * is updated.
509          */
510         rte_wmb();
511
512         internal_ram_wr(rxq->hw_rxq_prod_addr, sizeof(rx_prods),
513                         (uint32_t *)&rx_prods);
514
515         /* mmiowb is needed to synchronize doorbell writes from more than one
516          * processor. It guarantees that the write arrives to the device before
517          * the napi lock is released and another qede_poll is called (possibly
518          * on another CPU). Without this barrier, the next doorbell can bypass
519          * this doorbell. This is applicable to IA64/Altix systems.
520          */
521         rte_wmb();
522
523         PMD_RX_LOG(DEBUG, rxq, "bd_prod %u  cqe_prod %u\n", bd_prod, cqe_prod);
524 }
525
526 static inline uint32_t
527 qede_rxfh_indir_default(uint32_t index, uint32_t n_rx_rings)
528 {
529         return index % n_rx_rings;
530 }
531
532 static void qede_prandom_bytes(uint32_t *buff, size_t bytes)
533 {
534         unsigned int i;
535
536         srand((unsigned int)time(NULL));
537
538         for (i = 0; i < ECORE_RSS_KEY_SIZE; i++)
539                 buff[i] = rand();
540 }
541
542 static int
543 qede_config_rss(struct rte_eth_dev *eth_dev,
544                 struct qed_update_vport_rss_params *rss_params)
545 {
546         enum rte_eth_rx_mq_mode mode = eth_dev->data->dev_conf.rxmode.mq_mode;
547         struct rte_eth_rss_conf rss_conf =
548             eth_dev->data->dev_conf.rx_adv_conf.rss_conf;
549         struct qede_dev *qdev = eth_dev->data->dev_private;
550         struct ecore_dev *edev = &qdev->edev;
551         unsigned int i;
552
553         PMD_INIT_FUNC_TRACE(edev);
554
555         /* Check if RSS conditions are met.
556          * Note: Even though its meaningless to enable RSS with one queue, it
557          * could be used to produce RSS Hash, so skipping that check.
558          */
559
560         if (!(mode & ETH_MQ_RX_RSS)) {
561                 DP_INFO(edev, "RSS flag is not set\n");
562                 return -EINVAL;
563         }
564
565         DP_INFO(edev, "RSS flag is set\n");
566
567         if (rss_conf.rss_hf == 0) {
568                 DP_NOTICE(edev, false, "No RSS hash function to apply\n");
569                 return -EINVAL;
570         }
571
572         if (rss_conf.rss_key != NULL) {
573                 DP_NOTICE(edev, false,
574                           "User provided RSS key is not supported\n");
575                 return -EINVAL;
576         }
577
578         memset(rss_params, 0, sizeof(*rss_params));
579
580         for (i = 0; i < ECORE_RSS_IND_TABLE_SIZE; i++)
581                 rss_params->rss_ind_table[i] = qede_rxfh_indir_default(i,
582                                                         QEDE_RSS_CNT(qdev));
583
584         qede_prandom_bytes(rss_params->rss_key,
585                            sizeof(rss_params->rss_key));
586
587         DP_INFO(edev, "RSS check passes\n");
588
589         return 0;
590 }
591
592 static int qede_start_queues(struct rte_eth_dev *eth_dev, bool clear_stats)
593 {
594         struct qede_dev *qdev = eth_dev->data->dev_private;
595         struct ecore_dev *edev = &qdev->edev;
596         struct qed_update_vport_rss_params *rss_params = &qdev->rss_params;
597         struct qed_dev_info *qed_info = &qdev->dev_info.common;
598         struct qed_update_vport_params vport_update_params;
599         struct qed_start_vport_params start = { 0 };
600         int vlan_removal_en = 1;
601         int rc, tc, i;
602
603         if (!qdev->num_rss) {
604                 DP_ERR(edev,
605                        "Cannot update V-VPORT as active as "
606                        "there are no Rx queues\n");
607                 return -EINVAL;
608         }
609
610         start.remove_inner_vlan = vlan_removal_en;
611         start.gro_enable = !qdev->gro_disable;
612         start.mtu = qdev->mtu;
613         start.vport_id = 0;
614         start.drop_ttl0 = true;
615         start.clear_stats = clear_stats;
616
617         rc = qdev->ops->vport_start(edev, &start);
618         if (rc) {
619                 DP_ERR(edev, "Start V-PORT failed %d\n", rc);
620                 return rc;
621         }
622
623         DP_INFO(edev,
624                 "Start vport ramrod passed, vport_id = %d,"
625                 " MTU = %d, vlan_removal_en = %d\n",
626                 start.vport_id, qdev->mtu, vlan_removal_en);
627
628         for_each_rss(i) {
629                 struct qede_fastpath *fp = &qdev->fp_array[i];
630                 dma_addr_t p_phys_table;
631                 uint16_t page_cnt;
632
633                 p_phys_table = ecore_chain_get_pbl_phys(&fp->rxq->rx_comp_ring);
634                 page_cnt = ecore_chain_get_page_cnt(&fp->rxq->rx_comp_ring);
635
636                 ecore_sb_ack(fp->sb_info, IGU_INT_DISABLE, 0);  /* @DPDK */
637
638                 rc = qdev->ops->q_rx_start(edev, i, i, 0,
639                                            fp->sb_info->igu_sb_id,
640                                            RX_PI,
641                                            fp->rxq->rx_buf_size,
642                                            fp->rxq->rx_bd_ring.p_phys_addr,
643                                            p_phys_table,
644                                            page_cnt,
645                                            &fp->rxq->hw_rxq_prod_addr);
646                 if (rc) {
647                         DP_ERR(edev, "Start RXQ #%d failed %d\n", i, rc);
648                         return rc;
649                 }
650
651                 fp->rxq->hw_cons_ptr = &fp->sb_info->sb_virt->pi_array[RX_PI];
652
653                 qede_update_rx_prod(qdev, fp->rxq);
654
655                 for (tc = 0; tc < qdev->num_tc; tc++) {
656                         struct qede_tx_queue *txq = fp->txqs[tc];
657                         int txq_index = tc * QEDE_RSS_CNT(qdev) + i;
658
659                         p_phys_table = ecore_chain_get_pbl_phys(&txq->tx_pbl);
660                         page_cnt = ecore_chain_get_page_cnt(&txq->tx_pbl);
661                         rc = qdev->ops->q_tx_start(edev, i, txq_index,
662                                                    0,
663                                                    fp->sb_info->igu_sb_id,
664                                                    TX_PI(tc),
665                                                    p_phys_table, page_cnt,
666                                                    &txq->doorbell_addr);
667                         if (rc) {
668                                 DP_ERR(edev, "Start txq %u failed %d\n",
669                                        txq_index, rc);
670                                 return rc;
671                         }
672
673                         txq->hw_cons_ptr =
674                             &fp->sb_info->sb_virt->pi_array[TX_PI(tc)];
675                         SET_FIELD(txq->tx_db.data.params,
676                                   ETH_DB_DATA_DEST, DB_DEST_XCM);
677                         SET_FIELD(txq->tx_db.data.params, ETH_DB_DATA_AGG_CMD,
678                                   DB_AGG_CMD_SET);
679                         SET_FIELD(txq->tx_db.data.params,
680                                   ETH_DB_DATA_AGG_VAL_SEL,
681                                   DQ_XCM_ETH_TX_BD_PROD_CMD);
682
683                         txq->tx_db.data.agg_flags = DQ_XCM_ETH_DQ_CF_CMD;
684                 }
685         }
686
687         /* Prepare and send the vport enable */
688         memset(&vport_update_params, 0, sizeof(vport_update_params));
689         vport_update_params.vport_id = start.vport_id;
690         vport_update_params.update_vport_active_flg = 1;
691         vport_update_params.vport_active_flg = 1;
692
693         /* @DPDK */
694         if (qed_info->mf_mode == MF_NPAR && qed_info->tx_switching) {
695                 /* TBD: Check SRIOV enabled for VF */
696                 vport_update_params.update_tx_switching_flg = 1;
697                 vport_update_params.tx_switching_flg = 1;
698         }
699
700         if (!qede_config_rss(eth_dev, rss_params)) {
701                 vport_update_params.update_rss_flg = 1;
702
703                 qdev->rss_enabled = 1;
704                 DP_INFO(edev, "Updating RSS flag\n");
705         } else {
706                 qdev->rss_enabled = 0;
707                 DP_INFO(edev, "Not Updating RSS flag\n");
708         }
709
710         rte_memcpy(&vport_update_params.rss_params, rss_params,
711                sizeof(*rss_params));
712
713         rc = qdev->ops->vport_update(edev, &vport_update_params);
714         if (rc) {
715                 DP_ERR(edev, "Update V-PORT failed %d\n", rc);
716                 return rc;
717         }
718
719         return 0;
720 }
721
722 #ifdef ENC_SUPPORTED
723 static bool qede_tunn_exist(uint16_t flag)
724 {
725         return !!((PARSING_AND_ERR_FLAGS_TUNNELEXIST_MASK <<
726                     PARSING_AND_ERR_FLAGS_TUNNELEXIST_SHIFT) & flag);
727 }
728
729 static inline uint8_t qede_check_tunn_csum(uint16_t flag)
730 {
731         uint8_t tcsum = 0;
732         uint16_t csum_flag = 0;
733
734         if ((PARSING_AND_ERR_FLAGS_TUNNELL4CHKSMWASCALCULATED_MASK <<
735              PARSING_AND_ERR_FLAGS_TUNNELL4CHKSMWASCALCULATED_SHIFT) & flag)
736                 csum_flag |= PARSING_AND_ERR_FLAGS_TUNNELL4CHKSMERROR_MASK <<
737                     PARSING_AND_ERR_FLAGS_TUNNELL4CHKSMERROR_SHIFT;
738
739         if ((PARSING_AND_ERR_FLAGS_L4CHKSMWASCALCULATED_MASK <<
740              PARSING_AND_ERR_FLAGS_L4CHKSMWASCALCULATED_SHIFT) & flag) {
741                 csum_flag |= PARSING_AND_ERR_FLAGS_L4CHKSMERROR_MASK <<
742                     PARSING_AND_ERR_FLAGS_L4CHKSMERROR_SHIFT;
743                 tcsum = QEDE_TUNN_CSUM_UNNECESSARY;
744         }
745
746         csum_flag |= PARSING_AND_ERR_FLAGS_TUNNELIPHDRERROR_MASK <<
747             PARSING_AND_ERR_FLAGS_TUNNELIPHDRERROR_SHIFT |
748             PARSING_AND_ERR_FLAGS_IPHDRERROR_MASK <<
749             PARSING_AND_ERR_FLAGS_IPHDRERROR_SHIFT;
750
751         if (csum_flag & flag)
752                 return QEDE_CSUM_ERROR;
753
754         return QEDE_CSUM_UNNECESSARY | tcsum;
755 }
756 #else
757 static inline uint8_t qede_tunn_exist(uint16_t flag)
758 {
759         return 0;
760 }
761
762 static inline uint8_t qede_check_tunn_csum(uint16_t flag)
763 {
764         return 0;
765 }
766 #endif
767
768 static inline uint8_t qede_check_notunn_csum(uint16_t flag)
769 {
770         uint8_t csum = 0;
771         uint16_t csum_flag = 0;
772
773         if ((PARSING_AND_ERR_FLAGS_L4CHKSMWASCALCULATED_MASK <<
774              PARSING_AND_ERR_FLAGS_L4CHKSMWASCALCULATED_SHIFT) & flag) {
775                 csum_flag |= PARSING_AND_ERR_FLAGS_L4CHKSMERROR_MASK <<
776                     PARSING_AND_ERR_FLAGS_L4CHKSMERROR_SHIFT;
777                 csum = QEDE_CSUM_UNNECESSARY;
778         }
779
780         csum_flag |= PARSING_AND_ERR_FLAGS_IPHDRERROR_MASK <<
781             PARSING_AND_ERR_FLAGS_IPHDRERROR_SHIFT;
782
783         if (csum_flag & flag)
784                 return QEDE_CSUM_ERROR;
785
786         return csum;
787 }
788
789 static inline uint8_t qede_check_csum(uint16_t flag)
790 {
791         if (likely(!qede_tunn_exist(flag)))
792                 return qede_check_notunn_csum(flag);
793         else
794                 return qede_check_tunn_csum(flag);
795 }
796
797 static inline void qede_rx_bd_ring_consume(struct qede_rx_queue *rxq)
798 {
799         ecore_chain_consume(&rxq->rx_bd_ring);
800         rxq->sw_rx_cons++;
801 }
802
803 static inline void
804 qede_reuse_page(struct qede_dev *qdev,
805                 struct qede_rx_queue *rxq, struct qede_rx_entry *curr_cons)
806 {
807         struct eth_rx_bd *rx_bd_prod = ecore_chain_produce(&rxq->rx_bd_ring);
808         uint16_t idx = rxq->sw_rx_cons & NUM_RX_BDS(rxq);
809         struct qede_rx_entry *curr_prod;
810         dma_addr_t new_mapping;
811
812         curr_prod = &rxq->sw_rx_ring[idx];
813         *curr_prod = *curr_cons;
814
815         new_mapping = rte_mbuf_data_dma_addr_default(curr_prod->mbuf) +
816                       curr_prod->page_offset;
817
818         rx_bd_prod->addr.hi = rte_cpu_to_le_32(U64_HI(new_mapping));
819         rx_bd_prod->addr.lo = rte_cpu_to_le_32(U64_LO(new_mapping));
820
821         rxq->sw_rx_prod++;
822 }
823
824 static inline void
825 qede_recycle_rx_bd_ring(struct qede_rx_queue *rxq,
826                         struct qede_dev *qdev, uint8_t count)
827 {
828         struct qede_rx_entry *curr_cons;
829
830         for (; count > 0; count--) {
831                 curr_cons = &rxq->sw_rx_ring[rxq->sw_rx_cons & NUM_RX_BDS(rxq)];
832                 qede_reuse_page(qdev, rxq, curr_cons);
833                 qede_rx_bd_ring_consume(rxq);
834         }
835 }
836
837 static inline uint32_t qede_rx_cqe_to_pkt_type(uint16_t flags)
838 {
839         uint32_t p_type;
840         /* TBD - L4 indications needed ? */
841         uint16_t protocol = ((PARSING_AND_ERR_FLAGS_L3TYPE_MASK <<
842                               PARSING_AND_ERR_FLAGS_L3TYPE_SHIFT) & flags);
843
844         /* protocol = 3 means LLC/SNAP over Ethernet */
845         if (unlikely(protocol == 0 || protocol == 3))
846                 p_type = RTE_PTYPE_UNKNOWN;
847         else if (protocol == 1)
848                 p_type = RTE_PTYPE_L3_IPV4;
849         else if (protocol == 2)
850                 p_type = RTE_PTYPE_L3_IPV6;
851
852         return RTE_PTYPE_L2_ETHER | p_type;
853 }
854
855 uint16_t
856 qede_recv_pkts(void *p_rxq, struct rte_mbuf **rx_pkts, uint16_t nb_pkts)
857 {
858         struct qede_rx_queue *rxq = p_rxq;
859         struct qede_dev *qdev = rxq->qdev;
860         struct ecore_dev *edev = &qdev->edev;
861         struct qede_fastpath *fp = &qdev->fp_array[rxq->queue_id];
862         uint16_t hw_comp_cons, sw_comp_cons, sw_rx_index;
863         uint16_t rx_pkt = 0;
864         union eth_rx_cqe *cqe;
865         struct eth_fast_path_rx_reg_cqe *fp_cqe;
866         register struct rte_mbuf *rx_mb = NULL;
867         enum eth_rx_cqe_type cqe_type;
868         uint16_t len, pad;
869         uint16_t preload_idx;
870         uint8_t csum_flag;
871         uint16_t parse_flag;
872         enum rss_hash_type htype;
873
874         hw_comp_cons = rte_le_to_cpu_16(*rxq->hw_cons_ptr);
875         sw_comp_cons = ecore_chain_get_cons_idx(&rxq->rx_comp_ring);
876
877         rte_rmb();
878
879         if (hw_comp_cons == sw_comp_cons)
880                 return 0;
881
882         while (sw_comp_cons != hw_comp_cons) {
883                 /* Get the CQE from the completion ring */
884                 cqe =
885                     (union eth_rx_cqe *)ecore_chain_consume(&rxq->rx_comp_ring);
886                 cqe_type = cqe->fast_path_regular.type;
887
888                 if (unlikely(cqe_type == ETH_RX_CQE_TYPE_SLOW_PATH)) {
889                         PMD_RX_LOG(DEBUG, rxq, "Got a slowath CQE\n");
890
891                         qdev->ops->eth_cqe_completion(edev, fp->rss_id,
892                                 (struct eth_slow_path_rx_cqe *)cqe);
893                         goto next_cqe;
894                 }
895
896                 /* Get the data from the SW ring */
897                 sw_rx_index = rxq->sw_rx_cons & NUM_RX_BDS(rxq);
898                 rx_mb = rxq->sw_rx_ring[sw_rx_index].mbuf;
899                 assert(rx_mb != NULL);
900
901                 /* non GRO */
902                 fp_cqe = &cqe->fast_path_regular;
903
904                 len = rte_le_to_cpu_16(fp_cqe->len_on_first_bd);
905                 pad = fp_cqe->placement_offset;
906                 assert((len + pad) <= rx_mb->buf_len);
907
908                 PMD_RX_LOG(DEBUG, rxq,
909                            "CQE type = 0x%x, flags = 0x%x, vlan = 0x%x"
910                            " len = %u, parsing_flags = %d\n",
911                            cqe_type, fp_cqe->bitfields,
912                            rte_le_to_cpu_16(fp_cqe->vlan_tag),
913                            len, rte_le_to_cpu_16(fp_cqe->pars_flags.flags));
914
915                 /* If this is an error packet then drop it */
916                 parse_flag =
917                     rte_le_to_cpu_16(cqe->fast_path_regular.pars_flags.flags);
918                 csum_flag = qede_check_csum(parse_flag);
919                 if (unlikely(csum_flag == QEDE_CSUM_ERROR)) {
920                         PMD_RX_LOG(ERR, rxq,
921                                    "CQE in CONS = %u has error, flags = 0x%x "
922                                    "dropping incoming packet\n",
923                                    sw_comp_cons, parse_flag);
924                         rxq->rx_hw_errors++;
925                         qede_recycle_rx_bd_ring(rxq, qdev, fp_cqe->bd_num);
926                         goto next_cqe;
927                 }
928
929                 if (unlikely(qede_alloc_rx_buffer(rxq) != 0)) {
930                         PMD_RX_LOG(ERR, rxq,
931                                    "New buffer allocation failed,"
932                                    "dropping incoming packet\n");
933                         qede_recycle_rx_bd_ring(rxq, qdev, fp_cqe->bd_num);
934                         rte_eth_devices[rxq->port_id].
935                             data->rx_mbuf_alloc_failed++;
936                         rxq->rx_alloc_errors++;
937                         break;
938                 }
939
940                 qede_rx_bd_ring_consume(rxq);
941
942                 /* Prefetch next mbuf while processing current one. */
943                 preload_idx = rxq->sw_rx_cons & NUM_RX_BDS(rxq);
944                 rte_prefetch0(rxq->sw_rx_ring[preload_idx].mbuf);
945
946                 if (fp_cqe->bd_num != 1)
947                         PMD_RX_LOG(DEBUG, rxq,
948                                    "Jumbo-over-BD packet not supported\n");
949
950                 /* Update MBUF fields */
951                 rx_mb->ol_flags = 0;
952                 rx_mb->data_off = pad + RTE_PKTMBUF_HEADROOM;
953                 rx_mb->nb_segs = 1;
954                 rx_mb->data_len = len;
955                 rx_mb->pkt_len = len;
956                 rx_mb->port = rxq->port_id;
957                 rx_mb->packet_type = qede_rx_cqe_to_pkt_type(parse_flag);
958
959                 htype = (uint8_t)GET_FIELD(fp_cqe->bitfields,
960                                 ETH_FAST_PATH_RX_REG_CQE_RSS_HASH_TYPE);
961                 if (qdev->rss_enabled && htype) {
962                         rx_mb->ol_flags |= PKT_RX_RSS_HASH;
963                         rx_mb->hash.rss = rte_le_to_cpu_32(fp_cqe->rss_hash);
964                         PMD_RX_LOG(DEBUG, rxq, "Hash result 0x%x\n",
965                                    rx_mb->hash.rss);
966                 }
967
968                 rte_prefetch1(rte_pktmbuf_mtod(rx_mb, void *));
969
970                 if (CQE_HAS_VLAN(parse_flag)) {
971                         rx_mb->vlan_tci = rte_le_to_cpu_16(fp_cqe->vlan_tag);
972                         rx_mb->ol_flags |= PKT_RX_VLAN_PKT;
973                 }
974
975                 if (CQE_HAS_OUTER_VLAN(parse_flag)) {
976                         /* FW does not provide indication of Outer VLAN tag,
977                          * which is always stripped, so vlan_tci_outer is set
978                          * to 0. Here vlan_tag represents inner VLAN tag.
979                          */
980                         rx_mb->vlan_tci = rte_le_to_cpu_16(fp_cqe->vlan_tag);
981                         rx_mb->ol_flags |= PKT_RX_QINQ_PKT;
982                 }
983
984                 rx_pkts[rx_pkt] = rx_mb;
985                 rx_pkt++;
986 next_cqe:
987                 ecore_chain_recycle_consumed(&rxq->rx_comp_ring);
988                 sw_comp_cons = ecore_chain_get_cons_idx(&rxq->rx_comp_ring);
989                 if (rx_pkt == nb_pkts) {
990                         PMD_RX_LOG(DEBUG, rxq,
991                                    "Budget reached nb_pkts=%u received=%u\n",
992                                    rx_pkt, nb_pkts);
993                         break;
994                 }
995         }
996
997         qede_update_rx_prod(qdev, rxq);
998
999         PMD_RX_LOG(DEBUG, rxq, "rx_pkts=%u core=%d\n", rx_pkt, rte_lcore_id());
1000
1001         return rx_pkt;
1002 }
1003
1004 static inline int
1005 qede_free_tx_pkt(struct ecore_dev *edev, struct qede_tx_queue *txq)
1006 {
1007         uint16_t idx = TX_CONS(txq);
1008         struct eth_tx_bd *tx_data_bd;
1009         struct rte_mbuf *mbuf = txq->sw_tx_ring[idx].mbuf;
1010
1011         if (unlikely(!mbuf)) {
1012                 PMD_TX_LOG(ERR, txq,
1013                            "null mbuf nb_tx_desc %u nb_tx_avail %u "
1014                            "sw_tx_cons %u sw_tx_prod %u\n",
1015                            txq->nb_tx_desc, txq->nb_tx_avail, idx,
1016                            TX_PROD(txq));
1017                 return -1;
1018         }
1019
1020         /* Free now */
1021         rte_pktmbuf_free_seg(mbuf);
1022         txq->sw_tx_ring[idx].mbuf = NULL;
1023         ecore_chain_consume(&txq->tx_pbl);
1024         txq->nb_tx_avail++;
1025
1026         return 0;
1027 }
1028
1029 static inline uint16_t
1030 qede_process_tx_compl(struct ecore_dev *edev, struct qede_tx_queue *txq)
1031 {
1032         uint16_t tx_compl = 0;
1033         uint16_t hw_bd_cons;
1034         int rc;
1035
1036         hw_bd_cons = rte_le_to_cpu_16(*txq->hw_cons_ptr);
1037         rte_compiler_barrier();
1038
1039         while (hw_bd_cons != ecore_chain_get_cons_idx(&txq->tx_pbl)) {
1040                 rc = qede_free_tx_pkt(edev, txq);
1041                 if (rc) {
1042                         DP_NOTICE(edev, false,
1043                                   "hw_bd_cons = %d, chain_cons=%d\n",
1044                                   hw_bd_cons,
1045                                   ecore_chain_get_cons_idx(&txq->tx_pbl));
1046                         break;
1047                 }
1048                 txq->sw_tx_cons++;      /* Making TXD available */
1049                 tx_compl++;
1050         }
1051
1052         PMD_TX_LOG(DEBUG, txq, "Tx compl %u sw_tx_cons %u avail %u\n",
1053                    tx_compl, txq->sw_tx_cons, txq->nb_tx_avail);
1054         return tx_compl;
1055 }
1056
1057 uint16_t
1058 qede_xmit_pkts(void *p_txq, struct rte_mbuf **tx_pkts, uint16_t nb_pkts)
1059 {
1060         struct qede_tx_queue *txq = p_txq;
1061         struct qede_dev *qdev = txq->qdev;
1062         struct ecore_dev *edev = &qdev->edev;
1063         struct qede_fastpath *fp = &qdev->fp_array[txq->queue_id];
1064         struct eth_tx_1st_bd *first_bd;
1065         uint16_t nb_tx_pkts;
1066         uint16_t nb_pkt_sent = 0;
1067         uint16_t bd_prod;
1068         uint16_t idx;
1069         uint16_t tx_count;
1070
1071         if (unlikely(txq->nb_tx_avail < txq->tx_free_thresh)) {
1072                 PMD_TX_LOG(DEBUG, txq, "send=%u avail=%u free_thresh=%u\n",
1073                            nb_pkts, txq->nb_tx_avail, txq->tx_free_thresh);
1074                 (void)qede_process_tx_compl(edev, txq);
1075         }
1076
1077         nb_tx_pkts = RTE_MIN(nb_pkts, (txq->nb_tx_avail / MAX_NUM_TX_BDS));
1078         if (unlikely(nb_tx_pkts == 0)) {
1079                 PMD_TX_LOG(DEBUG, txq, "Out of BDs nb_pkts=%u avail=%u\n",
1080                            nb_pkts, txq->nb_tx_avail);
1081                 return 0;
1082         }
1083
1084         tx_count = nb_tx_pkts;
1085         while (nb_tx_pkts--) {
1086                 /* Fill the entry in the SW ring and the BDs in the FW ring */
1087                 idx = TX_PROD(txq);
1088                 struct rte_mbuf *mbuf = *tx_pkts++;
1089                 txq->sw_tx_ring[idx].mbuf = mbuf;
1090                 first_bd = (struct eth_tx_1st_bd *)
1091                     ecore_chain_produce(&txq->tx_pbl);
1092                 first_bd->data.bd_flags.bitfields =
1093                     1 << ETH_TX_1ST_BD_FLAGS_START_BD_SHIFT;
1094                 /* Map MBUF linear data for DMA and set in the first BD */
1095                 QEDE_BD_SET_ADDR_LEN(first_bd, rte_mbuf_data_dma_addr(mbuf),
1096                                      mbuf->data_len);
1097
1098                 /* Descriptor based VLAN insertion */
1099                 if (mbuf->ol_flags & (PKT_TX_VLAN_PKT | PKT_TX_QINQ_PKT)) {
1100                         first_bd->data.vlan = rte_cpu_to_le_16(mbuf->vlan_tci);
1101                         first_bd->data.bd_flags.bitfields |=
1102                             1 << ETH_TX_1ST_BD_FLAGS_VLAN_INSERTION_SHIFT;
1103                 }
1104
1105                 /* Offload the IP checksum in the hardware */
1106                 if (mbuf->ol_flags & PKT_TX_IP_CKSUM) {
1107                         first_bd->data.bd_flags.bitfields |=
1108                             1 << ETH_TX_1ST_BD_FLAGS_IP_CSUM_SHIFT;
1109                 }
1110
1111                 /* L4 checksum offload (tcp or udp) */
1112                 if (mbuf->ol_flags & (PKT_TX_TCP_CKSUM | PKT_TX_UDP_CKSUM)) {
1113                         first_bd->data.bd_flags.bitfields |=
1114                             1 << ETH_TX_1ST_BD_FLAGS_L4_CSUM_SHIFT;
1115                         /* IPv6 + extn. -> later */
1116                 }
1117                 first_bd->data.nbds = MAX_NUM_TX_BDS;
1118                 txq->sw_tx_prod++;
1119                 rte_prefetch0(txq->sw_tx_ring[TX_PROD(txq)].mbuf);
1120                 txq->nb_tx_avail--;
1121                 bd_prod =
1122                     rte_cpu_to_le_16(ecore_chain_get_prod_idx(&txq->tx_pbl));
1123                 nb_pkt_sent++;
1124         }
1125
1126         /* Write value of prod idx into bd_prod */
1127         txq->tx_db.data.bd_prod = bd_prod;
1128         rte_wmb();
1129         rte_compiler_barrier();
1130         DIRECT_REG_WR(edev, txq->doorbell_addr, txq->tx_db.raw);
1131         rte_wmb();
1132
1133         /* Check again for Tx completions */
1134         (void)qede_process_tx_compl(edev, txq);
1135
1136         PMD_TX_LOG(DEBUG, txq, "to_send=%u can_send=%u sent=%u core=%d\n",
1137                    nb_pkts, tx_count, nb_pkt_sent, rte_lcore_id());
1138
1139         return nb_pkt_sent;
1140 }
1141
1142 int qede_dev_start(struct rte_eth_dev *eth_dev)
1143 {
1144         struct qede_dev *qdev = eth_dev->data->dev_private;
1145         struct ecore_dev *edev = &qdev->edev;
1146         struct qed_link_output link_output;
1147         int rc;
1148
1149         DP_INFO(edev, "port %u\n", eth_dev->data->port_id);
1150
1151         if (qdev->state == QEDE_START) {
1152                 DP_INFO(edev, "device already started\n");
1153                 return 0;
1154         }
1155
1156         if (qdev->state == QEDE_CLOSE) {
1157                 rc = qede_alloc_fp_array(qdev);
1158                 qede_init_fp(qdev);
1159                 rc = qede_alloc_mem_load(qdev);
1160                 DP_INFO(edev, "Allocated %d RSS queues on %d TC/s\n",
1161                         QEDE_RSS_CNT(qdev), qdev->num_tc);
1162         } else if (qdev->state == QEDE_STOP) {
1163                 DP_INFO(edev, "restarting port %u\n", eth_dev->data->port_id);
1164         } else {
1165                 DP_INFO(edev, "unknown state port %u\n",
1166                         eth_dev->data->port_id);
1167                 return -EINVAL;
1168         }
1169
1170         rc = qede_start_queues(eth_dev, true);
1171
1172         if (rc) {
1173                 DP_ERR(edev, "Failed to start queues\n");
1174                 /* TBD: free */
1175                 return rc;
1176         }
1177
1178         DP_INFO(edev, "Start VPORT, RXQ and TXQ succeeded\n");
1179
1180         qede_dev_set_link_state(eth_dev, true);
1181
1182         /* Query whether link is already-up */
1183         memset(&link_output, 0, sizeof(link_output));
1184         qdev->ops->common->get_link(edev, &link_output);
1185         DP_NOTICE(edev, false, "link status: %s\n",
1186                   link_output.link_up ? "up" : "down");
1187
1188         qdev->state = QEDE_START;
1189
1190         qede_config_rx_mode(eth_dev);
1191
1192         DP_INFO(edev, "dev_state is QEDE_START\n");
1193
1194         return 0;
1195 }
1196
1197 static int qede_drain_txq(struct qede_dev *qdev,
1198                           struct qede_tx_queue *txq, bool allow_drain)
1199 {
1200         struct ecore_dev *edev = &qdev->edev;
1201         int rc, cnt = 1000;
1202
1203         while (txq->sw_tx_cons != txq->sw_tx_prod) {
1204                 qede_process_tx_compl(edev, txq);
1205                 if (!cnt) {
1206                         if (allow_drain) {
1207                                 DP_NOTICE(edev, false,
1208                                           "Tx queue[%u] is stuck,"
1209                                           "requesting MCP to drain\n",
1210                                           txq->queue_id);
1211                                 rc = qdev->ops->common->drain(edev);
1212                                 if (rc)
1213                                         return rc;
1214                                 return qede_drain_txq(qdev, txq, false);
1215                         }
1216
1217                         DP_NOTICE(edev, false,
1218                                   "Timeout waiting for tx queue[%d]:"
1219                                   "PROD=%d, CONS=%d\n",
1220                                   txq->queue_id, txq->sw_tx_prod,
1221                                   txq->sw_tx_cons);
1222                         return -ENODEV;
1223                 }
1224                 cnt--;
1225                 DELAY(1000);
1226                 rte_compiler_barrier();
1227         }
1228
1229         /* FW finished processing, wait for HW to transmit all tx packets */
1230         DELAY(2000);
1231
1232         return 0;
1233 }
1234
1235 static int qede_stop_queues(struct qede_dev *qdev)
1236 {
1237         struct qed_update_vport_params vport_update_params;
1238         struct ecore_dev *edev = &qdev->edev;
1239         int rc, tc, i;
1240
1241         /* Disable the vport */
1242         memset(&vport_update_params, 0, sizeof(vport_update_params));
1243         vport_update_params.vport_id = 0;
1244         vport_update_params.update_vport_active_flg = 1;
1245         vport_update_params.vport_active_flg = 0;
1246         vport_update_params.update_rss_flg = 0;
1247
1248         DP_INFO(edev, "vport_update\n");
1249
1250         rc = qdev->ops->vport_update(edev, &vport_update_params);
1251         if (rc) {
1252                 DP_ERR(edev, "Failed to update vport\n");
1253                 return rc;
1254         }
1255
1256         DP_INFO(edev, "Flushing tx queues\n");
1257
1258         /* Flush Tx queues. If needed, request drain from MCP */
1259         for_each_rss(i) {
1260                 struct qede_fastpath *fp = &qdev->fp_array[i];
1261                 for (tc = 0; tc < qdev->num_tc; tc++) {
1262                         struct qede_tx_queue *txq = fp->txqs[tc];
1263                         rc = qede_drain_txq(qdev, txq, true);
1264                         if (rc)
1265                                 return rc;
1266                 }
1267         }
1268
1269         /* Stop all Queues in reverse order */
1270         for (i = QEDE_RSS_CNT(qdev) - 1; i >= 0; i--) {
1271                 struct qed_stop_rxq_params rx_params;
1272
1273                 /* Stop the Tx Queue(s) */
1274                 for (tc = 0; tc < qdev->num_tc; tc++) {
1275                         struct qed_stop_txq_params tx_params;
1276
1277                         tx_params.rss_id = i;
1278                         tx_params.tx_queue_id = tc * QEDE_RSS_CNT(qdev) + i;
1279
1280                         DP_INFO(edev, "Stopping tx queues\n");
1281                         rc = qdev->ops->q_tx_stop(edev, &tx_params);
1282                         if (rc) {
1283                                 DP_ERR(edev, "Failed to stop TXQ #%d\n",
1284                                        tx_params.tx_queue_id);
1285                                 return rc;
1286                         }
1287                 }
1288
1289                 /* Stop the Rx Queue */
1290                 memset(&rx_params, 0, sizeof(rx_params));
1291                 rx_params.rss_id = i;
1292                 rx_params.rx_queue_id = i;
1293                 rx_params.eq_completion_only = 1;
1294
1295                 DP_INFO(edev, "Stopping rx queues\n");
1296
1297                 rc = qdev->ops->q_rx_stop(edev, &rx_params);
1298                 if (rc) {
1299                         DP_ERR(edev, "Failed to stop RXQ #%d\n", i);
1300                         return rc;
1301                 }
1302         }
1303
1304         DP_INFO(edev, "Stopping vports\n");
1305
1306         /* Stop the vport */
1307         rc = qdev->ops->vport_stop(edev, 0);
1308         if (rc)
1309                 DP_ERR(edev, "Failed to stop VPORT\n");
1310
1311         return rc;
1312 }
1313
1314 void qede_reset_fp_rings(struct qede_dev *qdev)
1315 {
1316         uint16_t rss_id;
1317         uint8_t tc;
1318
1319         for_each_rss(rss_id) {
1320                 DP_INFO(&qdev->edev, "reset fp chain for rss %u\n", rss_id);
1321                 struct qede_fastpath *fp = &qdev->fp_array[rss_id];
1322                 ecore_chain_reset(&fp->rxq->rx_bd_ring);
1323                 ecore_chain_reset(&fp->rxq->rx_comp_ring);
1324                 for (tc = 0; tc < qdev->num_tc; tc++) {
1325                         struct qede_tx_queue *txq = fp->txqs[tc];
1326                         ecore_chain_reset(&txq->tx_pbl);
1327                 }
1328         }
1329 }
1330
1331 /* This function frees all memory of a single fp */
1332 static void qede_free_mem_fp(struct qede_dev *qdev, struct qede_fastpath *fp)
1333 {
1334         uint8_t tc;
1335
1336         qede_rx_queue_release(fp->rxq);
1337         for (tc = 0; tc < qdev->num_tc; tc++)
1338                 qede_tx_queue_release(fp->txqs[tc]);
1339 }
1340
1341 void qede_free_mem_load(struct qede_dev *qdev)
1342 {
1343         uint8_t rss_id;
1344
1345         for_each_rss(rss_id) {
1346                 struct qede_fastpath *fp = &qdev->fp_array[rss_id];
1347                 qede_free_mem_fp(qdev, fp);
1348         }
1349         /* qdev->num_rss = 0; */
1350 }
1351
1352 /*
1353  * Stop an Ethernet device. The device can be restarted with a call to
1354  * rte_eth_dev_start().
1355  * Do not change link state and do not release sw structures.
1356  */
1357 void qede_dev_stop(struct rte_eth_dev *eth_dev)
1358 {
1359         struct qede_dev *qdev = eth_dev->data->dev_private;
1360         struct ecore_dev *edev = &qdev->edev;
1361         int rc;
1362
1363         DP_INFO(edev, "port %u\n", eth_dev->data->port_id);
1364
1365         if (qdev->state != QEDE_START) {
1366                 DP_INFO(edev, "device not yet started\n");
1367                 return;
1368         }
1369
1370         rc = qede_stop_queues(qdev);
1371
1372         if (rc)
1373                 DP_ERR(edev, "Didn't succeed to close queues\n");
1374
1375         DP_INFO(edev, "Stopped queues\n");
1376
1377         qdev->ops->fastpath_stop(edev);
1378
1379         qede_reset_fp_rings(qdev);
1380
1381         qdev->state = QEDE_STOP;
1382
1383         DP_INFO(edev, "dev_state is QEDE_STOP\n");
1384 }