2 * Copyright (C) 2015-2017 Netronome Systems, Inc.
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.
9 * The BSD 2-Clause License:
11 * Redistribution and use in source and binary forms, with or
12 * without modification, are permitted provided that the following
15 * 1. Redistributions of source code must retain the above
16 * copyright notice, this list of conditions and the following
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.
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
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>
41 #include <linux/module.h>
42 #include <linux/kernel.h>
43 #include <linux/init.h>
44 #include <linux/etherdevice.h>
46 #include "nfp_net_ctrl.h"
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
60 struct msix_entry irq_entries
[NFP_NET_NON_Q_VECTORS
+
61 NFP_NET_MAX_TX_RINGS
];
67 static const char nfp_net_driver_name
[] = "nfp_netvf";
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
,
75 { 0, } /* Required last entry. */
77 MODULE_DEVICE_TABLE(pci
, nfp_netvf_pci_device_ids
);
79 static void nfp_netvf_get_mac_addr(struct nfp_net
*nn
)
81 u8 mac_addr
[ETH_ALEN
];
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]);
86 if (!is_valid_ether_addr(mac_addr
)) {
87 eth_hw_addr_random(nn
->dp
.netdev
);
91 ether_addr_copy(nn
->dp
.netdev
->dev_addr
, mac_addr
);
92 ether_addr_copy(nn
->dp
.netdev
->perm_addr
, mac_addr
);
95 static int nfp_netvf_pci_probe(struct pci_dev
*pdev
,
96 const struct pci_device_id
*pci_id
)
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
;
111 vf
= kzalloc(sizeof(*vf
), GFP_KERNEL
);
114 pci_set_drvdata(pdev
, vf
);
116 err
= pci_enable_device_mem(pdev
);
120 err
= pci_request_regions(pdev
, nfp_net_driver_name
);
122 dev_err(&pdev
->dev
, "Unable to allocate device memory.\n");
123 goto err_pci_disable
;
126 pci_set_master(pdev
);
128 err
= dma_set_mask_and_coherent(&pdev
->dev
,
129 DMA_BIT_MASK(NFP_NET_MAX_DMA_BITS
));
131 goto err_pci_regions
;
133 /* Map the Control BAR.
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.
139 ctrl_bar
= ioremap_nocache(pci_resource_start(pdev
, NFP_NET_CTRL_BAR
),
143 "Failed to map resource %d\n", NFP_NET_CTRL_BAR
);
145 goto err_pci_regions
;
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
);
156 /* Determine stride */
157 if (nfp_net_fw_ver_eq(&fw_ver
, 0, 0, 0, 1)) {
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");
163 switch (fw_ver
.major
) {
166 tx_bar_no
= NFP_NET_Q0_BAR
;
167 rx_bar_no
= tx_bar_no
;
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
);
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
);
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
;
186 if (tx_bar_sz
> pci_resource_len(pdev
, tx_bar_no
)) {
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;
192 if (rx_bar_sz
> pci_resource_len(pdev
, rx_bar_no
)) {
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;
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
);
204 /* Allocate and initialise the netdev */
205 nn
= nfp_net_alloc(pdev
, true, max_tx_rings
, max_rx_rings
);
213 nn
->dp
.ctrl_bar
= ctrl_bar
;
215 nn
->stride_tx
= stride
;
216 nn
->stride_rx
= stride
;
218 if (rx_bar_no
== tx_bar_no
) {
220 resource_size_t map_addr
;
222 /* Make a single overlapping BAR mapping */
223 if (tx_bar_off
< rx_bar_off
)
224 bar_off
= tx_bar_off
;
226 bar_off
= rx_bar_off
;
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
;
231 bar_sz
= (rx_bar_off
+ rx_bar_sz
) - bar_off
;
233 map_addr
= pci_resource_start(pdev
, tx_bar_no
) + bar_off
;
234 vf
->q_bar
= ioremap_nocache(map_addr
, bar_sz
);
236 nn_err(nn
, "Failed to map resource %d\n", tx_bar_no
);
238 goto err_netdev_free
;
242 nn
->tx_bar
= vf
->q_bar
+ (tx_bar_off
- bar_off
);
244 nn
->rx_bar
= vf
->q_bar
+ (rx_bar_off
- bar_off
);
246 resource_size_t map_addr
;
249 map_addr
= pci_resource_start(pdev
, tx_bar_no
) + tx_bar_off
;
250 nn
->tx_bar
= ioremap_nocache(map_addr
, tx_bar_sz
);
252 nn_err(nn
, "Failed to map resource %d\n", tx_bar_no
);
254 goto err_netdev_free
;
258 map_addr
= pci_resource_start(pdev
, rx_bar_no
) + rx_bar_off
;
259 nn
->rx_bar
= ioremap_nocache(map_addr
, rx_bar_sz
);
261 nn_err(nn
, "Failed to map resource %d\n", rx_bar_no
);
267 nfp_netvf_get_mac_addr(nn
);
269 num_irqs
= nfp_net_irqs_alloc(pdev
, vf
->irq_entries
,
270 NFP_NET_MIN_VNIC_IRQS
,
271 NFP_NET_NON_Q_VECTORS
+
274 nn_warn(nn
, "Unable to allocate MSI-X Vectors. Exiting\n");
278 nfp_net_irqs_assign(nn
, vf
->irq_entries
, num_irqs
);
280 err
= nfp_net_init(nn
);
282 goto err_irqs_disable
;
285 vf
->ddir
= nfp_net_debugfs_device_add(pdev
);
286 nfp_net_debugfs_vnic_add(nn
, vf
->ddir
, 0);
291 nfp_net_irqs_disable(pdev
);
305 pci_release_regions(pdev
);
307 pci_disable_device(pdev
);
309 pci_set_drvdata(pdev
, NULL
);
314 static void nfp_netvf_pci_remove(struct pci_dev
*pdev
)
316 struct nfp_net_vf
*vf
= pci_get_drvdata(pdev
);
317 struct nfp_net
*nn
= vf
->nn
;
319 /* Note, the order is slightly different from above as we need
320 * to keep the nn pointer around till we have freed everything.
322 nfp_net_debugfs_dir_clean(&nn
->debugfs_dir
);
323 nfp_net_debugfs_dir_clean(&vf
->ddir
);
327 nfp_net_irqs_disable(pdev
);
335 iounmap(nn
->dp
.ctrl_bar
);
339 pci_release_regions(pdev
);
340 pci_disable_device(pdev
);
342 pci_set_drvdata(pdev
, NULL
);
346 struct pci_driver nfp_netvf_pci_driver
= {
347 .name
= nfp_net_driver_name
,
348 .id_table
= nfp_netvf_pci_device_ids
,
349 .probe
= nfp_netvf_pci_probe
,
350 .remove
= nfp_netvf_pci_remove
,