xref: /linux/drivers/net/ethernet/intel/idpf/idpf_main.c (revision ad30469a841b50dbb541df4d6971d891f703c297)
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 
7 #define DRV_SUMMARY	"Intel(R) Infrastructure Data Path Function Linux Driver"
8 
9 MODULE_DESCRIPTION(DRV_SUMMARY);
10 MODULE_LICENSE("GPL");
11 
12 /**
13  * idpf_remove - Device removal routine
14  * @pdev: PCI device information struct
15  */
16 static void idpf_remove(struct pci_dev *pdev)
17 {
18 	struct idpf_adapter *adapter = pci_get_drvdata(pdev);
19 	int i;
20 
21 	set_bit(IDPF_REMOVE_IN_PROG, adapter->flags);
22 
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.
27 	 */
28 	cancel_delayed_work_sync(&adapter->vc_event_task);
29 	if (adapter->num_vfs)
30 		idpf_sriov_configure(pdev, 0);
31 
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);
36 
37 	if (!adapter->netdevs)
38 		goto destroy_wqs;
39 
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.
44 	 */
45 	for (i = 0; i < adapter->max_vports; i++) {
46 		if (!adapter->netdevs[i])
47 			continue;
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;
52 	}
53 
54 destroy_wqs:
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);
60 
61 	for (i = 0; i < adapter->max_vports; i++) {
62 		kfree(adapter->vport_config[i]);
63 		adapter->vport_config[i] = NULL;
64 	}
65 	kfree(adapter->vport_config);
66 	adapter->vport_config = NULL;
67 	kfree(adapter->netdevs);
68 	adapter->netdevs = NULL;
69 
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);
74 
75 	pci_set_drvdata(pdev, NULL);
76 	kfree(adapter);
77 }
78 
79 /**
80  * idpf_shutdown - PCI callback for shutting down device
81  * @pdev: PCI device information struct
82  */
83 static void idpf_shutdown(struct pci_dev *pdev)
84 {
85 	idpf_remove(pdev);
86 
87 	if (system_state == SYSTEM_POWER_OFF)
88 		pci_set_power_state(pdev, PCI_D3hot);
89 }
90 
91 /**
92  * idpf_cfg_hw - Initialize HW struct
93  * @adapter: adapter to setup hw struct for
94  *
95  * Returns 0 on success, negative on failure
96  */
97 static int idpf_cfg_hw(struct idpf_adapter *adapter)
98 {
99 	struct pci_dev *pdev = adapter->pdev;
100 	struct idpf_hw *hw = &adapter->hw;
101 
102 	hw->hw_addr = pcim_iomap_table(pdev)[0];
103 	if (!hw->hw_addr) {
104 		pci_err(pdev, "failed to allocate PCI iomap table\n");
105 
106 		return -ENOMEM;
107 	}
108 
109 	hw->back = adapter;
110 
111 	return 0;
112 }
113 
114 /**
115  * idpf_probe - Device initialization routine
116  * @pdev: PCI device information struct
117  * @ent: entry in idpf_pci_tbl
118  *
119  * Returns 0 on success, negative on failure
120  */
121 static int idpf_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
122 {
123 	struct device *dev = &pdev->dev;
124 	struct idpf_adapter *adapter;
125 	int err;
126 
127 	adapter = kzalloc(sizeof(*adapter), GFP_KERNEL);
128 	if (!adapter)
129 		return -ENOMEM;
130 
131 	adapter->req_tx_splitq = true;
132 	adapter->req_rx_splitq = true;
133 
134 	switch (ent->device) {
135 	case IDPF_DEV_ID_PF:
136 		idpf_dev_ops_init(adapter);
137 		break;
138 	case IDPF_DEV_ID_VF:
139 		idpf_vf_dev_ops_init(adapter);
140 		adapter->crc_enable = true;
141 		break;
142 	default:
143 		err = -ENODEV;
144 		dev_err(&pdev->dev, "Unexpected dev ID 0x%x in idpf probe\n",
145 			ent->device);
146 		goto err_free;
147 	}
148 
149 	adapter->pdev = pdev;
150 	err = pcim_enable_device(pdev);
151 	if (err)
152 		goto err_free;
153 
154 	err = pcim_iomap_regions(pdev, BIT(0), pci_name(pdev));
155 	if (err) {
156 		pci_err(pdev, "pcim_iomap_regions failed %pe\n", ERR_PTR(err));
157 
158 		goto err_free;
159 	}
160 
161 	/* set up for high or low dma */
162 	err = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64));
163 	if (err) {
164 		pci_err(pdev, "DMA configuration failed: %pe\n", ERR_PTR(err));
165 
166 		goto err_free;
167 	}
168 
169 	pci_set_master(pdev);
170 	pci_set_drvdata(pdev, adapter);
171 
172 	adapter->init_wq = alloc_workqueue("%s-%s-init", 0, 0,
173 					   dev_driver_string(dev),
174 					   dev_name(dev));
175 	if (!adapter->init_wq) {
176 		dev_err(dev, "Failed to allocate init workqueue\n");
177 		err = -ENOMEM;
178 		goto err_free;
179 	}
180 
181 	adapter->serv_wq = alloc_workqueue("%s-%s-service", 0, 0,
182 					   dev_driver_string(dev),
183 					   dev_name(dev));
184 	if (!adapter->serv_wq) {
185 		dev_err(dev, "Failed to allocate service workqueue\n");
186 		err = -ENOMEM;
187 		goto err_serv_wq_alloc;
188 	}
189 
190 	adapter->mbx_wq = alloc_workqueue("%s-%s-mbx", 0, 0,
191 					  dev_driver_string(dev),
192 					  dev_name(dev));
193 	if (!adapter->mbx_wq) {
194 		dev_err(dev, "Failed to allocate mailbox workqueue\n");
195 		err = -ENOMEM;
196 		goto err_mbx_wq_alloc;
197 	}
198 
199 	adapter->stats_wq = alloc_workqueue("%s-%s-stats", 0, 0,
200 					    dev_driver_string(dev),
201 					    dev_name(dev));
202 	if (!adapter->stats_wq) {
203 		dev_err(dev, "Failed to allocate workqueue\n");
204 		err = -ENOMEM;
205 		goto err_stats_wq_alloc;
206 	}
207 
208 	adapter->vc_event_wq = alloc_workqueue("%s-%s-vc_event", 0, 0,
209 					       dev_driver_string(dev),
210 					       dev_name(dev));
211 	if (!adapter->vc_event_wq) {
212 		dev_err(dev, "Failed to allocate virtchnl event workqueue\n");
213 		err = -ENOMEM;
214 		goto err_vc_event_wq_alloc;
215 	}
216 
217 	/* setup msglvl */
218 	adapter->msg_enable = netif_msg_init(-1, IDPF_AVAIL_NETIF_M);
219 
220 	err = idpf_cfg_hw(adapter);
221 	if (err) {
222 		dev_err(dev, "Failed to configure HW structure for adapter: %d\n",
223 			err);
224 		goto err_cfg_hw;
225 	}
226 
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);
231 
232 	init_waitqueue_head(&adapter->vchnl_wq);
233 
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);
239 
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)));
244 
245 	return 0;
246 
247 err_cfg_hw:
248 	destroy_workqueue(adapter->vc_event_wq);
249 err_vc_event_wq_alloc:
250 	destroy_workqueue(adapter->stats_wq);
251 err_stats_wq_alloc:
252 	destroy_workqueue(adapter->mbx_wq);
253 err_mbx_wq_alloc:
254 	destroy_workqueue(adapter->serv_wq);
255 err_serv_wq_alloc:
256 	destroy_workqueue(adapter->init_wq);
257 err_free:
258 	kfree(adapter);
259 	return err;
260 }
261 
262 /* idpf_pci_tbl - PCI Dev idpf ID Table
263  */
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)},
267 	{ /* Sentinel */ }
268 };
269 MODULE_DEVICE_TABLE(pci, idpf_pci_tbl);
270 
271 static struct pci_driver idpf_driver = {
272 	.name			= KBUILD_MODNAME,
273 	.id_table		= idpf_pci_tbl,
274 	.probe			= idpf_probe,
275 	.sriov_configure	= idpf_sriov_configure,
276 	.remove			= idpf_remove,
277 	.shutdown		= idpf_shutdown,
278 };
279 module_pci_driver(idpf_driver);
280