xref: /dpdk/drivers/net/cxgbe/cxgbevf_ethdev.c (revision 25d11a86c56d50947af33d0b79ede622809bd8b9)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2018 Chelsio Communications.
3  * All rights reserved.
4  */
5 
6 #include <rte_ethdev_driver.h>
7 #include <rte_ethdev_pci.h>
8 
9 #include "cxgbe.h"
10 #include "cxgbe_pfvf.h"
11 
12 /*
13  * Macros needed to support the PCI Device ID Table ...
14  */
15 #define CH_PCI_DEVICE_ID_TABLE_DEFINE_BEGIN \
16 	static const struct rte_pci_id cxgb4vf_pci_tbl[] = {
17 #define CH_PCI_DEVICE_ID_FUNCTION 0x8
18 
19 #define PCI_VENDOR_ID_CHELSIO 0x1425
20 
21 #define CH_PCI_ID_TABLE_ENTRY(devid) \
22 		{ RTE_PCI_DEVICE(PCI_VENDOR_ID_CHELSIO, (devid)) }
23 
24 #define CH_PCI_DEVICE_ID_TABLE_DEFINE_END \
25 		{ .vendor_id = 0, } \
26 	}
27 
28 /*
29  *... and the PCI ID Table itself ...
30  */
31 #include "base/t4_pci_id_tbl.h"
32 
33 /*
34  * Get port statistics.
35  */
36 static int cxgbevf_dev_stats_get(struct rte_eth_dev *eth_dev,
37 				 struct rte_eth_stats *eth_stats)
38 {
39 	struct port_info *pi = (struct port_info *)(eth_dev->data->dev_private);
40 	struct adapter *adapter = pi->adapter;
41 	struct sge *s = &adapter->sge;
42 	struct port_stats ps;
43 	unsigned int i;
44 
45 	cxgbevf_stats_get(pi, &ps);
46 
47 	/* RX Stats */
48 	eth_stats->ierrors  = ps.rx_len_err;
49 
50 	/* TX Stats */
51 	eth_stats->opackets = ps.tx_bcast_frames + ps.tx_mcast_frames +
52 			      ps.tx_ucast_frames;
53 	eth_stats->obytes = ps.tx_octets;
54 	eth_stats->oerrors  = ps.tx_drop;
55 
56 	for (i = 0; i < pi->n_rx_qsets; i++) {
57 		struct sge_eth_rxq *rxq =
58 			&s->ethrxq[pi->first_qset + i];
59 
60 		eth_stats->q_ipackets[i] = rxq->stats.pkts;
61 		eth_stats->q_ibytes[i] = rxq->stats.rx_bytes;
62 		eth_stats->ipackets += eth_stats->q_ipackets[i];
63 		eth_stats->ibytes += eth_stats->q_ibytes[i];
64 	}
65 
66 	for (i = 0; i < pi->n_tx_qsets; i++) {
67 		struct sge_eth_txq *txq =
68 			&s->ethtxq[pi->first_qset + i];
69 
70 		eth_stats->q_opackets[i] = txq->stats.pkts;
71 		eth_stats->q_obytes[i] = txq->stats.tx_bytes;
72 		eth_stats->q_errors[i] = txq->stats.mapping_err;
73 	}
74 	return 0;
75 }
76 
77 static const struct eth_dev_ops cxgbevf_eth_dev_ops = {
78 	.dev_start              = cxgbe_dev_start,
79 	.dev_stop               = cxgbe_dev_stop,
80 	.dev_close              = cxgbe_dev_close,
81 	.promiscuous_enable     = cxgbe_dev_promiscuous_enable,
82 	.promiscuous_disable    = cxgbe_dev_promiscuous_disable,
83 	.allmulticast_enable    = cxgbe_dev_allmulticast_enable,
84 	.allmulticast_disable   = cxgbe_dev_allmulticast_disable,
85 	.dev_configure          = cxgbe_dev_configure,
86 	.dev_infos_get          = cxgbe_dev_info_get,
87 	.dev_supported_ptypes_get = cxgbe_dev_supported_ptypes_get,
88 	.link_update            = cxgbe_dev_link_update,
89 	.dev_set_link_up        = cxgbe_dev_set_link_up,
90 	.dev_set_link_down      = cxgbe_dev_set_link_down,
91 	.mtu_set                = cxgbe_dev_mtu_set,
92 	.tx_queue_setup         = cxgbe_dev_tx_queue_setup,
93 	.tx_queue_start         = cxgbe_dev_tx_queue_start,
94 	.tx_queue_stop          = cxgbe_dev_tx_queue_stop,
95 	.tx_queue_release       = cxgbe_dev_tx_queue_release,
96 	.rx_queue_setup         = cxgbe_dev_rx_queue_setup,
97 	.rx_queue_start         = cxgbe_dev_rx_queue_start,
98 	.rx_queue_stop          = cxgbe_dev_rx_queue_stop,
99 	.rx_queue_release       = cxgbe_dev_rx_queue_release,
100 	.stats_get		= cxgbevf_dev_stats_get,
101 	.mac_addr_set		= cxgbe_mac_addr_set,
102 };
103 
104 /*
105  * Initialize driver
106  * It returns 0 on success.
107  */
108 static int eth_cxgbevf_dev_init(struct rte_eth_dev *eth_dev)
109 {
110 	struct port_info *pi = (struct port_info *)(eth_dev->data->dev_private);
111 	struct rte_pci_device *pci_dev;
112 	char name[RTE_ETH_NAME_MAX_LEN];
113 	struct adapter *adapter = NULL;
114 	int err = 0;
115 
116 	CXGBE_FUNC_TRACE();
117 
118 	eth_dev->dev_ops = &cxgbevf_eth_dev_ops;
119 	eth_dev->rx_pkt_burst = &cxgbe_recv_pkts;
120 	eth_dev->tx_pkt_burst = &cxgbe_xmit_pkts;
121 	pci_dev = RTE_ETH_DEV_TO_PCI(eth_dev);
122 
123 	/* for secondary processes, we attach to ethdevs allocated by primary
124 	 * and do minimal initialization.
125 	 */
126 	if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
127 		int i;
128 
129 		for (i = 1; i < MAX_NPORTS; i++) {
130 			struct rte_eth_dev *rest_eth_dev;
131 			char namei[RTE_ETH_NAME_MAX_LEN];
132 
133 			snprintf(namei, sizeof(namei), "%s_%d",
134 				 pci_dev->device.name, i);
135 			rest_eth_dev = rte_eth_dev_attach_secondary(namei);
136 			if (rest_eth_dev) {
137 				rest_eth_dev->device = &pci_dev->device;
138 				rest_eth_dev->dev_ops =
139 					eth_dev->dev_ops;
140 				rest_eth_dev->rx_pkt_burst =
141 					eth_dev->rx_pkt_burst;
142 				rest_eth_dev->tx_pkt_burst =
143 					eth_dev->tx_pkt_burst;
144 				rte_eth_dev_probing_finish(rest_eth_dev);
145 			}
146 		}
147 		return 0;
148 	}
149 
150 	snprintf(name, sizeof(name), "cxgbevfadapter%d",
151 		 eth_dev->data->port_id);
152 	adapter = rte_zmalloc(name, sizeof(*adapter), 0);
153 	if (!adapter)
154 		return -1;
155 
156 	adapter->use_unpacked_mode = 1;
157 	adapter->regs = (void *)pci_dev->mem_resource[0].addr;
158 	if (!adapter->regs) {
159 		dev_err(adapter, "%s: cannot map device registers\n", __func__);
160 		err = -ENOMEM;
161 		goto out_free_adapter;
162 	}
163 	adapter->pdev = pci_dev;
164 	adapter->eth_dev = eth_dev;
165 	pi->adapter = adapter;
166 	err = cxgbevf_probe(adapter);
167 	if (err) {
168 		dev_err(adapter, "%s: cxgbevf probe failed with err %d\n",
169 			__func__, err);
170 		goto out_free_adapter;
171 	}
172 
173 	return 0;
174 
175 out_free_adapter:
176 	rte_free(adapter);
177 	return err;
178 }
179 
180 static int eth_cxgbevf_dev_uninit(struct rte_eth_dev *eth_dev)
181 {
182 	struct port_info *pi = (struct port_info *)(eth_dev->data->dev_private);
183 	struct adapter *adap = pi->adapter;
184 
185 	/* Free up other ports and all resources */
186 	cxgbe_close(adap);
187 	return 0;
188 }
189 
190 static int eth_cxgbevf_pci_probe(struct rte_pci_driver *pci_drv __rte_unused,
191 				 struct rte_pci_device *pci_dev)
192 {
193 	return rte_eth_dev_pci_generic_probe(pci_dev, sizeof(struct port_info),
194 					     eth_cxgbevf_dev_init);
195 }
196 
197 static int eth_cxgbevf_pci_remove(struct rte_pci_device *pci_dev)
198 {
199 	return rte_eth_dev_pci_generic_remove(pci_dev, eth_cxgbevf_dev_uninit);
200 }
201 
202 static struct rte_pci_driver rte_cxgbevf_pmd = {
203 	.id_table = cxgb4vf_pci_tbl,
204 	.drv_flags = RTE_PCI_DRV_NEED_MAPPING,
205 	.probe = eth_cxgbevf_pci_probe,
206 	.remove = eth_cxgbevf_pci_remove,
207 };
208 
209 RTE_PMD_REGISTER_PCI(net_cxgbevf, rte_cxgbevf_pmd);
210 RTE_PMD_REGISTER_PCI_TABLE(net_cxgbevf, cxgb4vf_pci_tbl);
211 RTE_PMD_REGISTER_KMOD_DEP(net_cxgbevf, "* igb_uio | vfio-pci");
212