xref: /linux/drivers/net/ethernet/netronome/nfp/nfp_netvf_main.c (revision e5c86679d5e864947a52fb31e45a425dea3e7fa9)
1 /*
2  * Copyright (C) 2015-2017 Netronome Systems, Inc.
3  *
4  * This software is dual licensed under the GNU General License Version 2,
5  * June 1991 as shown in the file COPYING in the top-level directory of this
6  * source tree or the BSD 2-Clause License provided below.  You have the
7  * option to license this software under the complete terms of either license.
8  *
9  * The BSD 2-Clause License:
10  *
11  *     Redistribution and use in source and binary forms, with or
12  *     without modification, are permitted provided that the following
13  *     conditions are met:
14  *
15  *      1. Redistributions of source code must retain the above
16  *         copyright notice, this list of conditions and the following
17  *         disclaimer.
18  *
19  *      2. Redistributions in binary form must reproduce the above
20  *         copyright notice, this list of conditions and the following
21  *         disclaimer in the documentation and/or other materials
22  *         provided with the distribution.
23  *
24  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
28  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
29  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
30  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31  * SOFTWARE.
32  */
33 
34 /*
35  * nfp_netvf_main.c
36  * Netronome virtual function network device driver: Main entry point
37  * Author: Jason McMullan <jason.mcmullan@netronome.com>
38  *         Rolf Neugebauer <rolf.neugebauer@netronome.com>
39  */
40 
41 #include <linux/module.h>
42 #include <linux/kernel.h>
43 #include <linux/init.h>
44 #include <linux/etherdevice.h>
45 
46 #include "nfp_net_ctrl.h"
47 #include "nfp_net.h"
48 #include "nfp_main.h"
49 
50 /**
51  * struct nfp_net_vf - NFP VF-specific device structure
52  * @nn:		NFP Net structure for this device
53  * @irq_entries: Pre-allocated array of MSI-X entries
54  * @q_bar:	Pointer to mapped QC memory (NULL if TX/RX mapped directly)
55  * @ddir:	Per-device debugfs directory
56  */
57 struct nfp_net_vf {
58 	struct nfp_net *nn;
59 
60 	struct msix_entry irq_entries[NFP_NET_NON_Q_VECTORS +
61 				      NFP_NET_MAX_TX_RINGS];
62 	u8 __iomem *q_bar;
63 
64 	struct dentry *ddir;
65 };
66 
67 static const char nfp_net_driver_name[] = "nfp_netvf";
68 
69 #define PCI_DEVICE_NFP6000VF		0x6003
70 static const struct pci_device_id nfp_netvf_pci_device_ids[] = {
71 	{ PCI_VENDOR_ID_NETRONOME, PCI_DEVICE_NFP6000VF,
72 	  PCI_VENDOR_ID_NETRONOME, PCI_ANY_ID,
73 	  PCI_ANY_ID, 0,
74 	},
75 	{ 0, } /* Required last entry. */
76 };
77 MODULE_DEVICE_TABLE(pci, nfp_netvf_pci_device_ids);
78 
79 static void nfp_netvf_get_mac_addr(struct nfp_net *nn)
80 {
81 	u8 mac_addr[ETH_ALEN];
82 
83 	put_unaligned_be32(nn_readl(nn, NFP_NET_CFG_MACADDR + 0), &mac_addr[0]);
84 	put_unaligned_be16(nn_readw(nn, NFP_NET_CFG_MACADDR + 6), &mac_addr[4]);
85 
86 	if (!is_valid_ether_addr(mac_addr)) {
87 		eth_hw_addr_random(nn->netdev);
88 		return;
89 	}
90 
91 	ether_addr_copy(nn->netdev->dev_addr, mac_addr);
92 	ether_addr_copy(nn->netdev->perm_addr, mac_addr);
93 }
94 
95 static int nfp_netvf_pci_probe(struct pci_dev *pdev,
96 			       const struct pci_device_id *pci_id)
97 {
98 	struct nfp_net_fw_version fw_ver;
99 	int max_tx_rings, max_rx_rings;
100 	u32 tx_bar_off, rx_bar_off;
101 	u32 tx_bar_sz, rx_bar_sz;
102 	int tx_bar_no, rx_bar_no;
103 	struct nfp_net_vf *vf;
104 	unsigned int num_irqs;
105 	u8 __iomem *ctrl_bar;
106 	struct nfp_net *nn;
107 	u32 startq;
108 	int stride;
109 	int err;
110 
111 	vf = kzalloc(sizeof(*vf), GFP_KERNEL);
112 	if (!vf)
113 		return -ENOMEM;
114 	pci_set_drvdata(pdev, vf);
115 
116 	err = pci_enable_device_mem(pdev);
117 	if (err)
118 		goto err_free_vf;
119 
120 	err = pci_request_regions(pdev, nfp_net_driver_name);
121 	if (err) {
122 		dev_err(&pdev->dev, "Unable to allocate device memory.\n");
123 		goto err_pci_disable;
124 	}
125 
126 	pci_set_master(pdev);
127 
128 	err = dma_set_mask_and_coherent(&pdev->dev,
129 					DMA_BIT_MASK(NFP_NET_MAX_DMA_BITS));
130 	if (err)
131 		goto err_pci_regions;
132 
133 	/* Map the Control BAR.
134 	 *
135 	 * Irrespective of the advertised BAR size we only map the
136 	 * first NFP_NET_CFG_BAR_SZ of the BAR.  This keeps the code
137 	 * the identical for PF and VF drivers.
138 	 */
139 	ctrl_bar = ioremap_nocache(pci_resource_start(pdev, NFP_NET_CTRL_BAR),
140 				   NFP_NET_CFG_BAR_SZ);
141 	if (!ctrl_bar) {
142 		dev_err(&pdev->dev,
143 			"Failed to map resource %d\n", NFP_NET_CTRL_BAR);
144 		err = -EIO;
145 		goto err_pci_regions;
146 	}
147 
148 	nfp_net_get_fw_version(&fw_ver, ctrl_bar);
149 	if (fw_ver.resv || fw_ver.class != NFP_NET_CFG_VERSION_CLASS_GENERIC) {
150 		dev_err(&pdev->dev, "Unknown Firmware ABI %d.%d.%d.%d\n",
151 			fw_ver.resv, fw_ver.class, fw_ver.major, fw_ver.minor);
152 		err = -EINVAL;
153 		goto err_ctrl_unmap;
154 	}
155 
156 	/* Determine stride */
157 	if (nfp_net_fw_ver_eq(&fw_ver, 0, 0, 0, 1)) {
158 		stride = 2;
159 		tx_bar_no = NFP_NET_Q0_BAR;
160 		rx_bar_no = NFP_NET_Q1_BAR;
161 		dev_warn(&pdev->dev, "OBSOLETE Firmware detected - VF isolation not available\n");
162 	} else {
163 		switch (fw_ver.major) {
164 		case 1 ... 4:
165 			stride = 4;
166 			tx_bar_no = NFP_NET_Q0_BAR;
167 			rx_bar_no = tx_bar_no;
168 			break;
169 		default:
170 			dev_err(&pdev->dev, "Unsupported Firmware ABI %d.%d.%d.%d\n",
171 				fw_ver.resv, fw_ver.class,
172 				fw_ver.major, fw_ver.minor);
173 			err = -EINVAL;
174 			goto err_ctrl_unmap;
175 		}
176 	}
177 
178 	/* Find out how many rings are supported */
179 	max_tx_rings = readl(ctrl_bar + NFP_NET_CFG_MAX_TXRINGS);
180 	max_rx_rings = readl(ctrl_bar + NFP_NET_CFG_MAX_RXRINGS);
181 
182 	tx_bar_sz = NFP_QCP_QUEUE_ADDR_SZ * max_tx_rings * stride;
183 	rx_bar_sz = NFP_QCP_QUEUE_ADDR_SZ * max_rx_rings * stride;
184 
185 	/* Sanity checks */
186 	if (tx_bar_sz > pci_resource_len(pdev, tx_bar_no)) {
187 		dev_err(&pdev->dev,
188 			"TX BAR too small for number of TX rings. Adjusting\n");
189 		tx_bar_sz = pci_resource_len(pdev, tx_bar_no);
190 		max_tx_rings = (tx_bar_sz / NFP_QCP_QUEUE_ADDR_SZ) / 2;
191 	}
192 	if (rx_bar_sz > pci_resource_len(pdev, rx_bar_no)) {
193 		dev_err(&pdev->dev,
194 			"RX BAR too small for number of RX rings. Adjusting\n");
195 		rx_bar_sz = pci_resource_len(pdev, rx_bar_no);
196 		max_rx_rings = (rx_bar_sz / NFP_QCP_QUEUE_ADDR_SZ) / 2;
197 	}
198 
199 	startq = readl(ctrl_bar + NFP_NET_CFG_START_TXQ);
200 	tx_bar_off = NFP_PCIE_QUEUE(startq);
201 	startq = readl(ctrl_bar + NFP_NET_CFG_START_RXQ);
202 	rx_bar_off = NFP_PCIE_QUEUE(startq);
203 
204 	/* Allocate and initialise the netdev */
205 	nn = nfp_net_netdev_alloc(pdev, max_tx_rings, max_rx_rings);
206 	if (IS_ERR(nn)) {
207 		err = PTR_ERR(nn);
208 		goto err_ctrl_unmap;
209 	}
210 	vf->nn = nn;
211 
212 	nn->fw_ver = fw_ver;
213 	nn->ctrl_bar = ctrl_bar;
214 	nn->is_vf = 1;
215 	nn->stride_tx = stride;
216 	nn->stride_rx = stride;
217 
218 	if (rx_bar_no == tx_bar_no) {
219 		u32 bar_off, bar_sz;
220 		resource_size_t map_addr;
221 
222 		/* Make a single overlapping BAR mapping */
223 		if (tx_bar_off < rx_bar_off)
224 			bar_off = tx_bar_off;
225 		else
226 			bar_off = rx_bar_off;
227 
228 		if ((tx_bar_off + tx_bar_sz) > (rx_bar_off + rx_bar_sz))
229 			bar_sz = (tx_bar_off + tx_bar_sz) - bar_off;
230 		else
231 			bar_sz = (rx_bar_off + rx_bar_sz) - bar_off;
232 
233 		map_addr = pci_resource_start(pdev, tx_bar_no) + bar_off;
234 		vf->q_bar = ioremap_nocache(map_addr, bar_sz);
235 		if (!vf->q_bar) {
236 			nn_err(nn, "Failed to map resource %d\n", tx_bar_no);
237 			err = -EIO;
238 			goto err_netdev_free;
239 		}
240 
241 		/* TX queues */
242 		nn->tx_bar = vf->q_bar + (tx_bar_off - bar_off);
243 		/* RX queues */
244 		nn->rx_bar = vf->q_bar + (rx_bar_off - bar_off);
245 	} else {
246 		resource_size_t map_addr;
247 
248 		/* TX queues */
249 		map_addr = pci_resource_start(pdev, tx_bar_no) + tx_bar_off;
250 		nn->tx_bar = ioremap_nocache(map_addr, tx_bar_sz);
251 		if (!nn->tx_bar) {
252 			nn_err(nn, "Failed to map resource %d\n", tx_bar_no);
253 			err = -EIO;
254 			goto err_netdev_free;
255 		}
256 
257 		/* RX queues */
258 		map_addr = pci_resource_start(pdev, rx_bar_no) + rx_bar_off;
259 		nn->rx_bar = ioremap_nocache(map_addr, rx_bar_sz);
260 		if (!nn->rx_bar) {
261 			nn_err(nn, "Failed to map resource %d\n", rx_bar_no);
262 			err = -EIO;
263 			goto err_unmap_tx;
264 		}
265 	}
266 
267 	nfp_netvf_get_mac_addr(nn);
268 
269 	num_irqs = nfp_net_irqs_alloc(pdev, vf->irq_entries,
270 				      NFP_NET_MIN_PORT_IRQS,
271 				      NFP_NET_NON_Q_VECTORS + nn->num_r_vecs);
272 	if (!num_irqs) {
273 		nn_warn(nn, "Unable to allocate MSI-X Vectors. Exiting\n");
274 		err = -EIO;
275 		goto err_unmap_rx;
276 	}
277 	nfp_net_irqs_assign(nn, vf->irq_entries, num_irqs);
278 
279 	/* Get ME clock frequency from ctrl BAR
280 	 * XXX for now frequency is hardcoded until we figure out how
281 	 * to get the value from nfp-hwinfo into ctrl bar
282 	 */
283 	nn->me_freq_mhz = 1200;
284 
285 	err = nfp_net_netdev_init(nn->netdev);
286 	if (err)
287 		goto err_irqs_disable;
288 
289 	nfp_net_info(nn);
290 	vf->ddir = nfp_net_debugfs_device_add(pdev);
291 	nfp_net_debugfs_port_add(nn, vf->ddir, 0);
292 
293 	return 0;
294 
295 err_irqs_disable:
296 	nfp_net_irqs_disable(pdev);
297 err_unmap_rx:
298 	if (!vf->q_bar)
299 		iounmap(nn->rx_bar);
300 err_unmap_tx:
301 	if (!vf->q_bar)
302 		iounmap(nn->tx_bar);
303 	else
304 		iounmap(vf->q_bar);
305 err_netdev_free:
306 	nfp_net_netdev_free(nn);
307 err_ctrl_unmap:
308 	iounmap(ctrl_bar);
309 err_pci_regions:
310 	pci_release_regions(pdev);
311 err_pci_disable:
312 	pci_disable_device(pdev);
313 err_free_vf:
314 	pci_set_drvdata(pdev, NULL);
315 	kfree(vf);
316 	return err;
317 }
318 
319 static void nfp_netvf_pci_remove(struct pci_dev *pdev)
320 {
321 	struct nfp_net_vf *vf = pci_get_drvdata(pdev);
322 	struct nfp_net *nn = vf->nn;
323 
324 	/* Note, the order is slightly different from above as we need
325 	 * to keep the nn pointer around till we have freed everything.
326 	 */
327 	nfp_net_debugfs_dir_clean(&nn->debugfs_dir);
328 	nfp_net_debugfs_dir_clean(&vf->ddir);
329 
330 	nfp_net_netdev_clean(nn->netdev);
331 
332 	nfp_net_irqs_disable(pdev);
333 
334 	if (!vf->q_bar) {
335 		iounmap(nn->rx_bar);
336 		iounmap(nn->tx_bar);
337 	} else {
338 		iounmap(vf->q_bar);
339 	}
340 	iounmap(nn->ctrl_bar);
341 
342 	nfp_net_netdev_free(nn);
343 
344 	pci_release_regions(pdev);
345 	pci_disable_device(pdev);
346 
347 	pci_set_drvdata(pdev, NULL);
348 	kfree(vf);
349 }
350 
351 struct pci_driver nfp_netvf_pci_driver = {
352 	.name        = nfp_net_driver_name,
353 	.id_table    = nfp_netvf_pci_device_ids,
354 	.probe       = nfp_netvf_pci_probe,
355 	.remove      = nfp_netvf_pci_remove,
356 };
357