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