xref: /linux/drivers/net/ethernet/intel/idpf/idpf_main.c (revision a1ff5a7d78a036d6c2178ee5acd6ba4946243800)
1e850efedSPhani Burra // SPDX-License-Identifier: GPL-2.0-only
2e850efedSPhani Burra /* Copyright (C) 2023 Intel Corporation */
3e850efedSPhani Burra 
4e850efedSPhani Burra #include "idpf.h"
5e850efedSPhani Burra #include "idpf_devids.h"
65dc283faSAlan Brady #include "idpf_virtchnl.h"
7e850efedSPhani Burra 
8e850efedSPhani Burra #define DRV_SUMMARY	"Intel(R) Infrastructure Data Path Function Linux Driver"
9e850efedSPhani Burra 
10e850efedSPhani Burra MODULE_DESCRIPTION(DRV_SUMMARY);
11*1b1b2620SAlexander Lobakin MODULE_IMPORT_NS(LIBETH);
12e850efedSPhani Burra MODULE_LICENSE("GPL");
13e850efedSPhani Burra 
14e850efedSPhani Burra /**
15e850efedSPhani Burra  * idpf_remove - Device removal routine
16e850efedSPhani Burra  * @pdev: PCI device information struct
17e850efedSPhani Burra  */
idpf_remove(struct pci_dev * pdev)18e850efedSPhani Burra static void idpf_remove(struct pci_dev *pdev)
19e850efedSPhani Burra {
20e850efedSPhani Burra 	struct idpf_adapter *adapter = pci_get_drvdata(pdev);
210fe45467SPavan Kumar Linga 	int i;
22e850efedSPhani Burra 
238077c727SJoshua Hay 	set_bit(IDPF_REMOVE_IN_PROG, adapter->flags);
248077c727SJoshua Hay 
258077c727SJoshua Hay 	/* Wait until vc_event_task is done to consider if any hard reset is
268077c727SJoshua Hay 	 * in progress else we may go ahead and release the resources but the
278077c727SJoshua Hay 	 * thread doing the hard reset might continue the init path and
288077c727SJoshua Hay 	 * end up in bad state.
298077c727SJoshua Hay 	 */
308077c727SJoshua Hay 	cancel_delayed_work_sync(&adapter->vc_event_task);
31a251eee6SJoshua Hay 	if (adapter->num_vfs)
32a251eee6SJoshua Hay 		idpf_sriov_configure(pdev, 0);
33a251eee6SJoshua Hay 
344930fbf4SPavan Kumar Linga 	idpf_vc_core_deinit(adapter);
3534c21fa8SAlan Brady 
368077c727SJoshua Hay 	/* Be a good citizen and leave the device clean on exit */
378077c727SJoshua Hay 	adapter->dev_ops.reg_ops.trigger_reset(adapter, IDPF_HR_FUNC_RESET);
388077c727SJoshua Hay 	idpf_deinit_dflt_mbx(adapter);
398077c727SJoshua Hay 
400fe45467SPavan Kumar Linga 	if (!adapter->netdevs)
410fe45467SPavan Kumar Linga 		goto destroy_wqs;
420fe45467SPavan Kumar Linga 
430fe45467SPavan Kumar Linga 	/* There are some cases where it's possible to still have netdevs
440fe45467SPavan Kumar Linga 	 * registered with the stack at this point, e.g. if the driver detected
450fe45467SPavan Kumar Linga 	 * a HW reset and rmmod is called before it fully recovers. Unregister
460fe45467SPavan Kumar Linga 	 * any stale netdevs here.
470fe45467SPavan Kumar Linga 	 */
480fe45467SPavan Kumar Linga 	for (i = 0; i < adapter->max_vports; i++) {
490fe45467SPavan Kumar Linga 		if (!adapter->netdevs[i])
500fe45467SPavan Kumar Linga 			continue;
510fe45467SPavan Kumar Linga 		if (adapter->netdevs[i]->reg_state != NETREG_UNINITIALIZED)
520fe45467SPavan Kumar Linga 			unregister_netdev(adapter->netdevs[i]);
530fe45467SPavan Kumar Linga 		free_netdev(adapter->netdevs[i]);
540fe45467SPavan Kumar Linga 		adapter->netdevs[i] = NULL;
550fe45467SPavan Kumar Linga 	}
560fe45467SPavan Kumar Linga 
570fe45467SPavan Kumar Linga destroy_wqs:
580fe45467SPavan Kumar Linga 	destroy_workqueue(adapter->init_wq);
594930fbf4SPavan Kumar Linga 	destroy_workqueue(adapter->serv_wq);
604930fbf4SPavan Kumar Linga 	destroy_workqueue(adapter->mbx_wq);
61a251eee6SJoshua Hay 	destroy_workqueue(adapter->stats_wq);
628077c727SJoshua Hay 	destroy_workqueue(adapter->vc_event_wq);
630fe45467SPavan Kumar Linga 
640fe45467SPavan Kumar Linga 	for (i = 0; i < adapter->max_vports; i++) {
650fe45467SPavan Kumar Linga 		kfree(adapter->vport_config[i]);
660fe45467SPavan Kumar Linga 		adapter->vport_config[i] = NULL;
670fe45467SPavan Kumar Linga 	}
680fe45467SPavan Kumar Linga 	kfree(adapter->vport_config);
690fe45467SPavan Kumar Linga 	adapter->vport_config = NULL;
700fe45467SPavan Kumar Linga 	kfree(adapter->netdevs);
710fe45467SPavan Kumar Linga 	adapter->netdevs = NULL;
7234c21fa8SAlan Brady 	kfree(adapter->vcxn_mngr);
7334c21fa8SAlan Brady 	adapter->vcxn_mngr = NULL;
740fe45467SPavan Kumar Linga 
758077c727SJoshua Hay 	mutex_destroy(&adapter->vport_ctrl_lock);
764930fbf4SPavan Kumar Linga 	mutex_destroy(&adapter->vector_lock);
770fe45467SPavan Kumar Linga 	mutex_destroy(&adapter->queue_lock);
784930fbf4SPavan Kumar Linga 	mutex_destroy(&adapter->vc_buf_lock);
798077c727SJoshua Hay 
80e850efedSPhani Burra 	pci_set_drvdata(pdev, NULL);
81e850efedSPhani Burra 	kfree(adapter);
82e850efedSPhani Burra }
83e850efedSPhani Burra 
84e850efedSPhani Burra /**
85e850efedSPhani Burra  * idpf_shutdown - PCI callback for shutting down device
86e850efedSPhani Burra  * @pdev: PCI device information struct
87e850efedSPhani Burra  */
idpf_shutdown(struct pci_dev * pdev)88e850efedSPhani Burra static void idpf_shutdown(struct pci_dev *pdev)
89e850efedSPhani Burra {
90e850efedSPhani Burra 	idpf_remove(pdev);
91e850efedSPhani Burra 
92e850efedSPhani Burra 	if (system_state == SYSTEM_POWER_OFF)
93e850efedSPhani Burra 		pci_set_power_state(pdev, PCI_D3hot);
94e850efedSPhani Burra }
95e850efedSPhani Burra 
96e850efedSPhani Burra /**
97e850efedSPhani Burra  * idpf_cfg_hw - Initialize HW struct
98e850efedSPhani Burra  * @adapter: adapter to setup hw struct for
99e850efedSPhani Burra  *
100e850efedSPhani Burra  * Returns 0 on success, negative on failure
101e850efedSPhani Burra  */
idpf_cfg_hw(struct idpf_adapter * adapter)102e850efedSPhani Burra static int idpf_cfg_hw(struct idpf_adapter *adapter)
103e850efedSPhani Burra {
104e850efedSPhani Burra 	struct pci_dev *pdev = adapter->pdev;
105e850efedSPhani Burra 	struct idpf_hw *hw = &adapter->hw;
106e850efedSPhani Burra 
107e850efedSPhani Burra 	hw->hw_addr = pcim_iomap_table(pdev)[0];
108e850efedSPhani Burra 	if (!hw->hw_addr) {
109e850efedSPhani Burra 		pci_err(pdev, "failed to allocate PCI iomap table\n");
110e850efedSPhani Burra 
111e850efedSPhani Burra 		return -ENOMEM;
112e850efedSPhani Burra 	}
113e850efedSPhani Burra 
114e850efedSPhani Burra 	hw->back = adapter;
115e850efedSPhani Burra 
116e850efedSPhani Burra 	return 0;
117e850efedSPhani Burra }
118e850efedSPhani Burra 
119e850efedSPhani Burra /**
120e850efedSPhani Burra  * idpf_probe - Device initialization routine
121e850efedSPhani Burra  * @pdev: PCI device information struct
122e850efedSPhani Burra  * @ent: entry in idpf_pci_tbl
123e850efedSPhani Burra  *
124e850efedSPhani Burra  * Returns 0 on success, negative on failure
125e850efedSPhani Burra  */
idpf_probe(struct pci_dev * pdev,const struct pci_device_id * ent)126e850efedSPhani Burra static int idpf_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
127e850efedSPhani Burra {
128e850efedSPhani Burra 	struct device *dev = &pdev->dev;
129e850efedSPhani Burra 	struct idpf_adapter *adapter;
130e850efedSPhani Burra 	int err;
131e850efedSPhani Burra 
132e850efedSPhani Burra 	adapter = kzalloc(sizeof(*adapter), GFP_KERNEL);
133e850efedSPhani Burra 	if (!adapter)
134e850efedSPhani Burra 		return -ENOMEM;
135e850efedSPhani Burra 
1360fe45467SPavan Kumar Linga 	adapter->req_tx_splitq = true;
1370fe45467SPavan Kumar Linga 	adapter->req_rx_splitq = true;
1380fe45467SPavan Kumar Linga 
1398077c727SJoshua Hay 	switch (ent->device) {
1408077c727SJoshua Hay 	case IDPF_DEV_ID_PF:
1418077c727SJoshua Hay 		idpf_dev_ops_init(adapter);
1428077c727SJoshua Hay 		break;
1438077c727SJoshua Hay 	case IDPF_DEV_ID_VF:
1448077c727SJoshua Hay 		idpf_vf_dev_ops_init(adapter);
145a5ab9ee0SJoshua Hay 		adapter->crc_enable = true;
1468077c727SJoshua Hay 		break;
1478077c727SJoshua Hay 	default:
1488077c727SJoshua Hay 		err = -ENODEV;
1498077c727SJoshua Hay 		dev_err(&pdev->dev, "Unexpected dev ID 0x%x in idpf probe\n",
1508077c727SJoshua Hay 			ent->device);
1518077c727SJoshua Hay 		goto err_free;
1528077c727SJoshua Hay 	}
1538077c727SJoshua Hay 
1548077c727SJoshua Hay 	adapter->pdev = pdev;
155e850efedSPhani Burra 	err = pcim_enable_device(pdev);
156e850efedSPhani Burra 	if (err)
157e850efedSPhani Burra 		goto err_free;
158e850efedSPhani Burra 
159e850efedSPhani Burra 	err = pcim_iomap_regions(pdev, BIT(0), pci_name(pdev));
160e850efedSPhani Burra 	if (err) {
161e850efedSPhani Burra 		pci_err(pdev, "pcim_iomap_regions failed %pe\n", ERR_PTR(err));
162e850efedSPhani Burra 
163e850efedSPhani Burra 		goto err_free;
164e850efedSPhani Burra 	}
165e850efedSPhani Burra 
166e850efedSPhani Burra 	/* set up for high or low dma */
167e850efedSPhani Burra 	err = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64));
168e850efedSPhani Burra 	if (err) {
169e850efedSPhani Burra 		pci_err(pdev, "DMA configuration failed: %pe\n", ERR_PTR(err));
170e850efedSPhani Burra 
171e850efedSPhani Burra 		goto err_free;
172e850efedSPhani Burra 	}
173e850efedSPhani Burra 
174e850efedSPhani Burra 	pci_set_master(pdev);
175e850efedSPhani Burra 	pci_set_drvdata(pdev, adapter);
176e850efedSPhani Burra 
1770fe45467SPavan Kumar Linga 	adapter->init_wq = alloc_workqueue("%s-%s-init", 0, 0,
1780fe45467SPavan Kumar Linga 					   dev_driver_string(dev),
1790fe45467SPavan Kumar Linga 					   dev_name(dev));
1800fe45467SPavan Kumar Linga 	if (!adapter->init_wq) {
1810fe45467SPavan Kumar Linga 		dev_err(dev, "Failed to allocate init workqueue\n");
1820fe45467SPavan Kumar Linga 		err = -ENOMEM;
1830fe45467SPavan Kumar Linga 		goto err_free;
1840fe45467SPavan Kumar Linga 	}
1850fe45467SPavan Kumar Linga 
1864930fbf4SPavan Kumar Linga 	adapter->serv_wq = alloc_workqueue("%s-%s-service", 0, 0,
1874930fbf4SPavan Kumar Linga 					   dev_driver_string(dev),
1884930fbf4SPavan Kumar Linga 					   dev_name(dev));
1894930fbf4SPavan Kumar Linga 	if (!adapter->serv_wq) {
1904930fbf4SPavan Kumar Linga 		dev_err(dev, "Failed to allocate service workqueue\n");
1914930fbf4SPavan Kumar Linga 		err = -ENOMEM;
1920fe45467SPavan Kumar Linga 		goto err_serv_wq_alloc;
1934930fbf4SPavan Kumar Linga 	}
1944930fbf4SPavan Kumar Linga 
1954930fbf4SPavan Kumar Linga 	adapter->mbx_wq = alloc_workqueue("%s-%s-mbx", 0, 0,
1964930fbf4SPavan Kumar Linga 					  dev_driver_string(dev),
1974930fbf4SPavan Kumar Linga 					  dev_name(dev));
1984930fbf4SPavan Kumar Linga 	if (!adapter->mbx_wq) {
1994930fbf4SPavan Kumar Linga 		dev_err(dev, "Failed to allocate mailbox workqueue\n");
2004930fbf4SPavan Kumar Linga 		err = -ENOMEM;
2014930fbf4SPavan Kumar Linga 		goto err_mbx_wq_alloc;
2024930fbf4SPavan Kumar Linga 	}
2034930fbf4SPavan Kumar Linga 
204a251eee6SJoshua Hay 	adapter->stats_wq = alloc_workqueue("%s-%s-stats", 0, 0,
205a251eee6SJoshua Hay 					    dev_driver_string(dev),
206a251eee6SJoshua Hay 					    dev_name(dev));
207a251eee6SJoshua Hay 	if (!adapter->stats_wq) {
208a251eee6SJoshua Hay 		dev_err(dev, "Failed to allocate workqueue\n");
209a251eee6SJoshua Hay 		err = -ENOMEM;
210a251eee6SJoshua Hay 		goto err_stats_wq_alloc;
211a251eee6SJoshua Hay 	}
212a251eee6SJoshua Hay 
2138077c727SJoshua Hay 	adapter->vc_event_wq = alloc_workqueue("%s-%s-vc_event", 0, 0,
2148077c727SJoshua Hay 					       dev_driver_string(dev),
2158077c727SJoshua Hay 					       dev_name(dev));
2168077c727SJoshua Hay 	if (!adapter->vc_event_wq) {
2178077c727SJoshua Hay 		dev_err(dev, "Failed to allocate virtchnl event workqueue\n");
2188077c727SJoshua Hay 		err = -ENOMEM;
2194930fbf4SPavan Kumar Linga 		goto err_vc_event_wq_alloc;
2208077c727SJoshua Hay 	}
2218077c727SJoshua Hay 
222e850efedSPhani Burra 	/* setup msglvl */
223e850efedSPhani Burra 	adapter->msg_enable = netif_msg_init(-1, IDPF_AVAIL_NETIF_M);
224e850efedSPhani Burra 
225e850efedSPhani Burra 	err = idpf_cfg_hw(adapter);
226e850efedSPhani Burra 	if (err) {
227e850efedSPhani Burra 		dev_err(dev, "Failed to configure HW structure for adapter: %d\n",
228e850efedSPhani Burra 			err);
2298077c727SJoshua Hay 		goto err_cfg_hw;
230e850efedSPhani Burra 	}
231e850efedSPhani Burra 
2328077c727SJoshua Hay 	mutex_init(&adapter->vport_ctrl_lock);
2334930fbf4SPavan Kumar Linga 	mutex_init(&adapter->vector_lock);
2340fe45467SPavan Kumar Linga 	mutex_init(&adapter->queue_lock);
2354930fbf4SPavan Kumar Linga 	mutex_init(&adapter->vc_buf_lock);
2368077c727SJoshua Hay 
2370fe45467SPavan Kumar Linga 	INIT_DELAYED_WORK(&adapter->init_task, idpf_init_task);
2384930fbf4SPavan Kumar Linga 	INIT_DELAYED_WORK(&adapter->serv_task, idpf_service_task);
2394930fbf4SPavan Kumar Linga 	INIT_DELAYED_WORK(&adapter->mbx_task, idpf_mbx_task);
240a251eee6SJoshua Hay 	INIT_DELAYED_WORK(&adapter->stats_task, idpf_statistics_task);
2418077c727SJoshua Hay 	INIT_DELAYED_WORK(&adapter->vc_event_task, idpf_vc_event_task);
2428077c727SJoshua Hay 
2438077c727SJoshua Hay 	adapter->dev_ops.reg_ops.reset_reg_init(adapter);
2448077c727SJoshua Hay 	set_bit(IDPF_HR_DRV_LOAD, adapter->flags);
2458077c727SJoshua Hay 	queue_delayed_work(adapter->vc_event_wq, &adapter->vc_event_task,
2468077c727SJoshua Hay 			   msecs_to_jiffies(10 * (pdev->devfn & 0x07)));
2478077c727SJoshua Hay 
248e850efedSPhani Burra 	return 0;
249e850efedSPhani Burra 
2508077c727SJoshua Hay err_cfg_hw:
2518077c727SJoshua Hay 	destroy_workqueue(adapter->vc_event_wq);
2524930fbf4SPavan Kumar Linga err_vc_event_wq_alloc:
253a251eee6SJoshua Hay 	destroy_workqueue(adapter->stats_wq);
254a251eee6SJoshua Hay err_stats_wq_alloc:
2554930fbf4SPavan Kumar Linga 	destroy_workqueue(adapter->mbx_wq);
2564930fbf4SPavan Kumar Linga err_mbx_wq_alloc:
2574930fbf4SPavan Kumar Linga 	destroy_workqueue(adapter->serv_wq);
2580fe45467SPavan Kumar Linga err_serv_wq_alloc:
2590fe45467SPavan Kumar Linga 	destroy_workqueue(adapter->init_wq);
260e850efedSPhani Burra err_free:
261e850efedSPhani Burra 	kfree(adapter);
262e850efedSPhani Burra 	return err;
263e850efedSPhani Burra }
264e850efedSPhani Burra 
265e850efedSPhani Burra /* idpf_pci_tbl - PCI Dev idpf ID Table
266e850efedSPhani Burra  */
267e850efedSPhani Burra static const struct pci_device_id idpf_pci_tbl[] = {
268e850efedSPhani Burra 	{ PCI_VDEVICE(INTEL, IDPF_DEV_ID_PF)},
269e850efedSPhani Burra 	{ PCI_VDEVICE(INTEL, IDPF_DEV_ID_VF)},
270e850efedSPhani Burra 	{ /* Sentinel */ }
271e850efedSPhani Burra };
272e850efedSPhani Burra MODULE_DEVICE_TABLE(pci, idpf_pci_tbl);
273e850efedSPhani Burra 
274e850efedSPhani Burra static struct pci_driver idpf_driver = {
275e850efedSPhani Burra 	.name			= KBUILD_MODNAME,
276e850efedSPhani Burra 	.id_table		= idpf_pci_tbl,
277e850efedSPhani Burra 	.probe			= idpf_probe,
278a251eee6SJoshua Hay 	.sriov_configure	= idpf_sriov_configure,
279e850efedSPhani Burra 	.remove			= idpf_remove,
280e850efedSPhani Burra 	.shutdown		= idpf_shutdown,
281e850efedSPhani Burra };
282e850efedSPhani Burra module_pci_driver(idpf_driver);
283