ethdev: make stats and xstats reset callbacks return int
[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
11 static int eth_axgbe_dev_init(struct rte_eth_dev *eth_dev);
12 static int eth_axgbe_dev_uninit(struct rte_eth_dev *eth_dev);
13 static int  axgbe_dev_configure(struct rte_eth_dev *dev);
14 static int  axgbe_dev_start(struct rte_eth_dev *dev);
15 static void axgbe_dev_stop(struct rte_eth_dev *dev);
16 static void axgbe_dev_interrupt_handler(void *param);
17 static void axgbe_dev_close(struct rte_eth_dev *dev);
18 static int axgbe_dev_promiscuous_enable(struct rte_eth_dev *dev);
19 static int axgbe_dev_promiscuous_disable(struct rte_eth_dev *dev);
20 static void axgbe_dev_allmulticast_enable(struct rte_eth_dev *dev);
21 static void axgbe_dev_allmulticast_disable(struct rte_eth_dev *dev);
22 static int axgbe_dev_link_update(struct rte_eth_dev *dev,
23                                  int wait_to_complete);
24 static int axgbe_dev_stats_get(struct rte_eth_dev *dev,
25                                 struct rte_eth_stats *stats);
26 static int axgbe_dev_stats_reset(struct rte_eth_dev *dev);
27 static int  axgbe_dev_info_get(struct rte_eth_dev *dev,
28                                struct rte_eth_dev_info *dev_info);
29
30 /* The set of PCI devices this driver supports */
31 #define AMD_PCI_VENDOR_ID       0x1022
32 #define AMD_PCI_AXGBE_DEVICE_V2A 0x1458
33 #define AMD_PCI_AXGBE_DEVICE_V2B 0x1459
34
35 int axgbe_logtype_init;
36 int axgbe_logtype_driver;
37
38 static const struct rte_pci_id pci_id_axgbe_map[] = {
39         {RTE_PCI_DEVICE(AMD_PCI_VENDOR_ID, AMD_PCI_AXGBE_DEVICE_V2A)},
40         {RTE_PCI_DEVICE(AMD_PCI_VENDOR_ID, AMD_PCI_AXGBE_DEVICE_V2B)},
41         { .vendor_id = 0, },
42 };
43
44 static struct axgbe_version_data axgbe_v2a = {
45         .init_function_ptrs_phy_impl    = axgbe_init_function_ptrs_phy_v2,
46         .xpcs_access                    = AXGBE_XPCS_ACCESS_V2,
47         .mmc_64bit                      = 1,
48         .tx_max_fifo_size               = 229376,
49         .rx_max_fifo_size               = 229376,
50         .tx_tstamp_workaround           = 1,
51         .ecc_support                    = 1,
52         .i2c_support                    = 1,
53         .an_cdr_workaround              = 1,
54 };
55
56 static struct axgbe_version_data axgbe_v2b = {
57         .init_function_ptrs_phy_impl    = axgbe_init_function_ptrs_phy_v2,
58         .xpcs_access                    = AXGBE_XPCS_ACCESS_V2,
59         .mmc_64bit                      = 1,
60         .tx_max_fifo_size               = 65536,
61         .rx_max_fifo_size               = 65536,
62         .tx_tstamp_workaround           = 1,
63         .ecc_support                    = 1,
64         .i2c_support                    = 1,
65         .an_cdr_workaround              = 1,
66 };
67
68 static const struct rte_eth_desc_lim rx_desc_lim = {
69         .nb_max = AXGBE_MAX_RING_DESC,
70         .nb_min = AXGBE_MIN_RING_DESC,
71         .nb_align = 8,
72 };
73
74 static const struct rte_eth_desc_lim tx_desc_lim = {
75         .nb_max = AXGBE_MAX_RING_DESC,
76         .nb_min = AXGBE_MIN_RING_DESC,
77         .nb_align = 8,
78 };
79
80 static const struct eth_dev_ops axgbe_eth_dev_ops = {
81         .dev_configure        = axgbe_dev_configure,
82         .dev_start            = axgbe_dev_start,
83         .dev_stop             = axgbe_dev_stop,
84         .dev_close            = axgbe_dev_close,
85         .promiscuous_enable   = axgbe_dev_promiscuous_enable,
86         .promiscuous_disable  = axgbe_dev_promiscuous_disable,
87         .allmulticast_enable  = axgbe_dev_allmulticast_enable,
88         .allmulticast_disable = axgbe_dev_allmulticast_disable,
89         .link_update          = axgbe_dev_link_update,
90         .stats_get            = axgbe_dev_stats_get,
91         .stats_reset          = axgbe_dev_stats_reset,
92         .dev_infos_get        = axgbe_dev_info_get,
93         .rx_queue_setup       = axgbe_dev_rx_queue_setup,
94         .rx_queue_release     = axgbe_dev_rx_queue_release,
95         .tx_queue_setup       = axgbe_dev_tx_queue_setup,
96         .tx_queue_release     = axgbe_dev_tx_queue_release,
97 };
98
99 static int axgbe_phy_reset(struct axgbe_port *pdata)
100 {
101         pdata->phy_link = -1;
102         pdata->phy_speed = SPEED_UNKNOWN;
103         return pdata->phy_if.phy_reset(pdata);
104 }
105
106 /*
107  * Interrupt handler triggered by NIC  for handling
108  * specific interrupt.
109  *
110  * @param handle
111  *  Pointer to interrupt handle.
112  * @param param
113  *  The address of parameter (struct rte_eth_dev *) regsitered before.
114  *
115  * @return
116  *  void
117  */
118 static void
119 axgbe_dev_interrupt_handler(void *param)
120 {
121         struct rte_eth_dev *dev = (struct rte_eth_dev *)param;
122         struct axgbe_port *pdata = dev->data->dev_private;
123         unsigned int dma_isr, dma_ch_isr;
124
125         pdata->phy_if.an_isr(pdata);
126         /*DMA related interrupts*/
127         dma_isr = AXGMAC_IOREAD(pdata, DMA_ISR);
128         if (dma_isr) {
129                 if (dma_isr & 1) {
130                         dma_ch_isr =
131                                 AXGMAC_DMA_IOREAD((struct axgbe_rx_queue *)
132                                                   pdata->rx_queues[0],
133                                                   DMA_CH_SR);
134                         AXGMAC_DMA_IOWRITE((struct axgbe_rx_queue *)
135                                            pdata->rx_queues[0],
136                                            DMA_CH_SR, dma_ch_isr);
137                 }
138         }
139         /* Unmask interrupts since disabled after generation */
140         rte_intr_ack(&pdata->pci_dev->intr_handle);
141 }
142
143 /*
144  * Configure device link speed and setup link.
145  * It returns 0 on success.
146  */
147 static int
148 axgbe_dev_configure(struct rte_eth_dev *dev)
149 {
150         struct axgbe_port *pdata =  dev->data->dev_private;
151         /* Checksum offload to hardware */
152         pdata->rx_csum_enable = dev->data->dev_conf.rxmode.offloads &
153                                 DEV_RX_OFFLOAD_CHECKSUM;
154         return 0;
155 }
156
157 static int
158 axgbe_dev_rx_mq_config(struct rte_eth_dev *dev)
159 {
160         struct axgbe_port *pdata = dev->data->dev_private;
161
162         if (dev->data->dev_conf.rxmode.mq_mode == ETH_MQ_RX_RSS)
163                 pdata->rss_enable = 1;
164         else if (dev->data->dev_conf.rxmode.mq_mode == ETH_MQ_RX_NONE)
165                 pdata->rss_enable = 0;
166         else
167                 return  -1;
168         return 0;
169 }
170
171 static int
172 axgbe_dev_start(struct rte_eth_dev *dev)
173 {
174         struct axgbe_port *pdata = dev->data->dev_private;
175         int ret;
176
177         PMD_INIT_FUNC_TRACE();
178
179         /* Multiqueue RSS */
180         ret = axgbe_dev_rx_mq_config(dev);
181         if (ret) {
182                 PMD_DRV_LOG(ERR, "Unable to config RX MQ\n");
183                 return ret;
184         }
185         ret = axgbe_phy_reset(pdata);
186         if (ret) {
187                 PMD_DRV_LOG(ERR, "phy reset failed\n");
188                 return ret;
189         }
190         ret = pdata->hw_if.init(pdata);
191         if (ret) {
192                 PMD_DRV_LOG(ERR, "dev_init failed\n");
193                 return ret;
194         }
195
196         /* enable uio/vfio intr/eventfd mapping */
197         rte_intr_enable(&pdata->pci_dev->intr_handle);
198
199         /* phy start*/
200         pdata->phy_if.phy_start(pdata);
201         axgbe_dev_enable_tx(dev);
202         axgbe_dev_enable_rx(dev);
203
204         axgbe_clear_bit(AXGBE_STOPPED, &pdata->dev_state);
205         axgbe_clear_bit(AXGBE_DOWN, &pdata->dev_state);
206         return 0;
207 }
208
209 /* Stop device: disable rx and tx functions to allow for reconfiguring. */
210 static void
211 axgbe_dev_stop(struct rte_eth_dev *dev)
212 {
213         struct axgbe_port *pdata = dev->data->dev_private;
214
215         PMD_INIT_FUNC_TRACE();
216
217         rte_intr_disable(&pdata->pci_dev->intr_handle);
218
219         if (axgbe_test_bit(AXGBE_STOPPED, &pdata->dev_state))
220                 return;
221
222         axgbe_set_bit(AXGBE_STOPPED, &pdata->dev_state);
223         axgbe_dev_disable_tx(dev);
224         axgbe_dev_disable_rx(dev);
225
226         pdata->phy_if.phy_stop(pdata);
227         pdata->hw_if.exit(pdata);
228         memset(&dev->data->dev_link, 0, sizeof(struct rte_eth_link));
229         axgbe_set_bit(AXGBE_DOWN, &pdata->dev_state);
230 }
231
232 /* Clear all resources like TX/RX queues. */
233 static void
234 axgbe_dev_close(struct rte_eth_dev *dev)
235 {
236         axgbe_dev_clear_queues(dev);
237 }
238
239 static int
240 axgbe_dev_promiscuous_enable(struct rte_eth_dev *dev)
241 {
242         struct axgbe_port *pdata = dev->data->dev_private;
243
244         PMD_INIT_FUNC_TRACE();
245
246         AXGMAC_IOWRITE_BITS(pdata, MAC_PFR, PR, 1);
247
248         return 0;
249 }
250
251 static int
252 axgbe_dev_promiscuous_disable(struct rte_eth_dev *dev)
253 {
254         struct axgbe_port *pdata = dev->data->dev_private;
255
256         PMD_INIT_FUNC_TRACE();
257
258         AXGMAC_IOWRITE_BITS(pdata, MAC_PFR, PR, 0);
259
260         return 0;
261 }
262
263 static void
264 axgbe_dev_allmulticast_enable(struct rte_eth_dev *dev)
265 {
266         struct axgbe_port *pdata = dev->data->dev_private;
267
268         PMD_INIT_FUNC_TRACE();
269
270         if (AXGMAC_IOREAD_BITS(pdata, MAC_PFR, PM))
271                 return;
272         AXGMAC_IOWRITE_BITS(pdata, MAC_PFR, PM, 1);
273 }
274
275 static void
276 axgbe_dev_allmulticast_disable(struct rte_eth_dev *dev)
277 {
278         struct axgbe_port *pdata = dev->data->dev_private;
279
280         PMD_INIT_FUNC_TRACE();
281
282         if (!AXGMAC_IOREAD_BITS(pdata, MAC_PFR, PM))
283                 return;
284         AXGMAC_IOWRITE_BITS(pdata, MAC_PFR, PM, 0);
285 }
286
287 /* return 0 means link status changed, -1 means not changed */
288 static int
289 axgbe_dev_link_update(struct rte_eth_dev *dev,
290                       int wait_to_complete __rte_unused)
291 {
292         struct axgbe_port *pdata = dev->data->dev_private;
293         struct rte_eth_link link;
294         int ret = 0;
295
296         PMD_INIT_FUNC_TRACE();
297         rte_delay_ms(800);
298
299         pdata->phy_if.phy_status(pdata);
300
301         memset(&link, 0, sizeof(struct rte_eth_link));
302         link.link_duplex = pdata->phy.duplex;
303         link.link_status = pdata->phy_link;
304         link.link_speed = pdata->phy_speed;
305         link.link_autoneg = !(dev->data->dev_conf.link_speeds &
306                               ETH_LINK_SPEED_FIXED);
307         ret = rte_eth_linkstatus_set(dev, &link);
308         if (ret == -1)
309                 PMD_DRV_LOG(ERR, "No change in link status\n");
310
311         return ret;
312 }
313
314 static int
315 axgbe_dev_stats_get(struct rte_eth_dev *dev,
316                     struct rte_eth_stats *stats)
317 {
318         struct axgbe_rx_queue *rxq;
319         struct axgbe_tx_queue *txq;
320         unsigned int i;
321
322         for (i = 0; i < dev->data->nb_rx_queues; i++) {
323                 rxq = dev->data->rx_queues[i];
324                 stats->q_ipackets[i] = rxq->pkts;
325                 stats->ipackets += rxq->pkts;
326                 stats->q_ibytes[i] = rxq->bytes;
327                 stats->ibytes += rxq->bytes;
328         }
329         for (i = 0; i < dev->data->nb_tx_queues; i++) {
330                 txq = dev->data->tx_queues[i];
331                 stats->q_opackets[i] = txq->pkts;
332                 stats->opackets += txq->pkts;
333                 stats->q_obytes[i] = txq->bytes;
334                 stats->obytes += txq->bytes;
335         }
336
337         return 0;
338 }
339
340 static int
341 axgbe_dev_stats_reset(struct rte_eth_dev *dev)
342 {
343         struct axgbe_rx_queue *rxq;
344         struct axgbe_tx_queue *txq;
345         unsigned int i;
346
347         for (i = 0; i < dev->data->nb_rx_queues; i++) {
348                 rxq = dev->data->rx_queues[i];
349                 rxq->pkts = 0;
350                 rxq->bytes = 0;
351                 rxq->errors = 0;
352         }
353         for (i = 0; i < dev->data->nb_tx_queues; i++) {
354                 txq = dev->data->tx_queues[i];
355                 txq->pkts = 0;
356                 txq->bytes = 0;
357                 txq->errors = 0;
358         }
359
360         return 0;
361 }
362
363 static int
364 axgbe_dev_info_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *dev_info)
365 {
366         struct axgbe_port *pdata = dev->data->dev_private;
367
368         dev_info->max_rx_queues = pdata->rx_ring_count;
369         dev_info->max_tx_queues = pdata->tx_ring_count;
370         dev_info->min_rx_bufsize = AXGBE_RX_MIN_BUF_SIZE;
371         dev_info->max_rx_pktlen = AXGBE_RX_MAX_BUF_SIZE;
372         dev_info->max_mac_addrs = AXGBE_MAX_MAC_ADDRS;
373         dev_info->speed_capa =  ETH_LINK_SPEED_10G;
374
375         dev_info->rx_offload_capa =
376                 DEV_RX_OFFLOAD_IPV4_CKSUM |
377                 DEV_RX_OFFLOAD_UDP_CKSUM  |
378                 DEV_RX_OFFLOAD_TCP_CKSUM  |
379                 DEV_RX_OFFLOAD_KEEP_CRC;
380
381         dev_info->tx_offload_capa =
382                 DEV_TX_OFFLOAD_IPV4_CKSUM  |
383                 DEV_TX_OFFLOAD_UDP_CKSUM   |
384                 DEV_TX_OFFLOAD_TCP_CKSUM;
385
386         if (pdata->hw_feat.rss) {
387                 dev_info->flow_type_rss_offloads = AXGBE_RSS_OFFLOAD;
388                 dev_info->reta_size = pdata->hw_feat.hash_table_size;
389                 dev_info->hash_key_size =  AXGBE_RSS_HASH_KEY_SIZE;
390         }
391
392         dev_info->rx_desc_lim = rx_desc_lim;
393         dev_info->tx_desc_lim = tx_desc_lim;
394
395         dev_info->default_rxconf = (struct rte_eth_rxconf) {
396                 .rx_free_thresh = AXGBE_RX_FREE_THRESH,
397         };
398
399         dev_info->default_txconf = (struct rte_eth_txconf) {
400                 .tx_free_thresh = AXGBE_TX_FREE_THRESH,
401         };
402
403         return 0;
404 }
405
406 static void axgbe_get_all_hw_features(struct axgbe_port *pdata)
407 {
408         unsigned int mac_hfr0, mac_hfr1, mac_hfr2;
409         struct axgbe_hw_features *hw_feat = &pdata->hw_feat;
410
411         mac_hfr0 = AXGMAC_IOREAD(pdata, MAC_HWF0R);
412         mac_hfr1 = AXGMAC_IOREAD(pdata, MAC_HWF1R);
413         mac_hfr2 = AXGMAC_IOREAD(pdata, MAC_HWF2R);
414
415         memset(hw_feat, 0, sizeof(*hw_feat));
416
417         hw_feat->version = AXGMAC_IOREAD(pdata, MAC_VR);
418
419         /* Hardware feature register 0 */
420         hw_feat->gmii        = AXGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, GMIISEL);
421         hw_feat->vlhash      = AXGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, VLHASH);
422         hw_feat->sma         = AXGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, SMASEL);
423         hw_feat->rwk         = AXGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, RWKSEL);
424         hw_feat->mgk         = AXGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, MGKSEL);
425         hw_feat->mmc         = AXGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, MMCSEL);
426         hw_feat->aoe         = AXGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, ARPOFFSEL);
427         hw_feat->ts          = AXGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, TSSEL);
428         hw_feat->eee         = AXGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, EEESEL);
429         hw_feat->tx_coe      = AXGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, TXCOESEL);
430         hw_feat->rx_coe      = AXGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, RXCOESEL);
431         hw_feat->addn_mac    = AXGMAC_GET_BITS(mac_hfr0, MAC_HWF0R,
432                                               ADDMACADRSEL);
433         hw_feat->ts_src      = AXGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, TSSTSSEL);
434         hw_feat->sa_vlan_ins = AXGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, SAVLANINS);
435
436         /* Hardware feature register 1 */
437         hw_feat->rx_fifo_size  = AXGMAC_GET_BITS(mac_hfr1, MAC_HWF1R,
438                                                 RXFIFOSIZE);
439         hw_feat->tx_fifo_size  = AXGMAC_GET_BITS(mac_hfr1, MAC_HWF1R,
440                                                 TXFIFOSIZE);
441         hw_feat->adv_ts_hi     = AXGMAC_GET_BITS(mac_hfr1,
442                                                  MAC_HWF1R, ADVTHWORD);
443         hw_feat->dma_width     = AXGMAC_GET_BITS(mac_hfr1, MAC_HWF1R, ADDR64);
444         hw_feat->dcb           = AXGMAC_GET_BITS(mac_hfr1, MAC_HWF1R, DCBEN);
445         hw_feat->sph           = AXGMAC_GET_BITS(mac_hfr1, MAC_HWF1R, SPHEN);
446         hw_feat->tso           = AXGMAC_GET_BITS(mac_hfr1, MAC_HWF1R, TSOEN);
447         hw_feat->dma_debug     = AXGMAC_GET_BITS(mac_hfr1, MAC_HWF1R, DBGMEMA);
448         hw_feat->rss           = AXGMAC_GET_BITS(mac_hfr1, MAC_HWF1R, RSSEN);
449         hw_feat->tc_cnt        = AXGMAC_GET_BITS(mac_hfr1, MAC_HWF1R, NUMTC);
450         hw_feat->hash_table_size = AXGMAC_GET_BITS(mac_hfr1, MAC_HWF1R,
451                                                   HASHTBLSZ);
452         hw_feat->l3l4_filter_num = AXGMAC_GET_BITS(mac_hfr1, MAC_HWF1R,
453                                                   L3L4FNUM);
454
455         /* Hardware feature register 2 */
456         hw_feat->rx_q_cnt     = AXGMAC_GET_BITS(mac_hfr2, MAC_HWF2R, RXQCNT);
457         hw_feat->tx_q_cnt     = AXGMAC_GET_BITS(mac_hfr2, MAC_HWF2R, TXQCNT);
458         hw_feat->rx_ch_cnt    = AXGMAC_GET_BITS(mac_hfr2, MAC_HWF2R, RXCHCNT);
459         hw_feat->tx_ch_cnt    = AXGMAC_GET_BITS(mac_hfr2, MAC_HWF2R, TXCHCNT);
460         hw_feat->pps_out_num  = AXGMAC_GET_BITS(mac_hfr2, MAC_HWF2R, PPSOUTNUM);
461         hw_feat->aux_snap_num = AXGMAC_GET_BITS(mac_hfr2, MAC_HWF2R,
462                                                 AUXSNAPNUM);
463
464         /* Translate the Hash Table size into actual number */
465         switch (hw_feat->hash_table_size) {
466         case 0:
467                 break;
468         case 1:
469                 hw_feat->hash_table_size = 64;
470                 break;
471         case 2:
472                 hw_feat->hash_table_size = 128;
473                 break;
474         case 3:
475                 hw_feat->hash_table_size = 256;
476                 break;
477         }
478
479         /* Translate the address width setting into actual number */
480         switch (hw_feat->dma_width) {
481         case 0:
482                 hw_feat->dma_width = 32;
483                 break;
484         case 1:
485                 hw_feat->dma_width = 40;
486                 break;
487         case 2:
488                 hw_feat->dma_width = 48;
489                 break;
490         default:
491                 hw_feat->dma_width = 32;
492         }
493
494         /* The Queue, Channel and TC counts are zero based so increment them
495          * to get the actual number
496          */
497         hw_feat->rx_q_cnt++;
498         hw_feat->tx_q_cnt++;
499         hw_feat->rx_ch_cnt++;
500         hw_feat->tx_ch_cnt++;
501         hw_feat->tc_cnt++;
502
503         /* Translate the fifo sizes into actual numbers */
504         hw_feat->rx_fifo_size = 1 << (hw_feat->rx_fifo_size + 7);
505         hw_feat->tx_fifo_size = 1 << (hw_feat->tx_fifo_size + 7);
506 }
507
508 static void axgbe_init_all_fptrs(struct axgbe_port *pdata)
509 {
510         axgbe_init_function_ptrs_dev(&pdata->hw_if);
511         axgbe_init_function_ptrs_phy(&pdata->phy_if);
512         axgbe_init_function_ptrs_i2c(&pdata->i2c_if);
513         pdata->vdata->init_function_ptrs_phy_impl(&pdata->phy_if);
514 }
515
516 static void axgbe_set_counts(struct axgbe_port *pdata)
517 {
518         /* Set all the function pointers */
519         axgbe_init_all_fptrs(pdata);
520
521         /* Populate the hardware features */
522         axgbe_get_all_hw_features(pdata);
523
524         /* Set default max values if not provided */
525         if (!pdata->tx_max_channel_count)
526                 pdata->tx_max_channel_count = pdata->hw_feat.tx_ch_cnt;
527         if (!pdata->rx_max_channel_count)
528                 pdata->rx_max_channel_count = pdata->hw_feat.rx_ch_cnt;
529
530         if (!pdata->tx_max_q_count)
531                 pdata->tx_max_q_count = pdata->hw_feat.tx_q_cnt;
532         if (!pdata->rx_max_q_count)
533                 pdata->rx_max_q_count = pdata->hw_feat.rx_q_cnt;
534
535         /* Calculate the number of Tx and Rx rings to be created
536          *  -Tx (DMA) Channels map 1-to-1 to Tx Queues so set
537          *   the number of Tx queues to the number of Tx channels
538          *   enabled
539          *  -Rx (DMA) Channels do not map 1-to-1 so use the actual
540          *   number of Rx queues or maximum allowed
541          */
542         pdata->tx_ring_count = RTE_MIN(pdata->hw_feat.tx_ch_cnt,
543                                      pdata->tx_max_channel_count);
544         pdata->tx_ring_count = RTE_MIN(pdata->tx_ring_count,
545                                      pdata->tx_max_q_count);
546
547         pdata->tx_q_count = pdata->tx_ring_count;
548
549         pdata->rx_ring_count = RTE_MIN(pdata->hw_feat.rx_ch_cnt,
550                                      pdata->rx_max_channel_count);
551
552         pdata->rx_q_count = RTE_MIN(pdata->hw_feat.rx_q_cnt,
553                                   pdata->rx_max_q_count);
554 }
555
556 static void axgbe_default_config(struct axgbe_port *pdata)
557 {
558         pdata->pblx8 = DMA_PBL_X8_ENABLE;
559         pdata->tx_sf_mode = MTL_TSF_ENABLE;
560         pdata->tx_threshold = MTL_TX_THRESHOLD_64;
561         pdata->tx_pbl = DMA_PBL_32;
562         pdata->tx_osp_mode = DMA_OSP_ENABLE;
563         pdata->rx_sf_mode = MTL_RSF_ENABLE;
564         pdata->rx_threshold = MTL_RX_THRESHOLD_64;
565         pdata->rx_pbl = DMA_PBL_32;
566         pdata->pause_autoneg = 1;
567         pdata->tx_pause = 0;
568         pdata->rx_pause = 0;
569         pdata->phy_speed = SPEED_UNKNOWN;
570         pdata->power_down = 0;
571 }
572
573 /*
574  * It returns 0 on success.
575  */
576 static int
577 eth_axgbe_dev_init(struct rte_eth_dev *eth_dev)
578 {
579         PMD_INIT_FUNC_TRACE();
580         struct axgbe_port *pdata;
581         struct rte_pci_device *pci_dev;
582         uint32_t reg, mac_lo, mac_hi;
583         int ret;
584
585         eth_dev->dev_ops = &axgbe_eth_dev_ops;
586         eth_dev->rx_pkt_burst = &axgbe_recv_pkts;
587
588         /*
589          * For secondary processes, we don't initialise any further as primary
590          * has already done this work.
591          */
592         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
593                 return 0;
594
595         pdata = eth_dev->data->dev_private;
596         /* initial state */
597         axgbe_set_bit(AXGBE_DOWN, &pdata->dev_state);
598         axgbe_set_bit(AXGBE_STOPPED, &pdata->dev_state);
599         pdata->eth_dev = eth_dev;
600
601         pci_dev = RTE_DEV_TO_PCI(eth_dev->device);
602         pdata->pci_dev = pci_dev;
603
604         pdata->xgmac_regs =
605                 (void *)pci_dev->mem_resource[AXGBE_AXGMAC_BAR].addr;
606         pdata->xprop_regs = (void *)((uint8_t *)pdata->xgmac_regs
607                                      + AXGBE_MAC_PROP_OFFSET);
608         pdata->xi2c_regs = (void *)((uint8_t *)pdata->xgmac_regs
609                                     + AXGBE_I2C_CTRL_OFFSET);
610         pdata->xpcs_regs = (void *)pci_dev->mem_resource[AXGBE_XPCS_BAR].addr;
611
612         /* version specific driver data*/
613         if (pci_dev->id.device_id == AMD_PCI_AXGBE_DEVICE_V2A)
614                 pdata->vdata = &axgbe_v2a;
615         else
616                 pdata->vdata = &axgbe_v2b;
617
618         /* Configure the PCS indirect addressing support */
619         reg = XPCS32_IOREAD(pdata, PCS_V2_WINDOW_DEF);
620         pdata->xpcs_window = XPCS_GET_BITS(reg, PCS_V2_WINDOW_DEF, OFFSET);
621         pdata->xpcs_window <<= 6;
622         pdata->xpcs_window_size = XPCS_GET_BITS(reg, PCS_V2_WINDOW_DEF, SIZE);
623         pdata->xpcs_window_size = 1 << (pdata->xpcs_window_size + 7);
624         pdata->xpcs_window_mask = pdata->xpcs_window_size - 1;
625         pdata->xpcs_window_def_reg = PCS_V2_WINDOW_DEF;
626         pdata->xpcs_window_sel_reg = PCS_V2_WINDOW_SELECT;
627         PMD_INIT_LOG(DEBUG,
628                      "xpcs window :%x, size :%x, mask :%x ", pdata->xpcs_window,
629                      pdata->xpcs_window_size, pdata->xpcs_window_mask);
630         XP_IOWRITE(pdata, XP_INT_EN, 0x1fffff);
631
632         /* Retrieve the MAC address */
633         mac_lo = XP_IOREAD(pdata, XP_MAC_ADDR_LO);
634         mac_hi = XP_IOREAD(pdata, XP_MAC_ADDR_HI);
635         pdata->mac_addr.addr_bytes[0] = mac_lo & 0xff;
636         pdata->mac_addr.addr_bytes[1] = (mac_lo >> 8) & 0xff;
637         pdata->mac_addr.addr_bytes[2] = (mac_lo >> 16) & 0xff;
638         pdata->mac_addr.addr_bytes[3] = (mac_lo >> 24) & 0xff;
639         pdata->mac_addr.addr_bytes[4] = mac_hi & 0xff;
640         pdata->mac_addr.addr_bytes[5] = (mac_hi >> 8)  &  0xff;
641
642         eth_dev->data->mac_addrs = rte_zmalloc("axgbe_mac_addr",
643                                                RTE_ETHER_ADDR_LEN, 0);
644         if (!eth_dev->data->mac_addrs) {
645                 PMD_INIT_LOG(ERR,
646                              "Failed to alloc %u bytes needed to store MAC addr tbl",
647                              RTE_ETHER_ADDR_LEN);
648                 return -ENOMEM;
649         }
650
651         if (!rte_is_valid_assigned_ether_addr(&pdata->mac_addr))
652                 rte_eth_random_addr(pdata->mac_addr.addr_bytes);
653
654         /* Copy the permanent MAC address */
655         rte_ether_addr_copy(&pdata->mac_addr, &eth_dev->data->mac_addrs[0]);
656
657         /* Clock settings */
658         pdata->sysclk_rate = AXGBE_V2_DMA_CLOCK_FREQ;
659         pdata->ptpclk_rate = AXGBE_V2_PTP_CLOCK_FREQ;
660
661         /* Set the DMA coherency values */
662         pdata->coherent = 1;
663         pdata->axdomain = AXGBE_DMA_OS_AXDOMAIN;
664         pdata->arcache = AXGBE_DMA_OS_ARCACHE;
665         pdata->awcache = AXGBE_DMA_OS_AWCACHE;
666
667         /* Set the maximum channels and queues */
668         reg = XP_IOREAD(pdata, XP_PROP_1);
669         pdata->tx_max_channel_count = XP_GET_BITS(reg, XP_PROP_1, MAX_TX_DMA);
670         pdata->rx_max_channel_count = XP_GET_BITS(reg, XP_PROP_1, MAX_RX_DMA);
671         pdata->tx_max_q_count = XP_GET_BITS(reg, XP_PROP_1, MAX_TX_QUEUES);
672         pdata->rx_max_q_count = XP_GET_BITS(reg, XP_PROP_1, MAX_RX_QUEUES);
673
674         /* Set the hardware channel and queue counts */
675         axgbe_set_counts(pdata);
676
677         /* Set the maximum fifo amounts */
678         reg = XP_IOREAD(pdata, XP_PROP_2);
679         pdata->tx_max_fifo_size = XP_GET_BITS(reg, XP_PROP_2, TX_FIFO_SIZE);
680         pdata->tx_max_fifo_size *= 16384;
681         pdata->tx_max_fifo_size = RTE_MIN(pdata->tx_max_fifo_size,
682                                           pdata->vdata->tx_max_fifo_size);
683         pdata->rx_max_fifo_size = XP_GET_BITS(reg, XP_PROP_2, RX_FIFO_SIZE);
684         pdata->rx_max_fifo_size *= 16384;
685         pdata->rx_max_fifo_size = RTE_MIN(pdata->rx_max_fifo_size,
686                                           pdata->vdata->rx_max_fifo_size);
687         /* Issue software reset to DMA */
688         ret = pdata->hw_if.exit(pdata);
689         if (ret)
690                 PMD_DRV_LOG(ERR, "hw_if->exit EBUSY error\n");
691
692         /* Set default configuration data */
693         axgbe_default_config(pdata);
694
695         /* Set default max values if not provided */
696         if (!pdata->tx_max_fifo_size)
697                 pdata->tx_max_fifo_size = pdata->hw_feat.tx_fifo_size;
698         if (!pdata->rx_max_fifo_size)
699                 pdata->rx_max_fifo_size = pdata->hw_feat.rx_fifo_size;
700
701         pdata->tx_desc_count = AXGBE_MAX_RING_DESC;
702         pdata->rx_desc_count = AXGBE_MAX_RING_DESC;
703         pthread_mutex_init(&pdata->xpcs_mutex, NULL);
704         pthread_mutex_init(&pdata->i2c_mutex, NULL);
705         pthread_mutex_init(&pdata->an_mutex, NULL);
706         pthread_mutex_init(&pdata->phy_mutex, NULL);
707
708         ret = pdata->phy_if.phy_init(pdata);
709         if (ret) {
710                 rte_free(eth_dev->data->mac_addrs);
711                 eth_dev->data->mac_addrs = NULL;
712                 return ret;
713         }
714
715         rte_intr_callback_register(&pci_dev->intr_handle,
716                                    axgbe_dev_interrupt_handler,
717                                    (void *)eth_dev);
718         PMD_INIT_LOG(DEBUG, "port %d vendorID=0x%x deviceID=0x%x",
719                      eth_dev->data->port_id, pci_dev->id.vendor_id,
720                      pci_dev->id.device_id);
721
722         return 0;
723 }
724
725 static int
726 eth_axgbe_dev_uninit(struct rte_eth_dev *eth_dev)
727 {
728         struct rte_pci_device *pci_dev;
729
730         PMD_INIT_FUNC_TRACE();
731
732         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
733                 return 0;
734
735         pci_dev = RTE_DEV_TO_PCI(eth_dev->device);
736         eth_dev->dev_ops = NULL;
737         eth_dev->rx_pkt_burst = NULL;
738         eth_dev->tx_pkt_burst = NULL;
739         axgbe_dev_clear_queues(eth_dev);
740
741         /* disable uio intr before callback unregister */
742         rte_intr_disable(&pci_dev->intr_handle);
743         rte_intr_callback_unregister(&pci_dev->intr_handle,
744                                      axgbe_dev_interrupt_handler,
745                                      (void *)eth_dev);
746
747         return 0;
748 }
749
750 static int eth_axgbe_pci_probe(struct rte_pci_driver *pci_drv __rte_unused,
751         struct rte_pci_device *pci_dev)
752 {
753         return rte_eth_dev_pci_generic_probe(pci_dev,
754                 sizeof(struct axgbe_port), eth_axgbe_dev_init);
755 }
756
757 static int eth_axgbe_pci_remove(struct rte_pci_device *pci_dev)
758 {
759         return rte_eth_dev_pci_generic_remove(pci_dev, eth_axgbe_dev_uninit);
760 }
761
762 static struct rte_pci_driver rte_axgbe_pmd = {
763         .id_table = pci_id_axgbe_map,
764         .drv_flags = RTE_PCI_DRV_NEED_MAPPING,
765         .probe = eth_axgbe_pci_probe,
766         .remove = eth_axgbe_pci_remove,
767 };
768
769 RTE_PMD_REGISTER_PCI(net_axgbe, rte_axgbe_pmd);
770 RTE_PMD_REGISTER_PCI_TABLE(net_axgbe, pci_id_axgbe_map);
771 RTE_PMD_REGISTER_KMOD_DEP(net_axgbe, "* igb_uio | uio_pci_generic | vfio-pci");
772
773 RTE_INIT(axgbe_init_log)
774 {
775         axgbe_logtype_init = rte_log_register("pmd.net.axgbe.init");
776         if (axgbe_logtype_init >= 0)
777                 rte_log_set_level(axgbe_logtype_init, RTE_LOG_NOTICE);
778         axgbe_logtype_driver = rte_log_register("pmd.net.axgbe.driver");
779         if (axgbe_logtype_driver >= 0)
780                 rte_log_set_level(axgbe_logtype_driver, RTE_LOG_NOTICE);
781 }