1 /* 2 * Copyright (C) 2015 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 49 const char nfp_net_driver_name[] = "nfp_netvf"; 50 const char nfp_net_driver_version[] = "0.1"; 51 #define PCI_DEVICE_NFP6000VF 0x6003 52 static const struct pci_device_id nfp_netvf_pci_device_ids[] = { 53 { PCI_VENDOR_ID_NETRONOME, PCI_DEVICE_NFP6000VF, 54 PCI_VENDOR_ID_NETRONOME, PCI_ANY_ID, 55 PCI_ANY_ID, 0, 56 }, 57 { 0, } /* Required last entry. */ 58 }; 59 MODULE_DEVICE_TABLE(pci, nfp_netvf_pci_device_ids); 60 61 static void nfp_netvf_get_mac_addr(struct nfp_net *nn) 62 { 63 u8 mac_addr[ETH_ALEN]; 64 65 put_unaligned_be32(nn_readl(nn, NFP_NET_CFG_MACADDR + 0), &mac_addr[0]); 66 put_unaligned_be16(nn_readw(nn, NFP_NET_CFG_MACADDR + 6), &mac_addr[4]); 67 68 if (!is_valid_ether_addr(mac_addr)) { 69 eth_hw_addr_random(nn->netdev); 70 return; 71 } 72 73 ether_addr_copy(nn->netdev->dev_addr, mac_addr); 74 ether_addr_copy(nn->netdev->perm_addr, mac_addr); 75 } 76 77 static int nfp_netvf_pci_probe(struct pci_dev *pdev, 78 const struct pci_device_id *pci_id) 79 { 80 struct nfp_net_fw_version fw_ver; 81 int max_tx_rings, max_rx_rings; 82 u32 tx_bar_off, rx_bar_off; 83 u32 tx_bar_sz, rx_bar_sz; 84 int tx_bar_no, rx_bar_no; 85 u8 __iomem *ctrl_bar; 86 struct nfp_net *nn; 87 u32 startq; 88 int stride; 89 int err; 90 91 err = pci_enable_device_mem(pdev); 92 if (err) 93 return err; 94 95 err = pci_request_regions(pdev, nfp_net_driver_name); 96 if (err) { 97 dev_err(&pdev->dev, "Unable to allocate device memory.\n"); 98 goto err_pci_disable; 99 } 100 101 pci_set_master(pdev); 102 103 err = dma_set_mask_and_coherent(&pdev->dev, 104 DMA_BIT_MASK(NFP_NET_MAX_DMA_BITS)); 105 if (err) 106 goto err_pci_regions; 107 108 /* Map the Control BAR. 109 * 110 * Irrespective of the advertised BAR size we only map the 111 * first NFP_NET_CFG_BAR_SZ of the BAR. This keeps the code 112 * the identical for PF and VF drivers. 113 */ 114 ctrl_bar = ioremap_nocache(pci_resource_start(pdev, NFP_NET_CTRL_BAR), 115 NFP_NET_CFG_BAR_SZ); 116 if (!ctrl_bar) { 117 dev_err(&pdev->dev, 118 "Failed to map resource %d\n", NFP_NET_CTRL_BAR); 119 err = -EIO; 120 goto err_pci_regions; 121 } 122 123 nfp_net_get_fw_version(&fw_ver, ctrl_bar); 124 if (fw_ver.resv || fw_ver.class != NFP_NET_CFG_VERSION_CLASS_GENERIC) { 125 dev_err(&pdev->dev, "Unknown Firmware ABI %d.%d.%d.%d\n", 126 fw_ver.resv, fw_ver.class, fw_ver.major, fw_ver.minor); 127 err = -EINVAL; 128 goto err_ctrl_unmap; 129 } 130 131 /* Determine stride */ 132 if (nfp_net_fw_ver_eq(&fw_ver, 0, 0, 0, 1)) { 133 stride = 2; 134 tx_bar_no = NFP_NET_Q0_BAR; 135 rx_bar_no = NFP_NET_Q1_BAR; 136 dev_warn(&pdev->dev, "OBSOLETE Firmware detected - VF isolation not available\n"); 137 } else { 138 switch (fw_ver.major) { 139 case 1 ... 4: 140 stride = 4; 141 tx_bar_no = NFP_NET_Q0_BAR; 142 rx_bar_no = tx_bar_no; 143 break; 144 default: 145 dev_err(&pdev->dev, "Unsupported Firmware ABI %d.%d.%d.%d\n", 146 fw_ver.resv, fw_ver.class, 147 fw_ver.major, fw_ver.minor); 148 err = -EINVAL; 149 goto err_ctrl_unmap; 150 } 151 } 152 153 /* Find out how many rings are supported */ 154 max_tx_rings = readl(ctrl_bar + NFP_NET_CFG_MAX_TXRINGS); 155 max_rx_rings = readl(ctrl_bar + NFP_NET_CFG_MAX_RXRINGS); 156 157 tx_bar_sz = NFP_QCP_QUEUE_ADDR_SZ * max_tx_rings * stride; 158 rx_bar_sz = NFP_QCP_QUEUE_ADDR_SZ * max_rx_rings * stride; 159 160 /* Sanity checks */ 161 if (tx_bar_sz > pci_resource_len(pdev, tx_bar_no)) { 162 dev_err(&pdev->dev, 163 "TX BAR too small for number of TX rings. Adjusting\n"); 164 tx_bar_sz = pci_resource_len(pdev, tx_bar_no); 165 max_tx_rings = (tx_bar_sz / NFP_QCP_QUEUE_ADDR_SZ) / 2; 166 } 167 if (rx_bar_sz > pci_resource_len(pdev, rx_bar_no)) { 168 dev_err(&pdev->dev, 169 "RX BAR too small for number of RX rings. Adjusting\n"); 170 rx_bar_sz = pci_resource_len(pdev, rx_bar_no); 171 max_rx_rings = (rx_bar_sz / NFP_QCP_QUEUE_ADDR_SZ) / 2; 172 } 173 174 startq = readl(ctrl_bar + NFP_NET_CFG_START_TXQ); 175 tx_bar_off = NFP_PCIE_QUEUE(startq); 176 startq = readl(ctrl_bar + NFP_NET_CFG_START_RXQ); 177 rx_bar_off = NFP_PCIE_QUEUE(startq); 178 179 /* Allocate and initialise the netdev */ 180 nn = nfp_net_netdev_alloc(pdev, max_tx_rings, max_rx_rings); 181 if (IS_ERR(nn)) { 182 err = PTR_ERR(nn); 183 goto err_ctrl_unmap; 184 } 185 186 nn->fw_ver = fw_ver; 187 nn->ctrl_bar = ctrl_bar; 188 nn->is_vf = 1; 189 nn->stride_tx = stride; 190 nn->stride_rx = stride; 191 192 if (rx_bar_no == tx_bar_no) { 193 u32 bar_off, bar_sz; 194 resource_size_t map_addr; 195 196 /* Make a single overlapping BAR mapping */ 197 if (tx_bar_off < rx_bar_off) 198 bar_off = tx_bar_off; 199 else 200 bar_off = rx_bar_off; 201 202 if ((tx_bar_off + tx_bar_sz) > (rx_bar_off + rx_bar_sz)) 203 bar_sz = (tx_bar_off + tx_bar_sz) - bar_off; 204 else 205 bar_sz = (rx_bar_off + rx_bar_sz) - bar_off; 206 207 map_addr = pci_resource_start(pdev, tx_bar_no) + bar_off; 208 nn->q_bar = ioremap_nocache(map_addr, bar_sz); 209 if (!nn->q_bar) { 210 nn_err(nn, "Failed to map resource %d\n", tx_bar_no); 211 err = -EIO; 212 goto err_netdev_free; 213 } 214 215 /* TX queues */ 216 nn->tx_bar = nn->q_bar + (tx_bar_off - bar_off); 217 /* RX queues */ 218 nn->rx_bar = nn->q_bar + (rx_bar_off - bar_off); 219 } else { 220 resource_size_t map_addr; 221 222 /* TX queues */ 223 map_addr = pci_resource_start(pdev, tx_bar_no) + tx_bar_off; 224 nn->tx_bar = ioremap_nocache(map_addr, tx_bar_sz); 225 if (!nn->tx_bar) { 226 nn_err(nn, "Failed to map resource %d\n", tx_bar_no); 227 err = -EIO; 228 goto err_netdev_free; 229 } 230 231 /* RX queues */ 232 map_addr = pci_resource_start(pdev, rx_bar_no) + rx_bar_off; 233 nn->rx_bar = ioremap_nocache(map_addr, rx_bar_sz); 234 if (!nn->rx_bar) { 235 nn_err(nn, "Failed to map resource %d\n", rx_bar_no); 236 err = -EIO; 237 goto err_unmap_tx; 238 } 239 } 240 241 nfp_netvf_get_mac_addr(nn); 242 243 err = nfp_net_irqs_alloc(nn); 244 if (!err) { 245 nn_warn(nn, "Unable to allocate MSI-X Vectors. Exiting\n"); 246 err = -EIO; 247 goto err_unmap_rx; 248 } 249 250 /* Get ME clock frequency from ctrl BAR 251 * XXX for now frequency is hardcoded until we figure out how 252 * to get the value from nfp-hwinfo into ctrl bar 253 */ 254 nn->me_freq_mhz = 1200; 255 256 err = nfp_net_netdev_init(nn->netdev); 257 if (err) 258 goto err_irqs_disable; 259 260 pci_set_drvdata(pdev, nn); 261 262 nfp_net_info(nn); 263 nfp_net_debugfs_adapter_add(nn); 264 265 return 0; 266 267 err_irqs_disable: 268 nfp_net_irqs_disable(nn); 269 err_unmap_rx: 270 if (!nn->q_bar) 271 iounmap(nn->rx_bar); 272 err_unmap_tx: 273 if (!nn->q_bar) 274 iounmap(nn->tx_bar); 275 else 276 iounmap(nn->q_bar); 277 err_netdev_free: 278 pci_set_drvdata(pdev, NULL); 279 nfp_net_netdev_free(nn); 280 err_ctrl_unmap: 281 iounmap(ctrl_bar); 282 err_pci_regions: 283 pci_release_regions(pdev); 284 err_pci_disable: 285 pci_disable_device(pdev); 286 return err; 287 } 288 289 static void nfp_netvf_pci_remove(struct pci_dev *pdev) 290 { 291 struct nfp_net *nn = pci_get_drvdata(pdev); 292 293 /* Note, the order is slightly different from above as we need 294 * to keep the nn pointer around till we have freed everything. 295 */ 296 nfp_net_debugfs_adapter_del(nn); 297 298 nfp_net_netdev_clean(nn->netdev); 299 300 nfp_net_irqs_disable(nn); 301 302 if (!nn->q_bar) { 303 iounmap(nn->rx_bar); 304 iounmap(nn->tx_bar); 305 } else { 306 iounmap(nn->q_bar); 307 } 308 iounmap(nn->ctrl_bar); 309 310 pci_set_drvdata(pdev, NULL); 311 312 nfp_net_netdev_free(nn); 313 314 pci_release_regions(pdev); 315 pci_disable_device(pdev); 316 } 317 318 static struct pci_driver nfp_netvf_pci_driver = { 319 .name = nfp_net_driver_name, 320 .id_table = nfp_netvf_pci_device_ids, 321 .probe = nfp_netvf_pci_probe, 322 .remove = nfp_netvf_pci_remove, 323 }; 324 325 static int __init nfp_netvf_init(void) 326 { 327 int err; 328 329 pr_info("%s: NFP VF Network driver, Copyright (C) 2014-2015 Netronome Systems\n", 330 nfp_net_driver_name); 331 332 nfp_net_debugfs_create(); 333 err = pci_register_driver(&nfp_netvf_pci_driver); 334 if (err) { 335 nfp_net_debugfs_destroy(); 336 return err; 337 } 338 339 return 0; 340 } 341 342 static void __exit nfp_netvf_exit(void) 343 { 344 pci_unregister_driver(&nfp_netvf_pci_driver); 345 nfp_net_debugfs_destroy(); 346 } 347 348 module_init(nfp_netvf_init); 349 module_exit(nfp_netvf_exit); 350 351 MODULE_AUTHOR("Netronome Systems <oss-drivers@netronome.com>"); 352 MODULE_LICENSE("GPL"); 353 MODULE_DESCRIPTION("NFP VF network device driver"); 354