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