1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
2 /* Copyright (C) 2015-2018 Netronome Systems, Inc. */
3 
4 /*
5  * nfp_netvf_main.c
6  * Netronome virtual function network device driver: Main entry point
7  * Author: Jason McMullan <jason.mcmullan@netronome.com>
8  *         Rolf Neugebauer <rolf.neugebauer@netronome.com>
9  */
10 
11 #include <linux/module.h>
12 #include <linux/kernel.h>
13 #include <linux/init.h>
14 #include <linux/etherdevice.h>
15 
16 #include "nfpcore/nfp_dev.h"
17 #include "nfp_net_ctrl.h"
18 #include "nfp_net.h"
19 #include "nfp_main.h"
20 
21 /**
22  * struct nfp_net_vf - NFP VF-specific device structure
23  * @nn:		NFP Net structure for this device
24  * @irq_entries: Pre-allocated array of MSI-X entries
25  * @q_bar:	Pointer to mapped QC memory (NULL if TX/RX mapped directly)
26  * @ddir:	Per-device debugfs directory
27  */
28 struct nfp_net_vf {
29 	struct nfp_net *nn;
30 
31 	struct msix_entry irq_entries[NFP_NET_NON_Q_VECTORS +
32 				      NFP_NET_MAX_TX_RINGS];
33 	u8 __iomem *q_bar;
34 
35 	struct dentry *ddir;
36 };
37 
38 static const char nfp_net_driver_name[] = "nfp_netvf";
39 
40 static const struct pci_device_id nfp_netvf_pci_device_ids[] = {
41 	{ PCI_VENDOR_ID_NETRONOME, PCI_DEVICE_ID_NETRONOME_NFP3800_VF,
42 	  PCI_VENDOR_ID_NETRONOME, PCI_ANY_ID,
43 	  PCI_ANY_ID, 0, NFP_DEV_NFP3800_VF,
44 	},
45 	{ PCI_VENDOR_ID_NETRONOME, PCI_DEVICE_ID_NETRONOME_NFP6000_VF,
46 	  PCI_VENDOR_ID_NETRONOME, PCI_ANY_ID,
47 	  PCI_ANY_ID, 0, NFP_DEV_NFP6000_VF,
48 	},
49 	{ 0, } /* Required last entry. */
50 };
51 MODULE_DEVICE_TABLE(pci, nfp_netvf_pci_device_ids);
52 
53 static void nfp_netvf_get_mac_addr(struct nfp_net *nn)
54 {
55 	u8 mac_addr[ETH_ALEN];
56 
57 	put_unaligned_be32(nn_readl(nn, NFP_NET_CFG_MACADDR + 0), &mac_addr[0]);
58 	put_unaligned_be16(nn_readw(nn, NFP_NET_CFG_MACADDR + 6), &mac_addr[4]);
59 
60 	if (!is_valid_ether_addr(mac_addr)) {
61 		eth_hw_addr_random(nn->dp.netdev);
62 		return;
63 	}
64 
65 	eth_hw_addr_set(nn->dp.netdev, mac_addr);
66 	ether_addr_copy(nn->dp.netdev->perm_addr, mac_addr);
67 }
68 
69 static int nfp_netvf_pci_probe(struct pci_dev *pdev,
70 			       const struct pci_device_id *pci_id)
71 {
72 	const struct nfp_dev_info *dev_info;
73 	struct nfp_net_fw_version fw_ver;
74 	int max_tx_rings, max_rx_rings;
75 	u32 tx_bar_off, rx_bar_off;
76 	u32 tx_bar_sz, rx_bar_sz;
77 	int tx_bar_no, rx_bar_no;
78 	struct nfp_net_vf *vf;
79 	unsigned int num_irqs;
80 	u8 __iomem *ctrl_bar;
81 	struct nfp_net *nn;
82 	u32 startq;
83 	int stride;
84 	int err;
85 
86 	dev_info = &nfp_dev_info[pci_id->driver_data];
87 
88 	vf = kzalloc(sizeof(*vf), GFP_KERNEL);
89 	if (!vf)
90 		return -ENOMEM;
91 	pci_set_drvdata(pdev, vf);
92 
93 	err = pci_enable_device_mem(pdev);
94 	if (err)
95 		goto err_free_vf;
96 
97 	err = pci_request_regions(pdev, nfp_net_driver_name);
98 	if (err) {
99 		dev_err(&pdev->dev, "Unable to allocate device memory.\n");
100 		goto err_pci_disable;
101 	}
102 
103 	pci_set_master(pdev);
104 
105 	err = dma_set_mask_and_coherent(&pdev->dev, dev_info->dma_mask);
106 	if (err)
107 		goto err_pci_regions;
108 
109 	/* Map the Control BAR.
110 	 *
111 	 * Irrespective of the advertised BAR size we only map the
112 	 * first NFP_NET_CFG_BAR_SZ of the BAR.  This keeps the code
113 	 * the identical for PF and VF drivers.
114 	 */
115 	ctrl_bar = ioremap(pci_resource_start(pdev, NFP_NET_CTRL_BAR),
116 				   NFP_NET_CFG_BAR_SZ);
117 	if (!ctrl_bar) {
118 		dev_err(&pdev->dev,
119 			"Failed to map resource %d\n", NFP_NET_CTRL_BAR);
120 		err = -EIO;
121 		goto err_pci_regions;
122 	}
123 
124 	nfp_net_get_fw_version(&fw_ver, ctrl_bar);
125 	if (fw_ver.extend & NFP_NET_CFG_VERSION_RESERVED_MASK ||
126 	    fw_ver.class != NFP_NET_CFG_VERSION_CLASS_GENERIC) {
127 		dev_err(&pdev->dev, "Unknown Firmware ABI %d.%d.%d.%d\n",
128 			fw_ver.extend, fw_ver.class,
129 			fw_ver.major, fw_ver.minor);
130 		err = -EINVAL;
131 		goto err_ctrl_unmap;
132 	}
133 
134 	/* Determine stride */
135 	if (nfp_net_fw_ver_eq(&fw_ver, 0, 0, 0, 1)) {
136 		stride = 2;
137 		tx_bar_no = NFP_NET_Q0_BAR;
138 		rx_bar_no = NFP_NET_Q1_BAR;
139 		dev_warn(&pdev->dev, "OBSOLETE Firmware detected - VF isolation not available\n");
140 	} else {
141 		switch (fw_ver.major) {
142 		case 1 ... 5:
143 			stride = 4;
144 			tx_bar_no = NFP_NET_Q0_BAR;
145 			rx_bar_no = tx_bar_no;
146 			break;
147 		default:
148 			dev_err(&pdev->dev, "Unsupported Firmware ABI %d.%d.%d.%d\n",
149 				fw_ver.extend, fw_ver.class,
150 				fw_ver.major, fw_ver.minor);
151 			err = -EINVAL;
152 			goto err_ctrl_unmap;
153 		}
154 	}
155 
156 	/* Find out how many rings are supported */
157 	max_tx_rings = readl(ctrl_bar + NFP_NET_CFG_MAX_TXRINGS);
158 	max_rx_rings = readl(ctrl_bar + NFP_NET_CFG_MAX_RXRINGS);
159 
160 	tx_bar_sz = NFP_QCP_QUEUE_ADDR_SZ * max_tx_rings * stride;
161 	rx_bar_sz = NFP_QCP_QUEUE_ADDR_SZ * max_rx_rings * stride;
162 
163 	/* Sanity checks */
164 	if (tx_bar_sz > pci_resource_len(pdev, tx_bar_no)) {
165 		dev_err(&pdev->dev,
166 			"TX BAR too small for number of TX rings. Adjusting\n");
167 		tx_bar_sz = pci_resource_len(pdev, tx_bar_no);
168 		max_tx_rings = (tx_bar_sz / NFP_QCP_QUEUE_ADDR_SZ) / 2;
169 	}
170 	if (rx_bar_sz > pci_resource_len(pdev, rx_bar_no)) {
171 		dev_err(&pdev->dev,
172 			"RX BAR too small for number of RX rings. Adjusting\n");
173 		rx_bar_sz = pci_resource_len(pdev, rx_bar_no);
174 		max_rx_rings = (rx_bar_sz / NFP_QCP_QUEUE_ADDR_SZ) / 2;
175 	}
176 
177 	startq = readl(ctrl_bar + NFP_NET_CFG_START_TXQ);
178 	tx_bar_off = nfp_qcp_queue_offset(dev_info, startq);
179 	startq = readl(ctrl_bar + NFP_NET_CFG_START_RXQ);
180 	rx_bar_off = nfp_qcp_queue_offset(dev_info, startq);
181 
182 	/* Allocate and initialise the netdev */
183 	nn = nfp_net_alloc(pdev, dev_info, ctrl_bar, true,
184 			   max_tx_rings, max_rx_rings);
185 	if (IS_ERR(nn)) {
186 		err = PTR_ERR(nn);
187 		goto err_ctrl_unmap;
188 	}
189 	vf->nn = nn;
190 
191 	nn->dp.is_vf = 1;
192 	nn->stride_tx = stride;
193 	nn->stride_rx = stride;
194 
195 	if (rx_bar_no == tx_bar_no) {
196 		u32 bar_off, bar_sz;
197 		resource_size_t map_addr;
198 
199 		/* Make a single overlapping BAR mapping */
200 		if (tx_bar_off < rx_bar_off)
201 			bar_off = tx_bar_off;
202 		else
203 			bar_off = rx_bar_off;
204 
205 		if ((tx_bar_off + tx_bar_sz) > (rx_bar_off + rx_bar_sz))
206 			bar_sz = (tx_bar_off + tx_bar_sz) - bar_off;
207 		else
208 			bar_sz = (rx_bar_off + rx_bar_sz) - bar_off;
209 
210 		map_addr = pci_resource_start(pdev, tx_bar_no) + bar_off;
211 		vf->q_bar = ioremap(map_addr, bar_sz);
212 		if (!vf->q_bar) {
213 			nn_err(nn, "Failed to map resource %d\n", tx_bar_no);
214 			err = -EIO;
215 			goto err_netdev_free;
216 		}
217 
218 		/* TX queues */
219 		nn->tx_bar = vf->q_bar + (tx_bar_off - bar_off);
220 		/* RX queues */
221 		nn->rx_bar = vf->q_bar + (rx_bar_off - bar_off);
222 	} else {
223 		resource_size_t map_addr;
224 
225 		/* TX queues */
226 		map_addr = pci_resource_start(pdev, tx_bar_no) + tx_bar_off;
227 		nn->tx_bar = ioremap(map_addr, tx_bar_sz);
228 		if (!nn->tx_bar) {
229 			nn_err(nn, "Failed to map resource %d\n", tx_bar_no);
230 			err = -EIO;
231 			goto err_netdev_free;
232 		}
233 
234 		/* RX queues */
235 		map_addr = pci_resource_start(pdev, rx_bar_no) + rx_bar_off;
236 		nn->rx_bar = ioremap(map_addr, rx_bar_sz);
237 		if (!nn->rx_bar) {
238 			nn_err(nn, "Failed to map resource %d\n", rx_bar_no);
239 			err = -EIO;
240 			goto err_unmap_tx;
241 		}
242 	}
243 
244 	nfp_netvf_get_mac_addr(nn);
245 
246 	num_irqs = nfp_net_irqs_alloc(pdev, vf->irq_entries,
247 				      NFP_NET_MIN_VNIC_IRQS,
248 				      NFP_NET_NON_Q_VECTORS +
249 				      nn->dp.num_r_vecs);
250 	if (!num_irqs) {
251 		nn_warn(nn, "Unable to allocate MSI-X Vectors. Exiting\n");
252 		err = -EIO;
253 		goto err_unmap_rx;
254 	}
255 	nfp_net_irqs_assign(nn, vf->irq_entries, num_irqs);
256 
257 	err = nfp_net_init(nn);
258 	if (err)
259 		goto err_irqs_disable;
260 
261 	nfp_net_info(nn);
262 	vf->ddir = nfp_net_debugfs_device_add(pdev);
263 	nfp_net_debugfs_vnic_add(nn, vf->ddir);
264 
265 	return 0;
266 
267 err_irqs_disable:
268 	nfp_net_irqs_disable(pdev);
269 err_unmap_rx:
270 	if (!vf->q_bar)
271 		iounmap(nn->rx_bar);
272 err_unmap_tx:
273 	if (!vf->q_bar)
274 		iounmap(nn->tx_bar);
275 	else
276 		iounmap(vf->q_bar);
277 err_netdev_free:
278 	nfp_net_free(nn);
279 err_ctrl_unmap:
280 	iounmap(ctrl_bar);
281 err_pci_regions:
282 	pci_release_regions(pdev);
283 err_pci_disable:
284 	pci_disable_device(pdev);
285 err_free_vf:
286 	pci_set_drvdata(pdev, NULL);
287 	kfree(vf);
288 	return err;
289 }
290 
291 static void nfp_netvf_pci_remove(struct pci_dev *pdev)
292 {
293 	struct nfp_net_vf *vf;
294 	struct nfp_net *nn;
295 
296 	vf = pci_get_drvdata(pdev);
297 	if (!vf)
298 		return;
299 
300 	nn = vf->nn;
301 
302 	/* Note, the order is slightly different from above as we need
303 	 * to keep the nn pointer around till we have freed everything.
304 	 */
305 	nfp_net_debugfs_dir_clean(&nn->debugfs_dir);
306 	nfp_net_debugfs_dir_clean(&vf->ddir);
307 
308 	nfp_net_clean(nn);
309 
310 	nfp_net_irqs_disable(pdev);
311 
312 	if (!vf->q_bar) {
313 		iounmap(nn->rx_bar);
314 		iounmap(nn->tx_bar);
315 	} else {
316 		iounmap(vf->q_bar);
317 	}
318 	iounmap(nn->dp.ctrl_bar);
319 
320 	nfp_net_free(nn);
321 
322 	pci_release_regions(pdev);
323 	pci_disable_device(pdev);
324 
325 	pci_set_drvdata(pdev, NULL);
326 	kfree(vf);
327 }
328 
329 struct pci_driver nfp_netvf_pci_driver = {
330 	.name        = nfp_net_driver_name,
331 	.id_table    = nfp_netvf_pci_device_ids,
332 	.probe       = nfp_netvf_pci_probe,
333 	.remove      = nfp_netvf_pci_remove,
334 	.shutdown    = nfp_netvf_pci_remove,
335 };
336