blob: 3f788794571a535a86e16b98d605df8db75db8e0 [file] [log] [blame]
Wu Zongyonge85087b2021-10-29 17:14:49 +08001// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * vDPA bridge driver for Alibaba ENI(Elastic Network Interface)
4 *
5 * Copyright (c) 2021, Alibaba Inc. All rights reserved.
6 * Author: Wu Zongyong <wuzongyong@linux.alibaba.com>
7 *
8 */
9
10#include "linux/bits.h"
11#include <linux/interrupt.h>
12#include <linux/module.h>
13#include <linux/pci.h>
14#include <linux/vdpa.h>
15#include <linux/virtio.h>
16#include <linux/virtio_config.h>
17#include <linux/virtio_ring.h>
18#include <linux/virtio_pci.h>
19#include <linux/virtio_pci_legacy.h>
20#include <uapi/linux/virtio_net.h>
21
22#define ENI_MSIX_NAME_SIZE 256
23
24#define ENI_ERR(pdev, fmt, ...) \
25 dev_err(&pdev->dev, "%s"fmt, "eni_vdpa: ", ##__VA_ARGS__)
26#define ENI_DBG(pdev, fmt, ...) \
27 dev_dbg(&pdev->dev, "%s"fmt, "eni_vdpa: ", ##__VA_ARGS__)
28#define ENI_INFO(pdev, fmt, ...) \
29 dev_info(&pdev->dev, "%s"fmt, "eni_vdpa: ", ##__VA_ARGS__)
30
31struct eni_vring {
32 void __iomem *notify;
33 char msix_name[ENI_MSIX_NAME_SIZE];
34 struct vdpa_callback cb;
35 int irq;
36};
37
38struct eni_vdpa {
39 struct vdpa_device vdpa;
40 struct virtio_pci_legacy_device ldev;
41 struct eni_vring *vring;
42 struct vdpa_callback config_cb;
43 char msix_name[ENI_MSIX_NAME_SIZE];
44 int config_irq;
45 int queues;
46 int vectors;
47};
48
49static struct eni_vdpa *vdpa_to_eni(struct vdpa_device *vdpa)
50{
51 return container_of(vdpa, struct eni_vdpa, vdpa);
52}
53
54static struct virtio_pci_legacy_device *vdpa_to_ldev(struct vdpa_device *vdpa)
55{
56 struct eni_vdpa *eni_vdpa = vdpa_to_eni(vdpa);
57
58 return &eni_vdpa->ldev;
59}
60
61static u64 eni_vdpa_get_features(struct vdpa_device *vdpa)
62{
63 struct virtio_pci_legacy_device *ldev = vdpa_to_ldev(vdpa);
64 u64 features = vp_legacy_get_features(ldev);
65
66 features |= BIT_ULL(VIRTIO_F_ACCESS_PLATFORM);
67 features |= BIT_ULL(VIRTIO_F_ORDER_PLATFORM);
68
69 return features;
70}
71
72static int eni_vdpa_set_features(struct vdpa_device *vdpa, u64 features)
73{
74 struct virtio_pci_legacy_device *ldev = vdpa_to_ldev(vdpa);
75
76 if (!(features & BIT_ULL(VIRTIO_NET_F_MRG_RXBUF)) && features) {
77 ENI_ERR(ldev->pci_dev,
78 "VIRTIO_NET_F_MRG_RXBUF is not negotiated\n");
79 return -EINVAL;
80 }
81
82 vp_legacy_set_features(ldev, (u32)features);
83
84 return 0;
85}
86
87static u8 eni_vdpa_get_status(struct vdpa_device *vdpa)
88{
89 struct virtio_pci_legacy_device *ldev = vdpa_to_ldev(vdpa);
90
91 return vp_legacy_get_status(ldev);
92}
93
94static int eni_vdpa_get_vq_irq(struct vdpa_device *vdpa, u16 idx)
95{
96 struct eni_vdpa *eni_vdpa = vdpa_to_eni(vdpa);
97 int irq = eni_vdpa->vring[idx].irq;
98
99 if (irq == VIRTIO_MSI_NO_VECTOR)
100 return -EINVAL;
101
102 return irq;
103}
104
105static void eni_vdpa_free_irq(struct eni_vdpa *eni_vdpa)
106{
107 struct virtio_pci_legacy_device *ldev = &eni_vdpa->ldev;
108 struct pci_dev *pdev = ldev->pci_dev;
109 int i;
110
111 for (i = 0; i < eni_vdpa->queues; i++) {
112 if (eni_vdpa->vring[i].irq != VIRTIO_MSI_NO_VECTOR) {
113 vp_legacy_queue_vector(ldev, i, VIRTIO_MSI_NO_VECTOR);
114 devm_free_irq(&pdev->dev, eni_vdpa->vring[i].irq,
115 &eni_vdpa->vring[i]);
116 eni_vdpa->vring[i].irq = VIRTIO_MSI_NO_VECTOR;
117 }
118 }
119
120 if (eni_vdpa->config_irq != VIRTIO_MSI_NO_VECTOR) {
121 vp_legacy_config_vector(ldev, VIRTIO_MSI_NO_VECTOR);
122 devm_free_irq(&pdev->dev, eni_vdpa->config_irq, eni_vdpa);
123 eni_vdpa->config_irq = VIRTIO_MSI_NO_VECTOR;
124 }
125
126 if (eni_vdpa->vectors) {
127 pci_free_irq_vectors(pdev);
128 eni_vdpa->vectors = 0;
129 }
130}
131
132static irqreturn_t eni_vdpa_vq_handler(int irq, void *arg)
133{
134 struct eni_vring *vring = arg;
135
136 if (vring->cb.callback)
137 return vring->cb.callback(vring->cb.private);
138
139 return IRQ_HANDLED;
140}
141
142static irqreturn_t eni_vdpa_config_handler(int irq, void *arg)
143{
144 struct eni_vdpa *eni_vdpa = arg;
145
146 if (eni_vdpa->config_cb.callback)
147 return eni_vdpa->config_cb.callback(eni_vdpa->config_cb.private);
148
149 return IRQ_HANDLED;
150}
151
152static int eni_vdpa_request_irq(struct eni_vdpa *eni_vdpa)
153{
154 struct virtio_pci_legacy_device *ldev = &eni_vdpa->ldev;
155 struct pci_dev *pdev = ldev->pci_dev;
156 int i, ret, irq;
157 int queues = eni_vdpa->queues;
158 int vectors = queues + 1;
159
160 ret = pci_alloc_irq_vectors(pdev, vectors, vectors, PCI_IRQ_MSIX);
161 if (ret != vectors) {
162 ENI_ERR(pdev,
163 "failed to allocate irq vectors want %d but %d\n",
164 vectors, ret);
165 return ret;
166 }
167
168 eni_vdpa->vectors = vectors;
169
170 for (i = 0; i < queues; i++) {
171 snprintf(eni_vdpa->vring[i].msix_name, ENI_MSIX_NAME_SIZE,
172 "eni-vdpa[%s]-%d\n", pci_name(pdev), i);
173 irq = pci_irq_vector(pdev, i);
174 ret = devm_request_irq(&pdev->dev, irq,
175 eni_vdpa_vq_handler,
176 0, eni_vdpa->vring[i].msix_name,
177 &eni_vdpa->vring[i]);
178 if (ret) {
179 ENI_ERR(pdev, "failed to request irq for vq %d\n", i);
180 goto err;
181 }
182 vp_legacy_queue_vector(ldev, i, i);
183 eni_vdpa->vring[i].irq = irq;
184 }
185
186 snprintf(eni_vdpa->msix_name, ENI_MSIX_NAME_SIZE, "eni-vdpa[%s]-config\n",
187 pci_name(pdev));
188 irq = pci_irq_vector(pdev, queues);
189 ret = devm_request_irq(&pdev->dev, irq, eni_vdpa_config_handler, 0,
190 eni_vdpa->msix_name, eni_vdpa);
191 if (ret) {
192 ENI_ERR(pdev, "failed to request irq for config vq %d\n", i);
193 goto err;
194 }
195 vp_legacy_config_vector(ldev, queues);
196 eni_vdpa->config_irq = irq;
197
198 return 0;
199err:
200 eni_vdpa_free_irq(eni_vdpa);
201 return ret;
202}
203
204static void eni_vdpa_set_status(struct vdpa_device *vdpa, u8 status)
205{
206 struct eni_vdpa *eni_vdpa = vdpa_to_eni(vdpa);
207 struct virtio_pci_legacy_device *ldev = &eni_vdpa->ldev;
208 u8 s = eni_vdpa_get_status(vdpa);
209
210 if (status & VIRTIO_CONFIG_S_DRIVER_OK &&
211 !(s & VIRTIO_CONFIG_S_DRIVER_OK)) {
212 eni_vdpa_request_irq(eni_vdpa);
213 }
214
215 vp_legacy_set_status(ldev, status);
216
217 if (!(status & VIRTIO_CONFIG_S_DRIVER_OK) &&
218 (s & VIRTIO_CONFIG_S_DRIVER_OK))
219 eni_vdpa_free_irq(eni_vdpa);
220}
221
222static int eni_vdpa_reset(struct vdpa_device *vdpa)
223{
224 struct eni_vdpa *eni_vdpa = vdpa_to_eni(vdpa);
225 struct virtio_pci_legacy_device *ldev = &eni_vdpa->ldev;
226 u8 s = eni_vdpa_get_status(vdpa);
227
228 vp_legacy_set_status(ldev, 0);
229
230 if (s & VIRTIO_CONFIG_S_DRIVER_OK)
231 eni_vdpa_free_irq(eni_vdpa);
232
233 return 0;
234}
235
236static u16 eni_vdpa_get_vq_num_max(struct vdpa_device *vdpa)
237{
238 struct virtio_pci_legacy_device *ldev = vdpa_to_ldev(vdpa);
239
240 return vp_legacy_get_queue_size(ldev, 0);
241}
242
243static u16 eni_vdpa_get_vq_num_min(struct vdpa_device *vdpa)
244{
245 struct virtio_pci_legacy_device *ldev = vdpa_to_ldev(vdpa);
246
247 return vp_legacy_get_queue_size(ldev, 0);
248}
249
250static int eni_vdpa_get_vq_state(struct vdpa_device *vdpa, u16 qid,
251 struct vdpa_vq_state *state)
252{
253 return -EOPNOTSUPP;
254}
255
256static int eni_vdpa_set_vq_state(struct vdpa_device *vdpa, u16 qid,
257 const struct vdpa_vq_state *state)
258{
259 struct virtio_pci_legacy_device *ldev = vdpa_to_ldev(vdpa);
260 const struct vdpa_vq_state_split *split = &state->split;
261
262 /* ENI is build upon virtio-pci specfication which not support
263 * to set state of virtqueue. But if the state is equal to the
264 * device initial state by chance, we can let it go.
265 */
266 if (!vp_legacy_get_queue_enable(ldev, qid)
267 && split->avail_index == 0)
268 return 0;
269
270 return -EOPNOTSUPP;
271}
272
273
274static void eni_vdpa_set_vq_cb(struct vdpa_device *vdpa, u16 qid,
275 struct vdpa_callback *cb)
276{
277 struct eni_vdpa *eni_vdpa = vdpa_to_eni(vdpa);
278
279 eni_vdpa->vring[qid].cb = *cb;
280}
281
282static void eni_vdpa_set_vq_ready(struct vdpa_device *vdpa, u16 qid,
283 bool ready)
284{
285 struct virtio_pci_legacy_device *ldev = vdpa_to_ldev(vdpa);
286
287 /* ENI is a legacy virtio-pci device. This is not supported
288 * by specification. But we can disable virtqueue by setting
289 * address to 0.
290 */
291 if (!ready)
292 vp_legacy_set_queue_address(ldev, qid, 0);
293}
294
295static bool eni_vdpa_get_vq_ready(struct vdpa_device *vdpa, u16 qid)
296{
297 struct virtio_pci_legacy_device *ldev = vdpa_to_ldev(vdpa);
298
299 return vp_legacy_get_queue_enable(ldev, qid);
300}
301
302static void eni_vdpa_set_vq_num(struct vdpa_device *vdpa, u16 qid,
303 u32 num)
304{
305 struct virtio_pci_legacy_device *ldev = vdpa_to_ldev(vdpa);
306 struct pci_dev *pdev = ldev->pci_dev;
307 u16 n = vp_legacy_get_queue_size(ldev, qid);
308
309 /* ENI is a legacy virtio-pci device which not allow to change
310 * virtqueue size. Just report a error if someone tries to
311 * change it.
312 */
313 if (num != n)
314 ENI_ERR(pdev,
315 "not support to set vq %u fixed num %u to %u\n",
316 qid, n, num);
317}
318
319static int eni_vdpa_set_vq_address(struct vdpa_device *vdpa, u16 qid,
320 u64 desc_area, u64 driver_area,
321 u64 device_area)
322{
323 struct virtio_pci_legacy_device *ldev = vdpa_to_ldev(vdpa);
324 u32 pfn = desc_area >> VIRTIO_PCI_QUEUE_ADDR_SHIFT;
325
326 vp_legacy_set_queue_address(ldev, qid, pfn);
327
328 return 0;
329}
330
331static void eni_vdpa_kick_vq(struct vdpa_device *vdpa, u16 qid)
332{
333 struct eni_vdpa *eni_vdpa = vdpa_to_eni(vdpa);
334
335 iowrite16(qid, eni_vdpa->vring[qid].notify);
336}
337
338static u32 eni_vdpa_get_device_id(struct vdpa_device *vdpa)
339{
340 struct virtio_pci_legacy_device *ldev = vdpa_to_ldev(vdpa);
341
342 return ldev->id.device;
343}
344
345static u32 eni_vdpa_get_vendor_id(struct vdpa_device *vdpa)
346{
347 struct virtio_pci_legacy_device *ldev = vdpa_to_ldev(vdpa);
348
349 return ldev->id.vendor;
350}
351
352static u32 eni_vdpa_get_vq_align(struct vdpa_device *vdpa)
353{
354 return VIRTIO_PCI_VRING_ALIGN;
355}
356
357static size_t eni_vdpa_get_config_size(struct vdpa_device *vdpa)
358{
359 return sizeof(struct virtio_net_config);
360}
361
362
363static void eni_vdpa_get_config(struct vdpa_device *vdpa,
364 unsigned int offset,
365 void *buf, unsigned int len)
366{
367 struct eni_vdpa *eni_vdpa = vdpa_to_eni(vdpa);
368 struct virtio_pci_legacy_device *ldev = &eni_vdpa->ldev;
369 void __iomem *ioaddr = ldev->ioaddr +
370 VIRTIO_PCI_CONFIG_OFF(eni_vdpa->vectors) +
371 offset;
372 u8 *p = buf;
373 int i;
374
375 for (i = 0; i < len; i++)
376 *p++ = ioread8(ioaddr + i);
377}
378
379static void eni_vdpa_set_config(struct vdpa_device *vdpa,
380 unsigned int offset, const void *buf,
381 unsigned int len)
382{
383 struct eni_vdpa *eni_vdpa = vdpa_to_eni(vdpa);
384 struct virtio_pci_legacy_device *ldev = &eni_vdpa->ldev;
385 void __iomem *ioaddr = ldev->ioaddr +
386 VIRTIO_PCI_CONFIG_OFF(eni_vdpa->vectors) +
387 offset;
388 const u8 *p = buf;
389 int i;
390
391 for (i = 0; i < len; i++)
392 iowrite8(*p++, ioaddr + i);
393}
394
395static void eni_vdpa_set_config_cb(struct vdpa_device *vdpa,
396 struct vdpa_callback *cb)
397{
398 struct eni_vdpa *eni_vdpa = vdpa_to_eni(vdpa);
399
400 eni_vdpa->config_cb = *cb;
401}
402
403static const struct vdpa_config_ops eni_vdpa_ops = {
404 .get_features = eni_vdpa_get_features,
405 .set_features = eni_vdpa_set_features,
406 .get_status = eni_vdpa_get_status,
407 .set_status = eni_vdpa_set_status,
408 .reset = eni_vdpa_reset,
409 .get_vq_num_max = eni_vdpa_get_vq_num_max,
410 .get_vq_num_min = eni_vdpa_get_vq_num_min,
411 .get_vq_state = eni_vdpa_get_vq_state,
412 .set_vq_state = eni_vdpa_set_vq_state,
413 .set_vq_cb = eni_vdpa_set_vq_cb,
414 .set_vq_ready = eni_vdpa_set_vq_ready,
415 .get_vq_ready = eni_vdpa_get_vq_ready,
416 .set_vq_num = eni_vdpa_set_vq_num,
417 .set_vq_address = eni_vdpa_set_vq_address,
418 .kick_vq = eni_vdpa_kick_vq,
419 .get_device_id = eni_vdpa_get_device_id,
420 .get_vendor_id = eni_vdpa_get_vendor_id,
421 .get_vq_align = eni_vdpa_get_vq_align,
422 .get_config_size = eni_vdpa_get_config_size,
423 .get_config = eni_vdpa_get_config,
424 .set_config = eni_vdpa_set_config,
425 .set_config_cb = eni_vdpa_set_config_cb,
426 .get_vq_irq = eni_vdpa_get_vq_irq,
427};
428
429
430static u16 eni_vdpa_get_num_queues(struct eni_vdpa *eni_vdpa)
431{
432 struct virtio_pci_legacy_device *ldev = &eni_vdpa->ldev;
433 u32 features = vp_legacy_get_features(ldev);
434 u16 num = 2;
435
436 if (features & BIT_ULL(VIRTIO_NET_F_MQ)) {
437 __virtio16 max_virtqueue_pairs;
438
439 eni_vdpa_get_config(&eni_vdpa->vdpa,
440 offsetof(struct virtio_net_config, max_virtqueue_pairs),
441 &max_virtqueue_pairs,
442 sizeof(max_virtqueue_pairs));
443 num = 2 * __virtio16_to_cpu(virtio_legacy_is_little_endian(),
444 max_virtqueue_pairs);
445 }
446
447 if (features & BIT_ULL(VIRTIO_NET_F_CTRL_VQ))
448 num += 1;
449
450 return num;
451}
452
453static void eni_vdpa_free_irq_vectors(void *data)
454{
455 pci_free_irq_vectors(data);
456}
457
458static int eni_vdpa_probe(struct pci_dev *pdev, const struct pci_device_id *id)
459{
460 struct device *dev = &pdev->dev;
461 struct eni_vdpa *eni_vdpa;
462 struct virtio_pci_legacy_device *ldev;
463 int ret, i;
464
465 ret = pcim_enable_device(pdev);
466 if (ret)
467 return ret;
468
469 eni_vdpa = vdpa_alloc_device(struct eni_vdpa, vdpa,
470 dev, &eni_vdpa_ops, NULL, false);
471 if (IS_ERR(eni_vdpa)) {
472 ENI_ERR(pdev, "failed to allocate vDPA structure\n");
473 return PTR_ERR(eni_vdpa);
474 }
475
476 ldev = &eni_vdpa->ldev;
477 ldev->pci_dev = pdev;
478
479 ret = vp_legacy_probe(ldev);
480 if (ret) {
481 ENI_ERR(pdev, "failed to probe legacy PCI device\n");
482 goto err;
483 }
484
485 pci_set_master(pdev);
486 pci_set_drvdata(pdev, eni_vdpa);
487
488 eni_vdpa->vdpa.dma_dev = &pdev->dev;
489 eni_vdpa->queues = eni_vdpa_get_num_queues(eni_vdpa);
490
491 ret = devm_add_action_or_reset(dev, eni_vdpa_free_irq_vectors, pdev);
492 if (ret) {
493 ENI_ERR(pdev,
494 "failed for adding devres for freeing irq vectors\n");
495 goto err;
496 }
497
498 eni_vdpa->vring = devm_kcalloc(&pdev->dev, eni_vdpa->queues,
499 sizeof(*eni_vdpa->vring),
500 GFP_KERNEL);
501 if (!eni_vdpa->vring) {
502 ret = -ENOMEM;
503 ENI_ERR(pdev, "failed to allocate virtqueues\n");
504 goto err;
505 }
506
507 for (i = 0; i < eni_vdpa->queues; i++) {
508 eni_vdpa->vring[i].irq = VIRTIO_MSI_NO_VECTOR;
509 eni_vdpa->vring[i].notify = ldev->ioaddr + VIRTIO_PCI_QUEUE_NOTIFY;
510 }
511 eni_vdpa->config_irq = VIRTIO_MSI_NO_VECTOR;
512
513 ret = vdpa_register_device(&eni_vdpa->vdpa, eni_vdpa->queues);
514 if (ret) {
515 ENI_ERR(pdev, "failed to register to vdpa bus\n");
516 goto err;
517 }
518
519 return 0;
520
521err:
522 put_device(&eni_vdpa->vdpa.dev);
523 return ret;
524}
525
526static void eni_vdpa_remove(struct pci_dev *pdev)
527{
528 struct eni_vdpa *eni_vdpa = pci_get_drvdata(pdev);
529
530 vdpa_unregister_device(&eni_vdpa->vdpa);
531 vp_legacy_remove(&eni_vdpa->ldev);
532}
533
534static struct pci_device_id eni_pci_ids[] = {
535 { PCI_DEVICE_SUB(PCI_VENDOR_ID_REDHAT_QUMRANET,
536 VIRTIO_TRANS_ID_NET,
537 PCI_SUBVENDOR_ID_REDHAT_QUMRANET,
538 VIRTIO_ID_NET) },
539 { 0 },
540};
541
542static struct pci_driver eni_vdpa_driver = {
543 .name = "alibaba-eni-vdpa",
544 .id_table = eni_pci_ids,
545 .probe = eni_vdpa_probe,
546 .remove = eni_vdpa_remove,
547};
548
549module_pci_driver(eni_vdpa_driver);
550
551MODULE_AUTHOR("Wu Zongyong <wuzongyong@linux.alibaba.com>");
552MODULE_DESCRIPTION("Alibaba ENI vDPA driver");
553MODULE_LICENSE("GPL v2");