xref: /openbmc/linux/drivers/vdpa/ifcvf/ifcvf_main.c (revision 14474950)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Intel IFC VF NIC driver for virtio dataplane offloading
4  *
5  * Copyright (C) 2020 Intel Corporation.
6  *
7  * Author: Zhu Lingshan <lingshan.zhu@intel.com>
8  *
9  */
10 
11 #include <linux/interrupt.h>
12 #include <linux/module.h>
13 #include <linux/pci.h>
14 #include <linux/sysfs.h>
15 #include "ifcvf_base.h"
16 
17 #define VERSION_STRING  "0.1"
18 #define DRIVER_AUTHOR   "Intel Corporation"
19 #define IFCVF_DRIVER_NAME       "ifcvf"
20 
21 static irqreturn_t ifcvf_config_changed(int irq, void *arg)
22 {
23 	struct ifcvf_hw *vf = arg;
24 
25 	if (vf->config_cb.callback)
26 		return vf->config_cb.callback(vf->config_cb.private);
27 
28 	return IRQ_HANDLED;
29 }
30 
31 static irqreturn_t ifcvf_intr_handler(int irq, void *arg)
32 {
33 	struct vring_info *vring = arg;
34 
35 	if (vring->cb.callback)
36 		return vring->cb.callback(vring->cb.private);
37 
38 	return IRQ_HANDLED;
39 }
40 
41 static void ifcvf_free_irq_vectors(void *data)
42 {
43 	pci_free_irq_vectors(data);
44 }
45 
46 static void ifcvf_free_irq(struct ifcvf_adapter *adapter, int queues)
47 {
48 	struct pci_dev *pdev = adapter->pdev;
49 	struct ifcvf_hw *vf = &adapter->vf;
50 	int i;
51 
52 
53 	for (i = 0; i < queues; i++)
54 		devm_free_irq(&pdev->dev, vf->vring[i].irq, &vf->vring[i]);
55 
56 	ifcvf_free_irq_vectors(pdev);
57 }
58 
59 static int ifcvf_request_irq(struct ifcvf_adapter *adapter)
60 {
61 	struct pci_dev *pdev = adapter->pdev;
62 	struct ifcvf_hw *vf = &adapter->vf;
63 	int vector, i, ret, irq;
64 
65 	ret = pci_alloc_irq_vectors(pdev, IFCVF_MAX_INTR,
66 				    IFCVF_MAX_INTR, PCI_IRQ_MSIX);
67 	if (ret < 0) {
68 		IFCVF_ERR(pdev, "Failed to alloc IRQ vectors\n");
69 		return ret;
70 	}
71 
72 	snprintf(vf->config_msix_name, 256, "ifcvf[%s]-config\n",
73 		 pci_name(pdev));
74 	vector = 0;
75 	irq = pci_irq_vector(pdev, vector);
76 	ret = devm_request_irq(&pdev->dev, irq,
77 			       ifcvf_config_changed, 0,
78 			       vf->config_msix_name, vf);
79 
80 	for (i = 0; i < IFCVF_MAX_QUEUE_PAIRS * 2; i++) {
81 		snprintf(vf->vring[i].msix_name, 256, "ifcvf[%s]-%d\n",
82 			 pci_name(pdev), i);
83 		vector = i + IFCVF_MSI_QUEUE_OFF;
84 		irq = pci_irq_vector(pdev, vector);
85 		ret = devm_request_irq(&pdev->dev, irq,
86 				       ifcvf_intr_handler, 0,
87 				       vf->vring[i].msix_name,
88 				       &vf->vring[i]);
89 		if (ret) {
90 			IFCVF_ERR(pdev,
91 				  "Failed to request irq for vq %d\n", i);
92 			ifcvf_free_irq(adapter, i);
93 
94 			return ret;
95 		}
96 
97 		vf->vring[i].irq = irq;
98 	}
99 
100 	return 0;
101 }
102 
103 static int ifcvf_start_datapath(void *private)
104 {
105 	struct ifcvf_hw *vf = ifcvf_private_to_vf(private);
106 	u8 status;
107 	int ret;
108 
109 	vf->nr_vring = IFCVF_MAX_QUEUE_PAIRS * 2;
110 	ret = ifcvf_start_hw(vf);
111 	if (ret < 0) {
112 		status = ifcvf_get_status(vf);
113 		status |= VIRTIO_CONFIG_S_FAILED;
114 		ifcvf_set_status(vf, status);
115 	}
116 
117 	return ret;
118 }
119 
120 static int ifcvf_stop_datapath(void *private)
121 {
122 	struct ifcvf_hw *vf = ifcvf_private_to_vf(private);
123 	int i;
124 
125 	for (i = 0; i < IFCVF_MAX_QUEUE_PAIRS * 2; i++)
126 		vf->vring[i].cb.callback = NULL;
127 
128 	ifcvf_stop_hw(vf);
129 
130 	return 0;
131 }
132 
133 static void ifcvf_reset_vring(struct ifcvf_adapter *adapter)
134 {
135 	struct ifcvf_hw *vf = ifcvf_private_to_vf(adapter);
136 	int i;
137 
138 	for (i = 0; i < IFCVF_MAX_QUEUE_PAIRS * 2; i++) {
139 		vf->vring[i].last_avail_idx = 0;
140 		vf->vring[i].desc = 0;
141 		vf->vring[i].avail = 0;
142 		vf->vring[i].used = 0;
143 		vf->vring[i].ready = 0;
144 		vf->vring[i].cb.callback = NULL;
145 		vf->vring[i].cb.private = NULL;
146 	}
147 
148 	ifcvf_reset(vf);
149 }
150 
151 static struct ifcvf_adapter *vdpa_to_adapter(struct vdpa_device *vdpa_dev)
152 {
153 	return container_of(vdpa_dev, struct ifcvf_adapter, vdpa);
154 }
155 
156 static struct ifcvf_hw *vdpa_to_vf(struct vdpa_device *vdpa_dev)
157 {
158 	struct ifcvf_adapter *adapter = vdpa_to_adapter(vdpa_dev);
159 
160 	return &adapter->vf;
161 }
162 
163 static u64 ifcvf_vdpa_get_features(struct vdpa_device *vdpa_dev)
164 {
165 	struct ifcvf_hw *vf = vdpa_to_vf(vdpa_dev);
166 	u64 features;
167 
168 	features = ifcvf_get_features(vf) & IFCVF_SUPPORTED_FEATURES;
169 
170 	return features;
171 }
172 
173 static int ifcvf_vdpa_set_features(struct vdpa_device *vdpa_dev, u64 features)
174 {
175 	struct ifcvf_hw *vf = vdpa_to_vf(vdpa_dev);
176 
177 	vf->req_features = features;
178 
179 	return 0;
180 }
181 
182 static u8 ifcvf_vdpa_get_status(struct vdpa_device *vdpa_dev)
183 {
184 	struct ifcvf_hw *vf = vdpa_to_vf(vdpa_dev);
185 
186 	return ifcvf_get_status(vf);
187 }
188 
189 static void ifcvf_vdpa_set_status(struct vdpa_device *vdpa_dev, u8 status)
190 {
191 	struct ifcvf_adapter *adapter;
192 	struct ifcvf_hw *vf;
193 	u8 status_old;
194 	int ret;
195 
196 	vf  = vdpa_to_vf(vdpa_dev);
197 	adapter = dev_get_drvdata(vdpa_dev->dev.parent);
198 	status_old = ifcvf_get_status(vf);
199 
200 	if (status_old == status)
201 		return;
202 
203 	if ((status_old & VIRTIO_CONFIG_S_DRIVER_OK) &&
204 	    !(status & VIRTIO_CONFIG_S_DRIVER_OK)) {
205 		ifcvf_stop_datapath(adapter);
206 		ifcvf_free_irq(adapter, IFCVF_MAX_QUEUE_PAIRS * 2);
207 	}
208 
209 	if (status == 0) {
210 		ifcvf_reset_vring(adapter);
211 		return;
212 	}
213 
214 	if ((status & VIRTIO_CONFIG_S_DRIVER_OK) &&
215 	    !(status_old & VIRTIO_CONFIG_S_DRIVER_OK)) {
216 		ret = ifcvf_request_irq(adapter);
217 		if (ret) {
218 			status = ifcvf_get_status(vf);
219 			status |= VIRTIO_CONFIG_S_FAILED;
220 			ifcvf_set_status(vf, status);
221 			return;
222 		}
223 
224 		if (ifcvf_start_datapath(adapter) < 0)
225 			IFCVF_ERR(adapter->pdev,
226 				  "Failed to set ifcvf vdpa  status %u\n",
227 				  status);
228 	}
229 
230 	ifcvf_set_status(vf, status);
231 }
232 
233 static u16 ifcvf_vdpa_get_vq_num_max(struct vdpa_device *vdpa_dev)
234 {
235 	return IFCVF_QUEUE_MAX;
236 }
237 
238 static u64 ifcvf_vdpa_get_vq_state(struct vdpa_device *vdpa_dev, u16 qid)
239 {
240 	struct ifcvf_hw *vf = vdpa_to_vf(vdpa_dev);
241 
242 	return ifcvf_get_vq_state(vf, qid);
243 }
244 
245 static int ifcvf_vdpa_set_vq_state(struct vdpa_device *vdpa_dev, u16 qid,
246 				   u64 num)
247 {
248 	struct ifcvf_hw *vf = vdpa_to_vf(vdpa_dev);
249 
250 	return ifcvf_set_vq_state(vf, qid, num);
251 }
252 
253 static void ifcvf_vdpa_set_vq_cb(struct vdpa_device *vdpa_dev, u16 qid,
254 				 struct vdpa_callback *cb)
255 {
256 	struct ifcvf_hw *vf = vdpa_to_vf(vdpa_dev);
257 
258 	vf->vring[qid].cb = *cb;
259 }
260 
261 static void ifcvf_vdpa_set_vq_ready(struct vdpa_device *vdpa_dev,
262 				    u16 qid, bool ready)
263 {
264 	struct ifcvf_hw *vf = vdpa_to_vf(vdpa_dev);
265 
266 	vf->vring[qid].ready = ready;
267 }
268 
269 static bool ifcvf_vdpa_get_vq_ready(struct vdpa_device *vdpa_dev, u16 qid)
270 {
271 	struct ifcvf_hw *vf = vdpa_to_vf(vdpa_dev);
272 
273 	return vf->vring[qid].ready;
274 }
275 
276 static void ifcvf_vdpa_set_vq_num(struct vdpa_device *vdpa_dev, u16 qid,
277 				  u32 num)
278 {
279 	struct ifcvf_hw *vf = vdpa_to_vf(vdpa_dev);
280 
281 	vf->vring[qid].size = num;
282 }
283 
284 static int ifcvf_vdpa_set_vq_address(struct vdpa_device *vdpa_dev, u16 qid,
285 				     u64 desc_area, u64 driver_area,
286 				     u64 device_area)
287 {
288 	struct ifcvf_hw *vf = vdpa_to_vf(vdpa_dev);
289 
290 	vf->vring[qid].desc = desc_area;
291 	vf->vring[qid].avail = driver_area;
292 	vf->vring[qid].used = device_area;
293 
294 	return 0;
295 }
296 
297 static void ifcvf_vdpa_kick_vq(struct vdpa_device *vdpa_dev, u16 qid)
298 {
299 	struct ifcvf_hw *vf = vdpa_to_vf(vdpa_dev);
300 
301 	ifcvf_notify_queue(vf, qid);
302 }
303 
304 static u32 ifcvf_vdpa_get_generation(struct vdpa_device *vdpa_dev)
305 {
306 	struct ifcvf_hw *vf = vdpa_to_vf(vdpa_dev);
307 
308 	return ioread8(&vf->common_cfg->config_generation);
309 }
310 
311 static u32 ifcvf_vdpa_get_device_id(struct vdpa_device *vdpa_dev)
312 {
313 	return VIRTIO_ID_NET;
314 }
315 
316 static u32 ifcvf_vdpa_get_vendor_id(struct vdpa_device *vdpa_dev)
317 {
318 	return IFCVF_SUBSYS_VENDOR_ID;
319 }
320 
321 static u32 ifcvf_vdpa_get_vq_align(struct vdpa_device *vdpa_dev)
322 {
323 	return IFCVF_QUEUE_ALIGNMENT;
324 }
325 
326 static void ifcvf_vdpa_get_config(struct vdpa_device *vdpa_dev,
327 				  unsigned int offset,
328 				  void *buf, unsigned int len)
329 {
330 	struct ifcvf_hw *vf = vdpa_to_vf(vdpa_dev);
331 
332 	WARN_ON(offset + len > sizeof(struct virtio_net_config));
333 	ifcvf_read_net_config(vf, offset, buf, len);
334 }
335 
336 static void ifcvf_vdpa_set_config(struct vdpa_device *vdpa_dev,
337 				  unsigned int offset, const void *buf,
338 				  unsigned int len)
339 {
340 	struct ifcvf_hw *vf = vdpa_to_vf(vdpa_dev);
341 
342 	WARN_ON(offset + len > sizeof(struct virtio_net_config));
343 	ifcvf_write_net_config(vf, offset, buf, len);
344 }
345 
346 static void ifcvf_vdpa_set_config_cb(struct vdpa_device *vdpa_dev,
347 				     struct vdpa_callback *cb)
348 {
349 	struct ifcvf_hw *vf = vdpa_to_vf(vdpa_dev);
350 
351 	vf->config_cb.callback = cb->callback;
352 	vf->config_cb.private = cb->private;
353 }
354 
355 /*
356  * IFCVF currently does't have on-chip IOMMU, so not
357  * implemented set_map()/dma_map()/dma_unmap()
358  */
359 static const struct vdpa_config_ops ifc_vdpa_ops = {
360 	.get_features	= ifcvf_vdpa_get_features,
361 	.set_features	= ifcvf_vdpa_set_features,
362 	.get_status	= ifcvf_vdpa_get_status,
363 	.set_status	= ifcvf_vdpa_set_status,
364 	.get_vq_num_max	= ifcvf_vdpa_get_vq_num_max,
365 	.get_vq_state	= ifcvf_vdpa_get_vq_state,
366 	.set_vq_state	= ifcvf_vdpa_set_vq_state,
367 	.set_vq_cb	= ifcvf_vdpa_set_vq_cb,
368 	.set_vq_ready	= ifcvf_vdpa_set_vq_ready,
369 	.get_vq_ready	= ifcvf_vdpa_get_vq_ready,
370 	.set_vq_num	= ifcvf_vdpa_set_vq_num,
371 	.set_vq_address	= ifcvf_vdpa_set_vq_address,
372 	.kick_vq	= ifcvf_vdpa_kick_vq,
373 	.get_generation	= ifcvf_vdpa_get_generation,
374 	.get_device_id	= ifcvf_vdpa_get_device_id,
375 	.get_vendor_id	= ifcvf_vdpa_get_vendor_id,
376 	.get_vq_align	= ifcvf_vdpa_get_vq_align,
377 	.get_config	= ifcvf_vdpa_get_config,
378 	.set_config	= ifcvf_vdpa_set_config,
379 	.set_config_cb  = ifcvf_vdpa_set_config_cb,
380 };
381 
382 static int ifcvf_probe(struct pci_dev *pdev, const struct pci_device_id *id)
383 {
384 	struct device *dev = &pdev->dev;
385 	struct ifcvf_adapter *adapter;
386 	struct ifcvf_hw *vf;
387 	int ret;
388 
389 	ret = pcim_enable_device(pdev);
390 	if (ret) {
391 		IFCVF_ERR(pdev, "Failed to enable device\n");
392 		return ret;
393 	}
394 
395 	ret = pcim_iomap_regions(pdev, BIT(0) | BIT(2) | BIT(4),
396 				 IFCVF_DRIVER_NAME);
397 	if (ret) {
398 		IFCVF_ERR(pdev, "Failed to request MMIO region\n");
399 		return ret;
400 	}
401 
402 	ret = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
403 	if (ret) {
404 		IFCVF_ERR(pdev, "No usable DMA confiugration\n");
405 		return ret;
406 	}
407 
408 	ret = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
409 	if (ret) {
410 		IFCVF_ERR(pdev,
411 			  "No usable coherent DMA confiugration\n");
412 		return ret;
413 	}
414 
415 	ret = devm_add_action_or_reset(dev, ifcvf_free_irq_vectors, pdev);
416 	if (ret) {
417 		IFCVF_ERR(pdev,
418 			  "Failed for adding devres for freeing irq vectors\n");
419 		return ret;
420 	}
421 
422 	adapter = vdpa_alloc_device(struct ifcvf_adapter, vdpa,
423 				    dev, &ifc_vdpa_ops);
424 	if (adapter == NULL) {
425 		IFCVF_ERR(pdev, "Failed to allocate vDPA structure");
426 		return -ENOMEM;
427 	}
428 
429 	pci_set_master(pdev);
430 	pci_set_drvdata(pdev, adapter);
431 
432 	vf = &adapter->vf;
433 	vf->base = pcim_iomap_table(pdev);
434 
435 	adapter->pdev = pdev;
436 	adapter->vdpa.dma_dev = &pdev->dev;
437 
438 	ret = ifcvf_init_hw(vf, pdev);
439 	if (ret) {
440 		IFCVF_ERR(pdev, "Failed to init IFCVF hw\n");
441 		goto err;
442 	}
443 
444 	ret = vdpa_register_device(&adapter->vdpa);
445 	if (ret) {
446 		IFCVF_ERR(pdev, "Failed to register ifcvf to vdpa bus");
447 		goto err;
448 	}
449 
450 	return 0;
451 
452 err:
453 	put_device(&adapter->vdpa.dev);
454 	return ret;
455 }
456 
457 static void ifcvf_remove(struct pci_dev *pdev)
458 {
459 	struct ifcvf_adapter *adapter = pci_get_drvdata(pdev);
460 
461 	vdpa_unregister_device(&adapter->vdpa);
462 }
463 
464 static struct pci_device_id ifcvf_pci_ids[] = {
465 	{ PCI_DEVICE_SUB(IFCVF_VENDOR_ID,
466 		IFCVF_DEVICE_ID,
467 		IFCVF_SUBSYS_VENDOR_ID,
468 		IFCVF_SUBSYS_DEVICE_ID) },
469 	{ 0 },
470 };
471 MODULE_DEVICE_TABLE(pci, ifcvf_pci_ids);
472 
473 static struct pci_driver ifcvf_driver = {
474 	.name     = IFCVF_DRIVER_NAME,
475 	.id_table = ifcvf_pci_ids,
476 	.probe    = ifcvf_probe,
477 	.remove   = ifcvf_remove,
478 };
479 
480 module_pci_driver(ifcvf_driver);
481 
482 MODULE_LICENSE("GPL v2");
483 MODULE_VERSION(VERSION_STRING);
484