1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (C) 2023 Intel Corporation */
5 #include "idpf_devids.h"
7 #define DRV_SUMMARY "Intel(R) Infrastructure Data Path Function Linux Driver"
9 MODULE_DESCRIPTION(DRV_SUMMARY);
10 MODULE_LICENSE("GPL");
13 * idpf_remove - Device removal routine
14 * @pdev: PCI device information struct
16 static void idpf_remove(struct pci_dev *pdev)
18 struct idpf_adapter *adapter = pci_get_drvdata(pdev);
21 set_bit(IDPF_REMOVE_IN_PROG, adapter->flags);
23 /* Wait until vc_event_task is done to consider if any hard reset is
24 * in progress else we may go ahead and release the resources but the
25 * thread doing the hard reset might continue the init path and
26 * end up in bad state.
28 cancel_delayed_work_sync(&adapter->vc_event_task);
30 idpf_sriov_configure(pdev, 0);
32 idpf_vc_core_deinit(adapter);
33 /* Be a good citizen and leave the device clean on exit */
34 adapter->dev_ops.reg_ops.trigger_reset(adapter, IDPF_HR_FUNC_RESET);
35 idpf_deinit_dflt_mbx(adapter);
37 if (!adapter->netdevs)
40 /* There are some cases where it's possible to still have netdevs
41 * registered with the stack at this point, e.g. if the driver detected
42 * a HW reset and rmmod is called before it fully recovers. Unregister
43 * any stale netdevs here.
45 for (i = 0; i < adapter->max_vports; i++) {
46 if (!adapter->netdevs[i])
48 if (adapter->netdevs[i]->reg_state != NETREG_UNINITIALIZED)
49 unregister_netdev(adapter->netdevs[i]);
50 free_netdev(adapter->netdevs[i]);
51 adapter->netdevs[i] = NULL;
55 destroy_workqueue(adapter->init_wq);
56 destroy_workqueue(adapter->serv_wq);
57 destroy_workqueue(adapter->mbx_wq);
58 destroy_workqueue(adapter->stats_wq);
59 destroy_workqueue(adapter->vc_event_wq);
61 for (i = 0; i < adapter->max_vports; i++) {
62 kfree(adapter->vport_config[i]);
63 adapter->vport_config[i] = NULL;
65 kfree(adapter->vport_config);
66 adapter->vport_config = NULL;
67 kfree(adapter->netdevs);
68 adapter->netdevs = NULL;
70 mutex_destroy(&adapter->vport_ctrl_lock);
71 mutex_destroy(&adapter->vector_lock);
72 mutex_destroy(&adapter->queue_lock);
73 mutex_destroy(&adapter->vc_buf_lock);
75 pci_set_drvdata(pdev, NULL);
80 * idpf_shutdown - PCI callback for shutting down device
81 * @pdev: PCI device information struct
83 static void idpf_shutdown(struct pci_dev *pdev)
87 if (system_state == SYSTEM_POWER_OFF)
88 pci_set_power_state(pdev, PCI_D3hot);
92 * idpf_cfg_hw - Initialize HW struct
93 * @adapter: adapter to setup hw struct for
95 * Returns 0 on success, negative on failure
97 static int idpf_cfg_hw(struct idpf_adapter *adapter)
99 struct pci_dev *pdev = adapter->pdev;
100 struct idpf_hw *hw = &adapter->hw;
102 hw->hw_addr = pcim_iomap_table(pdev)[0];
104 pci_err(pdev, "failed to allocate PCI iomap table\n");
115 * idpf_probe - Device initialization routine
116 * @pdev: PCI device information struct
117 * @ent: entry in idpf_pci_tbl
119 * Returns 0 on success, negative on failure
121 static int idpf_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
123 struct device *dev = &pdev->dev;
124 struct idpf_adapter *adapter;
127 adapter = kzalloc(sizeof(*adapter), GFP_KERNEL);
131 adapter->req_tx_splitq = true;
132 adapter->req_rx_splitq = true;
134 switch (ent->device) {
136 idpf_dev_ops_init(adapter);
139 idpf_vf_dev_ops_init(adapter);
140 adapter->crc_enable = true;
144 dev_err(&pdev->dev, "Unexpected dev ID 0x%x in idpf probe\n",
149 adapter->pdev = pdev;
150 err = pcim_enable_device(pdev);
154 err = pcim_iomap_regions(pdev, BIT(0), pci_name(pdev));
156 pci_err(pdev, "pcim_iomap_regions failed %pe\n", ERR_PTR(err));
161 /* set up for high or low dma */
162 err = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64));
164 pci_err(pdev, "DMA configuration failed: %pe\n", ERR_PTR(err));
169 pci_set_master(pdev);
170 pci_set_drvdata(pdev, adapter);
172 adapter->init_wq = alloc_workqueue("%s-%s-init", 0, 0,
173 dev_driver_string(dev),
175 if (!adapter->init_wq) {
176 dev_err(dev, "Failed to allocate init workqueue\n");
181 adapter->serv_wq = alloc_workqueue("%s-%s-service", 0, 0,
182 dev_driver_string(dev),
184 if (!adapter->serv_wq) {
185 dev_err(dev, "Failed to allocate service workqueue\n");
187 goto err_serv_wq_alloc;
190 adapter->mbx_wq = alloc_workqueue("%s-%s-mbx", 0, 0,
191 dev_driver_string(dev),
193 if (!adapter->mbx_wq) {
194 dev_err(dev, "Failed to allocate mailbox workqueue\n");
196 goto err_mbx_wq_alloc;
199 adapter->stats_wq = alloc_workqueue("%s-%s-stats", 0, 0,
200 dev_driver_string(dev),
202 if (!adapter->stats_wq) {
203 dev_err(dev, "Failed to allocate workqueue\n");
205 goto err_stats_wq_alloc;
208 adapter->vc_event_wq = alloc_workqueue("%s-%s-vc_event", 0, 0,
209 dev_driver_string(dev),
211 if (!adapter->vc_event_wq) {
212 dev_err(dev, "Failed to allocate virtchnl event workqueue\n");
214 goto err_vc_event_wq_alloc;
218 adapter->msg_enable = netif_msg_init(-1, IDPF_AVAIL_NETIF_M);
220 err = idpf_cfg_hw(adapter);
222 dev_err(dev, "Failed to configure HW structure for adapter: %d\n",
227 mutex_init(&adapter->vport_ctrl_lock);
228 mutex_init(&adapter->vector_lock);
229 mutex_init(&adapter->queue_lock);
230 mutex_init(&adapter->vc_buf_lock);
232 init_waitqueue_head(&adapter->vchnl_wq);
234 INIT_DELAYED_WORK(&adapter->init_task, idpf_init_task);
235 INIT_DELAYED_WORK(&adapter->serv_task, idpf_service_task);
236 INIT_DELAYED_WORK(&adapter->mbx_task, idpf_mbx_task);
237 INIT_DELAYED_WORK(&adapter->stats_task, idpf_statistics_task);
238 INIT_DELAYED_WORK(&adapter->vc_event_task, idpf_vc_event_task);
240 adapter->dev_ops.reg_ops.reset_reg_init(adapter);
241 set_bit(IDPF_HR_DRV_LOAD, adapter->flags);
242 queue_delayed_work(adapter->vc_event_wq, &adapter->vc_event_task,
243 msecs_to_jiffies(10 * (pdev->devfn & 0x07)));
248 destroy_workqueue(adapter->vc_event_wq);
249 err_vc_event_wq_alloc:
250 destroy_workqueue(adapter->stats_wq);
252 destroy_workqueue(adapter->mbx_wq);
254 destroy_workqueue(adapter->serv_wq);
256 destroy_workqueue(adapter->init_wq);
262 /* idpf_pci_tbl - PCI Dev idpf ID Table
264 static const struct pci_device_id idpf_pci_tbl[] = {
265 { PCI_VDEVICE(INTEL, IDPF_DEV_ID_PF)},
266 { PCI_VDEVICE(INTEL, IDPF_DEV_ID_VF)},
269 MODULE_DEVICE_TABLE(pci, idpf_pci_tbl);
271 static struct pci_driver idpf_driver = {
272 .name = KBUILD_MODNAME,
273 .id_table = idpf_pci_tbl,
275 .sriov_configure = idpf_sriov_configure,
276 .remove = idpf_remove,
277 .shutdown = idpf_shutdown,
279 module_pci_driver(idpf_driver);