mbuf: rename data address helpers to IOVA
[dpdk.git] / drivers / net / mrvl / mrvl_ethdev.c
index 7023f5b..a897ba0 100644 (file)
@@ -1,7 +1,9 @@
 /*-
  *   BSD LICENSE
  *
- *   Copyright(c) 2017 Semihalf. All rights reserved.
+ *   Copyright(c) 2017 Marvell International Ltd.
+ *   Copyright(c) 2017 Semihalf.
+ *   All rights reserved.
  *
  *   Redistribution and use in source and binary forms, with or without
  *   modification, are permitted provided that the following conditions
@@ -93,6 +95,9 @@
 #define MRVL_COOKIE_HIGH_ADDR_SHIFT    (sizeof(pp2_cookie_t) * 8)
 #define MRVL_COOKIE_HIGH_ADDR_MASK     (~0ULL << MRVL_COOKIE_HIGH_ADDR_SHIFT)
 
+/* Memory size (in bytes) for MUSDK dma buffers */
+#define MRVL_MUSDK_DMA_MEMSIZE 41943040
+
 static const char * const valid_args[] = {
        MRVL_IFACE_NAME_ARG,
        MRVL_CFG_ARG,
@@ -134,12 +139,16 @@ struct mrvl_rxq {
        struct rte_mempool *mp;
        int queue_id;
        int port_id;
+       int cksum_enabled;
+       uint64_t bytes_recv;
+       uint64_t drop_mac;
 };
 
 struct mrvl_txq {
        struct mrvl_priv *priv;
        int queue_id;
        int port_id;
+       uint64_t bytes_sent;
 };
 
 /*
@@ -279,6 +288,7 @@ mrvl_dev_configure(struct rte_eth_dev *dev)
                return ret;
 
        priv->ppio_params.outqs_params.num_outqs = dev->data->nb_tx_queues;
+       priv->ppio_params.maintain_stats = 1;
        priv->nb_rx_queues = dev->data->nb_rx_queues;
 
        if (dev->data->nb_rx_queues == 1 &&
@@ -833,6 +843,136 @@ mrvl_mac_addr_set(struct rte_eth_dev *dev, struct ether_addr *mac_addr)
        pp2_ppio_enable(priv->ppio);
 }
 
+/**
+ * DPDK callback to get device statistics.
+ *
+ * @param dev
+ *   Pointer to Ethernet device structure.
+ * @param stats
+ *   Stats structure output buffer.
+ *
+ * @return
+ *   0 on success, negative error value otherwise.
+ */
+static int
+mrvl_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
+{
+       struct mrvl_priv *priv = dev->data->dev_private;
+       struct pp2_ppio_statistics ppio_stats;
+       uint64_t drop_mac = 0;
+       unsigned int i, idx, ret;
+
+       for (i = 0; i < dev->data->nb_rx_queues; i++) {
+               struct mrvl_rxq *rxq = dev->data->rx_queues[i];
+               struct pp2_ppio_inq_statistics rx_stats;
+
+               if (!rxq)
+                       continue;
+
+               idx = rxq->queue_id;
+               if (unlikely(idx >= RTE_ETHDEV_QUEUE_STAT_CNTRS)) {
+                       RTE_LOG(ERR, PMD,
+                               "rx queue %d stats out of range (0 - %d)\n",
+                               idx, RTE_ETHDEV_QUEUE_STAT_CNTRS - 1);
+                       continue;
+               }
+
+               ret = pp2_ppio_inq_get_statistics(priv->ppio,
+                                                 priv->rxq_map[idx].tc,
+                                                 priv->rxq_map[idx].inq,
+                                                 &rx_stats, 0);
+               if (unlikely(ret)) {
+                       RTE_LOG(ERR, PMD,
+                               "Failed to update rx queue %d stats\n", idx);
+                       break;
+               }
+
+               stats->q_ibytes[idx] = rxq->bytes_recv;
+               stats->q_ipackets[idx] = rx_stats.enq_desc - rxq->drop_mac;
+               stats->q_errors[idx] = rx_stats.drop_early +
+                                      rx_stats.drop_fullq +
+                                      rx_stats.drop_bm +
+                                      rxq->drop_mac;
+               stats->ibytes += rxq->bytes_recv;
+               drop_mac += rxq->drop_mac;
+       }
+
+       for (i = 0; i < dev->data->nb_tx_queues; i++) {
+               struct mrvl_txq *txq = dev->data->tx_queues[i];
+               struct pp2_ppio_outq_statistics tx_stats;
+
+               if (!txq)
+                       continue;
+
+               idx = txq->queue_id;
+               if (unlikely(idx >= RTE_ETHDEV_QUEUE_STAT_CNTRS)) {
+                       RTE_LOG(ERR, PMD,
+                               "tx queue %d stats out of range (0 - %d)\n",
+                               idx, RTE_ETHDEV_QUEUE_STAT_CNTRS - 1);
+               }
+
+               ret = pp2_ppio_outq_get_statistics(priv->ppio, idx,
+                                                  &tx_stats, 0);
+               if (unlikely(ret)) {
+                       RTE_LOG(ERR, PMD,
+                               "Failed to update tx queue %d stats\n", idx);
+                       break;
+               }
+
+               stats->q_opackets[idx] = tx_stats.deq_desc;
+               stats->q_obytes[idx] = txq->bytes_sent;
+               stats->obytes += txq->bytes_sent;
+       }
+
+       ret = pp2_ppio_get_statistics(priv->ppio, &ppio_stats, 0);
+       if (unlikely(ret)) {
+               RTE_LOG(ERR, PMD, "Failed to update port statistics\n");
+               return ret;
+       }
+
+       stats->ipackets += ppio_stats.rx_packets - drop_mac;
+       stats->opackets += ppio_stats.tx_packets;
+       stats->imissed += ppio_stats.rx_fullq_dropped +
+                         ppio_stats.rx_bm_dropped +
+                         ppio_stats.rx_early_dropped +
+                         ppio_stats.rx_fifo_dropped +
+                         ppio_stats.rx_cls_dropped;
+       stats->ierrors = drop_mac;
+
+       return 0;
+}
+
+/**
+ * DPDK callback to clear device statistics.
+ *
+ * @param dev
+ *   Pointer to Ethernet device structure.
+ */
+static void
+mrvl_stats_reset(struct rte_eth_dev *dev)
+{
+       struct mrvl_priv *priv = dev->data->dev_private;
+       int i;
+
+       for (i = 0; i < dev->data->nb_rx_queues; i++) {
+               struct mrvl_rxq *rxq = dev->data->rx_queues[i];
+
+               pp2_ppio_inq_get_statistics(priv->ppio, priv->rxq_map[i].tc,
+                                           priv->rxq_map[i].inq, NULL, 1);
+               rxq->bytes_recv = 0;
+               rxq->drop_mac = 0;
+       }
+
+       for (i = 0; i < dev->data->nb_tx_queues; i++) {
+               struct mrvl_txq *txq = dev->data->tx_queues[i];
+
+               pp2_ppio_outq_get_statistics(priv->ppio, i, NULL, 1);
+               txq->bytes_sent = 0;
+       }
+
+       pp2_ppio_get_statistics(priv->ppio, NULL, 1);
+}
+
 /**
  * DPDK callback to get information about the device.
  *
@@ -863,7 +1003,15 @@ mrvl_dev_infos_get(struct rte_eth_dev *dev __rte_unused,
        info->tx_desc_lim.nb_align = MRVL_PP2_TXD_ALIGN;
 
        info->rx_offload_capa = DEV_RX_OFFLOAD_JUMBO_FRAME |
-                               DEV_RX_OFFLOAD_VLAN_FILTER;
+                               DEV_RX_OFFLOAD_VLAN_FILTER |
+                               DEV_RX_OFFLOAD_IPV4_CKSUM |
+                               DEV_RX_OFFLOAD_UDP_CKSUM |
+                               DEV_RX_OFFLOAD_TCP_CKSUM;
+
+       info->tx_offload_capa = DEV_TX_OFFLOAD_IPV4_CKSUM |
+                               DEV_TX_OFFLOAD_UDP_CKSUM |
+                               DEV_TX_OFFLOAD_TCP_CKSUM;
+
        info->flow_type_rss_offloads = ETH_RSS_IPV4 |
                                       ETH_RSS_NONFRAG_IPV4_TCP |
                                       ETH_RSS_NONFRAG_IPV4_UDP;
@@ -874,6 +1022,33 @@ mrvl_dev_infos_get(struct rte_eth_dev *dev __rte_unused,
        info->max_rx_pktlen = MRVL_PKT_SIZE_MAX;
 }
 
+/**
+ * Return supported packet types.
+ *
+ * @param dev
+ *   Pointer to Ethernet device structure (unused).
+ *
+ * @return
+ *   Const pointer to the table with supported packet types.
+ */
+static const uint32_t *
+mrvl_dev_supported_ptypes_get(struct rte_eth_dev *dev __rte_unused)
+{
+       static const uint32_t ptypes[] = {
+               RTE_PTYPE_L2_ETHER,
+               RTE_PTYPE_L3_IPV4,
+               RTE_PTYPE_L3_IPV4_EXT,
+               RTE_PTYPE_L3_IPV4_EXT_UNKNOWN,
+               RTE_PTYPE_L3_IPV6,
+               RTE_PTYPE_L3_IPV6_EXT,
+               RTE_PTYPE_L2_ETHER_ARP,
+               RTE_PTYPE_L4_TCP,
+               RTE_PTYPE_L4_UDP
+       };
+
+       return ptypes;
+}
+
 /**
  * DPDK callback to get information about specific receive queue.
  *
@@ -978,7 +1153,7 @@ mrvl_fill_bpool(struct mrvl_rxq *rxq, int num)
                }
 
                entries[i].buff.addr =
-                       rte_mbuf_data_dma_addr_default(mbufs[i]);
+                       rte_mbuf_data_iova_default(mbufs[i]);
                entries[i].buff.cookie = (pp2_cookie_t)(uint64_t)mbufs[i];
                entries[i].bpool = bpool;
        }
@@ -1059,6 +1234,7 @@ mrvl_rx_queue_setup(struct rte_eth_dev *dev, uint16_t idx, uint16_t desc,
 
        rxq->priv = priv;
        rxq->mp = mp;
+       rxq->cksum_enabled = dev->data->dev_conf.rxmode.hw_ip_checksum;
        rxq->queue_id = idx;
        rxq->port_id = dev->data->port_id;
        mrvl_port_to_bpool_lookup[rxq->port_id] = priv->bpool;
@@ -1244,7 +1420,10 @@ static const struct eth_dev_ops mrvl_ops = {
        .mac_addr_add = mrvl_mac_addr_add,
        .mac_addr_set = mrvl_mac_addr_set,
        .mtu_set = mrvl_mtu_set,
+       .stats_get = mrvl_stats_get,
+       .stats_reset = mrvl_stats_reset,
        .dev_infos_get = mrvl_dev_infos_get,
+       .dev_supported_ptypes_get = mrvl_dev_supported_ptypes_get,
        .rxq_info_get = mrvl_rxq_info_get,
        .txq_info_get = mrvl_txq_info_get,
        .vlan_filter_set = mrvl_vlan_filter_set,
@@ -1256,6 +1435,107 @@ static const struct eth_dev_ops mrvl_ops = {
        .rss_hash_conf_get = mrvl_rss_hash_conf_get,
 };
 
+/**
+ * Return packet type information and l3/l4 offsets.
+ *
+ * @param desc
+ *   Pointer to the received packet descriptor.
+ * @param l3_offset
+ *   l3 packet offset.
+ * @param l4_offset
+ *   l4 packet offset.
+ *
+ * @return
+ *   Packet type information.
+ */
+static inline uint64_t
+mrvl_desc_to_packet_type_and_offset(struct pp2_ppio_desc *desc,
+                                   uint8_t *l3_offset, uint8_t *l4_offset)
+{
+       enum pp2_inq_l3_type l3_type;
+       enum pp2_inq_l4_type l4_type;
+       uint64_t packet_type;
+
+       pp2_ppio_inq_desc_get_l3_info(desc, &l3_type, l3_offset);
+       pp2_ppio_inq_desc_get_l4_info(desc, &l4_type, l4_offset);
+
+       packet_type = RTE_PTYPE_L2_ETHER;
+
+       switch (l3_type) {
+       case PP2_INQ_L3_TYPE_IPV4_NO_OPTS:
+               packet_type |= RTE_PTYPE_L3_IPV4;
+               break;
+       case PP2_INQ_L3_TYPE_IPV4_OK:
+               packet_type |= RTE_PTYPE_L3_IPV4_EXT;
+               break;
+       case PP2_INQ_L3_TYPE_IPV4_TTL_ZERO:
+               packet_type |= RTE_PTYPE_L3_IPV4_EXT_UNKNOWN;
+               break;
+       case PP2_INQ_L3_TYPE_IPV6_NO_EXT:
+               packet_type |= RTE_PTYPE_L3_IPV6;
+               break;
+       case PP2_INQ_L3_TYPE_IPV6_EXT:
+               packet_type |= RTE_PTYPE_L3_IPV6_EXT;
+               break;
+       case PP2_INQ_L3_TYPE_ARP:
+               packet_type |= RTE_PTYPE_L2_ETHER_ARP;
+               /*
+                * In case of ARP l4_offset is set to wrong value.
+                * Set it to proper one so that later on mbuf->l3_len can be
+                * calculated subtracting l4_offset and l3_offset.
+                */
+               *l4_offset = *l3_offset + MRVL_ARP_LENGTH;
+               break;
+       default:
+               RTE_LOG(DEBUG, PMD, "Failed to recognise l3 packet type\n");
+               break;
+       }
+
+       switch (l4_type) {
+       case PP2_INQ_L4_TYPE_TCP:
+               packet_type |= RTE_PTYPE_L4_TCP;
+               break;
+       case PP2_INQ_L4_TYPE_UDP:
+               packet_type |= RTE_PTYPE_L4_UDP;
+               break;
+       default:
+               RTE_LOG(DEBUG, PMD, "Failed to recognise l4 packet type\n");
+               break;
+       }
+
+       return packet_type;
+}
+
+/**
+ * Get offload information from the received packet descriptor.
+ *
+ * @param desc
+ *   Pointer to the received packet descriptor.
+ *
+ * @return
+ *   Mbuf offload flags.
+ */
+static inline uint64_t
+mrvl_desc_to_ol_flags(struct pp2_ppio_desc *desc)
+{
+       uint64_t flags;
+       enum pp2_inq_desc_status status;
+
+       status = pp2_ppio_inq_desc_get_l3_pkt_error(desc);
+       if (unlikely(status != PP2_DESC_ERR_OK))
+               flags = PKT_RX_IP_CKSUM_BAD;
+       else
+               flags = PKT_RX_IP_CKSUM_GOOD;
+
+       status = pp2_ppio_inq_desc_get_l4_pkt_error(desc);
+       if (unlikely(status != PP2_DESC_ERR_OK))
+               flags |= PKT_RX_L4_CKSUM_BAD;
+       else
+               flags |= PKT_RX_L4_CKSUM_GOOD;
+
+       return flags;
+}
+
 /**
  * DPDK callback for receive.
  *
@@ -1294,6 +1574,7 @@ mrvl_rx_pkt_burst(void *rxq, struct rte_mbuf **rx_pkts, uint16_t nb_pkts)
 
        for (i = 0; i < nb_pkts; i++) {
                struct rte_mbuf *mbuf;
+               uint8_t l3_offset, l4_offset;
                enum pp2_inq_desc_status status;
                uint64_t addr;
 
@@ -1317,13 +1598,14 @@ mrvl_rx_pkt_burst(void *rxq, struct rte_mbuf **rx_pkts, uint16_t nb_pkts)
                status = pp2_ppio_inq_desc_get_l2_pkt_error(&descs[i]);
                if (unlikely(status != PP2_DESC_ERR_OK)) {
                        struct pp2_buff_inf binf = {
-                               .addr = rte_mbuf_data_dma_addr_default(mbuf),
+                               .addr = rte_mbuf_data_iova_default(mbuf),
                                .cookie = (pp2_cookie_t)(uint64_t)mbuf,
                        };
 
                        pp2_bpool_put_buff(hifs[core_id], bpool, &binf);
                        mrvl_port_bpool_size
                                [bpool->pp2_id][bpool->id][core_id]++;
+                       q->drop_mac++;
                        continue;
                }
 
@@ -1331,8 +1613,18 @@ mrvl_rx_pkt_burst(void *rxq, struct rte_mbuf **rx_pkts, uint16_t nb_pkts)
                mbuf->pkt_len = pp2_ppio_inq_desc_get_pkt_len(&descs[i]);
                mbuf->data_len = mbuf->pkt_len;
                mbuf->port = q->port_id;
+               mbuf->packet_type =
+                       mrvl_desc_to_packet_type_and_offset(&descs[i],
+                                                           &l3_offset,
+                                                           &l4_offset);
+               mbuf->l2_len = l3_offset;
+               mbuf->l3_len = l4_offset - l3_offset;
+
+               if (likely(q->cksum_enabled))
+                       mbuf->ol_flags = mrvl_desc_to_ol_flags(&descs[i]);
 
                rx_pkts[rx_done++] = mbuf;
+               q->bytes_recv += mbuf->pkt_len;
        }
 
        if (rte_spinlock_trylock(&q->priv->lock) == 1) {
@@ -1371,6 +1663,67 @@ mrvl_rx_pkt_burst(void *rxq, struct rte_mbuf **rx_pkts, uint16_t nb_pkts)
        return rx_done;
 }
 
+/**
+ * Prepare offload information.
+ *
+ * @param ol_flags
+ *   Offload flags.
+ * @param packet_type
+ *   Packet type bitfield.
+ * @param l3_type
+ *   Pointer to the pp2_ouq_l3_type structure.
+ * @param l4_type
+ *   Pointer to the pp2_outq_l4_type structure.
+ * @param gen_l3_cksum
+ *   Will be set to 1 in case l3 checksum is computed.
+ * @param l4_cksum
+ *   Will be set to 1 in case l4 checksum is computed.
+ *
+ * @return
+ *   0 on success, negative error value otherwise.
+ */
+static inline int
+mrvl_prepare_proto_info(uint64_t ol_flags, uint32_t packet_type,
+                       enum pp2_outq_l3_type *l3_type,
+                       enum pp2_outq_l4_type *l4_type,
+                       int *gen_l3_cksum,
+                       int *gen_l4_cksum)
+{
+       /*
+        * Based on ol_flags prepare information
+        * for pp2_ppio_outq_desc_set_proto_info() which setups descriptor
+        * for offloading.
+        */
+       if (ol_flags & PKT_TX_IPV4) {
+               *l3_type = PP2_OUTQ_L3_TYPE_IPV4;
+               *gen_l3_cksum = ol_flags & PKT_TX_IP_CKSUM ? 1 : 0;
+       } else if (ol_flags & PKT_TX_IPV6) {
+               *l3_type = PP2_OUTQ_L3_TYPE_IPV6;
+               /* no checksum for ipv6 header */
+               *gen_l3_cksum = 0;
+       } else {
+               /* if something different then stop processing */
+               return -1;
+       }
+
+       ol_flags &= PKT_TX_L4_MASK;
+       if ((packet_type & RTE_PTYPE_L4_TCP) &&
+           ol_flags == PKT_TX_TCP_CKSUM) {
+               *l4_type = PP2_OUTQ_L4_TYPE_TCP;
+               *gen_l4_cksum = 1;
+       } else if ((packet_type & RTE_PTYPE_L4_UDP) &&
+                  ol_flags == PKT_TX_UDP_CKSUM) {
+               *l4_type = PP2_OUTQ_L4_TYPE_UDP;
+               *gen_l4_cksum = 1;
+       } else {
+               *l4_type = PP2_OUTQ_L4_TYPE_OTHER;
+               /* no checksum for other type */
+               *gen_l4_cksum = 0;
+       }
+
+       return 0;
+}
+
 /**
  * Release already sent buffers to bpool (buffer-pool).
  *
@@ -1467,8 +1820,9 @@ mrvl_tx_pkt_burst(void *txq, struct rte_mbuf **tx_pkts, uint16_t nb_pkts)
        struct mrvl_shadow_txq *sq = &shadow_txqs[q->port_id][rte_lcore_id()];
        struct pp2_hif *hif = hifs[rte_lcore_id()];
        struct pp2_ppio_desc descs[nb_pkts];
-       int i;
+       int i, ret, bytes_sent = 0;
        uint16_t num, sq_free_size;
+       uint64_t addr;
 
        if (unlikely(!q->priv->ppio))
                return 0;
@@ -1486,6 +1840,9 @@ mrvl_tx_pkt_burst(void *txq, struct rte_mbuf **tx_pkts, uint16_t nb_pkts)
 
        for (i = 0; i < nb_pkts; i++) {
                struct rte_mbuf *mbuf = tx_pkts[i];
+               int gen_l3_cksum, gen_l4_cksum;
+               enum pp2_outq_l3_type l3_type;
+               enum pp2_outq_l4_type l4_type;
 
                if (likely(nb_pkts - i > MRVL_MUSDK_PREFETCH_SHIFT)) {
                        struct rte_mbuf *pref_pkt_hdr;
@@ -1497,7 +1854,7 @@ mrvl_tx_pkt_burst(void *txq, struct rte_mbuf **tx_pkts, uint16_t nb_pkts)
 
                sq->ent[sq->head].buff.cookie = (pp2_cookie_t)(uint64_t)mbuf;
                sq->ent[sq->head].buff.addr =
-                       rte_mbuf_data_dma_addr_default(mbuf);
+                       rte_mbuf_data_iova_default(mbuf);
                sq->ent[sq->head].bpool =
                        (unlikely(mbuf->port == 0xff || mbuf->refcnt > 1)) ?
                         NULL : mrvl_port_to_bpool_lookup[mbuf->port];
@@ -1506,10 +1863,26 @@ mrvl_tx_pkt_burst(void *txq, struct rte_mbuf **tx_pkts, uint16_t nb_pkts)
 
                pp2_ppio_outq_desc_reset(&descs[i]);
                pp2_ppio_outq_desc_set_phys_addr(&descs[i],
-                                                rte_pktmbuf_mtophys(mbuf));
+                                                rte_pktmbuf_iova(mbuf));
                pp2_ppio_outq_desc_set_pkt_offset(&descs[i], 0);
                pp2_ppio_outq_desc_set_pkt_len(&descs[i],
                                               rte_pktmbuf_pkt_len(mbuf));
+
+               bytes_sent += rte_pktmbuf_pkt_len(mbuf);
+               /*
+                * in case unsupported ol_flags were passed
+                * do not update descriptor offload information
+                */
+               ret = mrvl_prepare_proto_info(mbuf->ol_flags, mbuf->packet_type,
+                                             &l3_type, &l4_type, &gen_l3_cksum,
+                                             &gen_l4_cksum);
+               if (unlikely(ret))
+                       continue;
+
+               pp2_ppio_outq_desc_set_proto_info(&descs[i], l3_type, l4_type,
+                                                 mbuf->l2_len,
+                                                 mbuf->l2_len + mbuf->l3_len,
+                                                 gen_l3_cksum, gen_l4_cksum);
        }
 
        num = nb_pkts;
@@ -1519,10 +1892,15 @@ mrvl_tx_pkt_burst(void *txq, struct rte_mbuf **tx_pkts, uint16_t nb_pkts)
                for (i = nb_pkts; i < num; i++) {
                        sq->head = (MRVL_PP2_TX_SHADOWQ_SIZE + sq->head - 1) &
                                MRVL_PP2_TX_SHADOWQ_MASK;
+                       addr = cookie_addr_high | sq->ent[sq->head].buff.cookie;
+                       bytes_sent -=
+                               rte_pktmbuf_pkt_len((struct rte_mbuf *)addr);
                }
                sq->size -= num - nb_pkts;
        }
 
+       q->bytes_sent += bytes_sent;
+
        return nb_pkts;
 }
 
@@ -1820,9 +2198,14 @@ rte_pmd_mrvl_probe(struct rte_vdev_device *vdev)
         * ret == -EEXIST is correct, it means DMA
         * has been already initialized (by another PMD).
         */
-       ret = mv_sys_dma_mem_init(RTE_MRVL_MUSDK_DMA_MEMSIZE);
-       if (ret < 0 && ret != -EEXIST)
-               goto out_free_kvlist;
+       ret = mv_sys_dma_mem_init(MRVL_MUSDK_DMA_MEMSIZE);
+       if (ret < 0) {
+               if (ret != -EEXIST)
+                       goto out_free_kvlist;
+               else
+                       RTE_LOG(INFO, PMD,
+                               "DMA memory has been already initialized by a different driver.\n");
+       }
 
        ret = mrvl_init_pp2();
        if (ret) {