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