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