net/liquidio: add Tx data path for single segment
[dpdk.git] / drivers / net / liquidio / lio_ethdev.c
index a1dcdf6..5c5aade 100644 (file)
 #include "lio_ethdev.h"
 #include "lio_rxtx.h"
 
+static uint64_t
+lio_hweight64(uint64_t w)
+{
+       uint64_t res = w - ((w >> 1) & 0x5555555555555555ul);
+
+       res =
+           (res & 0x3333333333333333ul) + ((res >> 2) & 0x3333333333333333ul);
+       res = (res + (res >> 4)) & 0x0F0F0F0F0F0F0F0Ful;
+       res = res + (res >> 8);
+       res = res + (res >> 16);
+
+       return (res + (res >> 32)) & 0x00000000000000FFul;
+}
+
+/**
+ * Setup our receive queue/ringbuffer. This is the
+ * queue the Octeon uses to send us packets and
+ * responses. We are given a memory pool for our
+ * packet buffers that are used to populate the receive
+ * queue.
+ *
+ * @param eth_dev
+ *    Pointer to the structure rte_eth_dev
+ * @param q_no
+ *    Queue number
+ * @param num_rx_descs
+ *    Number of entries in the queue
+ * @param socket_id
+ *    Where to allocate memory
+ * @param rx_conf
+ *    Pointer to the struction rte_eth_rxconf
+ * @param mp
+ *    Pointer to the packet pool
+ *
+ * @return
+ *    - On success, return 0
+ *    - On failure, return -1
+ */
+static int
+lio_dev_rx_queue_setup(struct rte_eth_dev *eth_dev, uint16_t q_no,
+                      uint16_t num_rx_descs, unsigned int socket_id,
+                      const struct rte_eth_rxconf *rx_conf __rte_unused,
+                      struct rte_mempool *mp)
+{
+       struct lio_device *lio_dev = LIO_DEV(eth_dev);
+       struct rte_pktmbuf_pool_private *mbp_priv;
+       uint32_t fw_mapped_oq;
+       uint16_t buf_size;
+
+       if (q_no >= lio_dev->nb_rx_queues) {
+               lio_dev_err(lio_dev, "Invalid rx queue number %u\n", q_no);
+               return -EINVAL;
+       }
+
+       lio_dev_dbg(lio_dev, "setting up rx queue %u\n", q_no);
+
+       fw_mapped_oq = lio_dev->linfo.rxpciq[q_no].s.q_no;
+
+       if ((lio_dev->droq[fw_mapped_oq]) &&
+           (num_rx_descs != lio_dev->droq[fw_mapped_oq]->max_count)) {
+               lio_dev_err(lio_dev,
+                           "Reconfiguring Rx descs not supported. Configure descs to same value %u or restart application\n",
+                           lio_dev->droq[fw_mapped_oq]->max_count);
+               return -ENOTSUP;
+       }
+
+       mbp_priv = rte_mempool_get_priv(mp);
+       buf_size = mbp_priv->mbuf_data_room_size - RTE_PKTMBUF_HEADROOM;
+
+       if (lio_setup_droq(lio_dev, fw_mapped_oq, num_rx_descs, buf_size, mp,
+                          socket_id)) {
+               lio_dev_err(lio_dev, "droq allocation failed\n");
+               return -1;
+       }
+
+       eth_dev->data->rx_queues[q_no] = lio_dev->droq[fw_mapped_oq];
+
+       return 0;
+}
+
+/**
+ * Release the receive queue/ringbuffer. Called by
+ * the upper layers.
+ *
+ * @param rxq
+ *    Opaque pointer to the receive queue to release
+ *
+ * @return
+ *    - nothing
+ */
+static void
+lio_dev_rx_queue_release(void *rxq)
+{
+       struct lio_droq *droq = rxq;
+       struct lio_device *lio_dev = droq->lio_dev;
+       int oq_no;
+
+       /* Run time queue deletion not supported */
+       if (lio_dev->port_configured)
+               return;
+
+       if (droq != NULL) {
+               oq_no = droq->q_no;
+               lio_delete_droq_queue(droq->lio_dev, oq_no);
+       }
+}
+
+/**
+ * Allocate and initialize SW ring. Initialize associated HW registers.
+ *
+ * @param eth_dev
+ *   Pointer to structure rte_eth_dev
+ *
+ * @param q_no
+ *   Queue number
+ *
+ * @param num_tx_descs
+ *   Number of ringbuffer descriptors
+ *
+ * @param socket_id
+ *   NUMA socket id, used for memory allocations
+ *
+ * @param tx_conf
+ *   Pointer to the structure rte_eth_txconf
+ *
+ * @return
+ *   - On success, return 0
+ *   - On failure, return -errno value
+ */
+static int
+lio_dev_tx_queue_setup(struct rte_eth_dev *eth_dev, uint16_t q_no,
+                      uint16_t num_tx_descs, unsigned int socket_id,
+                      const struct rte_eth_txconf *tx_conf __rte_unused)
+{
+       struct lio_device *lio_dev = LIO_DEV(eth_dev);
+       int fw_mapped_iq = lio_dev->linfo.txpciq[q_no].s.q_no;
+       int retval;
+
+       if (q_no >= lio_dev->nb_tx_queues) {
+               lio_dev_err(lio_dev, "Invalid tx queue number %u\n", q_no);
+               return -EINVAL;
+       }
+
+       lio_dev_dbg(lio_dev, "setting up tx queue %u\n", q_no);
+
+       if ((lio_dev->instr_queue[fw_mapped_iq] != NULL) &&
+           (num_tx_descs != lio_dev->instr_queue[fw_mapped_iq]->max_count)) {
+               lio_dev_err(lio_dev,
+                           "Reconfiguring Tx descs not supported. Configure descs to same value %u or restart application\n",
+                           lio_dev->instr_queue[fw_mapped_iq]->max_count);
+               return -ENOTSUP;
+       }
+
+       retval = lio_setup_iq(lio_dev, q_no, lio_dev->linfo.txpciq[q_no],
+                             num_tx_descs, lio_dev, socket_id);
+
+       if (retval) {
+               lio_dev_err(lio_dev, "Runtime IQ(TxQ) creation failed.\n");
+               return retval;
+       }
+
+       retval = lio_setup_sglists(lio_dev, q_no, fw_mapped_iq,
+                               lio_dev->instr_queue[fw_mapped_iq]->max_count,
+                               socket_id);
+
+       if (retval) {
+               lio_delete_instruction_queue(lio_dev, fw_mapped_iq);
+               return retval;
+       }
+
+       eth_dev->data->tx_queues[q_no] = lio_dev->instr_queue[fw_mapped_iq];
+
+       return 0;
+}
+
+static int lio_dev_configure(struct rte_eth_dev *eth_dev)
+{
+       struct lio_device *lio_dev = LIO_DEV(eth_dev);
+       uint16_t timeout = LIO_MAX_CMD_TIMEOUT;
+       int retval, num_iqueues, num_oqueues;
+       uint8_t mac[ETHER_ADDR_LEN], i;
+       struct lio_if_cfg_resp *resp;
+       struct lio_soft_command *sc;
+       union lio_if_cfg if_cfg;
+       uint32_t resp_size;
+
+       PMD_INIT_FUNC_TRACE();
+
+       /* Re-configuring firmware not supported.
+        * Can't change tx/rx queues per port from initial value.
+        */
+       if (lio_dev->port_configured) {
+               if ((lio_dev->nb_rx_queues != eth_dev->data->nb_rx_queues) ||
+                   (lio_dev->nb_tx_queues != eth_dev->data->nb_tx_queues)) {
+                       lio_dev_err(lio_dev,
+                                   "rxq/txq re-conf not supported. Restart application with new value.\n");
+                       return -ENOTSUP;
+               }
+               return 0;
+       }
+
+       lio_dev->nb_rx_queues = eth_dev->data->nb_rx_queues;
+       lio_dev->nb_tx_queues = eth_dev->data->nb_tx_queues;
+
+       resp_size = sizeof(struct lio_if_cfg_resp);
+       sc = lio_alloc_soft_command(lio_dev, 0, resp_size, 0);
+       if (sc == NULL)
+               return -ENOMEM;
+
+       resp = (struct lio_if_cfg_resp *)sc->virtrptr;
+
+       /* Firmware doesn't have capability to reconfigure the queues,
+        * Claim all queues, and use as many required
+        */
+       if_cfg.if_cfg64 = 0;
+       if_cfg.s.num_iqueues = lio_dev->nb_tx_queues;
+       if_cfg.s.num_oqueues = lio_dev->nb_rx_queues;
+       if_cfg.s.base_queue = 0;
+
+       if_cfg.s.gmx_port_id = lio_dev->pf_num;
+
+       lio_prepare_soft_command(lio_dev, sc, LIO_OPCODE,
+                                LIO_OPCODE_IF_CFG, 0,
+                                if_cfg.if_cfg64, 0);
+
+       /* Setting wait time in seconds */
+       sc->wait_time = LIO_MAX_CMD_TIMEOUT / 1000;
+
+       retval = lio_send_soft_command(lio_dev, sc);
+       if (retval == LIO_IQ_SEND_FAILED) {
+               lio_dev_err(lio_dev, "iq/oq config failed status: %x\n",
+                           retval);
+               /* Soft instr is freed by driver in case of failure. */
+               goto nic_config_fail;
+       }
+
+       /* Sleep on a wait queue till the cond flag indicates that the
+        * response arrived or timed-out.
+        */
+       while ((*sc->status_word == LIO_COMPLETION_WORD_INIT) && --timeout) {
+               lio_process_ordered_list(lio_dev);
+               rte_delay_ms(1);
+       }
+
+       retval = resp->status;
+       if (retval) {
+               lio_dev_err(lio_dev, "iq/oq config failed\n");
+               goto nic_config_fail;
+       }
+
+       lio_swap_8B_data((uint64_t *)(&resp->cfg_info),
+                        sizeof(struct octeon_if_cfg_info) >> 3);
+
+       num_iqueues = lio_hweight64(resp->cfg_info.iqmask);
+       num_oqueues = lio_hweight64(resp->cfg_info.oqmask);
+
+       if (!(num_iqueues) || !(num_oqueues)) {
+               lio_dev_err(lio_dev,
+                           "Got bad iqueues (%016lx) or oqueues (%016lx) from firmware.\n",
+                           (unsigned long)resp->cfg_info.iqmask,
+                           (unsigned long)resp->cfg_info.oqmask);
+               goto nic_config_fail;
+       }
+
+       lio_dev_dbg(lio_dev,
+                   "interface %d, iqmask %016lx, oqmask %016lx, numiqueues %d, numoqueues %d\n",
+                   eth_dev->data->port_id,
+                   (unsigned long)resp->cfg_info.iqmask,
+                   (unsigned long)resp->cfg_info.oqmask,
+                   num_iqueues, num_oqueues);
+
+       lio_dev->linfo.num_rxpciq = num_oqueues;
+       lio_dev->linfo.num_txpciq = num_iqueues;
+
+       for (i = 0; i < num_oqueues; i++) {
+               lio_dev->linfo.rxpciq[i].rxpciq64 =
+                   resp->cfg_info.linfo.rxpciq[i].rxpciq64;
+               lio_dev_dbg(lio_dev, "index %d OQ %d\n",
+                           i, lio_dev->linfo.rxpciq[i].s.q_no);
+       }
+
+       for (i = 0; i < num_iqueues; i++) {
+               lio_dev->linfo.txpciq[i].txpciq64 =
+                   resp->cfg_info.linfo.txpciq[i].txpciq64;
+               lio_dev_dbg(lio_dev, "index %d IQ %d\n",
+                           i, lio_dev->linfo.txpciq[i].s.q_no);
+       }
+
+       lio_dev->linfo.hw_addr = resp->cfg_info.linfo.hw_addr;
+       lio_dev->linfo.gmxport = resp->cfg_info.linfo.gmxport;
+       lio_dev->linfo.link.link_status64 =
+                       resp->cfg_info.linfo.link.link_status64;
+
+       /* 64-bit swap required on LE machines */
+       lio_swap_8B_data(&lio_dev->linfo.hw_addr, 1);
+       for (i = 0; i < ETHER_ADDR_LEN; i++)
+               mac[i] = *((uint8_t *)(((uint8_t *)&lio_dev->linfo.hw_addr) +
+                                      2 + i));
+
+       /* Copy the permanent MAC address */
+       ether_addr_copy((struct ether_addr *)mac, &eth_dev->data->mac_addrs[0]);
+
+       lio_dev->glist_lock =
+           rte_zmalloc(NULL, sizeof(*lio_dev->glist_lock) * num_iqueues, 0);
+       if (lio_dev->glist_lock == NULL)
+               return -ENOMEM;
+
+       lio_dev->glist_head =
+               rte_zmalloc(NULL, sizeof(*lio_dev->glist_head) * num_iqueues,
+                           0);
+       if (lio_dev->glist_head == NULL) {
+               rte_free(lio_dev->glist_lock);
+               lio_dev->glist_lock = NULL;
+               return -ENOMEM;
+       }
+
+       lio_dev->port_configured = 1;
+
+       lio_free_soft_command(sc);
+
+       /* Disable iq_0 for reconf */
+       lio_dev->fn_list.disable_io_queues(lio_dev);
+
+       /* Reset ioq regs */
+       lio_dev->fn_list.setup_device_regs(lio_dev);
+
+       /* Free iq_0 used during init */
+       lio_free_instr_queue0(lio_dev);
+
+       return 0;
+
+nic_config_fail:
+       lio_dev_err(lio_dev, "Failed retval %d\n", retval);
+       lio_free_soft_command(sc);
+       lio_free_instr_queue0(lio_dev);
+
+       return -ENODEV;
+}
+
+/* Define our ethernet definitions */
+static const struct eth_dev_ops liovf_eth_dev_ops = {
+       .dev_configure          = lio_dev_configure,
+       .rx_queue_setup         = lio_dev_rx_queue_setup,
+       .rx_queue_release       = lio_dev_rx_queue_release,
+       .tx_queue_setup         = lio_dev_tx_queue_setup,
+};
+
 static void
 lio_check_pf_hs_response(void *lio_dev)
 {
@@ -107,6 +454,11 @@ lio_first_time_init(struct lio_device *lio_dev,
                return -1;
        }
 
+       /* Initialize lists to manage the requests of different types that
+        * arrive from applications for this lio device.
+        */
+       lio_setup_response_list(lio_dev);
+
        if (lio_dev->fn_list.setup_mbox(lio_dev)) {
                lio_dev_err(lio_dev, "Mailbox setup failed\n");
                goto error;
@@ -144,6 +496,10 @@ lio_first_time_init(struct lio_device *lio_dev,
        lio_dev->max_tx_queues = dpdk_queues;
        lio_dev->max_rx_queues = dpdk_queues;
 
+       /* Enable input and output queues for this device */
+       if (lio_dev->fn_list.enable_io_queues(lio_dev))
+               goto error;
+
        return 0;
 
 error:
@@ -172,6 +528,9 @@ lio_eth_dev_uninit(struct rte_eth_dev *eth_dev)
        rte_free(eth_dev->data->mac_addrs);
        eth_dev->data->mac_addrs = NULL;
 
+       eth_dev->rx_pkt_burst = NULL;
+       eth_dev->tx_pkt_burst = NULL;
+
        return 0;
 }
 
@@ -183,6 +542,9 @@ lio_eth_dev_init(struct rte_eth_dev *eth_dev)
 
        PMD_INIT_FUNC_TRACE();
 
+       eth_dev->rx_pkt_burst = &lio_dev_recv_pkts;
+       eth_dev->tx_pkt_burst = &lio_dev_xmit_pkts;
+
        /* Primary does the initialization. */
        if (rte_eal_process_type() != RTE_PROC_PRIMARY)
                return 0;
@@ -210,13 +572,24 @@ lio_eth_dev_init(struct rte_eth_dev *eth_dev)
                return -EINVAL;
        }
 
+       eth_dev->dev_ops = &liovf_eth_dev_ops;
        eth_dev->data->mac_addrs = rte_zmalloc("lio", ETHER_ADDR_LEN, 0);
        if (eth_dev->data->mac_addrs == NULL) {
                lio_dev_err(lio_dev,
                            "MAC addresses memory allocation failed\n");
+               eth_dev->dev_ops = NULL;
+               eth_dev->rx_pkt_burst = NULL;
+               eth_dev->tx_pkt_burst = NULL;
                return -ENOMEM;
        }
 
+       rte_atomic64_set(&lio_dev->status, LIO_DEV_RUNNING);
+       rte_wmb();
+
+       lio_dev->port_configured = 0;
+       /* Always allow unicast packets */
+       lio_dev->ifflags |= LIO_IFFLAG_UNICAST;
+
        return 0;
 }