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