1 /*-
2 * Copyright (c) 2011, Bryan Venteicher <bryanv@FreeBSD.org>
3 * All rights reserved.
4 *
5 * SPDX-License-Identifier: BSD-2-Clause
6 */
7 #include <openamp/virtio.h>
8
9 static const char *virtio_feature_name(unsigned long feature,
10 const struct virtio_feature_desc *);
11
12 //TODO : This structure may change depending on the types of devices we support.
13 static const struct virtio_ident {
14 unsigned short devid;
15 const char *name;
16 } virtio_ident_table[] = {
17 {
18 VIRTIO_ID_NETWORK, "Network"}, {
19 VIRTIO_ID_BLOCK, "Block"}, {
20 VIRTIO_ID_CONSOLE, "Console"}, {
21 VIRTIO_ID_ENTROPY, "Entropy"}, {
22 VIRTIO_ID_BALLOON, "Balloon"}, {
23 VIRTIO_ID_IOMEMORY, "IOMemory"}, {
24 VIRTIO_ID_SCSI, "SCSI"}, {
25 VIRTIO_ID_9P, "9P Transport"}, {
26 0, NULL}
27 };
28
29 /* Device independent features. */
30 static const struct virtio_feature_desc virtio_common_feature_desc[] = {
31 {VIRTIO_F_NOTIFY_ON_EMPTY, "NotifyOnEmpty"},
32 {VIRTIO_RING_F_INDIRECT_DESC, "RingIndirect"},
33 {VIRTIO_RING_F_EVENT_IDX, "EventIdx"},
34 {VIRTIO_F_BAD_FEATURE, "BadFeature"},
35
36 {0, NULL}
37 };
38
virtio_dev_name(unsigned short devid)39 const char *virtio_dev_name(unsigned short devid)
40 {
41 const struct virtio_ident *ident;
42
43 for (ident = virtio_ident_table; ident->name != NULL; ident++) {
44 if (ident->devid == devid)
45 return (ident->name);
46 }
47
48 return (NULL);
49 }
50
virtio_feature_name(unsigned long val,const struct virtio_feature_desc * desc)51 static const char *virtio_feature_name(unsigned long val,
52 const struct virtio_feature_desc *desc)
53 {
54 int i, j;
55 const struct virtio_feature_desc *descs[2] = { desc,
56 virtio_common_feature_desc
57 };
58
59 for (i = 0; i < 2; i++) {
60 if (!descs[i])
61 continue;
62
63 for (j = 0; descs[i][j].vfd_val != 0; j++) {
64 if (val == descs[i][j].vfd_val)
65 return (descs[i][j].vfd_str);
66 }
67 }
68
69 return (NULL);
70 }
71
virtio_describe(struct virtio_device * dev,const char * msg,uint32_t features,struct virtio_feature_desc * desc)72 void virtio_describe(struct virtio_device *dev, const char *msg,
73 uint32_t features, struct virtio_feature_desc *desc)
74 {
75 (void)dev;
76 (void)msg;
77 (void)features;
78
79 // TODO: Not used currently - keeping it for future use
80 virtio_feature_name(0, desc);
81 }
82
virtio_create_virtqueues(struct virtio_device * vdev,unsigned int flags,unsigned int nvqs,const char * names[],vq_callback * callbacks[])83 int virtio_create_virtqueues(struct virtio_device *vdev, unsigned int flags,
84 unsigned int nvqs, const char *names[],
85 vq_callback *callbacks[])
86 {
87 struct virtio_vring_info *vring_info;
88 struct vring_alloc_info *vring_alloc;
89 unsigned int num_vrings, i;
90 int ret;
91 (void)flags;
92
93 num_vrings = vdev->vrings_num;
94 if (nvqs > num_vrings)
95 return -ERROR_VQUEUE_INVLD_PARAM;
96 /* Initialize virtqueue for each vring */
97 for (i = 0; i < nvqs; i++) {
98 vring_info = &vdev->vrings_info[i];
99
100 vring_alloc = &vring_info->info;
101 #ifndef VIRTIO_SLAVE_ONLY
102 if (vdev->role == VIRTIO_DEV_MASTER) {
103 size_t offset;
104 struct metal_io_region *io = vring_info->io;
105
106 offset = metal_io_virt_to_offset(io,
107 vring_alloc->vaddr);
108 metal_io_block_set(io, offset, 0,
109 vring_size(vring_alloc->num_descs,
110 vring_alloc->align));
111 }
112 #endif
113 ret = virtqueue_create(vdev, i, names[i], vring_alloc,
114 callbacks[i], vdev->func->notify,
115 vring_info->vq);
116 if (ret)
117 return ret;
118 }
119 return 0;
120 }
121
122