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