xref: /linux/drivers/net/ethernet/intel/idpf/idpf_main.c (revision 85502b2214d50ba0ddf2a5fb454e4d28a160d175)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (C) 2023 Intel Corporation */
3 
4 #include "idpf.h"
5 #include "idpf_devids.h"
6 #include "idpf_virtchnl.h"
7 
8 #define DRV_SUMMARY	"Intel(R) Infrastructure Data Path Function Linux Driver"
9 
10 MODULE_DESCRIPTION(DRV_SUMMARY);
11 MODULE_IMPORT_NS("LIBETH");
12 MODULE_LICENSE("GPL");
13 
14 /**
15  * idpf_remove - Device removal routine
16  * @pdev: PCI device information struct
17  */
idpf_remove(struct pci_dev * pdev)18 static void idpf_remove(struct pci_dev *pdev)
19 {
20 	struct idpf_adapter *adapter = pci_get_drvdata(pdev);
21 	int i;
22 
23 	set_bit(IDPF_REMOVE_IN_PROG, adapter->flags);
24 
25 	/* Wait until vc_event_task is done to consider if any hard reset is
26 	 * in progress else we may go ahead and release the resources but the
27 	 * thread doing the hard reset might continue the init path and
28 	 * end up in bad state.
29 	 */
30 	cancel_delayed_work_sync(&adapter->vc_event_task);
31 	if (adapter->num_vfs)
32 		idpf_sriov_configure(pdev, 0);
33 
34 	idpf_vc_core_deinit(adapter);
35 
36 	/* Be a good citizen and leave the device clean on exit */
37 	adapter->dev_ops.reg_ops.trigger_reset(adapter, IDPF_HR_FUNC_RESET);
38 	idpf_deinit_dflt_mbx(adapter);
39 
40 	if (!adapter->netdevs)
41 		goto destroy_wqs;
42 
43 	/* There are some cases where it's possible to still have netdevs
44 	 * registered with the stack at this point, e.g. if the driver detected
45 	 * a HW reset and rmmod is called before it fully recovers. Unregister
46 	 * any stale netdevs here.
47 	 */
48 	for (i = 0; i < adapter->max_vports; i++) {
49 		if (!adapter->netdevs[i])
50 			continue;
51 		if (adapter->netdevs[i]->reg_state != NETREG_UNINITIALIZED)
52 			unregister_netdev(adapter->netdevs[i]);
53 		free_netdev(adapter->netdevs[i]);
54 		adapter->netdevs[i] = NULL;
55 	}
56 
57 destroy_wqs:
58 	destroy_workqueue(adapter->init_wq);
59 	destroy_workqueue(adapter->serv_wq);
60 	destroy_workqueue(adapter->mbx_wq);
61 	destroy_workqueue(adapter->stats_wq);
62 	destroy_workqueue(adapter->vc_event_wq);
63 
64 	for (i = 0; i < adapter->max_vports; i++) {
65 		kfree(adapter->vport_config[i]);
66 		adapter->vport_config[i] = NULL;
67 	}
68 	kfree(adapter->vport_config);
69 	adapter->vport_config = NULL;
70 	kfree(adapter->netdevs);
71 	adapter->netdevs = NULL;
72 	kfree(adapter->vcxn_mngr);
73 	adapter->vcxn_mngr = NULL;
74 
75 	mutex_destroy(&adapter->vport_ctrl_lock);
76 	mutex_destroy(&adapter->vector_lock);
77 	mutex_destroy(&adapter->queue_lock);
78 	mutex_destroy(&adapter->vc_buf_lock);
79 
80 	pci_set_drvdata(pdev, NULL);
81 	kfree(adapter);
82 }
83 
84 /**
85  * idpf_shutdown - PCI callback for shutting down device
86  * @pdev: PCI device information struct
87  */
idpf_shutdown(struct pci_dev * pdev)88 static void idpf_shutdown(struct pci_dev *pdev)
89 {
90 	struct idpf_adapter *adapter = pci_get_drvdata(pdev);
91 
92 	cancel_delayed_work_sync(&adapter->serv_task);
93 	cancel_delayed_work_sync(&adapter->vc_event_task);
94 	idpf_vc_core_deinit(adapter);
95 	idpf_deinit_dflt_mbx(adapter);
96 
97 	if (system_state == SYSTEM_POWER_OFF)
98 		pci_set_power_state(pdev, PCI_D3hot);
99 }
100 
101 /**
102  * idpf_cfg_hw - Initialize HW struct
103  * @adapter: adapter to setup hw struct for
104  *
105  * Returns 0 on success, negative on failure
106  */
idpf_cfg_hw(struct idpf_adapter * adapter)107 static int idpf_cfg_hw(struct idpf_adapter *adapter)
108 {
109 	struct pci_dev *pdev = adapter->pdev;
110 	struct idpf_hw *hw = &adapter->hw;
111 
112 	hw->hw_addr = pcim_iomap_table(pdev)[0];
113 	if (!hw->hw_addr) {
114 		pci_err(pdev, "failed to allocate PCI iomap table\n");
115 
116 		return -ENOMEM;
117 	}
118 
119 	hw->back = adapter;
120 
121 	return 0;
122 }
123 
124 /**
125  * idpf_probe - Device initialization routine
126  * @pdev: PCI device information struct
127  * @ent: entry in idpf_pci_tbl
128  *
129  * Returns 0 on success, negative on failure
130  */
idpf_probe(struct pci_dev * pdev,const struct pci_device_id * ent)131 static int idpf_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
132 {
133 	struct device *dev = &pdev->dev;
134 	struct idpf_adapter *adapter;
135 	int err;
136 
137 	adapter = kzalloc(sizeof(*adapter), GFP_KERNEL);
138 	if (!adapter)
139 		return -ENOMEM;
140 
141 	adapter->req_tx_splitq = true;
142 	adapter->req_rx_splitq = true;
143 
144 	switch (ent->device) {
145 	case IDPF_DEV_ID_PF:
146 		idpf_dev_ops_init(adapter);
147 		break;
148 	case IDPF_DEV_ID_VF:
149 		idpf_vf_dev_ops_init(adapter);
150 		adapter->crc_enable = true;
151 		break;
152 	default:
153 		err = -ENODEV;
154 		dev_err(&pdev->dev, "Unexpected dev ID 0x%x in idpf probe\n",
155 			ent->device);
156 		goto err_free;
157 	}
158 
159 	adapter->pdev = pdev;
160 	err = pcim_enable_device(pdev);
161 	if (err)
162 		goto err_free;
163 
164 	err = pcim_iomap_regions(pdev, BIT(0), pci_name(pdev));
165 	if (err) {
166 		pci_err(pdev, "pcim_iomap_regions failed %pe\n", ERR_PTR(err));
167 
168 		goto err_free;
169 	}
170 
171 	err = pci_enable_ptm(pdev, NULL);
172 	if (err)
173 		pci_dbg(pdev, "PCIe PTM is not supported by PCIe bus/controller\n");
174 
175 	/* set up for high or low dma */
176 	err = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64));
177 	if (err) {
178 		pci_err(pdev, "DMA configuration failed: %pe\n", ERR_PTR(err));
179 
180 		goto err_free;
181 	}
182 
183 	pci_set_master(pdev);
184 	pci_set_drvdata(pdev, adapter);
185 
186 	adapter->init_wq = alloc_workqueue("%s-%s-init",
187 					   WQ_UNBOUND | WQ_MEM_RECLAIM, 0,
188 					   dev_driver_string(dev),
189 					   dev_name(dev));
190 	if (!adapter->init_wq) {
191 		dev_err(dev, "Failed to allocate init workqueue\n");
192 		err = -ENOMEM;
193 		goto err_free;
194 	}
195 
196 	adapter->serv_wq = alloc_workqueue("%s-%s-service",
197 					   WQ_UNBOUND | WQ_MEM_RECLAIM, 0,
198 					   dev_driver_string(dev),
199 					   dev_name(dev));
200 	if (!adapter->serv_wq) {
201 		dev_err(dev, "Failed to allocate service workqueue\n");
202 		err = -ENOMEM;
203 		goto err_serv_wq_alloc;
204 	}
205 
206 	adapter->mbx_wq = alloc_workqueue("%s-%s-mbx", WQ_UNBOUND | WQ_HIGHPRI,
207 					  0, dev_driver_string(dev),
208 					  dev_name(dev));
209 	if (!adapter->mbx_wq) {
210 		dev_err(dev, "Failed to allocate mailbox workqueue\n");
211 		err = -ENOMEM;
212 		goto err_mbx_wq_alloc;
213 	}
214 
215 	adapter->stats_wq = alloc_workqueue("%s-%s-stats",
216 					    WQ_UNBOUND | WQ_MEM_RECLAIM, 0,
217 					    dev_driver_string(dev),
218 					    dev_name(dev));
219 	if (!adapter->stats_wq) {
220 		dev_err(dev, "Failed to allocate workqueue\n");
221 		err = -ENOMEM;
222 		goto err_stats_wq_alloc;
223 	}
224 
225 	adapter->vc_event_wq = alloc_workqueue("%s-%s-vc_event",
226 					       WQ_UNBOUND | WQ_MEM_RECLAIM, 0,
227 					       dev_driver_string(dev),
228 					       dev_name(dev));
229 	if (!adapter->vc_event_wq) {
230 		dev_err(dev, "Failed to allocate virtchnl event workqueue\n");
231 		err = -ENOMEM;
232 		goto err_vc_event_wq_alloc;
233 	}
234 
235 	/* setup msglvl */
236 	adapter->msg_enable = netif_msg_init(-1, IDPF_AVAIL_NETIF_M);
237 
238 	err = idpf_cfg_hw(adapter);
239 	if (err) {
240 		dev_err(dev, "Failed to configure HW structure for adapter: %d\n",
241 			err);
242 		goto err_cfg_hw;
243 	}
244 
245 	mutex_init(&adapter->vport_ctrl_lock);
246 	mutex_init(&adapter->vector_lock);
247 	mutex_init(&adapter->queue_lock);
248 	mutex_init(&adapter->vc_buf_lock);
249 
250 	INIT_DELAYED_WORK(&adapter->init_task, idpf_init_task);
251 	INIT_DELAYED_WORK(&adapter->serv_task, idpf_service_task);
252 	INIT_DELAYED_WORK(&adapter->mbx_task, idpf_mbx_task);
253 	INIT_DELAYED_WORK(&adapter->stats_task, idpf_statistics_task);
254 	INIT_DELAYED_WORK(&adapter->vc_event_task, idpf_vc_event_task);
255 
256 	adapter->dev_ops.reg_ops.reset_reg_init(adapter);
257 	set_bit(IDPF_HR_DRV_LOAD, adapter->flags);
258 	queue_delayed_work(adapter->vc_event_wq, &adapter->vc_event_task,
259 			   msecs_to_jiffies(10 * (pdev->devfn & 0x07)));
260 
261 	return 0;
262 
263 err_cfg_hw:
264 	destroy_workqueue(adapter->vc_event_wq);
265 err_vc_event_wq_alloc:
266 	destroy_workqueue(adapter->stats_wq);
267 err_stats_wq_alloc:
268 	destroy_workqueue(adapter->mbx_wq);
269 err_mbx_wq_alloc:
270 	destroy_workqueue(adapter->serv_wq);
271 err_serv_wq_alloc:
272 	destroy_workqueue(adapter->init_wq);
273 err_free:
274 	kfree(adapter);
275 	return err;
276 }
277 
278 /* idpf_pci_tbl - PCI Dev idpf ID Table
279  */
280 static const struct pci_device_id idpf_pci_tbl[] = {
281 	{ PCI_VDEVICE(INTEL, IDPF_DEV_ID_PF)},
282 	{ PCI_VDEVICE(INTEL, IDPF_DEV_ID_VF)},
283 	{ /* Sentinel */ }
284 };
285 MODULE_DEVICE_TABLE(pci, idpf_pci_tbl);
286 
287 static struct pci_driver idpf_driver = {
288 	.name			= KBUILD_MODNAME,
289 	.id_table		= idpf_pci_tbl,
290 	.probe			= idpf_probe,
291 	.sriov_configure	= idpf_sriov_configure,
292 	.remove			= idpf_remove,
293 	.shutdown		= idpf_shutdown,
294 };
295 module_pci_driver(idpf_driver);
296