325c93b92aa91dc66b3fea6683898c8dbe5cc6c9
[dpdk.git] / drivers / net / enetc / enetc_ethdev.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright 2018-2020 NXP
3  */
4
5 #include <stdbool.h>
6 #include <rte_ethdev_pci.h>
7 #include <rte_random.h>
8 #include <dpaax_iova_table.h>
9
10 #include "enetc_logs.h"
11 #include "enetc.h"
12
13 static int
14 enetc_dev_start(struct rte_eth_dev *dev)
15 {
16         struct enetc_eth_hw *hw =
17                 ENETC_DEV_PRIVATE_TO_HW(dev->data->dev_private);
18         struct enetc_hw *enetc_hw = &hw->hw;
19         uint32_t val;
20
21         PMD_INIT_FUNC_TRACE();
22         val = enetc_port_rd(enetc_hw, ENETC_PM0_CMD_CFG);
23         enetc_port_wr(enetc_hw, ENETC_PM0_CMD_CFG,
24                       val | ENETC_PM0_TX_EN | ENETC_PM0_RX_EN);
25
26         /* Enable port */
27         val = enetc_port_rd(enetc_hw, ENETC_PMR);
28         enetc_port_wr(enetc_hw, ENETC_PMR, val | ENETC_PMR_EN);
29
30         /* set auto-speed for RGMII */
31         if (enetc_port_rd(enetc_hw, ENETC_PM0_IF_MODE) & ENETC_PMO_IFM_RG) {
32                 enetc_port_wr(enetc_hw, ENETC_PM0_IF_MODE,
33                               ENETC_PM0_IFM_RGAUTO);
34                 enetc_port_wr(enetc_hw, ENETC_PM1_IF_MODE,
35                               ENETC_PM0_IFM_RGAUTO);
36         }
37         if (enetc_global_rd(enetc_hw,
38                             ENETC_G_EPFBLPR(1)) == ENETC_G_EPFBLPR1_XGMII) {
39                 enetc_port_wr(enetc_hw, ENETC_PM0_IF_MODE,
40                               ENETC_PM0_IFM_XGMII);
41                 enetc_port_wr(enetc_hw, ENETC_PM1_IF_MODE,
42                               ENETC_PM0_IFM_XGMII);
43         }
44
45         return 0;
46 }
47
48 static void
49 enetc_dev_stop(struct rte_eth_dev *dev)
50 {
51         struct enetc_eth_hw *hw =
52                 ENETC_DEV_PRIVATE_TO_HW(dev->data->dev_private);
53         struct enetc_hw *enetc_hw = &hw->hw;
54         uint32_t val;
55
56         PMD_INIT_FUNC_TRACE();
57         /* Disable port */
58         val = enetc_port_rd(enetc_hw, ENETC_PMR);
59         enetc_port_wr(enetc_hw, ENETC_PMR, val & (~ENETC_PMR_EN));
60
61         val = enetc_port_rd(enetc_hw, ENETC_PM0_CMD_CFG);
62         enetc_port_wr(enetc_hw, ENETC_PM0_CMD_CFG,
63                       val & (~(ENETC_PM0_TX_EN | ENETC_PM0_RX_EN)));
64 }
65
66 static const uint32_t *
67 enetc_supported_ptypes_get(struct rte_eth_dev *dev __rte_unused)
68 {
69         static const uint32_t ptypes[] = {
70                 RTE_PTYPE_L2_ETHER,
71                 RTE_PTYPE_L3_IPV4,
72                 RTE_PTYPE_L3_IPV6,
73                 RTE_PTYPE_L4_TCP,
74                 RTE_PTYPE_L4_UDP,
75                 RTE_PTYPE_L4_SCTP,
76                 RTE_PTYPE_L4_ICMP,
77                 RTE_PTYPE_UNKNOWN
78         };
79
80         return ptypes;
81 }
82
83 /* return 0 means link status changed, -1 means not changed */
84 static int
85 enetc_link_update(struct rte_eth_dev *dev, int wait_to_complete __rte_unused)
86 {
87         struct enetc_eth_hw *hw =
88                 ENETC_DEV_PRIVATE_TO_HW(dev->data->dev_private);
89         struct enetc_hw *enetc_hw = &hw->hw;
90         struct rte_eth_link link;
91         uint32_t status;
92
93         PMD_INIT_FUNC_TRACE();
94
95         memset(&link, 0, sizeof(link));
96
97         status = enetc_port_rd(enetc_hw, ENETC_PM0_STATUS);
98
99         if (status & ENETC_LINK_MODE)
100                 link.link_duplex = ETH_LINK_FULL_DUPLEX;
101         else
102                 link.link_duplex = ETH_LINK_HALF_DUPLEX;
103
104         if (status & ENETC_LINK_STATUS)
105                 link.link_status = ETH_LINK_UP;
106         else
107                 link.link_status = ETH_LINK_DOWN;
108
109         switch (status & ENETC_LINK_SPEED_MASK) {
110         case ENETC_LINK_SPEED_1G:
111                 link.link_speed = ETH_SPEED_NUM_1G;
112                 break;
113
114         case ENETC_LINK_SPEED_100M:
115                 link.link_speed = ETH_SPEED_NUM_100M;
116                 break;
117
118         default:
119         case ENETC_LINK_SPEED_10M:
120                 link.link_speed = ETH_SPEED_NUM_10M;
121         }
122
123         return rte_eth_linkstatus_set(dev, &link);
124 }
125
126 static void
127 print_ethaddr(const char *name, const struct rte_ether_addr *eth_addr)
128 {
129         char buf[RTE_ETHER_ADDR_FMT_SIZE];
130
131         rte_ether_format_addr(buf, RTE_ETHER_ADDR_FMT_SIZE, eth_addr);
132         ENETC_PMD_NOTICE("%s%s\n", name, buf);
133 }
134
135 static int
136 enetc_hardware_init(struct enetc_eth_hw *hw)
137 {
138         struct enetc_hw *enetc_hw = &hw->hw;
139         uint32_t *mac = (uint32_t *)hw->mac.addr;
140         uint32_t high_mac = 0;
141         uint16_t low_mac = 0;
142
143         PMD_INIT_FUNC_TRACE();
144         /* Calculating and storing the base HW addresses */
145         hw->hw.port = (void *)((size_t)hw->hw.reg + ENETC_PORT_BASE);
146         hw->hw.global = (void *)((size_t)hw->hw.reg + ENETC_GLOBAL_BASE);
147
148         /* WA for Rx lock-up HW erratum */
149         enetc_port_wr(enetc_hw, ENETC_PM0_RX_FIFO, 1);
150
151         /* set ENETC transaction flags to coherent, don't allocate.
152          * BD writes merge with surrounding cache line data, frame data writes
153          * overwrite cache line.
154          */
155         enetc_wr(enetc_hw, ENETC_SICAR0, ENETC_SICAR0_COHERENT);
156
157         /* Enabling Station Interface */
158         enetc_wr(enetc_hw, ENETC_SIMR, ENETC_SIMR_EN);
159
160         *mac = (uint32_t)enetc_port_rd(enetc_hw, ENETC_PSIPMAR0(0));
161         high_mac = (uint32_t)*mac;
162         mac++;
163         *mac = (uint16_t)enetc_port_rd(enetc_hw, ENETC_PSIPMAR1(0));
164         low_mac = (uint16_t)*mac;
165
166         if ((high_mac | low_mac) == 0) {
167                 char *first_byte;
168
169                 ENETC_PMD_NOTICE("MAC is not available for this SI, "
170                                 "set random MAC\n");
171                 mac = (uint32_t *)hw->mac.addr;
172                 *mac = (uint32_t)rte_rand();
173                 first_byte = (char *)mac;
174                 *first_byte &= 0xfe;    /* clear multicast bit */
175                 *first_byte |= 0x02;    /* set local assignment bit (IEEE802) */
176
177                 enetc_port_wr(enetc_hw, ENETC_PSIPMAR0(0), *mac);
178                 mac++;
179                 *mac = (uint16_t)rte_rand();
180                 enetc_port_wr(enetc_hw, ENETC_PSIPMAR1(0), *mac);
181                 print_ethaddr("New address: ",
182                               (const struct rte_ether_addr *)hw->mac.addr);
183         }
184
185         return 0;
186 }
187
188 static int
189 enetc_dev_infos_get(struct rte_eth_dev *dev __rte_unused,
190                     struct rte_eth_dev_info *dev_info)
191 {
192         PMD_INIT_FUNC_TRACE();
193         dev_info->rx_desc_lim = (struct rte_eth_desc_lim) {
194                 .nb_max = MAX_BD_COUNT,
195                 .nb_min = MIN_BD_COUNT,
196                 .nb_align = BD_ALIGN,
197         };
198         dev_info->tx_desc_lim = (struct rte_eth_desc_lim) {
199                 .nb_max = MAX_BD_COUNT,
200                 .nb_min = MIN_BD_COUNT,
201                 .nb_align = BD_ALIGN,
202         };
203         dev_info->max_rx_queues = MAX_RX_RINGS;
204         dev_info->max_tx_queues = MAX_TX_RINGS;
205         dev_info->max_rx_pktlen = ENETC_MAC_MAXFRM_SIZE;
206         dev_info->rx_offload_capa =
207                 (DEV_RX_OFFLOAD_IPV4_CKSUM |
208                  DEV_RX_OFFLOAD_UDP_CKSUM |
209                  DEV_RX_OFFLOAD_TCP_CKSUM |
210                  DEV_RX_OFFLOAD_KEEP_CRC |
211                  DEV_RX_OFFLOAD_JUMBO_FRAME);
212
213         return 0;
214 }
215
216 static int
217 enetc_alloc_txbdr(struct enetc_bdr *txr, uint16_t nb_desc)
218 {
219         int size;
220
221         size = nb_desc * sizeof(struct enetc_swbd);
222         txr->q_swbd = rte_malloc(NULL, size, ENETC_BD_RING_ALIGN);
223         if (txr->q_swbd == NULL)
224                 return -ENOMEM;
225
226         size = nb_desc * sizeof(struct enetc_tx_bd);
227         txr->bd_base = rte_malloc(NULL, size, ENETC_BD_RING_ALIGN);
228         if (txr->bd_base == NULL) {
229                 rte_free(txr->q_swbd);
230                 txr->q_swbd = NULL;
231                 return -ENOMEM;
232         }
233
234         txr->bd_count = nb_desc;
235         txr->next_to_clean = 0;
236         txr->next_to_use = 0;
237
238         return 0;
239 }
240
241 static void
242 enetc_free_bdr(struct enetc_bdr *rxr)
243 {
244         rte_free(rxr->q_swbd);
245         rte_free(rxr->bd_base);
246         rxr->q_swbd = NULL;
247         rxr->bd_base = NULL;
248 }
249
250 static void
251 enetc_setup_txbdr(struct enetc_hw *hw, struct enetc_bdr *tx_ring)
252 {
253         int idx = tx_ring->index;
254         phys_addr_t bd_address;
255
256         bd_address = (phys_addr_t)
257                      rte_mem_virt2iova((const void *)tx_ring->bd_base);
258         enetc_txbdr_wr(hw, idx, ENETC_TBBAR0,
259                        lower_32_bits((uint64_t)bd_address));
260         enetc_txbdr_wr(hw, idx, ENETC_TBBAR1,
261                        upper_32_bits((uint64_t)bd_address));
262         enetc_txbdr_wr(hw, idx, ENETC_TBLENR,
263                        ENETC_RTBLENR_LEN(tx_ring->bd_count));
264
265         enetc_txbdr_wr(hw, idx, ENETC_TBCIR, 0);
266         enetc_txbdr_wr(hw, idx, ENETC_TBCISR, 0);
267         tx_ring->tcir = (void *)((size_t)hw->reg +
268                         ENETC_BDR(TX, idx, ENETC_TBCIR));
269         tx_ring->tcisr = (void *)((size_t)hw->reg +
270                          ENETC_BDR(TX, idx, ENETC_TBCISR));
271 }
272
273 static int
274 enetc_tx_queue_setup(struct rte_eth_dev *dev,
275                      uint16_t queue_idx,
276                      uint16_t nb_desc,
277                      unsigned int socket_id __rte_unused,
278                      const struct rte_eth_txconf *tx_conf)
279 {
280         int err = 0;
281         struct enetc_bdr *tx_ring;
282         struct rte_eth_dev_data *data = dev->data;
283         struct enetc_eth_adapter *priv =
284                         ENETC_DEV_PRIVATE(data->dev_private);
285
286         PMD_INIT_FUNC_TRACE();
287         if (nb_desc > MAX_BD_COUNT)
288                 return -1;
289
290         tx_ring = rte_zmalloc(NULL, sizeof(struct enetc_bdr), 0);
291         if (tx_ring == NULL) {
292                 ENETC_PMD_ERR("Failed to allocate TX ring memory");
293                 err = -ENOMEM;
294                 return -1;
295         }
296
297         err = enetc_alloc_txbdr(tx_ring, nb_desc);
298         if (err)
299                 goto fail;
300
301         tx_ring->index = queue_idx;
302         tx_ring->ndev = dev;
303         enetc_setup_txbdr(&priv->hw.hw, tx_ring);
304         data->tx_queues[queue_idx] = tx_ring;
305
306         if (!tx_conf->tx_deferred_start) {
307                 /* enable ring */
308                 enetc_txbdr_wr(&priv->hw.hw, tx_ring->index,
309                                ENETC_TBMR, ENETC_TBMR_EN);
310                 dev->data->tx_queue_state[tx_ring->index] =
311                                RTE_ETH_QUEUE_STATE_STARTED;
312         } else {
313                 dev->data->tx_queue_state[tx_ring->index] =
314                                RTE_ETH_QUEUE_STATE_STOPPED;
315         }
316
317         return 0;
318 fail:
319         rte_free(tx_ring);
320
321         return err;
322 }
323
324 static void
325 enetc_tx_queue_release(void *txq)
326 {
327         if (txq == NULL)
328                 return;
329
330         struct enetc_bdr *tx_ring = (struct enetc_bdr *)txq;
331         struct enetc_eth_hw *eth_hw =
332                 ENETC_DEV_PRIVATE_TO_HW(tx_ring->ndev->data->dev_private);
333         struct enetc_hw *hw;
334         struct enetc_swbd *tx_swbd;
335         int i;
336         uint32_t val;
337
338         /* Disable the ring */
339         hw = &eth_hw->hw;
340         val = enetc_txbdr_rd(hw, tx_ring->index, ENETC_TBMR);
341         val &= (~ENETC_TBMR_EN);
342         enetc_txbdr_wr(hw, tx_ring->index, ENETC_TBMR, val);
343
344         /* clean the ring*/
345         i = tx_ring->next_to_clean;
346         tx_swbd = &tx_ring->q_swbd[i];
347         while (tx_swbd->buffer_addr != NULL) {
348                 rte_pktmbuf_free(tx_swbd->buffer_addr);
349                 tx_swbd->buffer_addr = NULL;
350                 tx_swbd++;
351                 i++;
352                 if (unlikely(i == tx_ring->bd_count)) {
353                         i = 0;
354                         tx_swbd = &tx_ring->q_swbd[i];
355                 }
356         }
357
358         enetc_free_bdr(tx_ring);
359         rte_free(tx_ring);
360 }
361
362 static int
363 enetc_alloc_rxbdr(struct enetc_bdr *rxr,
364                   uint16_t nb_rx_desc)
365 {
366         int size;
367
368         size = nb_rx_desc * sizeof(struct enetc_swbd);
369         rxr->q_swbd = rte_malloc(NULL, size, ENETC_BD_RING_ALIGN);
370         if (rxr->q_swbd == NULL)
371                 return -ENOMEM;
372
373         size = nb_rx_desc * sizeof(union enetc_rx_bd);
374         rxr->bd_base = rte_malloc(NULL, size, ENETC_BD_RING_ALIGN);
375         if (rxr->bd_base == NULL) {
376                 rte_free(rxr->q_swbd);
377                 rxr->q_swbd = NULL;
378                 return -ENOMEM;
379         }
380
381         rxr->bd_count = nb_rx_desc;
382         rxr->next_to_clean = 0;
383         rxr->next_to_use = 0;
384         rxr->next_to_alloc = 0;
385
386         return 0;
387 }
388
389 static void
390 enetc_setup_rxbdr(struct enetc_hw *hw, struct enetc_bdr *rx_ring,
391                   struct rte_mempool *mb_pool)
392 {
393         int idx = rx_ring->index;
394         uint16_t buf_size;
395         phys_addr_t bd_address;
396
397         bd_address = (phys_addr_t)
398                      rte_mem_virt2iova((const void *)rx_ring->bd_base);
399         enetc_rxbdr_wr(hw, idx, ENETC_RBBAR0,
400                        lower_32_bits((uint64_t)bd_address));
401         enetc_rxbdr_wr(hw, idx, ENETC_RBBAR1,
402                        upper_32_bits((uint64_t)bd_address));
403         enetc_rxbdr_wr(hw, idx, ENETC_RBLENR,
404                        ENETC_RTBLENR_LEN(rx_ring->bd_count));
405
406         rx_ring->mb_pool = mb_pool;
407         rx_ring->rcir = (void *)((size_t)hw->reg +
408                         ENETC_BDR(RX, idx, ENETC_RBCIR));
409         enetc_refill_rx_ring(rx_ring, (enetc_bd_unused(rx_ring)));
410         buf_size = (uint16_t)(rte_pktmbuf_data_room_size(rx_ring->mb_pool) -
411                    RTE_PKTMBUF_HEADROOM);
412         enetc_rxbdr_wr(hw, idx, ENETC_RBBSR, buf_size);
413         enetc_rxbdr_wr(hw, idx, ENETC_RBPIR, 0);
414 }
415
416 static int
417 enetc_rx_queue_setup(struct rte_eth_dev *dev,
418                      uint16_t rx_queue_id,
419                      uint16_t nb_rx_desc,
420                      unsigned int socket_id __rte_unused,
421                      const struct rte_eth_rxconf *rx_conf,
422                      struct rte_mempool *mb_pool)
423 {
424         int err = 0;
425         struct enetc_bdr *rx_ring;
426         struct rte_eth_dev_data *data =  dev->data;
427         struct enetc_eth_adapter *adapter =
428                         ENETC_DEV_PRIVATE(data->dev_private);
429         uint64_t rx_offloads = data->dev_conf.rxmode.offloads;
430
431         PMD_INIT_FUNC_TRACE();
432         if (nb_rx_desc > MAX_BD_COUNT)
433                 return -1;
434
435         rx_ring = rte_zmalloc(NULL, sizeof(struct enetc_bdr), 0);
436         if (rx_ring == NULL) {
437                 ENETC_PMD_ERR("Failed to allocate RX ring memory");
438                 err = -ENOMEM;
439                 return err;
440         }
441
442         err = enetc_alloc_rxbdr(rx_ring, nb_rx_desc);
443         if (err)
444                 goto fail;
445
446         rx_ring->index = rx_queue_id;
447         rx_ring->ndev = dev;
448         enetc_setup_rxbdr(&adapter->hw.hw, rx_ring, mb_pool);
449         data->rx_queues[rx_queue_id] = rx_ring;
450
451         if (!rx_conf->rx_deferred_start) {
452                 /* enable ring */
453                 enetc_rxbdr_wr(&adapter->hw.hw, rx_ring->index, ENETC_RBMR,
454                                ENETC_RBMR_EN);
455                 dev->data->rx_queue_state[rx_ring->index] =
456                                RTE_ETH_QUEUE_STATE_STARTED;
457         } else {
458                 dev->data->rx_queue_state[rx_ring->index] =
459                                RTE_ETH_QUEUE_STATE_STOPPED;
460         }
461
462         rx_ring->crc_len = (uint8_t)((rx_offloads & DEV_RX_OFFLOAD_KEEP_CRC) ?
463                                      RTE_ETHER_CRC_LEN : 0);
464
465         return 0;
466 fail:
467         rte_free(rx_ring);
468
469         return err;
470 }
471
472 static void
473 enetc_rx_queue_release(void *rxq)
474 {
475         if (rxq == NULL)
476                 return;
477
478         struct enetc_bdr *rx_ring = (struct enetc_bdr *)rxq;
479         struct enetc_eth_hw *eth_hw =
480                 ENETC_DEV_PRIVATE_TO_HW(rx_ring->ndev->data->dev_private);
481         struct enetc_swbd *q_swbd;
482         struct enetc_hw *hw;
483         uint32_t val;
484         int i;
485
486         /* Disable the ring */
487         hw = &eth_hw->hw;
488         val = enetc_rxbdr_rd(hw, rx_ring->index, ENETC_RBMR);
489         val &= (~ENETC_RBMR_EN);
490         enetc_rxbdr_wr(hw, rx_ring->index, ENETC_RBMR, val);
491
492         /* Clean the ring */
493         i = rx_ring->next_to_clean;
494         q_swbd = &rx_ring->q_swbd[i];
495         while (i != rx_ring->next_to_use) {
496                 rte_pktmbuf_free(q_swbd->buffer_addr);
497                 q_swbd->buffer_addr = NULL;
498                 q_swbd++;
499                 i++;
500                 if (unlikely(i == rx_ring->bd_count)) {
501                         i = 0;
502                         q_swbd = &rx_ring->q_swbd[i];
503                 }
504         }
505
506         enetc_free_bdr(rx_ring);
507         rte_free(rx_ring);
508 }
509
510 static
511 int enetc_stats_get(struct rte_eth_dev *dev,
512                     struct rte_eth_stats *stats)
513 {
514         struct enetc_eth_hw *hw =
515                 ENETC_DEV_PRIVATE_TO_HW(dev->data->dev_private);
516         struct enetc_hw *enetc_hw = &hw->hw;
517
518         /* Total received packets, bad + good, if we want to get counters of
519          * only good received packets then use ENETC_PM0_RFRM,
520          * ENETC_PM0_TFRM registers.
521          */
522         stats->ipackets = enetc_port_rd(enetc_hw, ENETC_PM0_RPKT);
523         stats->opackets = enetc_port_rd(enetc_hw, ENETC_PM0_TPKT);
524         stats->ibytes =  enetc_port_rd(enetc_hw, ENETC_PM0_REOCT);
525         stats->obytes = enetc_port_rd(enetc_hw, ENETC_PM0_TEOCT);
526         /* Dropped + Truncated packets, use ENETC_PM0_RDRNTP for without
527          * truncated packets
528          */
529         stats->imissed = enetc_port_rd(enetc_hw, ENETC_PM0_RDRP);
530         stats->ierrors = enetc_port_rd(enetc_hw, ENETC_PM0_RERR);
531         stats->oerrors = enetc_port_rd(enetc_hw, ENETC_PM0_TERR);
532
533         return 0;
534 }
535
536 static int
537 enetc_stats_reset(struct rte_eth_dev *dev)
538 {
539         struct enetc_eth_hw *hw =
540                 ENETC_DEV_PRIVATE_TO_HW(dev->data->dev_private);
541         struct enetc_hw *enetc_hw = &hw->hw;
542
543         enetc_port_wr(enetc_hw, ENETC_PM0_STAT_CONFIG, ENETC_CLEAR_STATS);
544
545         return 0;
546 }
547
548 static int
549 enetc_dev_close(struct rte_eth_dev *dev)
550 {
551         uint16_t i;
552
553         PMD_INIT_FUNC_TRACE();
554         enetc_dev_stop(dev);
555
556         for (i = 0; i < dev->data->nb_rx_queues; i++) {
557                 enetc_rx_queue_release(dev->data->rx_queues[i]);
558                 dev->data->rx_queues[i] = NULL;
559         }
560         dev->data->nb_rx_queues = 0;
561
562         for (i = 0; i < dev->data->nb_tx_queues; i++) {
563                 enetc_tx_queue_release(dev->data->tx_queues[i]);
564                 dev->data->tx_queues[i] = NULL;
565         }
566         dev->data->nb_tx_queues = 0;
567
568         if (rte_eal_iova_mode() == RTE_IOVA_PA)
569                 dpaax_iova_table_depopulate();
570
571         return 0;
572 }
573
574 static int
575 enetc_promiscuous_enable(struct rte_eth_dev *dev)
576 {
577         struct enetc_eth_hw *hw =
578                 ENETC_DEV_PRIVATE_TO_HW(dev->data->dev_private);
579         struct enetc_hw *enetc_hw = &hw->hw;
580         uint32_t psipmr = 0;
581
582         psipmr = enetc_port_rd(enetc_hw, ENETC_PSIPMR);
583
584         /* Setting to enable promiscuous mode*/
585         psipmr |= ENETC_PSIPMR_SET_UP(0) | ENETC_PSIPMR_SET_MP(0);
586
587         enetc_port_wr(enetc_hw, ENETC_PSIPMR, psipmr);
588
589         return 0;
590 }
591
592 static int
593 enetc_promiscuous_disable(struct rte_eth_dev *dev)
594 {
595         struct enetc_eth_hw *hw =
596                 ENETC_DEV_PRIVATE_TO_HW(dev->data->dev_private);
597         struct enetc_hw *enetc_hw = &hw->hw;
598         uint32_t psipmr = 0;
599
600         /* Setting to disable promiscuous mode for SI0*/
601         psipmr = enetc_port_rd(enetc_hw, ENETC_PSIPMR);
602         psipmr &= (~ENETC_PSIPMR_SET_UP(0));
603
604         if (dev->data->all_multicast == 0)
605                 psipmr &= (~ENETC_PSIPMR_SET_MP(0));
606
607         enetc_port_wr(enetc_hw, ENETC_PSIPMR, psipmr);
608
609         return 0;
610 }
611
612 static int
613 enetc_allmulticast_enable(struct rte_eth_dev *dev)
614 {
615         struct enetc_eth_hw *hw =
616                 ENETC_DEV_PRIVATE_TO_HW(dev->data->dev_private);
617         struct enetc_hw *enetc_hw = &hw->hw;
618         uint32_t psipmr = 0;
619
620         psipmr = enetc_port_rd(enetc_hw, ENETC_PSIPMR);
621
622         /* Setting to enable allmulticast mode for SI0*/
623         psipmr |= ENETC_PSIPMR_SET_MP(0);
624
625         enetc_port_wr(enetc_hw, ENETC_PSIPMR, psipmr);
626
627         return 0;
628 }
629
630 static int
631 enetc_allmulticast_disable(struct rte_eth_dev *dev)
632 {
633         struct enetc_eth_hw *hw =
634                 ENETC_DEV_PRIVATE_TO_HW(dev->data->dev_private);
635         struct enetc_hw *enetc_hw = &hw->hw;
636         uint32_t psipmr = 0;
637
638         if (dev->data->promiscuous == 1)
639                 return 0; /* must remain in all_multicast mode */
640
641         /* Setting to disable all multicast mode for SI0*/
642         psipmr = enetc_port_rd(enetc_hw, ENETC_PSIPMR) &
643                                ~(ENETC_PSIPMR_SET_MP(0));
644
645         enetc_port_wr(enetc_hw, ENETC_PSIPMR, psipmr);
646
647         return 0;
648 }
649
650 static int
651 enetc_mtu_set(struct rte_eth_dev *dev, uint16_t mtu)
652 {
653         struct enetc_eth_hw *hw =
654                 ENETC_DEV_PRIVATE_TO_HW(dev->data->dev_private);
655         struct enetc_hw *enetc_hw = &hw->hw;
656         uint32_t frame_size = mtu + RTE_ETHER_HDR_LEN + RTE_ETHER_CRC_LEN;
657
658         /* check that mtu is within the allowed range */
659         if (mtu < ENETC_MAC_MINFRM_SIZE || frame_size > ENETC_MAC_MAXFRM_SIZE)
660                 return -EINVAL;
661
662         /*
663          * Refuse mtu that requires the support of scattered packets
664          * when this feature has not been enabled before.
665          */
666         if (dev->data->min_rx_buf_size &&
667                 !dev->data->scattered_rx && frame_size >
668                 dev->data->min_rx_buf_size - RTE_PKTMBUF_HEADROOM) {
669                 ENETC_PMD_ERR("SG not enabled, will not fit in one buffer");
670                 return -EINVAL;
671         }
672
673         if (frame_size > RTE_ETHER_MAX_LEN)
674                 dev->data->dev_conf.rxmode.offloads &=
675                                                 DEV_RX_OFFLOAD_JUMBO_FRAME;
676         else
677                 dev->data->dev_conf.rxmode.offloads &=
678                                                 ~DEV_RX_OFFLOAD_JUMBO_FRAME;
679
680         enetc_port_wr(enetc_hw, ENETC_PTCMSDUR(0), ENETC_MAC_MAXFRM_SIZE);
681         enetc_port_wr(enetc_hw, ENETC_PTXMBAR, 2 * ENETC_MAC_MAXFRM_SIZE);
682
683         dev->data->dev_conf.rxmode.max_rx_pkt_len = frame_size;
684
685         /*setting the MTU*/
686         enetc_port_wr(enetc_hw, ENETC_PM0_MAXFRM, ENETC_SET_MAXFRM(frame_size) |
687                       ENETC_SET_TX_MTU(ENETC_MAC_MAXFRM_SIZE));
688
689         return 0;
690 }
691
692 static int
693 enetc_dev_configure(struct rte_eth_dev *dev)
694 {
695         struct enetc_eth_hw *hw =
696                 ENETC_DEV_PRIVATE_TO_HW(dev->data->dev_private);
697         struct enetc_hw *enetc_hw = &hw->hw;
698         struct rte_eth_conf *eth_conf = &dev->data->dev_conf;
699         uint64_t rx_offloads = eth_conf->rxmode.offloads;
700         uint32_t checksum = L3_CKSUM | L4_CKSUM;
701
702         PMD_INIT_FUNC_TRACE();
703
704         if (rx_offloads & DEV_RX_OFFLOAD_JUMBO_FRAME) {
705                 uint32_t max_len;
706
707                 max_len = dev->data->dev_conf.rxmode.max_rx_pkt_len;
708
709                 enetc_port_wr(enetc_hw, ENETC_PM0_MAXFRM,
710                               ENETC_SET_MAXFRM(max_len));
711                 enetc_port_wr(enetc_hw, ENETC_PTCMSDUR(0),
712                               ENETC_MAC_MAXFRM_SIZE);
713                 enetc_port_wr(enetc_hw, ENETC_PTXMBAR,
714                               2 * ENETC_MAC_MAXFRM_SIZE);
715                 dev->data->mtu = RTE_ETHER_MAX_LEN - RTE_ETHER_HDR_LEN -
716                         RTE_ETHER_CRC_LEN;
717         }
718
719         if (rx_offloads & DEV_RX_OFFLOAD_KEEP_CRC) {
720                 int config;
721
722                 config = enetc_port_rd(enetc_hw, ENETC_PM0_CMD_CFG);
723                 config |= ENETC_PM0_CRC;
724                 enetc_port_wr(enetc_hw, ENETC_PM0_CMD_CFG, config);
725         }
726
727         if (rx_offloads & DEV_RX_OFFLOAD_IPV4_CKSUM)
728                 checksum &= ~L3_CKSUM;
729
730         if (rx_offloads & (DEV_RX_OFFLOAD_UDP_CKSUM | DEV_RX_OFFLOAD_TCP_CKSUM))
731                 checksum &= ~L4_CKSUM;
732
733         enetc_port_wr(enetc_hw, ENETC_PAR_PORT_CFG, checksum);
734
735
736         return 0;
737 }
738
739 static int
740 enetc_rx_queue_start(struct rte_eth_dev *dev, uint16_t qidx)
741 {
742         struct enetc_eth_adapter *priv =
743                         ENETC_DEV_PRIVATE(dev->data->dev_private);
744         struct enetc_bdr *rx_ring;
745         uint32_t rx_data;
746
747         rx_ring = dev->data->rx_queues[qidx];
748         if (dev->data->rx_queue_state[qidx] == RTE_ETH_QUEUE_STATE_STOPPED) {
749                 rx_data = enetc_rxbdr_rd(&priv->hw.hw, rx_ring->index,
750                                          ENETC_RBMR);
751                 rx_data = rx_data | ENETC_RBMR_EN;
752                 enetc_rxbdr_wr(&priv->hw.hw, rx_ring->index, ENETC_RBMR,
753                                rx_data);
754                 dev->data->rx_queue_state[qidx] = RTE_ETH_QUEUE_STATE_STARTED;
755         }
756
757         return 0;
758 }
759
760 static int
761 enetc_rx_queue_stop(struct rte_eth_dev *dev, uint16_t qidx)
762 {
763         struct enetc_eth_adapter *priv =
764                         ENETC_DEV_PRIVATE(dev->data->dev_private);
765         struct enetc_bdr *rx_ring;
766         uint32_t rx_data;
767
768         rx_ring = dev->data->rx_queues[qidx];
769         if (dev->data->rx_queue_state[qidx] == RTE_ETH_QUEUE_STATE_STARTED) {
770                 rx_data = enetc_rxbdr_rd(&priv->hw.hw, rx_ring->index,
771                                          ENETC_RBMR);
772                 rx_data = rx_data & (~ENETC_RBMR_EN);
773                 enetc_rxbdr_wr(&priv->hw.hw, rx_ring->index, ENETC_RBMR,
774                                rx_data);
775                 dev->data->rx_queue_state[qidx] = RTE_ETH_QUEUE_STATE_STOPPED;
776         }
777
778         return 0;
779 }
780
781 static int
782 enetc_tx_queue_start(struct rte_eth_dev *dev, uint16_t qidx)
783 {
784         struct enetc_eth_adapter *priv =
785                         ENETC_DEV_PRIVATE(dev->data->dev_private);
786         struct enetc_bdr *tx_ring;
787         uint32_t tx_data;
788
789         tx_ring = dev->data->tx_queues[qidx];
790         if (dev->data->tx_queue_state[qidx] == RTE_ETH_QUEUE_STATE_STOPPED) {
791                 tx_data = enetc_txbdr_rd(&priv->hw.hw, tx_ring->index,
792                                          ENETC_TBMR);
793                 tx_data = tx_data | ENETC_TBMR_EN;
794                 enetc_txbdr_wr(&priv->hw.hw, tx_ring->index, ENETC_TBMR,
795                                tx_data);
796                 dev->data->tx_queue_state[qidx] = RTE_ETH_QUEUE_STATE_STARTED;
797         }
798
799         return 0;
800 }
801
802 static int
803 enetc_tx_queue_stop(struct rte_eth_dev *dev, uint16_t qidx)
804 {
805         struct enetc_eth_adapter *priv =
806                         ENETC_DEV_PRIVATE(dev->data->dev_private);
807         struct enetc_bdr *tx_ring;
808         uint32_t tx_data;
809
810         tx_ring = dev->data->tx_queues[qidx];
811         if (dev->data->tx_queue_state[qidx] == RTE_ETH_QUEUE_STATE_STARTED) {
812                 tx_data = enetc_txbdr_rd(&priv->hw.hw, tx_ring->index,
813                                          ENETC_TBMR);
814                 tx_data = tx_data & (~ENETC_TBMR_EN);
815                 enetc_txbdr_wr(&priv->hw.hw, tx_ring->index, ENETC_TBMR,
816                                tx_data);
817                 dev->data->tx_queue_state[qidx] = RTE_ETH_QUEUE_STATE_STOPPED;
818         }
819
820         return 0;
821 }
822
823 /*
824  * The set of PCI devices this driver supports
825  */
826 static const struct rte_pci_id pci_id_enetc_map[] = {
827         { RTE_PCI_DEVICE(PCI_VENDOR_ID_FREESCALE, ENETC_DEV_ID) },
828         { RTE_PCI_DEVICE(PCI_VENDOR_ID_FREESCALE, ENETC_DEV_ID_VF) },
829         { .vendor_id = 0, /* sentinel */ },
830 };
831
832 /* Features supported by this driver */
833 static const struct eth_dev_ops enetc_ops = {
834         .dev_configure        = enetc_dev_configure,
835         .dev_start            = enetc_dev_start,
836         .dev_stop             = enetc_dev_stop,
837         .dev_close            = enetc_dev_close,
838         .link_update          = enetc_link_update,
839         .stats_get            = enetc_stats_get,
840         .stats_reset          = enetc_stats_reset,
841         .promiscuous_enable   = enetc_promiscuous_enable,
842         .promiscuous_disable  = enetc_promiscuous_disable,
843         .allmulticast_enable  = enetc_allmulticast_enable,
844         .allmulticast_disable = enetc_allmulticast_disable,
845         .dev_infos_get        = enetc_dev_infos_get,
846         .mtu_set              = enetc_mtu_set,
847         .rx_queue_setup       = enetc_rx_queue_setup,
848         .rx_queue_start       = enetc_rx_queue_start,
849         .rx_queue_stop        = enetc_rx_queue_stop,
850         .rx_queue_release     = enetc_rx_queue_release,
851         .tx_queue_setup       = enetc_tx_queue_setup,
852         .tx_queue_start       = enetc_tx_queue_start,
853         .tx_queue_stop        = enetc_tx_queue_stop,
854         .tx_queue_release     = enetc_tx_queue_release,
855         .dev_supported_ptypes_get = enetc_supported_ptypes_get,
856 };
857
858 /**
859  * Initialisation of the enetc device
860  *
861  * @param eth_dev
862  *   - Pointer to the structure rte_eth_dev
863  *
864  * @return
865  *   - On success, zero.
866  *   - On failure, negative value.
867  */
868 static int
869 enetc_dev_init(struct rte_eth_dev *eth_dev)
870 {
871         int error = 0;
872         struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(eth_dev);
873         struct enetc_eth_hw *hw =
874                 ENETC_DEV_PRIVATE_TO_HW(eth_dev->data->dev_private);
875
876         PMD_INIT_FUNC_TRACE();
877         eth_dev->dev_ops = &enetc_ops;
878         eth_dev->rx_pkt_burst = &enetc_recv_pkts;
879         eth_dev->tx_pkt_burst = &enetc_xmit_pkts;
880
881         /* Retrieving and storing the HW base address of device */
882         hw->hw.reg = (void *)pci_dev->mem_resource[0].addr;
883         hw->device_id = pci_dev->id.device_id;
884
885         error = enetc_hardware_init(hw);
886         if (error != 0) {
887                 ENETC_PMD_ERR("Hardware initialization failed");
888                 return -1;
889         }
890
891         /* Allocate memory for storing MAC addresses */
892         eth_dev->data->mac_addrs = rte_zmalloc("enetc_eth",
893                                         RTE_ETHER_ADDR_LEN, 0);
894         if (!eth_dev->data->mac_addrs) {
895                 ENETC_PMD_ERR("Failed to allocate %d bytes needed to "
896                               "store MAC addresses",
897                               RTE_ETHER_ADDR_LEN * 1);
898                 error = -ENOMEM;
899                 return -1;
900         }
901
902         /* Copy the permanent MAC address */
903         rte_ether_addr_copy((struct rte_ether_addr *)hw->mac.addr,
904                         &eth_dev->data->mac_addrs[0]);
905
906         /* Set MTU */
907         enetc_port_wr(&hw->hw, ENETC_PM0_MAXFRM,
908                       ENETC_SET_MAXFRM(RTE_ETHER_MAX_LEN));
909         eth_dev->data->mtu = RTE_ETHER_MAX_LEN - RTE_ETHER_HDR_LEN -
910                 RTE_ETHER_CRC_LEN;
911
912         if (rte_eal_iova_mode() == RTE_IOVA_PA)
913                 dpaax_iova_table_populate();
914
915         ENETC_PMD_DEBUG("port_id %d vendorID=0x%x deviceID=0x%x",
916                         eth_dev->data->port_id, pci_dev->id.vendor_id,
917                         pci_dev->id.device_id);
918         return 0;
919 }
920
921 static int
922 enetc_dev_uninit(struct rte_eth_dev *eth_dev)
923 {
924         PMD_INIT_FUNC_TRACE();
925
926         return enetc_dev_close(eth_dev);
927 }
928
929 static int
930 enetc_pci_probe(struct rte_pci_driver *pci_drv __rte_unused,
931                            struct rte_pci_device *pci_dev)
932 {
933         return rte_eth_dev_pci_generic_probe(pci_dev,
934                                              sizeof(struct enetc_eth_adapter),
935                                              enetc_dev_init);
936 }
937
938 static int
939 enetc_pci_remove(struct rte_pci_device *pci_dev)
940 {
941         return rte_eth_dev_pci_generic_remove(pci_dev, enetc_dev_uninit);
942 }
943
944 static struct rte_pci_driver rte_enetc_pmd = {
945         .id_table = pci_id_enetc_map,
946         .drv_flags = RTE_PCI_DRV_NEED_MAPPING,
947         .probe = enetc_pci_probe,
948         .remove = enetc_pci_remove,
949 };
950
951 RTE_PMD_REGISTER_PCI(net_enetc, rte_enetc_pmd);
952 RTE_PMD_REGISTER_PCI_TABLE(net_enetc, pci_id_enetc_map);
953 RTE_PMD_REGISTER_KMOD_DEP(net_enetc, "* vfio-pci");
954 RTE_LOG_REGISTER(enetc_logtype_pmd, pmd.net.enetc, NOTICE);