net/nfp: support new firmware medatada API
[dpdk.git] / drivers / net / nfp / nfp_net.c
1 /*
2  * Copyright (c) 2014, 2015 Netronome Systems, Inc.
3  * All rights reserved.
4  *
5  * Small portions derived from code Copyright(c) 2010-2015 Intel Corporation.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright notice,
11  *  this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *  notice, this list of conditions and the following disclaimer in the
15  *  documentation and/or other materials provided with the distribution
16  *
17  * 3. Neither the name of the copyright holder nor the names of its
18  *  contributors may be used to endorse or promote products derived from this
19  *  software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
25  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31  * POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 /*
35  * vim:shiftwidth=8:noexpandtab
36  *
37  * @file dpdk/pmd/nfp_net.c
38  *
39  * Netronome vNIC DPDK Poll-Mode Driver: Main entry point
40  */
41
42 #include <rte_byteorder.h>
43 #include <rte_common.h>
44 #include <rte_log.h>
45 #include <rte_debug.h>
46 #include <rte_ethdev.h>
47 #include <rte_ethdev_pci.h>
48 #include <rte_dev.h>
49 #include <rte_ether.h>
50 #include <rte_malloc.h>
51 #include <rte_memzone.h>
52 #include <rte_mempool.h>
53 #include <rte_version.h>
54 #include <rte_string_fns.h>
55 #include <rte_alarm.h>
56 #include <rte_spinlock.h>
57
58 #include "nfp_net_pmd.h"
59 #include "nfp_net_logs.h"
60 #include "nfp_net_ctrl.h"
61
62 /* Prototypes */
63 static void nfp_net_close(struct rte_eth_dev *dev);
64 static int nfp_net_configure(struct rte_eth_dev *dev);
65 static void nfp_net_dev_interrupt_handler(void *param);
66 static void nfp_net_dev_interrupt_delayed_handler(void *param);
67 static int nfp_net_dev_mtu_set(struct rte_eth_dev *dev, uint16_t mtu);
68 static void nfp_net_infos_get(struct rte_eth_dev *dev,
69                               struct rte_eth_dev_info *dev_info);
70 static int nfp_net_init(struct rte_eth_dev *eth_dev);
71 static int nfp_net_link_update(struct rte_eth_dev *dev, int wait_to_complete);
72 static void nfp_net_promisc_enable(struct rte_eth_dev *dev);
73 static void nfp_net_promisc_disable(struct rte_eth_dev *dev);
74 static int nfp_net_rx_fill_freelist(struct nfp_net_rxq *rxq);
75 static uint32_t nfp_net_rx_queue_count(struct rte_eth_dev *dev,
76                                        uint16_t queue_idx);
77 static uint16_t nfp_net_recv_pkts(void *rx_queue, struct rte_mbuf **rx_pkts,
78                                   uint16_t nb_pkts);
79 static void nfp_net_rx_queue_release(void *rxq);
80 static int nfp_net_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 static int nfp_net_tx_free_bufs(struct nfp_net_txq *txq);
85 static void nfp_net_tx_queue_release(void *txq);
86 static int nfp_net_tx_queue_setup(struct rte_eth_dev *dev, uint16_t queue_idx,
87                                   uint16_t nb_desc, unsigned int socket_id,
88                                   const struct rte_eth_txconf *tx_conf);
89 static int nfp_net_start(struct rte_eth_dev *dev);
90 static void nfp_net_stats_get(struct rte_eth_dev *dev,
91                               struct rte_eth_stats *stats);
92 static void nfp_net_stats_reset(struct rte_eth_dev *dev);
93 static void nfp_net_stop(struct rte_eth_dev *dev);
94 static uint16_t nfp_net_xmit_pkts(void *tx_queue, struct rte_mbuf **tx_pkts,
95                                   uint16_t nb_pkts);
96
97 /*
98  * The offset of the queue controller queues in the PCIe Target. These
99  * happen to be at the same offset on the NFP6000 and the NFP3200 so
100  * we use a single macro here.
101  */
102 #define NFP_PCIE_QUEUE(_q)      (0x80000 + (0x800 * ((_q) & 0xff)))
103
104 /* Maximum value which can be added to a queue with one transaction */
105 #define NFP_QCP_MAX_ADD 0x7f
106
107 #define RTE_MBUF_DMA_ADDR_DEFAULT(mb) \
108         (uint64_t)((mb)->buf_physaddr + RTE_PKTMBUF_HEADROOM)
109
110 /* nfp_qcp_ptr - Read or Write Pointer of a queue */
111 enum nfp_qcp_ptr {
112         NFP_QCP_READ_PTR = 0,
113         NFP_QCP_WRITE_PTR
114 };
115
116 /*
117  * nfp_qcp_ptr_add - Add the value to the selected pointer of a queue
118  * @q: Base address for queue structure
119  * @ptr: Add to the Read or Write pointer
120  * @val: Value to add to the queue pointer
121  *
122  * If @val is greater than @NFP_QCP_MAX_ADD multiple writes are performed.
123  */
124 static inline void
125 nfp_qcp_ptr_add(uint8_t *q, enum nfp_qcp_ptr ptr, uint32_t val)
126 {
127         uint32_t off;
128
129         if (ptr == NFP_QCP_READ_PTR)
130                 off = NFP_QCP_QUEUE_ADD_RPTR;
131         else
132                 off = NFP_QCP_QUEUE_ADD_WPTR;
133
134         while (val > NFP_QCP_MAX_ADD) {
135                 nn_writel(rte_cpu_to_le_32(NFP_QCP_MAX_ADD), q + off);
136                 val -= NFP_QCP_MAX_ADD;
137         }
138
139         nn_writel(rte_cpu_to_le_32(val), q + off);
140 }
141
142 /*
143  * nfp_qcp_read - Read the current Read/Write pointer value for a queue
144  * @q:  Base address for queue structure
145  * @ptr: Read or Write pointer
146  */
147 static inline uint32_t
148 nfp_qcp_read(uint8_t *q, enum nfp_qcp_ptr ptr)
149 {
150         uint32_t off;
151         uint32_t val;
152
153         if (ptr == NFP_QCP_READ_PTR)
154                 off = NFP_QCP_QUEUE_STS_LO;
155         else
156                 off = NFP_QCP_QUEUE_STS_HI;
157
158         val = rte_cpu_to_le_32(nn_readl(q + off));
159
160         if (ptr == NFP_QCP_READ_PTR)
161                 return val & NFP_QCP_QUEUE_STS_LO_READPTR_mask;
162         else
163                 return val & NFP_QCP_QUEUE_STS_HI_WRITEPTR_mask;
164 }
165
166 /*
167  * Functions to read/write from/to Config BAR
168  * Performs any endian conversion necessary.
169  */
170 static inline uint8_t
171 nn_cfg_readb(struct nfp_net_hw *hw, int off)
172 {
173         return nn_readb(hw->ctrl_bar + off);
174 }
175
176 static inline void
177 nn_cfg_writeb(struct nfp_net_hw *hw, int off, uint8_t val)
178 {
179         nn_writeb(val, hw->ctrl_bar + off);
180 }
181
182 static inline uint32_t
183 nn_cfg_readl(struct nfp_net_hw *hw, int off)
184 {
185         return rte_le_to_cpu_32(nn_readl(hw->ctrl_bar + off));
186 }
187
188 static inline void
189 nn_cfg_writel(struct nfp_net_hw *hw, int off, uint32_t val)
190 {
191         nn_writel(rte_cpu_to_le_32(val), hw->ctrl_bar + off);
192 }
193
194 static inline uint64_t
195 nn_cfg_readq(struct nfp_net_hw *hw, int off)
196 {
197         return rte_le_to_cpu_64(nn_readq(hw->ctrl_bar + off));
198 }
199
200 static inline void
201 nn_cfg_writeq(struct nfp_net_hw *hw, int off, uint64_t val)
202 {
203         nn_writeq(rte_cpu_to_le_64(val), hw->ctrl_bar + off);
204 }
205
206 /*
207  * Atomically reads link status information from global structure rte_eth_dev.
208  *
209  * @param dev
210  *   - Pointer to the structure rte_eth_dev to read from.
211  *   - Pointer to the buffer to be saved with the link status.
212  *
213  * @return
214  *   - On success, zero.
215  *   - On failure, negative value.
216  */
217 static inline int
218 nfp_net_dev_atomic_read_link_status(struct rte_eth_dev *dev,
219                                     struct rte_eth_link *link)
220 {
221         struct rte_eth_link *dst = link;
222         struct rte_eth_link *src = &dev->data->dev_link;
223
224         if (rte_atomic64_cmpset((uint64_t *)dst, *(uint64_t *)dst,
225                                 *(uint64_t *)src) == 0)
226                 return -1;
227
228         return 0;
229 }
230
231 /*
232  * Atomically writes the link status information into global
233  * structure rte_eth_dev.
234  *
235  * @param dev
236  *   - Pointer to the structure rte_eth_dev to read from.
237  *   - Pointer to the buffer to be saved with the link status.
238  *
239  * @return
240  *   - On success, zero.
241  *   - On failure, negative value.
242  */
243 static inline int
244 nfp_net_dev_atomic_write_link_status(struct rte_eth_dev *dev,
245                                      struct rte_eth_link *link)
246 {
247         struct rte_eth_link *dst = &dev->data->dev_link;
248         struct rte_eth_link *src = link;
249
250         if (rte_atomic64_cmpset((uint64_t *)dst, *(uint64_t *)dst,
251                                 *(uint64_t *)src) == 0)
252                 return -1;
253
254         return 0;
255 }
256
257 static void
258 nfp_net_rx_queue_release_mbufs(struct nfp_net_rxq *rxq)
259 {
260         unsigned i;
261
262         if (rxq->rxbufs == NULL)
263                 return;
264
265         for (i = 0; i < rxq->rx_count; i++) {
266                 if (rxq->rxbufs[i].mbuf) {
267                         rte_pktmbuf_free_seg(rxq->rxbufs[i].mbuf);
268                         rxq->rxbufs[i].mbuf = NULL;
269                 }
270         }
271 }
272
273 static void
274 nfp_net_rx_queue_release(void *rx_queue)
275 {
276         struct nfp_net_rxq *rxq = rx_queue;
277
278         if (rxq) {
279                 nfp_net_rx_queue_release_mbufs(rxq);
280                 rte_free(rxq->rxbufs);
281                 rte_free(rxq);
282         }
283 }
284
285 static void
286 nfp_net_reset_rx_queue(struct nfp_net_rxq *rxq)
287 {
288         nfp_net_rx_queue_release_mbufs(rxq);
289         rxq->rd_p = 0;
290         rxq->nb_rx_hold = 0;
291 }
292
293 static void
294 nfp_net_tx_queue_release_mbufs(struct nfp_net_txq *txq)
295 {
296         unsigned i;
297
298         if (txq->txbufs == NULL)
299                 return;
300
301         for (i = 0; i < txq->tx_count; i++) {
302                 if (txq->txbufs[i].mbuf) {
303                         rte_pktmbuf_free(txq->txbufs[i].mbuf);
304                         txq->txbufs[i].mbuf = NULL;
305                 }
306         }
307 }
308
309 static void
310 nfp_net_tx_queue_release(void *tx_queue)
311 {
312         struct nfp_net_txq *txq = tx_queue;
313
314         if (txq) {
315                 nfp_net_tx_queue_release_mbufs(txq);
316                 rte_free(txq->txbufs);
317                 rte_free(txq);
318         }
319 }
320
321 static void
322 nfp_net_reset_tx_queue(struct nfp_net_txq *txq)
323 {
324         nfp_net_tx_queue_release_mbufs(txq);
325         txq->wr_p = 0;
326         txq->rd_p = 0;
327 }
328
329 static int
330 __nfp_net_reconfig(struct nfp_net_hw *hw, uint32_t update)
331 {
332         int cnt;
333         uint32_t new;
334         struct timespec wait;
335
336         PMD_DRV_LOG(DEBUG, "Writing to the configuration queue (%p)...\n",
337                     hw->qcp_cfg);
338
339         if (hw->qcp_cfg == NULL)
340                 rte_panic("Bad configuration queue pointer\n");
341
342         nfp_qcp_ptr_add(hw->qcp_cfg, NFP_QCP_WRITE_PTR, 1);
343
344         wait.tv_sec = 0;
345         wait.tv_nsec = 1000000;
346
347         PMD_DRV_LOG(DEBUG, "Polling for update ack...\n");
348
349         /* Poll update field, waiting for NFP to ack the config */
350         for (cnt = 0; ; cnt++) {
351                 new = nn_cfg_readl(hw, NFP_NET_CFG_UPDATE);
352                 if (new == 0)
353                         break;
354                 if (new & NFP_NET_CFG_UPDATE_ERR) {
355                         PMD_INIT_LOG(ERR, "Reconfig error: 0x%08x", new);
356                         return -1;
357                 }
358                 if (cnt >= NFP_NET_POLL_TIMEOUT) {
359                         PMD_INIT_LOG(ERR, "Reconfig timeout for 0x%08x after"
360                                           " %dms", update, cnt);
361                         rte_panic("Exiting\n");
362                 }
363                 nanosleep(&wait, 0); /* waiting for a 1ms */
364         }
365         PMD_DRV_LOG(DEBUG, "Ack DONE\n");
366         return 0;
367 }
368
369 /*
370  * Reconfigure the NIC
371  * @nn:    device to reconfigure
372  * @ctrl:    The value for the ctrl field in the BAR config
373  * @update:  The value for the update field in the BAR config
374  *
375  * Write the update word to the BAR and ping the reconfig queue. Then poll
376  * until the firmware has acknowledged the update by zeroing the update word.
377  */
378 static int
379 nfp_net_reconfig(struct nfp_net_hw *hw, uint32_t ctrl, uint32_t update)
380 {
381         uint32_t err;
382
383         PMD_DRV_LOG(DEBUG, "nfp_net_reconfig: ctrl=%08x update=%08x\n",
384                     ctrl, update);
385
386         rte_spinlock_lock(&hw->reconfig_lock);
387
388         nn_cfg_writel(hw, NFP_NET_CFG_CTRL, ctrl);
389         nn_cfg_writel(hw, NFP_NET_CFG_UPDATE, update);
390
391         rte_wmb();
392
393         err = __nfp_net_reconfig(hw, update);
394
395         rte_spinlock_unlock(&hw->reconfig_lock);
396
397         if (!err)
398                 return 0;
399
400         /*
401          * Reconfig errors imply situations where they can be handled.
402          * Otherwise, rte_panic is called inside __nfp_net_reconfig
403          */
404         PMD_INIT_LOG(ERR, "Error nfp_net reconfig for ctrl: %x update: %x",
405                      ctrl, update);
406         return -EIO;
407 }
408
409 /*
410  * Configure an Ethernet device. This function must be invoked first
411  * before any other function in the Ethernet API. This function can
412  * also be re-invoked when a device is in the stopped state.
413  */
414 static int
415 nfp_net_configure(struct rte_eth_dev *dev)
416 {
417         struct rte_eth_conf *dev_conf;
418         struct rte_eth_rxmode *rxmode;
419         struct rte_eth_txmode *txmode;
420         uint32_t new_ctrl = 0;
421         uint32_t update = 0;
422         struct nfp_net_hw *hw;
423
424         hw = NFP_NET_DEV_PRIVATE_TO_HW(dev->data->dev_private);
425
426         /*
427          * A DPDK app sends info about how many queues to use and how
428          * those queues need to be configured. This is used by the
429          * DPDK core and it makes sure no more queues than those
430          * advertised by the driver are requested. This function is
431          * called after that internal process
432          */
433
434         PMD_INIT_LOG(DEBUG, "Configure");
435
436         dev_conf = &dev->data->dev_conf;
437         rxmode = &dev_conf->rxmode;
438         txmode = &dev_conf->txmode;
439
440         /* Checking TX mode */
441         if (txmode->mq_mode) {
442                 PMD_INIT_LOG(INFO, "TX mq_mode DCB and VMDq not supported");
443                 return -EINVAL;
444         }
445
446         /* Checking RX mode */
447         if (rxmode->mq_mode & ETH_MQ_RX_RSS) {
448                 if (hw->cap & NFP_NET_CFG_CTRL_RSS) {
449                         update = NFP_NET_CFG_UPDATE_RSS;
450                         new_ctrl = NFP_NET_CFG_CTRL_RSS;
451                 } else {
452                         PMD_INIT_LOG(INFO, "RSS not supported");
453                         return -EINVAL;
454                 }
455         }
456
457         if (rxmode->split_hdr_size) {
458                 PMD_INIT_LOG(INFO, "rxmode does not support split header");
459                 return -EINVAL;
460         }
461
462         if (rxmode->hw_ip_checksum) {
463                 if (hw->cap & NFP_NET_CFG_CTRL_RXCSUM) {
464                         new_ctrl |= NFP_NET_CFG_CTRL_RXCSUM;
465                 } else {
466                         PMD_INIT_LOG(INFO, "RXCSUM not supported");
467                         return -EINVAL;
468                 }
469         }
470
471         if (rxmode->hw_vlan_filter) {
472                 PMD_INIT_LOG(INFO, "VLAN filter not supported");
473                 return -EINVAL;
474         }
475
476         if (rxmode->hw_vlan_strip) {
477                 if (hw->cap & NFP_NET_CFG_CTRL_RXVLAN) {
478                         new_ctrl |= NFP_NET_CFG_CTRL_RXVLAN;
479                 } else {
480                         PMD_INIT_LOG(INFO, "hw vlan strip not supported");
481                         return -EINVAL;
482                 }
483         }
484
485         if (rxmode->hw_vlan_extend) {
486                 PMD_INIT_LOG(INFO, "VLAN extended not supported");
487                 return -EINVAL;
488         }
489
490         /* Supporting VLAN insertion by default */
491         if (hw->cap & NFP_NET_CFG_CTRL_TXVLAN)
492                 new_ctrl |= NFP_NET_CFG_CTRL_TXVLAN;
493
494         if (rxmode->jumbo_frame)
495                 /* this is handled in rte_eth_dev_configure */
496
497         if (rxmode->hw_strip_crc) {
498                 PMD_INIT_LOG(INFO, "strip CRC not supported");
499                 return -EINVAL;
500         }
501
502         if (rxmode->enable_scatter) {
503                 PMD_INIT_LOG(INFO, "Scatter not supported");
504                 return -EINVAL;
505         }
506
507         if (!new_ctrl)
508                 return 0;
509
510         update |= NFP_NET_CFG_UPDATE_GEN;
511
512         nn_cfg_writel(hw, NFP_NET_CFG_CTRL, new_ctrl);
513         if (nfp_net_reconfig(hw, new_ctrl, update) < 0)
514                 return -EIO;
515
516         hw->ctrl = new_ctrl;
517
518         return 0;
519 }
520
521 static void
522 nfp_net_enable_queues(struct rte_eth_dev *dev)
523 {
524         struct nfp_net_hw *hw;
525         uint64_t enabled_queues = 0;
526         int i;
527
528         hw = NFP_NET_DEV_PRIVATE_TO_HW(dev->data->dev_private);
529
530         /* Enabling the required TX queues in the device */
531         for (i = 0; i < dev->data->nb_tx_queues; i++)
532                 enabled_queues |= (1 << i);
533
534         nn_cfg_writeq(hw, NFP_NET_CFG_TXRS_ENABLE, enabled_queues);
535
536         enabled_queues = 0;
537
538         /* Enabling the required RX queues in the device */
539         for (i = 0; i < dev->data->nb_rx_queues; i++)
540                 enabled_queues |= (1 << i);
541
542         nn_cfg_writeq(hw, NFP_NET_CFG_RXRS_ENABLE, enabled_queues);
543 }
544
545 static void
546 nfp_net_disable_queues(struct rte_eth_dev *dev)
547 {
548         struct nfp_net_hw *hw;
549         uint32_t new_ctrl, update = 0;
550
551         hw = NFP_NET_DEV_PRIVATE_TO_HW(dev->data->dev_private);
552
553         nn_cfg_writeq(hw, NFP_NET_CFG_TXRS_ENABLE, 0);
554         nn_cfg_writeq(hw, NFP_NET_CFG_RXRS_ENABLE, 0);
555
556         new_ctrl = hw->ctrl & ~NFP_NET_CFG_CTRL_ENABLE;
557         update = NFP_NET_CFG_UPDATE_GEN | NFP_NET_CFG_UPDATE_RING |
558                  NFP_NET_CFG_UPDATE_MSIX;
559
560         if (hw->cap & NFP_NET_CFG_CTRL_RINGCFG)
561                 new_ctrl &= ~NFP_NET_CFG_CTRL_RINGCFG;
562
563         /* If an error when reconfig we avoid to change hw state */
564         if (nfp_net_reconfig(hw, new_ctrl, update) < 0)
565                 return;
566
567         hw->ctrl = new_ctrl;
568 }
569
570 static int
571 nfp_net_rx_freelist_setup(struct rte_eth_dev *dev)
572 {
573         int i;
574
575         for (i = 0; i < dev->data->nb_rx_queues; i++) {
576                 if (nfp_net_rx_fill_freelist(dev->data->rx_queues[i]) < 0)
577                         return -1;
578         }
579         return 0;
580 }
581
582 static void
583 nfp_net_params_setup(struct nfp_net_hw *hw)
584 {
585         nn_cfg_writel(hw, NFP_NET_CFG_MTU, hw->mtu);
586         nn_cfg_writel(hw, NFP_NET_CFG_FLBUFSZ, hw->flbufsz);
587 }
588
589 static void
590 nfp_net_cfg_queue_setup(struct nfp_net_hw *hw)
591 {
592         hw->qcp_cfg = hw->tx_bar + NFP_QCP_QUEUE_ADDR_SZ;
593 }
594
595 static void nfp_net_read_mac(struct nfp_net_hw *hw)
596 {
597         uint32_t tmp;
598
599         tmp = rte_be_to_cpu_32(nn_cfg_readl(hw, NFP_NET_CFG_MACADDR));
600         memcpy(&hw->mac_addr[0], &tmp, sizeof(struct ether_addr));
601
602         tmp = rte_be_to_cpu_32(nn_cfg_readl(hw, NFP_NET_CFG_MACADDR + 4));
603         memcpy(&hw->mac_addr[4], &tmp, 2);
604 }
605
606 static void
607 nfp_net_write_mac(struct nfp_net_hw *hw, uint8_t *mac)
608 {
609         uint32_t mac0 = *(uint32_t *)mac;
610         uint16_t mac1;
611
612         nn_writel(rte_cpu_to_be_32(mac0), hw->ctrl_bar + NFP_NET_CFG_MACADDR);
613
614         mac += 4;
615         mac1 = *(uint16_t *)mac;
616         nn_writew(rte_cpu_to_be_16(mac1),
617                   hw->ctrl_bar + NFP_NET_CFG_MACADDR + 6);
618 }
619
620 static int
621 nfp_configure_rx_interrupt(struct rte_eth_dev *dev,
622                            struct rte_intr_handle *intr_handle)
623 {
624         struct nfp_net_hw *hw;
625         int i;
626
627         if (!intr_handle->intr_vec) {
628                 intr_handle->intr_vec =
629                         rte_zmalloc("intr_vec",
630                                     dev->data->nb_rx_queues * sizeof(int), 0);
631                 if (!intr_handle->intr_vec) {
632                         PMD_INIT_LOG(ERR, "Failed to allocate %d rx_queues"
633                                      " intr_vec", dev->data->nb_rx_queues);
634                         return -ENOMEM;
635                 }
636         }
637
638         hw = NFP_NET_DEV_PRIVATE_TO_HW(dev->data->dev_private);
639
640         if (intr_handle->type == RTE_INTR_HANDLE_UIO) {
641                 PMD_INIT_LOG(INFO, "VF: enabling RX interrupt with UIO");
642                 /* UIO just supports one queue and no LSC*/
643                 nn_cfg_writeb(hw, NFP_NET_CFG_RXR_VEC(0), 0);
644                 intr_handle->intr_vec[0] = 0;
645         } else {
646                 PMD_INIT_LOG(INFO, "VF: enabling RX interrupt with VFIO");
647                 for (i = 0; i < dev->data->nb_rx_queues; i++) {
648                         /*
649                          * The first msix vector is reserved for non
650                          * efd interrupts
651                         */
652                         nn_cfg_writeb(hw, NFP_NET_CFG_RXR_VEC(i), i + 1);
653                         intr_handle->intr_vec[i] = i + 1;
654                         PMD_INIT_LOG(DEBUG, "intr_vec[%d]= %d\n", i,
655                                             intr_handle->intr_vec[i]);
656                 }
657         }
658
659         /* Avoiding TX interrupts */
660         hw->ctrl |= NFP_NET_CFG_CTRL_MSIX_TX_OFF;
661         return 0;
662 }
663
664 static int
665 nfp_net_start(struct rte_eth_dev *dev)
666 {
667         struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev);
668         struct rte_intr_handle *intr_handle = &pci_dev->intr_handle;
669         uint32_t new_ctrl, update = 0;
670         struct nfp_net_hw *hw;
671         uint32_t intr_vector;
672         int ret;
673
674         hw = NFP_NET_DEV_PRIVATE_TO_HW(dev->data->dev_private);
675
676         PMD_INIT_LOG(DEBUG, "Start");
677
678         /* Disabling queues just in case... */
679         nfp_net_disable_queues(dev);
680
681         /* Writing configuration parameters in the device */
682         nfp_net_params_setup(hw);
683
684         /* Enabling the required queues in the device */
685         nfp_net_enable_queues(dev);
686
687         /* check and configure queue intr-vector mapping */
688         if (dev->data->dev_conf.intr_conf.rxq != 0) {
689                 if (intr_handle->type == RTE_INTR_HANDLE_UIO) {
690                         /*
691                          * Better not to share LSC with RX interrupts.
692                          * Unregistering LSC interrupt handler
693                          */
694                         rte_intr_callback_unregister(&pci_dev->intr_handle,
695                                 nfp_net_dev_interrupt_handler, (void *)dev);
696
697                         if (dev->data->nb_rx_queues > 1) {
698                                 PMD_INIT_LOG(ERR, "PMD rx interrupt only "
699                                              "supports 1 queue with UIO");
700                                 return -EIO;
701                         }
702                 }
703                 intr_vector = dev->data->nb_rx_queues;
704                 if (rte_intr_efd_enable(intr_handle, intr_vector))
705                         return -1;
706
707                 nfp_configure_rx_interrupt(dev, intr_handle);
708                 update = NFP_NET_CFG_UPDATE_MSIX;
709         }
710
711         rte_intr_enable(intr_handle);
712
713         /* Enable device */
714         new_ctrl = hw->ctrl | NFP_NET_CFG_CTRL_ENABLE;
715
716         update |= NFP_NET_CFG_UPDATE_GEN | NFP_NET_CFG_UPDATE_RING;
717
718         if (hw->cap & NFP_NET_CFG_CTRL_RINGCFG)
719                 new_ctrl |= NFP_NET_CFG_CTRL_RINGCFG;
720
721         nn_cfg_writel(hw, NFP_NET_CFG_CTRL, new_ctrl);
722         if (nfp_net_reconfig(hw, new_ctrl, update) < 0)
723                 return -EIO;
724
725         /*
726          * Allocating rte mbuffs for configured rx queues.
727          * This requires queues being enabled before
728          */
729         if (nfp_net_rx_freelist_setup(dev) < 0) {
730                 ret = -ENOMEM;
731                 goto error;
732         }
733
734         hw->ctrl = new_ctrl;
735
736         return 0;
737
738 error:
739         /*
740          * An error returned by this function should mean the app
741          * exiting and then the system releasing all the memory
742          * allocated even memory coming from hugepages.
743          *
744          * The device could be enabled at this point with some queues
745          * ready for getting packets. This is true if the call to
746          * nfp_net_rx_freelist_setup() succeeds for some queues but
747          * fails for subsequent queues.
748          *
749          * This should make the app exiting but better if we tell the
750          * device first.
751          */
752         nfp_net_disable_queues(dev);
753
754         return ret;
755 }
756
757 /* Stop device: disable rx and tx functions to allow for reconfiguring. */
758 static void
759 nfp_net_stop(struct rte_eth_dev *dev)
760 {
761         int i;
762
763         PMD_INIT_LOG(DEBUG, "Stop");
764
765         nfp_net_disable_queues(dev);
766
767         /* Clear queues */
768         for (i = 0; i < dev->data->nb_tx_queues; i++) {
769                 nfp_net_reset_tx_queue(
770                         (struct nfp_net_txq *)dev->data->tx_queues[i]);
771         }
772
773         for (i = 0; i < dev->data->nb_rx_queues; i++) {
774                 nfp_net_reset_rx_queue(
775                         (struct nfp_net_rxq *)dev->data->rx_queues[i]);
776         }
777 }
778
779 /* Reset and stop device. The device can not be restarted. */
780 static void
781 nfp_net_close(struct rte_eth_dev *dev)
782 {
783         struct nfp_net_hw *hw;
784         struct rte_pci_device *pci_dev;
785
786         PMD_INIT_LOG(DEBUG, "Close");
787
788         hw = NFP_NET_DEV_PRIVATE_TO_HW(dev->data->dev_private);
789         pci_dev = RTE_ETH_DEV_TO_PCI(dev);
790
791         /*
792          * We assume that the DPDK application is stopping all the
793          * threads/queues before calling the device close function.
794          */
795
796         nfp_net_stop(dev);
797
798         rte_intr_disable(&pci_dev->intr_handle);
799         nn_cfg_writeb(hw, NFP_NET_CFG_LSC, 0xff);
800
801         /* unregister callback func from eal lib */
802         rte_intr_callback_unregister(&pci_dev->intr_handle,
803                                      nfp_net_dev_interrupt_handler,
804                                      (void *)dev);
805
806         /*
807          * The ixgbe PMD driver disables the pcie master on the
808          * device. The i40e does not...
809          */
810 }
811
812 static void
813 nfp_net_promisc_enable(struct rte_eth_dev *dev)
814 {
815         uint32_t new_ctrl, update = 0;
816         struct nfp_net_hw *hw;
817
818         PMD_DRV_LOG(DEBUG, "Promiscuous mode enable\n");
819
820         hw = NFP_NET_DEV_PRIVATE_TO_HW(dev->data->dev_private);
821
822         if (!(hw->cap & NFP_NET_CFG_CTRL_PROMISC)) {
823                 PMD_INIT_LOG(INFO, "Promiscuous mode not supported");
824                 return;
825         }
826
827         if (hw->ctrl & NFP_NET_CFG_CTRL_PROMISC) {
828                 PMD_DRV_LOG(INFO, "Promiscuous mode already enabled\n");
829                 return;
830         }
831
832         new_ctrl = hw->ctrl | NFP_NET_CFG_CTRL_PROMISC;
833         update = NFP_NET_CFG_UPDATE_GEN;
834
835         /*
836          * DPDK sets promiscuous mode on just after this call assuming
837          * it can not fail ...
838          */
839         if (nfp_net_reconfig(hw, new_ctrl, update) < 0)
840                 return;
841
842         hw->ctrl = new_ctrl;
843 }
844
845 static void
846 nfp_net_promisc_disable(struct rte_eth_dev *dev)
847 {
848         uint32_t new_ctrl, update = 0;
849         struct nfp_net_hw *hw;
850
851         hw = NFP_NET_DEV_PRIVATE_TO_HW(dev->data->dev_private);
852
853         if ((hw->ctrl & NFP_NET_CFG_CTRL_PROMISC) == 0) {
854                 PMD_DRV_LOG(INFO, "Promiscuous mode already disabled\n");
855                 return;
856         }
857
858         new_ctrl = hw->ctrl & ~NFP_NET_CFG_CTRL_PROMISC;
859         update = NFP_NET_CFG_UPDATE_GEN;
860
861         /*
862          * DPDK sets promiscuous mode off just before this call
863          * assuming it can not fail ...
864          */
865         if (nfp_net_reconfig(hw, new_ctrl, update) < 0)
866                 return;
867
868         hw->ctrl = new_ctrl;
869 }
870
871 /*
872  * return 0 means link status changed, -1 means not changed
873  *
874  * Wait to complete is needed as it can take up to 9 seconds to get the Link
875  * status.
876  */
877 static int
878 nfp_net_link_update(struct rte_eth_dev *dev, __rte_unused int wait_to_complete)
879 {
880         struct nfp_net_hw *hw;
881         struct rte_eth_link link, old;
882         uint32_t nn_link_status;
883
884         static const uint32_t ls_to_ethtool[] = {
885                 [NFP_NET_CFG_STS_LINK_RATE_UNSUPPORTED] = ETH_SPEED_NUM_NONE,
886                 [NFP_NET_CFG_STS_LINK_RATE_UNKNOWN]     = ETH_SPEED_NUM_NONE,
887                 [NFP_NET_CFG_STS_LINK_RATE_1G]          = ETH_SPEED_NUM_1G,
888                 [NFP_NET_CFG_STS_LINK_RATE_10G]         = ETH_SPEED_NUM_10G,
889                 [NFP_NET_CFG_STS_LINK_RATE_25G]         = ETH_SPEED_NUM_25G,
890                 [NFP_NET_CFG_STS_LINK_RATE_40G]         = ETH_SPEED_NUM_40G,
891                 [NFP_NET_CFG_STS_LINK_RATE_50G]         = ETH_SPEED_NUM_50G,
892                 [NFP_NET_CFG_STS_LINK_RATE_100G]        = ETH_SPEED_NUM_100G,
893         };
894
895         PMD_DRV_LOG(DEBUG, "Link update\n");
896
897         hw = NFP_NET_DEV_PRIVATE_TO_HW(dev->data->dev_private);
898
899         memset(&old, 0, sizeof(old));
900         nfp_net_dev_atomic_read_link_status(dev, &old);
901
902         nn_link_status = nn_cfg_readl(hw, NFP_NET_CFG_STS);
903
904         memset(&link, 0, sizeof(struct rte_eth_link));
905
906         if (nn_link_status & NFP_NET_CFG_STS_LINK)
907                 link.link_status = ETH_LINK_UP;
908
909         link.link_duplex = ETH_LINK_FULL_DUPLEX;
910
911         nn_link_status = (nn_link_status >> NFP_NET_CFG_STS_LINK_RATE_SHIFT) &
912                          NFP_NET_CFG_STS_LINK_RATE_MASK;
913
914         if ((NFD_CFG_MAJOR_VERSION_of(hw->ver) < 4) ||
915             ((NFD_CFG_MINOR_VERSION_of(hw->ver) == 4) &&
916             (NFD_CFG_MINOR_VERSION_of(hw->ver) == 0)))
917                 /* We really do not know the speed wil old firmware */
918                 link.link_speed = ETH_SPEED_NUM_NONE;
919         else {
920                 if (nn_link_status >= RTE_DIM(ls_to_ethtool))
921                         link.link_speed = ETH_SPEED_NUM_NONE;
922                 else
923                         link.link_speed = ls_to_ethtool[nn_link_status];
924         }
925
926         if (old.link_status != link.link_status) {
927                 nfp_net_dev_atomic_write_link_status(dev, &link);
928                 if (link.link_status)
929                         PMD_DRV_LOG(INFO, "NIC Link is Up\n");
930                 else
931                         PMD_DRV_LOG(INFO, "NIC Link is Down\n");
932                 return 0;
933         }
934
935         return -1;
936 }
937
938 static void
939 nfp_net_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
940 {
941         int i;
942         struct nfp_net_hw *hw;
943         struct rte_eth_stats nfp_dev_stats;
944
945         hw = NFP_NET_DEV_PRIVATE_TO_HW(dev->data->dev_private);
946
947         /* RTE_ETHDEV_QUEUE_STAT_CNTRS default value is 16 */
948
949         /* reading per RX ring stats */
950         for (i = 0; i < dev->data->nb_rx_queues; i++) {
951                 if (i == RTE_ETHDEV_QUEUE_STAT_CNTRS)
952                         break;
953
954                 nfp_dev_stats.q_ipackets[i] =
955                         nn_cfg_readq(hw, NFP_NET_CFG_RXR_STATS(i));
956
957                 nfp_dev_stats.q_ipackets[i] -=
958                         hw->eth_stats_base.q_ipackets[i];
959
960                 nfp_dev_stats.q_ibytes[i] =
961                         nn_cfg_readq(hw, NFP_NET_CFG_RXR_STATS(i) + 0x8);
962
963                 nfp_dev_stats.q_ibytes[i] -=
964                         hw->eth_stats_base.q_ibytes[i];
965         }
966
967         /* reading per TX ring stats */
968         for (i = 0; i < dev->data->nb_tx_queues; i++) {
969                 if (i == RTE_ETHDEV_QUEUE_STAT_CNTRS)
970                         break;
971
972                 nfp_dev_stats.q_opackets[i] =
973                         nn_cfg_readq(hw, NFP_NET_CFG_TXR_STATS(i));
974
975                 nfp_dev_stats.q_opackets[i] -=
976                         hw->eth_stats_base.q_opackets[i];
977
978                 nfp_dev_stats.q_obytes[i] =
979                         nn_cfg_readq(hw, NFP_NET_CFG_TXR_STATS(i) + 0x8);
980
981                 nfp_dev_stats.q_obytes[i] -=
982                         hw->eth_stats_base.q_obytes[i];
983         }
984
985         nfp_dev_stats.ipackets =
986                 nn_cfg_readq(hw, NFP_NET_CFG_STATS_RX_FRAMES);
987
988         nfp_dev_stats.ipackets -= hw->eth_stats_base.ipackets;
989
990         nfp_dev_stats.ibytes =
991                 nn_cfg_readq(hw, NFP_NET_CFG_STATS_RX_OCTETS);
992
993         nfp_dev_stats.ibytes -= hw->eth_stats_base.ibytes;
994
995         nfp_dev_stats.opackets =
996                 nn_cfg_readq(hw, NFP_NET_CFG_STATS_TX_FRAMES);
997
998         nfp_dev_stats.opackets -= hw->eth_stats_base.opackets;
999
1000         nfp_dev_stats.obytes =
1001                 nn_cfg_readq(hw, NFP_NET_CFG_STATS_TX_OCTETS);
1002
1003         nfp_dev_stats.obytes -= hw->eth_stats_base.obytes;
1004
1005         /* reading general device stats */
1006         nfp_dev_stats.ierrors =
1007                 nn_cfg_readq(hw, NFP_NET_CFG_STATS_RX_ERRORS);
1008
1009         nfp_dev_stats.ierrors -= hw->eth_stats_base.ierrors;
1010
1011         nfp_dev_stats.oerrors =
1012                 nn_cfg_readq(hw, NFP_NET_CFG_STATS_TX_ERRORS);
1013
1014         nfp_dev_stats.oerrors -= hw->eth_stats_base.oerrors;
1015
1016         /* RX ring mbuf allocation failures */
1017         nfp_dev_stats.rx_nombuf = dev->data->rx_mbuf_alloc_failed;
1018
1019         nfp_dev_stats.imissed =
1020                 nn_cfg_readq(hw, NFP_NET_CFG_STATS_RX_DISCARDS);
1021
1022         nfp_dev_stats.imissed -= hw->eth_stats_base.imissed;
1023
1024         if (stats)
1025                 memcpy(stats, &nfp_dev_stats, sizeof(*stats));
1026 }
1027
1028 static void
1029 nfp_net_stats_reset(struct rte_eth_dev *dev)
1030 {
1031         int i;
1032         struct nfp_net_hw *hw;
1033
1034         hw = NFP_NET_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1035
1036         /*
1037          * hw->eth_stats_base records the per counter starting point.
1038          * Lets update it now
1039          */
1040
1041         /* reading per RX ring stats */
1042         for (i = 0; i < dev->data->nb_rx_queues; i++) {
1043                 if (i == RTE_ETHDEV_QUEUE_STAT_CNTRS)
1044                         break;
1045
1046                 hw->eth_stats_base.q_ipackets[i] =
1047                         nn_cfg_readq(hw, NFP_NET_CFG_RXR_STATS(i));
1048
1049                 hw->eth_stats_base.q_ibytes[i] =
1050                         nn_cfg_readq(hw, NFP_NET_CFG_RXR_STATS(i) + 0x8);
1051         }
1052
1053         /* reading per TX ring stats */
1054         for (i = 0; i < dev->data->nb_tx_queues; i++) {
1055                 if (i == RTE_ETHDEV_QUEUE_STAT_CNTRS)
1056                         break;
1057
1058                 hw->eth_stats_base.q_opackets[i] =
1059                         nn_cfg_readq(hw, NFP_NET_CFG_TXR_STATS(i));
1060
1061                 hw->eth_stats_base.q_obytes[i] =
1062                         nn_cfg_readq(hw, NFP_NET_CFG_TXR_STATS(i) + 0x8);
1063         }
1064
1065         hw->eth_stats_base.ipackets =
1066                 nn_cfg_readq(hw, NFP_NET_CFG_STATS_RX_FRAMES);
1067
1068         hw->eth_stats_base.ibytes =
1069                 nn_cfg_readq(hw, NFP_NET_CFG_STATS_RX_OCTETS);
1070
1071         hw->eth_stats_base.opackets =
1072                 nn_cfg_readq(hw, NFP_NET_CFG_STATS_TX_FRAMES);
1073
1074         hw->eth_stats_base.obytes =
1075                 nn_cfg_readq(hw, NFP_NET_CFG_STATS_TX_OCTETS);
1076
1077         /* reading general device stats */
1078         hw->eth_stats_base.ierrors =
1079                 nn_cfg_readq(hw, NFP_NET_CFG_STATS_RX_ERRORS);
1080
1081         hw->eth_stats_base.oerrors =
1082                 nn_cfg_readq(hw, NFP_NET_CFG_STATS_TX_ERRORS);
1083
1084         /* RX ring mbuf allocation failures */
1085         dev->data->rx_mbuf_alloc_failed = 0;
1086
1087         hw->eth_stats_base.imissed =
1088                 nn_cfg_readq(hw, NFP_NET_CFG_STATS_RX_DISCARDS);
1089 }
1090
1091 static void
1092 nfp_net_infos_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *dev_info)
1093 {
1094         struct nfp_net_hw *hw;
1095
1096         hw = NFP_NET_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1097
1098         dev_info->pci_dev = RTE_ETH_DEV_TO_PCI(dev);
1099         dev_info->max_rx_queues = (uint16_t)hw->max_rx_queues;
1100         dev_info->max_tx_queues = (uint16_t)hw->max_tx_queues;
1101         dev_info->min_rx_bufsize = ETHER_MIN_MTU;
1102         dev_info->max_rx_pktlen = hw->mtu;
1103         /* Next should change when PF support is implemented */
1104         dev_info->max_mac_addrs = 1;
1105
1106         if (hw->cap & NFP_NET_CFG_CTRL_RXVLAN)
1107                 dev_info->rx_offload_capa = DEV_RX_OFFLOAD_VLAN_STRIP;
1108
1109         if (hw->cap & NFP_NET_CFG_CTRL_RXCSUM)
1110                 dev_info->rx_offload_capa |= DEV_RX_OFFLOAD_IPV4_CKSUM |
1111                                              DEV_RX_OFFLOAD_UDP_CKSUM |
1112                                              DEV_RX_OFFLOAD_TCP_CKSUM;
1113
1114         if (hw->cap & NFP_NET_CFG_CTRL_TXVLAN)
1115                 dev_info->tx_offload_capa = DEV_TX_OFFLOAD_VLAN_INSERT;
1116
1117         if (hw->cap & NFP_NET_CFG_CTRL_TXCSUM)
1118                 dev_info->tx_offload_capa |= DEV_TX_OFFLOAD_IPV4_CKSUM |
1119                                              DEV_TX_OFFLOAD_UDP_CKSUM |
1120                                              DEV_TX_OFFLOAD_TCP_CKSUM;
1121
1122         dev_info->default_rxconf = (struct rte_eth_rxconf) {
1123                 .rx_thresh = {
1124                         .pthresh = DEFAULT_RX_PTHRESH,
1125                         .hthresh = DEFAULT_RX_HTHRESH,
1126                         .wthresh = DEFAULT_RX_WTHRESH,
1127                 },
1128                 .rx_free_thresh = DEFAULT_RX_FREE_THRESH,
1129                 .rx_drop_en = 0,
1130         };
1131
1132         dev_info->default_txconf = (struct rte_eth_txconf) {
1133                 .tx_thresh = {
1134                         .pthresh = DEFAULT_TX_PTHRESH,
1135                         .hthresh = DEFAULT_TX_HTHRESH,
1136                         .wthresh = DEFAULT_TX_WTHRESH,
1137                 },
1138                 .tx_free_thresh = DEFAULT_TX_FREE_THRESH,
1139                 .tx_rs_thresh = DEFAULT_TX_RSBIT_THRESH,
1140                 .txq_flags = ETH_TXQ_FLAGS_NOMULTSEGS |
1141                              ETH_TXQ_FLAGS_NOOFFLOADS,
1142         };
1143
1144         dev_info->reta_size = NFP_NET_CFG_RSS_ITBL_SZ;
1145         dev_info->hash_key_size = NFP_NET_CFG_RSS_KEY_SZ;
1146
1147         dev_info->speed_capa = ETH_SPEED_NUM_1G | ETH_LINK_SPEED_10G |
1148                                ETH_SPEED_NUM_25G | ETH_SPEED_NUM_40G |
1149                                ETH_SPEED_NUM_50G | ETH_LINK_SPEED_100G;
1150
1151         if (hw->cap & NFP_NET_CFG_CTRL_LSO)
1152                 dev_info->tx_offload_capa |= DEV_TX_OFFLOAD_TCP_TSO;
1153 }
1154
1155 static const uint32_t *
1156 nfp_net_supported_ptypes_get(struct rte_eth_dev *dev)
1157 {
1158         static const uint32_t ptypes[] = {
1159                 /* refers to nfp_net_set_hash() */
1160                 RTE_PTYPE_INNER_L3_IPV4,
1161                 RTE_PTYPE_INNER_L3_IPV6,
1162                 RTE_PTYPE_INNER_L3_IPV6_EXT,
1163                 RTE_PTYPE_INNER_L4_MASK,
1164                 RTE_PTYPE_UNKNOWN
1165         };
1166
1167         if (dev->rx_pkt_burst == nfp_net_recv_pkts)
1168                 return ptypes;
1169         return NULL;
1170 }
1171
1172 static uint32_t
1173 nfp_net_rx_queue_count(struct rte_eth_dev *dev, uint16_t queue_idx)
1174 {
1175         struct nfp_net_rxq *rxq;
1176         struct nfp_net_rx_desc *rxds;
1177         uint32_t idx;
1178         uint32_t count;
1179
1180         rxq = (struct nfp_net_rxq *)dev->data->rx_queues[queue_idx];
1181
1182         idx = rxq->rd_p;
1183
1184         count = 0;
1185
1186         /*
1187          * Other PMDs are just checking the DD bit in intervals of 4
1188          * descriptors and counting all four if the first has the DD
1189          * bit on. Of course, this is not accurate but can be good for
1190          * performance. But ideally that should be done in descriptors
1191          * chunks belonging to the same cache line
1192          */
1193
1194         while (count < rxq->rx_count) {
1195                 rxds = &rxq->rxds[idx];
1196                 if ((rxds->rxd.meta_len_dd & PCIE_DESC_RX_DD) == 0)
1197                         break;
1198
1199                 count++;
1200                 idx++;
1201
1202                 /* Wrapping? */
1203                 if ((idx) == rxq->rx_count)
1204                         idx = 0;
1205         }
1206
1207         return count;
1208 }
1209
1210 static int
1211 nfp_rx_queue_intr_enable(struct rte_eth_dev *dev, uint16_t queue_id)
1212 {
1213         struct rte_pci_device *pci_dev;
1214         struct nfp_net_hw *hw;
1215         int base = 0;
1216
1217         hw = NFP_NET_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1218         pci_dev = RTE_ETH_DEV_TO_PCI(dev);
1219
1220         if (pci_dev->intr_handle.type != RTE_INTR_HANDLE_UIO)
1221                 base = 1;
1222
1223         /* Make sure all updates are written before un-masking */
1224         rte_wmb();
1225         nn_cfg_writeb(hw, NFP_NET_CFG_ICR(base + queue_id),
1226                       NFP_NET_CFG_ICR_UNMASKED);
1227         return 0;
1228 }
1229
1230 static int
1231 nfp_rx_queue_intr_disable(struct rte_eth_dev *dev, uint16_t queue_id)
1232 {
1233         struct rte_pci_device *pci_dev;
1234         struct nfp_net_hw *hw;
1235         int base = 0;
1236
1237         hw = NFP_NET_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1238         pci_dev = RTE_ETH_DEV_TO_PCI(dev);
1239
1240         if (pci_dev->intr_handle.type != RTE_INTR_HANDLE_UIO)
1241                 base = 1;
1242
1243         /* Make sure all updates are written before un-masking */
1244         rte_wmb();
1245         nn_cfg_writeb(hw, NFP_NET_CFG_ICR(base + queue_id), 0x1);
1246         return 0;
1247 }
1248
1249 static void
1250 nfp_net_dev_link_status_print(struct rte_eth_dev *dev)
1251 {
1252         struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev);
1253         struct rte_eth_link link;
1254
1255         memset(&link, 0, sizeof(link));
1256         nfp_net_dev_atomic_read_link_status(dev, &link);
1257         if (link.link_status)
1258                 RTE_LOG(INFO, PMD, "Port %d: Link Up - speed %u Mbps - %s\n",
1259                         (int)(dev->data->port_id), (unsigned)link.link_speed,
1260                         link.link_duplex == ETH_LINK_FULL_DUPLEX
1261                         ? "full-duplex" : "half-duplex");
1262         else
1263                 RTE_LOG(INFO, PMD, " Port %d: Link Down\n",
1264                         (int)(dev->data->port_id));
1265
1266         RTE_LOG(INFO, PMD, "PCI Address: %04d:%02d:%02d:%d\n",
1267                 pci_dev->addr.domain, pci_dev->addr.bus,
1268                 pci_dev->addr.devid, pci_dev->addr.function);
1269 }
1270
1271 /* Interrupt configuration and handling */
1272
1273 /*
1274  * nfp_net_irq_unmask - Unmask an interrupt
1275  *
1276  * If MSI-X auto-masking is enabled clear the mask bit, otherwise
1277  * clear the ICR for the entry.
1278  */
1279 static void
1280 nfp_net_irq_unmask(struct rte_eth_dev *dev)
1281 {
1282         struct nfp_net_hw *hw;
1283         struct rte_pci_device *pci_dev;
1284
1285         hw = NFP_NET_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1286         pci_dev = RTE_ETH_DEV_TO_PCI(dev);
1287
1288         if (hw->ctrl & NFP_NET_CFG_CTRL_MSIXAUTO) {
1289                 /* If MSI-X auto-masking is used, clear the entry */
1290                 rte_wmb();
1291                 rte_intr_enable(&pci_dev->intr_handle);
1292         } else {
1293                 /* Make sure all updates are written before un-masking */
1294                 rte_wmb();
1295                 nn_cfg_writeb(hw, NFP_NET_CFG_ICR(NFP_NET_IRQ_LSC_IDX),
1296                               NFP_NET_CFG_ICR_UNMASKED);
1297         }
1298 }
1299
1300 static void
1301 nfp_net_dev_interrupt_handler(void *param)
1302 {
1303         int64_t timeout;
1304         struct rte_eth_link link;
1305         struct rte_eth_dev *dev = (struct rte_eth_dev *)param;
1306
1307         PMD_DRV_LOG(DEBUG, "We got a LSC interrupt!!!\n");
1308
1309         /* get the link status */
1310         memset(&link, 0, sizeof(link));
1311         nfp_net_dev_atomic_read_link_status(dev, &link);
1312
1313         nfp_net_link_update(dev, 0);
1314
1315         /* likely to up */
1316         if (!link.link_status) {
1317                 /* handle it 1 sec later, wait it being stable */
1318                 timeout = NFP_NET_LINK_UP_CHECK_TIMEOUT;
1319                 /* likely to down */
1320         } else {
1321                 /* handle it 4 sec later, wait it being stable */
1322                 timeout = NFP_NET_LINK_DOWN_CHECK_TIMEOUT;
1323         }
1324
1325         if (rte_eal_alarm_set(timeout * 1000,
1326                               nfp_net_dev_interrupt_delayed_handler,
1327                               (void *)dev) < 0) {
1328                 RTE_LOG(ERR, PMD, "Error setting alarm");
1329                 /* Unmasking */
1330                 nfp_net_irq_unmask(dev);
1331         }
1332 }
1333
1334 /*
1335  * Interrupt handler which shall be registered for alarm callback for delayed
1336  * handling specific interrupt to wait for the stable nic state. As the NIC
1337  * interrupt state is not stable for nfp after link is just down, it needs
1338  * to wait 4 seconds to get the stable status.
1339  *
1340  * @param handle   Pointer to interrupt handle.
1341  * @param param    The address of parameter (struct rte_eth_dev *)
1342  *
1343  * @return  void
1344  */
1345 static void
1346 nfp_net_dev_interrupt_delayed_handler(void *param)
1347 {
1348         struct rte_eth_dev *dev = (struct rte_eth_dev *)param;
1349
1350         nfp_net_link_update(dev, 0);
1351         _rte_eth_dev_callback_process(dev, RTE_ETH_EVENT_INTR_LSC, NULL, NULL);
1352
1353         nfp_net_dev_link_status_print(dev);
1354
1355         /* Unmasking */
1356         nfp_net_irq_unmask(dev);
1357 }
1358
1359 static int
1360 nfp_net_dev_mtu_set(struct rte_eth_dev *dev, uint16_t mtu)
1361 {
1362         struct nfp_net_hw *hw;
1363
1364         hw = NFP_NET_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1365
1366         /* check that mtu is within the allowed range */
1367         if ((mtu < ETHER_MIN_MTU) || ((uint32_t)mtu > hw->max_mtu))
1368                 return -EINVAL;
1369
1370         /* switch to jumbo mode if needed */
1371         if ((uint32_t)mtu > ETHER_MAX_LEN)
1372                 dev->data->dev_conf.rxmode.jumbo_frame = 1;
1373         else
1374                 dev->data->dev_conf.rxmode.jumbo_frame = 0;
1375
1376         /* update max frame size */
1377         dev->data->dev_conf.rxmode.max_rx_pkt_len = (uint32_t)mtu;
1378
1379         /* writing to configuration space */
1380         nn_cfg_writel(hw, NFP_NET_CFG_MTU, (uint32_t)mtu);
1381
1382         hw->mtu = mtu;
1383
1384         return 0;
1385 }
1386
1387 static int
1388 nfp_net_rx_queue_setup(struct rte_eth_dev *dev,
1389                        uint16_t queue_idx, uint16_t nb_desc,
1390                        unsigned int socket_id,
1391                        const struct rte_eth_rxconf *rx_conf,
1392                        struct rte_mempool *mp)
1393 {
1394         const struct rte_memzone *tz;
1395         struct nfp_net_rxq *rxq;
1396         struct nfp_net_hw *hw;
1397
1398         hw = NFP_NET_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1399
1400         PMD_INIT_FUNC_TRACE();
1401
1402         /* Validating number of descriptors */
1403         if (((nb_desc * sizeof(struct nfp_net_rx_desc)) % 128) != 0 ||
1404             (nb_desc > NFP_NET_MAX_RX_DESC) ||
1405             (nb_desc < NFP_NET_MIN_RX_DESC)) {
1406                 RTE_LOG(ERR, PMD, "Wrong nb_desc value\n");
1407                 return -EINVAL;
1408         }
1409
1410         /*
1411          * Free memory prior to re-allocation if needed. This is the case after
1412          * calling nfp_net_stop
1413          */
1414         if (dev->data->rx_queues[queue_idx]) {
1415                 nfp_net_rx_queue_release(dev->data->rx_queues[queue_idx]);
1416                 dev->data->rx_queues[queue_idx] = NULL;
1417         }
1418
1419         /* Allocating rx queue data structure */
1420         rxq = rte_zmalloc_socket("ethdev RX queue", sizeof(struct nfp_net_rxq),
1421                                  RTE_CACHE_LINE_SIZE, socket_id);
1422         if (rxq == NULL)
1423                 return -ENOMEM;
1424
1425         /* Hw queues mapping based on firmware confifguration */
1426         rxq->qidx = queue_idx;
1427         rxq->fl_qcidx = queue_idx * hw->stride_rx;
1428         rxq->rx_qcidx = rxq->fl_qcidx + (hw->stride_rx - 1);
1429         rxq->qcp_fl = hw->rx_bar + NFP_QCP_QUEUE_OFF(rxq->fl_qcidx);
1430         rxq->qcp_rx = hw->rx_bar + NFP_QCP_QUEUE_OFF(rxq->rx_qcidx);
1431
1432         /*
1433          * Tracking mbuf size for detecting a potential mbuf overflow due to
1434          * RX offset
1435          */
1436         rxq->mem_pool = mp;
1437         rxq->mbuf_size = rxq->mem_pool->elt_size;
1438         rxq->mbuf_size -= (sizeof(struct rte_mbuf) + RTE_PKTMBUF_HEADROOM);
1439         hw->flbufsz = rxq->mbuf_size;
1440
1441         rxq->rx_count = nb_desc;
1442         rxq->port_id = dev->data->port_id;
1443         rxq->rx_free_thresh = rx_conf->rx_free_thresh;
1444         rxq->crc_len = (uint8_t) ((dev->data->dev_conf.rxmode.hw_strip_crc) ? 0
1445                                   : ETHER_CRC_LEN);
1446         rxq->drop_en = rx_conf->rx_drop_en;
1447
1448         /*
1449          * Allocate RX ring hardware descriptors. A memzone large enough to
1450          * handle the maximum ring size is allocated in order to allow for
1451          * resizing in later calls to the queue setup function.
1452          */
1453         tz = rte_eth_dma_zone_reserve(dev, "rx_ring", queue_idx,
1454                                    sizeof(struct nfp_net_rx_desc) *
1455                                    NFP_NET_MAX_RX_DESC, NFP_MEMZONE_ALIGN,
1456                                    socket_id);
1457
1458         if (tz == NULL) {
1459                 RTE_LOG(ERR, PMD, "Error allocatig rx dma\n");
1460                 nfp_net_rx_queue_release(rxq);
1461                 return -ENOMEM;
1462         }
1463
1464         /* Saving physical and virtual addresses for the RX ring */
1465         rxq->dma = (uint64_t)tz->phys_addr;
1466         rxq->rxds = (struct nfp_net_rx_desc *)tz->addr;
1467
1468         /* mbuf pointers array for referencing mbufs linked to RX descriptors */
1469         rxq->rxbufs = rte_zmalloc_socket("rxq->rxbufs",
1470                                          sizeof(*rxq->rxbufs) * nb_desc,
1471                                          RTE_CACHE_LINE_SIZE, socket_id);
1472         if (rxq->rxbufs == NULL) {
1473                 nfp_net_rx_queue_release(rxq);
1474                 return -ENOMEM;
1475         }
1476
1477         PMD_RX_LOG(DEBUG, "rxbufs=%p hw_ring=%p dma_addr=0x%" PRIx64 "\n",
1478                    rxq->rxbufs, rxq->rxds, (unsigned long int)rxq->dma);
1479
1480         nfp_net_reset_rx_queue(rxq);
1481
1482         dev->data->rx_queues[queue_idx] = rxq;
1483         rxq->hw = hw;
1484
1485         /*
1486          * Telling the HW about the physical address of the RX ring and number
1487          * of descriptors in log2 format
1488          */
1489         nn_cfg_writeq(hw, NFP_NET_CFG_RXR_ADDR(queue_idx), rxq->dma);
1490         nn_cfg_writeb(hw, NFP_NET_CFG_RXR_SZ(queue_idx), rte_log2_u32(nb_desc));
1491
1492         return 0;
1493 }
1494
1495 static int
1496 nfp_net_rx_fill_freelist(struct nfp_net_rxq *rxq)
1497 {
1498         struct nfp_net_rx_buff *rxe = rxq->rxbufs;
1499         uint64_t dma_addr;
1500         unsigned i;
1501
1502         PMD_RX_LOG(DEBUG, "nfp_net_rx_fill_freelist for %u descriptors\n",
1503                    rxq->rx_count);
1504
1505         for (i = 0; i < rxq->rx_count; i++) {
1506                 struct nfp_net_rx_desc *rxd;
1507                 struct rte_mbuf *mbuf = rte_pktmbuf_alloc(rxq->mem_pool);
1508
1509                 if (mbuf == NULL) {
1510                         RTE_LOG(ERR, PMD, "RX mbuf alloc failed queue_id=%u\n",
1511                                 (unsigned)rxq->qidx);
1512                         return -ENOMEM;
1513                 }
1514
1515                 dma_addr = rte_cpu_to_le_64(RTE_MBUF_DMA_ADDR_DEFAULT(mbuf));
1516
1517                 rxd = &rxq->rxds[i];
1518                 rxd->fld.dd = 0;
1519                 rxd->fld.dma_addr_hi = (dma_addr >> 32) & 0xff;
1520                 rxd->fld.dma_addr_lo = dma_addr & 0xffffffff;
1521                 rxe[i].mbuf = mbuf;
1522                 PMD_RX_LOG(DEBUG, "[%d]: %" PRIx64 "\n", i, dma_addr);
1523         }
1524
1525         /* Make sure all writes are flushed before telling the hardware */
1526         rte_wmb();
1527
1528         /* Not advertising the whole ring as the firmware gets confused if so */
1529         PMD_RX_LOG(DEBUG, "Increment FL write pointer in %u\n",
1530                    rxq->rx_count - 1);
1531
1532         nfp_qcp_ptr_add(rxq->qcp_fl, NFP_QCP_WRITE_PTR, rxq->rx_count - 1);
1533
1534         return 0;
1535 }
1536
1537 static int
1538 nfp_net_tx_queue_setup(struct rte_eth_dev *dev, uint16_t queue_idx,
1539                        uint16_t nb_desc, unsigned int socket_id,
1540                        const struct rte_eth_txconf *tx_conf)
1541 {
1542         const struct rte_memzone *tz;
1543         struct nfp_net_txq *txq;
1544         uint16_t tx_free_thresh;
1545         struct nfp_net_hw *hw;
1546
1547         hw = NFP_NET_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1548
1549         PMD_INIT_FUNC_TRACE();
1550
1551         /* Validating number of descriptors */
1552         if (((nb_desc * sizeof(struct nfp_net_tx_desc)) % 128) != 0 ||
1553             (nb_desc > NFP_NET_MAX_TX_DESC) ||
1554             (nb_desc < NFP_NET_MIN_TX_DESC)) {
1555                 RTE_LOG(ERR, PMD, "Wrong nb_desc value\n");
1556                 return -EINVAL;
1557         }
1558
1559         tx_free_thresh = (uint16_t)((tx_conf->tx_free_thresh) ?
1560                                     tx_conf->tx_free_thresh :
1561                                     DEFAULT_TX_FREE_THRESH);
1562
1563         if (tx_free_thresh > (nb_desc)) {
1564                 RTE_LOG(ERR, PMD,
1565                         "tx_free_thresh must be less than the number of TX "
1566                         "descriptors. (tx_free_thresh=%u port=%d "
1567                         "queue=%d)\n", (unsigned int)tx_free_thresh,
1568                         (int)dev->data->port_id, (int)queue_idx);
1569                 return -(EINVAL);
1570         }
1571
1572         /*
1573          * Free memory prior to re-allocation if needed. This is the case after
1574          * calling nfp_net_stop
1575          */
1576         if (dev->data->tx_queues[queue_idx]) {
1577                 PMD_TX_LOG(DEBUG, "Freeing memory prior to re-allocation %d\n",
1578                            queue_idx);
1579                 nfp_net_tx_queue_release(dev->data->tx_queues[queue_idx]);
1580                 dev->data->tx_queues[queue_idx] = NULL;
1581         }
1582
1583         /* Allocating tx queue data structure */
1584         txq = rte_zmalloc_socket("ethdev TX queue", sizeof(struct nfp_net_txq),
1585                                  RTE_CACHE_LINE_SIZE, socket_id);
1586         if (txq == NULL) {
1587                 RTE_LOG(ERR, PMD, "Error allocating tx dma\n");
1588                 return -ENOMEM;
1589         }
1590
1591         /*
1592          * Allocate TX ring hardware descriptors. A memzone large enough to
1593          * handle the maximum ring size is allocated in order to allow for
1594          * resizing in later calls to the queue setup function.
1595          */
1596         tz = rte_eth_dma_zone_reserve(dev, "tx_ring", queue_idx,
1597                                    sizeof(struct nfp_net_tx_desc) *
1598                                    NFP_NET_MAX_TX_DESC, NFP_MEMZONE_ALIGN,
1599                                    socket_id);
1600         if (tz == NULL) {
1601                 RTE_LOG(ERR, PMD, "Error allocating tx dma\n");
1602                 nfp_net_tx_queue_release(txq);
1603                 return -ENOMEM;
1604         }
1605
1606         txq->tx_count = nb_desc;
1607         txq->tx_free_thresh = tx_free_thresh;
1608         txq->tx_pthresh = tx_conf->tx_thresh.pthresh;
1609         txq->tx_hthresh = tx_conf->tx_thresh.hthresh;
1610         txq->tx_wthresh = tx_conf->tx_thresh.wthresh;
1611
1612         /* queue mapping based on firmware configuration */
1613         txq->qidx = queue_idx;
1614         txq->tx_qcidx = queue_idx * hw->stride_tx;
1615         txq->qcp_q = hw->tx_bar + NFP_QCP_QUEUE_OFF(txq->tx_qcidx);
1616
1617         txq->port_id = dev->data->port_id;
1618         txq->txq_flags = tx_conf->txq_flags;
1619
1620         /* Saving physical and virtual addresses for the TX ring */
1621         txq->dma = (uint64_t)tz->phys_addr;
1622         txq->txds = (struct nfp_net_tx_desc *)tz->addr;
1623
1624         /* mbuf pointers array for referencing mbufs linked to TX descriptors */
1625         txq->txbufs = rte_zmalloc_socket("txq->txbufs",
1626                                          sizeof(*txq->txbufs) * nb_desc,
1627                                          RTE_CACHE_LINE_SIZE, socket_id);
1628         if (txq->txbufs == NULL) {
1629                 nfp_net_tx_queue_release(txq);
1630                 return -ENOMEM;
1631         }
1632         PMD_TX_LOG(DEBUG, "txbufs=%p hw_ring=%p dma_addr=0x%" PRIx64 "\n",
1633                    txq->txbufs, txq->txds, (unsigned long int)txq->dma);
1634
1635         nfp_net_reset_tx_queue(txq);
1636
1637         dev->data->tx_queues[queue_idx] = txq;
1638         txq->hw = hw;
1639
1640         /*
1641          * Telling the HW about the physical address of the TX ring and number
1642          * of descriptors in log2 format
1643          */
1644         nn_cfg_writeq(hw, NFP_NET_CFG_TXR_ADDR(queue_idx), txq->dma);
1645         nn_cfg_writeb(hw, NFP_NET_CFG_TXR_SZ(queue_idx), rte_log2_u32(nb_desc));
1646
1647         return 0;
1648 }
1649
1650 /* nfp_net_tx_tso - Set TX descriptor for TSO */
1651 static inline void
1652 nfp_net_tx_tso(struct nfp_net_txq *txq, struct nfp_net_tx_desc *txd,
1653                struct rte_mbuf *mb)
1654 {
1655         uint64_t ol_flags;
1656         struct nfp_net_hw *hw = txq->hw;
1657
1658         if (!(hw->cap & NFP_NET_CFG_CTRL_LSO))
1659                 goto clean_txd;
1660
1661         ol_flags = mb->ol_flags;
1662
1663         if (!(ol_flags & PKT_TX_TCP_SEG))
1664                 goto clean_txd;
1665
1666         txd->l4_offset = mb->l2_len + mb->l3_len + mb->l4_len;
1667         txd->lso = rte_cpu_to_le_16(mb->tso_segsz);
1668         txd->flags = PCIE_DESC_TX_LSO;
1669         return;
1670
1671 clean_txd:
1672         txd->flags = 0;
1673         txd->l4_offset = 0;
1674         txd->lso = 0;
1675 }
1676
1677 /* nfp_net_tx_cksum - Set TX CSUM offload flags in TX descriptor */
1678 static inline void
1679 nfp_net_tx_cksum(struct nfp_net_txq *txq, struct nfp_net_tx_desc *txd,
1680                  struct rte_mbuf *mb)
1681 {
1682         uint64_t ol_flags;
1683         struct nfp_net_hw *hw = txq->hw;
1684
1685         if (!(hw->cap & NFP_NET_CFG_CTRL_TXCSUM))
1686                 return;
1687
1688         ol_flags = mb->ol_flags;
1689
1690         /* IPv6 does not need checksum */
1691         if (ol_flags & PKT_TX_IP_CKSUM)
1692                 txd->flags |= PCIE_DESC_TX_IP4_CSUM;
1693
1694         switch (ol_flags & PKT_TX_L4_MASK) {
1695         case PKT_TX_UDP_CKSUM:
1696                 txd->flags |= PCIE_DESC_TX_UDP_CSUM;
1697                 break;
1698         case PKT_TX_TCP_CKSUM:
1699                 txd->flags |= PCIE_DESC_TX_TCP_CSUM;
1700                 break;
1701         }
1702
1703         if (ol_flags & (PKT_TX_IP_CKSUM | PKT_TX_L4_MASK))
1704                 txd->flags |= PCIE_DESC_TX_CSUM;
1705 }
1706
1707 /* nfp_net_rx_cksum - set mbuf checksum flags based on RX descriptor flags */
1708 static inline void
1709 nfp_net_rx_cksum(struct nfp_net_rxq *rxq, struct nfp_net_rx_desc *rxd,
1710                  struct rte_mbuf *mb)
1711 {
1712         struct nfp_net_hw *hw = rxq->hw;
1713
1714         if (!(hw->ctrl & NFP_NET_CFG_CTRL_RXCSUM))
1715                 return;
1716
1717         /* If IPv4 and IP checksum error, fail */
1718         if ((rxd->rxd.flags & PCIE_DESC_RX_IP4_CSUM) &&
1719             !(rxd->rxd.flags & PCIE_DESC_RX_IP4_CSUM_OK))
1720                 mb->ol_flags |= PKT_RX_IP_CKSUM_BAD;
1721
1722         /* If neither UDP nor TCP return */
1723         if (!(rxd->rxd.flags & PCIE_DESC_RX_TCP_CSUM) &&
1724             !(rxd->rxd.flags & PCIE_DESC_RX_UDP_CSUM))
1725                 return;
1726
1727         if ((rxd->rxd.flags & PCIE_DESC_RX_TCP_CSUM) &&
1728             !(rxd->rxd.flags & PCIE_DESC_RX_TCP_CSUM_OK))
1729                 mb->ol_flags |= PKT_RX_L4_CKSUM_BAD;
1730
1731         if ((rxd->rxd.flags & PCIE_DESC_RX_UDP_CSUM) &&
1732             !(rxd->rxd.flags & PCIE_DESC_RX_UDP_CSUM_OK))
1733                 mb->ol_flags |= PKT_RX_L4_CKSUM_BAD;
1734 }
1735
1736 #define NFP_HASH_OFFSET      ((uint8_t *)mbuf->buf_addr + mbuf->data_off - 4)
1737 #define NFP_HASH_TYPE_OFFSET ((uint8_t *)mbuf->buf_addr + mbuf->data_off - 8)
1738
1739 #define NFP_DESC_META_LEN(d) (d->rxd.meta_len_dd & PCIE_DESC_RX_META_LEN_MASK)
1740
1741 /*
1742  * nfp_net_set_hash - Set mbuf hash data
1743  *
1744  * The RSS hash and hash-type are pre-pended to the packet data.
1745  * Extract and decode it and set the mbuf fields.
1746  */
1747 static inline void
1748 nfp_net_set_hash(struct nfp_net_rxq *rxq, struct nfp_net_rx_desc *rxd,
1749                  struct rte_mbuf *mbuf)
1750 {
1751         struct nfp_net_hw *hw = rxq->hw;
1752         uint8_t *meta_offset;
1753         uint32_t meta_info;
1754         uint32_t hash = 0;
1755         uint32_t hash_type = 0;
1756
1757         if (!(hw->ctrl & NFP_NET_CFG_CTRL_RSS))
1758                 return;
1759
1760         if (NFD_CFG_MAJOR_VERSION_of(hw->ver) <= 3) {
1761                 if (!(rxd->rxd.flags & PCIE_DESC_RX_RSS))
1762                         return;
1763
1764                 hash = rte_be_to_cpu_32(*(uint32_t *)NFP_HASH_OFFSET);
1765                 hash_type = rte_be_to_cpu_32(*(uint32_t *)NFP_HASH_TYPE_OFFSET);
1766
1767         } else if (NFP_DESC_META_LEN(rxd)) {
1768                 /*
1769                  * new metadata api:
1770                  * <----  32 bit  ----->
1771                  * m    field type word
1772                  * e     data field #2
1773                  * t     data field #1
1774                  * a     data field #0
1775                  * ====================
1776                  *    packet data
1777                  *
1778                  * Field type word contains up to 8 4bit field types
1779                  * A 4bit field type refers to a data field word
1780                  * A data field word can have several 4bit field types
1781                  */
1782                 meta_offset = rte_pktmbuf_mtod(mbuf, uint8_t *);
1783                 meta_offset -= NFP_DESC_META_LEN(rxd);
1784                 meta_info = rte_be_to_cpu_32(*(uint32_t *)meta_offset);
1785                 meta_offset += 4;
1786                 /* NFP PMD just supports metadata for hashing */
1787                 switch (meta_info & NFP_NET_META_FIELD_MASK) {
1788                 case NFP_NET_META_HASH:
1789                         /* next field type is about the hash type */
1790                         meta_info >>= NFP_NET_META_FIELD_SIZE;
1791                         /* hash value is in the data field */
1792                         hash = rte_be_to_cpu_32(*(uint32_t *)meta_offset);
1793                         hash_type = meta_info & NFP_NET_META_FIELD_MASK;
1794                         break;
1795                 default:
1796                         /* Unsupported metadata can be a performance issue */
1797                         return;
1798                 }
1799         } else {
1800                 return;
1801         }
1802
1803         mbuf->hash.rss = hash;
1804         mbuf->ol_flags |= PKT_RX_RSS_HASH;
1805
1806         switch (hash_type) {
1807         case NFP_NET_RSS_IPV4:
1808                 mbuf->packet_type |= RTE_PTYPE_INNER_L3_IPV4;
1809                 break;
1810         case NFP_NET_RSS_IPV6:
1811                 mbuf->packet_type |= RTE_PTYPE_INNER_L3_IPV6;
1812                 break;
1813         case NFP_NET_RSS_IPV6_EX:
1814                 mbuf->packet_type |= RTE_PTYPE_INNER_L3_IPV6_EXT;
1815                 break;
1816         default:
1817                 mbuf->packet_type |= RTE_PTYPE_INNER_L4_MASK;
1818         }
1819 }
1820
1821 static inline void
1822 nfp_net_mbuf_alloc_failed(struct nfp_net_rxq *rxq)
1823 {
1824         rte_eth_devices[rxq->port_id].data->rx_mbuf_alloc_failed++;
1825 }
1826
1827 #define NFP_DESC_META_LEN(d) (d->rxd.meta_len_dd & PCIE_DESC_RX_META_LEN_MASK)
1828
1829 /*
1830  * RX path design:
1831  *
1832  * There are some decissions to take:
1833  * 1) How to check DD RX descriptors bit
1834  * 2) How and when to allocate new mbufs
1835  *
1836  * Current implementation checks just one single DD bit each loop. As each
1837  * descriptor is 8 bytes, it is likely a good idea to check descriptors in
1838  * a single cache line instead. Tests with this change have not shown any
1839  * performance improvement but it requires further investigation. For example,
1840  * depending on which descriptor is next, the number of descriptors could be
1841  * less than 8 for just checking those in the same cache line. This implies
1842  * extra work which could be counterproductive by itself. Indeed, last firmware
1843  * changes are just doing this: writing several descriptors with the DD bit
1844  * for saving PCIe bandwidth and DMA operations from the NFP.
1845  *
1846  * Mbuf allocation is done when a new packet is received. Then the descriptor
1847  * is automatically linked with the new mbuf and the old one is given to the
1848  * user. The main drawback with this design is mbuf allocation is heavier than
1849  * using bulk allocations allowed by DPDK with rte_mempool_get_bulk. From the
1850  * cache point of view it does not seem allocating the mbuf early on as we are
1851  * doing now have any benefit at all. Again, tests with this change have not
1852  * shown any improvement. Also, rte_mempool_get_bulk returns all or nothing
1853  * so looking at the implications of this type of allocation should be studied
1854  * deeply
1855  */
1856
1857 static uint16_t
1858 nfp_net_recv_pkts(void *rx_queue, struct rte_mbuf **rx_pkts, uint16_t nb_pkts)
1859 {
1860         struct nfp_net_rxq *rxq;
1861         struct nfp_net_rx_desc *rxds;
1862         struct nfp_net_rx_buff *rxb;
1863         struct nfp_net_hw *hw;
1864         struct rte_mbuf *mb;
1865         struct rte_mbuf *new_mb;
1866         uint16_t nb_hold;
1867         uint64_t dma_addr;
1868         int avail;
1869
1870         rxq = rx_queue;
1871         if (unlikely(rxq == NULL)) {
1872                 /*
1873                  * DPDK just checks the queue is lower than max queues
1874                  * enabled. But the queue needs to be configured
1875                  */
1876                 RTE_LOG_DP(ERR, PMD, "RX Bad queue\n");
1877                 return -EINVAL;
1878         }
1879
1880         hw = rxq->hw;
1881         avail = 0;
1882         nb_hold = 0;
1883
1884         while (avail < nb_pkts) {
1885                 rxb = &rxq->rxbufs[rxq->rd_p];
1886                 if (unlikely(rxb == NULL)) {
1887                         RTE_LOG_DP(ERR, PMD, "rxb does not exist!\n");
1888                         break;
1889                 }
1890
1891                 /*
1892                  * Memory barrier to ensure that we won't do other
1893                  * reads before the DD bit.
1894                  */
1895                 rte_rmb();
1896
1897                 rxds = &rxq->rxds[rxq->rd_p];
1898                 if ((rxds->rxd.meta_len_dd & PCIE_DESC_RX_DD) == 0)
1899                         break;
1900
1901                 /*
1902                  * We got a packet. Let's alloc a new mbuff for refilling the
1903                  * free descriptor ring as soon as possible
1904                  */
1905                 new_mb = rte_pktmbuf_alloc(rxq->mem_pool);
1906                 if (unlikely(new_mb == NULL)) {
1907                         RTE_LOG_DP(DEBUG, PMD, "RX mbuf alloc failed port_id=%u "
1908                                 "queue_id=%u\n", (unsigned)rxq->port_id,
1909                                 (unsigned)rxq->qidx);
1910                         nfp_net_mbuf_alloc_failed(rxq);
1911                         break;
1912                 }
1913
1914                 nb_hold++;
1915
1916                 /*
1917                  * Grab the mbuff and refill the descriptor with the
1918                  * previously allocated mbuff
1919                  */
1920                 mb = rxb->mbuf;
1921                 rxb->mbuf = new_mb;
1922
1923                 PMD_RX_LOG(DEBUG, "Packet len: %u, mbuf_size: %u\n",
1924                            rxds->rxd.data_len, rxq->mbuf_size);
1925
1926                 /* Size of this segment */
1927                 mb->data_len = rxds->rxd.data_len - NFP_DESC_META_LEN(rxds);
1928                 /* Size of the whole packet. We just support 1 segment */
1929                 mb->pkt_len = rxds->rxd.data_len - NFP_DESC_META_LEN(rxds);
1930
1931                 if (unlikely((mb->data_len + hw->rx_offset) >
1932                              rxq->mbuf_size)) {
1933                         /*
1934                          * This should not happen and the user has the
1935                          * responsibility of avoiding it. But we have
1936                          * to give some info about the error
1937                          */
1938                         RTE_LOG_DP(ERR, PMD,
1939                                 "mbuf overflow likely due to the RX offset.\n"
1940                                 "\t\tYour mbuf size should have extra space for"
1941                                 " RX offset=%u bytes.\n"
1942                                 "\t\tCurrently you just have %u bytes available"
1943                                 " but the received packet is %u bytes long",
1944                                 hw->rx_offset,
1945                                 rxq->mbuf_size - hw->rx_offset,
1946                                 mb->data_len);
1947                         return -EINVAL;
1948                 }
1949
1950                 /* Filling the received mbuff with packet info */
1951                 if (hw->rx_offset)
1952                         mb->data_off = RTE_PKTMBUF_HEADROOM + hw->rx_offset;
1953                 else
1954                         mb->data_off = RTE_PKTMBUF_HEADROOM +
1955                                        NFP_DESC_META_LEN(rxds);
1956
1957                 /* No scatter mode supported */
1958                 mb->nb_segs = 1;
1959                 mb->next = NULL;
1960
1961                 /* Checking the RSS flag */
1962                 nfp_net_set_hash(rxq, rxds, mb);
1963
1964                 /* Checking the checksum flag */
1965                 nfp_net_rx_cksum(rxq, rxds, mb);
1966
1967                 if ((rxds->rxd.flags & PCIE_DESC_RX_VLAN) &&
1968                     (hw->ctrl & NFP_NET_CFG_CTRL_RXVLAN)) {
1969                         mb->vlan_tci = rte_cpu_to_le_32(rxds->rxd.vlan);
1970                         mb->ol_flags |= PKT_RX_VLAN_PKT | PKT_RX_VLAN_STRIPPED;
1971                 }
1972
1973                 /* Adding the mbuff to the mbuff array passed by the app */
1974                 rx_pkts[avail++] = mb;
1975
1976                 /* Now resetting and updating the descriptor */
1977                 rxds->vals[0] = 0;
1978                 rxds->vals[1] = 0;
1979                 dma_addr = rte_cpu_to_le_64(RTE_MBUF_DMA_ADDR_DEFAULT(new_mb));
1980                 rxds->fld.dd = 0;
1981                 rxds->fld.dma_addr_hi = (dma_addr >> 32) & 0xff;
1982                 rxds->fld.dma_addr_lo = dma_addr & 0xffffffff;
1983
1984                 rxq->rd_p++;
1985                 if (unlikely(rxq->rd_p == rxq->rx_count)) /* wrapping?*/
1986                         rxq->rd_p = 0;
1987         }
1988
1989         if (nb_hold == 0)
1990                 return nb_hold;
1991
1992         PMD_RX_LOG(DEBUG, "RX  port_id=%u queue_id=%u, %d packets received\n",
1993                    (unsigned)rxq->port_id, (unsigned)rxq->qidx, nb_hold);
1994
1995         nb_hold += rxq->nb_rx_hold;
1996
1997         /*
1998          * FL descriptors needs to be written before incrementing the
1999          * FL queue WR pointer
2000          */
2001         rte_wmb();
2002         if (nb_hold > rxq->rx_free_thresh) {
2003                 PMD_RX_LOG(DEBUG, "port=%u queue=%u nb_hold=%u avail=%u\n",
2004                            (unsigned)rxq->port_id, (unsigned)rxq->qidx,
2005                            (unsigned)nb_hold, (unsigned)avail);
2006                 nfp_qcp_ptr_add(rxq->qcp_fl, NFP_QCP_WRITE_PTR, nb_hold);
2007                 nb_hold = 0;
2008         }
2009         rxq->nb_rx_hold = nb_hold;
2010
2011         return avail;
2012 }
2013
2014 /*
2015  * nfp_net_tx_free_bufs - Check for descriptors with a complete
2016  * status
2017  * @txq: TX queue to work with
2018  * Returns number of descriptors freed
2019  */
2020 int
2021 nfp_net_tx_free_bufs(struct nfp_net_txq *txq)
2022 {
2023         uint32_t qcp_rd_p;
2024         int todo;
2025
2026         PMD_TX_LOG(DEBUG, "queue %u. Check for descriptor with a complete"
2027                    " status\n", txq->qidx);
2028
2029         /* Work out how many packets have been sent */
2030         qcp_rd_p = nfp_qcp_read(txq->qcp_q, NFP_QCP_READ_PTR);
2031
2032         if (qcp_rd_p == txq->rd_p) {
2033                 PMD_TX_LOG(DEBUG, "queue %u: It seems harrier is not sending "
2034                            "packets (%u, %u)\n", txq->qidx,
2035                            qcp_rd_p, txq->rd_p);
2036                 return 0;
2037         }
2038
2039         if (qcp_rd_p > txq->rd_p)
2040                 todo = qcp_rd_p - txq->rd_p;
2041         else
2042                 todo = qcp_rd_p + txq->tx_count - txq->rd_p;
2043
2044         PMD_TX_LOG(DEBUG, "qcp_rd_p %u, txq->rd_p: %u, qcp->rd_p: %u\n",
2045                    qcp_rd_p, txq->rd_p, txq->rd_p);
2046
2047         if (todo == 0)
2048                 return todo;
2049
2050         txq->rd_p += todo;
2051         if (unlikely(txq->rd_p >= txq->tx_count))
2052                 txq->rd_p -= txq->tx_count;
2053
2054         return todo;
2055 }
2056
2057 /* Leaving always free descriptors for avoiding wrapping confusion */
2058 static inline
2059 uint32_t nfp_free_tx_desc(struct nfp_net_txq *txq)
2060 {
2061         if (txq->wr_p >= txq->rd_p)
2062                 return txq->tx_count - (txq->wr_p - txq->rd_p) - 8;
2063         else
2064                 return txq->rd_p - txq->wr_p - 8;
2065 }
2066
2067 /*
2068  * nfp_net_txq_full - Check if the TX queue free descriptors
2069  * is below tx_free_threshold
2070  *
2071  * @txq: TX queue to check
2072  *
2073  * This function uses the host copy* of read/write pointers
2074  */
2075 static inline
2076 uint32_t nfp_net_txq_full(struct nfp_net_txq *txq)
2077 {
2078         return (nfp_free_tx_desc(txq) < txq->tx_free_thresh);
2079 }
2080
2081 static uint16_t
2082 nfp_net_xmit_pkts(void *tx_queue, struct rte_mbuf **tx_pkts, uint16_t nb_pkts)
2083 {
2084         struct nfp_net_txq *txq;
2085         struct nfp_net_hw *hw;
2086         struct nfp_net_tx_desc *txds, txd;
2087         struct rte_mbuf *pkt;
2088         uint64_t dma_addr;
2089         int pkt_size, dma_size;
2090         uint16_t free_descs, issued_descs;
2091         struct rte_mbuf **lmbuf;
2092         int i;
2093
2094         txq = tx_queue;
2095         hw = txq->hw;
2096         txds = &txq->txds[txq->wr_p];
2097
2098         PMD_TX_LOG(DEBUG, "working for queue %u at pos %d and %u packets\n",
2099                    txq->qidx, txq->wr_p, nb_pkts);
2100
2101         if ((nfp_free_tx_desc(txq) < nb_pkts) || (nfp_net_txq_full(txq)))
2102                 nfp_net_tx_free_bufs(txq);
2103
2104         free_descs = (uint16_t)nfp_free_tx_desc(txq);
2105         if (unlikely(free_descs == 0))
2106                 return 0;
2107
2108         pkt = *tx_pkts;
2109
2110         i = 0;
2111         issued_descs = 0;
2112         PMD_TX_LOG(DEBUG, "queue: %u. Sending %u packets\n",
2113                    txq->qidx, nb_pkts);
2114         /* Sending packets */
2115         while ((i < nb_pkts) && free_descs) {
2116                 /* Grabbing the mbuf linked to the current descriptor */
2117                 lmbuf = &txq->txbufs[txq->wr_p].mbuf;
2118                 /* Warming the cache for releasing the mbuf later on */
2119                 RTE_MBUF_PREFETCH_TO_FREE(*lmbuf);
2120
2121                 pkt = *(tx_pkts + i);
2122
2123                 if (unlikely((pkt->nb_segs > 1) &&
2124                              !(hw->cap & NFP_NET_CFG_CTRL_GATHER))) {
2125                         PMD_INIT_LOG(INFO, "NFP_NET_CFG_CTRL_GATHER not set");
2126                         rte_panic("Multisegment packet unsupported\n");
2127                 }
2128
2129                 /* Checking if we have enough descriptors */
2130                 if (unlikely(pkt->nb_segs > free_descs))
2131                         goto xmit_end;
2132
2133                 /*
2134                  * Checksum and VLAN flags just in the first descriptor for a
2135                  * multisegment packet, but TSO info needs to be in all of them.
2136                  */
2137                 txd.data_len = pkt->pkt_len;
2138                 nfp_net_tx_tso(txq, &txd, pkt);
2139                 nfp_net_tx_cksum(txq, &txd, pkt);
2140
2141                 if ((pkt->ol_flags & PKT_TX_VLAN_PKT) &&
2142                     (hw->cap & NFP_NET_CFG_CTRL_TXVLAN)) {
2143                         txd.flags |= PCIE_DESC_TX_VLAN;
2144                         txd.vlan = pkt->vlan_tci;
2145                 }
2146
2147                 /*
2148                  * mbuf data_len is the data in one segment and pkt_len data
2149                  * in the whole packet. When the packet is just one segment,
2150                  * then data_len = pkt_len
2151                  */
2152                 pkt_size = pkt->pkt_len;
2153
2154                 while (pkt) {
2155                         /* Copying TSO, VLAN and cksum info */
2156                         *txds = txd;
2157
2158                         /* Releasing mbuf used by this descriptor previously*/
2159                         if (*lmbuf)
2160                                 rte_pktmbuf_free_seg(*lmbuf);
2161
2162                         /*
2163                          * Linking mbuf with descriptor for being released
2164                          * next time descriptor is used
2165                          */
2166                         *lmbuf = pkt;
2167
2168                         dma_size = pkt->data_len;
2169                         dma_addr = rte_mbuf_data_dma_addr(pkt);
2170                         PMD_TX_LOG(DEBUG, "Working with mbuf at dma address:"
2171                                    "%" PRIx64 "\n", dma_addr);
2172
2173                         /* Filling descriptors fields */
2174                         txds->dma_len = dma_size;
2175                         txds->data_len = txd.data_len;
2176                         txds->dma_addr_hi = (dma_addr >> 32) & 0xff;
2177                         txds->dma_addr_lo = (dma_addr & 0xffffffff);
2178                         ASSERT(free_descs > 0);
2179                         free_descs--;
2180
2181                         txq->wr_p++;
2182                         if (unlikely(txq->wr_p == txq->tx_count)) /* wrapping?*/
2183                                 txq->wr_p = 0;
2184
2185                         pkt_size -= dma_size;
2186                         if (!pkt_size)
2187                                 /* End of packet */
2188                                 txds->offset_eop |= PCIE_DESC_TX_EOP;
2189                         else
2190                                 txds->offset_eop &= PCIE_DESC_TX_OFFSET_MASK;
2191
2192                         pkt = pkt->next;
2193                         /* Referencing next free TX descriptor */
2194                         txds = &txq->txds[txq->wr_p];
2195                         lmbuf = &txq->txbufs[txq->wr_p].mbuf;
2196                         issued_descs++;
2197                 }
2198                 i++;
2199         }
2200
2201 xmit_end:
2202         /* Increment write pointers. Force memory write before we let HW know */
2203         rte_wmb();
2204         nfp_qcp_ptr_add(txq->qcp_q, NFP_QCP_WRITE_PTR, issued_descs);
2205
2206         return i;
2207 }
2208
2209 static void
2210 nfp_net_vlan_offload_set(struct rte_eth_dev *dev, int mask)
2211 {
2212         uint32_t new_ctrl, update;
2213         struct nfp_net_hw *hw;
2214
2215         hw = NFP_NET_DEV_PRIVATE_TO_HW(dev->data->dev_private);
2216         new_ctrl = 0;
2217
2218         if ((mask & ETH_VLAN_FILTER_OFFLOAD) ||
2219             (mask & ETH_VLAN_EXTEND_OFFLOAD))
2220                 RTE_LOG(INFO, PMD, "No support for ETH_VLAN_FILTER_OFFLOAD or"
2221                         " ETH_VLAN_EXTEND_OFFLOAD");
2222
2223         /* Enable vlan strip if it is not configured yet */
2224         if ((mask & ETH_VLAN_STRIP_OFFLOAD) &&
2225             !(hw->ctrl & NFP_NET_CFG_CTRL_RXVLAN))
2226                 new_ctrl = hw->ctrl | NFP_NET_CFG_CTRL_RXVLAN;
2227
2228         /* Disable vlan strip just if it is configured */
2229         if (!(mask & ETH_VLAN_STRIP_OFFLOAD) &&
2230             (hw->ctrl & NFP_NET_CFG_CTRL_RXVLAN))
2231                 new_ctrl = hw->ctrl & ~NFP_NET_CFG_CTRL_RXVLAN;
2232
2233         if (new_ctrl == 0)
2234                 return;
2235
2236         update = NFP_NET_CFG_UPDATE_GEN;
2237
2238         if (nfp_net_reconfig(hw, new_ctrl, update) < 0)
2239                 return;
2240
2241         hw->ctrl = new_ctrl;
2242 }
2243
2244 /* Update Redirection Table(RETA) of Receive Side Scaling of Ethernet device */
2245 static int
2246 nfp_net_reta_update(struct rte_eth_dev *dev,
2247                     struct rte_eth_rss_reta_entry64 *reta_conf,
2248                     uint16_t reta_size)
2249 {
2250         uint32_t reta, mask;
2251         int i, j;
2252         int idx, shift;
2253         uint32_t update;
2254         struct nfp_net_hw *hw =
2255                 NFP_NET_DEV_PRIVATE_TO_HW(dev->data->dev_private);
2256
2257         if (!(hw->ctrl & NFP_NET_CFG_CTRL_RSS))
2258                 return -EINVAL;
2259
2260         if (reta_size != NFP_NET_CFG_RSS_ITBL_SZ) {
2261                 RTE_LOG(ERR, PMD, "The size of hash lookup table configured "
2262                         "(%d) doesn't match the number hardware can supported "
2263                         "(%d)\n", reta_size, NFP_NET_CFG_RSS_ITBL_SZ);
2264                 return -EINVAL;
2265         }
2266
2267         /*
2268          * Update Redirection Table. There are 128 8bit-entries which can be
2269          * manage as 32 32bit-entries
2270          */
2271         for (i = 0; i < reta_size; i += 4) {
2272                 /* Handling 4 RSS entries per loop */
2273                 idx = i / RTE_RETA_GROUP_SIZE;
2274                 shift = i % RTE_RETA_GROUP_SIZE;
2275                 mask = (uint8_t)((reta_conf[idx].mask >> shift) & 0xF);
2276
2277                 if (!mask)
2278                         continue;
2279
2280                 reta = 0;
2281                 /* If all 4 entries were set, don't need read RETA register */
2282                 if (mask != 0xF)
2283                         reta = nn_cfg_readl(hw, NFP_NET_CFG_RSS_ITBL + i);
2284
2285                 for (j = 0; j < 4; j++) {
2286                         if (!(mask & (0x1 << j)))
2287                                 continue;
2288                         if (mask != 0xF)
2289                                 /* Clearing the entry bits */
2290                                 reta &= ~(0xFF << (8 * j));
2291                         reta |= reta_conf[idx].reta[shift + j] << (8 * j);
2292                 }
2293                 nn_cfg_writel(hw, NFP_NET_CFG_RSS_ITBL + (idx * 64) + shift,
2294                               reta);
2295         }
2296
2297         update = NFP_NET_CFG_UPDATE_RSS;
2298
2299         if (nfp_net_reconfig(hw, hw->ctrl, update) < 0)
2300                 return -EIO;
2301
2302         return 0;
2303 }
2304
2305  /* Query Redirection Table(RETA) of Receive Side Scaling of Ethernet device. */
2306 static int
2307 nfp_net_reta_query(struct rte_eth_dev *dev,
2308                    struct rte_eth_rss_reta_entry64 *reta_conf,
2309                    uint16_t reta_size)
2310 {
2311         uint8_t i, j, mask;
2312         int idx, shift;
2313         uint32_t reta;
2314         struct nfp_net_hw *hw;
2315
2316         hw = NFP_NET_DEV_PRIVATE_TO_HW(dev->data->dev_private);
2317
2318         if (!(hw->ctrl & NFP_NET_CFG_CTRL_RSS))
2319                 return -EINVAL;
2320
2321         if (reta_size != NFP_NET_CFG_RSS_ITBL_SZ) {
2322                 RTE_LOG(ERR, PMD, "The size of hash lookup table configured "
2323                         "(%d) doesn't match the number hardware can supported "
2324                         "(%d)\n", reta_size, NFP_NET_CFG_RSS_ITBL_SZ);
2325                 return -EINVAL;
2326         }
2327
2328         /*
2329          * Reading Redirection Table. There are 128 8bit-entries which can be
2330          * manage as 32 32bit-entries
2331          */
2332         for (i = 0; i < reta_size; i += 4) {
2333                 /* Handling 4 RSS entries per loop */
2334                 idx = i / RTE_RETA_GROUP_SIZE;
2335                 shift = i % RTE_RETA_GROUP_SIZE;
2336                 mask = (uint8_t)((reta_conf[idx].mask >> shift) & 0xF);
2337
2338                 if (!mask)
2339                         continue;
2340
2341                 reta = nn_cfg_readl(hw, NFP_NET_CFG_RSS_ITBL + (idx * 64) +
2342                                     shift);
2343                 for (j = 0; j < 4; j++) {
2344                         if (!(mask & (0x1 << j)))
2345                                 continue;
2346                         reta_conf->reta[shift + j] =
2347                                 (uint8_t)((reta >> (8 * j)) & 0xF);
2348                 }
2349         }
2350         return 0;
2351 }
2352
2353 static int
2354 nfp_net_rss_hash_update(struct rte_eth_dev *dev,
2355                         struct rte_eth_rss_conf *rss_conf)
2356 {
2357         uint32_t update;
2358         uint32_t cfg_rss_ctrl = 0;
2359         uint8_t key;
2360         uint64_t rss_hf;
2361         int i;
2362         struct nfp_net_hw *hw;
2363
2364         hw = NFP_NET_DEV_PRIVATE_TO_HW(dev->data->dev_private);
2365
2366         rss_hf = rss_conf->rss_hf;
2367
2368         /* Checking if RSS is enabled */
2369         if (!(hw->ctrl & NFP_NET_CFG_CTRL_RSS)) {
2370                 if (rss_hf != 0) { /* Enable RSS? */
2371                         RTE_LOG(ERR, PMD, "RSS unsupported\n");
2372                         return -EINVAL;
2373                 }
2374                 return 0; /* Nothing to do */
2375         }
2376
2377         if (rss_conf->rss_key_len > NFP_NET_CFG_RSS_KEY_SZ) {
2378                 RTE_LOG(ERR, PMD, "hash key too long\n");
2379                 return -EINVAL;
2380         }
2381
2382         if (rss_hf & ETH_RSS_IPV4)
2383                 cfg_rss_ctrl |= NFP_NET_CFG_RSS_IPV4 |
2384                                 NFP_NET_CFG_RSS_IPV4_TCP |
2385                                 NFP_NET_CFG_RSS_IPV4_UDP;
2386
2387         if (rss_hf & ETH_RSS_IPV6)
2388                 cfg_rss_ctrl |= NFP_NET_CFG_RSS_IPV6 |
2389                                 NFP_NET_CFG_RSS_IPV6_TCP |
2390                                 NFP_NET_CFG_RSS_IPV6_UDP;
2391
2392         cfg_rss_ctrl |= NFP_NET_CFG_RSS_MASK;
2393         cfg_rss_ctrl |= NFP_NET_CFG_RSS_TOEPLITZ;
2394
2395         /* configuring where to apply the RSS hash */
2396         nn_cfg_writel(hw, NFP_NET_CFG_RSS_CTRL, cfg_rss_ctrl);
2397
2398         /* Writing the key byte a byte */
2399         for (i = 0; i < rss_conf->rss_key_len; i++) {
2400                 memcpy(&key, &rss_conf->rss_key[i], 1);
2401                 nn_cfg_writeb(hw, NFP_NET_CFG_RSS_KEY + i, key);
2402         }
2403
2404         /* Writing the key size */
2405         nn_cfg_writeb(hw, NFP_NET_CFG_RSS_KEY_SZ, rss_conf->rss_key_len);
2406
2407         update = NFP_NET_CFG_UPDATE_RSS;
2408
2409         if (nfp_net_reconfig(hw, hw->ctrl, update) < 0)
2410                 return -EIO;
2411
2412         return 0;
2413 }
2414
2415 static int
2416 nfp_net_rss_hash_conf_get(struct rte_eth_dev *dev,
2417                           struct rte_eth_rss_conf *rss_conf)
2418 {
2419         uint64_t rss_hf;
2420         uint32_t cfg_rss_ctrl;
2421         uint8_t key;
2422         int i;
2423         struct nfp_net_hw *hw;
2424
2425         hw = NFP_NET_DEV_PRIVATE_TO_HW(dev->data->dev_private);
2426
2427         if (!(hw->ctrl & NFP_NET_CFG_CTRL_RSS))
2428                 return -EINVAL;
2429
2430         rss_hf = rss_conf->rss_hf;
2431         cfg_rss_ctrl = nn_cfg_readl(hw, NFP_NET_CFG_RSS_CTRL);
2432
2433         if (cfg_rss_ctrl & NFP_NET_CFG_RSS_IPV4)
2434                 rss_hf |= ETH_RSS_NONFRAG_IPV4_TCP | ETH_RSS_NONFRAG_IPV4_UDP;
2435
2436         if (cfg_rss_ctrl & NFP_NET_CFG_RSS_IPV4_TCP)
2437                 rss_hf |= ETH_RSS_NONFRAG_IPV4_TCP;
2438
2439         if (cfg_rss_ctrl & NFP_NET_CFG_RSS_IPV6_TCP)
2440                 rss_hf |= ETH_RSS_NONFRAG_IPV6_TCP;
2441
2442         if (cfg_rss_ctrl & NFP_NET_CFG_RSS_IPV4_UDP)
2443                 rss_hf |= ETH_RSS_NONFRAG_IPV4_UDP;
2444
2445         if (cfg_rss_ctrl & NFP_NET_CFG_RSS_IPV6_UDP)
2446                 rss_hf |= ETH_RSS_NONFRAG_IPV6_UDP;
2447
2448         if (cfg_rss_ctrl & NFP_NET_CFG_RSS_IPV6)
2449                 rss_hf |= ETH_RSS_NONFRAG_IPV4_UDP | ETH_RSS_NONFRAG_IPV6_UDP;
2450
2451         /* Reading the key size */
2452         rss_conf->rss_key_len = nn_cfg_readl(hw, NFP_NET_CFG_RSS_KEY_SZ);
2453
2454         /* Reading the key byte a byte */
2455         for (i = 0; i < rss_conf->rss_key_len; i++) {
2456                 key = nn_cfg_readb(hw, NFP_NET_CFG_RSS_KEY + i);
2457                 memcpy(&rss_conf->rss_key[i], &key, 1);
2458         }
2459
2460         return 0;
2461 }
2462
2463 /* Initialise and register driver with DPDK Application */
2464 static const struct eth_dev_ops nfp_net_eth_dev_ops = {
2465         .dev_configure          = nfp_net_configure,
2466         .dev_start              = nfp_net_start,
2467         .dev_stop               = nfp_net_stop,
2468         .dev_close              = nfp_net_close,
2469         .promiscuous_enable     = nfp_net_promisc_enable,
2470         .promiscuous_disable    = nfp_net_promisc_disable,
2471         .link_update            = nfp_net_link_update,
2472         .stats_get              = nfp_net_stats_get,
2473         .stats_reset            = nfp_net_stats_reset,
2474         .dev_infos_get          = nfp_net_infos_get,
2475         .dev_supported_ptypes_get = nfp_net_supported_ptypes_get,
2476         .mtu_set                = nfp_net_dev_mtu_set,
2477         .vlan_offload_set       = nfp_net_vlan_offload_set,
2478         .reta_update            = nfp_net_reta_update,
2479         .reta_query             = nfp_net_reta_query,
2480         .rss_hash_update        = nfp_net_rss_hash_update,
2481         .rss_hash_conf_get      = nfp_net_rss_hash_conf_get,
2482         .rx_queue_setup         = nfp_net_rx_queue_setup,
2483         .rx_queue_release       = nfp_net_rx_queue_release,
2484         .rx_queue_count         = nfp_net_rx_queue_count,
2485         .tx_queue_setup         = nfp_net_tx_queue_setup,
2486         .tx_queue_release       = nfp_net_tx_queue_release,
2487         .rx_queue_intr_enable   = nfp_rx_queue_intr_enable,
2488         .rx_queue_intr_disable  = nfp_rx_queue_intr_disable,
2489 };
2490
2491 static int
2492 nfp_net_init(struct rte_eth_dev *eth_dev)
2493 {
2494         struct rte_pci_device *pci_dev;
2495         struct nfp_net_hw *hw;
2496
2497         uint32_t tx_bar_off, rx_bar_off;
2498         uint32_t start_q;
2499         int stride = 4;
2500
2501         PMD_INIT_FUNC_TRACE();
2502
2503         hw = NFP_NET_DEV_PRIVATE_TO_HW(eth_dev->data->dev_private);
2504
2505         eth_dev->dev_ops = &nfp_net_eth_dev_ops;
2506         eth_dev->rx_pkt_burst = &nfp_net_recv_pkts;
2507         eth_dev->tx_pkt_burst = &nfp_net_xmit_pkts;
2508
2509         /* For secondary processes, the primary has done all the work */
2510         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
2511                 return 0;
2512
2513         pci_dev = RTE_ETH_DEV_TO_PCI(eth_dev);
2514         rte_eth_copy_pci_info(eth_dev, pci_dev);
2515         eth_dev->data->dev_flags |= RTE_ETH_DEV_DETACHABLE;
2516
2517         hw->device_id = pci_dev->id.device_id;
2518         hw->vendor_id = pci_dev->id.vendor_id;
2519         hw->subsystem_device_id = pci_dev->id.subsystem_device_id;
2520         hw->subsystem_vendor_id = pci_dev->id.subsystem_vendor_id;
2521
2522         PMD_INIT_LOG(DEBUG, "nfp_net: device (%u:%u) %u:%u:%u:%u",
2523                      pci_dev->id.vendor_id, pci_dev->id.device_id,
2524                      pci_dev->addr.domain, pci_dev->addr.bus,
2525                      pci_dev->addr.devid, pci_dev->addr.function);
2526
2527         hw->ctrl_bar = (uint8_t *)pci_dev->mem_resource[0].addr;
2528         if (hw->ctrl_bar == NULL) {
2529                 RTE_LOG(ERR, PMD,
2530                         "hw->ctrl_bar is NULL. BAR0 not configured\n");
2531                 return -ENODEV;
2532         }
2533         hw->max_rx_queues = nn_cfg_readl(hw, NFP_NET_CFG_MAX_RXRINGS);
2534         hw->max_tx_queues = nn_cfg_readl(hw, NFP_NET_CFG_MAX_TXRINGS);
2535
2536         /* Work out where in the BAR the queues start. */
2537         switch (pci_dev->id.device_id) {
2538         case PCI_DEVICE_ID_NFP6000_VF_NIC:
2539                 start_q = nn_cfg_readl(hw, NFP_NET_CFG_START_TXQ);
2540                 tx_bar_off = NFP_PCIE_QUEUE(start_q);
2541                 start_q = nn_cfg_readl(hw, NFP_NET_CFG_START_RXQ);
2542                 rx_bar_off = NFP_PCIE_QUEUE(start_q);
2543                 break;
2544         default:
2545                 RTE_LOG(ERR, PMD, "nfp_net: no device ID matching\n");
2546                 return -ENODEV;
2547         }
2548
2549         PMD_INIT_LOG(DEBUG, "tx_bar_off: 0x%08x", tx_bar_off);
2550         PMD_INIT_LOG(DEBUG, "rx_bar_off: 0x%08x", rx_bar_off);
2551
2552         hw->tx_bar = (uint8_t *)pci_dev->mem_resource[2].addr + tx_bar_off;
2553         hw->rx_bar = (uint8_t *)pci_dev->mem_resource[2].addr + rx_bar_off;
2554
2555         PMD_INIT_LOG(DEBUG, "ctrl_bar: %p, tx_bar: %p, rx_bar: %p",
2556                      hw->ctrl_bar, hw->tx_bar, hw->rx_bar);
2557
2558         nfp_net_cfg_queue_setup(hw);
2559
2560         /* Get some of the read-only fields from the config BAR */
2561         hw->ver = nn_cfg_readl(hw, NFP_NET_CFG_VERSION);
2562         hw->cap = nn_cfg_readl(hw, NFP_NET_CFG_CAP);
2563         hw->max_mtu = nn_cfg_readl(hw, NFP_NET_CFG_MAX_MTU);
2564         hw->mtu = hw->max_mtu;
2565
2566         if (NFD_CFG_MAJOR_VERSION_of(hw->ver) < 2)
2567                 hw->rx_offset = NFP_NET_RX_OFFSET;
2568         else
2569                 hw->rx_offset = nn_cfg_readl(hw, NFP_NET_CFG_RX_OFFSET_ADDR);
2570
2571         PMD_INIT_LOG(INFO, "VER: %#x, Maximum supported MTU: %d",
2572                      hw->ver, hw->max_mtu);
2573         PMD_INIT_LOG(INFO, "CAP: %#x, %s%s%s%s%s%s%s%s%s", hw->cap,
2574                      hw->cap & NFP_NET_CFG_CTRL_PROMISC ? "PROMISC " : "",
2575                      hw->cap & NFP_NET_CFG_CTRL_RXCSUM  ? "RXCSUM "  : "",
2576                      hw->cap & NFP_NET_CFG_CTRL_TXCSUM  ? "TXCSUM "  : "",
2577                      hw->cap & NFP_NET_CFG_CTRL_RXVLAN  ? "RXVLAN "  : "",
2578                      hw->cap & NFP_NET_CFG_CTRL_TXVLAN  ? "TXVLAN "  : "",
2579                      hw->cap & NFP_NET_CFG_CTRL_SCATTER ? "SCATTER " : "",
2580                      hw->cap & NFP_NET_CFG_CTRL_GATHER  ? "GATHER "  : "",
2581                      hw->cap & NFP_NET_CFG_CTRL_LSO     ? "TSO "     : "",
2582                      hw->cap & NFP_NET_CFG_CTRL_RSS     ? "RSS "     : "");
2583
2584         hw->ctrl = 0;
2585
2586         hw->stride_rx = stride;
2587         hw->stride_tx = stride;
2588
2589         PMD_INIT_LOG(INFO, "max_rx_queues: %u, max_tx_queues: %u",
2590                      hw->max_rx_queues, hw->max_tx_queues);
2591
2592         /* Initializing spinlock for reconfigs */
2593         rte_spinlock_init(&hw->reconfig_lock);
2594
2595         /* Allocating memory for mac addr */
2596         eth_dev->data->mac_addrs = rte_zmalloc("mac_addr", ETHER_ADDR_LEN, 0);
2597         if (eth_dev->data->mac_addrs == NULL) {
2598                 PMD_INIT_LOG(ERR, "Failed to space for MAC address");
2599                 return -ENOMEM;
2600         }
2601
2602         nfp_net_read_mac(hw);
2603
2604         if (!is_valid_assigned_ether_addr((struct ether_addr *)&hw->mac_addr)) {
2605                 /* Using random mac addresses for VFs */
2606                 eth_random_addr(&hw->mac_addr[0]);
2607                 nfp_net_write_mac(hw, (uint8_t *)&hw->mac_addr);
2608         }
2609
2610         /* Copying mac address to DPDK eth_dev struct */
2611         ether_addr_copy((struct ether_addr *)hw->mac_addr,
2612                         &eth_dev->data->mac_addrs[0]);
2613
2614         PMD_INIT_LOG(INFO, "port %d VendorID=0x%x DeviceID=0x%x "
2615                      "mac=%02x:%02x:%02x:%02x:%02x:%02x",
2616                      eth_dev->data->port_id, pci_dev->id.vendor_id,
2617                      pci_dev->id.device_id,
2618                      hw->mac_addr[0], hw->mac_addr[1], hw->mac_addr[2],
2619                      hw->mac_addr[3], hw->mac_addr[4], hw->mac_addr[5]);
2620
2621         /* Registering LSC interrupt handler */
2622         rte_intr_callback_register(&pci_dev->intr_handle,
2623                                    nfp_net_dev_interrupt_handler,
2624                                    (void *)eth_dev);
2625
2626         /* Telling the firmware about the LSC interrupt entry */
2627         nn_cfg_writeb(hw, NFP_NET_CFG_LSC, NFP_NET_IRQ_LSC_IDX);
2628
2629         /* Recording current stats counters values */
2630         nfp_net_stats_reset(eth_dev);
2631
2632         return 0;
2633 }
2634
2635 static const struct rte_pci_id pci_id_nfp_net_map[] = {
2636         {
2637                 RTE_PCI_DEVICE(PCI_VENDOR_ID_NETRONOME,
2638                                PCI_DEVICE_ID_NFP6000_PF_NIC)
2639         },
2640         {
2641                 RTE_PCI_DEVICE(PCI_VENDOR_ID_NETRONOME,
2642                                PCI_DEVICE_ID_NFP6000_VF_NIC)
2643         },
2644         {
2645                 .vendor_id = 0,
2646         },
2647 };
2648
2649 static int eth_nfp_pci_probe(struct rte_pci_driver *pci_drv __rte_unused,
2650         struct rte_pci_device *pci_dev)
2651 {
2652         return rte_eth_dev_pci_generic_probe(pci_dev,
2653                 sizeof(struct nfp_net_adapter), nfp_net_init);
2654 }
2655
2656 static int eth_nfp_pci_remove(struct rte_pci_device *pci_dev)
2657 {
2658         return rte_eth_dev_pci_generic_remove(pci_dev, NULL);
2659 }
2660
2661 static struct rte_pci_driver rte_nfp_net_pmd = {
2662         .id_table = pci_id_nfp_net_map,
2663         .drv_flags = RTE_PCI_DRV_NEED_MAPPING | RTE_PCI_DRV_INTR_LSC,
2664         .probe = eth_nfp_pci_probe,
2665         .remove = eth_nfp_pci_remove,
2666 };
2667
2668 RTE_PMD_REGISTER_PCI(net_nfp, rte_nfp_net_pmd);
2669 RTE_PMD_REGISTER_PCI_TABLE(net_nfp, pci_id_nfp_net_map);
2670 RTE_PMD_REGISTER_KMOD_DEP(net_nfp, "* igb_uio | uio_pci_generic | vfio-pci");
2671
2672 /*
2673  * Local variables:
2674  * c-file-style: "Linux"
2675  * indent-tabs-mode: t
2676  * End:
2677  */