xref: /dpdk/drivers/net/cxgbe/cxgbe_ethdev.c (revision 89b890df3fbc12f5349d8fc84acbe46f8e79ff2d)
183189849SRahul Lakkireddy /*-
283189849SRahul Lakkireddy  *   BSD LICENSE
383189849SRahul Lakkireddy  *
413b0f500SRahul Lakkireddy  *   Copyright(c) 2014-2016 Chelsio Communications.
583189849SRahul Lakkireddy  *   All rights reserved.
683189849SRahul Lakkireddy  *
783189849SRahul Lakkireddy  *   Redistribution and use in source and binary forms, with or without
883189849SRahul Lakkireddy  *   modification, are permitted provided that the following conditions
983189849SRahul Lakkireddy  *   are met:
1083189849SRahul Lakkireddy  *
1183189849SRahul Lakkireddy  *     * Redistributions of source code must retain the above copyright
1283189849SRahul Lakkireddy  *       notice, this list of conditions and the following disclaimer.
1383189849SRahul Lakkireddy  *     * Redistributions in binary form must reproduce the above copyright
1483189849SRahul Lakkireddy  *       notice, this list of conditions and the following disclaimer in
1583189849SRahul Lakkireddy  *       the documentation and/or other materials provided with the
1683189849SRahul Lakkireddy  *       distribution.
1783189849SRahul Lakkireddy  *     * Neither the name of Chelsio Communications nor the names of its
1883189849SRahul Lakkireddy  *       contributors may be used to endorse or promote products derived
1983189849SRahul Lakkireddy  *       from this software without specific prior written permission.
2083189849SRahul Lakkireddy  *
2183189849SRahul Lakkireddy  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
2283189849SRahul Lakkireddy  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
2383189849SRahul Lakkireddy  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
2483189849SRahul Lakkireddy  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
2583189849SRahul Lakkireddy  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2683189849SRahul Lakkireddy  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2783189849SRahul Lakkireddy  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2883189849SRahul Lakkireddy  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2983189849SRahul Lakkireddy  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
3083189849SRahul Lakkireddy  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
3183189849SRahul Lakkireddy  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3283189849SRahul Lakkireddy  */
3383189849SRahul Lakkireddy 
3483189849SRahul Lakkireddy #include <sys/queue.h>
3583189849SRahul Lakkireddy #include <stdio.h>
3683189849SRahul Lakkireddy #include <errno.h>
3783189849SRahul Lakkireddy #include <stdint.h>
3883189849SRahul Lakkireddy #include <string.h>
3983189849SRahul Lakkireddy #include <unistd.h>
4083189849SRahul Lakkireddy #include <stdarg.h>
4183189849SRahul Lakkireddy #include <inttypes.h>
4283189849SRahul Lakkireddy #include <netinet/in.h>
4383189849SRahul Lakkireddy 
4483189849SRahul Lakkireddy #include <rte_byteorder.h>
4583189849SRahul Lakkireddy #include <rte_common.h>
4683189849SRahul Lakkireddy #include <rte_cycles.h>
4783189849SRahul Lakkireddy #include <rte_interrupts.h>
4883189849SRahul Lakkireddy #include <rte_log.h>
4983189849SRahul Lakkireddy #include <rte_debug.h>
5083189849SRahul Lakkireddy #include <rte_pci.h>
5183189849SRahul Lakkireddy #include <rte_atomic.h>
5283189849SRahul Lakkireddy #include <rte_branch_prediction.h>
5383189849SRahul Lakkireddy #include <rte_memory.h>
5483189849SRahul Lakkireddy #include <rte_memzone.h>
5583189849SRahul Lakkireddy #include <rte_tailq.h>
5683189849SRahul Lakkireddy #include <rte_eal.h>
5783189849SRahul Lakkireddy #include <rte_alarm.h>
5883189849SRahul Lakkireddy #include <rte_ether.h>
5983189849SRahul Lakkireddy #include <rte_ethdev.h>
6083189849SRahul Lakkireddy #include <rte_atomic.h>
6183189849SRahul Lakkireddy #include <rte_malloc.h>
6283189849SRahul Lakkireddy #include <rte_random.h>
6383189849SRahul Lakkireddy #include <rte_dev.h>
6483189849SRahul Lakkireddy 
6583189849SRahul Lakkireddy #include "cxgbe.h"
6683189849SRahul Lakkireddy 
6783189849SRahul Lakkireddy /*
6883189849SRahul Lakkireddy  * Macros needed to support the PCI Device ID Table ...
6983189849SRahul Lakkireddy  */
7083189849SRahul Lakkireddy #define CH_PCI_DEVICE_ID_TABLE_DEFINE_BEGIN \
7183189849SRahul Lakkireddy 	static struct rte_pci_id cxgb4_pci_tbl[] = {
7283189849SRahul Lakkireddy #define CH_PCI_DEVICE_ID_FUNCTION 0x4
7383189849SRahul Lakkireddy 
7483189849SRahul Lakkireddy #define PCI_VENDOR_ID_CHELSIO 0x1425
7583189849SRahul Lakkireddy 
7683189849SRahul Lakkireddy #define CH_PCI_ID_TABLE_ENTRY(devid) \
7783189849SRahul Lakkireddy 		{ RTE_PCI_DEVICE(PCI_VENDOR_ID_CHELSIO, (devid)) }
7883189849SRahul Lakkireddy 
7983189849SRahul Lakkireddy #define CH_PCI_DEVICE_ID_TABLE_DEFINE_END \
8083189849SRahul Lakkireddy 		{ .vendor_id = 0, } \
8183189849SRahul Lakkireddy 	}
8283189849SRahul Lakkireddy 
8383189849SRahul Lakkireddy /*
8483189849SRahul Lakkireddy  *... and the PCI ID Table itself ...
8583189849SRahul Lakkireddy  */
8683189849SRahul Lakkireddy #include "t4_pci_id_tbl.h"
8783189849SRahul Lakkireddy 
884a01078bSRahul Lakkireddy static uint16_t cxgbe_xmit_pkts(void *tx_queue, struct rte_mbuf **tx_pkts,
894a01078bSRahul Lakkireddy 				uint16_t nb_pkts)
904a01078bSRahul Lakkireddy {
914a01078bSRahul Lakkireddy 	struct sge_eth_txq *txq = (struct sge_eth_txq *)tx_queue;
924a01078bSRahul Lakkireddy 	uint16_t pkts_sent, pkts_remain;
934a01078bSRahul Lakkireddy 	uint16_t total_sent = 0;
944a01078bSRahul Lakkireddy 	int ret = 0;
954a01078bSRahul Lakkireddy 
964a01078bSRahul Lakkireddy 	CXGBE_DEBUG_TX(adapter, "%s: txq = %p; tx_pkts = %p; nb_pkts = %d\n",
974a01078bSRahul Lakkireddy 		       __func__, txq, tx_pkts, nb_pkts);
984a01078bSRahul Lakkireddy 
994a01078bSRahul Lakkireddy 	t4_os_lock(&txq->txq_lock);
1004a01078bSRahul Lakkireddy 	/* free up desc from already completed tx */
1014a01078bSRahul Lakkireddy 	reclaim_completed_tx(&txq->q);
1024a01078bSRahul Lakkireddy 	while (total_sent < nb_pkts) {
1034a01078bSRahul Lakkireddy 		pkts_remain = nb_pkts - total_sent;
1044a01078bSRahul Lakkireddy 
1054a01078bSRahul Lakkireddy 		for (pkts_sent = 0; pkts_sent < pkts_remain; pkts_sent++) {
1064a01078bSRahul Lakkireddy 			ret = t4_eth_xmit(txq, tx_pkts[total_sent + pkts_sent]);
1074a01078bSRahul Lakkireddy 			if (ret < 0)
1084a01078bSRahul Lakkireddy 				break;
1094a01078bSRahul Lakkireddy 		}
1104a01078bSRahul Lakkireddy 		if (!pkts_sent)
1114a01078bSRahul Lakkireddy 			break;
1124a01078bSRahul Lakkireddy 		total_sent += pkts_sent;
1134a01078bSRahul Lakkireddy 		/* reclaim as much as possible */
1144a01078bSRahul Lakkireddy 		reclaim_completed_tx(&txq->q);
1154a01078bSRahul Lakkireddy 	}
1164a01078bSRahul Lakkireddy 
1174a01078bSRahul Lakkireddy 	t4_os_unlock(&txq->txq_lock);
1184a01078bSRahul Lakkireddy 	return total_sent;
1194a01078bSRahul Lakkireddy }
1204a01078bSRahul Lakkireddy 
12192c8a632SRahul Lakkireddy static uint16_t cxgbe_recv_pkts(void *rx_queue, struct rte_mbuf **rx_pkts,
12292c8a632SRahul Lakkireddy 				uint16_t nb_pkts)
12392c8a632SRahul Lakkireddy {
12492c8a632SRahul Lakkireddy 	struct sge_eth_rxq *rxq = (struct sge_eth_rxq *)rx_queue;
12592c8a632SRahul Lakkireddy 	unsigned int work_done;
12692c8a632SRahul Lakkireddy 
12792c8a632SRahul Lakkireddy 	CXGBE_DEBUG_RX(adapter, "%s: rxq->rspq.cntxt_id = %u; nb_pkts = %d\n",
12892c8a632SRahul Lakkireddy 		       __func__, rxq->rspq.cntxt_id, nb_pkts);
12992c8a632SRahul Lakkireddy 
13092c8a632SRahul Lakkireddy 	if (cxgbe_poll(&rxq->rspq, rx_pkts, (unsigned int)nb_pkts, &work_done))
13192c8a632SRahul Lakkireddy 		dev_err(adapter, "error in cxgbe poll\n");
13292c8a632SRahul Lakkireddy 
13392c8a632SRahul Lakkireddy 	CXGBE_DEBUG_RX(adapter, "%s: work_done = %u\n", __func__, work_done);
13492c8a632SRahul Lakkireddy 	return work_done;
13592c8a632SRahul Lakkireddy }
13692c8a632SRahul Lakkireddy 
13792c8a632SRahul Lakkireddy static void cxgbe_dev_info_get(struct rte_eth_dev *eth_dev,
13892c8a632SRahul Lakkireddy 			       struct rte_eth_dev_info *device_info)
13992c8a632SRahul Lakkireddy {
14092c8a632SRahul Lakkireddy 	struct port_info *pi = (struct port_info *)(eth_dev->data->dev_private);
14192c8a632SRahul Lakkireddy 	struct adapter *adapter = pi->adapter;
14292c8a632SRahul Lakkireddy 	int max_queues = adapter->sge.max_ethqsets / adapter->params.nports;
14392c8a632SRahul Lakkireddy 
144946c9ed9SKonstantin Ananyev 	static const struct rte_eth_desc_lim cxgbe_desc_lim = {
145946c9ed9SKonstantin Ananyev 		.nb_max = CXGBE_MAX_RING_DESC_SIZE,
146946c9ed9SKonstantin Ananyev 		.nb_min = CXGBE_MIN_RING_DESC_SIZE,
147946c9ed9SKonstantin Ananyev 		.nb_align = 1,
148946c9ed9SKonstantin Ananyev 	};
149946c9ed9SKonstantin Ananyev 
1504b2eff45SRahul Lakkireddy 	device_info->min_rx_bufsize = CXGBE_MIN_RX_BUFSIZE;
1514b2eff45SRahul Lakkireddy 	device_info->max_rx_pktlen = CXGBE_MAX_RX_PKTLEN;
15292c8a632SRahul Lakkireddy 	device_info->max_rx_queues = max_queues;
15392c8a632SRahul Lakkireddy 	device_info->max_tx_queues = max_queues;
15492c8a632SRahul Lakkireddy 	device_info->max_mac_addrs = 1;
15592c8a632SRahul Lakkireddy 	/* XXX: For now we support one MAC/port */
15692c8a632SRahul Lakkireddy 	device_info->max_vfs = adapter->params.arch.vfcount;
15792c8a632SRahul Lakkireddy 	device_info->max_vmdq_pools = 0; /* XXX: For now no support for VMDQ */
15892c8a632SRahul Lakkireddy 
15992c8a632SRahul Lakkireddy 	device_info->rx_offload_capa = DEV_RX_OFFLOAD_VLAN_STRIP |
16092c8a632SRahul Lakkireddy 				       DEV_RX_OFFLOAD_IPV4_CKSUM |
16192c8a632SRahul Lakkireddy 				       DEV_RX_OFFLOAD_UDP_CKSUM |
16292c8a632SRahul Lakkireddy 				       DEV_RX_OFFLOAD_TCP_CKSUM;
16392c8a632SRahul Lakkireddy 
16492c8a632SRahul Lakkireddy 	device_info->tx_offload_capa = DEV_TX_OFFLOAD_VLAN_INSERT |
16592c8a632SRahul Lakkireddy 				       DEV_TX_OFFLOAD_IPV4_CKSUM |
16692c8a632SRahul Lakkireddy 				       DEV_TX_OFFLOAD_UDP_CKSUM |
16792c8a632SRahul Lakkireddy 				       DEV_TX_OFFLOAD_TCP_CKSUM |
16892c8a632SRahul Lakkireddy 				       DEV_TX_OFFLOAD_TCP_TSO;
16992c8a632SRahul Lakkireddy 
17092c8a632SRahul Lakkireddy 	device_info->reta_size = pi->rss_size;
171946c9ed9SKonstantin Ananyev 
172946c9ed9SKonstantin Ananyev 	device_info->rx_desc_lim = cxgbe_desc_lim;
173946c9ed9SKonstantin Ananyev 	device_info->tx_desc_lim = cxgbe_desc_lim;
17492c8a632SRahul Lakkireddy }
17592c8a632SRahul Lakkireddy 
176cdac6e2eSRahul Lakkireddy static void cxgbe_dev_promiscuous_enable(struct rte_eth_dev *eth_dev)
177cdac6e2eSRahul Lakkireddy {
178cdac6e2eSRahul Lakkireddy 	struct port_info *pi = (struct port_info *)(eth_dev->data->dev_private);
179cdac6e2eSRahul Lakkireddy 	struct adapter *adapter = pi->adapter;
180cdac6e2eSRahul Lakkireddy 
181cdac6e2eSRahul Lakkireddy 	t4_set_rxmode(adapter, adapter->mbox, pi->viid, -1,
182cdac6e2eSRahul Lakkireddy 		      1, -1, 1, -1, false);
183cdac6e2eSRahul Lakkireddy }
184cdac6e2eSRahul Lakkireddy 
185cdac6e2eSRahul Lakkireddy static void cxgbe_dev_promiscuous_disable(struct rte_eth_dev *eth_dev)
186cdac6e2eSRahul Lakkireddy {
187cdac6e2eSRahul Lakkireddy 	struct port_info *pi = (struct port_info *)(eth_dev->data->dev_private);
188cdac6e2eSRahul Lakkireddy 	struct adapter *adapter = pi->adapter;
189cdac6e2eSRahul Lakkireddy 
190cdac6e2eSRahul Lakkireddy 	t4_set_rxmode(adapter, adapter->mbox, pi->viid, -1,
191cdac6e2eSRahul Lakkireddy 		      0, -1, 1, -1, false);
192cdac6e2eSRahul Lakkireddy }
193cdac6e2eSRahul Lakkireddy 
194cdac6e2eSRahul Lakkireddy static void cxgbe_dev_allmulticast_enable(struct rte_eth_dev *eth_dev)
195cdac6e2eSRahul Lakkireddy {
196cdac6e2eSRahul Lakkireddy 	struct port_info *pi = (struct port_info *)(eth_dev->data->dev_private);
197cdac6e2eSRahul Lakkireddy 	struct adapter *adapter = pi->adapter;
198cdac6e2eSRahul Lakkireddy 
199cdac6e2eSRahul Lakkireddy 	/* TODO: address filters ?? */
200cdac6e2eSRahul Lakkireddy 
201cdac6e2eSRahul Lakkireddy 	t4_set_rxmode(adapter, adapter->mbox, pi->viid, -1,
202cdac6e2eSRahul Lakkireddy 		      -1, 1, 1, -1, false);
203cdac6e2eSRahul Lakkireddy }
204cdac6e2eSRahul Lakkireddy 
205cdac6e2eSRahul Lakkireddy static void cxgbe_dev_allmulticast_disable(struct rte_eth_dev *eth_dev)
206cdac6e2eSRahul Lakkireddy {
207cdac6e2eSRahul Lakkireddy 	struct port_info *pi = (struct port_info *)(eth_dev->data->dev_private);
208cdac6e2eSRahul Lakkireddy 	struct adapter *adapter = pi->adapter;
209cdac6e2eSRahul Lakkireddy 
210cdac6e2eSRahul Lakkireddy 	/* TODO: address filters ?? */
211cdac6e2eSRahul Lakkireddy 
212cdac6e2eSRahul Lakkireddy 	t4_set_rxmode(adapter, adapter->mbox, pi->viid, -1,
213cdac6e2eSRahul Lakkireddy 		      -1, 0, 1, -1, false);
214cdac6e2eSRahul Lakkireddy }
215cdac6e2eSRahul Lakkireddy 
216cdac6e2eSRahul Lakkireddy static int cxgbe_dev_link_update(struct rte_eth_dev *eth_dev,
217cdac6e2eSRahul Lakkireddy 				 __rte_unused int wait_to_complete)
218cdac6e2eSRahul Lakkireddy {
219cdac6e2eSRahul Lakkireddy 	struct port_info *pi = (struct port_info *)(eth_dev->data->dev_private);
220cdac6e2eSRahul Lakkireddy 	struct adapter *adapter = pi->adapter;
221cdac6e2eSRahul Lakkireddy 	struct sge *s = &adapter->sge;
222cdac6e2eSRahul Lakkireddy 	struct rte_eth_link *old_link = &eth_dev->data->dev_link;
223cdac6e2eSRahul Lakkireddy 	unsigned int work_done, budget = 4;
224cdac6e2eSRahul Lakkireddy 
225cdac6e2eSRahul Lakkireddy 	cxgbe_poll(&s->fw_evtq, NULL, budget, &work_done);
226cdac6e2eSRahul Lakkireddy 	if (old_link->link_status == pi->link_cfg.link_ok)
227cdac6e2eSRahul Lakkireddy 		return -1;  /* link not changed */
228cdac6e2eSRahul Lakkireddy 
229cdac6e2eSRahul Lakkireddy 	eth_dev->data->dev_link.link_status = pi->link_cfg.link_ok;
230cdac6e2eSRahul Lakkireddy 	eth_dev->data->dev_link.link_duplex = ETH_LINK_FULL_DUPLEX;
231cdac6e2eSRahul Lakkireddy 	eth_dev->data->dev_link.link_speed = pi->link_cfg.speed;
232cdac6e2eSRahul Lakkireddy 
233cdac6e2eSRahul Lakkireddy 	/* link has changed */
234cdac6e2eSRahul Lakkireddy 	return 0;
235cdac6e2eSRahul Lakkireddy }
236cdac6e2eSRahul Lakkireddy 
2370ec33be4SRahul Lakkireddy static int cxgbe_dev_mtu_set(struct rte_eth_dev *eth_dev, uint16_t mtu)
2380ec33be4SRahul Lakkireddy {
2390ec33be4SRahul Lakkireddy 	struct port_info *pi = (struct port_info *)(eth_dev->data->dev_private);
2400ec33be4SRahul Lakkireddy 	struct adapter *adapter = pi->adapter;
2410ec33be4SRahul Lakkireddy 	struct rte_eth_dev_info dev_info;
2420ec33be4SRahul Lakkireddy 	int err;
2430ec33be4SRahul Lakkireddy 	uint16_t new_mtu = mtu + ETHER_HDR_LEN + ETHER_CRC_LEN;
2440ec33be4SRahul Lakkireddy 
2450ec33be4SRahul Lakkireddy 	cxgbe_dev_info_get(eth_dev, &dev_info);
2460ec33be4SRahul Lakkireddy 
2470ec33be4SRahul Lakkireddy 	/* Must accommodate at least ETHER_MIN_MTU */
2480ec33be4SRahul Lakkireddy 	if ((new_mtu < ETHER_MIN_MTU) || (new_mtu > dev_info.max_rx_pktlen))
2490ec33be4SRahul Lakkireddy 		return -EINVAL;
2500ec33be4SRahul Lakkireddy 
2510ec33be4SRahul Lakkireddy 	/* set to jumbo mode if needed */
2520ec33be4SRahul Lakkireddy 	if (new_mtu > ETHER_MAX_LEN)
2530ec33be4SRahul Lakkireddy 		eth_dev->data->dev_conf.rxmode.jumbo_frame = 1;
2540ec33be4SRahul Lakkireddy 	else
2550ec33be4SRahul Lakkireddy 		eth_dev->data->dev_conf.rxmode.jumbo_frame = 0;
2560ec33be4SRahul Lakkireddy 
2570ec33be4SRahul Lakkireddy 	err = t4_set_rxmode(adapter, adapter->mbox, pi->viid, new_mtu, -1, -1,
2580ec33be4SRahul Lakkireddy 			    -1, -1, true);
2590ec33be4SRahul Lakkireddy 	if (!err)
2600ec33be4SRahul Lakkireddy 		eth_dev->data->dev_conf.rxmode.max_rx_pkt_len = new_mtu;
2610ec33be4SRahul Lakkireddy 
2620ec33be4SRahul Lakkireddy 	return err;
2630ec33be4SRahul Lakkireddy }
2640ec33be4SRahul Lakkireddy 
2654a01078bSRahul Lakkireddy static int cxgbe_dev_tx_queue_start(struct rte_eth_dev *eth_dev,
2664a01078bSRahul Lakkireddy 				    uint16_t tx_queue_id);
26792c8a632SRahul Lakkireddy static int cxgbe_dev_rx_queue_start(struct rte_eth_dev *eth_dev,
26892c8a632SRahul Lakkireddy 				    uint16_t tx_queue_id);
2694a01078bSRahul Lakkireddy static void cxgbe_dev_tx_queue_release(void *q);
27092c8a632SRahul Lakkireddy static void cxgbe_dev_rx_queue_release(void *q);
27192c8a632SRahul Lakkireddy 
2720462d115SRahul Lakkireddy /*
2730462d115SRahul Lakkireddy  * Stop device.
2740462d115SRahul Lakkireddy  */
2750462d115SRahul Lakkireddy static void cxgbe_dev_close(struct rte_eth_dev *eth_dev)
2760462d115SRahul Lakkireddy {
2770462d115SRahul Lakkireddy 	struct port_info *pi = (struct port_info *)(eth_dev->data->dev_private);
2780462d115SRahul Lakkireddy 	struct adapter *adapter = pi->adapter;
2790462d115SRahul Lakkireddy 	int i, dev_down = 0;
2800462d115SRahul Lakkireddy 
2810462d115SRahul Lakkireddy 	CXGBE_FUNC_TRACE();
2820462d115SRahul Lakkireddy 
2830462d115SRahul Lakkireddy 	if (!(adapter->flags & FULL_INIT_DONE))
2840462d115SRahul Lakkireddy 		return;
2850462d115SRahul Lakkireddy 
2860462d115SRahul Lakkireddy 	cxgbe_down(pi);
2870462d115SRahul Lakkireddy 
2880462d115SRahul Lakkireddy 	/*
2890462d115SRahul Lakkireddy 	 *  We clear queues only if both tx and rx path of the port
2900462d115SRahul Lakkireddy 	 *  have been disabled
2910462d115SRahul Lakkireddy 	 */
2920462d115SRahul Lakkireddy 	t4_sge_eth_clear_queues(pi);
2930462d115SRahul Lakkireddy 
2940462d115SRahul Lakkireddy 	/*  See if all ports are down */
2950462d115SRahul Lakkireddy 	for_each_port(adapter, i) {
2960462d115SRahul Lakkireddy 		pi = adap2pinfo(adapter, i);
2970462d115SRahul Lakkireddy 		/*
2980462d115SRahul Lakkireddy 		 * Skip first port of the adapter since it will be closed
2990462d115SRahul Lakkireddy 		 * by DPDK
3000462d115SRahul Lakkireddy 		 */
3010462d115SRahul Lakkireddy 		if (i == 0)
3020462d115SRahul Lakkireddy 			continue;
3030462d115SRahul Lakkireddy 		dev_down += (pi->eth_dev->data->dev_started == 0) ? 1 : 0;
3040462d115SRahul Lakkireddy 	}
3050462d115SRahul Lakkireddy 
3060462d115SRahul Lakkireddy 	/* If rest of the ports are stopped, then free up resources */
3070462d115SRahul Lakkireddy 	if (dev_down == (adapter->params.nports - 1))
3080462d115SRahul Lakkireddy 		cxgbe_close(adapter);
3090462d115SRahul Lakkireddy }
3100462d115SRahul Lakkireddy 
3110462d115SRahul Lakkireddy /* Start the device.
3120462d115SRahul Lakkireddy  * It returns 0 on success.
3130462d115SRahul Lakkireddy  */
3140462d115SRahul Lakkireddy static int cxgbe_dev_start(struct rte_eth_dev *eth_dev)
3150462d115SRahul Lakkireddy {
3160462d115SRahul Lakkireddy 	struct port_info *pi = (struct port_info *)(eth_dev->data->dev_private);
3170462d115SRahul Lakkireddy 	struct adapter *adapter = pi->adapter;
3180462d115SRahul Lakkireddy 	int err = 0, i;
3190462d115SRahul Lakkireddy 
3200462d115SRahul Lakkireddy 	CXGBE_FUNC_TRACE();
3210462d115SRahul Lakkireddy 
3220462d115SRahul Lakkireddy 	/*
3230462d115SRahul Lakkireddy 	 * If we don't have a connection to the firmware there's nothing we
3240462d115SRahul Lakkireddy 	 * can do.
3250462d115SRahul Lakkireddy 	 */
3260462d115SRahul Lakkireddy 	if (!(adapter->flags & FW_OK)) {
3270462d115SRahul Lakkireddy 		err = -ENXIO;
3280462d115SRahul Lakkireddy 		goto out;
3290462d115SRahul Lakkireddy 	}
3300462d115SRahul Lakkireddy 
3310462d115SRahul Lakkireddy 	if (!(adapter->flags & FULL_INIT_DONE)) {
3320462d115SRahul Lakkireddy 		err = cxgbe_up(adapter);
3330462d115SRahul Lakkireddy 		if (err < 0)
3340462d115SRahul Lakkireddy 			goto out;
3350462d115SRahul Lakkireddy 	}
3360462d115SRahul Lakkireddy 
3370462d115SRahul Lakkireddy 	err = setup_rss(pi);
3380462d115SRahul Lakkireddy 	if (err)
3390462d115SRahul Lakkireddy 		goto out;
3400462d115SRahul Lakkireddy 
3410462d115SRahul Lakkireddy 	for (i = 0; i < pi->n_tx_qsets; i++) {
3420462d115SRahul Lakkireddy 		err = cxgbe_dev_tx_queue_start(eth_dev, i);
3430462d115SRahul Lakkireddy 		if (err)
3440462d115SRahul Lakkireddy 			goto out;
3450462d115SRahul Lakkireddy 	}
3460462d115SRahul Lakkireddy 
3470462d115SRahul Lakkireddy 	for (i = 0; i < pi->n_rx_qsets; i++) {
3480462d115SRahul Lakkireddy 		err = cxgbe_dev_rx_queue_start(eth_dev, i);
3490462d115SRahul Lakkireddy 		if (err)
3500462d115SRahul Lakkireddy 			goto out;
3510462d115SRahul Lakkireddy 	}
3520462d115SRahul Lakkireddy 
3530462d115SRahul Lakkireddy 	err = link_start(pi);
3540462d115SRahul Lakkireddy 	if (err)
3550462d115SRahul Lakkireddy 		goto out;
3560462d115SRahul Lakkireddy 
3570462d115SRahul Lakkireddy out:
3580462d115SRahul Lakkireddy 	return err;
3590462d115SRahul Lakkireddy }
3600462d115SRahul Lakkireddy 
3610462d115SRahul Lakkireddy /*
3620462d115SRahul Lakkireddy  * Stop device: disable rx and tx functions to allow for reconfiguring.
3630462d115SRahul Lakkireddy  */
3640462d115SRahul Lakkireddy static void cxgbe_dev_stop(struct rte_eth_dev *eth_dev)
3650462d115SRahul Lakkireddy {
3660462d115SRahul Lakkireddy 	struct port_info *pi = (struct port_info *)(eth_dev->data->dev_private);
3670462d115SRahul Lakkireddy 	struct adapter *adapter = pi->adapter;
3680462d115SRahul Lakkireddy 
3690462d115SRahul Lakkireddy 	CXGBE_FUNC_TRACE();
3700462d115SRahul Lakkireddy 
3710462d115SRahul Lakkireddy 	if (!(adapter->flags & FULL_INIT_DONE))
3720462d115SRahul Lakkireddy 		return;
3730462d115SRahul Lakkireddy 
3740462d115SRahul Lakkireddy 	cxgbe_down(pi);
3750462d115SRahul Lakkireddy 
3760462d115SRahul Lakkireddy 	/*
3770462d115SRahul Lakkireddy 	 *  We clear queues only if both tx and rx path of the port
3780462d115SRahul Lakkireddy 	 *  have been disabled
3790462d115SRahul Lakkireddy 	 */
3800462d115SRahul Lakkireddy 	t4_sge_eth_clear_queues(pi);
3810462d115SRahul Lakkireddy }
3820462d115SRahul Lakkireddy 
38392c8a632SRahul Lakkireddy static int cxgbe_dev_configure(struct rte_eth_dev *eth_dev)
38492c8a632SRahul Lakkireddy {
38592c8a632SRahul Lakkireddy 	struct port_info *pi = (struct port_info *)(eth_dev->data->dev_private);
38692c8a632SRahul Lakkireddy 	struct adapter *adapter = pi->adapter;
38792c8a632SRahul Lakkireddy 	int err;
38892c8a632SRahul Lakkireddy 
38992c8a632SRahul Lakkireddy 	CXGBE_FUNC_TRACE();
39092c8a632SRahul Lakkireddy 
39192c8a632SRahul Lakkireddy 	if (!(adapter->flags & FW_QUEUE_BOUND)) {
39292c8a632SRahul Lakkireddy 		err = setup_sge_fwevtq(adapter);
39392c8a632SRahul Lakkireddy 		if (err)
39492c8a632SRahul Lakkireddy 			return err;
39592c8a632SRahul Lakkireddy 		adapter->flags |= FW_QUEUE_BOUND;
39692c8a632SRahul Lakkireddy 	}
39792c8a632SRahul Lakkireddy 
39892c8a632SRahul Lakkireddy 	err = cfg_queue_count(eth_dev);
39992c8a632SRahul Lakkireddy 	if (err)
40092c8a632SRahul Lakkireddy 		return err;
40192c8a632SRahul Lakkireddy 
40292c8a632SRahul Lakkireddy 	return 0;
40392c8a632SRahul Lakkireddy }
40492c8a632SRahul Lakkireddy 
4054a01078bSRahul Lakkireddy static int cxgbe_dev_tx_queue_start(struct rte_eth_dev *eth_dev,
4064a01078bSRahul Lakkireddy 				    uint16_t tx_queue_id)
4074a01078bSRahul Lakkireddy {
4086b6861c1SPablo de Lara 	int ret;
4094a01078bSRahul Lakkireddy 	struct sge_eth_txq *txq = (struct sge_eth_txq *)
4104a01078bSRahul Lakkireddy 				  (eth_dev->data->tx_queues[tx_queue_id]);
4114a01078bSRahul Lakkireddy 
4124a01078bSRahul Lakkireddy 	dev_debug(NULL, "%s: tx_queue_id = %d\n", __func__, tx_queue_id);
4134a01078bSRahul Lakkireddy 
4146b6861c1SPablo de Lara 	ret = t4_sge_eth_txq_start(txq);
4156b6861c1SPablo de Lara 	if (ret == 0)
4166b6861c1SPablo de Lara 		eth_dev->data->tx_queue_state[tx_queue_id] = RTE_ETH_QUEUE_STATE_STARTED;
4176b6861c1SPablo de Lara 
4186b6861c1SPablo de Lara 	return ret;
4194a01078bSRahul Lakkireddy }
4204a01078bSRahul Lakkireddy 
4214a01078bSRahul Lakkireddy static int cxgbe_dev_tx_queue_stop(struct rte_eth_dev *eth_dev,
4224a01078bSRahul Lakkireddy 				   uint16_t tx_queue_id)
4234a01078bSRahul Lakkireddy {
4246b6861c1SPablo de Lara 	int ret;
4254a01078bSRahul Lakkireddy 	struct sge_eth_txq *txq = (struct sge_eth_txq *)
4264a01078bSRahul Lakkireddy 				  (eth_dev->data->tx_queues[tx_queue_id]);
4274a01078bSRahul Lakkireddy 
4284a01078bSRahul Lakkireddy 	dev_debug(NULL, "%s: tx_queue_id = %d\n", __func__, tx_queue_id);
4294a01078bSRahul Lakkireddy 
4306b6861c1SPablo de Lara 	ret = t4_sge_eth_txq_stop(txq);
4316b6861c1SPablo de Lara 	if (ret == 0)
4326b6861c1SPablo de Lara 		eth_dev->data->tx_queue_state[tx_queue_id] = RTE_ETH_QUEUE_STATE_STOPPED;
4336b6861c1SPablo de Lara 
4346b6861c1SPablo de Lara 	return ret;
4354a01078bSRahul Lakkireddy }
4364a01078bSRahul Lakkireddy 
4374a01078bSRahul Lakkireddy static int cxgbe_dev_tx_queue_setup(struct rte_eth_dev *eth_dev,
4384a01078bSRahul Lakkireddy 				    uint16_t queue_idx,	uint16_t nb_desc,
4394a01078bSRahul Lakkireddy 				    unsigned int socket_id,
4404a01078bSRahul Lakkireddy 				    const struct rte_eth_txconf *tx_conf)
4414a01078bSRahul Lakkireddy {
4424a01078bSRahul Lakkireddy 	struct port_info *pi = (struct port_info *)(eth_dev->data->dev_private);
4434a01078bSRahul Lakkireddy 	struct adapter *adapter = pi->adapter;
4444a01078bSRahul Lakkireddy 	struct sge *s = &adapter->sge;
4454a01078bSRahul Lakkireddy 	struct sge_eth_txq *txq = &s->ethtxq[pi->first_qset + queue_idx];
4464a01078bSRahul Lakkireddy 	int err = 0;
4474a01078bSRahul Lakkireddy 	unsigned int temp_nb_desc;
4484a01078bSRahul Lakkireddy 
4494a01078bSRahul Lakkireddy 	RTE_SET_USED(tx_conf);
4504a01078bSRahul Lakkireddy 
4514a01078bSRahul Lakkireddy 	dev_debug(adapter, "%s: eth_dev->data->nb_tx_queues = %d; queue_idx = %d; nb_desc = %d; socket_id = %d; pi->first_qset = %u\n",
4524a01078bSRahul Lakkireddy 		  __func__, eth_dev->data->nb_tx_queues, queue_idx, nb_desc,
4534a01078bSRahul Lakkireddy 		  socket_id, pi->first_qset);
4544a01078bSRahul Lakkireddy 
4554a01078bSRahul Lakkireddy 	/*  Free up the existing queue  */
4564a01078bSRahul Lakkireddy 	if (eth_dev->data->tx_queues[queue_idx]) {
4574a01078bSRahul Lakkireddy 		cxgbe_dev_tx_queue_release(eth_dev->data->tx_queues[queue_idx]);
4584a01078bSRahul Lakkireddy 		eth_dev->data->tx_queues[queue_idx] = NULL;
4594a01078bSRahul Lakkireddy 	}
4604a01078bSRahul Lakkireddy 
4614a01078bSRahul Lakkireddy 	eth_dev->data->tx_queues[queue_idx] = (void *)txq;
4624a01078bSRahul Lakkireddy 
4634a01078bSRahul Lakkireddy 	/* Sanity Checking
4644a01078bSRahul Lakkireddy 	 *
4654a01078bSRahul Lakkireddy 	 * nb_desc should be > 1023 and <= CXGBE_MAX_RING_DESC_SIZE
4664a01078bSRahul Lakkireddy 	 */
4674a01078bSRahul Lakkireddy 	temp_nb_desc = nb_desc;
4684a01078bSRahul Lakkireddy 	if (nb_desc < CXGBE_MIN_RING_DESC_SIZE) {
4694a01078bSRahul Lakkireddy 		dev_warn(adapter, "%s: number of descriptors must be >= %d. Using default [%d]\n",
4704a01078bSRahul Lakkireddy 			 __func__, CXGBE_MIN_RING_DESC_SIZE,
4714a01078bSRahul Lakkireddy 			 CXGBE_DEFAULT_TX_DESC_SIZE);
4724a01078bSRahul Lakkireddy 		temp_nb_desc = CXGBE_DEFAULT_TX_DESC_SIZE;
4734a01078bSRahul Lakkireddy 	} else if (nb_desc > CXGBE_MAX_RING_DESC_SIZE) {
4744a01078bSRahul Lakkireddy 		dev_err(adapter, "%s: number of descriptors must be between %d and %d inclusive. Default [%d]\n",
4754a01078bSRahul Lakkireddy 			__func__, CXGBE_MIN_RING_DESC_SIZE,
4764a01078bSRahul Lakkireddy 			CXGBE_MAX_RING_DESC_SIZE, CXGBE_DEFAULT_TX_DESC_SIZE);
4774a01078bSRahul Lakkireddy 		return -(EINVAL);
4784a01078bSRahul Lakkireddy 	}
4794a01078bSRahul Lakkireddy 
4804a01078bSRahul Lakkireddy 	txq->q.size = temp_nb_desc;
4814a01078bSRahul Lakkireddy 
4824a01078bSRahul Lakkireddy 	err = t4_sge_alloc_eth_txq(adapter, txq, eth_dev, queue_idx,
4834a01078bSRahul Lakkireddy 				   s->fw_evtq.cntxt_id, socket_id);
4844a01078bSRahul Lakkireddy 
4854a01078bSRahul Lakkireddy 	dev_debug(adapter, "%s: txq->q.cntxt_id= %d err = %d\n",
4864a01078bSRahul Lakkireddy 		  __func__, txq->q.cntxt_id, err);
4874a01078bSRahul Lakkireddy 
4884a01078bSRahul Lakkireddy 	return err;
4894a01078bSRahul Lakkireddy }
4904a01078bSRahul Lakkireddy 
4914a01078bSRahul Lakkireddy static void cxgbe_dev_tx_queue_release(void *q)
4924a01078bSRahul Lakkireddy {
4934a01078bSRahul Lakkireddy 	struct sge_eth_txq *txq = (struct sge_eth_txq *)q;
4944a01078bSRahul Lakkireddy 
4954a01078bSRahul Lakkireddy 	if (txq) {
4964a01078bSRahul Lakkireddy 		struct port_info *pi = (struct port_info *)
4974a01078bSRahul Lakkireddy 				       (txq->eth_dev->data->dev_private);
4984a01078bSRahul Lakkireddy 		struct adapter *adap = pi->adapter;
4994a01078bSRahul Lakkireddy 
5004a01078bSRahul Lakkireddy 		dev_debug(adapter, "%s: pi->port_id = %d; tx_queue_id = %d\n",
5014a01078bSRahul Lakkireddy 			  __func__, pi->port_id, txq->q.cntxt_id);
5024a01078bSRahul Lakkireddy 
5034a01078bSRahul Lakkireddy 		t4_sge_eth_txq_release(adap, txq);
5044a01078bSRahul Lakkireddy 	}
5054a01078bSRahul Lakkireddy }
5064a01078bSRahul Lakkireddy 
50792c8a632SRahul Lakkireddy static int cxgbe_dev_rx_queue_start(struct rte_eth_dev *eth_dev,
50892c8a632SRahul Lakkireddy 				    uint16_t rx_queue_id)
50992c8a632SRahul Lakkireddy {
5106b6861c1SPablo de Lara 	int ret;
51192c8a632SRahul Lakkireddy 	struct port_info *pi = (struct port_info *)(eth_dev->data->dev_private);
51292c8a632SRahul Lakkireddy 	struct adapter *adap = pi->adapter;
51392c8a632SRahul Lakkireddy 	struct sge_rspq *q;
51492c8a632SRahul Lakkireddy 
51592c8a632SRahul Lakkireddy 	dev_debug(adapter, "%s: pi->port_id = %d; rx_queue_id = %d\n",
51692c8a632SRahul Lakkireddy 		  __func__, pi->port_id, rx_queue_id);
51792c8a632SRahul Lakkireddy 
51892c8a632SRahul Lakkireddy 	q = eth_dev->data->rx_queues[rx_queue_id];
5196b6861c1SPablo de Lara 
5206b6861c1SPablo de Lara 	ret = t4_sge_eth_rxq_start(adap, q);
5216b6861c1SPablo de Lara 	if (ret == 0)
5226b6861c1SPablo de Lara 		eth_dev->data->rx_queue_state[rx_queue_id] = RTE_ETH_QUEUE_STATE_STARTED;
5236b6861c1SPablo de Lara 
5246b6861c1SPablo de Lara 	return ret;
52592c8a632SRahul Lakkireddy }
52692c8a632SRahul Lakkireddy 
52792c8a632SRahul Lakkireddy static int cxgbe_dev_rx_queue_stop(struct rte_eth_dev *eth_dev,
52892c8a632SRahul Lakkireddy 				   uint16_t rx_queue_id)
52992c8a632SRahul Lakkireddy {
5306b6861c1SPablo de Lara 	int ret;
53192c8a632SRahul Lakkireddy 	struct port_info *pi = (struct port_info *)(eth_dev->data->dev_private);
53292c8a632SRahul Lakkireddy 	struct adapter *adap = pi->adapter;
53392c8a632SRahul Lakkireddy 	struct sge_rspq *q;
53492c8a632SRahul Lakkireddy 
53592c8a632SRahul Lakkireddy 	dev_debug(adapter, "%s: pi->port_id = %d; rx_queue_id = %d\n",
53692c8a632SRahul Lakkireddy 		  __func__, pi->port_id, rx_queue_id);
53792c8a632SRahul Lakkireddy 
53892c8a632SRahul Lakkireddy 	q = eth_dev->data->rx_queues[rx_queue_id];
5396b6861c1SPablo de Lara 	ret = t4_sge_eth_rxq_stop(adap, q);
5406b6861c1SPablo de Lara 	if (ret == 0)
5416b6861c1SPablo de Lara 		eth_dev->data->rx_queue_state[rx_queue_id] = RTE_ETH_QUEUE_STATE_STOPPED;
5426b6861c1SPablo de Lara 
5436b6861c1SPablo de Lara 	return ret;
54492c8a632SRahul Lakkireddy }
54592c8a632SRahul Lakkireddy 
54692c8a632SRahul Lakkireddy static int cxgbe_dev_rx_queue_setup(struct rte_eth_dev *eth_dev,
54792c8a632SRahul Lakkireddy 				    uint16_t queue_idx,	uint16_t nb_desc,
54892c8a632SRahul Lakkireddy 				    unsigned int socket_id,
54992c8a632SRahul Lakkireddy 				    const struct rte_eth_rxconf *rx_conf,
55092c8a632SRahul Lakkireddy 				    struct rte_mempool *mp)
55192c8a632SRahul Lakkireddy {
55292c8a632SRahul Lakkireddy 	struct port_info *pi = (struct port_info *)(eth_dev->data->dev_private);
55392c8a632SRahul Lakkireddy 	struct adapter *adapter = pi->adapter;
55492c8a632SRahul Lakkireddy 	struct sge *s = &adapter->sge;
55592c8a632SRahul Lakkireddy 	struct sge_eth_rxq *rxq = &s->ethrxq[pi->first_qset + queue_idx];
55692c8a632SRahul Lakkireddy 	int err = 0;
55792c8a632SRahul Lakkireddy 	int msi_idx = 0;
55892c8a632SRahul Lakkireddy 	unsigned int temp_nb_desc;
5594b2eff45SRahul Lakkireddy 	struct rte_eth_dev_info dev_info;
5604b2eff45SRahul Lakkireddy 	unsigned int pkt_len = eth_dev->data->dev_conf.rxmode.max_rx_pkt_len;
56192c8a632SRahul Lakkireddy 
56292c8a632SRahul Lakkireddy 	RTE_SET_USED(rx_conf);
56392c8a632SRahul Lakkireddy 
56492c8a632SRahul Lakkireddy 	dev_debug(adapter, "%s: eth_dev->data->nb_rx_queues = %d; queue_idx = %d; nb_desc = %d; socket_id = %d; mp = %p\n",
56592c8a632SRahul Lakkireddy 		  __func__, eth_dev->data->nb_rx_queues, queue_idx, nb_desc,
56692c8a632SRahul Lakkireddy 		  socket_id, mp);
56792c8a632SRahul Lakkireddy 
5684b2eff45SRahul Lakkireddy 	cxgbe_dev_info_get(eth_dev, &dev_info);
5694b2eff45SRahul Lakkireddy 
5704b2eff45SRahul Lakkireddy 	/* Must accommodate at least ETHER_MIN_MTU */
5714b2eff45SRahul Lakkireddy 	if ((pkt_len < dev_info.min_rx_bufsize) ||
5724b2eff45SRahul Lakkireddy 	    (pkt_len > dev_info.max_rx_pktlen)) {
5734b2eff45SRahul Lakkireddy 		dev_err(adap, "%s: max pkt len must be > %d and <= %d\n",
5744b2eff45SRahul Lakkireddy 			__func__, dev_info.min_rx_bufsize,
5754b2eff45SRahul Lakkireddy 			dev_info.max_rx_pktlen);
5764b2eff45SRahul Lakkireddy 		return -EINVAL;
5774b2eff45SRahul Lakkireddy 	}
5784b2eff45SRahul Lakkireddy 
57992c8a632SRahul Lakkireddy 	/*  Free up the existing queue  */
58092c8a632SRahul Lakkireddy 	if (eth_dev->data->rx_queues[queue_idx]) {
58192c8a632SRahul Lakkireddy 		cxgbe_dev_rx_queue_release(eth_dev->data->rx_queues[queue_idx]);
58292c8a632SRahul Lakkireddy 		eth_dev->data->rx_queues[queue_idx] = NULL;
58392c8a632SRahul Lakkireddy 	}
58492c8a632SRahul Lakkireddy 
58592c8a632SRahul Lakkireddy 	eth_dev->data->rx_queues[queue_idx] = (void *)rxq;
58692c8a632SRahul Lakkireddy 
58792c8a632SRahul Lakkireddy 	/* Sanity Checking
58892c8a632SRahul Lakkireddy 	 *
58992c8a632SRahul Lakkireddy 	 * nb_desc should be > 0 and <= CXGBE_MAX_RING_DESC_SIZE
59092c8a632SRahul Lakkireddy 	 */
59192c8a632SRahul Lakkireddy 	temp_nb_desc = nb_desc;
59292c8a632SRahul Lakkireddy 	if (nb_desc < CXGBE_MIN_RING_DESC_SIZE) {
59392c8a632SRahul Lakkireddy 		dev_warn(adapter, "%s: number of descriptors must be >= %d. Using default [%d]\n",
59492c8a632SRahul Lakkireddy 			 __func__, CXGBE_MIN_RING_DESC_SIZE,
59592c8a632SRahul Lakkireddy 			 CXGBE_DEFAULT_RX_DESC_SIZE);
59692c8a632SRahul Lakkireddy 		temp_nb_desc = CXGBE_DEFAULT_RX_DESC_SIZE;
59792c8a632SRahul Lakkireddy 	} else if (nb_desc > CXGBE_MAX_RING_DESC_SIZE) {
59892c8a632SRahul Lakkireddy 		dev_err(adapter, "%s: number of descriptors must be between %d and %d inclusive. Default [%d]\n",
59992c8a632SRahul Lakkireddy 			__func__, CXGBE_MIN_RING_DESC_SIZE,
60092c8a632SRahul Lakkireddy 			CXGBE_MAX_RING_DESC_SIZE, CXGBE_DEFAULT_RX_DESC_SIZE);
60192c8a632SRahul Lakkireddy 		return -(EINVAL);
60292c8a632SRahul Lakkireddy 	}
60392c8a632SRahul Lakkireddy 
60492c8a632SRahul Lakkireddy 	rxq->rspq.size = temp_nb_desc;
60592c8a632SRahul Lakkireddy 	if ((&rxq->fl) != NULL)
60692c8a632SRahul Lakkireddy 		rxq->fl.size = temp_nb_desc;
60792c8a632SRahul Lakkireddy 
6084b2eff45SRahul Lakkireddy 	/* Set to jumbo mode if necessary */
6094b2eff45SRahul Lakkireddy 	if (pkt_len > ETHER_MAX_LEN)
6104b2eff45SRahul Lakkireddy 		eth_dev->data->dev_conf.rxmode.jumbo_frame = 1;
6114b2eff45SRahul Lakkireddy 	else
6124b2eff45SRahul Lakkireddy 		eth_dev->data->dev_conf.rxmode.jumbo_frame = 0;
6134b2eff45SRahul Lakkireddy 
61492c8a632SRahul Lakkireddy 	err = t4_sge_alloc_rxq(adapter, &rxq->rspq, false, eth_dev, msi_idx,
61592c8a632SRahul Lakkireddy 			       &rxq->fl, t4_ethrx_handler,
61692c8a632SRahul Lakkireddy 			       t4_get_mps_bg_map(adapter, pi->tx_chan), mp,
61792c8a632SRahul Lakkireddy 			       queue_idx, socket_id);
61892c8a632SRahul Lakkireddy 
61992c8a632SRahul Lakkireddy 	dev_debug(adapter, "%s: err = %d; port_id = %d; cntxt_id = %u\n",
62092c8a632SRahul Lakkireddy 		  __func__, err, pi->port_id, rxq->rspq.cntxt_id);
62192c8a632SRahul Lakkireddy 	return err;
62292c8a632SRahul Lakkireddy }
62392c8a632SRahul Lakkireddy 
62492c8a632SRahul Lakkireddy static void cxgbe_dev_rx_queue_release(void *q)
62592c8a632SRahul Lakkireddy {
62692c8a632SRahul Lakkireddy 	struct sge_eth_rxq *rxq = (struct sge_eth_rxq *)q;
62792c8a632SRahul Lakkireddy 	struct sge_rspq *rq = &rxq->rspq;
62892c8a632SRahul Lakkireddy 
62992c8a632SRahul Lakkireddy 	if (rq) {
63092c8a632SRahul Lakkireddy 		struct port_info *pi = (struct port_info *)
63192c8a632SRahul Lakkireddy 				       (rq->eth_dev->data->dev_private);
63292c8a632SRahul Lakkireddy 		struct adapter *adap = pi->adapter;
63392c8a632SRahul Lakkireddy 
63492c8a632SRahul Lakkireddy 		dev_debug(adapter, "%s: pi->port_id = %d; rx_queue_id = %d\n",
63592c8a632SRahul Lakkireddy 			  __func__, pi->port_id, rxq->rspq.cntxt_id);
63692c8a632SRahul Lakkireddy 
63792c8a632SRahul Lakkireddy 		t4_sge_eth_rxq_release(adap, rxq);
63892c8a632SRahul Lakkireddy 	}
63992c8a632SRahul Lakkireddy }
64092c8a632SRahul Lakkireddy 
641856505d3SRahul Lakkireddy /*
642856505d3SRahul Lakkireddy  * Get port statistics.
643856505d3SRahul Lakkireddy  */
644856505d3SRahul Lakkireddy static void cxgbe_dev_stats_get(struct rte_eth_dev *eth_dev,
645856505d3SRahul Lakkireddy 				struct rte_eth_stats *eth_stats)
646856505d3SRahul Lakkireddy {
647856505d3SRahul Lakkireddy 	struct port_info *pi = (struct port_info *)(eth_dev->data->dev_private);
648856505d3SRahul Lakkireddy 	struct adapter *adapter = pi->adapter;
649856505d3SRahul Lakkireddy 	struct sge *s = &adapter->sge;
650856505d3SRahul Lakkireddy 	struct port_stats ps;
651856505d3SRahul Lakkireddy 	unsigned int i;
652856505d3SRahul Lakkireddy 
653856505d3SRahul Lakkireddy 	cxgbe_stats_get(pi, &ps);
654856505d3SRahul Lakkireddy 
655856505d3SRahul Lakkireddy 	/* RX Stats */
656856505d3SRahul Lakkireddy 	eth_stats->ipackets = ps.rx_frames;
657856505d3SRahul Lakkireddy 	eth_stats->ibytes   = ps.rx_octets;
658856505d3SRahul Lakkireddy 	eth_stats->imcasts  = ps.rx_mcast_frames;
659856505d3SRahul Lakkireddy 	eth_stats->imissed  = ps.rx_ovflow0 + ps.rx_ovflow1 +
660856505d3SRahul Lakkireddy 			      ps.rx_ovflow2 + ps.rx_ovflow3 +
661856505d3SRahul Lakkireddy 			      ps.rx_trunc0 + ps.rx_trunc1 +
662856505d3SRahul Lakkireddy 			      ps.rx_trunc2 + ps.rx_trunc3;
663b5d5b4a8SStephen Hemminger 	eth_stats->ierrors  = ps.rx_symbol_err + ps.rx_fcs_err +
664b5d5b4a8SStephen Hemminger 			      ps.rx_jabber + ps.rx_too_long + ps.rx_runt +
66586057c99SIgor Ryzhov 			      ps.rx_len_err;
666856505d3SRahul Lakkireddy 
667856505d3SRahul Lakkireddy 	/* TX Stats */
668856505d3SRahul Lakkireddy 	eth_stats->opackets = ps.tx_frames;
669856505d3SRahul Lakkireddy 	eth_stats->obytes   = ps.tx_octets;
670856505d3SRahul Lakkireddy 	eth_stats->oerrors  = ps.tx_error_frames;
671856505d3SRahul Lakkireddy 
672856505d3SRahul Lakkireddy 	for (i = 0; i < pi->n_rx_qsets; i++) {
673856505d3SRahul Lakkireddy 		struct sge_eth_rxq *rxq =
674856505d3SRahul Lakkireddy 			&s->ethrxq[pi->first_qset + i];
675856505d3SRahul Lakkireddy 
676856505d3SRahul Lakkireddy 		eth_stats->q_ipackets[i] = rxq->stats.pkts;
677856505d3SRahul Lakkireddy 		eth_stats->q_ibytes[i] = rxq->stats.rx_bytes;
678856505d3SRahul Lakkireddy 	}
679856505d3SRahul Lakkireddy 
680856505d3SRahul Lakkireddy 	for (i = 0; i < pi->n_tx_qsets; i++) {
681856505d3SRahul Lakkireddy 		struct sge_eth_txq *txq =
682856505d3SRahul Lakkireddy 			&s->ethtxq[pi->first_qset + i];
683856505d3SRahul Lakkireddy 
684856505d3SRahul Lakkireddy 		eth_stats->q_opackets[i] = txq->stats.pkts;
685856505d3SRahul Lakkireddy 		eth_stats->q_obytes[i] = txq->stats.tx_bytes;
686856505d3SRahul Lakkireddy 		eth_stats->q_errors[i] = txq->stats.mapping_err;
687856505d3SRahul Lakkireddy 	}
688856505d3SRahul Lakkireddy }
689856505d3SRahul Lakkireddy 
690856505d3SRahul Lakkireddy /*
691856505d3SRahul Lakkireddy  * Reset port statistics.
692856505d3SRahul Lakkireddy  */
693856505d3SRahul Lakkireddy static void cxgbe_dev_stats_reset(struct rte_eth_dev *eth_dev)
694856505d3SRahul Lakkireddy {
695856505d3SRahul Lakkireddy 	struct port_info *pi = (struct port_info *)(eth_dev->data->dev_private);
696856505d3SRahul Lakkireddy 	struct adapter *adapter = pi->adapter;
697856505d3SRahul Lakkireddy 	struct sge *s = &adapter->sge;
698856505d3SRahul Lakkireddy 	unsigned int i;
699856505d3SRahul Lakkireddy 
700856505d3SRahul Lakkireddy 	cxgbe_stats_reset(pi);
701856505d3SRahul Lakkireddy 	for (i = 0; i < pi->n_rx_qsets; i++) {
702856505d3SRahul Lakkireddy 		struct sge_eth_rxq *rxq =
703856505d3SRahul Lakkireddy 			&s->ethrxq[pi->first_qset + i];
704856505d3SRahul Lakkireddy 
705856505d3SRahul Lakkireddy 		rxq->stats.pkts = 0;
706856505d3SRahul Lakkireddy 		rxq->stats.rx_bytes = 0;
707856505d3SRahul Lakkireddy 	}
708856505d3SRahul Lakkireddy 	for (i = 0; i < pi->n_tx_qsets; i++) {
709856505d3SRahul Lakkireddy 		struct sge_eth_txq *txq =
710856505d3SRahul Lakkireddy 			&s->ethtxq[pi->first_qset + i];
711856505d3SRahul Lakkireddy 
712856505d3SRahul Lakkireddy 		txq->stats.pkts = 0;
713856505d3SRahul Lakkireddy 		txq->stats.tx_bytes = 0;
714856505d3SRahul Lakkireddy 		txq->stats.mapping_err = 0;
715856505d3SRahul Lakkireddy 	}
716856505d3SRahul Lakkireddy }
717856505d3SRahul Lakkireddy 
718631dfc71SRahul Lakkireddy static int cxgbe_flow_ctrl_get(struct rte_eth_dev *eth_dev,
719631dfc71SRahul Lakkireddy 			       struct rte_eth_fc_conf *fc_conf)
720631dfc71SRahul Lakkireddy {
721631dfc71SRahul Lakkireddy 	struct port_info *pi = (struct port_info *)(eth_dev->data->dev_private);
722631dfc71SRahul Lakkireddy 	struct link_config *lc = &pi->link_cfg;
723631dfc71SRahul Lakkireddy 	int rx_pause, tx_pause;
724631dfc71SRahul Lakkireddy 
725631dfc71SRahul Lakkireddy 	fc_conf->autoneg = lc->fc & PAUSE_AUTONEG;
726631dfc71SRahul Lakkireddy 	rx_pause = lc->fc & PAUSE_RX;
727631dfc71SRahul Lakkireddy 	tx_pause = lc->fc & PAUSE_TX;
728631dfc71SRahul Lakkireddy 
729631dfc71SRahul Lakkireddy 	if (rx_pause && tx_pause)
730631dfc71SRahul Lakkireddy 		fc_conf->mode = RTE_FC_FULL;
731631dfc71SRahul Lakkireddy 	else if (rx_pause)
732631dfc71SRahul Lakkireddy 		fc_conf->mode = RTE_FC_RX_PAUSE;
733631dfc71SRahul Lakkireddy 	else if (tx_pause)
734631dfc71SRahul Lakkireddy 		fc_conf->mode = RTE_FC_TX_PAUSE;
735631dfc71SRahul Lakkireddy 	else
736631dfc71SRahul Lakkireddy 		fc_conf->mode = RTE_FC_NONE;
737631dfc71SRahul Lakkireddy 	return 0;
738631dfc71SRahul Lakkireddy }
739631dfc71SRahul Lakkireddy 
740631dfc71SRahul Lakkireddy static int cxgbe_flow_ctrl_set(struct rte_eth_dev *eth_dev,
741631dfc71SRahul Lakkireddy 			       struct rte_eth_fc_conf *fc_conf)
742631dfc71SRahul Lakkireddy {
743631dfc71SRahul Lakkireddy 	struct port_info *pi = (struct port_info *)(eth_dev->data->dev_private);
744631dfc71SRahul Lakkireddy 	struct adapter *adapter = pi->adapter;
745631dfc71SRahul Lakkireddy 	struct link_config *lc = &pi->link_cfg;
746631dfc71SRahul Lakkireddy 
747631dfc71SRahul Lakkireddy 	if (lc->supported & FW_PORT_CAP_ANEG) {
748631dfc71SRahul Lakkireddy 		if (fc_conf->autoneg)
749631dfc71SRahul Lakkireddy 			lc->requested_fc |= PAUSE_AUTONEG;
750631dfc71SRahul Lakkireddy 		else
751631dfc71SRahul Lakkireddy 			lc->requested_fc &= ~PAUSE_AUTONEG;
752631dfc71SRahul Lakkireddy 	}
753631dfc71SRahul Lakkireddy 
754631dfc71SRahul Lakkireddy 	if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
755631dfc71SRahul Lakkireddy 	    (fc_conf->mode & RTE_FC_RX_PAUSE))
756631dfc71SRahul Lakkireddy 		lc->requested_fc |= PAUSE_RX;
757631dfc71SRahul Lakkireddy 	else
758631dfc71SRahul Lakkireddy 		lc->requested_fc &= ~PAUSE_RX;
759631dfc71SRahul Lakkireddy 
760631dfc71SRahul Lakkireddy 	if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
761631dfc71SRahul Lakkireddy 	    (fc_conf->mode & RTE_FC_TX_PAUSE))
762631dfc71SRahul Lakkireddy 		lc->requested_fc |= PAUSE_TX;
763631dfc71SRahul Lakkireddy 	else
764631dfc71SRahul Lakkireddy 		lc->requested_fc &= ~PAUSE_TX;
765631dfc71SRahul Lakkireddy 
766631dfc71SRahul Lakkireddy 	return t4_link_l1cfg(adapter, adapter->mbox, pi->tx_chan,
767631dfc71SRahul Lakkireddy 			     &pi->link_cfg);
768631dfc71SRahul Lakkireddy }
769631dfc71SRahul Lakkireddy 
77078a38edfSJianfeng Tan static const uint32_t *
77178a38edfSJianfeng Tan cxgbe_dev_supported_ptypes_get(struct rte_eth_dev *eth_dev)
77278a38edfSJianfeng Tan {
77378a38edfSJianfeng Tan 	static const uint32_t ptypes[] = {
77478a38edfSJianfeng Tan 		RTE_PTYPE_L3_IPV4,
77578a38edfSJianfeng Tan 		RTE_PTYPE_L3_IPV6,
77678a38edfSJianfeng Tan 		RTE_PTYPE_UNKNOWN
77778a38edfSJianfeng Tan 	};
77878a38edfSJianfeng Tan 
77978a38edfSJianfeng Tan 	if (eth_dev->rx_pkt_burst == cxgbe_recv_pkts)
78078a38edfSJianfeng Tan 		return ptypes;
78178a38edfSJianfeng Tan 	return NULL;
78278a38edfSJianfeng Tan }
78378a38edfSJianfeng Tan 
784*89b890dfSStephen Hemminger static const struct eth_dev_ops cxgbe_eth_dev_ops = {
7850462d115SRahul Lakkireddy 	.dev_start		= cxgbe_dev_start,
7860462d115SRahul Lakkireddy 	.dev_stop		= cxgbe_dev_stop,
7870462d115SRahul Lakkireddy 	.dev_close		= cxgbe_dev_close,
788cdac6e2eSRahul Lakkireddy 	.promiscuous_enable	= cxgbe_dev_promiscuous_enable,
789cdac6e2eSRahul Lakkireddy 	.promiscuous_disable	= cxgbe_dev_promiscuous_disable,
790cdac6e2eSRahul Lakkireddy 	.allmulticast_enable	= cxgbe_dev_allmulticast_enable,
791cdac6e2eSRahul Lakkireddy 	.allmulticast_disable	= cxgbe_dev_allmulticast_disable,
79292c8a632SRahul Lakkireddy 	.dev_configure		= cxgbe_dev_configure,
79392c8a632SRahul Lakkireddy 	.dev_infos_get		= cxgbe_dev_info_get,
79478a38edfSJianfeng Tan 	.dev_supported_ptypes_get = cxgbe_dev_supported_ptypes_get,
795cdac6e2eSRahul Lakkireddy 	.link_update		= cxgbe_dev_link_update,
7960ec33be4SRahul Lakkireddy 	.mtu_set		= cxgbe_dev_mtu_set,
7974a01078bSRahul Lakkireddy 	.tx_queue_setup         = cxgbe_dev_tx_queue_setup,
7984a01078bSRahul Lakkireddy 	.tx_queue_start		= cxgbe_dev_tx_queue_start,
7994a01078bSRahul Lakkireddy 	.tx_queue_stop		= cxgbe_dev_tx_queue_stop,
8004a01078bSRahul Lakkireddy 	.tx_queue_release	= cxgbe_dev_tx_queue_release,
80192c8a632SRahul Lakkireddy 	.rx_queue_setup         = cxgbe_dev_rx_queue_setup,
80292c8a632SRahul Lakkireddy 	.rx_queue_start		= cxgbe_dev_rx_queue_start,
80392c8a632SRahul Lakkireddy 	.rx_queue_stop		= cxgbe_dev_rx_queue_stop,
80492c8a632SRahul Lakkireddy 	.rx_queue_release	= cxgbe_dev_rx_queue_release,
805856505d3SRahul Lakkireddy 	.stats_get		= cxgbe_dev_stats_get,
806856505d3SRahul Lakkireddy 	.stats_reset		= cxgbe_dev_stats_reset,
807631dfc71SRahul Lakkireddy 	.flow_ctrl_get		= cxgbe_flow_ctrl_get,
808631dfc71SRahul Lakkireddy 	.flow_ctrl_set		= cxgbe_flow_ctrl_set,
80983189849SRahul Lakkireddy };
81083189849SRahul Lakkireddy 
81183189849SRahul Lakkireddy /*
81283189849SRahul Lakkireddy  * Initialize driver
81383189849SRahul Lakkireddy  * It returns 0 on success.
81483189849SRahul Lakkireddy  */
81583189849SRahul Lakkireddy static int eth_cxgbe_dev_init(struct rte_eth_dev *eth_dev)
81683189849SRahul Lakkireddy {
81783189849SRahul Lakkireddy 	struct rte_pci_device *pci_dev;
81883189849SRahul Lakkireddy 	struct port_info *pi = (struct port_info *)(eth_dev->data->dev_private);
81983189849SRahul Lakkireddy 	struct adapter *adapter = NULL;
82083189849SRahul Lakkireddy 	char name[RTE_ETH_NAME_MAX_LEN];
82183189849SRahul Lakkireddy 	int err = 0;
82283189849SRahul Lakkireddy 
82383189849SRahul Lakkireddy 	CXGBE_FUNC_TRACE();
82483189849SRahul Lakkireddy 
82583189849SRahul Lakkireddy 	eth_dev->dev_ops = &cxgbe_eth_dev_ops;
82692c8a632SRahul Lakkireddy 	eth_dev->rx_pkt_burst = &cxgbe_recv_pkts;
8274a01078bSRahul Lakkireddy 	eth_dev->tx_pkt_burst = &cxgbe_xmit_pkts;
82883189849SRahul Lakkireddy 
82983189849SRahul Lakkireddy 	/* for secondary processes, we don't initialise any further as primary
83083189849SRahul Lakkireddy 	 * has already done this work.
83183189849SRahul Lakkireddy 	 */
83283189849SRahul Lakkireddy 	if (rte_eal_process_type() != RTE_PROC_PRIMARY)
83383189849SRahul Lakkireddy 		return 0;
83483189849SRahul Lakkireddy 
83583189849SRahul Lakkireddy 	pci_dev = eth_dev->pci_dev;
836eeefe73fSBernard Iremonger 
83783189849SRahul Lakkireddy 	snprintf(name, sizeof(name), "cxgbeadapter%d", eth_dev->data->port_id);
83883189849SRahul Lakkireddy 	adapter = rte_zmalloc(name, sizeof(*adapter), 0);
83983189849SRahul Lakkireddy 	if (!adapter)
84083189849SRahul Lakkireddy 		return -1;
84183189849SRahul Lakkireddy 
84283189849SRahul Lakkireddy 	adapter->use_unpacked_mode = 1;
84383189849SRahul Lakkireddy 	adapter->regs = (void *)pci_dev->mem_resource[0].addr;
84483189849SRahul Lakkireddy 	if (!adapter->regs) {
84583189849SRahul Lakkireddy 		dev_err(adapter, "%s: cannot map device registers\n", __func__);
84683189849SRahul Lakkireddy 		err = -ENOMEM;
84783189849SRahul Lakkireddy 		goto out_free_adapter;
84883189849SRahul Lakkireddy 	}
84983189849SRahul Lakkireddy 	adapter->pdev = pci_dev;
85083189849SRahul Lakkireddy 	adapter->eth_dev = eth_dev;
85183189849SRahul Lakkireddy 	pi->adapter = adapter;
85283189849SRahul Lakkireddy 
85383189849SRahul Lakkireddy 	err = cxgbe_probe(adapter);
8541c1789ccSRahul Lakkireddy 	if (err) {
85583189849SRahul Lakkireddy 		dev_err(adapter, "%s: cxgbe probe failed with err %d\n",
85683189849SRahul Lakkireddy 			__func__, err);
8571c1789ccSRahul Lakkireddy 		goto out_free_adapter;
8581c1789ccSRahul Lakkireddy 	}
8591c1789ccSRahul Lakkireddy 
8601c1789ccSRahul Lakkireddy 	return 0;
86183189849SRahul Lakkireddy 
86283189849SRahul Lakkireddy out_free_adapter:
8631c1789ccSRahul Lakkireddy 	rte_free(adapter);
86483189849SRahul Lakkireddy 	return err;
86583189849SRahul Lakkireddy }
86683189849SRahul Lakkireddy 
86783189849SRahul Lakkireddy static struct eth_driver rte_cxgbe_pmd = {
868ca26d3b4SStephen Hemminger 	.pci_drv = {
86983189849SRahul Lakkireddy 		.name = "rte_cxgbe_pmd",
87083189849SRahul Lakkireddy 		.id_table = cxgb4_pci_tbl,
87183189849SRahul Lakkireddy 		.drv_flags = RTE_PCI_DRV_NEED_MAPPING | RTE_PCI_DRV_INTR_LSC,
87283189849SRahul Lakkireddy 	},
87383189849SRahul Lakkireddy 	.eth_dev_init = eth_cxgbe_dev_init,
87483189849SRahul Lakkireddy 	.dev_private_size = sizeof(struct port_info),
87583189849SRahul Lakkireddy };
87683189849SRahul Lakkireddy 
87783189849SRahul Lakkireddy /*
87883189849SRahul Lakkireddy  * Driver initialization routine.
87983189849SRahul Lakkireddy  * Invoked once at EAL init time.
88083189849SRahul Lakkireddy  * Register itself as the [Poll Mode] Driver of PCI CXGBE devices.
88183189849SRahul Lakkireddy  */
88283189849SRahul Lakkireddy static int rte_cxgbe_pmd_init(const char *name __rte_unused,
88383189849SRahul Lakkireddy 			      const char *params __rte_unused)
88483189849SRahul Lakkireddy {
88583189849SRahul Lakkireddy 	CXGBE_FUNC_TRACE();
88683189849SRahul Lakkireddy 
88783189849SRahul Lakkireddy 	rte_eth_driver_register(&rte_cxgbe_pmd);
88883189849SRahul Lakkireddy 	return 0;
88983189849SRahul Lakkireddy }
89083189849SRahul Lakkireddy 
89183189849SRahul Lakkireddy static struct rte_driver rte_cxgbe_driver = {
89283189849SRahul Lakkireddy 	.name = "cxgbe_driver",
89383189849SRahul Lakkireddy 	.type = PMD_PDEV,
89483189849SRahul Lakkireddy 	.init = rte_cxgbe_pmd_init,
89583189849SRahul Lakkireddy };
89683189849SRahul Lakkireddy 
89783189849SRahul Lakkireddy PMD_REGISTER_DRIVER(rte_cxgbe_driver);
898