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