Linux Audio

Check our new training course

Loading...
v6.13.7
  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
 10MODULE_DESCRIPTION(DRV_SUMMARY);
 11MODULE_IMPORT_NS("LIBETH");
 12MODULE_LICENSE("GPL");
 13
 14/**
 15 * idpf_remove - Device removal routine
 16 * @pdev: PCI device information struct
 17 */
 18static 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
 57destroy_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 */
 88static void idpf_shutdown(struct pci_dev *pdev)
 89{
 90	idpf_remove(pdev);
 91
 92	if (system_state == SYSTEM_POWER_OFF)
 93		pci_set_power_state(pdev, PCI_D3hot);
 94}
 95
 96/**
 97 * idpf_cfg_hw - Initialize HW struct
 98 * @adapter: adapter to setup hw struct for
 99 *
100 * Returns 0 on success, negative on failure
101 */
102static int idpf_cfg_hw(struct idpf_adapter *adapter)
103{
104	struct pci_dev *pdev = adapter->pdev;
105	struct idpf_hw *hw = &adapter->hw;
106
107	hw->hw_addr = pcim_iomap_table(pdev)[0];
108	if (!hw->hw_addr) {
109		pci_err(pdev, "failed to allocate PCI iomap table\n");
110
111		return -ENOMEM;
112	}
113
114	hw->back = adapter;
115
116	return 0;
117}
118
119/**
120 * idpf_probe - Device initialization routine
121 * @pdev: PCI device information struct
122 * @ent: entry in idpf_pci_tbl
123 *
124 * Returns 0 on success, negative on failure
125 */
126static int idpf_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
127{
128	struct device *dev = &pdev->dev;
129	struct idpf_adapter *adapter;
130	int err;
131
132	adapter = kzalloc(sizeof(*adapter), GFP_KERNEL);
133	if (!adapter)
134		return -ENOMEM;
135
136	adapter->req_tx_splitq = true;
137	adapter->req_rx_splitq = true;
138
139	switch (ent->device) {
140	case IDPF_DEV_ID_PF:
141		idpf_dev_ops_init(adapter);
142		break;
143	case IDPF_DEV_ID_VF:
144		idpf_vf_dev_ops_init(adapter);
145		adapter->crc_enable = true;
146		break;
147	default:
148		err = -ENODEV;
149		dev_err(&pdev->dev, "Unexpected dev ID 0x%x in idpf probe\n",
150			ent->device);
151		goto err_free;
152	}
153
154	adapter->pdev = pdev;
155	err = pcim_enable_device(pdev);
156	if (err)
157		goto err_free;
158
159	err = pcim_iomap_regions(pdev, BIT(0), pci_name(pdev));
160	if (err) {
161		pci_err(pdev, "pcim_iomap_regions failed %pe\n", ERR_PTR(err));
162
163		goto err_free;
164	}
165
166	/* set up for high or low dma */
167	err = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64));
168	if (err) {
169		pci_err(pdev, "DMA configuration failed: %pe\n", ERR_PTR(err));
170
171		goto err_free;
172	}
173
174	pci_set_master(pdev);
175	pci_set_drvdata(pdev, adapter);
176
177	adapter->init_wq = alloc_workqueue("%s-%s-init",
178					   WQ_UNBOUND | WQ_MEM_RECLAIM, 0,
179					   dev_driver_string(dev),
180					   dev_name(dev));
181	if (!adapter->init_wq) {
182		dev_err(dev, "Failed to allocate init workqueue\n");
183		err = -ENOMEM;
184		goto err_free;
185	}
186
187	adapter->serv_wq = alloc_workqueue("%s-%s-service",
188					   WQ_UNBOUND | WQ_MEM_RECLAIM, 0,
189					   dev_driver_string(dev),
190					   dev_name(dev));
191	if (!adapter->serv_wq) {
192		dev_err(dev, "Failed to allocate service workqueue\n");
193		err = -ENOMEM;
194		goto err_serv_wq_alloc;
195	}
196
197	adapter->mbx_wq = alloc_workqueue("%s-%s-mbx",
198					  WQ_UNBOUND | WQ_MEM_RECLAIM, 0,
199					  dev_driver_string(dev),
200					  dev_name(dev));
201	if (!adapter->mbx_wq) {
202		dev_err(dev, "Failed to allocate mailbox workqueue\n");
203		err = -ENOMEM;
204		goto err_mbx_wq_alloc;
205	}
206
207	adapter->stats_wq = alloc_workqueue("%s-%s-stats",
208					    WQ_UNBOUND | WQ_MEM_RECLAIM, 0,
209					    dev_driver_string(dev),
210					    dev_name(dev));
211	if (!adapter->stats_wq) {
212		dev_err(dev, "Failed to allocate workqueue\n");
213		err = -ENOMEM;
214		goto err_stats_wq_alloc;
215	}
216
217	adapter->vc_event_wq = alloc_workqueue("%s-%s-vc_event",
218					       WQ_UNBOUND | WQ_MEM_RECLAIM, 0,
219					       dev_driver_string(dev),
220					       dev_name(dev));
221	if (!adapter->vc_event_wq) {
222		dev_err(dev, "Failed to allocate virtchnl event workqueue\n");
223		err = -ENOMEM;
224		goto err_vc_event_wq_alloc;
225	}
226
227	/* setup msglvl */
228	adapter->msg_enable = netif_msg_init(-1, IDPF_AVAIL_NETIF_M);
229
230	err = idpf_cfg_hw(adapter);
231	if (err) {
232		dev_err(dev, "Failed to configure HW structure for adapter: %d\n",
233			err);
234		goto err_cfg_hw;
235	}
236
237	mutex_init(&adapter->vport_ctrl_lock);
238	mutex_init(&adapter->vector_lock);
239	mutex_init(&adapter->queue_lock);
240	mutex_init(&adapter->vc_buf_lock);
 
 
241
242	INIT_DELAYED_WORK(&adapter->init_task, idpf_init_task);
243	INIT_DELAYED_WORK(&adapter->serv_task, idpf_service_task);
244	INIT_DELAYED_WORK(&adapter->mbx_task, idpf_mbx_task);
245	INIT_DELAYED_WORK(&adapter->stats_task, idpf_statistics_task);
246	INIT_DELAYED_WORK(&adapter->vc_event_task, idpf_vc_event_task);
247
248	adapter->dev_ops.reg_ops.reset_reg_init(adapter);
249	set_bit(IDPF_HR_DRV_LOAD, adapter->flags);
250	queue_delayed_work(adapter->vc_event_wq, &adapter->vc_event_task,
251			   msecs_to_jiffies(10 * (pdev->devfn & 0x07)));
252
253	return 0;
254
255err_cfg_hw:
256	destroy_workqueue(adapter->vc_event_wq);
257err_vc_event_wq_alloc:
258	destroy_workqueue(adapter->stats_wq);
259err_stats_wq_alloc:
260	destroy_workqueue(adapter->mbx_wq);
261err_mbx_wq_alloc:
262	destroy_workqueue(adapter->serv_wq);
263err_serv_wq_alloc:
264	destroy_workqueue(adapter->init_wq);
265err_free:
266	kfree(adapter);
267	return err;
268}
269
270/* idpf_pci_tbl - PCI Dev idpf ID Table
271 */
272static const struct pci_device_id idpf_pci_tbl[] = {
273	{ PCI_VDEVICE(INTEL, IDPF_DEV_ID_PF)},
274	{ PCI_VDEVICE(INTEL, IDPF_DEV_ID_VF)},
275	{ /* Sentinel */ }
276};
277MODULE_DEVICE_TABLE(pci, idpf_pci_tbl);
278
279static struct pci_driver idpf_driver = {
280	.name			= KBUILD_MODNAME,
281	.id_table		= idpf_pci_tbl,
282	.probe			= idpf_probe,
283	.sriov_configure	= idpf_sriov_configure,
284	.remove			= idpf_remove,
285	.shutdown		= idpf_shutdown,
286};
287module_pci_driver(idpf_driver);
v6.8
  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
  9MODULE_DESCRIPTION(DRV_SUMMARY);
 
 10MODULE_LICENSE("GPL");
 11
 12/**
 13 * idpf_remove - Device removal routine
 14 * @pdev: PCI device information struct
 15 */
 16static 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
 54destroy_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 */
 83static 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 */
 97static 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 */
121static 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
247err_cfg_hw:
248	destroy_workqueue(adapter->vc_event_wq);
249err_vc_event_wq_alloc:
250	destroy_workqueue(adapter->stats_wq);
251err_stats_wq_alloc:
252	destroy_workqueue(adapter->mbx_wq);
253err_mbx_wq_alloc:
254	destroy_workqueue(adapter->serv_wq);
255err_serv_wq_alloc:
256	destroy_workqueue(adapter->init_wq);
257err_free:
258	kfree(adapter);
259	return err;
260}
261
262/* idpf_pci_tbl - PCI Dev idpf ID Table
263 */
264static 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};
269MODULE_DEVICE_TABLE(pci, idpf_pci_tbl);
270
271static 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};
279module_pci_driver(idpf_driver);