1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright(c) 2017 - 2019 Pensando Systems, Inc */
4 #include <linux/module.h>
5 #include <linux/netdevice.h>
6 #include <linux/etherdevice.h>
10 #include "ionic_bus.h"
11 #include "ionic_lif.h"
12 #include "ionic_debugfs.h"
14 /* Supported devices */
15 static const struct pci_device_id ionic_id_table[] = {
16 { PCI_VDEVICE(PENSANDO, PCI_DEVICE_ID_PENSANDO_IONIC_ETH_PF) },
17 { PCI_VDEVICE(PENSANDO, PCI_DEVICE_ID_PENSANDO_IONIC_ETH_VF) },
18 { 0, } /* end of table */
20 MODULE_DEVICE_TABLE(pci, ionic_id_table);
22 int ionic_bus_get_irq(struct ionic *ionic, unsigned int num)
24 return pci_irq_vector(ionic->pdev, num);
27 const char *ionic_bus_info(struct ionic *ionic)
29 return pci_name(ionic->pdev);
32 int ionic_bus_alloc_irq_vectors(struct ionic *ionic, unsigned int nintrs)
34 return pci_alloc_irq_vectors(ionic->pdev, nintrs, nintrs,
38 void ionic_bus_free_irq_vectors(struct ionic *ionic)
43 pci_free_irq_vectors(ionic->pdev);
46 static int ionic_map_bars(struct ionic *ionic)
48 struct pci_dev *pdev = ionic->pdev;
49 struct device *dev = ionic->dev;
50 struct ionic_dev_bar *bars;
56 for (i = 0, j = 0; i < IONIC_BARS_MAX; i++) {
57 if (!(pci_resource_flags(pdev, i) & IORESOURCE_MEM))
59 bars[j].len = pci_resource_len(pdev, i);
61 /* only map the whole bar 0 */
65 bars[j].vaddr = pci_iomap(pdev, i, bars[j].len);
68 "Cannot memory-map BAR %d, aborting\n",
74 bars[j].bus_addr = pci_resource_start(pdev, i);
75 bars[j].res_index = i;
83 static void ionic_unmap_bars(struct ionic *ionic)
85 struct ionic_dev_bar *bars = ionic->bars;
88 for (i = 0; i < IONIC_BARS_MAX; i++) {
90 iounmap(bars[i].vaddr);
98 void __iomem *ionic_bus_map_dbpage(struct ionic *ionic, int page_num)
100 return pci_iomap_range(ionic->pdev,
101 ionic->bars[IONIC_PCI_BAR_DBELL].res_index,
102 (u64)page_num << PAGE_SHIFT, PAGE_SIZE);
105 void ionic_bus_unmap_dbpage(struct ionic *ionic, void __iomem *page)
110 static void ionic_vf_dealloc_locked(struct ionic *ionic)
119 for (i = ionic->num_vfs - 1; i >= 0; i--) {
123 (void)ionic_set_vf_config(ionic, i,
124 IONIC_VF_ATTR_STATSADDR,
126 dma_unmap_single(ionic->dev, v->stats_pa,
127 sizeof(v->stats), DMA_FROM_DEVICE);
137 static void ionic_vf_dealloc(struct ionic *ionic)
139 down_write(&ionic->vf_op_lock);
140 ionic_vf_dealloc_locked(ionic);
141 up_write(&ionic->vf_op_lock);
144 static int ionic_vf_alloc(struct ionic *ionic, int num_vfs)
150 down_write(&ionic->vf_op_lock);
152 ionic->vfs = kcalloc(num_vfs, sizeof(struct ionic_vf), GFP_KERNEL);
158 for (i = 0; i < num_vfs; i++) {
160 v->stats_pa = dma_map_single(ionic->dev, &v->stats,
161 sizeof(v->stats), DMA_FROM_DEVICE);
162 if (dma_mapping_error(ionic->dev, v->stats_pa)) {
169 /* ignore failures from older FW, we just won't get stats */
170 (void)ionic_set_vf_config(ionic, i, IONIC_VF_ATTR_STATSADDR,
176 ionic_vf_dealloc_locked(ionic);
177 up_write(&ionic->vf_op_lock);
181 static int ionic_sriov_configure(struct pci_dev *pdev, int num_vfs)
183 struct ionic *ionic = pci_get_drvdata(pdev);
184 struct device *dev = ionic->dev;
188 test_bit(IONIC_LIF_F_FW_RESET, ionic->lif->state))
192 ret = pci_enable_sriov(pdev, num_vfs);
194 dev_err(dev, "Cannot enable SRIOV: %d\n", ret);
198 ret = ionic_vf_alloc(ionic, num_vfs);
200 dev_err(dev, "Cannot alloc VFs: %d\n", ret);
201 pci_disable_sriov(pdev);
207 pci_disable_sriov(pdev);
208 ionic_vf_dealloc(ionic);
215 static int ionic_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
217 struct device *dev = &pdev->dev;
222 ionic = ionic_devlink_alloc(dev);
228 pci_set_drvdata(pdev, ionic);
229 mutex_init(&ionic->dev_cmd_lock);
231 /* Query system for DMA addressing limitation for the device. */
232 err = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(IONIC_ADDR_LEN));
234 dev_err(dev, "Unable to obtain 64-bit DMA for consistent allocations, aborting. err=%d\n",
236 goto err_out_clear_drvdata;
239 ionic_debugfs_add_dev(ionic);
241 /* Setup PCI device */
242 err = pci_enable_device_mem(pdev);
244 dev_err(dev, "Cannot enable PCI device: %d, aborting\n", err);
245 goto err_out_debugfs_del_dev;
248 err = pci_request_regions(pdev, IONIC_DRV_NAME);
250 dev_err(dev, "Cannot request PCI regions: %d, aborting\n", err);
251 goto err_out_pci_disable_device;
254 pcie_print_link_status(pdev);
256 err = ionic_map_bars(ionic);
258 goto err_out_pci_disable_device;
260 /* Configure the device */
261 err = ionic_setup(ionic);
263 dev_err(dev, "Cannot setup device: %d, aborting\n", err);
264 goto err_out_unmap_bars;
266 pci_set_master(pdev);
268 err = ionic_identify(ionic);
270 dev_err(dev, "Cannot identify device: %d, aborting\n", err);
271 goto err_out_teardown;
273 ionic_debugfs_add_ident(ionic);
275 err = ionic_init(ionic);
277 dev_err(dev, "Cannot init device: %d, aborting\n", err);
278 goto err_out_teardown;
281 /* Configure the ports */
282 err = ionic_port_identify(ionic);
284 dev_err(dev, "Cannot identify port: %d, aborting\n", err);
288 err = ionic_port_init(ionic);
290 dev_err(dev, "Cannot init port: %d, aborting\n", err);
294 /* Allocate and init the LIF */
295 err = ionic_lif_size(ionic);
297 dev_err(dev, "Cannot size LIF: %d, aborting\n", err);
298 goto err_out_port_reset;
301 err = ionic_lif_alloc(ionic);
303 dev_err(dev, "Cannot allocate LIF: %d, aborting\n", err);
304 goto err_out_free_irqs;
307 err = ionic_lif_init(ionic->lif);
309 dev_err(dev, "Cannot init LIF: %d, aborting\n", err);
310 goto err_out_free_lifs;
313 init_rwsem(&ionic->vf_op_lock);
314 num_vfs = pci_num_vf(pdev);
316 dev_info(dev, "%d VFs found already enabled\n", num_vfs);
317 err = ionic_vf_alloc(ionic, num_vfs);
319 dev_err(dev, "Cannot enable existing VFs: %d\n", err);
322 err = ionic_lif_register(ionic->lif);
324 dev_err(dev, "Cannot register LIF: %d, aborting\n", err);
325 goto err_out_deinit_lifs;
328 err = ionic_devlink_register(ionic);
330 dev_err(dev, "Cannot register devlink: %d\n", err);
331 goto err_out_deregister_lifs;
334 mod_timer(&ionic->watchdog_timer,
335 round_jiffies(jiffies + ionic->watchdog_period));
339 err_out_deregister_lifs:
340 ionic_lif_unregister(ionic->lif);
342 ionic_vf_dealloc(ionic);
343 ionic_lif_deinit(ionic->lif);
345 ionic_lif_free(ionic->lif);
348 ionic_bus_free_irq_vectors(ionic);
350 ionic_port_reset(ionic);
354 pci_clear_master(pdev);
355 /* Don't fail the probe for these errors, keep
356 * the hw interface around for inspection
361 ionic_unmap_bars(ionic);
362 pci_release_regions(pdev);
363 err_out_pci_disable_device:
364 pci_disable_device(pdev);
365 err_out_debugfs_del_dev:
366 ionic_debugfs_del_dev(ionic);
367 err_out_clear_drvdata:
368 mutex_destroy(&ionic->dev_cmd_lock);
369 ionic_devlink_free(ionic);
374 static void ionic_remove(struct pci_dev *pdev)
376 struct ionic *ionic = pci_get_drvdata(pdev);
378 del_timer_sync(&ionic->watchdog_timer);
381 ionic_devlink_unregister(ionic);
382 ionic_lif_unregister(ionic->lif);
383 ionic_lif_deinit(ionic->lif);
384 ionic_lif_free(ionic->lif);
386 ionic_bus_free_irq_vectors(ionic);
389 ionic_port_reset(ionic);
391 pci_clear_master(pdev);
392 ionic_unmap_bars(ionic);
393 pci_release_regions(pdev);
394 pci_disable_device(pdev);
395 ionic_debugfs_del_dev(ionic);
396 mutex_destroy(&ionic->dev_cmd_lock);
397 ionic_devlink_free(ionic);
400 static struct pci_driver ionic_driver = {
401 .name = IONIC_DRV_NAME,
402 .id_table = ionic_id_table,
403 .probe = ionic_probe,
404 .remove = ionic_remove,
405 .sriov_configure = ionic_sriov_configure,
408 int ionic_bus_register_driver(void)
410 return pci_register_driver(&ionic_driver);
413 void ionic_bus_unregister_driver(void)
415 pci_unregister_driver(&ionic_driver);