nfp: add statistics
[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 <stdio.h>
43 #include <stdlib.h>
44 #include <stdint.h>
45 #include <unistd.h>
46 #include <string.h>
47 #include <sys/mman.h>
48 #include <sys/socket.h>
49 #include <sys/io.h>
50 #include <assert.h>
51 #include <time.h>
52 #include <math.h>
53 #include <inttypes.h>
54
55 #include <rte_byteorder.h>
56 #include <rte_common.h>
57 #include <rte_log.h>
58 #include <rte_debug.h>
59 #include <rte_ethdev.h>
60 #include <rte_dev.h>
61 #include <rte_ether.h>
62 #include <rte_malloc.h>
63 #include <rte_memzone.h>
64 #include <rte_mempool.h>
65 #include <rte_version.h>
66 #include <rte_string_fns.h>
67 #include <rte_alarm.h>
68
69 #include "nfp_net_pmd.h"
70 #include "nfp_net_logs.h"
71 #include "nfp_net_ctrl.h"
72
73 /* Prototypes */
74 static void nfp_net_close(struct rte_eth_dev *dev);
75 static int nfp_net_configure(struct rte_eth_dev *dev);
76 static int nfp_net_init(struct rte_eth_dev *eth_dev);
77 static int nfp_net_rx_fill_freelist(struct nfp_net_rxq *rxq);
78 static uint32_t nfp_net_rx_queue_count(struct rte_eth_dev *dev,
79                                        uint16_t queue_idx);
80 static uint16_t nfp_net_recv_pkts(void *rx_queue, struct rte_mbuf **rx_pkts,
81                                   uint16_t nb_pkts);
82 static void nfp_net_rx_queue_release(void *rxq);
83 static int nfp_net_rx_queue_setup(struct rte_eth_dev *dev, uint16_t queue_idx,
84                                   uint16_t nb_desc, unsigned int socket_id,
85                                   const struct rte_eth_rxconf *rx_conf,
86                                   struct rte_mempool *mp);
87 static int nfp_net_tx_free_bufs(struct nfp_net_txq *txq);
88 static void nfp_net_tx_queue_release(void *txq);
89 static int nfp_net_tx_queue_setup(struct rte_eth_dev *dev, uint16_t queue_idx,
90                                   uint16_t nb_desc, unsigned int socket_id,
91                                   const struct rte_eth_txconf *tx_conf);
92 static int nfp_net_start(struct rte_eth_dev *dev);
93 static void nfp_net_stats_get(struct rte_eth_dev *dev,
94                               struct rte_eth_stats *stats);
95 static void nfp_net_stats_reset(struct rte_eth_dev *dev);
96 static void nfp_net_stop(struct rte_eth_dev *dev);
97 static uint16_t nfp_net_xmit_pkts(void *tx_queue, struct rte_mbuf **tx_pkts,
98                                   uint16_t nb_pkts);
99
100 /*
101  * The offset of the queue controller queues in the PCIe Target. These
102  * happen to be at the same offset on the NFP6000 and the NFP3200 so
103  * we use a single macro here.
104  */
105 #define NFP_PCIE_QUEUE(_q)      (0x80000 + (0x800 * ((_q) & 0xff)))
106
107 /* Maximum value which can be added to a queue with one transaction */
108 #define NFP_QCP_MAX_ADD 0x7f
109
110 #define RTE_MBUF_DMA_ADDR_DEFAULT(mb) \
111         (uint64_t)((mb)->buf_physaddr + RTE_PKTMBUF_HEADROOM)
112
113 /* nfp_qcp_ptr - Read or Write Pointer of a queue */
114 enum nfp_qcp_ptr {
115         NFP_QCP_READ_PTR = 0,
116         NFP_QCP_WRITE_PTR
117 };
118
119 /*
120  * nfp_qcp_ptr_add - Add the value to the selected pointer of a queue
121  * @q: Base address for queue structure
122  * @ptr: Add to the Read or Write pointer
123  * @val: Value to add to the queue pointer
124  *
125  * If @val is greater than @NFP_QCP_MAX_ADD multiple writes are performed.
126  */
127 static inline void
128 nfp_qcp_ptr_add(uint8_t *q, enum nfp_qcp_ptr ptr, uint32_t val)
129 {
130         uint32_t off;
131
132         if (ptr == NFP_QCP_READ_PTR)
133                 off = NFP_QCP_QUEUE_ADD_RPTR;
134         else
135                 off = NFP_QCP_QUEUE_ADD_WPTR;
136
137         while (val > NFP_QCP_MAX_ADD) {
138                 nn_writel(rte_cpu_to_le_32(NFP_QCP_MAX_ADD), q + off);
139                 val -= NFP_QCP_MAX_ADD;
140         }
141
142         nn_writel(rte_cpu_to_le_32(val), q + off);
143 }
144
145 /*
146  * nfp_qcp_read - Read the current Read/Write pointer value for a queue
147  * @q:  Base address for queue structure
148  * @ptr: Read or Write pointer
149  */
150 static inline uint32_t
151 nfp_qcp_read(uint8_t *q, enum nfp_qcp_ptr ptr)
152 {
153         uint32_t off;
154         uint32_t val;
155
156         if (ptr == NFP_QCP_READ_PTR)
157                 off = NFP_QCP_QUEUE_STS_LO;
158         else
159                 off = NFP_QCP_QUEUE_STS_HI;
160
161         val = rte_cpu_to_le_32(nn_readl(q + off));
162
163         if (ptr == NFP_QCP_READ_PTR)
164                 return val & NFP_QCP_QUEUE_STS_LO_READPTR_mask;
165         else
166                 return val & NFP_QCP_QUEUE_STS_HI_WRITEPTR_mask;
167 }
168
169 /*
170  * Functions to read/write from/to Config BAR
171  * Performs any endian conversion necessary.
172  */
173 static inline uint8_t
174 nn_cfg_readb(struct nfp_net_hw *hw, int off)
175 {
176         return nn_readb(hw->ctrl_bar + off);
177 }
178
179 static inline void
180 nn_cfg_writeb(struct nfp_net_hw *hw, int off, uint8_t val)
181 {
182         nn_writeb(val, hw->ctrl_bar + off);
183 }
184
185 static inline uint32_t
186 nn_cfg_readl(struct nfp_net_hw *hw, int off)
187 {
188         return rte_le_to_cpu_32(nn_readl(hw->ctrl_bar + off));
189 }
190
191 static inline void
192 nn_cfg_writel(struct nfp_net_hw *hw, int off, uint32_t val)
193 {
194         nn_writel(rte_cpu_to_le_32(val), hw->ctrl_bar + off);
195 }
196
197 static inline uint64_t
198 nn_cfg_readq(struct nfp_net_hw *hw, int off)
199 {
200         return rte_le_to_cpu_64(nn_readq(hw->ctrl_bar + off));
201 }
202
203 static inline void
204 nn_cfg_writeq(struct nfp_net_hw *hw, int off, uint64_t val)
205 {
206         nn_writeq(rte_cpu_to_le_64(val), hw->ctrl_bar + off);
207 }
208
209 /* Creating memzone for hardware rings. */
210 static const struct rte_memzone *
211 ring_dma_zone_reserve(struct rte_eth_dev *dev, const char *ring_name,
212                       uint16_t queue_id, uint32_t ring_size, int socket_id)
213 {
214         char z_name[RTE_MEMZONE_NAMESIZE];
215         const struct rte_memzone *mz;
216
217         snprintf(z_name, sizeof(z_name), "%s_%s_%d_%d",
218                  dev->driver->pci_drv.name,
219                  ring_name, dev->data->port_id, queue_id);
220
221         mz = rte_memzone_lookup(z_name);
222         if (mz)
223                 return mz;
224
225         return rte_memzone_reserve_aligned(z_name, ring_size, socket_id, 0,
226                                            NFP_MEMZONE_ALIGN);
227 }
228
229 static void
230 nfp_net_rx_queue_release_mbufs(struct nfp_net_rxq *rxq)
231 {
232         unsigned i;
233
234         if (rxq->rxbufs == NULL)
235                 return;
236
237         for (i = 0; i < rxq->rx_count; i++) {
238                 if (rxq->rxbufs[i].mbuf) {
239                         rte_pktmbuf_free_seg(rxq->rxbufs[i].mbuf);
240                         rxq->rxbufs[i].mbuf = NULL;
241                 }
242         }
243 }
244
245 static void
246 nfp_net_rx_queue_release(void *rx_queue)
247 {
248         struct nfp_net_rxq *rxq = rx_queue;
249
250         if (rxq) {
251                 nfp_net_rx_queue_release_mbufs(rxq);
252                 rte_free(rxq->rxbufs);
253                 rte_free(rxq);
254         }
255 }
256
257 static void
258 nfp_net_reset_rx_queue(struct nfp_net_rxq *rxq)
259 {
260         nfp_net_rx_queue_release_mbufs(rxq);
261         rxq->wr_p = 0;
262         rxq->rd_p = 0;
263         rxq->nb_rx_hold = 0;
264 }
265
266 static void
267 nfp_net_tx_queue_release_mbufs(struct nfp_net_txq *txq)
268 {
269         unsigned i;
270
271         if (txq->txbufs == NULL)
272                 return;
273
274         for (i = 0; i < txq->tx_count; i++) {
275                 if (txq->txbufs[i].mbuf) {
276                         rte_pktmbuf_free_seg(txq->txbufs[i].mbuf);
277                         txq->txbufs[i].mbuf = NULL;
278                 }
279         }
280 }
281
282 static void
283 nfp_net_tx_queue_release(void *tx_queue)
284 {
285         struct nfp_net_txq *txq = tx_queue;
286
287         if (txq) {
288                 nfp_net_tx_queue_release_mbufs(txq);
289                 rte_free(txq->txbufs);
290                 rte_free(txq);
291         }
292 }
293
294 static void
295 nfp_net_reset_tx_queue(struct nfp_net_txq *txq)
296 {
297         nfp_net_tx_queue_release_mbufs(txq);
298         txq->wr_p = 0;
299         txq->rd_p = 0;
300         txq->tail = 0;
301 }
302
303 static int
304 __nfp_net_reconfig(struct nfp_net_hw *hw, uint32_t update)
305 {
306         int cnt;
307         uint32_t new;
308         struct timespec wait;
309
310         PMD_DRV_LOG(DEBUG, "Writing to the configuration queue (%p)...\n",
311                     hw->qcp_cfg);
312
313         if (hw->qcp_cfg == NULL)
314                 rte_panic("Bad configuration queue pointer\n");
315
316         nfp_qcp_ptr_add(hw->qcp_cfg, NFP_QCP_WRITE_PTR, 1);
317
318         wait.tv_sec = 0;
319         wait.tv_nsec = 1000000;
320
321         PMD_DRV_LOG(DEBUG, "Polling for update ack...\n");
322
323         /* Poll update field, waiting for NFP to ack the config */
324         for (cnt = 0; ; cnt++) {
325                 new = nn_cfg_readl(hw, NFP_NET_CFG_UPDATE);
326                 if (new == 0)
327                         break;
328                 if (new & NFP_NET_CFG_UPDATE_ERR) {
329                         PMD_INIT_LOG(ERR, "Reconfig error: 0x%08x\n", new);
330                         return -1;
331                 }
332                 if (cnt >= NFP_NET_POLL_TIMEOUT) {
333                         PMD_INIT_LOG(ERR, "Reconfig timeout for 0x%08x after"
334                                           " %dms\n", update, cnt);
335                         rte_panic("Exiting\n");
336                 }
337                 nanosleep(&wait, 0); /* waiting for a 1ms */
338         }
339         PMD_DRV_LOG(DEBUG, "Ack DONE\n");
340         return 0;
341 }
342
343 /*
344  * Reconfigure the NIC
345  * @nn:    device to reconfigure
346  * @ctrl:    The value for the ctrl field in the BAR config
347  * @update:  The value for the update field in the BAR config
348  *
349  * Write the update word to the BAR and ping the reconfig queue. Then poll
350  * until the firmware has acknowledged the update by zeroing the update word.
351  */
352 static int
353 nfp_net_reconfig(struct nfp_net_hw *hw, uint32_t ctrl, uint32_t update)
354 {
355         uint32_t err;
356
357         PMD_DRV_LOG(DEBUG, "nfp_net_reconfig: ctrl=%08x update=%08x\n",
358                     ctrl, update);
359
360         nn_cfg_writel(hw, NFP_NET_CFG_CTRL, ctrl);
361         nn_cfg_writel(hw, NFP_NET_CFG_UPDATE, update);
362
363         rte_wmb();
364
365         err = __nfp_net_reconfig(hw, update);
366
367         if (!err)
368                 return 0;
369
370         /*
371          * Reconfig errors imply situations where they can be handled.
372          * Otherwise, rte_panic is called inside __nfp_net_reconfig
373          */
374         PMD_INIT_LOG(ERR, "Error nfp_net reconfig for ctrl: %x update: %x\n",
375                      ctrl, update);
376         return -EIO;
377 }
378
379 /*
380  * Configure an Ethernet device. This function must be invoked first
381  * before any other function in the Ethernet API. This function can
382  * also be re-invoked when a device is in the stopped state.
383  */
384 static int
385 nfp_net_configure(struct rte_eth_dev *dev)
386 {
387         struct rte_eth_conf *dev_conf;
388         struct rte_eth_rxmode *rxmode;
389         struct rte_eth_txmode *txmode;
390         uint32_t new_ctrl = 0;
391         uint32_t update = 0;
392         struct nfp_net_hw *hw;
393
394         hw = NFP_NET_DEV_PRIVATE_TO_HW(dev->data->dev_private);
395
396         /*
397          * A DPDK app sends info about how many queues to use and how
398          * those queues need to be configured. This is used by the
399          * DPDK core and it makes sure no more queues than those
400          * advertised by the driver are requested. This function is
401          * called after that internal process
402          */
403
404         PMD_INIT_LOG(DEBUG, "Configure\n");
405
406         dev_conf = &dev->data->dev_conf;
407         rxmode = &dev_conf->rxmode;
408         txmode = &dev_conf->txmode;
409
410         /* Checking TX mode */
411         if (txmode->mq_mode) {
412                 PMD_INIT_LOG(INFO, "TX mq_mode DCB and VMDq not supported\n");
413                 return -EINVAL;
414         }
415
416         /* Checking RX mode */
417         if (rxmode->mq_mode & ETH_MQ_RX_RSS) {
418                 if (hw->cap & NFP_NET_CFG_CTRL_RSS) {
419                         update = NFP_NET_CFG_UPDATE_RSS;
420                         new_ctrl = NFP_NET_CFG_CTRL_RSS;
421                 } else {
422                         PMD_INIT_LOG(INFO, "RSS not supported\n");
423                         return -EINVAL;
424                 }
425         }
426
427         if (rxmode->split_hdr_size) {
428                 PMD_INIT_LOG(INFO, "rxmode does not support split header\n");
429                 return -EINVAL;
430         }
431
432         if (rxmode->hw_ip_checksum) {
433                 if (hw->cap & NFP_NET_CFG_CTRL_RXCSUM) {
434                         new_ctrl |= NFP_NET_CFG_CTRL_RXCSUM;
435                 } else {
436                         PMD_INIT_LOG(INFO, "RXCSUM not supported\n");
437                         return -EINVAL;
438                 }
439         }
440
441         if (rxmode->hw_vlan_filter) {
442                 PMD_INIT_LOG(INFO, "VLAN filter not supported\n");
443                 return -EINVAL;
444         }
445
446         if (rxmode->hw_vlan_strip) {
447                 if (hw->cap & NFP_NET_CFG_CTRL_RXVLAN) {
448                         new_ctrl |= NFP_NET_CFG_CTRL_RXVLAN;
449                 } else {
450                         PMD_INIT_LOG(INFO, "hw vlan strip not supported\n");
451                         return -EINVAL;
452                 }
453         }
454
455         if (rxmode->hw_vlan_extend) {
456                 PMD_INIT_LOG(INFO, "VLAN extended not supported\n");
457                 return -EINVAL;
458         }
459
460         /* Supporting VLAN insertion by default */
461         if (hw->cap & NFP_NET_CFG_CTRL_TXVLAN)
462                 new_ctrl |= NFP_NET_CFG_CTRL_TXVLAN;
463
464         if (rxmode->jumbo_frame)
465                 /* this is handled in rte_eth_dev_configure */
466
467         if (rxmode->hw_strip_crc) {
468                 PMD_INIT_LOG(INFO, "strip CRC not supported\n");
469                 return -EINVAL;
470         }
471
472         if (rxmode->enable_scatter) {
473                 PMD_INIT_LOG(INFO, "Scatter not supported\n");
474                 return -EINVAL;
475         }
476
477         if (!new_ctrl)
478                 return 0;
479
480         update |= NFP_NET_CFG_UPDATE_GEN;
481
482         nn_cfg_writel(hw, NFP_NET_CFG_CTRL, new_ctrl);
483         if (nfp_net_reconfig(hw, new_ctrl, update) < 0)
484                 return -EIO;
485
486         hw->ctrl = new_ctrl;
487
488         return 0;
489 }
490
491 static void
492 nfp_net_enable_queues(struct rte_eth_dev *dev)
493 {
494         struct nfp_net_hw *hw;
495         uint64_t enabled_queues = 0;
496         int i;
497
498         hw = NFP_NET_DEV_PRIVATE_TO_HW(dev->data->dev_private);
499
500         /* Enabling the required TX queues in the device */
501         for (i = 0; i < dev->data->nb_tx_queues; i++)
502                 enabled_queues |= (1 << i);
503
504         nn_cfg_writeq(hw, NFP_NET_CFG_TXRS_ENABLE, enabled_queues);
505
506         enabled_queues = 0;
507
508         /* Enabling the required RX queues in the device */
509         for (i = 0; i < dev->data->nb_rx_queues; i++)
510                 enabled_queues |= (1 << i);
511
512         nn_cfg_writeq(hw, NFP_NET_CFG_RXRS_ENABLE, enabled_queues);
513 }
514
515 static void
516 nfp_net_disable_queues(struct rte_eth_dev *dev)
517 {
518         struct nfp_net_hw *hw;
519         uint32_t new_ctrl, update = 0;
520
521         hw = NFP_NET_DEV_PRIVATE_TO_HW(dev->data->dev_private);
522
523         nn_cfg_writeq(hw, NFP_NET_CFG_TXRS_ENABLE, 0);
524         nn_cfg_writeq(hw, NFP_NET_CFG_RXRS_ENABLE, 0);
525
526         new_ctrl = hw->ctrl & ~NFP_NET_CFG_CTRL_ENABLE;
527         update = NFP_NET_CFG_UPDATE_GEN | NFP_NET_CFG_UPDATE_RING |
528                  NFP_NET_CFG_UPDATE_MSIX;
529
530         if (hw->cap & NFP_NET_CFG_CTRL_RINGCFG)
531                 new_ctrl &= ~NFP_NET_CFG_CTRL_RINGCFG;
532
533         /* If an error when reconfig we avoid to change hw state */
534         if (nfp_net_reconfig(hw, new_ctrl, update) < 0)
535                 return;
536
537         hw->ctrl = new_ctrl;
538 }
539
540 static int
541 nfp_net_rx_freelist_setup(struct rte_eth_dev *dev)
542 {
543         int i;
544
545         for (i = 0; i < dev->data->nb_rx_queues; i++) {
546                 if (nfp_net_rx_fill_freelist(dev->data->rx_queues[i]) < 0)
547                         return -1;
548         }
549         return 0;
550 }
551
552 static void
553 nfp_net_params_setup(struct nfp_net_hw *hw)
554 {
555         uint32_t *mac_address;
556
557         nn_cfg_writel(hw, NFP_NET_CFG_MTU, hw->mtu);
558         nn_cfg_writel(hw, NFP_NET_CFG_FLBUFSZ, hw->flbufsz);
559
560         /* A MAC address is 8 bytes long */
561         mac_address = (uint32_t *)(hw->mac_addr);
562
563         nn_cfg_writel(hw, NFP_NET_CFG_MACADDR,
564                       rte_cpu_to_be_32(*mac_address));
565         nn_cfg_writel(hw, NFP_NET_CFG_MACADDR + 4,
566                       rte_cpu_to_be_32(*(mac_address + 4)));
567 }
568
569 static void
570 nfp_net_cfg_queue_setup(struct nfp_net_hw *hw)
571 {
572         hw->qcp_cfg = hw->tx_bar + NFP_QCP_QUEUE_ADDR_SZ;
573 }
574
575 static int
576 nfp_net_start(struct rte_eth_dev *dev)
577 {
578         uint32_t new_ctrl, update = 0;
579         struct nfp_net_hw *hw;
580         int ret;
581
582         hw = NFP_NET_DEV_PRIVATE_TO_HW(dev->data->dev_private);
583
584         PMD_INIT_LOG(DEBUG, "Start\n");
585
586         /* Disabling queues just in case... */
587         nfp_net_disable_queues(dev);
588
589         /* Writing configuration parameters in the device */
590         nfp_net_params_setup(hw);
591
592         /* Enabling the required queues in the device */
593         nfp_net_enable_queues(dev);
594
595         /* Enable device */
596         new_ctrl = hw->ctrl | NFP_NET_CFG_CTRL_ENABLE | NFP_NET_CFG_UPDATE_MSIX;
597         update = NFP_NET_CFG_UPDATE_GEN | NFP_NET_CFG_UPDATE_RING;
598
599         if (hw->cap & NFP_NET_CFG_CTRL_RINGCFG)
600                 new_ctrl |= NFP_NET_CFG_CTRL_RINGCFG;
601
602         nn_cfg_writel(hw, NFP_NET_CFG_CTRL, new_ctrl);
603         if (nfp_net_reconfig(hw, new_ctrl, update) < 0)
604                 return -EIO;
605
606         /*
607          * Allocating rte mbuffs for configured rx queues.
608          * This requires queues being enabled before
609          */
610         if (nfp_net_rx_freelist_setup(dev) < 0) {
611                 ret = -ENOMEM;
612                 goto error;
613         }
614
615         hw->ctrl = new_ctrl;
616
617         return 0;
618
619 error:
620         /*
621          * An error returned by this function should mean the app
622          * exiting and then the system releasing all the memory
623          * allocated even memory coming from hugepages.
624          *
625          * The device could be enabled at this point with some queues
626          * ready for getting packets. This is true if the call to
627          * nfp_net_rx_freelist_setup() succeeds for some queues but
628          * fails for subsequent queues.
629          *
630          * This should make the app exiting but better if we tell the
631          * device first.
632          */
633         nfp_net_disable_queues(dev);
634
635         return ret;
636 }
637
638 /* Stop device: disable rx and tx functions to allow for reconfiguring. */
639 static void
640 nfp_net_stop(struct rte_eth_dev *dev)
641 {
642         int i;
643
644         PMD_INIT_LOG(DEBUG, "Stop\n");
645
646         nfp_net_disable_queues(dev);
647
648         /* Clear queues */
649         for (i = 0; i < dev->data->nb_tx_queues; i++) {
650                 nfp_net_reset_tx_queue(
651                         (struct nfp_net_txq *)dev->data->tx_queues[i]);
652         }
653
654         for (i = 0; i < dev->data->nb_rx_queues; i++) {
655                 nfp_net_reset_rx_queue(
656                         (struct nfp_net_rxq *)dev->data->rx_queues[i]);
657         }
658 }
659
660 /* Reset and stop device. The device can not be restarted. */
661 static void
662 nfp_net_close(struct rte_eth_dev *dev)
663 {
664         struct nfp_net_hw *hw;
665
666         PMD_INIT_LOG(DEBUG, "Close\n");
667
668         hw = NFP_NET_DEV_PRIVATE_TO_HW(dev->data->dev_private);
669
670         /*
671          * We assume that the DPDK application is stopping all the
672          * threads/queues before calling the device close function.
673          */
674
675         nfp_net_stop(dev);
676
677         nn_cfg_writeb(hw, NFP_NET_CFG_LSC, 0xff);
678
679         /*
680          * The ixgbe PMD driver disables the pcie master on the
681          * device. The i40e does not...
682          */
683 }
684
685 static void
686 nfp_net_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
687 {
688         int i;
689         struct nfp_net_hw *hw;
690         struct rte_eth_stats nfp_dev_stats;
691
692         hw = NFP_NET_DEV_PRIVATE_TO_HW(dev->data->dev_private);
693
694         /* RTE_ETHDEV_QUEUE_STAT_CNTRS default value is 16 */
695
696         /* reading per RX ring stats */
697         for (i = 0; i < dev->data->nb_rx_queues; i++) {
698                 if (i == RTE_ETHDEV_QUEUE_STAT_CNTRS)
699                         break;
700
701                 nfp_dev_stats.q_ipackets[i] =
702                         nn_cfg_readq(hw, NFP_NET_CFG_RXR_STATS(i));
703
704                 nfp_dev_stats.q_ipackets[i] -=
705                         hw->eth_stats_base.q_ipackets[i];
706
707                 nfp_dev_stats.q_ibytes[i] =
708                         nn_cfg_readq(hw, NFP_NET_CFG_RXR_STATS(i) + 0x8);
709
710                 nfp_dev_stats.q_ibytes[i] -=
711                         hw->eth_stats_base.q_ibytes[i];
712         }
713
714         /* reading per TX ring stats */
715         for (i = 0; i < dev->data->nb_tx_queues; i++) {
716                 if (i == RTE_ETHDEV_QUEUE_STAT_CNTRS)
717                         break;
718
719                 nfp_dev_stats.q_opackets[i] =
720                         nn_cfg_readq(hw, NFP_NET_CFG_TXR_STATS(i));
721
722                 nfp_dev_stats.q_opackets[i] -=
723                         hw->eth_stats_base.q_opackets[i];
724
725                 nfp_dev_stats.q_obytes[i] =
726                         nn_cfg_readq(hw, NFP_NET_CFG_TXR_STATS(i) + 0x8);
727
728                 nfp_dev_stats.q_obytes[i] -=
729                         hw->eth_stats_base.q_obytes[i];
730         }
731
732         nfp_dev_stats.ipackets =
733                 nn_cfg_readq(hw, NFP_NET_CFG_STATS_RX_FRAMES);
734
735         nfp_dev_stats.ipackets -= hw->eth_stats_base.ipackets;
736
737         nfp_dev_stats.ibytes =
738                 nn_cfg_readq(hw, NFP_NET_CFG_STATS_RX_OCTETS);
739
740         nfp_dev_stats.ibytes -= hw->eth_stats_base.ibytes;
741
742         nfp_dev_stats.opackets =
743                 nn_cfg_readq(hw, NFP_NET_CFG_STATS_TX_FRAMES);
744
745         nfp_dev_stats.opackets -= hw->eth_stats_base.opackets;
746
747         nfp_dev_stats.obytes =
748                 nn_cfg_readq(hw, NFP_NET_CFG_STATS_TX_OCTETS);
749
750         nfp_dev_stats.obytes -= hw->eth_stats_base.obytes;
751
752         nfp_dev_stats.imcasts =
753                 nn_cfg_readq(hw, NFP_NET_CFG_STATS_RX_MC_FRAMES);
754
755         nfp_dev_stats.imcasts -= hw->eth_stats_base.imcasts;
756
757         /* reading general device stats */
758         nfp_dev_stats.ierrors =
759                 nn_cfg_readq(hw, NFP_NET_CFG_STATS_RX_ERRORS);
760
761         nfp_dev_stats.ierrors -= hw->eth_stats_base.ierrors;
762
763         nfp_dev_stats.oerrors =
764                 nn_cfg_readq(hw, NFP_NET_CFG_STATS_TX_ERRORS);
765
766         nfp_dev_stats.oerrors -= hw->eth_stats_base.oerrors;
767
768         /* Multicast frames received */
769         nfp_dev_stats.imcasts =
770                 nn_cfg_readq(hw, NFP_NET_CFG_STATS_RX_MC_FRAMES);
771
772         nfp_dev_stats.imcasts -= hw->eth_stats_base.imcasts;
773
774         /* RX ring mbuf allocation failures */
775         nfp_dev_stats.rx_nombuf = dev->data->rx_mbuf_alloc_failed;
776
777         nfp_dev_stats.imissed =
778                 nn_cfg_readq(hw, NFP_NET_CFG_STATS_RX_DISCARDS);
779
780         nfp_dev_stats.imissed -= hw->eth_stats_base.imissed;
781
782         if (stats)
783                 memcpy(stats, &nfp_dev_stats, sizeof(*stats));
784 }
785
786 static void
787 nfp_net_stats_reset(struct rte_eth_dev *dev)
788 {
789         int i;
790         struct nfp_net_hw *hw;
791
792         hw = NFP_NET_DEV_PRIVATE_TO_HW(dev->data->dev_private);
793
794         /*
795          * hw->eth_stats_base records the per counter starting point.
796          * Lets update it now
797          */
798
799         /* reading per RX ring stats */
800         for (i = 0; i < dev->data->nb_rx_queues; i++) {
801                 if (i == RTE_ETHDEV_QUEUE_STAT_CNTRS)
802                         break;
803
804                 hw->eth_stats_base.q_ipackets[i] =
805                         nn_cfg_readq(hw, NFP_NET_CFG_RXR_STATS(i));
806
807                 hw->eth_stats_base.q_ibytes[i] =
808                         nn_cfg_readq(hw, NFP_NET_CFG_RXR_STATS(i) + 0x8);
809         }
810
811         /* reading per TX ring stats */
812         for (i = 0; i < dev->data->nb_tx_queues; i++) {
813                 if (i == RTE_ETHDEV_QUEUE_STAT_CNTRS)
814                         break;
815
816                 hw->eth_stats_base.q_opackets[i] =
817                         nn_cfg_readq(hw, NFP_NET_CFG_TXR_STATS(i));
818
819                 hw->eth_stats_base.q_obytes[i] =
820                         nn_cfg_readq(hw, NFP_NET_CFG_TXR_STATS(i) + 0x8);
821         }
822
823         hw->eth_stats_base.ipackets =
824                 nn_cfg_readq(hw, NFP_NET_CFG_STATS_RX_FRAMES);
825
826         hw->eth_stats_base.ibytes =
827                 nn_cfg_readq(hw, NFP_NET_CFG_STATS_RX_OCTETS);
828
829         hw->eth_stats_base.opackets =
830                 nn_cfg_readq(hw, NFP_NET_CFG_STATS_TX_FRAMES);
831
832         hw->eth_stats_base.obytes =
833                 nn_cfg_readq(hw, NFP_NET_CFG_STATS_TX_OCTETS);
834
835         hw->eth_stats_base.imcasts =
836                 nn_cfg_readq(hw, NFP_NET_CFG_STATS_RX_MC_FRAMES);
837
838         /* reading general device stats */
839         hw->eth_stats_base.ierrors =
840                 nn_cfg_readq(hw, NFP_NET_CFG_STATS_RX_ERRORS);
841
842         hw->eth_stats_base.oerrors =
843                 nn_cfg_readq(hw, NFP_NET_CFG_STATS_TX_ERRORS);
844
845         /* Multicast frames received */
846         hw->eth_stats_base.imcasts =
847                 nn_cfg_readq(hw, NFP_NET_CFG_STATS_RX_MC_FRAMES);
848
849         /* RX ring mbuf allocation failures */
850         dev->data->rx_mbuf_alloc_failed = 0;
851
852         hw->eth_stats_base.imissed =
853                 nn_cfg_readq(hw, NFP_NET_CFG_STATS_RX_DISCARDS);
854 }
855
856 static uint32_t
857 nfp_net_rx_queue_count(struct rte_eth_dev *dev, uint16_t queue_idx)
858 {
859         struct nfp_net_rxq *rxq;
860         struct nfp_net_rx_desc *rxds;
861         uint32_t idx;
862         uint32_t count;
863
864         rxq = (struct nfp_net_rxq *)dev->data->rx_queues[queue_idx];
865
866         if (rxq == NULL) {
867                 PMD_INIT_LOG(ERR, "Bad queue: %u\n", queue_idx);
868                 return 0;
869         }
870
871         idx = rxq->rd_p % rxq->rx_count;
872         rxds = &rxq->rxds[idx];
873
874         count = 0;
875
876         /*
877          * Other PMDs are just checking the DD bit in intervals of 4
878          * descriptors and counting all four if the first has the DD
879          * bit on. Of course, this is not accurate but can be good for
880          * perfomance. But ideally that should be done in descriptors
881          * chunks belonging to the same cache line
882          */
883
884         while (count < rxq->rx_count) {
885                 rxds = &rxq->rxds[idx];
886                 if ((rxds->rxd.meta_len_dd & PCIE_DESC_RX_DD) == 0)
887                         break;
888
889                 count++;
890                 idx++;
891
892                 /* Wrapping? */
893                 if ((idx) == rxq->rx_count)
894                         idx = 0;
895         }
896
897         return count;
898 }
899
900 static int
901 nfp_net_rx_queue_setup(struct rte_eth_dev *dev,
902                        uint16_t queue_idx, uint16_t nb_desc,
903                        unsigned int socket_id,
904                        const struct rte_eth_rxconf *rx_conf,
905                        struct rte_mempool *mp)
906 {
907         const struct rte_memzone *tz;
908         struct nfp_net_rxq *rxq;
909         struct nfp_net_hw *hw;
910
911         hw = NFP_NET_DEV_PRIVATE_TO_HW(dev->data->dev_private);
912
913         PMD_INIT_FUNC_TRACE();
914
915         /* Validating number of descriptors */
916         if (((nb_desc * sizeof(struct nfp_net_rx_desc)) % 128) != 0 ||
917             (nb_desc > NFP_NET_MAX_RX_DESC) ||
918             (nb_desc < NFP_NET_MIN_RX_DESC)) {
919                 RTE_LOG(ERR, PMD, "Wrong nb_desc value\n");
920                 return (-EINVAL);
921         }
922
923         /*
924          * Free memory prior to re-allocation if needed. This is the case after
925          * calling nfp_net_stop
926          */
927         if (dev->data->rx_queues[queue_idx]) {
928                 nfp_net_rx_queue_release(dev->data->rx_queues[queue_idx]);
929                 dev->data->rx_queues[queue_idx] = NULL;
930         }
931
932         /* Allocating rx queue data structure */
933         rxq = rte_zmalloc_socket("ethdev RX queue", sizeof(struct nfp_net_rxq),
934                                  RTE_CACHE_LINE_SIZE, socket_id);
935         if (rxq == NULL)
936                 return (-ENOMEM);
937
938         /* Hw queues mapping based on firmware confifguration */
939         rxq->qidx = queue_idx;
940         rxq->fl_qcidx = queue_idx * hw->stride_rx;
941         rxq->rx_qcidx = rxq->fl_qcidx + (hw->stride_rx - 1);
942         rxq->qcp_fl = hw->rx_bar + NFP_QCP_QUEUE_OFF(rxq->fl_qcidx);
943         rxq->qcp_rx = hw->rx_bar + NFP_QCP_QUEUE_OFF(rxq->rx_qcidx);
944
945         /*
946          * Tracking mbuf size for detecting a potential mbuf overflow due to
947          * RX offset
948          */
949         rxq->mem_pool = mp;
950         rxq->mbuf_size = rxq->mem_pool->elt_size;
951         rxq->mbuf_size -= (sizeof(struct rte_mbuf) + RTE_PKTMBUF_HEADROOM);
952         hw->flbufsz = rxq->mbuf_size;
953
954         rxq->rx_count = nb_desc;
955         rxq->port_id = dev->data->port_id;
956         rxq->rx_free_thresh = rx_conf->rx_free_thresh;
957         rxq->crc_len = (uint8_t) ((dev->data->dev_conf.rxmode.hw_strip_crc) ? 0
958                                   : ETHER_CRC_LEN);
959         rxq->drop_en = rx_conf->rx_drop_en;
960
961         /*
962          * Allocate RX ring hardware descriptors. A memzone large enough to
963          * handle the maximum ring size is allocated in order to allow for
964          * resizing in later calls to the queue setup function.
965          */
966         tz = ring_dma_zone_reserve(dev, "rx_ring", queue_idx,
967                                    sizeof(struct nfp_net_rx_desc) *
968                                    NFP_NET_MAX_RX_DESC, socket_id);
969
970         if (tz == NULL) {
971                 RTE_LOG(ERR, PMD, "Error allocatig rx dma\n");
972                 nfp_net_rx_queue_release(rxq);
973                 return (-ENOMEM);
974         }
975
976         /* Saving physical and virtual addresses for the RX ring */
977         rxq->dma = (uint64_t)tz->phys_addr;
978         rxq->rxds = (struct nfp_net_rx_desc *)tz->addr;
979
980         /* mbuf pointers array for referencing mbufs linked to RX descriptors */
981         rxq->rxbufs = rte_zmalloc_socket("rxq->rxbufs",
982                                          sizeof(*rxq->rxbufs) * nb_desc,
983                                          RTE_CACHE_LINE_SIZE, socket_id);
984         if (rxq->rxbufs == NULL) {
985                 nfp_net_rx_queue_release(rxq);
986                 return (-ENOMEM);
987         }
988
989         PMD_RX_LOG(DEBUG, "rxbufs=%p hw_ring=%p dma_addr=0x%" PRIx64 "\n",
990                    rxq->rxbufs, rxq->rxds, (unsigned long int)rxq->dma);
991
992         nfp_net_reset_rx_queue(rxq);
993
994         dev->data->rx_queues[queue_idx] = rxq;
995         rxq->hw = hw;
996
997         /*
998          * Telling the HW about the physical address of the RX ring and number
999          * of descriptors in log2 format
1000          */
1001         nn_cfg_writeq(hw, NFP_NET_CFG_RXR_ADDR(queue_idx), rxq->dma);
1002         nn_cfg_writeb(hw, NFP_NET_CFG_RXR_SZ(queue_idx), log2(nb_desc));
1003
1004         return 0;
1005 }
1006
1007 static int
1008 nfp_net_rx_fill_freelist(struct nfp_net_rxq *rxq)
1009 {
1010         struct nfp_net_rx_buff *rxe = rxq->rxbufs;
1011         uint64_t dma_addr;
1012         unsigned i;
1013
1014         PMD_RX_LOG(DEBUG, "nfp_net_rx_fill_freelist for %u descriptors\n",
1015                    rxq->rx_count);
1016
1017         for (i = 0; i < rxq->rx_count; i++) {
1018                 struct nfp_net_rx_desc *rxd;
1019                 struct rte_mbuf *mbuf = rte_pktmbuf_alloc(rxq->mem_pool);
1020
1021                 if (mbuf == NULL) {
1022                         RTE_LOG(ERR, PMD, "RX mbuf alloc failed queue_id=%u\n",
1023                                 (unsigned)rxq->qidx);
1024                         return (-ENOMEM);
1025                 }
1026
1027                 dma_addr = rte_cpu_to_le_64(RTE_MBUF_DMA_ADDR_DEFAULT(mbuf));
1028
1029                 rxd = &rxq->rxds[i];
1030                 rxd->fld.dd = 0;
1031                 rxd->fld.dma_addr_hi = (dma_addr >> 32) & 0xff;
1032                 rxd->fld.dma_addr_lo = dma_addr & 0xffffffff;
1033                 rxe[i].mbuf = mbuf;
1034                 PMD_RX_LOG(DEBUG, "[%d]: %" PRIx64 "\n", i, dma_addr);
1035
1036                 rxq->wr_p++;
1037         }
1038
1039         /* Make sure all writes are flushed before telling the hardware */
1040         rte_wmb();
1041
1042         /* Not advertising the whole ring as the firmware gets confused if so */
1043         PMD_RX_LOG(DEBUG, "Increment FL write pointer in %u\n",
1044                    rxq->rx_count - 1);
1045
1046         nfp_qcp_ptr_add(rxq->qcp_fl, NFP_QCP_WRITE_PTR, rxq->rx_count - 1);
1047
1048         return 0;
1049 }
1050
1051 static int
1052 nfp_net_tx_queue_setup(struct rte_eth_dev *dev, uint16_t queue_idx,
1053                        uint16_t nb_desc, unsigned int socket_id,
1054                        const struct rte_eth_txconf *tx_conf)
1055 {
1056         const struct rte_memzone *tz;
1057         struct nfp_net_txq *txq;
1058         uint16_t tx_free_thresh;
1059         struct nfp_net_hw *hw;
1060
1061         hw = NFP_NET_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1062
1063         PMD_INIT_FUNC_TRACE();
1064
1065         /* Validating number of descriptors */
1066         if (((nb_desc * sizeof(struct nfp_net_tx_desc)) % 128) != 0 ||
1067             (nb_desc > NFP_NET_MAX_TX_DESC) ||
1068             (nb_desc < NFP_NET_MIN_TX_DESC)) {
1069                 RTE_LOG(ERR, PMD, "Wrong nb_desc value\n");
1070                 return -EINVAL;
1071         }
1072
1073         tx_free_thresh = (uint16_t)((tx_conf->tx_free_thresh) ?
1074                                     tx_conf->tx_free_thresh :
1075                                     DEFAULT_TX_FREE_THRESH);
1076
1077         if (tx_free_thresh > (nb_desc)) {
1078                 RTE_LOG(ERR, PMD,
1079                         "tx_free_thresh must be less than the number of TX "
1080                         "descriptors. (tx_free_thresh=%u port=%d "
1081                         "queue=%d)\n", (unsigned int)tx_free_thresh,
1082                         (int)dev->data->port_id, (int)queue_idx);
1083                 return -(EINVAL);
1084         }
1085
1086         /*
1087          * Free memory prior to re-allocation if needed. This is the case after
1088          * calling nfp_net_stop
1089          */
1090         if (dev->data->tx_queues[queue_idx]) {
1091                 PMD_TX_LOG(DEBUG, "Freeing memory prior to re-allocation %d\n",
1092                            queue_idx);
1093                 nfp_net_tx_queue_release(dev->data->tx_queues[queue_idx]);
1094                 dev->data->tx_queues[queue_idx] = NULL;
1095         }
1096
1097         /* Allocating tx queue data structure */
1098         txq = rte_zmalloc_socket("ethdev TX queue", sizeof(struct nfp_net_txq),
1099                                  RTE_CACHE_LINE_SIZE, socket_id);
1100         if (txq == NULL) {
1101                 RTE_LOG(ERR, PMD, "Error allocating tx dma\n");
1102                 return (-ENOMEM);
1103         }
1104
1105         /*
1106          * Allocate TX ring hardware descriptors. A memzone large enough to
1107          * handle the maximum ring size is allocated in order to allow for
1108          * resizing in later calls to the queue setup function.
1109          */
1110         tz = ring_dma_zone_reserve(dev, "tx_ring", queue_idx,
1111                                    sizeof(struct nfp_net_tx_desc) *
1112                                    NFP_NET_MAX_TX_DESC, socket_id);
1113         if (tz == NULL) {
1114                 RTE_LOG(ERR, PMD, "Error allocating tx dma\n");
1115                 nfp_net_tx_queue_release(txq);
1116                 return (-ENOMEM);
1117         }
1118
1119         txq->tx_count = nb_desc;
1120         txq->tail = 0;
1121         txq->tx_free_thresh = tx_free_thresh;
1122         txq->tx_pthresh = tx_conf->tx_thresh.pthresh;
1123         txq->tx_hthresh = tx_conf->tx_thresh.hthresh;
1124         txq->tx_wthresh = tx_conf->tx_thresh.wthresh;
1125
1126         /* queue mapping based on firmware configuration */
1127         txq->qidx = queue_idx;
1128         txq->tx_qcidx = queue_idx * hw->stride_tx;
1129         txq->qcp_q = hw->tx_bar + NFP_QCP_QUEUE_OFF(txq->tx_qcidx);
1130
1131         txq->port_id = dev->data->port_id;
1132         txq->txq_flags = tx_conf->txq_flags;
1133
1134         /* Saving physical and virtual addresses for the TX ring */
1135         txq->dma = (uint64_t)tz->phys_addr;
1136         txq->txds = (struct nfp_net_tx_desc *)tz->addr;
1137
1138         /* mbuf pointers array for referencing mbufs linked to TX descriptors */
1139         txq->txbufs = rte_zmalloc_socket("txq->txbufs",
1140                                          sizeof(*txq->txbufs) * nb_desc,
1141                                          RTE_CACHE_LINE_SIZE, socket_id);
1142         if (txq->txbufs == NULL) {
1143                 nfp_net_tx_queue_release(txq);
1144                 return (-ENOMEM);
1145         }
1146         PMD_TX_LOG(DEBUG, "txbufs=%p hw_ring=%p dma_addr=0x%" PRIx64 "\n",
1147                    txq->txbufs, txq->txds, (unsigned long int)txq->dma);
1148
1149         nfp_net_reset_tx_queue(txq);
1150
1151         dev->data->tx_queues[queue_idx] = txq;
1152         txq->hw = hw;
1153
1154         /*
1155          * Telling the HW about the physical address of the TX ring and number
1156          * of descriptors in log2 format
1157          */
1158         nn_cfg_writeq(hw, NFP_NET_CFG_TXR_ADDR(queue_idx), txq->dma);
1159         nn_cfg_writeb(hw, NFP_NET_CFG_TXR_SZ(queue_idx), log2(nb_desc));
1160
1161         return 0;
1162 }
1163
1164 /* nfp_net_tx_cksum - Set TX CSUM offload flags in TX descriptor */
1165 static inline void
1166 nfp_net_tx_cksum(struct nfp_net_txq *txq, struct nfp_net_tx_desc *txd,
1167                  struct rte_mbuf *mb)
1168 {
1169         uint16_t ol_flags;
1170         struct nfp_net_hw *hw = txq->hw;
1171
1172         if (!(hw->cap & NFP_NET_CFG_CTRL_TXCSUM))
1173                 return;
1174
1175         ol_flags = mb->ol_flags;
1176
1177         /* IPv6 does not need checksum */
1178         if (ol_flags & PKT_TX_IP_CKSUM)
1179                 txd->flags |= PCIE_DESC_TX_IP4_CSUM;
1180
1181         switch (ol_flags & PKT_TX_L4_MASK) {
1182         case PKT_TX_UDP_CKSUM:
1183                 txd->flags |= PCIE_DESC_TX_UDP_CSUM;
1184                 break;
1185         case PKT_TX_TCP_CKSUM:
1186                 txd->flags |= PCIE_DESC_TX_TCP_CSUM;
1187                 break;
1188         }
1189
1190         txd->flags |= PCIE_DESC_TX_CSUM;
1191 }
1192
1193 /* nfp_net_rx_cksum - set mbuf checksum flags based on RX descriptor flags */
1194 static inline void
1195 nfp_net_rx_cksum(struct nfp_net_rxq *rxq, struct nfp_net_rx_desc *rxd,
1196                  struct rte_mbuf *mb)
1197 {
1198         struct nfp_net_hw *hw = rxq->hw;
1199
1200         if (!(hw->ctrl & NFP_NET_CFG_CTRL_RXCSUM))
1201                 return;
1202
1203         /* If IPv4 and IP checksum error, fail */
1204         if ((rxd->rxd.flags & PCIE_DESC_RX_IP4_CSUM) &&
1205             !(rxd->rxd.flags & PCIE_DESC_RX_IP4_CSUM_OK))
1206                 mb->ol_flags |= PKT_RX_IP_CKSUM_BAD;
1207
1208         /* If neither UDP nor TCP return */
1209         if (!(rxd->rxd.flags & PCIE_DESC_RX_TCP_CSUM) &&
1210             !(rxd->rxd.flags & PCIE_DESC_RX_UDP_CSUM))
1211                 return;
1212
1213         if ((rxd->rxd.flags & PCIE_DESC_RX_TCP_CSUM) &&
1214             !(rxd->rxd.flags & PCIE_DESC_RX_TCP_CSUM_OK))
1215                 mb->ol_flags |= PKT_RX_L4_CKSUM_BAD;
1216
1217         if ((rxd->rxd.flags & PCIE_DESC_RX_UDP_CSUM) &&
1218             !(rxd->rxd.flags & PCIE_DESC_RX_UDP_CSUM_OK))
1219                 mb->ol_flags |= PKT_RX_L4_CKSUM_BAD;
1220 }
1221
1222 #define NFP_HASH_OFFSET      ((uint8_t *)mbuf->buf_addr + mbuf->data_off - 4)
1223 #define NFP_HASH_TYPE_OFFSET ((uint8_t *)mbuf->buf_addr + mbuf->data_off - 8)
1224
1225 /*
1226  * nfp_net_set_hash - Set mbuf hash data
1227  *
1228  * The RSS hash and hash-type are pre-pended to the packet data.
1229  * Extract and decode it and set the mbuf fields.
1230  */
1231 static inline void
1232 nfp_net_set_hash(struct nfp_net_rxq *rxq, struct nfp_net_rx_desc *rxd,
1233                  struct rte_mbuf *mbuf)
1234 {
1235         uint32_t hash;
1236         uint32_t hash_type;
1237         struct nfp_net_hw *hw = rxq->hw;
1238
1239         if (!(hw->ctrl & NFP_NET_CFG_CTRL_RSS))
1240                 return;
1241
1242         if (!(rxd->rxd.flags & PCIE_DESC_RX_RSS))
1243                 return;
1244
1245         hash = rte_be_to_cpu_32(*(uint32_t *)NFP_HASH_OFFSET);
1246         hash_type = rte_be_to_cpu_32(*(uint32_t *)NFP_HASH_TYPE_OFFSET);
1247
1248         /*
1249          * hash type is sharing the same word with input port info
1250          * 31-8: input port
1251          * 7:0: hash type
1252          */
1253         hash_type &= 0xff;
1254         mbuf->hash.rss = hash;
1255         mbuf->ol_flags |= PKT_RX_RSS_HASH;
1256
1257         switch (hash_type) {
1258         case NFP_NET_RSS_IPV4:
1259                 mbuf->packet_type |= RTE_PTYPE_INNER_L3_IPV4;
1260                 break;
1261         case NFP_NET_RSS_IPV6:
1262                 mbuf->packet_type |= RTE_PTYPE_INNER_L3_IPV6;
1263                 break;
1264         case NFP_NET_RSS_IPV6_EX:
1265                 mbuf->packet_type |= RTE_PTYPE_INNER_L3_IPV6_EXT;
1266                 break;
1267         default:
1268                 mbuf->packet_type |= RTE_PTYPE_INNER_L4_MASK;
1269         }
1270 }
1271
1272 /* nfp_net_check_port - Set mbuf in_port field */
1273 static void
1274 nfp_net_check_port(struct nfp_net_rx_desc *rxd, struct rte_mbuf *mbuf)
1275 {
1276         uint32_t port;
1277
1278         if (!(rxd->rxd.flags & PCIE_DESC_RX_INGRESS_PORT)) {
1279                 mbuf->port = 0;
1280                 return;
1281         }
1282
1283         port = rte_be_to_cpu_32(*(uint32_t *)((uint8_t *)mbuf->buf_addr +
1284                                               mbuf->data_off - 8));
1285
1286         /*
1287          * hash type is sharing the same word with input port info
1288          * 31-8: input port
1289          * 7:0: hash type
1290          */
1291         port = (uint8_t)(port >> 8);
1292         mbuf->port = port;
1293 }
1294
1295 static inline void
1296 nfp_net_mbuf_alloc_failed(struct nfp_net_rxq *rxq)
1297 {
1298         rte_eth_devices[rxq->port_id].data->rx_mbuf_alloc_failed++;
1299 }
1300
1301 #define NFP_DESC_META_LEN(d) (d->rxd.meta_len_dd & PCIE_DESC_RX_META_LEN_MASK)
1302
1303 /*
1304  * RX path design:
1305  *
1306  * There are some decissions to take:
1307  * 1) How to check DD RX descriptors bit
1308  * 2) How and when to allocate new mbufs
1309  *
1310  * Current implementation checks just one single DD bit each loop. As each
1311  * descriptor is 8 bytes, it is likely a good idea to check descriptors in
1312  * a single cache line instead. Tests with this change have not shown any
1313  * performance improvement but it requires further investigation. For example,
1314  * depending on which descriptor is next, the number of descriptors could be
1315  * less than 8 for just checking those in the same cache line. This implies
1316  * extra work which could be counterproductive by itself. Indeed, last firmware
1317  * changes are just doing this: writing several descriptors with the DD bit
1318  * for saving PCIe bandwidth and DMA operations from the NFP.
1319  *
1320  * Mbuf allocation is done when a new packet is received. Then the descriptor
1321  * is automatically linked with the new mbuf and the old one is given to the
1322  * user. The main drawback with this design is mbuf allocation is heavier than
1323  * using bulk allocations allowed by DPDK with rte_mempool_get_bulk. From the
1324  * cache point of view it does not seem allocating the mbuf early on as we are
1325  * doing now have any benefit at all. Again, tests with this change have not
1326  * shown any improvement. Also, rte_mempool_get_bulk returns all or nothing
1327  * so looking at the implications of this type of allocation should be studied
1328  * deeply
1329  */
1330
1331 static uint16_t
1332 nfp_net_recv_pkts(void *rx_queue, struct rte_mbuf **rx_pkts, uint16_t nb_pkts)
1333 {
1334         struct nfp_net_rxq *rxq;
1335         struct nfp_net_rx_desc *rxds;
1336         struct nfp_net_rx_buff *rxb;
1337         struct nfp_net_hw *hw;
1338         struct rte_mbuf *mb;
1339         struct rte_mbuf *new_mb;
1340         int idx;
1341         uint16_t nb_hold;
1342         uint64_t dma_addr;
1343         int avail;
1344
1345         rxq = rx_queue;
1346         if (unlikely(rxq == NULL)) {
1347                 /*
1348                  * DPDK just checks the queue is lower than max queues
1349                  * enabled. But the queue needs to be configured
1350                  */
1351                 RTE_LOG(ERR, PMD, "RX Bad queue\n");
1352                 return -EINVAL;
1353         }
1354
1355         hw = rxq->hw;
1356         avail = 0;
1357         nb_hold = 0;
1358
1359         while (avail < nb_pkts) {
1360                 idx = rxq->rd_p % rxq->rx_count;
1361
1362                 rxb = &rxq->rxbufs[idx];
1363                 if (unlikely(rxb == NULL)) {
1364                         RTE_LOG(ERR, PMD, "rxb does not exist!\n");
1365                         break;
1366                 }
1367
1368                 /*
1369                  * Memory barrier to ensure that we won't do other
1370                  * reads before the DD bit.
1371                  */
1372                 rte_rmb();
1373
1374                 rxds = &rxq->rxds[idx];
1375                 if ((rxds->rxd.meta_len_dd & PCIE_DESC_RX_DD) == 0)
1376                         break;
1377
1378                 /*
1379                  * We got a packet. Let's alloc a new mbuff for refilling the
1380                  * free descriptor ring as soon as possible
1381                  */
1382                 new_mb = rte_pktmbuf_alloc(rxq->mem_pool);
1383                 if (unlikely(new_mb == NULL)) {
1384                         RTE_LOG(DEBUG, PMD, "RX mbuf alloc failed port_id=%u "
1385                                 "queue_id=%u\n", (unsigned)rxq->port_id,
1386                                 (unsigned)rxq->qidx);
1387                         nfp_net_mbuf_alloc_failed(rxq);
1388                         break;
1389                 }
1390
1391                 nb_hold++;
1392
1393                 /*
1394                  * Grab the mbuff and refill the descriptor with the
1395                  * previously allocated mbuff
1396                  */
1397                 mb = rxb->mbuf;
1398                 rxb->mbuf = new_mb;
1399
1400                 PMD_RX_LOG(DEBUG, "Packet len: %u, mbuf_size: %u\n",
1401                            rxds->rxd.data_len, rxq->mbuf_size);
1402
1403                 /* Size of this segment */
1404                 mb->data_len = rxds->rxd.data_len - NFP_DESC_META_LEN(rxds);
1405                 /* Size of the whole packet. We just support 1 segment */
1406                 mb->pkt_len = rxds->rxd.data_len - NFP_DESC_META_LEN(rxds);
1407
1408                 if (unlikely((mb->data_len + hw->rx_offset) >
1409                              rxq->mbuf_size)) {
1410                         /*
1411                          * This should not happen and the user has the
1412                          * responsibility of avoiding it. But we have
1413                          * to give some info about the error
1414                          */
1415                         RTE_LOG(ERR, PMD,
1416                                 "mbuf overflow likely due to the RX offset.\n"
1417                                 "\t\tYour mbuf size should have extra space for"
1418                                 " RX offset=%u bytes.\n"
1419                                 "\t\tCurrently you just have %u bytes available"
1420                                 " but the received packet is %u bytes long",
1421                                 hw->rx_offset,
1422                                 rxq->mbuf_size - hw->rx_offset,
1423                                 mb->data_len);
1424                         return -EINVAL;
1425                 }
1426
1427                 /* Filling the received mbuff with packet info */
1428                 if (hw->rx_offset)
1429                         mb->data_off = RTE_PKTMBUF_HEADROOM + hw->rx_offset;
1430                 else
1431                         mb->data_off = RTE_PKTMBUF_HEADROOM +
1432                                        NFP_DESC_META_LEN(rxds);
1433
1434                 /* No scatter mode supported */
1435                 mb->nb_segs = 1;
1436                 mb->next = NULL;
1437
1438                 /* Checking the RSS flag */
1439                 nfp_net_set_hash(rxq, rxds, mb);
1440
1441                 /* Checking the checksum flag */
1442                 nfp_net_rx_cksum(rxq, rxds, mb);
1443
1444                 /* Checking the port flag */
1445                 nfp_net_check_port(rxds, mb);
1446
1447                 if ((rxds->rxd.flags & PCIE_DESC_RX_VLAN) &&
1448                     (hw->ctrl & NFP_NET_CFG_CTRL_RXVLAN)) {
1449                         mb->vlan_tci = rte_cpu_to_le_32(rxds->rxd.vlan);
1450                         mb->ol_flags |= PKT_RX_VLAN_PKT;
1451                 }
1452
1453                 /* Adding the mbuff to the mbuff array passed by the app */
1454                 rx_pkts[avail++] = mb;
1455
1456                 /* Now resetting and updating the descriptor */
1457                 rxds->vals[0] = 0;
1458                 rxds->vals[1] = 0;
1459                 dma_addr = rte_cpu_to_le_64(RTE_MBUF_DMA_ADDR_DEFAULT(new_mb));
1460                 rxds->fld.dd = 0;
1461                 rxds->fld.dma_addr_hi = (dma_addr >> 32) & 0xff;
1462                 rxds->fld.dma_addr_lo = dma_addr & 0xffffffff;
1463
1464                 rxq->rd_p++;
1465         }
1466
1467         if (nb_hold == 0)
1468                 return nb_hold;
1469
1470         PMD_RX_LOG(DEBUG, "RX  port_id=%u queue_id=%u, %d packets received\n",
1471                    (unsigned)rxq->port_id, (unsigned)rxq->qidx, nb_hold);
1472
1473         nb_hold += rxq->nb_rx_hold;
1474
1475         /*
1476          * FL descriptors needs to be written before incrementing the
1477          * FL queue WR pointer
1478          */
1479         rte_wmb();
1480         if (nb_hold > rxq->rx_free_thresh) {
1481                 PMD_RX_LOG(DEBUG, "port=%u queue=%u nb_hold=%u avail=%u\n",
1482                            (unsigned)rxq->port_id, (unsigned)rxq->qidx,
1483                            (unsigned)nb_hold, (unsigned)avail);
1484                 nfp_qcp_ptr_add(rxq->qcp_fl, NFP_QCP_WRITE_PTR, nb_hold);
1485                 nb_hold = 0;
1486         }
1487         rxq->nb_rx_hold = nb_hold;
1488
1489         return avail;
1490 }
1491
1492 /*
1493  * nfp_net_tx_free_bufs - Check for descriptors with a complete
1494  * status
1495  * @txq: TX queue to work with
1496  * Returns number of descriptors freed
1497  */
1498 int
1499 nfp_net_tx_free_bufs(struct nfp_net_txq *txq)
1500 {
1501         uint32_t qcp_rd_p;
1502         int todo;
1503
1504         PMD_TX_LOG(DEBUG, "queue %u. Check for descriptor with a complete"
1505                    " status\n", txq->qidx);
1506
1507         /* Work out how many packets have been sent */
1508         qcp_rd_p = nfp_qcp_read(txq->qcp_q, NFP_QCP_READ_PTR);
1509
1510         if (qcp_rd_p == txq->qcp_rd_p) {
1511                 PMD_TX_LOG(DEBUG, "queue %u: It seems harrier is not sending "
1512                            "packets (%u, %u)\n", txq->qidx,
1513                            qcp_rd_p, txq->qcp_rd_p);
1514                 return 0;
1515         }
1516
1517         if (qcp_rd_p > txq->qcp_rd_p)
1518                 todo = qcp_rd_p - txq->qcp_rd_p;
1519         else
1520                 todo = qcp_rd_p + txq->tx_count - txq->qcp_rd_p;
1521
1522         PMD_TX_LOG(DEBUG, "qcp_rd_p %u, txq->qcp_rd_p: %u, qcp->rd_p: %u\n",
1523                    qcp_rd_p, txq->qcp_rd_p, txq->rd_p);
1524
1525         if (todo == 0)
1526                 return todo;
1527
1528         txq->qcp_rd_p += todo;
1529         txq->qcp_rd_p %= txq->tx_count;
1530         txq->rd_p += todo;
1531
1532         return todo;
1533 }
1534
1535 /* Leaving always free descriptors for avoiding wrapping confusion */
1536 #define NFP_FREE_TX_DESC(t) (t->tx_count - (t->wr_p - t->rd_p) - 8)
1537
1538 /*
1539  * nfp_net_txq_full - Check if the TX queue free descriptors
1540  * is below tx_free_threshold
1541  *
1542  * @txq: TX queue to check
1543  *
1544  * This function uses the host copy* of read/write pointers
1545  */
1546 static inline
1547 int nfp_net_txq_full(struct nfp_net_txq *txq)
1548 {
1549         return NFP_FREE_TX_DESC(txq) < txq->tx_free_thresh;
1550 }
1551
1552 static uint16_t
1553 nfp_net_xmit_pkts(void *tx_queue, struct rte_mbuf **tx_pkts, uint16_t nb_pkts)
1554 {
1555         struct nfp_net_txq *txq;
1556         struct nfp_net_hw *hw;
1557         struct nfp_net_tx_desc *txds;
1558         struct rte_mbuf *pkt;
1559         uint64_t dma_addr;
1560         int pkt_size, dma_size;
1561         uint16_t free_descs, issued_descs;
1562         struct rte_mbuf **lmbuf;
1563         int i;
1564
1565         txq = tx_queue;
1566         hw = txq->hw;
1567         txds = &txq->txds[txq->tail];
1568
1569         PMD_TX_LOG(DEBUG, "working for queue %u at pos %d and %u packets\n",
1570                    txq->qidx, txq->tail, nb_pkts);
1571
1572         if ((NFP_FREE_TX_DESC(txq) < nb_pkts) || (nfp_net_txq_full(txq)))
1573                 nfp_net_tx_free_bufs(txq);
1574
1575         free_descs = (uint16_t)NFP_FREE_TX_DESC(txq);
1576         if (unlikely(free_descs == 0))
1577                 return 0;
1578
1579         pkt = *tx_pkts;
1580
1581         i = 0;
1582         issued_descs = 0;
1583         PMD_TX_LOG(DEBUG, "queue: %u. Sending %u packets\n",
1584                    txq->qidx, nb_pkts);
1585         /* Sending packets */
1586         while ((i < nb_pkts) && free_descs) {
1587                 /* Grabbing the mbuf linked to the current descriptor */
1588                 lmbuf = &txq->txbufs[txq->tail].mbuf;
1589                 /* Warming the cache for releasing the mbuf later on */
1590                 RTE_MBUF_PREFETCH_TO_FREE(*lmbuf);
1591
1592                 pkt = *(tx_pkts + i);
1593
1594                 if (unlikely((pkt->nb_segs > 1) &&
1595                              !(hw->cap & NFP_NET_CFG_CTRL_GATHER))) {
1596                         PMD_INIT_LOG(INFO, "NFP_NET_CFG_CTRL_GATHER not set\n");
1597                         rte_panic("Multisegment packet unsupported\n");
1598                 }
1599
1600                 /* Checking if we have enough descriptors */
1601                 if (unlikely(pkt->nb_segs > free_descs))
1602                         goto xmit_end;
1603
1604                 /*
1605                  * Checksum and VLAN flags just in the first descriptor for a
1606                  * multisegment packet
1607                  */
1608                 nfp_net_tx_cksum(txq, txds, pkt);
1609
1610                 if ((pkt->ol_flags & PKT_TX_VLAN_PKT) &&
1611                     (hw->cap & NFP_NET_CFG_CTRL_TXVLAN)) {
1612                         txds->flags |= PCIE_DESC_TX_VLAN;
1613                         txds->vlan = pkt->vlan_tci;
1614                 }
1615
1616                 if (pkt->ol_flags & PKT_TX_TCP_SEG)
1617                         rte_panic("TSO is not supported\n");
1618
1619                 /*
1620                  * mbuf data_len is the data in one segment and pkt_len data
1621                  * in the whole packet. When the packet is just one segment,
1622                  * then data_len = pkt_len
1623                  */
1624                 pkt_size = pkt->pkt_len;
1625
1626                 while (pkt_size) {
1627                         /* Releasing mbuf which was prefetched above */
1628                         if (*lmbuf)
1629                                 rte_pktmbuf_free_seg(*lmbuf);
1630
1631                         dma_size = pkt->data_len;
1632                         dma_addr = RTE_MBUF_DATA_DMA_ADDR(pkt);
1633                         PMD_TX_LOG(DEBUG, "Working with mbuf at dma address:"
1634                                    "%" PRIx64 "\n", dma_addr);
1635
1636                         /* Filling descriptors fields */
1637                         txds->dma_len = dma_size;
1638                         txds->data_len = pkt->pkt_len;
1639                         txds->dma_addr_hi = (dma_addr >> 32) & 0xff;
1640                         txds->dma_addr_lo = (dma_addr & 0xffffffff);
1641                         ASSERT(free_descs > 0);
1642                         free_descs--;
1643
1644                         /*
1645                          * Linking mbuf with descriptor for being released
1646                          * next time descriptor is used
1647                          */
1648                         *lmbuf = pkt;
1649
1650                         txq->wr_p++;
1651                         txq->tail++;
1652                         if (unlikely(txq->tail == txq->tx_count)) /* wrapping?*/
1653                                 txq->tail = 0;
1654
1655                         pkt_size -= dma_size;
1656                         if (!pkt_size) {
1657                                 /* End of packet */
1658                                 txds->offset_eop |= PCIE_DESC_TX_EOP;
1659                         } else {
1660                                 txds->offset_eop &= PCIE_DESC_TX_OFFSET_MASK;
1661                                 pkt = pkt->next;
1662                         }
1663                         /* Referencing next free TX descriptor */
1664                         txds = &txq->txds[txq->tail];
1665                         issued_descs++;
1666                 }
1667                 i++;
1668         }
1669
1670 xmit_end:
1671         /* Increment write pointers. Force memory write before we let HW know */
1672         rte_wmb();
1673         nfp_qcp_ptr_add(txq->qcp_q, NFP_QCP_WRITE_PTR, issued_descs);
1674
1675         return i;
1676 }
1677
1678 /* Update Redirection Table(RETA) of Receive Side Scaling of Ethernet device */
1679 static int
1680 nfp_net_reta_update(struct rte_eth_dev *dev,
1681                     struct rte_eth_rss_reta_entry64 *reta_conf,
1682                     uint16_t reta_size)
1683 {
1684         uint32_t reta, mask;
1685         int i, j;
1686         int idx, shift;
1687         uint32_t update;
1688         struct nfp_net_hw *hw =
1689                 NFP_NET_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1690
1691         if (!(hw->ctrl & NFP_NET_CFG_CTRL_RSS))
1692                 return -EINVAL;
1693
1694         if (reta_size != NFP_NET_CFG_RSS_ITBL_SZ) {
1695                 RTE_LOG(ERR, PMD, "The size of hash lookup table configured "
1696                         "(%d) doesn't match the number hardware can supported "
1697                         "(%d)\n", reta_size, NFP_NET_CFG_RSS_ITBL_SZ);
1698                 return -EINVAL;
1699         }
1700
1701         /*
1702          * Update Redirection Table. There are 128 8bit-entries which can be
1703          * manage as 32 32bit-entries
1704          */
1705         for (i = 0; i < reta_size; i += 4) {
1706                 /* Handling 4 RSS entries per loop */
1707                 idx = i / RTE_RETA_GROUP_SIZE;
1708                 shift = i % RTE_RETA_GROUP_SIZE;
1709                 mask = (uint8_t)((reta_conf[idx].mask >> shift) & 0xF);
1710
1711                 if (!mask)
1712                         continue;
1713
1714                 reta = 0;
1715                 /* If all 4 entries were set, don't need read RETA register */
1716                 if (mask != 0xF)
1717                         reta = nn_cfg_readl(hw, NFP_NET_CFG_RSS_ITBL + i);
1718
1719                 for (j = 0; j < 4; j++) {
1720                         if (!(mask & (0x1 << j)))
1721                                 continue;
1722                         if (mask != 0xF)
1723                                 /* Clearing the entry bits */
1724                                 reta &= ~(0xFF << (8 * j));
1725                         reta |= reta_conf[idx].reta[shift + j] << (8 * j);
1726                 }
1727                 nn_cfg_writel(hw, NFP_NET_CFG_RSS_ITBL + shift, reta);
1728         }
1729
1730         update = NFP_NET_CFG_UPDATE_RSS;
1731
1732         if (nfp_net_reconfig(hw, hw->ctrl, update) < 0)
1733                 return -EIO;
1734
1735         return 0;
1736 }
1737
1738  /* Query Redirection Table(RETA) of Receive Side Scaling of Ethernet device. */
1739 static int
1740 nfp_net_reta_query(struct rte_eth_dev *dev,
1741                    struct rte_eth_rss_reta_entry64 *reta_conf,
1742                    uint16_t reta_size)
1743 {
1744         uint8_t i, j, mask;
1745         int idx, shift;
1746         uint32_t reta;
1747         struct nfp_net_hw *hw;
1748
1749         hw = NFP_NET_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1750
1751         if (!(hw->ctrl & NFP_NET_CFG_CTRL_RSS))
1752                 return -EINVAL;
1753
1754         if (reta_size != NFP_NET_CFG_RSS_ITBL_SZ) {
1755                 RTE_LOG(ERR, PMD, "The size of hash lookup table configured "
1756                         "(%d) doesn't match the number hardware can supported "
1757                         "(%d)\n", reta_size, NFP_NET_CFG_RSS_ITBL_SZ);
1758                 return -EINVAL;
1759         }
1760
1761         /*
1762          * Reading Redirection Table. There are 128 8bit-entries which can be
1763          * manage as 32 32bit-entries
1764          */
1765         for (i = 0; i < reta_size; i += 4) {
1766                 /* Handling 4 RSS entries per loop */
1767                 idx = i / RTE_RETA_GROUP_SIZE;
1768                 shift = i % RTE_RETA_GROUP_SIZE;
1769                 mask = (uint8_t)((reta_conf[idx].mask >> shift) & 0xF);
1770
1771                 if (!mask)
1772                         continue;
1773
1774                 reta = nn_cfg_readl(hw, NFP_NET_CFG_RSS_ITBL + shift);
1775                 for (j = 0; j < 4; j++) {
1776                         if (!(mask & (0x1 << j)))
1777                                 continue;
1778                         reta_conf->reta[shift + j] =
1779                                 (uint8_t)((reta >> (8 * j)) & 0xF);
1780                 }
1781         }
1782         return 0;
1783 }
1784
1785 static int
1786 nfp_net_rss_hash_update(struct rte_eth_dev *dev,
1787                         struct rte_eth_rss_conf *rss_conf)
1788 {
1789         uint32_t update;
1790         uint32_t cfg_rss_ctrl = 0;
1791         uint8_t key;
1792         uint64_t rss_hf;
1793         int i;
1794         struct nfp_net_hw *hw;
1795
1796         hw = NFP_NET_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1797
1798         rss_hf = rss_conf->rss_hf;
1799
1800         /* Checking if RSS is enabled */
1801         if (!(hw->ctrl & NFP_NET_CFG_CTRL_RSS)) {
1802                 if (rss_hf != 0) { /* Enable RSS? */
1803                         RTE_LOG(ERR, PMD, "RSS unsupported\n");
1804                         return -EINVAL;
1805                 }
1806                 return 0; /* Nothing to do */
1807         }
1808
1809         if (rss_conf->rss_key_len > NFP_NET_CFG_RSS_KEY_SZ) {
1810                 RTE_LOG(ERR, PMD, "hash key too long\n");
1811                 return -EINVAL;
1812         }
1813
1814         if (rss_hf & ETH_RSS_IPV4)
1815                 cfg_rss_ctrl |= NFP_NET_CFG_RSS_IPV4 |
1816                                 NFP_NET_CFG_RSS_IPV4_TCP |
1817                                 NFP_NET_CFG_RSS_IPV4_UDP;
1818
1819         if (rss_hf & ETH_RSS_IPV6)
1820                 cfg_rss_ctrl |= NFP_NET_CFG_RSS_IPV6 |
1821                                 NFP_NET_CFG_RSS_IPV6_TCP |
1822                                 NFP_NET_CFG_RSS_IPV6_UDP;
1823
1824         /* configuring where to apply the RSS hash */
1825         nn_cfg_writel(hw, NFP_NET_CFG_RSS_CTRL, cfg_rss_ctrl);
1826
1827         /* Writing the key byte a byte */
1828         for (i = 0; i < rss_conf->rss_key_len; i++) {
1829                 memcpy(&key, &rss_conf->rss_key[i], 1);
1830                 nn_cfg_writeb(hw, NFP_NET_CFG_RSS_KEY + i, key);
1831         }
1832
1833         /* Writing the key size */
1834         nn_cfg_writeb(hw, NFP_NET_CFG_RSS_KEY_SZ, rss_conf->rss_key_len);
1835
1836         update = NFP_NET_CFG_UPDATE_RSS;
1837
1838         if (nfp_net_reconfig(hw, hw->ctrl, update) < 0)
1839                 return -EIO;
1840
1841         return 0;
1842 }
1843
1844 static int
1845 nfp_net_rss_hash_conf_get(struct rte_eth_dev *dev,
1846                           struct rte_eth_rss_conf *rss_conf)
1847 {
1848         uint64_t rss_hf;
1849         uint32_t cfg_rss_ctrl;
1850         uint8_t key;
1851         int i;
1852         struct nfp_net_hw *hw;
1853
1854         hw = NFP_NET_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1855
1856         if (!(hw->ctrl & NFP_NET_CFG_CTRL_RSS))
1857                 return -EINVAL;
1858
1859         rss_hf = rss_conf->rss_hf;
1860         cfg_rss_ctrl = nn_cfg_readl(hw, NFP_NET_CFG_RSS_CTRL);
1861
1862         if (cfg_rss_ctrl & NFP_NET_CFG_RSS_IPV4)
1863                 rss_hf |= ETH_RSS_NONFRAG_IPV4_TCP | ETH_RSS_NONFRAG_IPV4_UDP;
1864
1865         if (cfg_rss_ctrl & NFP_NET_CFG_RSS_IPV4_TCP)
1866                 rss_hf |= ETH_RSS_NONFRAG_IPV4_TCP;
1867
1868         if (cfg_rss_ctrl & NFP_NET_CFG_RSS_IPV6_TCP)
1869                 rss_hf |= ETH_RSS_NONFRAG_IPV6_TCP;
1870
1871         if (cfg_rss_ctrl & NFP_NET_CFG_RSS_IPV4_UDP)
1872                 rss_hf |= ETH_RSS_NONFRAG_IPV4_UDP;
1873
1874         if (cfg_rss_ctrl & NFP_NET_CFG_RSS_IPV6_UDP)
1875                 rss_hf |= ETH_RSS_NONFRAG_IPV6_UDP;
1876
1877         if (cfg_rss_ctrl & NFP_NET_CFG_RSS_IPV6)
1878                 rss_hf |= ETH_RSS_NONFRAG_IPV4_UDP | ETH_RSS_NONFRAG_IPV6_UDP;
1879
1880         /* Reading the key size */
1881         rss_conf->rss_key_len = nn_cfg_readl(hw, NFP_NET_CFG_RSS_KEY_SZ);
1882
1883         /* Reading the key byte a byte */
1884         for (i = 0; i < rss_conf->rss_key_len; i++) {
1885                 key = nn_cfg_readb(hw, NFP_NET_CFG_RSS_KEY + i);
1886                 memcpy(&rss_conf->rss_key[i], &key, 1);
1887         }
1888
1889         return 0;
1890 }
1891
1892 /* Initialise and register driver with DPDK Application */
1893 static struct eth_dev_ops nfp_net_eth_dev_ops = {
1894         .dev_configure          = nfp_net_configure,
1895         .dev_start              = nfp_net_start,
1896         .dev_stop               = nfp_net_stop,
1897         .dev_close              = nfp_net_close,
1898         .stats_get              = nfp_net_stats_get,
1899         .stats_reset            = nfp_net_stats_reset,
1900         .reta_update            = nfp_net_reta_update,
1901         .reta_query             = nfp_net_reta_query,
1902         .rss_hash_update        = nfp_net_rss_hash_update,
1903         .rss_hash_conf_get      = nfp_net_rss_hash_conf_get,
1904         .rx_queue_setup         = nfp_net_rx_queue_setup,
1905         .rx_queue_release       = nfp_net_rx_queue_release,
1906         .rx_queue_count         = nfp_net_rx_queue_count,
1907         .tx_queue_setup         = nfp_net_tx_queue_setup,
1908         .tx_queue_release       = nfp_net_tx_queue_release,
1909 };
1910
1911 static int
1912 nfp_net_init(struct rte_eth_dev *eth_dev)
1913 {
1914         struct rte_pci_device *pci_dev;
1915         struct nfp_net_hw *hw;
1916
1917         uint32_t tx_bar_off, rx_bar_off;
1918         uint32_t start_q;
1919         int stride = 4;
1920
1921         PMD_INIT_FUNC_TRACE();
1922
1923         hw = NFP_NET_DEV_PRIVATE_TO_HW(eth_dev->data->dev_private);
1924
1925         eth_dev->dev_ops = &nfp_net_eth_dev_ops;
1926         eth_dev->rx_pkt_burst = &nfp_net_recv_pkts;
1927         eth_dev->tx_pkt_burst = &nfp_net_xmit_pkts;
1928
1929         /* For secondary processes, the primary has done all the work */
1930         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
1931                 return 0;
1932
1933         pci_dev = eth_dev->pci_dev;
1934         hw->device_id = pci_dev->id.device_id;
1935         hw->vendor_id = pci_dev->id.vendor_id;
1936         hw->subsystem_device_id = pci_dev->id.subsystem_device_id;
1937         hw->subsystem_vendor_id = pci_dev->id.subsystem_vendor_id;
1938
1939         PMD_INIT_LOG(DEBUG, "nfp_net: device (%u:%u) %u:%u:%u:%u\n",
1940                      pci_dev->id.vendor_id, pci_dev->id.device_id,
1941                      pci_dev->addr.domain, pci_dev->addr.bus,
1942                      pci_dev->addr.devid, pci_dev->addr.function);
1943
1944         hw->ctrl_bar = (uint8_t *)pci_dev->mem_resource[0].addr;
1945         if (hw->ctrl_bar == NULL) {
1946                 RTE_LOG(ERR, PMD,
1947                         "hw->ctrl_bar is NULL. BAR0 not configured\n");
1948                 return -ENODEV;
1949         }
1950         hw->max_rx_queues = nn_cfg_readl(hw, NFP_NET_CFG_MAX_RXRINGS);
1951         hw->max_tx_queues = nn_cfg_readl(hw, NFP_NET_CFG_MAX_TXRINGS);
1952
1953         /* Work out where in the BAR the queues start. */
1954         switch (pci_dev->id.device_id) {
1955         case PCI_DEVICE_ID_NFP6000_VF_NIC:
1956                 start_q = nn_cfg_readl(hw, NFP_NET_CFG_START_TXQ);
1957                 tx_bar_off = NFP_PCIE_QUEUE(start_q);
1958                 start_q = nn_cfg_readl(hw, NFP_NET_CFG_START_RXQ);
1959                 rx_bar_off = NFP_PCIE_QUEUE(start_q);
1960                 break;
1961         default:
1962                 RTE_LOG(ERR, PMD, "nfp_net: no device ID matching\n");
1963                 return -ENODEV;
1964         }
1965
1966         PMD_INIT_LOG(DEBUG, "tx_bar_off: 0x%08x\n", tx_bar_off);
1967         PMD_INIT_LOG(DEBUG, "rx_bar_off: 0x%08x\n", rx_bar_off);
1968
1969         hw->tx_bar = (uint8_t *)pci_dev->mem_resource[2].addr + tx_bar_off;
1970         hw->rx_bar = (uint8_t *)pci_dev->mem_resource[2].addr + rx_bar_off;
1971
1972         PMD_INIT_LOG(DEBUG, "ctrl_bar: %p, tx_bar: %p, rx_bar: %p\n",
1973                      hw->ctrl_bar, hw->tx_bar, hw->rx_bar);
1974
1975         nfp_net_cfg_queue_setup(hw);
1976
1977         /* Get some of the read-only fields from the config BAR */
1978         hw->ver = nn_cfg_readl(hw, NFP_NET_CFG_VERSION);
1979         hw->cap = nn_cfg_readl(hw, NFP_NET_CFG_CAP);
1980         hw->max_mtu = nn_cfg_readl(hw, NFP_NET_CFG_MAX_MTU);
1981         hw->mtu = hw->max_mtu;
1982
1983         if (NFD_CFG_MAJOR_VERSION_of(hw->ver) < 2)
1984                 hw->rx_offset = NFP_NET_RX_OFFSET;
1985         else
1986                 hw->rx_offset = nn_cfg_readl(hw, NFP_NET_CFG_RX_OFFSET_ADDR);
1987
1988         PMD_INIT_LOG(INFO, "VER: %#x, Maximum supported MTU: %d\n",
1989                      hw->ver, hw->max_mtu);
1990         PMD_INIT_LOG(INFO, "CAP: %#x, %s%s%s%s%s%s%s%s%s\n", hw->cap,
1991                      hw->cap & NFP_NET_CFG_CTRL_PROMISC ? "PROMISC " : "",
1992                      hw->cap & NFP_NET_CFG_CTRL_RXCSUM  ? "RXCSUM "  : "",
1993                      hw->cap & NFP_NET_CFG_CTRL_TXCSUM  ? "TXCSUM "  : "",
1994                      hw->cap & NFP_NET_CFG_CTRL_RXVLAN  ? "RXVLAN "  : "",
1995                      hw->cap & NFP_NET_CFG_CTRL_TXVLAN  ? "TXVLAN "  : "",
1996                      hw->cap & NFP_NET_CFG_CTRL_SCATTER ? "SCATTER " : "",
1997                      hw->cap & NFP_NET_CFG_CTRL_GATHER  ? "GATHER "  : "",
1998                      hw->cap & NFP_NET_CFG_CTRL_LSO     ? "TSO "     : "",
1999                      hw->cap & NFP_NET_CFG_CTRL_RSS     ? "RSS "     : "");
2000
2001         pci_dev = eth_dev->pci_dev;
2002         hw->ctrl = 0;
2003
2004         hw->stride_rx = stride;
2005         hw->stride_tx = stride;
2006
2007         PMD_INIT_LOG(INFO, "max_rx_queues: %u, max_tx_queues: %u\n",
2008                      hw->max_rx_queues, hw->max_tx_queues);
2009
2010         /* Allocating memory for mac addr */
2011         eth_dev->data->mac_addrs = rte_zmalloc("mac_addr", ETHER_ADDR_LEN, 0);
2012         if (eth_dev->data->mac_addrs == NULL) {
2013                 PMD_INIT_LOG(ERR, "Failed to space for MAC address");
2014                 return -ENOMEM;
2015         }
2016
2017         /* Using random mac addresses for VFs */
2018         eth_random_addr(&hw->mac_addr[0]);
2019
2020         /* Copying mac address to DPDK eth_dev struct */
2021         ether_addr_copy(&eth_dev->data->mac_addrs[0],
2022                         (struct ether_addr *)hw->mac_addr);
2023
2024         PMD_INIT_LOG(INFO, "port %d VendorID=0x%x DeviceID=0x%x "
2025                      "mac=%02x:%02x:%02x:%02x:%02x:%02x",
2026                      eth_dev->data->port_id, pci_dev->id.vendor_id,
2027                      pci_dev->id.device_id,
2028                      hw->mac_addr[0], hw->mac_addr[1], hw->mac_addr[2],
2029                      hw->mac_addr[3], hw->mac_addr[4], hw->mac_addr[5]);
2030
2031         /* Recording current stats counters values */
2032         nfp_net_stats_reset(eth_dev);
2033
2034         return 0;
2035 }
2036
2037 static struct rte_pci_id pci_id_nfp_net_map[] = {
2038         {
2039                 .vendor_id = PCI_VENDOR_ID_NETRONOME,
2040                 .device_id = PCI_DEVICE_ID_NFP6000_PF_NIC,
2041                 .subsystem_vendor_id = PCI_ANY_ID,
2042                 .subsystem_device_id = PCI_ANY_ID,
2043         },
2044         {
2045                 .vendor_id = PCI_VENDOR_ID_NETRONOME,
2046                 .device_id = PCI_DEVICE_ID_NFP6000_VF_NIC,
2047                 .subsystem_vendor_id = PCI_ANY_ID,
2048                 .subsystem_device_id = PCI_ANY_ID,
2049         },
2050         {
2051                 .vendor_id = 0,
2052         },
2053 };
2054
2055 static struct eth_driver rte_nfp_net_pmd = {
2056         {
2057                 .name = "rte_nfp_net_pmd",
2058                 .id_table = pci_id_nfp_net_map,
2059                 .drv_flags = RTE_PCI_DRV_NEED_MAPPING | RTE_PCI_DRV_INTR_LSC,
2060         },
2061         .eth_dev_init = nfp_net_init,
2062         .dev_private_size = sizeof(struct nfp_net_adapter),
2063 };
2064
2065 static int
2066 nfp_net_pmd_init(const char *name __rte_unused,
2067                  const char *params __rte_unused)
2068 {
2069         PMD_INIT_FUNC_TRACE();
2070         PMD_INIT_LOG(INFO, "librte_pmd_nfp_net version %s\n",
2071                      NFP_NET_PMD_VERSION);
2072
2073         rte_eth_driver_register(&rte_nfp_net_pmd);
2074         return 0;
2075 }
2076
2077 static struct rte_driver rte_nfp_net_driver = {
2078         .type = PMD_PDEV,
2079         .init = nfp_net_pmd_init,
2080 };
2081
2082 PMD_REGISTER_DRIVER(rte_nfp_net_driver);
2083
2084 /*
2085  * Local variables:
2086  * c-file-style: "Linux"
2087  * indent-tabs-mode: t
2088  * End:
2089  */