net/axgbe: support scattered Rx
[dpdk.git] / drivers / net / axgbe / axgbe_ethdev.c
1 /*   SPDX-License-Identifier: BSD-3-Clause
2  *   Copyright(c) 2018 Advanced Micro Devices, Inc. All rights reserved.
3  *   Copyright(c) 2018 Synopsys, Inc. All rights reserved.
4  */
5
6 #include "axgbe_rxtx.h"
7 #include "axgbe_ethdev.h"
8 #include "axgbe_common.h"
9 #include "axgbe_phy.h"
10 #include "axgbe_regs.h"
11
12 static int eth_axgbe_dev_init(struct rte_eth_dev *eth_dev);
13 static int eth_axgbe_dev_uninit(struct rte_eth_dev *eth_dev);
14 static int  axgbe_dev_configure(struct rte_eth_dev *dev);
15 static int  axgbe_dev_start(struct rte_eth_dev *dev);
16 static void axgbe_dev_stop(struct rte_eth_dev *dev);
17 static void axgbe_dev_interrupt_handler(void *param);
18 static void axgbe_dev_close(struct rte_eth_dev *dev);
19 static int axgbe_dev_promiscuous_enable(struct rte_eth_dev *dev);
20 static int axgbe_dev_promiscuous_disable(struct rte_eth_dev *dev);
21 static int axgbe_dev_allmulticast_enable(struct rte_eth_dev *dev);
22 static int axgbe_dev_allmulticast_disable(struct rte_eth_dev *dev);
23 static int axgbe_dev_link_update(struct rte_eth_dev *dev,
24                                  int wait_to_complete);
25 static int axgbe_dev_get_regs(struct rte_eth_dev *dev,
26                               struct rte_dev_reg_info *regs);
27 static int axgbe_dev_stats_get(struct rte_eth_dev *dev,
28                                 struct rte_eth_stats *stats);
29 static int axgbe_dev_stats_reset(struct rte_eth_dev *dev);
30 static int axgbe_dev_xstats_get(struct rte_eth_dev *dev,
31                                 struct rte_eth_xstat *stats,
32                                 unsigned int n);
33 static int
34 axgbe_dev_xstats_get_names(struct rte_eth_dev *dev,
35                            struct rte_eth_xstat_name *xstats_names,
36                            unsigned int size);
37 static int
38 axgbe_dev_xstats_get_by_id(struct rte_eth_dev *dev,
39                            const uint64_t *ids,
40                            uint64_t *values,
41                            unsigned int n);
42 static int
43 axgbe_dev_xstats_get_names_by_id(struct rte_eth_dev *dev,
44                                  struct rte_eth_xstat_name *xstats_names,
45                                  const uint64_t *ids,
46                                  unsigned int size);
47 static int axgbe_dev_xstats_reset(struct rte_eth_dev *dev);
48 static int  axgbe_dev_info_get(struct rte_eth_dev *dev,
49                                struct rte_eth_dev_info *dev_info);
50
51 struct axgbe_xstats {
52         char name[RTE_ETH_XSTATS_NAME_SIZE];
53         int offset;
54 };
55
56 #define AXGMAC_MMC_STAT(_string, _var)                           \
57         { _string,                                              \
58           offsetof(struct axgbe_mmc_stats, _var),       \
59         }
60
61 static const struct axgbe_xstats axgbe_xstats_strings[] = {
62         AXGMAC_MMC_STAT("tx_bytes", txoctetcount_gb),
63         AXGMAC_MMC_STAT("tx_packets", txframecount_gb),
64         AXGMAC_MMC_STAT("tx_unicast_packets", txunicastframes_gb),
65         AXGMAC_MMC_STAT("tx_broadcast_packets", txbroadcastframes_gb),
66         AXGMAC_MMC_STAT("tx_multicast_packets", txmulticastframes_gb),
67         AXGMAC_MMC_STAT("tx_vlan_packets", txvlanframes_g),
68         AXGMAC_MMC_STAT("tx_64_byte_packets", tx64octets_gb),
69         AXGMAC_MMC_STAT("tx_65_to_127_byte_packets", tx65to127octets_gb),
70         AXGMAC_MMC_STAT("tx_128_to_255_byte_packets", tx128to255octets_gb),
71         AXGMAC_MMC_STAT("tx_256_to_511_byte_packets", tx256to511octets_gb),
72         AXGMAC_MMC_STAT("tx_512_to_1023_byte_packets", tx512to1023octets_gb),
73         AXGMAC_MMC_STAT("tx_1024_to_max_byte_packets", tx1024tomaxoctets_gb),
74         AXGMAC_MMC_STAT("tx_underflow_errors", txunderflowerror),
75         AXGMAC_MMC_STAT("tx_pause_frames", txpauseframes),
76
77         AXGMAC_MMC_STAT("rx_bytes", rxoctetcount_gb),
78         AXGMAC_MMC_STAT("rx_packets", rxframecount_gb),
79         AXGMAC_MMC_STAT("rx_unicast_packets", rxunicastframes_g),
80         AXGMAC_MMC_STAT("rx_broadcast_packets", rxbroadcastframes_g),
81         AXGMAC_MMC_STAT("rx_multicast_packets", rxmulticastframes_g),
82         AXGMAC_MMC_STAT("rx_vlan_packets", rxvlanframes_gb),
83         AXGMAC_MMC_STAT("rx_64_byte_packets", rx64octets_gb),
84         AXGMAC_MMC_STAT("rx_65_to_127_byte_packets", rx65to127octets_gb),
85         AXGMAC_MMC_STAT("rx_128_to_255_byte_packets", rx128to255octets_gb),
86         AXGMAC_MMC_STAT("rx_256_to_511_byte_packets", rx256to511octets_gb),
87         AXGMAC_MMC_STAT("rx_512_to_1023_byte_packets", rx512to1023octets_gb),
88         AXGMAC_MMC_STAT("rx_1024_to_max_byte_packets", rx1024tomaxoctets_gb),
89         AXGMAC_MMC_STAT("rx_undersize_packets", rxundersize_g),
90         AXGMAC_MMC_STAT("rx_oversize_packets", rxoversize_g),
91         AXGMAC_MMC_STAT("rx_crc_errors", rxcrcerror),
92         AXGMAC_MMC_STAT("rx_crc_errors_small_packets", rxrunterror),
93         AXGMAC_MMC_STAT("rx_crc_errors_giant_packets", rxjabbererror),
94         AXGMAC_MMC_STAT("rx_length_errors", rxlengtherror),
95         AXGMAC_MMC_STAT("rx_out_of_range_errors", rxoutofrangetype),
96         AXGMAC_MMC_STAT("rx_fifo_overflow_errors", rxfifooverflow),
97         AXGMAC_MMC_STAT("rx_watchdog_errors", rxwatchdogerror),
98         AXGMAC_MMC_STAT("rx_pause_frames", rxpauseframes),
99 };
100
101 #define AXGBE_XSTATS_COUNT        ARRAY_SIZE(axgbe_xstats_strings)
102
103 /* The set of PCI devices this driver supports */
104 #define AMD_PCI_VENDOR_ID       0x1022
105 #define AMD_PCI_RV_ROOT_COMPLEX_ID      0x15d0
106 #define AMD_PCI_AXGBE_DEVICE_V2A 0x1458
107 #define AMD_PCI_AXGBE_DEVICE_V2B 0x1459
108
109 int axgbe_logtype_init;
110 int axgbe_logtype_driver;
111
112 static const struct rte_pci_id pci_id_axgbe_map[] = {
113         {RTE_PCI_DEVICE(AMD_PCI_VENDOR_ID, AMD_PCI_AXGBE_DEVICE_V2A)},
114         {RTE_PCI_DEVICE(AMD_PCI_VENDOR_ID, AMD_PCI_AXGBE_DEVICE_V2B)},
115         { .vendor_id = 0, },
116 };
117
118 static struct axgbe_version_data axgbe_v2a = {
119         .init_function_ptrs_phy_impl    = axgbe_init_function_ptrs_phy_v2,
120         .xpcs_access                    = AXGBE_XPCS_ACCESS_V2,
121         .mmc_64bit                      = 1,
122         .tx_max_fifo_size               = 229376,
123         .rx_max_fifo_size               = 229376,
124         .tx_tstamp_workaround           = 1,
125         .ecc_support                    = 1,
126         .i2c_support                    = 1,
127         .an_cdr_workaround              = 1,
128 };
129
130 static struct axgbe_version_data axgbe_v2b = {
131         .init_function_ptrs_phy_impl    = axgbe_init_function_ptrs_phy_v2,
132         .xpcs_access                    = AXGBE_XPCS_ACCESS_V2,
133         .mmc_64bit                      = 1,
134         .tx_max_fifo_size               = 65536,
135         .rx_max_fifo_size               = 65536,
136         .tx_tstamp_workaround           = 1,
137         .ecc_support                    = 1,
138         .i2c_support                    = 1,
139         .an_cdr_workaround              = 1,
140 };
141
142 static const struct rte_eth_desc_lim rx_desc_lim = {
143         .nb_max = AXGBE_MAX_RING_DESC,
144         .nb_min = AXGBE_MIN_RING_DESC,
145         .nb_align = 8,
146 };
147
148 static const struct rte_eth_desc_lim tx_desc_lim = {
149         .nb_max = AXGBE_MAX_RING_DESC,
150         .nb_min = AXGBE_MIN_RING_DESC,
151         .nb_align = 8,
152 };
153
154 static const struct eth_dev_ops axgbe_eth_dev_ops = {
155         .dev_configure        = axgbe_dev_configure,
156         .dev_start            = axgbe_dev_start,
157         .dev_stop             = axgbe_dev_stop,
158         .dev_close            = axgbe_dev_close,
159         .promiscuous_enable   = axgbe_dev_promiscuous_enable,
160         .promiscuous_disable  = axgbe_dev_promiscuous_disable,
161         .allmulticast_enable  = axgbe_dev_allmulticast_enable,
162         .allmulticast_disable = axgbe_dev_allmulticast_disable,
163         .link_update          = axgbe_dev_link_update,
164         .get_reg              = axgbe_dev_get_regs,
165         .stats_get            = axgbe_dev_stats_get,
166         .stats_reset          = axgbe_dev_stats_reset,
167         .xstats_get           = axgbe_dev_xstats_get,
168         .xstats_reset         = axgbe_dev_xstats_reset,
169         .xstats_get_names     = axgbe_dev_xstats_get_names,
170         .xstats_get_names_by_id = axgbe_dev_xstats_get_names_by_id,
171         .xstats_get_by_id     = axgbe_dev_xstats_get_by_id,
172         .dev_infos_get        = axgbe_dev_info_get,
173         .rx_queue_setup       = axgbe_dev_rx_queue_setup,
174         .rx_queue_release     = axgbe_dev_rx_queue_release,
175         .tx_queue_setup       = axgbe_dev_tx_queue_setup,
176         .tx_queue_release     = axgbe_dev_tx_queue_release,
177 };
178
179 static int axgbe_phy_reset(struct axgbe_port *pdata)
180 {
181         pdata->phy_link = -1;
182         pdata->phy_speed = SPEED_UNKNOWN;
183         return pdata->phy_if.phy_reset(pdata);
184 }
185
186 /*
187  * Interrupt handler triggered by NIC  for handling
188  * specific interrupt.
189  *
190  * @param handle
191  *  Pointer to interrupt handle.
192  * @param param
193  *  The address of parameter (struct rte_eth_dev *) regsitered before.
194  *
195  * @return
196  *  void
197  */
198 static void
199 axgbe_dev_interrupt_handler(void *param)
200 {
201         struct rte_eth_dev *dev = (struct rte_eth_dev *)param;
202         struct axgbe_port *pdata = dev->data->dev_private;
203         unsigned int dma_isr, dma_ch_isr;
204
205         pdata->phy_if.an_isr(pdata);
206         /*DMA related interrupts*/
207         dma_isr = AXGMAC_IOREAD(pdata, DMA_ISR);
208         PMD_DRV_LOG(DEBUG, "DMA_ISR=%#010x\n", dma_isr);
209         if (dma_isr) {
210                 if (dma_isr & 1) {
211                         dma_ch_isr =
212                                 AXGMAC_DMA_IOREAD((struct axgbe_rx_queue *)
213                                                   pdata->rx_queues[0],
214                                                   DMA_CH_SR);
215                         PMD_DRV_LOG(DEBUG, "DMA_CH0_ISR=%#010x\n", dma_ch_isr);
216                         AXGMAC_DMA_IOWRITE((struct axgbe_rx_queue *)
217                                            pdata->rx_queues[0],
218                                            DMA_CH_SR, dma_ch_isr);
219                 }
220         }
221         /* Unmask interrupts since disabled after generation */
222         rte_intr_ack(&pdata->pci_dev->intr_handle);
223 }
224
225 /*
226  * Configure device link speed and setup link.
227  * It returns 0 on success.
228  */
229 static int
230 axgbe_dev_configure(struct rte_eth_dev *dev)
231 {
232         struct axgbe_port *pdata =  dev->data->dev_private;
233         /* Checksum offload to hardware */
234         pdata->rx_csum_enable = dev->data->dev_conf.rxmode.offloads &
235                                 DEV_RX_OFFLOAD_CHECKSUM;
236         return 0;
237 }
238
239 static int
240 axgbe_dev_rx_mq_config(struct rte_eth_dev *dev)
241 {
242         struct axgbe_port *pdata = dev->data->dev_private;
243
244         if (dev->data->dev_conf.rxmode.mq_mode == ETH_MQ_RX_RSS)
245                 pdata->rss_enable = 1;
246         else if (dev->data->dev_conf.rxmode.mq_mode == ETH_MQ_RX_NONE)
247                 pdata->rss_enable = 0;
248         else
249                 return  -1;
250         return 0;
251 }
252
253 static int
254 axgbe_dev_start(struct rte_eth_dev *dev)
255 {
256         struct axgbe_port *pdata = dev->data->dev_private;
257         int ret;
258         struct rte_eth_dev_data *dev_data = dev->data;
259         uint16_t max_pkt_len = dev_data->dev_conf.rxmode.max_rx_pkt_len;
260
261         dev->dev_ops = &axgbe_eth_dev_ops;
262
263         PMD_INIT_FUNC_TRACE();
264
265         /* Multiqueue RSS */
266         ret = axgbe_dev_rx_mq_config(dev);
267         if (ret) {
268                 PMD_DRV_LOG(ERR, "Unable to config RX MQ\n");
269                 return ret;
270         }
271         ret = axgbe_phy_reset(pdata);
272         if (ret) {
273                 PMD_DRV_LOG(ERR, "phy reset failed\n");
274                 return ret;
275         }
276         ret = pdata->hw_if.init(pdata);
277         if (ret) {
278                 PMD_DRV_LOG(ERR, "dev_init failed\n");
279                 return ret;
280         }
281
282         /* enable uio/vfio intr/eventfd mapping */
283         rte_intr_enable(&pdata->pci_dev->intr_handle);
284
285         /* phy start*/
286         pdata->phy_if.phy_start(pdata);
287         axgbe_dev_enable_tx(dev);
288         axgbe_dev_enable_rx(dev);
289
290         axgbe_clear_bit(AXGBE_STOPPED, &pdata->dev_state);
291         axgbe_clear_bit(AXGBE_DOWN, &pdata->dev_state);
292         if ((dev_data->dev_conf.rxmode.offloads & DEV_RX_OFFLOAD_SCATTER) ||
293                                 max_pkt_len > pdata->rx_buf_size)
294                 dev_data->scattered_rx = 1;
295
296         /*  Scatter Rx handling */
297         if (dev_data->scattered_rx)
298                 dev->rx_pkt_burst = &eth_axgbe_recv_scattered_pkts;
299         else
300                 dev->rx_pkt_burst = &axgbe_recv_pkts;
301
302         return 0;
303 }
304
305 /* Stop device: disable rx and tx functions to allow for reconfiguring. */
306 static void
307 axgbe_dev_stop(struct rte_eth_dev *dev)
308 {
309         struct axgbe_port *pdata = dev->data->dev_private;
310
311         PMD_INIT_FUNC_TRACE();
312
313         rte_intr_disable(&pdata->pci_dev->intr_handle);
314
315         if (axgbe_test_bit(AXGBE_STOPPED, &pdata->dev_state))
316                 return;
317
318         axgbe_set_bit(AXGBE_STOPPED, &pdata->dev_state);
319         axgbe_dev_disable_tx(dev);
320         axgbe_dev_disable_rx(dev);
321
322         pdata->phy_if.phy_stop(pdata);
323         pdata->hw_if.exit(pdata);
324         memset(&dev->data->dev_link, 0, sizeof(struct rte_eth_link));
325         axgbe_set_bit(AXGBE_DOWN, &pdata->dev_state);
326 }
327
328 /* Clear all resources like TX/RX queues. */
329 static void
330 axgbe_dev_close(struct rte_eth_dev *dev)
331 {
332         axgbe_dev_clear_queues(dev);
333 }
334
335 static int
336 axgbe_dev_promiscuous_enable(struct rte_eth_dev *dev)
337 {
338         struct axgbe_port *pdata = dev->data->dev_private;
339
340         PMD_INIT_FUNC_TRACE();
341
342         AXGMAC_IOWRITE_BITS(pdata, MAC_PFR, PR, 1);
343
344         return 0;
345 }
346
347 static int
348 axgbe_dev_promiscuous_disable(struct rte_eth_dev *dev)
349 {
350         struct axgbe_port *pdata = dev->data->dev_private;
351
352         PMD_INIT_FUNC_TRACE();
353
354         AXGMAC_IOWRITE_BITS(pdata, MAC_PFR, PR, 0);
355
356         return 0;
357 }
358
359 static int
360 axgbe_dev_allmulticast_enable(struct rte_eth_dev *dev)
361 {
362         struct axgbe_port *pdata = dev->data->dev_private;
363
364         PMD_INIT_FUNC_TRACE();
365
366         if (AXGMAC_IOREAD_BITS(pdata, MAC_PFR, PM))
367                 return 0;
368         AXGMAC_IOWRITE_BITS(pdata, MAC_PFR, PM, 1);
369
370         return 0;
371 }
372
373 static int
374 axgbe_dev_allmulticast_disable(struct rte_eth_dev *dev)
375 {
376         struct axgbe_port *pdata = dev->data->dev_private;
377
378         PMD_INIT_FUNC_TRACE();
379
380         if (!AXGMAC_IOREAD_BITS(pdata, MAC_PFR, PM))
381                 return 0;
382         AXGMAC_IOWRITE_BITS(pdata, MAC_PFR, PM, 0);
383
384         return 0;
385 }
386
387 /* return 0 means link status changed, -1 means not changed */
388 static int
389 axgbe_dev_link_update(struct rte_eth_dev *dev,
390                       int wait_to_complete __rte_unused)
391 {
392         struct axgbe_port *pdata = dev->data->dev_private;
393         struct rte_eth_link link;
394         int ret = 0;
395
396         PMD_INIT_FUNC_TRACE();
397         rte_delay_ms(800);
398
399         pdata->phy_if.phy_status(pdata);
400
401         memset(&link, 0, sizeof(struct rte_eth_link));
402         link.link_duplex = pdata->phy.duplex;
403         link.link_status = pdata->phy_link;
404         link.link_speed = pdata->phy_speed;
405         link.link_autoneg = !(dev->data->dev_conf.link_speeds &
406                               ETH_LINK_SPEED_FIXED);
407         ret = rte_eth_linkstatus_set(dev, &link);
408         if (ret == -1)
409                 PMD_DRV_LOG(ERR, "No change in link status\n");
410
411         return ret;
412 }
413
414 static int
415 axgbe_dev_get_regs(struct rte_eth_dev *dev, struct rte_dev_reg_info *regs)
416 {
417         struct axgbe_port *pdata = dev->data->dev_private;
418
419         if (regs->data == NULL) {
420                 regs->length = axgbe_regs_get_count(pdata);
421                 regs->width = sizeof(uint32_t);
422                 return 0;
423         }
424
425         /* Only full register dump is supported */
426         if (regs->length &&
427             regs->length != (uint32_t)axgbe_regs_get_count(pdata))
428                 return -ENOTSUP;
429
430         regs->version = pdata->pci_dev->id.vendor_id << 16 |
431                         pdata->pci_dev->id.device_id;
432         axgbe_regs_dump(pdata, regs->data);
433         return 0;
434 }
435 static void axgbe_read_mmc_stats(struct axgbe_port *pdata)
436 {
437         struct axgbe_mmc_stats *stats = &pdata->mmc_stats;
438
439         /* Freeze counters */
440         AXGMAC_IOWRITE_BITS(pdata, MMC_CR, MCF, 1);
441
442         /* Tx counters */
443         stats->txoctetcount_gb +=
444                 AXGMAC_IOREAD(pdata, MMC_TXOCTETCOUNT_GB_LO);
445         stats->txoctetcount_gb +=
446         ((uint64_t)AXGMAC_IOREAD(pdata, MMC_TXOCTETCOUNT_GB_HI) << 32);
447
448         stats->txframecount_gb +=
449                 AXGMAC_IOREAD(pdata, MMC_TXFRAMECOUNT_GB_LO);
450         stats->txframecount_gb +=
451         ((uint64_t)AXGMAC_IOREAD(pdata, MMC_TXFRAMECOUNT_GB_HI) << 32);
452
453         stats->txbroadcastframes_g +=
454                 AXGMAC_IOREAD(pdata, MMC_TXBROADCASTFRAMES_G_LO);
455         stats->txbroadcastframes_g +=
456         ((uint64_t)AXGMAC_IOREAD(pdata, MMC_TXBROADCASTFRAMES_G_HI) << 32);
457
458         stats->txmulticastframes_g +=
459                 AXGMAC_IOREAD(pdata, MMC_TXMULTICASTFRAMES_G_LO);
460         stats->txmulticastframes_g +=
461         ((uint64_t)AXGMAC_IOREAD(pdata, MMC_TXMULTICASTFRAMES_G_HI) << 32);
462
463         stats->tx64octets_gb +=
464                 AXGMAC_IOREAD(pdata, MMC_TX64OCTETS_GB_LO);
465         stats->tx64octets_gb +=
466         ((uint64_t)AXGMAC_IOREAD(pdata, MMC_TX64OCTETS_GB_HI) << 32);
467
468         stats->tx65to127octets_gb +=
469                 AXGMAC_IOREAD(pdata, MMC_TX65TO127OCTETS_GB_LO);
470         stats->tx65to127octets_gb +=
471         ((uint64_t)AXGMAC_IOREAD(pdata, MMC_TX65TO127OCTETS_GB_HI) << 32);
472
473         stats->tx128to255octets_gb +=
474                 AXGMAC_IOREAD(pdata, MMC_TX128TO255OCTETS_GB_LO);
475         stats->tx128to255octets_gb +=
476         ((uint64_t)AXGMAC_IOREAD(pdata, MMC_TX128TO255OCTETS_GB_HI) << 32);
477
478         stats->tx256to511octets_gb +=
479                 AXGMAC_IOREAD(pdata, MMC_TX256TO511OCTETS_GB_LO);
480         stats->tx256to511octets_gb +=
481         ((uint64_t)AXGMAC_IOREAD(pdata, MMC_TX256TO511OCTETS_GB_HI) << 32);
482
483         stats->tx512to1023octets_gb +=
484                 AXGMAC_IOREAD(pdata, MMC_TX512TO1023OCTETS_GB_LO);
485         stats->tx512to1023octets_gb +=
486         ((uint64_t)AXGMAC_IOREAD(pdata, MMC_TX512TO1023OCTETS_GB_HI) << 32);
487
488         stats->tx1024tomaxoctets_gb +=
489                 AXGMAC_IOREAD(pdata, MMC_TX1024TOMAXOCTETS_GB_LO);
490         stats->tx1024tomaxoctets_gb +=
491         ((uint64_t)AXGMAC_IOREAD(pdata, MMC_TX1024TOMAXOCTETS_GB_HI) << 32);
492
493         stats->txunicastframes_gb +=
494                 AXGMAC_IOREAD(pdata, MMC_TXUNICASTFRAMES_GB_LO);
495         stats->txunicastframes_gb +=
496         ((uint64_t)AXGMAC_IOREAD(pdata, MMC_TXUNICASTFRAMES_GB_HI) << 32);
497
498         stats->txmulticastframes_gb +=
499                 AXGMAC_IOREAD(pdata, MMC_TXMULTICASTFRAMES_GB_LO);
500         stats->txmulticastframes_gb +=
501         ((uint64_t)AXGMAC_IOREAD(pdata, MMC_TXMULTICASTFRAMES_GB_HI) << 32);
502
503         stats->txbroadcastframes_g +=
504                 AXGMAC_IOREAD(pdata, MMC_TXBROADCASTFRAMES_GB_LO);
505         stats->txbroadcastframes_g +=
506         ((uint64_t)AXGMAC_IOREAD(pdata, MMC_TXBROADCASTFRAMES_GB_HI) << 32);
507
508         stats->txunderflowerror +=
509                 AXGMAC_IOREAD(pdata, MMC_TXUNDERFLOWERROR_LO);
510         stats->txunderflowerror +=
511         ((uint64_t)AXGMAC_IOREAD(pdata, MMC_TXUNDERFLOWERROR_HI) << 32);
512
513         stats->txoctetcount_g +=
514                 AXGMAC_IOREAD(pdata, MMC_TXOCTETCOUNT_G_LO);
515         stats->txoctetcount_g +=
516         ((uint64_t)AXGMAC_IOREAD(pdata, MMC_TXOCTETCOUNT_G_HI) << 32);
517
518         stats->txframecount_g +=
519                 AXGMAC_IOREAD(pdata, MMC_TXFRAMECOUNT_G_LO);
520         stats->txframecount_g +=
521         ((uint64_t)AXGMAC_IOREAD(pdata, MMC_TXFRAMECOUNT_G_HI) << 32);
522
523         stats->txpauseframes +=
524                 AXGMAC_IOREAD(pdata, MMC_TXPAUSEFRAMES_LO);
525         stats->txpauseframes +=
526         ((uint64_t)AXGMAC_IOREAD(pdata, MMC_TXPAUSEFRAMES_HI) << 32);
527
528         stats->txvlanframes_g +=
529                 AXGMAC_IOREAD(pdata, MMC_TXVLANFRAMES_G_LO);
530         stats->txvlanframes_g +=
531         ((uint64_t)AXGMAC_IOREAD(pdata, MMC_TXVLANFRAMES_G_HI) << 32);
532
533         /* Rx counters */
534         stats->rxframecount_gb +=
535                 AXGMAC_IOREAD(pdata, MMC_RXFRAMECOUNT_GB_LO);
536         stats->rxframecount_gb +=
537         ((uint64_t)AXGMAC_IOREAD(pdata, MMC_RXFRAMECOUNT_GB_HI) << 32);
538
539         stats->rxoctetcount_gb +=
540                 AXGMAC_IOREAD(pdata, MMC_RXOCTETCOUNT_GB_LO);
541         stats->rxoctetcount_gb +=
542         ((uint64_t)AXGMAC_IOREAD(pdata, MMC_RXOCTETCOUNT_GB_HI) << 32);
543
544         stats->rxoctetcount_g +=
545                 AXGMAC_IOREAD(pdata, MMC_RXOCTETCOUNT_G_LO);
546         stats->rxoctetcount_g +=
547         ((uint64_t)AXGMAC_IOREAD(pdata, MMC_RXOCTETCOUNT_G_HI) << 32);
548
549         stats->rxbroadcastframes_g +=
550                 AXGMAC_IOREAD(pdata, MMC_RXBROADCASTFRAMES_G_LO);
551         stats->rxbroadcastframes_g +=
552         ((uint64_t)AXGMAC_IOREAD(pdata, MMC_RXBROADCASTFRAMES_G_HI) << 32);
553
554         stats->rxmulticastframes_g +=
555                 AXGMAC_IOREAD(pdata, MMC_RXMULTICASTFRAMES_G_LO);
556         stats->rxmulticastframes_g +=
557         ((uint64_t)AXGMAC_IOREAD(pdata, MMC_RXMULTICASTFRAMES_G_HI) << 32);
558
559         stats->rxcrcerror +=
560                 AXGMAC_IOREAD(pdata, MMC_RXCRCERROR_LO);
561         stats->rxcrcerror +=
562         ((uint64_t)AXGMAC_IOREAD(pdata, MMC_RXCRCERROR_HI) << 32);
563
564         stats->rxrunterror +=
565                 AXGMAC_IOREAD(pdata, MMC_RXRUNTERROR);
566
567         stats->rxjabbererror +=
568                 AXGMAC_IOREAD(pdata, MMC_RXJABBERERROR);
569
570         stats->rxundersize_g +=
571                 AXGMAC_IOREAD(pdata, MMC_RXUNDERSIZE_G);
572
573         stats->rxoversize_g +=
574                 AXGMAC_IOREAD(pdata, MMC_RXOVERSIZE_G);
575
576         stats->rx64octets_gb +=
577                 AXGMAC_IOREAD(pdata, MMC_RX64OCTETS_GB_LO);
578         stats->rx64octets_gb +=
579         ((uint64_t)AXGMAC_IOREAD(pdata, MMC_RX64OCTETS_GB_HI) << 32);
580
581         stats->rx65to127octets_gb +=
582                 AXGMAC_IOREAD(pdata, MMC_RX65TO127OCTETS_GB_LO);
583         stats->rx65to127octets_gb +=
584         ((uint64_t)AXGMAC_IOREAD(pdata, MMC_RX65TO127OCTETS_GB_HI) << 32);
585
586         stats->rx128to255octets_gb +=
587                 AXGMAC_IOREAD(pdata, MMC_RX128TO255OCTETS_GB_LO);
588         stats->rx128to255octets_gb +=
589         ((uint64_t)AXGMAC_IOREAD(pdata, MMC_RX128TO255OCTETS_GB_HI) << 32);
590
591         stats->rx256to511octets_gb +=
592                 AXGMAC_IOREAD(pdata, MMC_RX256TO511OCTETS_GB_LO);
593         stats->rx256to511octets_gb +=
594         ((uint64_t)AXGMAC_IOREAD(pdata, MMC_RX256TO511OCTETS_GB_HI) << 32);
595
596         stats->rx512to1023octets_gb +=
597                 AXGMAC_IOREAD(pdata, MMC_RX512TO1023OCTETS_GB_LO);
598         stats->rx512to1023octets_gb +=
599         ((uint64_t)AXGMAC_IOREAD(pdata, MMC_RX512TO1023OCTETS_GB_HI) << 32);
600
601         stats->rx1024tomaxoctets_gb +=
602                 AXGMAC_IOREAD(pdata, MMC_RX1024TOMAXOCTETS_GB_LO);
603         stats->rx1024tomaxoctets_gb +=
604         ((uint64_t)AXGMAC_IOREAD(pdata, MMC_RX1024TOMAXOCTETS_GB_HI) << 32);
605
606         stats->rxunicastframes_g +=
607                 AXGMAC_IOREAD(pdata, MMC_RXUNICASTFRAMES_G_LO);
608         stats->rxunicastframes_g +=
609         ((uint64_t)AXGMAC_IOREAD(pdata, MMC_RXUNICASTFRAMES_G_HI) << 32);
610
611         stats->rxlengtherror +=
612                 AXGMAC_IOREAD(pdata, MMC_RXLENGTHERROR_LO);
613         stats->rxlengtherror +=
614         ((uint64_t)AXGMAC_IOREAD(pdata, MMC_RXLENGTHERROR_HI) << 32);
615
616         stats->rxoutofrangetype +=
617                 AXGMAC_IOREAD(pdata, MMC_RXOUTOFRANGETYPE_LO);
618         stats->rxoutofrangetype +=
619         ((uint64_t)AXGMAC_IOREAD(pdata, MMC_RXOUTOFRANGETYPE_HI) << 32);
620
621         stats->rxpauseframes +=
622                 AXGMAC_IOREAD(pdata, MMC_RXPAUSEFRAMES_LO);
623         stats->rxpauseframes +=
624         ((uint64_t)AXGMAC_IOREAD(pdata, MMC_RXPAUSEFRAMES_HI) << 32);
625
626         stats->rxfifooverflow +=
627                 AXGMAC_IOREAD(pdata, MMC_RXFIFOOVERFLOW_LO);
628         stats->rxfifooverflow +=
629         ((uint64_t)AXGMAC_IOREAD(pdata, MMC_RXFIFOOVERFLOW_HI) << 32);
630
631         stats->rxvlanframes_gb +=
632                 AXGMAC_IOREAD(pdata, MMC_RXVLANFRAMES_GB_LO);
633         stats->rxvlanframes_gb +=
634         ((uint64_t)AXGMAC_IOREAD(pdata, MMC_RXVLANFRAMES_GB_HI) << 32);
635
636         stats->rxwatchdogerror +=
637                 AXGMAC_IOREAD(pdata, MMC_RXWATCHDOGERROR);
638
639         /* Un-freeze counters */
640         AXGMAC_IOWRITE_BITS(pdata, MMC_CR, MCF, 0);
641 }
642
643 static int
644 axgbe_dev_xstats_get(struct rte_eth_dev *dev, struct rte_eth_xstat *stats,
645                      unsigned int n)
646 {
647         struct axgbe_port *pdata = dev->data->dev_private;
648         unsigned int i;
649
650         if (!stats)
651                 return 0;
652
653         axgbe_read_mmc_stats(pdata);
654
655         for (i = 0; i < n && i < AXGBE_XSTATS_COUNT; i++) {
656                 stats[i].id = i;
657                 stats[i].value = *(u64 *)((uint8_t *)&pdata->mmc_stats +
658                                 axgbe_xstats_strings[i].offset);
659         }
660
661         return i;
662 }
663
664 static int
665 axgbe_dev_xstats_get_names(__rte_unused struct rte_eth_dev *dev,
666                            struct rte_eth_xstat_name *xstats_names,
667                            unsigned int n)
668 {
669         unsigned int i;
670
671         if (n >= AXGBE_XSTATS_COUNT && xstats_names) {
672                 for (i = 0; i < AXGBE_XSTATS_COUNT; ++i) {
673                         snprintf(xstats_names[i].name,
674                                  RTE_ETH_XSTATS_NAME_SIZE, "%s",
675                                  axgbe_xstats_strings[i].name);
676                 }
677         }
678
679         return AXGBE_XSTATS_COUNT;
680 }
681
682 static int
683 axgbe_dev_xstats_get_by_id(struct rte_eth_dev *dev, const uint64_t *ids,
684                            uint64_t *values, unsigned int n)
685 {
686         unsigned int i;
687         uint64_t values_copy[AXGBE_XSTATS_COUNT];
688
689         if (!ids) {
690                 struct axgbe_port *pdata = dev->data->dev_private;
691
692                 if (n < AXGBE_XSTATS_COUNT)
693                         return AXGBE_XSTATS_COUNT;
694
695                 axgbe_read_mmc_stats(pdata);
696
697                 for (i = 0; i < AXGBE_XSTATS_COUNT; i++) {
698                         values[i] = *(u64 *)((uint8_t *)&pdata->mmc_stats +
699                                         axgbe_xstats_strings[i].offset);
700                 }
701
702                 return i;
703         }
704
705         axgbe_dev_xstats_get_by_id(dev, NULL, values_copy, AXGBE_XSTATS_COUNT);
706
707         for (i = 0; i < n; i++) {
708                 if (ids[i] >= AXGBE_XSTATS_COUNT) {
709                         PMD_DRV_LOG(ERR, "id value isn't valid\n");
710                         return -1;
711                 }
712                 values[i] = values_copy[ids[i]];
713         }
714         return n;
715 }
716
717 static int
718 axgbe_dev_xstats_get_names_by_id(struct rte_eth_dev *dev,
719                                  struct rte_eth_xstat_name *xstats_names,
720                                  const uint64_t *ids,
721                                  unsigned int size)
722 {
723         struct rte_eth_xstat_name xstats_names_copy[AXGBE_XSTATS_COUNT];
724         unsigned int i;
725
726         if (!ids)
727                 return axgbe_dev_xstats_get_names(dev, xstats_names, size);
728
729         axgbe_dev_xstats_get_names(dev, xstats_names_copy, size);
730
731         for (i = 0; i < size; i++) {
732                 if (ids[i] >= AXGBE_XSTATS_COUNT) {
733                         PMD_DRV_LOG(ERR, "id value isn't valid\n");
734                         return -1;
735                 }
736                 strcpy(xstats_names[i].name, xstats_names_copy[ids[i]].name);
737         }
738         return size;
739 }
740
741 static int
742 axgbe_dev_xstats_reset(struct rte_eth_dev *dev)
743 {
744         struct axgbe_port *pdata = dev->data->dev_private;
745         struct axgbe_mmc_stats *stats = &pdata->mmc_stats;
746
747         /* MMC registers are configured for reset on read */
748         axgbe_read_mmc_stats(pdata);
749
750         /* Reset stats */
751         memset(stats, 0, sizeof(*stats));
752
753         return 0;
754 }
755
756 static int
757 axgbe_dev_stats_get(struct rte_eth_dev *dev,
758                     struct rte_eth_stats *stats)
759 {
760         struct axgbe_rx_queue *rxq;
761         struct axgbe_tx_queue *txq;
762         struct axgbe_port *pdata = dev->data->dev_private;
763         struct axgbe_mmc_stats *mmc_stats = &pdata->mmc_stats;
764         unsigned int i;
765
766         axgbe_read_mmc_stats(pdata);
767
768         stats->imissed = mmc_stats->rxfifooverflow;
769
770         for (i = 0; i < dev->data->nb_rx_queues; i++) {
771                 rxq = dev->data->rx_queues[i];
772                 stats->q_ipackets[i] = rxq->pkts;
773                 stats->ipackets += rxq->pkts;
774                 stats->q_ibytes[i] = rxq->bytes;
775                 stats->ibytes += rxq->bytes;
776                 stats->rx_nombuf += rxq->rx_mbuf_alloc_failed;
777                 stats->q_errors[i] = rxq->errors + rxq->rx_mbuf_alloc_failed;
778                 stats->ierrors += rxq->errors;
779         }
780
781         for (i = 0; i < dev->data->nb_tx_queues; i++) {
782                 txq = dev->data->tx_queues[i];
783                 stats->q_opackets[i] = txq->pkts;
784                 stats->opackets += txq->pkts;
785                 stats->q_obytes[i] = txq->bytes;
786                 stats->obytes += txq->bytes;
787                 stats->oerrors += txq->errors;
788         }
789
790         return 0;
791 }
792
793 static int
794 axgbe_dev_stats_reset(struct rte_eth_dev *dev)
795 {
796         struct axgbe_rx_queue *rxq;
797         struct axgbe_tx_queue *txq;
798         unsigned int i;
799
800         for (i = 0; i < dev->data->nb_rx_queues; i++) {
801                 rxq = dev->data->rx_queues[i];
802                 rxq->pkts = 0;
803                 rxq->bytes = 0;
804                 rxq->errors = 0;
805                 rxq->rx_mbuf_alloc_failed = 0;
806         }
807         for (i = 0; i < dev->data->nb_tx_queues; i++) {
808                 txq = dev->data->tx_queues[i];
809                 txq->pkts = 0;
810                 txq->bytes = 0;
811                 txq->errors = 0;
812         }
813
814         return 0;
815 }
816
817 static int
818 axgbe_dev_info_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *dev_info)
819 {
820         struct axgbe_port *pdata = dev->data->dev_private;
821
822         dev_info->max_rx_queues = pdata->rx_ring_count;
823         dev_info->max_tx_queues = pdata->tx_ring_count;
824         dev_info->min_rx_bufsize = AXGBE_RX_MIN_BUF_SIZE;
825         dev_info->max_rx_pktlen = AXGBE_RX_MAX_BUF_SIZE;
826         dev_info->max_mac_addrs = AXGBE_MAX_MAC_ADDRS;
827         dev_info->speed_capa =  ETH_LINK_SPEED_10G;
828
829         dev_info->rx_offload_capa =
830                 DEV_RX_OFFLOAD_IPV4_CKSUM |
831                 DEV_RX_OFFLOAD_UDP_CKSUM  |
832                 DEV_RX_OFFLOAD_TCP_CKSUM  |
833                 DEV_RX_OFFLOAD_JUMBO_FRAME      |
834                 DEV_RX_OFFLOAD_SCATTER    |
835                 DEV_RX_OFFLOAD_KEEP_CRC;
836
837         dev_info->tx_offload_capa =
838                 DEV_TX_OFFLOAD_IPV4_CKSUM  |
839                 DEV_TX_OFFLOAD_UDP_CKSUM   |
840                 DEV_TX_OFFLOAD_TCP_CKSUM;
841
842         if (pdata->hw_feat.rss) {
843                 dev_info->flow_type_rss_offloads = AXGBE_RSS_OFFLOAD;
844                 dev_info->reta_size = pdata->hw_feat.hash_table_size;
845                 dev_info->hash_key_size =  AXGBE_RSS_HASH_KEY_SIZE;
846         }
847
848         dev_info->rx_desc_lim = rx_desc_lim;
849         dev_info->tx_desc_lim = tx_desc_lim;
850
851         dev_info->default_rxconf = (struct rte_eth_rxconf) {
852                 .rx_free_thresh = AXGBE_RX_FREE_THRESH,
853         };
854
855         dev_info->default_txconf = (struct rte_eth_txconf) {
856                 .tx_free_thresh = AXGBE_TX_FREE_THRESH,
857         };
858
859         return 0;
860 }
861
862 static void axgbe_get_all_hw_features(struct axgbe_port *pdata)
863 {
864         unsigned int mac_hfr0, mac_hfr1, mac_hfr2;
865         struct axgbe_hw_features *hw_feat = &pdata->hw_feat;
866
867         mac_hfr0 = AXGMAC_IOREAD(pdata, MAC_HWF0R);
868         mac_hfr1 = AXGMAC_IOREAD(pdata, MAC_HWF1R);
869         mac_hfr2 = AXGMAC_IOREAD(pdata, MAC_HWF2R);
870
871         memset(hw_feat, 0, sizeof(*hw_feat));
872
873         hw_feat->version = AXGMAC_IOREAD(pdata, MAC_VR);
874
875         /* Hardware feature register 0 */
876         hw_feat->gmii        = AXGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, GMIISEL);
877         hw_feat->vlhash      = AXGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, VLHASH);
878         hw_feat->sma         = AXGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, SMASEL);
879         hw_feat->rwk         = AXGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, RWKSEL);
880         hw_feat->mgk         = AXGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, MGKSEL);
881         hw_feat->mmc         = AXGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, MMCSEL);
882         hw_feat->aoe         = AXGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, ARPOFFSEL);
883         hw_feat->ts          = AXGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, TSSEL);
884         hw_feat->eee         = AXGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, EEESEL);
885         hw_feat->tx_coe      = AXGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, TXCOESEL);
886         hw_feat->rx_coe      = AXGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, RXCOESEL);
887         hw_feat->addn_mac    = AXGMAC_GET_BITS(mac_hfr0, MAC_HWF0R,
888                                               ADDMACADRSEL);
889         hw_feat->ts_src      = AXGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, TSSTSSEL);
890         hw_feat->sa_vlan_ins = AXGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, SAVLANINS);
891
892         /* Hardware feature register 1 */
893         hw_feat->rx_fifo_size  = AXGMAC_GET_BITS(mac_hfr1, MAC_HWF1R,
894                                                 RXFIFOSIZE);
895         hw_feat->tx_fifo_size  = AXGMAC_GET_BITS(mac_hfr1, MAC_HWF1R,
896                                                 TXFIFOSIZE);
897         hw_feat->adv_ts_hi     = AXGMAC_GET_BITS(mac_hfr1,
898                                                  MAC_HWF1R, ADVTHWORD);
899         hw_feat->dma_width     = AXGMAC_GET_BITS(mac_hfr1, MAC_HWF1R, ADDR64);
900         hw_feat->dcb           = AXGMAC_GET_BITS(mac_hfr1, MAC_HWF1R, DCBEN);
901         hw_feat->sph           = AXGMAC_GET_BITS(mac_hfr1, MAC_HWF1R, SPHEN);
902         hw_feat->tso           = AXGMAC_GET_BITS(mac_hfr1, MAC_HWF1R, TSOEN);
903         hw_feat->dma_debug     = AXGMAC_GET_BITS(mac_hfr1, MAC_HWF1R, DBGMEMA);
904         hw_feat->rss           = AXGMAC_GET_BITS(mac_hfr1, MAC_HWF1R, RSSEN);
905         hw_feat->tc_cnt        = AXGMAC_GET_BITS(mac_hfr1, MAC_HWF1R, NUMTC);
906         hw_feat->hash_table_size = AXGMAC_GET_BITS(mac_hfr1, MAC_HWF1R,
907                                                   HASHTBLSZ);
908         hw_feat->l3l4_filter_num = AXGMAC_GET_BITS(mac_hfr1, MAC_HWF1R,
909                                                   L3L4FNUM);
910
911         /* Hardware feature register 2 */
912         hw_feat->rx_q_cnt     = AXGMAC_GET_BITS(mac_hfr2, MAC_HWF2R, RXQCNT);
913         hw_feat->tx_q_cnt     = AXGMAC_GET_BITS(mac_hfr2, MAC_HWF2R, TXQCNT);
914         hw_feat->rx_ch_cnt    = AXGMAC_GET_BITS(mac_hfr2, MAC_HWF2R, RXCHCNT);
915         hw_feat->tx_ch_cnt    = AXGMAC_GET_BITS(mac_hfr2, MAC_HWF2R, TXCHCNT);
916         hw_feat->pps_out_num  = AXGMAC_GET_BITS(mac_hfr2, MAC_HWF2R, PPSOUTNUM);
917         hw_feat->aux_snap_num = AXGMAC_GET_BITS(mac_hfr2, MAC_HWF2R,
918                                                 AUXSNAPNUM);
919
920         /* Translate the Hash Table size into actual number */
921         switch (hw_feat->hash_table_size) {
922         case 0:
923                 break;
924         case 1:
925                 hw_feat->hash_table_size = 64;
926                 break;
927         case 2:
928                 hw_feat->hash_table_size = 128;
929                 break;
930         case 3:
931                 hw_feat->hash_table_size = 256;
932                 break;
933         }
934
935         /* Translate the address width setting into actual number */
936         switch (hw_feat->dma_width) {
937         case 0:
938                 hw_feat->dma_width = 32;
939                 break;
940         case 1:
941                 hw_feat->dma_width = 40;
942                 break;
943         case 2:
944                 hw_feat->dma_width = 48;
945                 break;
946         default:
947                 hw_feat->dma_width = 32;
948         }
949
950         /* The Queue, Channel and TC counts are zero based so increment them
951          * to get the actual number
952          */
953         hw_feat->rx_q_cnt++;
954         hw_feat->tx_q_cnt++;
955         hw_feat->rx_ch_cnt++;
956         hw_feat->tx_ch_cnt++;
957         hw_feat->tc_cnt++;
958
959         /* Translate the fifo sizes into actual numbers */
960         hw_feat->rx_fifo_size = 1 << (hw_feat->rx_fifo_size + 7);
961         hw_feat->tx_fifo_size = 1 << (hw_feat->tx_fifo_size + 7);
962 }
963
964 static void axgbe_init_all_fptrs(struct axgbe_port *pdata)
965 {
966         axgbe_init_function_ptrs_dev(&pdata->hw_if);
967         axgbe_init_function_ptrs_phy(&pdata->phy_if);
968         axgbe_init_function_ptrs_i2c(&pdata->i2c_if);
969         pdata->vdata->init_function_ptrs_phy_impl(&pdata->phy_if);
970 }
971
972 static void axgbe_set_counts(struct axgbe_port *pdata)
973 {
974         /* Set all the function pointers */
975         axgbe_init_all_fptrs(pdata);
976
977         /* Populate the hardware features */
978         axgbe_get_all_hw_features(pdata);
979
980         /* Set default max values if not provided */
981         if (!pdata->tx_max_channel_count)
982                 pdata->tx_max_channel_count = pdata->hw_feat.tx_ch_cnt;
983         if (!pdata->rx_max_channel_count)
984                 pdata->rx_max_channel_count = pdata->hw_feat.rx_ch_cnt;
985
986         if (!pdata->tx_max_q_count)
987                 pdata->tx_max_q_count = pdata->hw_feat.tx_q_cnt;
988         if (!pdata->rx_max_q_count)
989                 pdata->rx_max_q_count = pdata->hw_feat.rx_q_cnt;
990
991         /* Calculate the number of Tx and Rx rings to be created
992          *  -Tx (DMA) Channels map 1-to-1 to Tx Queues so set
993          *   the number of Tx queues to the number of Tx channels
994          *   enabled
995          *  -Rx (DMA) Channels do not map 1-to-1 so use the actual
996          *   number of Rx queues or maximum allowed
997          */
998         pdata->tx_ring_count = RTE_MIN(pdata->hw_feat.tx_ch_cnt,
999                                      pdata->tx_max_channel_count);
1000         pdata->tx_ring_count = RTE_MIN(pdata->tx_ring_count,
1001                                      pdata->tx_max_q_count);
1002
1003         pdata->tx_q_count = pdata->tx_ring_count;
1004
1005         pdata->rx_ring_count = RTE_MIN(pdata->hw_feat.rx_ch_cnt,
1006                                      pdata->rx_max_channel_count);
1007
1008         pdata->rx_q_count = RTE_MIN(pdata->hw_feat.rx_q_cnt,
1009                                   pdata->rx_max_q_count);
1010 }
1011
1012 static void axgbe_default_config(struct axgbe_port *pdata)
1013 {
1014         pdata->pblx8 = DMA_PBL_X8_ENABLE;
1015         pdata->tx_sf_mode = MTL_TSF_ENABLE;
1016         pdata->tx_threshold = MTL_TX_THRESHOLD_64;
1017         pdata->tx_pbl = DMA_PBL_32;
1018         pdata->tx_osp_mode = DMA_OSP_ENABLE;
1019         pdata->rx_sf_mode = MTL_RSF_ENABLE;
1020         pdata->rx_threshold = MTL_RX_THRESHOLD_64;
1021         pdata->rx_pbl = DMA_PBL_32;
1022         pdata->pause_autoneg = 1;
1023         pdata->tx_pause = 0;
1024         pdata->rx_pause = 0;
1025         pdata->phy_speed = SPEED_UNKNOWN;
1026         pdata->power_down = 0;
1027 }
1028
1029 static int
1030 pci_device_cmp(const struct rte_device *dev, const void *_pci_id)
1031 {
1032         const struct rte_pci_device *pdev = RTE_DEV_TO_PCI_CONST(dev);
1033         const struct rte_pci_id *pcid = _pci_id;
1034
1035         if (pdev->id.vendor_id == AMD_PCI_VENDOR_ID &&
1036                         pdev->id.device_id == pcid->device_id)
1037                 return 0;
1038         return 1;
1039 }
1040
1041 static bool
1042 pci_search_device(int device_id)
1043 {
1044         struct rte_bus *pci_bus;
1045         struct rte_pci_id dev_id;
1046
1047         dev_id.device_id = device_id;
1048         pci_bus = rte_bus_find_by_name("pci");
1049         return (pci_bus != NULL) &&
1050                 (pci_bus->find_device(NULL, pci_device_cmp, &dev_id) != NULL);
1051 }
1052
1053 /*
1054  * It returns 0 on success.
1055  */
1056 static int
1057 eth_axgbe_dev_init(struct rte_eth_dev *eth_dev)
1058 {
1059         PMD_INIT_FUNC_TRACE();
1060         struct axgbe_port *pdata;
1061         struct rte_pci_device *pci_dev;
1062         uint32_t reg, mac_lo, mac_hi;
1063         int ret;
1064
1065         eth_dev->dev_ops = &axgbe_eth_dev_ops;
1066
1067         /*
1068          * For secondary processes, we don't initialise any further as primary
1069          * has already done this work.
1070          */
1071         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
1072                 return 0;
1073
1074         pdata = eth_dev->data->dev_private;
1075         /* initial state */
1076         axgbe_set_bit(AXGBE_DOWN, &pdata->dev_state);
1077         axgbe_set_bit(AXGBE_STOPPED, &pdata->dev_state);
1078         pdata->eth_dev = eth_dev;
1079
1080         pci_dev = RTE_DEV_TO_PCI(eth_dev->device);
1081         pdata->pci_dev = pci_dev;
1082
1083         /*
1084          * Use root complex device ID to differentiate RV AXGBE vs SNOWY AXGBE
1085          */
1086         if (pci_search_device(AMD_PCI_RV_ROOT_COMPLEX_ID)) {
1087                 pdata->xpcs_window_def_reg = PCS_V2_RV_WINDOW_DEF;
1088                 pdata->xpcs_window_sel_reg = PCS_V2_RV_WINDOW_SELECT;
1089         } else {
1090                 pdata->xpcs_window_def_reg = PCS_V2_WINDOW_DEF;
1091                 pdata->xpcs_window_sel_reg = PCS_V2_WINDOW_SELECT;
1092         }
1093
1094         pdata->xgmac_regs =
1095                 (void *)pci_dev->mem_resource[AXGBE_AXGMAC_BAR].addr;
1096         pdata->xprop_regs = (void *)((uint8_t *)pdata->xgmac_regs
1097                                      + AXGBE_MAC_PROP_OFFSET);
1098         pdata->xi2c_regs = (void *)((uint8_t *)pdata->xgmac_regs
1099                                     + AXGBE_I2C_CTRL_OFFSET);
1100         pdata->xpcs_regs = (void *)pci_dev->mem_resource[AXGBE_XPCS_BAR].addr;
1101
1102         /* version specific driver data*/
1103         if (pci_dev->id.device_id == AMD_PCI_AXGBE_DEVICE_V2A)
1104                 pdata->vdata = &axgbe_v2a;
1105         else
1106                 pdata->vdata = &axgbe_v2b;
1107
1108         /* Configure the PCS indirect addressing support */
1109         reg = XPCS32_IOREAD(pdata, pdata->xpcs_window_def_reg);
1110         pdata->xpcs_window = XPCS_GET_BITS(reg, PCS_V2_WINDOW_DEF, OFFSET);
1111         pdata->xpcs_window <<= 6;
1112         pdata->xpcs_window_size = XPCS_GET_BITS(reg, PCS_V2_WINDOW_DEF, SIZE);
1113         pdata->xpcs_window_size = 1 << (pdata->xpcs_window_size + 7);
1114         pdata->xpcs_window_mask = pdata->xpcs_window_size - 1;
1115
1116         PMD_INIT_LOG(DEBUG,
1117                      "xpcs window :%x, size :%x, mask :%x ", pdata->xpcs_window,
1118                      pdata->xpcs_window_size, pdata->xpcs_window_mask);
1119         XP_IOWRITE(pdata, XP_INT_EN, 0x1fffff);
1120
1121         /* Retrieve the MAC address */
1122         mac_lo = XP_IOREAD(pdata, XP_MAC_ADDR_LO);
1123         mac_hi = XP_IOREAD(pdata, XP_MAC_ADDR_HI);
1124         pdata->mac_addr.addr_bytes[0] = mac_lo & 0xff;
1125         pdata->mac_addr.addr_bytes[1] = (mac_lo >> 8) & 0xff;
1126         pdata->mac_addr.addr_bytes[2] = (mac_lo >> 16) & 0xff;
1127         pdata->mac_addr.addr_bytes[3] = (mac_lo >> 24) & 0xff;
1128         pdata->mac_addr.addr_bytes[4] = mac_hi & 0xff;
1129         pdata->mac_addr.addr_bytes[5] = (mac_hi >> 8)  &  0xff;
1130
1131         eth_dev->data->mac_addrs = rte_zmalloc("axgbe_mac_addr",
1132                                                RTE_ETHER_ADDR_LEN, 0);
1133         if (!eth_dev->data->mac_addrs) {
1134                 PMD_INIT_LOG(ERR,
1135                              "Failed to alloc %u bytes needed to store MAC addr tbl",
1136                              RTE_ETHER_ADDR_LEN);
1137                 return -ENOMEM;
1138         }
1139
1140         if (!rte_is_valid_assigned_ether_addr(&pdata->mac_addr))
1141                 rte_eth_random_addr(pdata->mac_addr.addr_bytes);
1142
1143         /* Copy the permanent MAC address */
1144         rte_ether_addr_copy(&pdata->mac_addr, &eth_dev->data->mac_addrs[0]);
1145
1146         /* Clock settings */
1147         pdata->sysclk_rate = AXGBE_V2_DMA_CLOCK_FREQ;
1148         pdata->ptpclk_rate = AXGBE_V2_PTP_CLOCK_FREQ;
1149
1150         /* Set the DMA coherency values */
1151         pdata->coherent = 1;
1152         pdata->axdomain = AXGBE_DMA_OS_AXDOMAIN;
1153         pdata->arcache = AXGBE_DMA_OS_ARCACHE;
1154         pdata->awcache = AXGBE_DMA_OS_AWCACHE;
1155
1156         /* Set the maximum channels and queues */
1157         reg = XP_IOREAD(pdata, XP_PROP_1);
1158         pdata->tx_max_channel_count = XP_GET_BITS(reg, XP_PROP_1, MAX_TX_DMA);
1159         pdata->rx_max_channel_count = XP_GET_BITS(reg, XP_PROP_1, MAX_RX_DMA);
1160         pdata->tx_max_q_count = XP_GET_BITS(reg, XP_PROP_1, MAX_TX_QUEUES);
1161         pdata->rx_max_q_count = XP_GET_BITS(reg, XP_PROP_1, MAX_RX_QUEUES);
1162
1163         /* Set the hardware channel and queue counts */
1164         axgbe_set_counts(pdata);
1165
1166         /* Set the maximum fifo amounts */
1167         reg = XP_IOREAD(pdata, XP_PROP_2);
1168         pdata->tx_max_fifo_size = XP_GET_BITS(reg, XP_PROP_2, TX_FIFO_SIZE);
1169         pdata->tx_max_fifo_size *= 16384;
1170         pdata->tx_max_fifo_size = RTE_MIN(pdata->tx_max_fifo_size,
1171                                           pdata->vdata->tx_max_fifo_size);
1172         pdata->rx_max_fifo_size = XP_GET_BITS(reg, XP_PROP_2, RX_FIFO_SIZE);
1173         pdata->rx_max_fifo_size *= 16384;
1174         pdata->rx_max_fifo_size = RTE_MIN(pdata->rx_max_fifo_size,
1175                                           pdata->vdata->rx_max_fifo_size);
1176         /* Issue software reset to DMA */
1177         ret = pdata->hw_if.exit(pdata);
1178         if (ret)
1179                 PMD_DRV_LOG(ERR, "hw_if->exit EBUSY error\n");
1180
1181         /* Set default configuration data */
1182         axgbe_default_config(pdata);
1183
1184         /* Set default max values if not provided */
1185         if (!pdata->tx_max_fifo_size)
1186                 pdata->tx_max_fifo_size = pdata->hw_feat.tx_fifo_size;
1187         if (!pdata->rx_max_fifo_size)
1188                 pdata->rx_max_fifo_size = pdata->hw_feat.rx_fifo_size;
1189
1190         pdata->tx_desc_count = AXGBE_MAX_RING_DESC;
1191         pdata->rx_desc_count = AXGBE_MAX_RING_DESC;
1192         pthread_mutex_init(&pdata->xpcs_mutex, NULL);
1193         pthread_mutex_init(&pdata->i2c_mutex, NULL);
1194         pthread_mutex_init(&pdata->an_mutex, NULL);
1195         pthread_mutex_init(&pdata->phy_mutex, NULL);
1196
1197         ret = pdata->phy_if.phy_init(pdata);
1198         if (ret) {
1199                 rte_free(eth_dev->data->mac_addrs);
1200                 eth_dev->data->mac_addrs = NULL;
1201                 return ret;
1202         }
1203
1204         rte_intr_callback_register(&pci_dev->intr_handle,
1205                                    axgbe_dev_interrupt_handler,
1206                                    (void *)eth_dev);
1207         PMD_INIT_LOG(DEBUG, "port %d vendorID=0x%x deviceID=0x%x",
1208                      eth_dev->data->port_id, pci_dev->id.vendor_id,
1209                      pci_dev->id.device_id);
1210
1211         return 0;
1212 }
1213
1214 static int
1215 eth_axgbe_dev_uninit(struct rte_eth_dev *eth_dev)
1216 {
1217         struct rte_pci_device *pci_dev;
1218
1219         PMD_INIT_FUNC_TRACE();
1220
1221         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
1222                 return 0;
1223
1224         pci_dev = RTE_DEV_TO_PCI(eth_dev->device);
1225         eth_dev->dev_ops = NULL;
1226         eth_dev->rx_pkt_burst = NULL;
1227         eth_dev->tx_pkt_burst = NULL;
1228         axgbe_dev_clear_queues(eth_dev);
1229
1230         /* disable uio intr before callback unregister */
1231         rte_intr_disable(&pci_dev->intr_handle);
1232         rte_intr_callback_unregister(&pci_dev->intr_handle,
1233                                      axgbe_dev_interrupt_handler,
1234                                      (void *)eth_dev);
1235
1236         return 0;
1237 }
1238
1239 static int eth_axgbe_pci_probe(struct rte_pci_driver *pci_drv __rte_unused,
1240         struct rte_pci_device *pci_dev)
1241 {
1242         return rte_eth_dev_pci_generic_probe(pci_dev,
1243                 sizeof(struct axgbe_port), eth_axgbe_dev_init);
1244 }
1245
1246 static int eth_axgbe_pci_remove(struct rte_pci_device *pci_dev)
1247 {
1248         return rte_eth_dev_pci_generic_remove(pci_dev, eth_axgbe_dev_uninit);
1249 }
1250
1251 static struct rte_pci_driver rte_axgbe_pmd = {
1252         .id_table = pci_id_axgbe_map,
1253         .drv_flags = RTE_PCI_DRV_NEED_MAPPING,
1254         .probe = eth_axgbe_pci_probe,
1255         .remove = eth_axgbe_pci_remove,
1256 };
1257
1258 RTE_PMD_REGISTER_PCI(net_axgbe, rte_axgbe_pmd);
1259 RTE_PMD_REGISTER_PCI_TABLE(net_axgbe, pci_id_axgbe_map);
1260 RTE_PMD_REGISTER_KMOD_DEP(net_axgbe, "* igb_uio | uio_pci_generic | vfio-pci");
1261
1262 RTE_INIT(axgbe_init_log)
1263 {
1264         axgbe_logtype_init = rte_log_register("pmd.net.axgbe.init");
1265         if (axgbe_logtype_init >= 0)
1266                 rte_log_set_level(axgbe_logtype_init, RTE_LOG_NOTICE);
1267         axgbe_logtype_driver = rte_log_register("pmd.net.axgbe.driver");
1268         if (axgbe_logtype_driver >= 0)
1269                 rte_log_set_level(axgbe_logtype_driver, RTE_LOG_NOTICE);
1270 }