1 /*-
2  * Copyright (c) 2011, Bryan Venteicher <bryanv@FreeBSD.org>
3  * All rights reserved.
4  *
5  * SPDX-License-Identifier: BSD-2-Clause
6  */
7 
8 #include <openamp/virtio.h>
9 
10 /*
11  * TODO :
12  * This structure may change depending on the types of devices we support.
13  */
14 static const struct virtio_ident {
15 	unsigned short devid;
16 	const char *name;
17 } virtio_ident_table[] = {
18 	{
19 	VIRTIO_ID_NETWORK, "Network"}, {
20 	VIRTIO_ID_BLOCK, "Block"}, {
21 	VIRTIO_ID_CONSOLE, "Console"}, {
22 	VIRTIO_ID_ENTROPY, "Entropy"}, {
23 	VIRTIO_ID_BALLOON, "Balloon"}, {
24 	VIRTIO_ID_IOMEMORY, "IOMemory"}, {
25 	VIRTIO_ID_SCSI, "SCSI"}, {
26 	VIRTIO_ID_9P, "9P Transport"}, {
27 	VIRTIO_ID_MAC80211_WLAN, "MAC80211 WLAN"}, {
28 	VIRTIO_ID_RPROC_SERIAL, "Remoteproc Serial"}, {
29 	VIRTIO_ID_GPU, "GPU"}, {
30 	VIRTIO_ID_INPUT, "Input"}, {
31 	VIRTIO_ID_VSOCK, "Vsock Transport"}, {
32 	VIRTIO_ID_SOUND, "Sound"}, {
33 	VIRTIO_ID_FS, "File System"}, {
34 	VIRTIO_ID_MAC80211_HWSIM, "MAC80211 HWSIM"}, {
35 	VIRTIO_ID_I2C_ADAPTER, "I2C Adapter"}, {
36 	VIRTIO_ID_BT, "Bluetooth"}, {
37 	VIRTIO_ID_GPIO, "GPIO" }, {
38 	0, NULL}
39 };
40 
virtio_dev_name(unsigned short devid)41 const char *virtio_dev_name(unsigned short devid)
42 {
43 	const struct virtio_ident *ident;
44 
45 	for (ident = virtio_ident_table; ident->name; ident++) {
46 		if (ident->devid == devid)
47 			return ident->name;
48 	}
49 
50 	return NULL;
51 }
52 
virtio_describe(struct virtio_device * dev,const char * msg,uint32_t features,struct virtio_feature_desc * desc)53 __deprecated void virtio_describe(struct virtio_device *dev, const char *msg,
54 				  uint32_t features, struct virtio_feature_desc *desc)
55 {
56 	(void)dev;
57 	(void)msg;
58 	(void)features;
59 	(void)desc;
60 }
61 
virtio_create_virtqueues(struct virtio_device * vdev,unsigned int flags,unsigned int nvqs,const char * names[],vq_callback callbacks[],void * callback_args[])62 int virtio_create_virtqueues(struct virtio_device *vdev, unsigned int flags,
63 			     unsigned int nvqs, const char *names[],
64 			     vq_callback callbacks[], void *callback_args[])
65 {
66 	struct virtio_vring_info *vring_info;
67 	struct vring_alloc_info *vring_alloc;
68 	unsigned int num_vrings, i;
69 	int ret;
70 	(void)flags;
71 
72 	if (!vdev)
73 		return -EINVAL;
74 
75 	if (vdev->func && vdev->func->create_virtqueues) {
76 		return vdev->func->create_virtqueues(vdev, flags, nvqs,
77 						     names, callbacks, callback_args);
78 	}
79 
80 	num_vrings = vdev->vrings_num;
81 	if (nvqs > num_vrings)
82 		return ERROR_VQUEUE_INVLD_PARAM;
83 	/* Initialize virtqueue for each vring */
84 	for (i = 0; i < nvqs; i++) {
85 		vring_info = &vdev->vrings_info[i];
86 
87 		vring_alloc = &vring_info->info;
88 		if (VIRTIO_ROLE_IS_DRIVER(vdev)) {
89 			size_t offset;
90 			struct metal_io_region *io = vring_info->io;
91 
92 			offset = metal_io_virt_to_offset(io,
93 							 vring_alloc->vaddr);
94 			metal_io_block_set(io, offset, 0,
95 					   vring_size(vring_alloc->num_descs,
96 						      vring_alloc->align));
97 		}
98 		ret = virtqueue_create(vdev, i, names[i], vring_alloc,
99 				       callbacks[i], vdev->func->notify,
100 				       vring_info->vq);
101 		if (ret)
102 			return ret;
103 	}
104 	return 0;
105 }
106 
107