xref: /dpdk/drivers/net/cxgbe/cxgbe_ethdev.c (revision eeefe73f0af19a287b5dec1d0b63bb83821d8dc9)
183189849SRahul Lakkireddy /*-
283189849SRahul Lakkireddy  *   BSD LICENSE
383189849SRahul Lakkireddy  *
483189849SRahul Lakkireddy  *   Copyright(c) 2014-2015 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 {
4084a01078bSRahul Lakkireddy 	struct sge_eth_txq *txq = (struct sge_eth_txq *)
4094a01078bSRahul Lakkireddy 				  (eth_dev->data->tx_queues[tx_queue_id]);
4104a01078bSRahul Lakkireddy 
4114a01078bSRahul Lakkireddy 	dev_debug(NULL, "%s: tx_queue_id = %d\n", __func__, tx_queue_id);
4124a01078bSRahul Lakkireddy 
4134a01078bSRahul Lakkireddy 	return t4_sge_eth_txq_start(txq);
4144a01078bSRahul Lakkireddy }
4154a01078bSRahul Lakkireddy 
4164a01078bSRahul Lakkireddy static int cxgbe_dev_tx_queue_stop(struct rte_eth_dev *eth_dev,
4174a01078bSRahul Lakkireddy 				   uint16_t tx_queue_id)
4184a01078bSRahul Lakkireddy {
4194a01078bSRahul Lakkireddy 	struct sge_eth_txq *txq = (struct sge_eth_txq *)
4204a01078bSRahul Lakkireddy 				  (eth_dev->data->tx_queues[tx_queue_id]);
4214a01078bSRahul Lakkireddy 
4224a01078bSRahul Lakkireddy 	dev_debug(NULL, "%s: tx_queue_id = %d\n", __func__, tx_queue_id);
4234a01078bSRahul Lakkireddy 
4244a01078bSRahul Lakkireddy 	return t4_sge_eth_txq_stop(txq);
4254a01078bSRahul Lakkireddy }
4264a01078bSRahul Lakkireddy 
4274a01078bSRahul Lakkireddy static int cxgbe_dev_tx_queue_setup(struct rte_eth_dev *eth_dev,
4284a01078bSRahul Lakkireddy 				    uint16_t queue_idx,	uint16_t nb_desc,
4294a01078bSRahul Lakkireddy 				    unsigned int socket_id,
4304a01078bSRahul Lakkireddy 				    const struct rte_eth_txconf *tx_conf)
4314a01078bSRahul Lakkireddy {
4324a01078bSRahul Lakkireddy 	struct port_info *pi = (struct port_info *)(eth_dev->data->dev_private);
4334a01078bSRahul Lakkireddy 	struct adapter *adapter = pi->adapter;
4344a01078bSRahul Lakkireddy 	struct sge *s = &adapter->sge;
4354a01078bSRahul Lakkireddy 	struct sge_eth_txq *txq = &s->ethtxq[pi->first_qset + queue_idx];
4364a01078bSRahul Lakkireddy 	int err = 0;
4374a01078bSRahul Lakkireddy 	unsigned int temp_nb_desc;
4384a01078bSRahul Lakkireddy 
4394a01078bSRahul Lakkireddy 	RTE_SET_USED(tx_conf);
4404a01078bSRahul Lakkireddy 
4414a01078bSRahul 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",
4424a01078bSRahul Lakkireddy 		  __func__, eth_dev->data->nb_tx_queues, queue_idx, nb_desc,
4434a01078bSRahul Lakkireddy 		  socket_id, pi->first_qset);
4444a01078bSRahul Lakkireddy 
4454a01078bSRahul Lakkireddy 	/*  Free up the existing queue  */
4464a01078bSRahul Lakkireddy 	if (eth_dev->data->tx_queues[queue_idx]) {
4474a01078bSRahul Lakkireddy 		cxgbe_dev_tx_queue_release(eth_dev->data->tx_queues[queue_idx]);
4484a01078bSRahul Lakkireddy 		eth_dev->data->tx_queues[queue_idx] = NULL;
4494a01078bSRahul Lakkireddy 	}
4504a01078bSRahul Lakkireddy 
4514a01078bSRahul Lakkireddy 	eth_dev->data->tx_queues[queue_idx] = (void *)txq;
4524a01078bSRahul Lakkireddy 
4534a01078bSRahul Lakkireddy 	/* Sanity Checking
4544a01078bSRahul Lakkireddy 	 *
4554a01078bSRahul Lakkireddy 	 * nb_desc should be > 1023 and <= CXGBE_MAX_RING_DESC_SIZE
4564a01078bSRahul Lakkireddy 	 */
4574a01078bSRahul Lakkireddy 	temp_nb_desc = nb_desc;
4584a01078bSRahul Lakkireddy 	if (nb_desc < CXGBE_MIN_RING_DESC_SIZE) {
4594a01078bSRahul Lakkireddy 		dev_warn(adapter, "%s: number of descriptors must be >= %d. Using default [%d]\n",
4604a01078bSRahul Lakkireddy 			 __func__, CXGBE_MIN_RING_DESC_SIZE,
4614a01078bSRahul Lakkireddy 			 CXGBE_DEFAULT_TX_DESC_SIZE);
4624a01078bSRahul Lakkireddy 		temp_nb_desc = CXGBE_DEFAULT_TX_DESC_SIZE;
4634a01078bSRahul Lakkireddy 	} else if (nb_desc > CXGBE_MAX_RING_DESC_SIZE) {
4644a01078bSRahul Lakkireddy 		dev_err(adapter, "%s: number of descriptors must be between %d and %d inclusive. Default [%d]\n",
4654a01078bSRahul Lakkireddy 			__func__, CXGBE_MIN_RING_DESC_SIZE,
4664a01078bSRahul Lakkireddy 			CXGBE_MAX_RING_DESC_SIZE, CXGBE_DEFAULT_TX_DESC_SIZE);
4674a01078bSRahul Lakkireddy 		return -(EINVAL);
4684a01078bSRahul Lakkireddy 	}
4694a01078bSRahul Lakkireddy 
4704a01078bSRahul Lakkireddy 	txq->q.size = temp_nb_desc;
4714a01078bSRahul Lakkireddy 
4724a01078bSRahul Lakkireddy 	err = t4_sge_alloc_eth_txq(adapter, txq, eth_dev, queue_idx,
4734a01078bSRahul Lakkireddy 				   s->fw_evtq.cntxt_id, socket_id);
4744a01078bSRahul Lakkireddy 
4754a01078bSRahul Lakkireddy 	dev_debug(adapter, "%s: txq->q.cntxt_id= %d err = %d\n",
4764a01078bSRahul Lakkireddy 		  __func__, txq->q.cntxt_id, err);
4774a01078bSRahul Lakkireddy 
4784a01078bSRahul Lakkireddy 	return err;
4794a01078bSRahul Lakkireddy }
4804a01078bSRahul Lakkireddy 
4814a01078bSRahul Lakkireddy static void cxgbe_dev_tx_queue_release(void *q)
4824a01078bSRahul Lakkireddy {
4834a01078bSRahul Lakkireddy 	struct sge_eth_txq *txq = (struct sge_eth_txq *)q;
4844a01078bSRahul Lakkireddy 
4854a01078bSRahul Lakkireddy 	if (txq) {
4864a01078bSRahul Lakkireddy 		struct port_info *pi = (struct port_info *)
4874a01078bSRahul Lakkireddy 				       (txq->eth_dev->data->dev_private);
4884a01078bSRahul Lakkireddy 		struct adapter *adap = pi->adapter;
4894a01078bSRahul Lakkireddy 
4904a01078bSRahul Lakkireddy 		dev_debug(adapter, "%s: pi->port_id = %d; tx_queue_id = %d\n",
4914a01078bSRahul Lakkireddy 			  __func__, pi->port_id, txq->q.cntxt_id);
4924a01078bSRahul Lakkireddy 
4934a01078bSRahul Lakkireddy 		t4_sge_eth_txq_release(adap, txq);
4944a01078bSRahul Lakkireddy 	}
4954a01078bSRahul Lakkireddy }
4964a01078bSRahul Lakkireddy 
49792c8a632SRahul Lakkireddy static int cxgbe_dev_rx_queue_start(struct rte_eth_dev *eth_dev,
49892c8a632SRahul Lakkireddy 				    uint16_t rx_queue_id)
49992c8a632SRahul Lakkireddy {
50092c8a632SRahul Lakkireddy 	struct port_info *pi = (struct port_info *)(eth_dev->data->dev_private);
50192c8a632SRahul Lakkireddy 	struct adapter *adap = pi->adapter;
50292c8a632SRahul Lakkireddy 	struct sge_rspq *q;
50392c8a632SRahul Lakkireddy 
50492c8a632SRahul Lakkireddy 	dev_debug(adapter, "%s: pi->port_id = %d; rx_queue_id = %d\n",
50592c8a632SRahul Lakkireddy 		  __func__, pi->port_id, rx_queue_id);
50692c8a632SRahul Lakkireddy 
50792c8a632SRahul Lakkireddy 	q = eth_dev->data->rx_queues[rx_queue_id];
50892c8a632SRahul Lakkireddy 	return t4_sge_eth_rxq_start(adap, q);
50992c8a632SRahul Lakkireddy }
51092c8a632SRahul Lakkireddy 
51192c8a632SRahul Lakkireddy static int cxgbe_dev_rx_queue_stop(struct rte_eth_dev *eth_dev,
51292c8a632SRahul Lakkireddy 				   uint16_t rx_queue_id)
51392c8a632SRahul Lakkireddy {
51492c8a632SRahul Lakkireddy 	struct port_info *pi = (struct port_info *)(eth_dev->data->dev_private);
51592c8a632SRahul Lakkireddy 	struct adapter *adap = pi->adapter;
51692c8a632SRahul Lakkireddy 	struct sge_rspq *q;
51792c8a632SRahul Lakkireddy 
51892c8a632SRahul Lakkireddy 	dev_debug(adapter, "%s: pi->port_id = %d; rx_queue_id = %d\n",
51992c8a632SRahul Lakkireddy 		  __func__, pi->port_id, rx_queue_id);
52092c8a632SRahul Lakkireddy 
52192c8a632SRahul Lakkireddy 	q = eth_dev->data->rx_queues[rx_queue_id];
52292c8a632SRahul Lakkireddy 	return t4_sge_eth_rxq_stop(adap, q);
52392c8a632SRahul Lakkireddy }
52492c8a632SRahul Lakkireddy 
52592c8a632SRahul Lakkireddy static int cxgbe_dev_rx_queue_setup(struct rte_eth_dev *eth_dev,
52692c8a632SRahul Lakkireddy 				    uint16_t queue_idx,	uint16_t nb_desc,
52792c8a632SRahul Lakkireddy 				    unsigned int socket_id,
52892c8a632SRahul Lakkireddy 				    const struct rte_eth_rxconf *rx_conf,
52992c8a632SRahul Lakkireddy 				    struct rte_mempool *mp)
53092c8a632SRahul Lakkireddy {
53192c8a632SRahul Lakkireddy 	struct port_info *pi = (struct port_info *)(eth_dev->data->dev_private);
53292c8a632SRahul Lakkireddy 	struct adapter *adapter = pi->adapter;
53392c8a632SRahul Lakkireddy 	struct sge *s = &adapter->sge;
53492c8a632SRahul Lakkireddy 	struct sge_eth_rxq *rxq = &s->ethrxq[pi->first_qset + queue_idx];
53592c8a632SRahul Lakkireddy 	int err = 0;
53692c8a632SRahul Lakkireddy 	int msi_idx = 0;
53792c8a632SRahul Lakkireddy 	unsigned int temp_nb_desc;
5384b2eff45SRahul Lakkireddy 	struct rte_eth_dev_info dev_info;
5394b2eff45SRahul Lakkireddy 	unsigned int pkt_len = eth_dev->data->dev_conf.rxmode.max_rx_pkt_len;
54092c8a632SRahul Lakkireddy 
54192c8a632SRahul Lakkireddy 	RTE_SET_USED(rx_conf);
54292c8a632SRahul Lakkireddy 
54392c8a632SRahul Lakkireddy 	dev_debug(adapter, "%s: eth_dev->data->nb_rx_queues = %d; queue_idx = %d; nb_desc = %d; socket_id = %d; mp = %p\n",
54492c8a632SRahul Lakkireddy 		  __func__, eth_dev->data->nb_rx_queues, queue_idx, nb_desc,
54592c8a632SRahul Lakkireddy 		  socket_id, mp);
54692c8a632SRahul Lakkireddy 
5474b2eff45SRahul Lakkireddy 	cxgbe_dev_info_get(eth_dev, &dev_info);
5484b2eff45SRahul Lakkireddy 
5494b2eff45SRahul Lakkireddy 	/* Must accommodate at least ETHER_MIN_MTU */
5504b2eff45SRahul Lakkireddy 	if ((pkt_len < dev_info.min_rx_bufsize) ||
5514b2eff45SRahul Lakkireddy 	    (pkt_len > dev_info.max_rx_pktlen)) {
5524b2eff45SRahul Lakkireddy 		dev_err(adap, "%s: max pkt len must be > %d and <= %d\n",
5534b2eff45SRahul Lakkireddy 			__func__, dev_info.min_rx_bufsize,
5544b2eff45SRahul Lakkireddy 			dev_info.max_rx_pktlen);
5554b2eff45SRahul Lakkireddy 		return -EINVAL;
5564b2eff45SRahul Lakkireddy 	}
5574b2eff45SRahul Lakkireddy 
55892c8a632SRahul Lakkireddy 	/*  Free up the existing queue  */
55992c8a632SRahul Lakkireddy 	if (eth_dev->data->rx_queues[queue_idx]) {
56092c8a632SRahul Lakkireddy 		cxgbe_dev_rx_queue_release(eth_dev->data->rx_queues[queue_idx]);
56192c8a632SRahul Lakkireddy 		eth_dev->data->rx_queues[queue_idx] = NULL;
56292c8a632SRahul Lakkireddy 	}
56392c8a632SRahul Lakkireddy 
56492c8a632SRahul Lakkireddy 	eth_dev->data->rx_queues[queue_idx] = (void *)rxq;
56592c8a632SRahul Lakkireddy 
56692c8a632SRahul Lakkireddy 	/* Sanity Checking
56792c8a632SRahul Lakkireddy 	 *
56892c8a632SRahul Lakkireddy 	 * nb_desc should be > 0 and <= CXGBE_MAX_RING_DESC_SIZE
56992c8a632SRahul Lakkireddy 	 */
57092c8a632SRahul Lakkireddy 	temp_nb_desc = nb_desc;
57192c8a632SRahul Lakkireddy 	if (nb_desc < CXGBE_MIN_RING_DESC_SIZE) {
57292c8a632SRahul Lakkireddy 		dev_warn(adapter, "%s: number of descriptors must be >= %d. Using default [%d]\n",
57392c8a632SRahul Lakkireddy 			 __func__, CXGBE_MIN_RING_DESC_SIZE,
57492c8a632SRahul Lakkireddy 			 CXGBE_DEFAULT_RX_DESC_SIZE);
57592c8a632SRahul Lakkireddy 		temp_nb_desc = CXGBE_DEFAULT_RX_DESC_SIZE;
57692c8a632SRahul Lakkireddy 	} else if (nb_desc > CXGBE_MAX_RING_DESC_SIZE) {
57792c8a632SRahul Lakkireddy 		dev_err(adapter, "%s: number of descriptors must be between %d and %d inclusive. Default [%d]\n",
57892c8a632SRahul Lakkireddy 			__func__, CXGBE_MIN_RING_DESC_SIZE,
57992c8a632SRahul Lakkireddy 			CXGBE_MAX_RING_DESC_SIZE, CXGBE_DEFAULT_RX_DESC_SIZE);
58092c8a632SRahul Lakkireddy 		return -(EINVAL);
58192c8a632SRahul Lakkireddy 	}
58292c8a632SRahul Lakkireddy 
58392c8a632SRahul Lakkireddy 	rxq->rspq.size = temp_nb_desc;
58492c8a632SRahul Lakkireddy 	if ((&rxq->fl) != NULL)
58592c8a632SRahul Lakkireddy 		rxq->fl.size = temp_nb_desc;
58692c8a632SRahul Lakkireddy 
5874b2eff45SRahul Lakkireddy 	/* Set to jumbo mode if necessary */
5884b2eff45SRahul Lakkireddy 	if (pkt_len > ETHER_MAX_LEN)
5894b2eff45SRahul Lakkireddy 		eth_dev->data->dev_conf.rxmode.jumbo_frame = 1;
5904b2eff45SRahul Lakkireddy 	else
5914b2eff45SRahul Lakkireddy 		eth_dev->data->dev_conf.rxmode.jumbo_frame = 0;
5924b2eff45SRahul Lakkireddy 
59392c8a632SRahul Lakkireddy 	err = t4_sge_alloc_rxq(adapter, &rxq->rspq, false, eth_dev, msi_idx,
59492c8a632SRahul Lakkireddy 			       &rxq->fl, t4_ethrx_handler,
59592c8a632SRahul Lakkireddy 			       t4_get_mps_bg_map(adapter, pi->tx_chan), mp,
59692c8a632SRahul Lakkireddy 			       queue_idx, socket_id);
59792c8a632SRahul Lakkireddy 
59892c8a632SRahul Lakkireddy 	dev_debug(adapter, "%s: err = %d; port_id = %d; cntxt_id = %u\n",
59992c8a632SRahul Lakkireddy 		  __func__, err, pi->port_id, rxq->rspq.cntxt_id);
60092c8a632SRahul Lakkireddy 	return err;
60192c8a632SRahul Lakkireddy }
60292c8a632SRahul Lakkireddy 
60392c8a632SRahul Lakkireddy static void cxgbe_dev_rx_queue_release(void *q)
60492c8a632SRahul Lakkireddy {
60592c8a632SRahul Lakkireddy 	struct sge_eth_rxq *rxq = (struct sge_eth_rxq *)q;
60692c8a632SRahul Lakkireddy 	struct sge_rspq *rq = &rxq->rspq;
60792c8a632SRahul Lakkireddy 
60892c8a632SRahul Lakkireddy 	if (rq) {
60992c8a632SRahul Lakkireddy 		struct port_info *pi = (struct port_info *)
61092c8a632SRahul Lakkireddy 				       (rq->eth_dev->data->dev_private);
61192c8a632SRahul Lakkireddy 		struct adapter *adap = pi->adapter;
61292c8a632SRahul Lakkireddy 
61392c8a632SRahul Lakkireddy 		dev_debug(adapter, "%s: pi->port_id = %d; rx_queue_id = %d\n",
61492c8a632SRahul Lakkireddy 			  __func__, pi->port_id, rxq->rspq.cntxt_id);
61592c8a632SRahul Lakkireddy 
61692c8a632SRahul Lakkireddy 		t4_sge_eth_rxq_release(adap, rxq);
61792c8a632SRahul Lakkireddy 	}
61892c8a632SRahul Lakkireddy }
61992c8a632SRahul Lakkireddy 
620856505d3SRahul Lakkireddy /*
621856505d3SRahul Lakkireddy  * Get port statistics.
622856505d3SRahul Lakkireddy  */
623856505d3SRahul Lakkireddy static void cxgbe_dev_stats_get(struct rte_eth_dev *eth_dev,
624856505d3SRahul Lakkireddy 				struct rte_eth_stats *eth_stats)
625856505d3SRahul Lakkireddy {
626856505d3SRahul Lakkireddy 	struct port_info *pi = (struct port_info *)(eth_dev->data->dev_private);
627856505d3SRahul Lakkireddy 	struct adapter *adapter = pi->adapter;
628856505d3SRahul Lakkireddy 	struct sge *s = &adapter->sge;
629856505d3SRahul Lakkireddy 	struct port_stats ps;
630856505d3SRahul Lakkireddy 	unsigned int i;
631856505d3SRahul Lakkireddy 
632856505d3SRahul Lakkireddy 	cxgbe_stats_get(pi, &ps);
633856505d3SRahul Lakkireddy 
634856505d3SRahul Lakkireddy 	/* RX Stats */
635856505d3SRahul Lakkireddy 	eth_stats->ipackets = ps.rx_frames;
636856505d3SRahul Lakkireddy 	eth_stats->ibytes   = ps.rx_octets;
637856505d3SRahul Lakkireddy 	eth_stats->imcasts  = ps.rx_mcast_frames;
638856505d3SRahul Lakkireddy 	eth_stats->imissed  = ps.rx_ovflow0 + ps.rx_ovflow1 +
639856505d3SRahul Lakkireddy 			      ps.rx_ovflow2 + ps.rx_ovflow3 +
640856505d3SRahul Lakkireddy 			      ps.rx_trunc0 + ps.rx_trunc1 +
641856505d3SRahul Lakkireddy 			      ps.rx_trunc2 + ps.rx_trunc3;
642856505d3SRahul Lakkireddy 	eth_stats->ibadcrc  = ps.rx_fcs_err;
643856505d3SRahul Lakkireddy 	eth_stats->ibadlen  = ps.rx_jabber + ps.rx_too_long + ps.rx_runt;
644856505d3SRahul Lakkireddy 	eth_stats->ierrors  = ps.rx_symbol_err + eth_stats->ibadcrc +
645856505d3SRahul Lakkireddy 			      eth_stats->ibadlen + ps.rx_len_err +
646856505d3SRahul Lakkireddy 			      eth_stats->imissed;
647856505d3SRahul Lakkireddy 	eth_stats->rx_pause_xon  = ps.rx_pause;
648856505d3SRahul Lakkireddy 
649856505d3SRahul Lakkireddy 	/* TX Stats */
650856505d3SRahul Lakkireddy 	eth_stats->opackets = ps.tx_frames;
651856505d3SRahul Lakkireddy 	eth_stats->obytes   = ps.tx_octets;
652856505d3SRahul Lakkireddy 	eth_stats->oerrors  = ps.tx_error_frames;
653856505d3SRahul Lakkireddy 	eth_stats->tx_pause_xon  = ps.tx_pause;
654856505d3SRahul Lakkireddy 
655856505d3SRahul Lakkireddy 	for (i = 0; i < pi->n_rx_qsets; i++) {
656856505d3SRahul Lakkireddy 		struct sge_eth_rxq *rxq =
657856505d3SRahul Lakkireddy 			&s->ethrxq[pi->first_qset + i];
658856505d3SRahul Lakkireddy 
659856505d3SRahul Lakkireddy 		eth_stats->q_ipackets[i] = rxq->stats.pkts;
660856505d3SRahul Lakkireddy 		eth_stats->q_ibytes[i] = rxq->stats.rx_bytes;
661856505d3SRahul Lakkireddy 	}
662856505d3SRahul Lakkireddy 
663856505d3SRahul Lakkireddy 	for (i = 0; i < pi->n_tx_qsets; i++) {
664856505d3SRahul Lakkireddy 		struct sge_eth_txq *txq =
665856505d3SRahul Lakkireddy 			&s->ethtxq[pi->first_qset + i];
666856505d3SRahul Lakkireddy 
667856505d3SRahul Lakkireddy 		eth_stats->q_opackets[i] = txq->stats.pkts;
668856505d3SRahul Lakkireddy 		eth_stats->q_obytes[i] = txq->stats.tx_bytes;
669856505d3SRahul Lakkireddy 		eth_stats->q_errors[i] = txq->stats.mapping_err;
670856505d3SRahul Lakkireddy 	}
671856505d3SRahul Lakkireddy }
672856505d3SRahul Lakkireddy 
673856505d3SRahul Lakkireddy /*
674856505d3SRahul Lakkireddy  * Reset port statistics.
675856505d3SRahul Lakkireddy  */
676856505d3SRahul Lakkireddy static void cxgbe_dev_stats_reset(struct rte_eth_dev *eth_dev)
677856505d3SRahul Lakkireddy {
678856505d3SRahul Lakkireddy 	struct port_info *pi = (struct port_info *)(eth_dev->data->dev_private);
679856505d3SRahul Lakkireddy 	struct adapter *adapter = pi->adapter;
680856505d3SRahul Lakkireddy 	struct sge *s = &adapter->sge;
681856505d3SRahul Lakkireddy 	unsigned int i;
682856505d3SRahul Lakkireddy 
683856505d3SRahul Lakkireddy 	cxgbe_stats_reset(pi);
684856505d3SRahul Lakkireddy 	for (i = 0; i < pi->n_rx_qsets; i++) {
685856505d3SRahul Lakkireddy 		struct sge_eth_rxq *rxq =
686856505d3SRahul Lakkireddy 			&s->ethrxq[pi->first_qset + i];
687856505d3SRahul Lakkireddy 
688856505d3SRahul Lakkireddy 		rxq->stats.pkts = 0;
689856505d3SRahul Lakkireddy 		rxq->stats.rx_bytes = 0;
690856505d3SRahul Lakkireddy 	}
691856505d3SRahul Lakkireddy 	for (i = 0; i < pi->n_tx_qsets; i++) {
692856505d3SRahul Lakkireddy 		struct sge_eth_txq *txq =
693856505d3SRahul Lakkireddy 			&s->ethtxq[pi->first_qset + i];
694856505d3SRahul Lakkireddy 
695856505d3SRahul Lakkireddy 		txq->stats.pkts = 0;
696856505d3SRahul Lakkireddy 		txq->stats.tx_bytes = 0;
697856505d3SRahul Lakkireddy 		txq->stats.mapping_err = 0;
698856505d3SRahul Lakkireddy 	}
699856505d3SRahul Lakkireddy }
700856505d3SRahul Lakkireddy 
701631dfc71SRahul Lakkireddy static int cxgbe_flow_ctrl_get(struct rte_eth_dev *eth_dev,
702631dfc71SRahul Lakkireddy 			       struct rte_eth_fc_conf *fc_conf)
703631dfc71SRahul Lakkireddy {
704631dfc71SRahul Lakkireddy 	struct port_info *pi = (struct port_info *)(eth_dev->data->dev_private);
705631dfc71SRahul Lakkireddy 	struct link_config *lc = &pi->link_cfg;
706631dfc71SRahul Lakkireddy 	int rx_pause, tx_pause;
707631dfc71SRahul Lakkireddy 
708631dfc71SRahul Lakkireddy 	fc_conf->autoneg = lc->fc & PAUSE_AUTONEG;
709631dfc71SRahul Lakkireddy 	rx_pause = lc->fc & PAUSE_RX;
710631dfc71SRahul Lakkireddy 	tx_pause = lc->fc & PAUSE_TX;
711631dfc71SRahul Lakkireddy 
712631dfc71SRahul Lakkireddy 	if (rx_pause && tx_pause)
713631dfc71SRahul Lakkireddy 		fc_conf->mode = RTE_FC_FULL;
714631dfc71SRahul Lakkireddy 	else if (rx_pause)
715631dfc71SRahul Lakkireddy 		fc_conf->mode = RTE_FC_RX_PAUSE;
716631dfc71SRahul Lakkireddy 	else if (tx_pause)
717631dfc71SRahul Lakkireddy 		fc_conf->mode = RTE_FC_TX_PAUSE;
718631dfc71SRahul Lakkireddy 	else
719631dfc71SRahul Lakkireddy 		fc_conf->mode = RTE_FC_NONE;
720631dfc71SRahul Lakkireddy 	return 0;
721631dfc71SRahul Lakkireddy }
722631dfc71SRahul Lakkireddy 
723631dfc71SRahul Lakkireddy static int cxgbe_flow_ctrl_set(struct rte_eth_dev *eth_dev,
724631dfc71SRahul Lakkireddy 			       struct rte_eth_fc_conf *fc_conf)
725631dfc71SRahul Lakkireddy {
726631dfc71SRahul Lakkireddy 	struct port_info *pi = (struct port_info *)(eth_dev->data->dev_private);
727631dfc71SRahul Lakkireddy 	struct adapter *adapter = pi->adapter;
728631dfc71SRahul Lakkireddy 	struct link_config *lc = &pi->link_cfg;
729631dfc71SRahul Lakkireddy 
730631dfc71SRahul Lakkireddy 	if (lc->supported & FW_PORT_CAP_ANEG) {
731631dfc71SRahul Lakkireddy 		if (fc_conf->autoneg)
732631dfc71SRahul Lakkireddy 			lc->requested_fc |= PAUSE_AUTONEG;
733631dfc71SRahul Lakkireddy 		else
734631dfc71SRahul Lakkireddy 			lc->requested_fc &= ~PAUSE_AUTONEG;
735631dfc71SRahul Lakkireddy 	}
736631dfc71SRahul Lakkireddy 
737631dfc71SRahul Lakkireddy 	if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
738631dfc71SRahul Lakkireddy 	    (fc_conf->mode & RTE_FC_RX_PAUSE))
739631dfc71SRahul Lakkireddy 		lc->requested_fc |= PAUSE_RX;
740631dfc71SRahul Lakkireddy 	else
741631dfc71SRahul Lakkireddy 		lc->requested_fc &= ~PAUSE_RX;
742631dfc71SRahul Lakkireddy 
743631dfc71SRahul Lakkireddy 	if (((fc_conf->mode & RTE_FC_FULL) == RTE_FC_FULL) ||
744631dfc71SRahul Lakkireddy 	    (fc_conf->mode & RTE_FC_TX_PAUSE))
745631dfc71SRahul Lakkireddy 		lc->requested_fc |= PAUSE_TX;
746631dfc71SRahul Lakkireddy 	else
747631dfc71SRahul Lakkireddy 		lc->requested_fc &= ~PAUSE_TX;
748631dfc71SRahul Lakkireddy 
749631dfc71SRahul Lakkireddy 	return t4_link_l1cfg(adapter, adapter->mbox, pi->tx_chan,
750631dfc71SRahul Lakkireddy 			     &pi->link_cfg);
751631dfc71SRahul Lakkireddy }
752631dfc71SRahul Lakkireddy 
75383189849SRahul Lakkireddy static struct eth_dev_ops cxgbe_eth_dev_ops = {
7540462d115SRahul Lakkireddy 	.dev_start		= cxgbe_dev_start,
7550462d115SRahul Lakkireddy 	.dev_stop		= cxgbe_dev_stop,
7560462d115SRahul Lakkireddy 	.dev_close		= cxgbe_dev_close,
757cdac6e2eSRahul Lakkireddy 	.promiscuous_enable	= cxgbe_dev_promiscuous_enable,
758cdac6e2eSRahul Lakkireddy 	.promiscuous_disable	= cxgbe_dev_promiscuous_disable,
759cdac6e2eSRahul Lakkireddy 	.allmulticast_enable	= cxgbe_dev_allmulticast_enable,
760cdac6e2eSRahul Lakkireddy 	.allmulticast_disable	= cxgbe_dev_allmulticast_disable,
76192c8a632SRahul Lakkireddy 	.dev_configure		= cxgbe_dev_configure,
76292c8a632SRahul Lakkireddy 	.dev_infos_get		= cxgbe_dev_info_get,
763cdac6e2eSRahul Lakkireddy 	.link_update		= cxgbe_dev_link_update,
7640ec33be4SRahul Lakkireddy 	.mtu_set		= cxgbe_dev_mtu_set,
7654a01078bSRahul Lakkireddy 	.tx_queue_setup         = cxgbe_dev_tx_queue_setup,
7664a01078bSRahul Lakkireddy 	.tx_queue_start		= cxgbe_dev_tx_queue_start,
7674a01078bSRahul Lakkireddy 	.tx_queue_stop		= cxgbe_dev_tx_queue_stop,
7684a01078bSRahul Lakkireddy 	.tx_queue_release	= cxgbe_dev_tx_queue_release,
76992c8a632SRahul Lakkireddy 	.rx_queue_setup         = cxgbe_dev_rx_queue_setup,
77092c8a632SRahul Lakkireddy 	.rx_queue_start		= cxgbe_dev_rx_queue_start,
77192c8a632SRahul Lakkireddy 	.rx_queue_stop		= cxgbe_dev_rx_queue_stop,
77292c8a632SRahul Lakkireddy 	.rx_queue_release	= cxgbe_dev_rx_queue_release,
773856505d3SRahul Lakkireddy 	.stats_get		= cxgbe_dev_stats_get,
774856505d3SRahul Lakkireddy 	.stats_reset		= cxgbe_dev_stats_reset,
775631dfc71SRahul Lakkireddy 	.flow_ctrl_get		= cxgbe_flow_ctrl_get,
776631dfc71SRahul Lakkireddy 	.flow_ctrl_set		= cxgbe_flow_ctrl_set,
77783189849SRahul Lakkireddy };
77883189849SRahul Lakkireddy 
77983189849SRahul Lakkireddy /*
78083189849SRahul Lakkireddy  * Initialize driver
78183189849SRahul Lakkireddy  * It returns 0 on success.
78283189849SRahul Lakkireddy  */
78383189849SRahul Lakkireddy static int eth_cxgbe_dev_init(struct rte_eth_dev *eth_dev)
78483189849SRahul Lakkireddy {
78583189849SRahul Lakkireddy 	struct rte_pci_device *pci_dev;
78683189849SRahul Lakkireddy 	struct port_info *pi = (struct port_info *)(eth_dev->data->dev_private);
78783189849SRahul Lakkireddy 	struct adapter *adapter = NULL;
78883189849SRahul Lakkireddy 	char name[RTE_ETH_NAME_MAX_LEN];
78983189849SRahul Lakkireddy 	int err = 0;
79083189849SRahul Lakkireddy 
79183189849SRahul Lakkireddy 	CXGBE_FUNC_TRACE();
79283189849SRahul Lakkireddy 
79383189849SRahul Lakkireddy 	eth_dev->dev_ops = &cxgbe_eth_dev_ops;
79492c8a632SRahul Lakkireddy 	eth_dev->rx_pkt_burst = &cxgbe_recv_pkts;
7954a01078bSRahul Lakkireddy 	eth_dev->tx_pkt_burst = &cxgbe_xmit_pkts;
79683189849SRahul Lakkireddy 
79783189849SRahul Lakkireddy 	/* for secondary processes, we don't initialise any further as primary
79883189849SRahul Lakkireddy 	 * has already done this work.
79983189849SRahul Lakkireddy 	 */
80083189849SRahul Lakkireddy 	if (rte_eal_process_type() != RTE_PROC_PRIMARY)
80183189849SRahul Lakkireddy 		return 0;
80283189849SRahul Lakkireddy 
80383189849SRahul Lakkireddy 	pci_dev = eth_dev->pci_dev;
804*eeefe73fSBernard Iremonger 
805*eeefe73fSBernard Iremonger 	rte_eth_copy_pci_info(eth_dev, pci_dev);
806*eeefe73fSBernard Iremonger 
80783189849SRahul Lakkireddy 	snprintf(name, sizeof(name), "cxgbeadapter%d", eth_dev->data->port_id);
80883189849SRahul Lakkireddy 	adapter = rte_zmalloc(name, sizeof(*adapter), 0);
80983189849SRahul Lakkireddy 	if (!adapter)
81083189849SRahul Lakkireddy 		return -1;
81183189849SRahul Lakkireddy 
81283189849SRahul Lakkireddy 	adapter->use_unpacked_mode = 1;
81383189849SRahul Lakkireddy 	adapter->regs = (void *)pci_dev->mem_resource[0].addr;
81483189849SRahul Lakkireddy 	if (!adapter->regs) {
81583189849SRahul Lakkireddy 		dev_err(adapter, "%s: cannot map device registers\n", __func__);
81683189849SRahul Lakkireddy 		err = -ENOMEM;
81783189849SRahul Lakkireddy 		goto out_free_adapter;
81883189849SRahul Lakkireddy 	}
81983189849SRahul Lakkireddy 	adapter->pdev = pci_dev;
82083189849SRahul Lakkireddy 	adapter->eth_dev = eth_dev;
82183189849SRahul Lakkireddy 	pi->adapter = adapter;
82283189849SRahul Lakkireddy 
82383189849SRahul Lakkireddy 	err = cxgbe_probe(adapter);
82483189849SRahul Lakkireddy 	if (err)
82583189849SRahul Lakkireddy 		dev_err(adapter, "%s: cxgbe probe failed with err %d\n",
82683189849SRahul Lakkireddy 			__func__, err);
82783189849SRahul Lakkireddy 
82883189849SRahul Lakkireddy out_free_adapter:
82983189849SRahul Lakkireddy 	return err;
83083189849SRahul Lakkireddy }
83183189849SRahul Lakkireddy 
83283189849SRahul Lakkireddy static struct eth_driver rte_cxgbe_pmd = {
83383189849SRahul Lakkireddy 	{
83483189849SRahul Lakkireddy 		.name = "rte_cxgbe_pmd",
83583189849SRahul Lakkireddy 		.id_table = cxgb4_pci_tbl,
83683189849SRahul Lakkireddy 		.drv_flags = RTE_PCI_DRV_NEED_MAPPING | RTE_PCI_DRV_INTR_LSC,
83783189849SRahul Lakkireddy 	},
83883189849SRahul Lakkireddy 	.eth_dev_init = eth_cxgbe_dev_init,
83983189849SRahul Lakkireddy 	.dev_private_size = sizeof(struct port_info),
84083189849SRahul Lakkireddy };
84183189849SRahul Lakkireddy 
84283189849SRahul Lakkireddy /*
84383189849SRahul Lakkireddy  * Driver initialization routine.
84483189849SRahul Lakkireddy  * Invoked once at EAL init time.
84583189849SRahul Lakkireddy  * Register itself as the [Poll Mode] Driver of PCI CXGBE devices.
84683189849SRahul Lakkireddy  */
84783189849SRahul Lakkireddy static int rte_cxgbe_pmd_init(const char *name __rte_unused,
84883189849SRahul Lakkireddy 			      const char *params __rte_unused)
84983189849SRahul Lakkireddy {
85083189849SRahul Lakkireddy 	CXGBE_FUNC_TRACE();
85183189849SRahul Lakkireddy 
85283189849SRahul Lakkireddy 	rte_eth_driver_register(&rte_cxgbe_pmd);
85383189849SRahul Lakkireddy 	return 0;
85483189849SRahul Lakkireddy }
85583189849SRahul Lakkireddy 
85683189849SRahul Lakkireddy static struct rte_driver rte_cxgbe_driver = {
85783189849SRahul Lakkireddy 	.name = "cxgbe_driver",
85883189849SRahul Lakkireddy 	.type = PMD_PDEV,
85983189849SRahul Lakkireddy 	.init = rte_cxgbe_pmd_init,
86083189849SRahul Lakkireddy };
86183189849SRahul Lakkireddy 
86283189849SRahul Lakkireddy PMD_REGISTER_DRIVER(rte_cxgbe_driver);
863