X-Git-Url: http://git.droids-corp.org/?a=blobdiff_plain;f=drivers%2Fnet%2Fszedata2%2Frte_eth_szedata2.c;h=30c888cd9626c7ed7ad81a80326844552f651d12;hb=4d4e245ad637c0befbd6da9976c28174a1d74b88;hp=fc0d81f249a7dd69cc717fc4809db3a48ebe608b;hpb=906cea82aa3594fc6c8f8d948fa4734d06c0f391;p=dpdk.git diff --git a/drivers/net/szedata2/rte_eth_szedata2.c b/drivers/net/szedata2/rte_eth_szedata2.c index fc0d81f249..30c888cd96 100644 --- a/drivers/net/szedata2/rte_eth_szedata2.c +++ b/drivers/net/szedata2/rte_eth_szedata2.c @@ -1,55 +1,29 @@ -/*- - * BSD LICENSE - * - * Copyright (c) 2015 CESNET - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * * Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in - * the documentation and/or other materials provided with the - * distribution. - * * Neither the name of CESNET nor the names of its - * contributors may be used to endorse or promote products derived - * from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +/* SPDX-License-Identifier: BSD-3-Clause + * Copyright(c) 2015 - 2016 CESNET */ #include #include #include #include +#include +#include +#include +#include +#include #include #include -#include +#include +#include #include #include #include #include #include "rte_eth_szedata2.h" - -#define RTE_ETH_SZEDATA2_DEV_PATH_ARG "dev_path" -#define RTE_ETH_SZEDATA2_RX_IFACES_ARG "rx_ifaces" -#define RTE_ETH_SZEDATA2_TX_IFACES_ARG "tx_ifaces" +#include "szedata2_logs.h" #define RTE_ETH_SZEDATA2_MAX_RX_QUEUES 32 #define RTE_ETH_SZEDATA2_MAX_TX_QUEUES 32 @@ -60,10 +34,58 @@ */ #define RTE_SZE2_PACKET_HEADER_SIZE_ALIGNED 8 +#define RTE_SZEDATA2_DRIVER_NAME net_szedata2 + +#define SZEDATA2_DEV_PATH_FMT "/dev/szedataII%u" + +/** + * Format string for suffix used to differentiate between Ethernet ports + * on the same PCI device. + */ +#define SZEDATA2_ETH_DEV_NAME_SUFFIX_FMT "-port%u" + +/** + * Maximum number of ports for one device. + */ +#define SZEDATA2_MAX_PORTS 2 + +/** + * Entry in list of PCI devices for this driver. + */ +struct pci_dev_list_entry; +struct pci_dev_list_entry { + LIST_ENTRY(pci_dev_list_entry) next; + struct rte_pci_device *pci_dev; + unsigned int port_count; +}; + +/* List of PCI devices with number of ports for this driver. */ +LIST_HEAD(pci_dev_list, pci_dev_list_entry) szedata2_pci_dev_list = + LIST_HEAD_INITIALIZER(szedata2_pci_dev_list); + +struct port_info { + unsigned int rx_base_id; + unsigned int tx_base_id; + unsigned int rx_count; + unsigned int tx_count; + int numa_node; +}; + +struct pmd_internals { + struct rte_eth_dev *dev; + uint16_t max_rx_queues; + uint16_t max_tx_queues; + unsigned int rxq_base_id; + unsigned int txq_base_id; + char *sze_dev_path; +}; + struct szedata2_rx_queue { + struct pmd_internals *priv; struct szedata *sze; uint8_t rx_channel; - uint8_t in_port; + uint16_t qid; + uint16_t in_port; struct rte_mempool *mb_pool; volatile uint64_t rx_pkts; volatile uint64_t rx_bytes; @@ -71,59 +93,18 @@ struct szedata2_rx_queue { }; struct szedata2_tx_queue { + struct pmd_internals *priv; struct szedata *sze; uint8_t tx_channel; + uint16_t qid; volatile uint64_t tx_pkts; - volatile uint64_t err_pkts; volatile uint64_t tx_bytes; + volatile uint64_t err_pkts; }; -struct rxtx_szedata2 { - uint32_t num_of_rx; - uint32_t num_of_tx; - uint32_t sze_rx_mask_req; - uint32_t sze_tx_mask_req; - char *sze_dev; -}; - -struct pmd_internals { - struct szedata2_rx_queue rx_queue[RTE_ETH_SZEDATA2_MAX_RX_QUEUES]; - struct szedata2_tx_queue tx_queue[RTE_ETH_SZEDATA2_MAX_TX_QUEUES]; - unsigned nb_rx_queues; - unsigned nb_tx_queues; - uint32_t num_of_rx; - uint32_t num_of_tx; - uint32_t sze_rx_req; - uint32_t sze_tx_req; - int if_index; - char *sze_dev; -}; - -static const char *valid_arguments[] = { - RTE_ETH_SZEDATA2_DEV_PATH_ARG, - RTE_ETH_SZEDATA2_RX_IFACES_ARG, - RTE_ETH_SZEDATA2_TX_IFACES_ARG, - NULL -}; - -static struct ether_addr eth_addr = { +static struct rte_ether_addr eth_addr = { .addr_bytes = { 0x00, 0x11, 0x17, 0x00, 0x00, 0x00 } }; -static const char *drivername = "SZEdata2 PMD"; -static struct rte_eth_link pmd_link = { - .link_speed = ETH_LINK_SPEED_10G, - .link_duplex = ETH_LINK_FULL_DUPLEX, - .link_status = 0 -}; - - -static uint32_t -count_ones(uint32_t num) -{ - num = num - ((num >> 1) & 0x55555555); /* reuse input as temporary */ - num = (num & 0x33333333) + ((num >> 2) & 0x33333333); /* temp */ - return (((num + (num >> 4)) & 0xF0F0F0F) * 0x1010101) >> 24; /* count */ -} static uint16_t eth_szedata2_rx(void *queue, @@ -159,8 +140,10 @@ eth_szedata2_rx(void *queue, for (i = 0; i < nb_pkts; i++) { mbuf = rte_pktmbuf_alloc(sze_q->mb_pool); - if (unlikely(mbuf == NULL)) + if (unlikely(mbuf == NULL)) { + sze_q->priv->dev->data->rx_mbuf_alloc_failed++; break; + } /* get the next sze packet */ if (sze->ct_rx_lck != NULL && !sze->ct_rx_rem_bytes && @@ -344,10 +327,10 @@ eth_szedata2_rx(void *queue, * sze packet will not fit in one mbuf, * scattered mode is not enabled, drop packet */ - RTE_LOG(ERR, PMD, + PMD_DRV_LOG(ERR, "SZE segment %d bytes will not fit in one mbuf " "(%d bytes), scattered mode is not enabled, " - "drop packet!!\n", + "drop packet!!", packet_size, buf_size); rte_pktmbuf_free(mbuf); } @@ -359,149 +342,486 @@ eth_szedata2_rx(void *queue, } static uint16_t -eth_szedata2_tx(void *queue, +eth_szedata2_rx_scattered(void *queue, struct rte_mbuf **bufs, uint16_t nb_pkts) { + unsigned int i; struct rte_mbuf *mbuf; - struct szedata2_tx_queue *sze_q = queue; - uint16_t num_tx = 0; + struct szedata2_rx_queue *sze_q = queue; + struct rte_pktmbuf_pool_private *mbp_priv; + uint16_t num_rx = 0; + uint16_t buf_size; + uint16_t sg_size; + uint16_t hw_size; + uint16_t packet_size; uint64_t num_bytes = 0; + struct szedata *sze = sze_q->sze; + uint8_t *header_ptr = NULL; /* header of packet */ + uint8_t *packet_ptr1 = NULL; + uint8_t *packet_ptr2 = NULL; + uint16_t packet_len1 = 0; + uint16_t packet_len2 = 0; + uint16_t hw_data_align; + uint64_t *mbuf_failed_ptr = + &sze_q->priv->dev->data->rx_mbuf_alloc_failed; - const struct szedata_lock *lck; - uint32_t lock_size; - uint32_t lock_size2; - void *dst; - uint32_t pkt_len; - uint32_t hwpkt_len; - uint32_t unlock_size; - uint32_t rem_len; - uint8_t mbuf_segs; - uint16_t pkt_left = nb_pkts; - - if (sze_q->sze == NULL || nb_pkts == 0) + if (unlikely(sze_q->sze == NULL || nb_pkts == 0)) return 0; - while (pkt_left > 0) { - unlock_size = 0; - lck = szedata_tx_lock_data(sze_q->sze, - RTE_ETH_SZEDATA2_TX_LOCK_SIZE, - sze_q->tx_channel); - if (lck == NULL) - continue; + /* + * Reads the given number of packets from szedata2 channel given + * by queue and copies the packet data into a newly allocated mbuf + * to return. + */ + for (i = 0; i < nb_pkts; i++) { + const struct szedata_lock *ct_rx_lck_backup; + unsigned int ct_rx_rem_bytes_backup; + unsigned char *ct_rx_cur_ptr_backup; - dst = lck->start; - lock_size = lck->len; - lock_size2 = lck->next ? lck->next->len : 0; + /* get the next sze packet */ + if (sze->ct_rx_lck != NULL && !sze->ct_rx_rem_bytes && + sze->ct_rx_lck->next == NULL) { + /* unlock old data */ + szedata_rx_unlock_data(sze_q->sze, sze->ct_rx_lck_orig); + sze->ct_rx_lck_orig = NULL; + sze->ct_rx_lck = NULL; + } -next_packet: - mbuf = bufs[nb_pkts - pkt_left]; + /* + * Store items from sze structure which can be changed + * before mbuf allocating. Use these items in case of mbuf + * allocating failure. + */ + ct_rx_lck_backup = sze->ct_rx_lck; + ct_rx_rem_bytes_backup = sze->ct_rx_rem_bytes; + ct_rx_cur_ptr_backup = sze->ct_rx_cur_ptr; - pkt_len = mbuf->pkt_len; - mbuf_segs = mbuf->nb_segs; + if (!sze->ct_rx_rem_bytes && sze->ct_rx_lck_orig == NULL) { + /* nothing to read, lock new data */ + sze->ct_rx_lck = szedata_rx_lock_data(sze_q->sze, ~0U); + sze->ct_rx_lck_orig = sze->ct_rx_lck; - hwpkt_len = RTE_SZE2_PACKET_HEADER_SIZE_ALIGNED + - RTE_SZE2_ALIGN8(pkt_len); + /* + * Backup items from sze structure must be updated + * after locking to contain pointers to new locks. + */ + ct_rx_lck_backup = sze->ct_rx_lck; + ct_rx_rem_bytes_backup = sze->ct_rx_rem_bytes; + ct_rx_cur_ptr_backup = sze->ct_rx_cur_ptr; - if (lock_size + lock_size2 < hwpkt_len) { - szedata_tx_unlock_data(sze_q->sze, lck, unlock_size); - continue; + if (sze->ct_rx_lck == NULL) + /* nothing to lock */ + break; + + sze->ct_rx_cur_ptr = sze->ct_rx_lck->start; + sze->ct_rx_rem_bytes = sze->ct_rx_lck->len; + + if (!sze->ct_rx_rem_bytes) + break; } - num_bytes += pkt_len; + if (sze->ct_rx_rem_bytes < RTE_SZE2_PACKET_HEADER_SIZE) { + /* + * cut in header - copy parts of header to merge buffer + */ + if (sze->ct_rx_lck->next == NULL) + break; - if (lock_size > hwpkt_len) { - void *tmp_dst; + /* copy first part of header */ + rte_memcpy(sze->ct_rx_buffer, sze->ct_rx_cur_ptr, + sze->ct_rx_rem_bytes); - rem_len = 0; + /* copy second part of header */ + sze->ct_rx_lck = sze->ct_rx_lck->next; + sze->ct_rx_cur_ptr = sze->ct_rx_lck->start; + rte_memcpy(sze->ct_rx_buffer + sze->ct_rx_rem_bytes, + sze->ct_rx_cur_ptr, + RTE_SZE2_PACKET_HEADER_SIZE - + sze->ct_rx_rem_bytes); - /* write packet length at first 2 bytes in 8B header */ - *((uint16_t *)dst) = htole16( - RTE_SZE2_PACKET_HEADER_SIZE_ALIGNED + - pkt_len); - *(((uint16_t *)dst) + 1) = htole16(0); + sze->ct_rx_cur_ptr += RTE_SZE2_PACKET_HEADER_SIZE - + sze->ct_rx_rem_bytes; + sze->ct_rx_rem_bytes = sze->ct_rx_lck->len - + RTE_SZE2_PACKET_HEADER_SIZE + + sze->ct_rx_rem_bytes; - /* copy packet from mbuf */ - tmp_dst = ((uint8_t *)(dst)) + - RTE_SZE2_PACKET_HEADER_SIZE_ALIGNED; - if (mbuf_segs == 1) { - /* - * non-scattered packet, - * transmit from one mbuf - */ - rte_memcpy(tmp_dst, - rte_pktmbuf_mtod(mbuf, const void *), - pkt_len); - } else { - /* scattered packet, transmit from more mbufs */ - struct rte_mbuf *m = mbuf; - while (m) { - rte_memcpy(tmp_dst, - rte_pktmbuf_mtod(m, - const void *), - m->data_len); - tmp_dst = ((uint8_t *)(tmp_dst)) + - m->data_len; - m = m->next; - } - } + header_ptr = (uint8_t *)sze->ct_rx_buffer; + } else { + /* not cut */ + header_ptr = (uint8_t *)sze->ct_rx_cur_ptr; + sze->ct_rx_cur_ptr += RTE_SZE2_PACKET_HEADER_SIZE; + sze->ct_rx_rem_bytes -= RTE_SZE2_PACKET_HEADER_SIZE; + } + sg_size = le16toh(*((uint16_t *)header_ptr)); + hw_size = le16toh(*(((uint16_t *)header_ptr) + 1)); + packet_size = sg_size - + RTE_SZE2_ALIGN8(RTE_SZE2_PACKET_HEADER_SIZE + hw_size); - dst = ((uint8_t *)dst) + hwpkt_len; - unlock_size += hwpkt_len; - lock_size -= hwpkt_len; - rte_pktmbuf_free(mbuf); - num_tx++; - pkt_left--; - if (pkt_left == 0) { - szedata_tx_unlock_data(sze_q->sze, lck, - unlock_size); - break; - } - goto next_packet; - } else if (lock_size + lock_size2 >= hwpkt_len) { - void *tmp_dst; - uint16_t write_len; + /* checks if packet all right */ + if (!sg_size) + errx(5, "Zero segsize"); - /* write packet length at first 2 bytes in 8B header */ - *((uint16_t *)dst) = - htole16(RTE_SZE2_PACKET_HEADER_SIZE_ALIGNED + - pkt_len); - *(((uint16_t *)dst) + 1) = htole16(0); + /* check sg_size and hwsize */ + if (hw_size > sg_size - RTE_SZE2_PACKET_HEADER_SIZE) { + errx(10, "Hwsize bigger than expected. Segsize: %d, " + "hwsize: %d", sg_size, hw_size); + } - /* - * If the raw packet (pkt_len) is smaller than lock_size - * get the correct length for memcpy - */ - write_len = - pkt_len < lock_size - - RTE_SZE2_PACKET_HEADER_SIZE_ALIGNED ? - pkt_len : - lock_size - RTE_SZE2_PACKET_HEADER_SIZE_ALIGNED; + hw_data_align = + RTE_SZE2_ALIGN8((RTE_SZE2_PACKET_HEADER_SIZE + + hw_size)) - RTE_SZE2_PACKET_HEADER_SIZE; - rem_len = hwpkt_len - lock_size; + if (sze->ct_rx_rem_bytes >= + (uint16_t)(sg_size - + RTE_SZE2_PACKET_HEADER_SIZE)) { + /* no cut */ + /* one packet ready - go to another */ + packet_ptr1 = sze->ct_rx_cur_ptr + hw_data_align; + packet_len1 = packet_size; + packet_ptr2 = NULL; + packet_len2 = 0; - tmp_dst = ((uint8_t *)(dst)) + - RTE_SZE2_PACKET_HEADER_SIZE_ALIGNED; - if (mbuf_segs == 1) { - /* - * non-scattered packet, - * transmit from one mbuf - */ - /* copy part of packet to first area */ - rte_memcpy(tmp_dst, - rte_pktmbuf_mtod(mbuf, const void *), - write_len); + sze->ct_rx_cur_ptr += RTE_SZE2_ALIGN8(sg_size) - + RTE_SZE2_PACKET_HEADER_SIZE; + sze->ct_rx_rem_bytes -= RTE_SZE2_ALIGN8(sg_size) - + RTE_SZE2_PACKET_HEADER_SIZE; + } else { + /* cut in data */ + if (sze->ct_rx_lck->next == NULL) { + errx(6, "Need \"next\" lock, but it is " + "missing: %u", sze->ct_rx_rem_bytes); + } - if (lck->next) - dst = lck->next->start; + /* skip hw data */ + if (sze->ct_rx_rem_bytes <= hw_data_align) { + uint16_t rem_size = hw_data_align - + sze->ct_rx_rem_bytes; - /* copy part of packet to second area */ - rte_memcpy(dst, - (const void *)(rte_pktmbuf_mtod(mbuf, - const uint8_t *) + - write_len), pkt_len - write_len); + /* MOVE to next lock */ + sze->ct_rx_lck = sze->ct_rx_lck->next; + sze->ct_rx_cur_ptr = + (void *)(((uint8_t *) + (sze->ct_rx_lck->start)) + rem_size); + + packet_ptr1 = sze->ct_rx_cur_ptr; + packet_len1 = packet_size; + packet_ptr2 = NULL; + packet_len2 = 0; + + sze->ct_rx_cur_ptr += + RTE_SZE2_ALIGN8(packet_size); + sze->ct_rx_rem_bytes = sze->ct_rx_lck->len - + rem_size - RTE_SZE2_ALIGN8(packet_size); + } else { + /* get pointer and length from first part */ + packet_ptr1 = sze->ct_rx_cur_ptr + + hw_data_align; + packet_len1 = sze->ct_rx_rem_bytes - + hw_data_align; + + /* MOVE to next lock */ + sze->ct_rx_lck = sze->ct_rx_lck->next; + sze->ct_rx_cur_ptr = sze->ct_rx_lck->start; + + /* get pointer and length from second part */ + packet_ptr2 = sze->ct_rx_cur_ptr; + packet_len2 = packet_size - packet_len1; + + sze->ct_rx_cur_ptr += + RTE_SZE2_ALIGN8(packet_size) - + packet_len1; + sze->ct_rx_rem_bytes = sze->ct_rx_lck->len - + (RTE_SZE2_ALIGN8(packet_size) - + packet_len1); + } + } + + if (unlikely(packet_ptr1 == NULL)) + break; + + mbuf = rte_pktmbuf_alloc(sze_q->mb_pool); + + if (unlikely(mbuf == NULL)) { + /* + * Restore items from sze structure to state after + * unlocking (eventually locking). + */ + sze->ct_rx_lck = ct_rx_lck_backup; + sze->ct_rx_rem_bytes = ct_rx_rem_bytes_backup; + sze->ct_rx_cur_ptr = ct_rx_cur_ptr_backup; + sze_q->priv->dev->data->rx_mbuf_alloc_failed++; + break; + } + + /* get the space available for data in the mbuf */ + mbp_priv = rte_mempool_get_priv(sze_q->mb_pool); + buf_size = (uint16_t)(mbp_priv->mbuf_data_room_size - + RTE_PKTMBUF_HEADROOM); + + if (packet_size <= buf_size) { + /* sze packet will fit in one mbuf, go ahead and copy */ + rte_memcpy(rte_pktmbuf_mtod(mbuf, void *), + packet_ptr1, packet_len1); + if (packet_ptr2 != NULL) { + rte_memcpy((void *) + (rte_pktmbuf_mtod(mbuf, uint8_t *) + + packet_len1), packet_ptr2, packet_len2); + } + mbuf->data_len = (uint16_t)packet_size; + } else { + /* + * sze packet will not fit in one mbuf, + * scatter packet into more mbufs + */ + struct rte_mbuf *m = mbuf; + uint16_t len = rte_pktmbuf_tailroom(mbuf); + + /* copy first part of packet */ + /* fill first mbuf */ + rte_memcpy(rte_pktmbuf_append(mbuf, len), packet_ptr1, + len); + packet_len1 -= len; + packet_ptr1 = ((uint8_t *)packet_ptr1) + len; + + while (packet_len1 > 0) { + /* fill new mbufs */ + m->next = rte_pktmbuf_alloc(sze_q->mb_pool); + + if (unlikely(m->next == NULL)) { + rte_pktmbuf_free(mbuf); + /* + * Restore items from sze structure + * to state after unlocking (eventually + * locking). + */ + sze->ct_rx_lck = ct_rx_lck_backup; + sze->ct_rx_rem_bytes = + ct_rx_rem_bytes_backup; + sze->ct_rx_cur_ptr = + ct_rx_cur_ptr_backup; + (*mbuf_failed_ptr)++; + goto finish; + } + + m = m->next; + + len = RTE_MIN(rte_pktmbuf_tailroom(m), + packet_len1); + rte_memcpy(rte_pktmbuf_append(mbuf, len), + packet_ptr1, len); + + (mbuf->nb_segs)++; + packet_len1 -= len; + packet_ptr1 = ((uint8_t *)packet_ptr1) + len; + } + + if (packet_ptr2 != NULL) { + /* copy second part of packet, if exists */ + /* fill the rest of currently last mbuf */ + len = rte_pktmbuf_tailroom(m); + rte_memcpy(rte_pktmbuf_append(mbuf, len), + packet_ptr2, len); + packet_len2 -= len; + packet_ptr2 = ((uint8_t *)packet_ptr2) + len; + + while (packet_len2 > 0) { + /* fill new mbufs */ + m->next = rte_pktmbuf_alloc( + sze_q->mb_pool); + + if (unlikely(m->next == NULL)) { + rte_pktmbuf_free(mbuf); + /* + * Restore items from sze + * structure to state after + * unlocking (eventually + * locking). + */ + sze->ct_rx_lck = + ct_rx_lck_backup; + sze->ct_rx_rem_bytes = + ct_rx_rem_bytes_backup; + sze->ct_rx_cur_ptr = + ct_rx_cur_ptr_backup; + (*mbuf_failed_ptr)++; + goto finish; + } + + m = m->next; + + len = RTE_MIN(rte_pktmbuf_tailroom(m), + packet_len2); + rte_memcpy( + rte_pktmbuf_append(mbuf, len), + packet_ptr2, len); + + (mbuf->nb_segs)++; + packet_len2 -= len; + packet_ptr2 = ((uint8_t *)packet_ptr2) + + len; + } + } + } + mbuf->pkt_len = packet_size; + mbuf->port = sze_q->in_port; + bufs[num_rx] = mbuf; + num_rx++; + num_bytes += packet_size; + } + +finish: + sze_q->rx_pkts += num_rx; + sze_q->rx_bytes += num_bytes; + return num_rx; +} + +static uint16_t +eth_szedata2_tx(void *queue, + struct rte_mbuf **bufs, + uint16_t nb_pkts) +{ + struct rte_mbuf *mbuf; + struct szedata2_tx_queue *sze_q = queue; + uint16_t num_tx = 0; + uint64_t num_bytes = 0; + + const struct szedata_lock *lck; + uint32_t lock_size; + uint32_t lock_size2; + void *dst; + uint32_t pkt_len; + uint32_t hwpkt_len; + uint32_t unlock_size; + uint32_t rem_len; + uint16_t mbuf_segs; + uint16_t pkt_left = nb_pkts; + + if (sze_q->sze == NULL || nb_pkts == 0) + return 0; + + while (pkt_left > 0) { + unlock_size = 0; + lck = szedata_tx_lock_data(sze_q->sze, + RTE_ETH_SZEDATA2_TX_LOCK_SIZE, + sze_q->tx_channel); + if (lck == NULL) + continue; + + dst = lck->start; + lock_size = lck->len; + lock_size2 = lck->next ? lck->next->len : 0; + +next_packet: + mbuf = bufs[nb_pkts - pkt_left]; + + pkt_len = mbuf->pkt_len; + mbuf_segs = mbuf->nb_segs; + + hwpkt_len = RTE_SZE2_PACKET_HEADER_SIZE_ALIGNED + + RTE_SZE2_ALIGN8(pkt_len); + + if (lock_size + lock_size2 < hwpkt_len) { + szedata_tx_unlock_data(sze_q->sze, lck, unlock_size); + continue; + } + + num_bytes += pkt_len; + + if (lock_size > hwpkt_len) { + void *tmp_dst; + + rem_len = 0; + + /* write packet length at first 2 bytes in 8B header */ + *((uint16_t *)dst) = htole16( + RTE_SZE2_PACKET_HEADER_SIZE_ALIGNED + + pkt_len); + *(((uint16_t *)dst) + 1) = htole16(0); + + /* copy packet from mbuf */ + tmp_dst = ((uint8_t *)(dst)) + + RTE_SZE2_PACKET_HEADER_SIZE_ALIGNED; + if (mbuf_segs == 1) { + /* + * non-scattered packet, + * transmit from one mbuf + */ + rte_memcpy(tmp_dst, + rte_pktmbuf_mtod(mbuf, const void *), + pkt_len); + } else { + /* scattered packet, transmit from more mbufs */ + struct rte_mbuf *m = mbuf; + while (m) { + rte_memcpy(tmp_dst, + rte_pktmbuf_mtod(m, + const void *), + m->data_len); + tmp_dst = ((uint8_t *)(tmp_dst)) + + m->data_len; + m = m->next; + } + } + + + dst = ((uint8_t *)dst) + hwpkt_len; + unlock_size += hwpkt_len; + lock_size -= hwpkt_len; + + rte_pktmbuf_free(mbuf); + num_tx++; + pkt_left--; + if (pkt_left == 0) { + szedata_tx_unlock_data(sze_q->sze, lck, + unlock_size); + break; + } + goto next_packet; + } else if (lock_size + lock_size2 >= hwpkt_len) { + void *tmp_dst; + uint16_t write_len; + + /* write packet length at first 2 bytes in 8B header */ + *((uint16_t *)dst) = + htole16(RTE_SZE2_PACKET_HEADER_SIZE_ALIGNED + + pkt_len); + *(((uint16_t *)dst) + 1) = htole16(0); + + /* + * If the raw packet (pkt_len) is smaller than lock_size + * get the correct length for memcpy + */ + write_len = + pkt_len < lock_size - + RTE_SZE2_PACKET_HEADER_SIZE_ALIGNED ? + pkt_len : + lock_size - RTE_SZE2_PACKET_HEADER_SIZE_ALIGNED; + + rem_len = hwpkt_len - lock_size; + + tmp_dst = ((uint8_t *)(dst)) + + RTE_SZE2_PACKET_HEADER_SIZE_ALIGNED; + if (mbuf_segs == 1) { + /* + * non-scattered packet, + * transmit from one mbuf + */ + /* copy part of packet to first area */ + rte_memcpy(tmp_dst, + rte_pktmbuf_mtod(mbuf, const void *), + write_len); + + if (lck->next) + dst = lck->next->start; + + /* copy part of packet to second area */ + rte_memcpy(dst, + (const void *)(rte_pktmbuf_mtod(mbuf, + const uint8_t *) + + write_len), pkt_len - write_len); } else { /* scattered packet, transmit from more mbufs */ struct rte_mbuf *m = mbuf; @@ -573,341 +893,209 @@ next_packet: } static int -init_rx_channels(struct rte_eth_dev *dev, int v) +eth_rx_queue_start(struct rte_eth_dev *dev, uint16_t rxq_id) { - struct pmd_internals *internals = dev->data->dev_private; + struct szedata2_rx_queue *rxq = dev->data->rx_queues[rxq_id]; int ret; - uint32_t i; - uint32_t count = internals->num_of_rx; - uint32_t num_sub = 0; - uint32_t x; - uint32_t rx; - uint32_t tx; - - rx = internals->sze_rx_req; - tx = 0; - - for (i = 0; i < count; i++) { - /* - * Open, subscribe rx,tx channels and start device - */ - if (v) - RTE_LOG(INFO, PMD, "Opening SZE device %u. time\n", i); - - internals->rx_queue[num_sub].sze = - szedata_open(internals->sze_dev); - if (internals->rx_queue[num_sub].sze == NULL) - return -1; - - /* separate least significant non-zero bit */ - x = rx & ((~rx) + 1); - - if (v) - RTE_LOG(INFO, PMD, "Subscribing rx channel: 0x%x " - "tx channel: 0x%x\n", x, tx); - - ret = szedata_subscribe3(internals->rx_queue[num_sub].sze, - &x, &tx); - if (ret) { - szedata_close(internals->rx_queue[num_sub].sze); - internals->rx_queue[num_sub].sze = NULL; - return -1; - } - - if (v) - RTE_LOG(INFO, PMD, "Subscribed rx channel: 0x%x " - "tx channel: 0x%x\n", x, tx); - - if (x) { - if (v) - RTE_LOG(INFO, PMD, "Starting SZE device for " - "rx queue: %u\n", num_sub); - - ret = szedata_start(internals->rx_queue[num_sub].sze); - if (ret) { - szedata_close(internals->rx_queue[num_sub].sze); - internals->rx_queue[num_sub].sze = NULL; - return -1; - } - - /* - * set to 1 all bits lower than bit set to 1 - * and that bit to 0 - */ - x -= 1; - internals->rx_queue[num_sub].rx_channel = - count_ones(x); - - if (v) - RTE_LOG(INFO, PMD, "Subscribed rx channel " - "no: %u\n", - internals->rx_queue[num_sub].rx_channel - ); - - num_sub++; - internals->nb_rx_queues = num_sub; - } else { - if (v) - RTE_LOG(INFO, PMD, - "Could not subscribe any rx channel. " - "Closing SZE device\n"); - - szedata_close(internals->rx_queue[num_sub].sze); - internals->rx_queue[num_sub].sze = NULL; - } - - /* set least significant non-zero bit to zero */ - rx = rx & (rx - 1); + struct pmd_internals *internals = (struct pmd_internals *) + dev->data->dev_private; + + if (rxq->sze == NULL) { + uint32_t rx = 1 << rxq->rx_channel; + uint32_t tx = 0; + rxq->sze = szedata_open(internals->sze_dev_path); + if (rxq->sze == NULL) + return -EINVAL; + ret = szedata_subscribe3(rxq->sze, &rx, &tx); + if (ret != 0 || rx == 0) + goto err; } - dev->data->nb_rx_queues = (uint16_t)num_sub; - - if (v) - RTE_LOG(INFO, PMD, "Successfully opened rx channels: %u\n", - num_sub); - - return 0; -} - -static int -init_tx_channels(struct rte_eth_dev *dev, int v) -{ - struct pmd_internals *internals = dev->data->dev_private; - int ret; - uint32_t i; - uint32_t count = internals->num_of_tx; - uint32_t num_sub = 0; - uint32_t x; - uint32_t rx; - uint32_t tx; - - rx = 0; - tx = internals->sze_tx_req; - - for (i = 0; i < count; i++) { - /* - * Open, subscribe rx,tx channels and start device - */ - if (v) - RTE_LOG(INFO, PMD, "Opening SZE device %u. time\n", - i + internals->num_of_rx); - - internals->tx_queue[num_sub].sze = - szedata_open(internals->sze_dev); - if (internals->tx_queue[num_sub].sze == NULL) - return -1; - - /* separate least significant non-zero bit */ - x = tx & ((~tx) + 1); - - if (v) - RTE_LOG(INFO, PMD, "Subscribing rx channel: 0x%x " - "tx channel: 0x%x\n", rx, x); - - ret = szedata_subscribe3(internals->tx_queue[num_sub].sze, - &rx, &x); - if (ret) { - szedata_close(internals->tx_queue[num_sub].sze); - internals->tx_queue[num_sub].sze = NULL; - return -1; - } - - if (v) - RTE_LOG(INFO, PMD, "Subscribed rx channel: 0x%x " - "tx channel: 0x%x\n", rx, x); - - if (x) { - if (v) - RTE_LOG(INFO, PMD, "Starting SZE device for " - "tx queue: %u\n", num_sub); - - ret = szedata_start(internals->tx_queue[num_sub].sze); - if (ret) { - szedata_close(internals->tx_queue[num_sub].sze); - internals->tx_queue[num_sub].sze = NULL; - return -1; - } - - /* - * set to 1 all bits lower than bit set to 1 - * and that bit to 0 - */ - x -= 1; - internals->tx_queue[num_sub].tx_channel = - count_ones(x); - - if (v) - RTE_LOG(INFO, PMD, "Subscribed tx channel " - "no: %u\n", - internals->tx_queue[num_sub].tx_channel - ); - - num_sub++; - internals->nb_tx_queues = num_sub; - } else { - if (v) - RTE_LOG(INFO, PMD, - "Could not subscribe any tx channel. " - "Closing SZE device\n"); - - szedata_close(internals->tx_queue[num_sub].sze); - internals->tx_queue[num_sub].sze = NULL; - } + ret = szedata_start(rxq->sze); + if (ret != 0) + goto err; + dev->data->rx_queue_state[rxq_id] = RTE_ETH_QUEUE_STATE_STARTED; + return 0; - /* set least significant non-zero bit to zero */ - tx = tx & (tx - 1); - } +err: + szedata_close(rxq->sze); + rxq->sze = NULL; + return -EINVAL; +} - dev->data->nb_tx_queues = (uint16_t)num_sub; +static int +eth_rx_queue_stop(struct rte_eth_dev *dev, uint16_t rxq_id) +{ + struct szedata2_rx_queue *rxq = dev->data->rx_queues[rxq_id]; - if (v) - RTE_LOG(INFO, PMD, "Successfully opened tx channels: %u\n", - num_sub); + if (rxq->sze != NULL) { + szedata_close(rxq->sze); + rxq->sze = NULL; + } + dev->data->rx_queue_state[rxq_id] = RTE_ETH_QUEUE_STATE_STOPPED; return 0; } -static void -close_rx_channels(struct rte_eth_dev *dev) +static int +eth_tx_queue_start(struct rte_eth_dev *dev, uint16_t txq_id) { - struct pmd_internals *internals = dev->data->dev_private; - uint32_t i; - uint32_t num_sub = internals->nb_rx_queues; - - for (i = 0; i < num_sub; i++) { - if (internals->rx_queue[i].sze != NULL) { - szedata_close(internals->rx_queue[i].sze); - internals->rx_queue[i].sze = NULL; - } + struct szedata2_tx_queue *txq = dev->data->tx_queues[txq_id]; + int ret; + struct pmd_internals *internals = (struct pmd_internals *) + dev->data->dev_private; + + if (txq->sze == NULL) { + uint32_t rx = 0; + uint32_t tx = 1 << txq->tx_channel; + txq->sze = szedata_open(internals->sze_dev_path); + if (txq->sze == NULL) + return -EINVAL; + ret = szedata_subscribe3(txq->sze, &rx, &tx); + if (ret != 0 || tx == 0) + goto err; } - /* set number of rx queues to zero */ - internals->nb_rx_queues = 0; - dev->data->nb_rx_queues = (uint16_t)0; + + ret = szedata_start(txq->sze); + if (ret != 0) + goto err; + dev->data->tx_queue_state[txq_id] = RTE_ETH_QUEUE_STATE_STARTED; + return 0; + +err: + szedata_close(txq->sze); + txq->sze = NULL; + return -EINVAL; } -static void -close_tx_channels(struct rte_eth_dev *dev) +static int +eth_tx_queue_stop(struct rte_eth_dev *dev, uint16_t txq_id) { - struct pmd_internals *internals = dev->data->dev_private; - uint32_t i; - uint32_t num_sub = internals->nb_tx_queues; + struct szedata2_tx_queue *txq = dev->data->tx_queues[txq_id]; - for (i = 0; i < num_sub; i++) { - if (internals->tx_queue[i].sze != NULL) { - szedata_close(internals->tx_queue[i].sze); - internals->tx_queue[i].sze = NULL; - } + if (txq->sze != NULL) { + szedata_close(txq->sze); + txq->sze = NULL; } - /* set number of rx queues to zero */ - internals->nb_tx_queues = 0; - dev->data->nb_tx_queues = (uint16_t)0; + + dev->data->tx_queue_state[txq_id] = RTE_ETH_QUEUE_STATE_STOPPED; + return 0; } static int eth_dev_start(struct rte_eth_dev *dev) { - struct pmd_internals *internals = dev->data->dev_private; int ret; - - if (internals->nb_rx_queues == 0) { - ret = init_rx_channels(dev, 0); - if (ret != 0) { - close_rx_channels(dev); - return -1; - } + uint16_t i; + uint16_t nb_rx = dev->data->nb_rx_queues; + uint16_t nb_tx = dev->data->nb_tx_queues; + + for (i = 0; i < nb_rx; i++) { + ret = eth_rx_queue_start(dev, i); + if (ret != 0) + goto err_rx; } - if (internals->nb_tx_queues == 0) { - ret = init_tx_channels(dev, 0); - if (ret != 0) { - close_tx_channels(dev); - close_rx_channels(dev); - return -1; - } + for (i = 0; i < nb_tx; i++) { + ret = eth_tx_queue_start(dev, i); + if (ret != 0) + goto err_tx; } - dev->data->dev_link.link_status = 1; return 0; + +err_tx: + for (i = 0; i < nb_tx; i++) + eth_tx_queue_stop(dev, i); +err_rx: + for (i = 0; i < nb_rx; i++) + eth_rx_queue_stop(dev, i); + return ret; } static void eth_dev_stop(struct rte_eth_dev *dev) { - unsigned i; - struct pmd_internals *internals = dev->data->dev_private; - - for (i = 0; i < internals->nb_rx_queues; i++) { - if (internals->rx_queue[i].sze != NULL) { - szedata_close(internals->rx_queue[i].sze); - internals->rx_queue[i].sze = NULL; - } - } - - for (i = 0; i < internals->nb_tx_queues; i++) { - if (internals->tx_queue[i].sze != NULL) { - szedata_close(internals->tx_queue[i].sze); - internals->tx_queue[i].sze = NULL; - } - } + uint16_t i; + uint16_t nb_rx = dev->data->nb_rx_queues; + uint16_t nb_tx = dev->data->nb_tx_queues; - internals->nb_rx_queues = 0; - internals->nb_tx_queues = 0; + for (i = 0; i < nb_tx; i++) + eth_tx_queue_stop(dev, i); - dev->data->nb_rx_queues = (uint16_t)0; - dev->data->nb_tx_queues = (uint16_t)0; - - dev->data->dev_link.link_status = 0; + for (i = 0; i < nb_rx; i++) + eth_rx_queue_stop(dev, i); } static int -eth_dev_configure(struct rte_eth_dev *dev __rte_unused) +eth_dev_configure(struct rte_eth_dev *dev) { + struct rte_eth_dev_data *data = dev->data; + if (data->dev_conf.rxmode.offloads & DEV_RX_OFFLOAD_SCATTER) { + dev->rx_pkt_burst = eth_szedata2_rx_scattered; + data->scattered_rx = 1; + } else { + dev->rx_pkt_burst = eth_szedata2_rx; + data->scattered_rx = 0; + } return 0; } -static void +static int eth_dev_info(struct rte_eth_dev *dev, struct rte_eth_dev_info *dev_info) { struct pmd_internals *internals = dev->data->dev_private; - dev_info->driver_name = drivername; - dev_info->if_index = internals->if_index; + + dev_info->if_index = 0; dev_info->max_mac_addrs = 1; dev_info->max_rx_pktlen = (uint32_t)-1; - dev_info->max_rx_queues = (uint16_t)internals->nb_rx_queues; - dev_info->max_tx_queues = (uint16_t)internals->nb_tx_queues; + dev_info->max_rx_queues = internals->max_rx_queues; + dev_info->max_tx_queues = internals->max_tx_queues; dev_info->min_rx_bufsize = 0; - dev_info->pci_dev = NULL; + dev_info->rx_offload_capa = DEV_RX_OFFLOAD_SCATTER; + dev_info->tx_offload_capa = 0; + dev_info->rx_queue_offload_capa = 0; + dev_info->tx_queue_offload_capa = 0; + dev_info->speed_capa = ETH_LINK_SPEED_100G; + + return 0; } -static void +static int eth_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats) { - unsigned i; + uint16_t i; + uint16_t nb_rx = dev->data->nb_rx_queues; + uint16_t nb_tx = dev->data->nb_tx_queues; uint64_t rx_total = 0; uint64_t tx_total = 0; uint64_t tx_err_total = 0; uint64_t rx_total_bytes = 0; uint64_t tx_total_bytes = 0; - const struct pmd_internals *internal = dev->data->dev_private; - - for (i = 0; i < RTE_ETHDEV_QUEUE_STAT_CNTRS && - i < internal->nb_rx_queues; i++) { - stats->q_ipackets[i] = internal->rx_queue[i].rx_pkts; - stats->q_ibytes[i] = internal->rx_queue[i].rx_bytes; - rx_total += stats->q_ipackets[i]; - rx_total_bytes += stats->q_ibytes[i]; + + for (i = 0; i < nb_rx; i++) { + struct szedata2_rx_queue *rxq = dev->data->rx_queues[i]; + + if (i < RTE_ETHDEV_QUEUE_STAT_CNTRS) { + stats->q_ipackets[i] = rxq->rx_pkts; + stats->q_ibytes[i] = rxq->rx_bytes; + } + rx_total += rxq->rx_pkts; + rx_total_bytes += rxq->rx_bytes; } - for (i = 0; i < RTE_ETHDEV_QUEUE_STAT_CNTRS && - i < internal->nb_tx_queues; i++) { - stats->q_opackets[i] = internal->tx_queue[i].tx_pkts; - stats->q_errors[i] = internal->tx_queue[i].err_pkts; - stats->q_obytes[i] = internal->tx_queue[i].tx_bytes; - tx_total += stats->q_opackets[i]; - tx_err_total += stats->q_errors[i]; - tx_total_bytes += stats->q_obytes[i]; + for (i = 0; i < nb_tx; i++) { + struct szedata2_tx_queue *txq = dev->data->tx_queues[i]; + + if (i < RTE_ETHDEV_QUEUE_STAT_CNTRS) { + stats->q_opackets[i] = txq->tx_pkts; + stats->q_obytes[i] = txq->tx_bytes; + } + tx_total += txq->tx_pkts; + tx_total_bytes += txq->tx_bytes; + tx_err_total += txq->err_pkts; } stats->ipackets = rx_total; @@ -915,60 +1103,113 @@ eth_stats_get(struct rte_eth_dev *dev, stats->ibytes = rx_total_bytes; stats->obytes = tx_total_bytes; stats->oerrors = tx_err_total; + stats->rx_nombuf = dev->data->rx_mbuf_alloc_failed; + + return 0; } -static void +static int eth_stats_reset(struct rte_eth_dev *dev) { - unsigned i; - struct pmd_internals *internal = dev->data->dev_private; - for (i = 0; i < internal->nb_rx_queues; i++) { - internal->rx_queue[i].rx_pkts = 0; - internal->rx_queue[i].rx_bytes = 0; + uint16_t i; + uint16_t nb_rx = dev->data->nb_rx_queues; + uint16_t nb_tx = dev->data->nb_tx_queues; + + for (i = 0; i < nb_rx; i++) { + struct szedata2_rx_queue *rxq = dev->data->rx_queues[i]; + rxq->rx_pkts = 0; + rxq->rx_bytes = 0; + rxq->err_pkts = 0; + } + for (i = 0; i < nb_tx; i++) { + struct szedata2_tx_queue *txq = dev->data->tx_queues[i]; + txq->tx_pkts = 0; + txq->tx_bytes = 0; + txq->err_pkts = 0; + } + + return 0; +} + +static void +eth_rx_queue_release(void *q) +{ + struct szedata2_rx_queue *rxq = (struct szedata2_rx_queue *)q; + + if (rxq != NULL) { + if (rxq->sze != NULL) + szedata_close(rxq->sze); + rte_free(rxq); } - for (i = 0; i < internal->nb_tx_queues; i++) { - internal->tx_queue[i].tx_pkts = 0; - internal->tx_queue[i].err_pkts = 0; - internal->tx_queue[i].tx_bytes = 0; +} + +static void +eth_tx_queue_release(void *q) +{ + struct szedata2_tx_queue *txq = (struct szedata2_tx_queue *)q; + + if (txq != NULL) { + if (txq->sze != NULL) + szedata_close(txq->sze); + rte_free(txq); } } static void eth_dev_close(struct rte_eth_dev *dev) { - unsigned i; struct pmd_internals *internals = dev->data->dev_private; + uint16_t i; + uint16_t nb_rx = dev->data->nb_rx_queues; + uint16_t nb_tx = dev->data->nb_tx_queues; - for (i = 0; i < internals->nb_rx_queues; i++) { - if (internals->rx_queue[i].sze != NULL) { - szedata_close(internals->rx_queue[i].sze); - internals->rx_queue[i].sze = NULL; - } - } + eth_dev_stop(dev); - for (i = 0; i < internals->nb_tx_queues; i++) { - if (internals->tx_queue[i].sze != NULL) { - szedata_close(internals->tx_queue[i].sze); - internals->tx_queue[i].sze = NULL; - } + free(internals->sze_dev_path); + + for (i = 0; i < nb_rx; i++) { + eth_rx_queue_release(dev->data->rx_queues[i]); + dev->data->rx_queues[i] = NULL; } + dev->data->nb_rx_queues = 0; + for (i = 0; i < nb_tx; i++) { + eth_tx_queue_release(dev->data->tx_queues[i]); + dev->data->tx_queues[i] = NULL; + } + dev->data->nb_tx_queues = 0; + + rte_free(dev->data->mac_addrs); + dev->data->mac_addrs = NULL; +} + +static int +eth_link_update(struct rte_eth_dev *dev, + int wait_to_complete __rte_unused) +{ + struct rte_eth_link link; + + memset(&link, 0, sizeof(link)); - internals->nb_rx_queues = 0; - internals->nb_tx_queues = 0; + link.link_speed = ETH_SPEED_NUM_100G; + link.link_duplex = ETH_LINK_FULL_DUPLEX; + link.link_status = ETH_LINK_UP; + link.link_autoneg = ETH_LINK_FIXED; - dev->data->nb_rx_queues = (uint16_t)0; - dev->data->nb_tx_queues = (uint16_t)0; + rte_eth_linkstatus_set(dev, &link); + return 0; } -static void -eth_queue_release(void *q __rte_unused) +static int +eth_dev_set_link_up(struct rte_eth_dev *dev __rte_unused) { + PMD_DRV_LOG(WARNING, "Setting link up is not supported."); + return 0; } static int -eth_link_update(struct rte_eth_dev *dev __rte_unused, - int wait_to_complete __rte_unused) +eth_dev_set_link_down(struct rte_eth_dev *dev __rte_unused) { + PMD_DRV_LOG(WARNING, "Setting link down is not supported."); return 0; } @@ -976,16 +1217,62 @@ static int eth_rx_queue_setup(struct rte_eth_dev *dev, uint16_t rx_queue_id, uint16_t nb_rx_desc __rte_unused, - unsigned int socket_id __rte_unused, + unsigned int socket_id, const struct rte_eth_rxconf *rx_conf __rte_unused, struct rte_mempool *mb_pool) { + struct szedata2_rx_queue *rxq; + int ret; struct pmd_internals *internals = dev->data->dev_private; - struct szedata2_rx_queue *szedata2_q = - &internals->rx_queue[rx_queue_id]; - szedata2_q->mb_pool = mb_pool; - dev->data->rx_queues[rx_queue_id] = szedata2_q; - szedata2_q->in_port = dev->data->port_id; + uint8_t rx_channel = internals->rxq_base_id + rx_queue_id; + uint32_t rx = 1 << rx_channel; + uint32_t tx = 0; + + PMD_INIT_FUNC_TRACE(); + + if (dev->data->rx_queues[rx_queue_id] != NULL) { + eth_rx_queue_release(dev->data->rx_queues[rx_queue_id]); + dev->data->rx_queues[rx_queue_id] = NULL; + } + + rxq = rte_zmalloc_socket("szedata2 rx queue", + sizeof(struct szedata2_rx_queue), + RTE_CACHE_LINE_SIZE, socket_id); + if (rxq == NULL) { + PMD_INIT_LOG(ERR, "rte_zmalloc_socket() failed for rx queue id " + "%" PRIu16 "!", rx_queue_id); + return -ENOMEM; + } + + rxq->priv = internals; + rxq->sze = szedata_open(internals->sze_dev_path); + if (rxq->sze == NULL) { + PMD_INIT_LOG(ERR, "szedata_open() failed for rx queue id " + "%" PRIu16 "!", rx_queue_id); + eth_rx_queue_release(rxq); + return -EINVAL; + } + ret = szedata_subscribe3(rxq->sze, &rx, &tx); + if (ret != 0 || rx == 0) { + PMD_INIT_LOG(ERR, "szedata_subscribe3() failed for rx queue id " + "%" PRIu16 "!", rx_queue_id); + eth_rx_queue_release(rxq); + return -EINVAL; + } + rxq->rx_channel = rx_channel; + rxq->qid = rx_queue_id; + rxq->in_port = dev->data->port_id; + rxq->mb_pool = mb_pool; + rxq->rx_pkts = 0; + rxq->rx_bytes = 0; + rxq->err_pkts = 0; + + dev->data->rx_queues[rx_queue_id] = rxq; + + PMD_INIT_LOG(DEBUG, "Configured rx queue id %" PRIu16 " on socket " + "%u (channel id %u).", rxq->qid, socket_id, + rxq->rx_channel); + return 0; } @@ -993,282 +1280,663 @@ static int eth_tx_queue_setup(struct rte_eth_dev *dev, uint16_t tx_queue_id, uint16_t nb_tx_desc __rte_unused, - unsigned int socket_id __rte_unused, + unsigned int socket_id, const struct rte_eth_txconf *tx_conf __rte_unused) { + struct szedata2_tx_queue *txq; + int ret; struct pmd_internals *internals = dev->data->dev_private; - dev->data->tx_queues[tx_queue_id] = &internals->tx_queue[tx_queue_id]; - return 0; -} + uint8_t tx_channel = internals->txq_base_id + tx_queue_id; + uint32_t rx = 0; + uint32_t tx = 1 << tx_channel; -static void -eth_mac_addr_set(struct rte_eth_dev *dev __rte_unused, - struct ether_addr *mac_addr __rte_unused) -{ -} + PMD_INIT_FUNC_TRACE(); -static struct eth_dev_ops ops = { - .dev_start = eth_dev_start, - .dev_stop = eth_dev_stop, - .dev_close = eth_dev_close, - .dev_configure = eth_dev_configure, - .dev_infos_get = eth_dev_info, - .rx_queue_setup = eth_rx_queue_setup, - .tx_queue_setup = eth_tx_queue_setup, - .rx_queue_release = eth_queue_release, - .tx_queue_release = eth_queue_release, - .link_update = eth_link_update, - .stats_get = eth_stats_get, - .stats_reset = eth_stats_reset, - .mac_addr_set = eth_mac_addr_set, -}; + if (dev->data->tx_queues[tx_queue_id] != NULL) { + eth_tx_queue_release(dev->data->tx_queues[tx_queue_id]); + dev->data->tx_queues[tx_queue_id] = NULL; + } -static int -parse_mask(const char *mask_str, uint32_t *mask_num) -{ - char *endptr; - long int value; + txq = rte_zmalloc_socket("szedata2 tx queue", + sizeof(struct szedata2_tx_queue), + RTE_CACHE_LINE_SIZE, socket_id); + if (txq == NULL) { + PMD_INIT_LOG(ERR, "rte_zmalloc_socket() failed for tx queue id " + "%" PRIu16 "!", tx_queue_id); + return -ENOMEM; + } - value = strtol(mask_str, &endptr, 0); - if (*endptr != '\0' || value > UINT32_MAX || value < 0) - return -1; + txq->priv = internals; + txq->sze = szedata_open(internals->sze_dev_path); + if (txq->sze == NULL) { + PMD_INIT_LOG(ERR, "szedata_open() failed for tx queue id " + "%" PRIu16 "!", tx_queue_id); + eth_tx_queue_release(txq); + return -EINVAL; + } + ret = szedata_subscribe3(txq->sze, &rx, &tx); + if (ret != 0 || tx == 0) { + PMD_INIT_LOG(ERR, "szedata_subscribe3() failed for tx queue id " + "%" PRIu16 "!", tx_queue_id); + eth_tx_queue_release(txq); + return -EINVAL; + } + txq->tx_channel = tx_channel; + txq->qid = tx_queue_id; + txq->tx_pkts = 0; + txq->tx_bytes = 0; + txq->err_pkts = 0; + + dev->data->tx_queues[tx_queue_id] = txq; + + PMD_INIT_LOG(DEBUG, "Configured tx queue id %" PRIu16 " on socket " + "%u (channel id %u).", txq->qid, socket_id, + txq->tx_channel); - *mask_num = (uint32_t)value; return 0; } static int -add_rx_mask(const char *key __rte_unused, const char *value, void *extra_args) +eth_mac_addr_set(struct rte_eth_dev *dev __rte_unused, + struct rte_ether_addr *mac_addr __rte_unused) { - struct rxtx_szedata2 *szedata2 = extra_args; - uint32_t mask; - - if (parse_mask(value, &mask) != 0) - return -1; - - szedata2->sze_rx_mask_req |= mask; return 0; } static int -add_tx_mask(const char *key __rte_unused, const char *value, void *extra_args) +eth_promiscuous_enable(struct rte_eth_dev *dev __rte_unused) { - struct rxtx_szedata2 *szedata2 = extra_args; - uint32_t mask; - - if (parse_mask(value, &mask) != 0) - return -1; - - szedata2->sze_tx_mask_req |= mask; + PMD_DRV_LOG(WARNING, "Enabling promiscuous mode is not supported. " + "The card is always in promiscuous mode."); return 0; } static int -rte_pmd_init_internals(const char *name, const unsigned nb_rx_queues, - const unsigned nb_tx_queues, - const unsigned numa_node, - struct pmd_internals **internals, - struct rte_eth_dev **eth_dev) +eth_promiscuous_disable(struct rte_eth_dev *dev __rte_unused) { - struct rte_eth_dev_data *data = NULL; + PMD_DRV_LOG(WARNING, "Disabling promiscuous mode is not supported. " + "The card is always in promiscuous mode."); + return -ENOTSUP; +} - RTE_LOG(INFO, PMD, - "Creating szedata2-backed ethdev on numa socket %u\n", - numa_node); +static int +eth_allmulticast_enable(struct rte_eth_dev *dev __rte_unused) +{ + PMD_DRV_LOG(WARNING, "Enabling allmulticast mode is not supported."); + return -ENOTSUP; +} - /* - * now do all data allocation - for eth_dev structure - * and internal (private) data - */ - data = rte_zmalloc_socket(name, sizeof(*data), 0, numa_node); - if (data == NULL) - goto error; +static int +eth_allmulticast_disable(struct rte_eth_dev *dev __rte_unused) +{ + PMD_DRV_LOG(WARNING, "Disabling allmulticast mode is not supported."); + return -ENOTSUP; +} - *internals = rte_zmalloc_socket(name, sizeof(**internals), 0, - numa_node); - if (*internals == NULL) - goto error; +static const struct eth_dev_ops ops = { + .dev_start = eth_dev_start, + .dev_stop = eth_dev_stop, + .dev_set_link_up = eth_dev_set_link_up, + .dev_set_link_down = eth_dev_set_link_down, + .dev_close = eth_dev_close, + .dev_configure = eth_dev_configure, + .dev_infos_get = eth_dev_info, + .promiscuous_enable = eth_promiscuous_enable, + .promiscuous_disable = eth_promiscuous_disable, + .allmulticast_enable = eth_allmulticast_enable, + .allmulticast_disable = eth_allmulticast_disable, + .rx_queue_start = eth_rx_queue_start, + .rx_queue_stop = eth_rx_queue_stop, + .tx_queue_start = eth_tx_queue_start, + .tx_queue_stop = eth_tx_queue_stop, + .rx_queue_setup = eth_rx_queue_setup, + .tx_queue_setup = eth_tx_queue_setup, + .rx_queue_release = eth_rx_queue_release, + .tx_queue_release = eth_tx_queue_release, + .link_update = eth_link_update, + .stats_get = eth_stats_get, + .stats_reset = eth_stats_reset, + .mac_addr_set = eth_mac_addr_set, +}; - /* reserve an ethdev entry */ - *eth_dev = rte_eth_dev_allocate(name, RTE_ETH_DEV_VIRTUAL); - if (*eth_dev == NULL) - goto error; +/* + * This function goes through sysfs and looks for an index of szedata2 + * device file (/dev/szedataIIX, where X is the index). + * + * @return + * 0 on success + * -1 on error + */ +static int +get_szedata2_index(const struct rte_pci_addr *pcislot_addr, uint32_t *index) +{ + DIR *dir; + struct dirent *entry; + int ret; + uint32_t tmp_index; + FILE *fd; + char pcislot_path[PATH_MAX]; + uint32_t domain; + uint8_t bus; + uint8_t devid; + uint8_t function; + + dir = opendir("/sys/class/combo"); + if (dir == NULL) + return -1; /* - * now put it all together - * - store queue data in internals, - * - store numa_node info in pci_driver - * - point eth_dev_data to internals - * - and point eth_dev structure to new eth_dev_data structure - * - * NOTE: we'll replace the data element, of originally allocated eth_dev - * so the rings are local per-process + * Iterate through all combosixX directories. + * When the value in /sys/class/combo/combosixX/device/pcislot + * file is the location of the ethernet device dev, "X" is the + * index of the device. */ + while ((entry = readdir(dir)) != NULL) { + ret = sscanf(entry->d_name, "combosix%u", &tmp_index); + if (ret != 1) + continue; - (*internals)->nb_rx_queues = nb_rx_queues; - (*internals)->nb_tx_queues = nb_tx_queues; - - (*internals)->if_index = 0; + snprintf(pcislot_path, PATH_MAX, + "/sys/class/combo/combosix%u/device/pcislot", + tmp_index); - data->dev_private = *internals; - data->port_id = (*eth_dev)->data->port_id; - snprintf(data->name, sizeof(data->name), "%s", (*eth_dev)->data->name); - data->nb_rx_queues = (uint16_t)nb_rx_queues; - data->nb_tx_queues = (uint16_t)nb_tx_queues; - data->dev_link = pmd_link; - data->mac_addrs = ð_addr; + fd = fopen(pcislot_path, "r"); + if (fd == NULL) + continue; - (*eth_dev)->data = data; - (*eth_dev)->dev_ops = &ops; - (*eth_dev)->data->dev_flags = RTE_ETH_DEV_DETACHABLE; - (*eth_dev)->driver = NULL; - (*eth_dev)->data->kdrv = RTE_KDRV_NONE; - (*eth_dev)->data->drv_name = drivername; - (*eth_dev)->data->numa_node = numa_node; + ret = fscanf(fd, "%8" SCNx32 ":%2" SCNx8 ":%2" SCNx8 ".%" SCNx8, + &domain, &bus, &devid, &function); + fclose(fd); + if (ret != 4) + continue; - return 0; + if (pcislot_addr->domain == domain && + pcislot_addr->bus == bus && + pcislot_addr->devid == devid && + pcislot_addr->function == function) { + *index = tmp_index; + closedir(dir); + return 0; + } + } -error: - rte_free(data); - rte_free(*internals); + closedir(dir); return -1; } +/** + * @brief Initializes rte_eth_dev device. + * @param dev Device to initialize. + * @param pi Structure with info about DMA queues. + * @return 0 on success, negative error code on error. + */ static int -rte_eth_from_szedata2(const char *name, - struct rxtx_szedata2 *szedata2, - const unsigned numa_node) +rte_szedata2_eth_dev_init(struct rte_eth_dev *dev, struct port_info *pi) { - struct pmd_internals *internals = NULL; - struct rte_eth_dev *eth_dev = NULL; int ret; + uint32_t szedata2_index; + char name[PATH_MAX]; + struct rte_eth_dev_data *data = dev->data; + struct pmd_internals *internals = (struct pmd_internals *) + data->dev_private; + struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev); - if (rte_pmd_init_internals(name, 0, 0, numa_node, - &internals, ð_dev) < 0) - return -1; + PMD_INIT_FUNC_TRACE(); - internals->sze_dev = szedata2->sze_dev; - internals->sze_rx_req = szedata2->sze_rx_mask_req; - internals->sze_tx_req = szedata2->sze_tx_mask_req; - internals->num_of_rx = szedata2->num_of_rx; - internals->num_of_tx = szedata2->num_of_tx; + PMD_INIT_LOG(INFO, "Initializing eth_dev %s (driver %s)", data->name, + RTE_STR(RTE_SZEDATA2_DRIVER_NAME)); - RTE_LOG(INFO, PMD, "Number of rx channels to open: %u mask: 0x%x\n", - internals->num_of_rx, internals->sze_rx_req); - RTE_LOG(INFO, PMD, "Number of tx channels to open: %u mask: 0x%x\n", - internals->num_of_tx, internals->sze_tx_req); + /* Let rte_eth_dev_close() release the port resources */ + dev->data->dev_flags |= RTE_ETH_DEV_CLOSE_REMOVE; - ret = init_rx_channels(eth_dev, 1); + /* Fill internal private structure. */ + internals->dev = dev; + /* Get index of szedata2 device file and create path to device file */ + ret = get_szedata2_index(&pci_dev->addr, &szedata2_index); if (ret != 0) { - close_rx_channels(eth_dev); - return -1; + PMD_INIT_LOG(ERR, "Failed to get szedata2 device index!"); + return -ENODEV; } - - ret = init_tx_channels(eth_dev, 1); - if (ret != 0) { - close_tx_channels(eth_dev); - close_rx_channels(eth_dev); - return -1; + snprintf(name, PATH_MAX, SZEDATA2_DEV_PATH_FMT, szedata2_index); + internals->sze_dev_path = strdup(name); + if (internals->sze_dev_path == NULL) { + PMD_INIT_LOG(ERR, "strdup() failed!"); + return -ENOMEM; + } + PMD_INIT_LOG(INFO, "SZEDATA2 path: %s", internals->sze_dev_path); + internals->max_rx_queues = pi->rx_count; + internals->max_tx_queues = pi->tx_count; + internals->rxq_base_id = pi->rx_base_id; + internals->txq_base_id = pi->tx_base_id; + PMD_INIT_LOG(INFO, "%u RX DMA channels from id %u", + internals->max_rx_queues, internals->rxq_base_id); + PMD_INIT_LOG(INFO, "%u TX DMA channels from id %u", + internals->max_tx_queues, internals->txq_base_id); + + /* Set rx, tx burst functions */ + if (data->scattered_rx == 1) + dev->rx_pkt_burst = eth_szedata2_rx_scattered; + else + dev->rx_pkt_burst = eth_szedata2_rx; + dev->tx_pkt_burst = eth_szedata2_tx; + + /* Set function callbacks for Ethernet API */ + dev->dev_ops = &ops; + + /* Get link state */ + eth_link_update(dev, 0); + + /* Allocate space for one mac address */ + data->mac_addrs = rte_zmalloc(data->name, sizeof(struct rte_ether_addr), + RTE_CACHE_LINE_SIZE); + if (data->mac_addrs == NULL) { + PMD_INIT_LOG(ERR, "Could not alloc space for MAC address!"); + free(internals->sze_dev_path); + return -ENOMEM; } - eth_dev->rx_pkt_burst = eth_szedata2_rx; - eth_dev->tx_pkt_burst = eth_szedata2_tx; + rte_ether_addr_copy(ð_addr, data->mac_addrs); + + PMD_INIT_LOG(INFO, "%s device %s successfully initialized", + RTE_STR(RTE_SZEDATA2_DRIVER_NAME), data->name); + + return 0; +} + +/** + * @brief Unitializes rte_eth_dev device. + * @param dev Device to uninitialize. + * @return 0 on success, negative error code on error. + */ +static int +rte_szedata2_eth_dev_uninit(struct rte_eth_dev *dev) +{ + PMD_INIT_FUNC_TRACE(); + + eth_dev_close(dev); + + PMD_DRV_LOG(INFO, "%s device %s successfully uninitialized", + RTE_STR(RTE_SZEDATA2_DRIVER_NAME), dev->data->name); return 0; } +static const struct rte_pci_id rte_szedata2_pci_id_table[] = { + { + RTE_PCI_DEVICE(PCI_VENDOR_ID_NETCOPE, + PCI_DEVICE_ID_NETCOPE_COMBO80G) + }, + { + RTE_PCI_DEVICE(PCI_VENDOR_ID_NETCOPE, + PCI_DEVICE_ID_NETCOPE_COMBO100G) + }, + { + RTE_PCI_DEVICE(PCI_VENDOR_ID_NETCOPE, + PCI_DEVICE_ID_NETCOPE_COMBO100G2) + }, + { + RTE_PCI_DEVICE(PCI_VENDOR_ID_NETCOPE, + PCI_DEVICE_ID_NETCOPE_NFB200G2QL) + }, + { + RTE_PCI_DEVICE(PCI_VENDOR_ID_SILICOM, + PCI_DEVICE_ID_FB2CGG3) + }, + { + RTE_PCI_DEVICE(PCI_VENDOR_ID_SILICOM, + PCI_DEVICE_ID_FB2CGG3D) + }, + { + .vendor_id = 0, + } +}; +/** + * @brief Gets info about DMA queues for ports. + * @param pci_dev PCI device structure. + * @param port_count Pointer to variable set with number of ports. + * @param pi Pointer to array of structures with info about DMA queues + * for ports. + * @param max_ports Maximum number of ports. + * @return 0 on success, negative error code on error. + */ static int -rte_pmd_szedata2_devinit(const char *name, const char *params) +get_port_info(struct rte_pci_device *pci_dev, unsigned int *port_count, + struct port_info *pi, unsigned int max_ports) { - unsigned numa_node; + struct szedata *szedata_temp; + char sze_dev_path[PATH_MAX]; + uint32_t szedata2_index; int ret; - struct rte_kvargs *kvlist; - unsigned k_idx; - struct rte_kvargs_pair *pair = NULL; - struct rxtx_szedata2 szedata2 = { 0, 0, 0, 0, NULL }; - bool dev_path_missing = true; + uint16_t max_rx_queues; + uint16_t max_tx_queues; - RTE_LOG(INFO, PMD, "Initializing pmd_szedata2 for %s\n", name); + if (max_ports == 0) + return -EINVAL; - numa_node = rte_socket_id(); + memset(pi, 0, max_ports * sizeof(struct port_info)); + *port_count = 0; - kvlist = rte_kvargs_parse(params, valid_arguments); - if (kvlist == NULL) - return -1; + /* Get index of szedata2 device file and create path to device file */ + ret = get_szedata2_index(&pci_dev->addr, &szedata2_index); + if (ret != 0) { + PMD_INIT_LOG(ERR, "Failed to get szedata2 device index!"); + return -ENODEV; + } + snprintf(sze_dev_path, PATH_MAX, SZEDATA2_DEV_PATH_FMT, szedata2_index); /* - * Get szedata2 device path and rx,tx channels from passed arguments. + * Get number of available DMA RX and TX channels, which is maximum + * number of queues that can be created. */ + szedata_temp = szedata_open(sze_dev_path); + if (szedata_temp == NULL) { + PMD_INIT_LOG(ERR, "szedata_open(%s) failed", sze_dev_path); + return -EINVAL; + } + max_rx_queues = szedata_ifaces_available(szedata_temp, SZE2_DIR_RX); + max_tx_queues = szedata_ifaces_available(szedata_temp, SZE2_DIR_TX); + PMD_INIT_LOG(INFO, "Available DMA channels RX: %u TX: %u", + max_rx_queues, max_tx_queues); + if (max_rx_queues > RTE_ETH_SZEDATA2_MAX_RX_QUEUES) { + PMD_INIT_LOG(ERR, "%u RX queues exceeds supported number %u", + max_rx_queues, RTE_ETH_SZEDATA2_MAX_RX_QUEUES); + szedata_close(szedata_temp); + return -EINVAL; + } + if (max_tx_queues > RTE_ETH_SZEDATA2_MAX_TX_QUEUES) { + PMD_INIT_LOG(ERR, "%u TX queues exceeds supported number %u", + max_tx_queues, RTE_ETH_SZEDATA2_MAX_TX_QUEUES); + szedata_close(szedata_temp); + return -EINVAL; + } - if (rte_kvargs_count(kvlist, RTE_ETH_SZEDATA2_DEV_PATH_ARG) != 1) - goto err; - - if (rte_kvargs_count(kvlist, RTE_ETH_SZEDATA2_RX_IFACES_ARG) < 1) - goto err; + if (pci_dev->id.device_id == PCI_DEVICE_ID_NETCOPE_NFB200G2QL) { + unsigned int i; + unsigned int rx_queues = max_rx_queues / max_ports; + unsigned int tx_queues = max_tx_queues / max_ports; - if (rte_kvargs_count(kvlist, RTE_ETH_SZEDATA2_TX_IFACES_ARG) < 1) - goto err; + /* + * Number of queues reported by szedata_ifaces_available() + * is the number of all queues from all DMA controllers which + * may reside at different numa locations. + * All queues from the same DMA controller have the same numa + * node. + * Numa node from the first queue of each DMA controller is + * retrieved. + * If the numa node differs from the numa node of the queues + * from the previous DMA controller the queues are assigned + * to the next port. + */ - for (k_idx = 0; k_idx < kvlist->count; k_idx++) { - pair = &kvlist->pairs[k_idx]; - if (strstr(pair->key, RTE_ETH_SZEDATA2_DEV_PATH_ARG) != NULL) { - szedata2.sze_dev = pair->value; - dev_path_missing = false; - break; + for (i = 0; i < max_ports; i++) { + int numa_rx = szedata_get_area_numa_node(szedata_temp, + SZE2_DIR_RX, rx_queues * i); + int numa_tx = szedata_get_area_numa_node(szedata_temp, + SZE2_DIR_TX, tx_queues * i); + unsigned int port_rx_queues = numa_rx != -1 ? + rx_queues : 0; + unsigned int port_tx_queues = numa_tx != -1 ? + tx_queues : 0; + PMD_INIT_LOG(DEBUG, "%u rx queues from id %u, numa %d", + rx_queues, rx_queues * i, numa_rx); + PMD_INIT_LOG(DEBUG, "%u tx queues from id %u, numa %d", + tx_queues, tx_queues * i, numa_tx); + + if (port_rx_queues != 0 && port_tx_queues != 0 && + numa_rx != numa_tx) { + PMD_INIT_LOG(ERR, "RX queue %u numa %d differs " + "from TX queue %u numa %d " + "unexpectedly", + rx_queues * i, numa_rx, + tx_queues * i, numa_tx); + szedata_close(szedata_temp); + return -EINVAL; + } else if (port_rx_queues == 0 && port_tx_queues == 0) { + continue; + } else { + unsigned int j; + unsigned int current = *port_count; + int port_numa = port_rx_queues != 0 ? + numa_rx : numa_tx; + + for (j = 0; j < *port_count; j++) { + if (pi[j].numa_node == + port_numa) { + current = j; + break; + } + } + if (pi[current].rx_count == 0 && + pi[current].tx_count == 0) { + pi[current].rx_base_id = rx_queues * i; + pi[current].tx_base_id = tx_queues * i; + (*port_count)++; + } else if ((rx_queues * i != + pi[current].rx_base_id + + pi[current].rx_count) || + (tx_queues * i != + pi[current].tx_base_id + + pi[current].tx_count)) { + PMD_INIT_LOG(ERR, "Queue ids does not " + "fulfill constraints"); + szedata_close(szedata_temp); + return -EINVAL; + } + pi[current].rx_count += port_rx_queues; + pi[current].tx_count += port_tx_queues; + pi[current].numa_node = port_numa; + } } + } else { + pi[0].rx_count = max_rx_queues; + pi[0].tx_count = max_tx_queues; + pi[0].numa_node = pci_dev->device.numa_node; + *port_count = 1; } - if (dev_path_missing) - goto err; + szedata_close(szedata_temp); + return 0; +} - ret = rte_kvargs_process(kvlist, RTE_ETH_SZEDATA2_RX_IFACES_ARG, - &add_rx_mask, &szedata2); - if (ret < 0) - goto err; +/** + * @brief Allocates rte_eth_dev device. + * @param pci_dev Corresponding PCI device. + * @param numa_node NUMA node on which device is allocated. + * @param port_no Id of rte_eth_device created on PCI device pci_dev. + * @return Pointer to allocated device or NULL on error. + */ +static struct rte_eth_dev * +szedata2_eth_dev_allocate(struct rte_pci_device *pci_dev, int numa_node, + unsigned int port_no) +{ + struct rte_eth_dev *eth_dev; + char name[RTE_ETH_NAME_MAX_LEN]; - ret = rte_kvargs_process(kvlist, RTE_ETH_SZEDATA2_TX_IFACES_ARG, - &add_tx_mask, &szedata2); - if (ret < 0) - goto err; + PMD_INIT_FUNC_TRACE(); - szedata2.num_of_rx = count_ones(szedata2.sze_rx_mask_req); - szedata2.num_of_tx = count_ones(szedata2.sze_tx_mask_req); + snprintf(name, RTE_ETH_NAME_MAX_LEN, "%s" + SZEDATA2_ETH_DEV_NAME_SUFFIX_FMT, + pci_dev->device.name, port_no); + PMD_INIT_LOG(DEBUG, "Allocating eth_dev %s", name); - RTE_LOG(INFO, PMD, "SZE device found at path %s\n", szedata2.sze_dev); + if (rte_eal_process_type() == RTE_PROC_PRIMARY) { + eth_dev = rte_eth_dev_allocate(name); + if (!eth_dev) + return NULL; - return rte_eth_from_szedata2(name, &szedata2, numa_node); -err: - rte_kvargs_free(kvlist); - return -1; + eth_dev->data->dev_private = rte_zmalloc_socket(name, + sizeof(struct pmd_internals), RTE_CACHE_LINE_SIZE, + numa_node); + if (!eth_dev->data->dev_private) { + rte_eth_dev_release_port(eth_dev); + return NULL; + } + } else { + eth_dev = rte_eth_dev_attach_secondary(name); + if (!eth_dev) + return NULL; + } + + eth_dev->device = &pci_dev->device; + rte_eth_copy_pci_info(eth_dev, pci_dev); + eth_dev->data->numa_node = numa_node; + return eth_dev; } -static int -rte_pmd_szedata2_devuninit(const char *name) +/** + * @brief Releases interval of rte_eth_dev devices from array. + * @param eth_devs Array of pointers to rte_eth_dev devices. + * @param from Index in array eth_devs to start with. + * @param to Index in array right after the last element to release. + * + * Used for releasing at failed initialization. + */ +static void +szedata2_eth_dev_release_interval(struct rte_eth_dev **eth_devs, + unsigned int from, unsigned int to) { - struct rte_eth_dev *dev = NULL; + unsigned int i; - RTE_LOG(INFO, PMD, "Uninitializing pmd_szedata2 for %s " - "on numa socket %u\n", name, rte_socket_id()); + PMD_INIT_FUNC_TRACE(); - if (name == NULL) - return -1; + for (i = from; i < to; i++) { + rte_szedata2_eth_dev_uninit(eth_devs[i]); + rte_eth_dev_pci_release(eth_devs[i]); + } +} - dev = rte_eth_dev_allocated(name); - if (dev == NULL) - return -1; +/** + * @brief Callback .probe for struct rte_pci_driver. + */ +static int szedata2_eth_pci_probe(struct rte_pci_driver *pci_drv __rte_unused, + struct rte_pci_device *pci_dev) +{ + struct port_info port_info[SZEDATA2_MAX_PORTS]; + unsigned int port_count; + int ret; + unsigned int i; + struct pci_dev_list_entry *list_entry; + struct rte_eth_dev *eth_devs[SZEDATA2_MAX_PORTS] = {NULL,}; + + PMD_INIT_FUNC_TRACE(); + + ret = get_port_info(pci_dev, &port_count, port_info, + SZEDATA2_MAX_PORTS); + if (ret != 0) + return ret; + + if (port_count == 0) { + PMD_INIT_LOG(ERR, "No available ports!"); + return -ENODEV; + } - rte_free(dev->data->dev_private); - rte_free(dev->data); - rte_eth_dev_release_port(dev); + list_entry = rte_zmalloc(NULL, sizeof(struct pci_dev_list_entry), + RTE_CACHE_LINE_SIZE); + if (list_entry == NULL) { + PMD_INIT_LOG(ERR, "rte_zmalloc() failed!"); + return -ENOMEM; + } + + for (i = 0; i < port_count; i++) { + eth_devs[i] = szedata2_eth_dev_allocate(pci_dev, + port_info[i].numa_node, i); + if (eth_devs[i] == NULL) { + PMD_INIT_LOG(ERR, "Failed to alloc eth_dev for port %u", + i); + szedata2_eth_dev_release_interval(eth_devs, 0, i); + rte_free(list_entry); + return -ENOMEM; + } + + ret = rte_szedata2_eth_dev_init(eth_devs[i], &port_info[i]); + if (ret != 0) { + PMD_INIT_LOG(ERR, "Failed to init eth_dev for port %u", + i); + rte_eth_dev_pci_release(eth_devs[i]); + szedata2_eth_dev_release_interval(eth_devs, 0, i); + rte_free(list_entry); + return ret; + } + + rte_eth_dev_probing_finish(eth_devs[i]); + } + + /* + * Add pci_dev to list of PCI devices for this driver + * which is used at remove callback to release all created eth_devs. + */ + list_entry->pci_dev = pci_dev; + list_entry->port_count = port_count; + LIST_INSERT_HEAD(&szedata2_pci_dev_list, list_entry, next); return 0; } -static struct rte_driver pmd_szedata2_drv = { - .name = "eth_szedata2", - .type = PMD_VDEV, - .init = rte_pmd_szedata2_devinit, - .uninit = rte_pmd_szedata2_devuninit, +/** + * @brief Callback .remove for struct rte_pci_driver. + */ +static int szedata2_eth_pci_remove(struct rte_pci_device *pci_dev) +{ + unsigned int i; + unsigned int port_count; + char name[RTE_ETH_NAME_MAX_LEN]; + struct rte_eth_dev *eth_dev; + int ret; + int retval = 0; + bool found = false; + struct pci_dev_list_entry *list_entry = NULL; + + PMD_INIT_FUNC_TRACE(); + + LIST_FOREACH(list_entry, &szedata2_pci_dev_list, next) { + if (list_entry->pci_dev == pci_dev) { + port_count = list_entry->port_count; + found = true; + break; + } + } + LIST_REMOVE(list_entry, next); + rte_free(list_entry); + + if (!found) { + PMD_DRV_LOG(ERR, "PCI device " PCI_PRI_FMT " not found", + pci_dev->addr.domain, pci_dev->addr.bus, + pci_dev->addr.devid, pci_dev->addr.function); + return -ENODEV; + } + + for (i = 0; i < port_count; i++) { + snprintf(name, RTE_ETH_NAME_MAX_LEN, "%s" + SZEDATA2_ETH_DEV_NAME_SUFFIX_FMT, + pci_dev->device.name, i); + PMD_DRV_LOG(DEBUG, "Removing eth_dev %s", name); + eth_dev = rte_eth_dev_allocated(name); + if (!eth_dev) { + PMD_DRV_LOG(ERR, "eth_dev %s not found", name); + retval = retval ? retval : -ENODEV; + } + + ret = rte_szedata2_eth_dev_uninit(eth_dev); + if (ret != 0) { + PMD_DRV_LOG(ERR, "eth_dev %s uninit failed", name); + retval = retval ? retval : ret; + } + + rte_eth_dev_pci_release(eth_dev); + } + + return retval; +} + +static struct rte_pci_driver szedata2_eth_driver = { + .id_table = rte_szedata2_pci_id_table, + .probe = szedata2_eth_pci_probe, + .remove = szedata2_eth_pci_remove, }; -PMD_REGISTER_DRIVER(pmd_szedata2_drv); +RTE_PMD_REGISTER_PCI(RTE_SZEDATA2_DRIVER_NAME, szedata2_eth_driver); +RTE_PMD_REGISTER_PCI_TABLE(RTE_SZEDATA2_DRIVER_NAME, rte_szedata2_pci_id_table); +RTE_PMD_REGISTER_KMOD_DEP(RTE_SZEDATA2_DRIVER_NAME, + "* combo6core & combov3 & szedata2 & ( szedata2_cv3 | szedata2_cv3_fdt )"); +RTE_LOG_REGISTER(szedata2_logtype_init, pmd.net.szedata2.init, NOTICE); +RTE_LOG_REGISTER(szedata2_logtype_driver, pmd.net.szedata2.driver, NOTICE);