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