hc
2024-01-05 071106ecf68c401173c58808b1cf5f68cc50d390
kernel/drivers/rpmsg/virtio_rpmsg_bus.c
....@@ -11,21 +11,22 @@
1111
1212 #define pr_fmt(fmt) "%s: " fmt, __func__
1313
14
-#include <linux/kernel.h>
15
-#include <linux/module.h>
16
-#include <linux/virtio.h>
17
-#include <linux/virtio_ids.h>
18
-#include <linux/virtio_config.h>
19
-#include <linux/scatterlist.h>
2014 #include <linux/dma-mapping.h>
21
-#include <linux/slab.h>
2215 #include <linux/idr.h>
2316 #include <linux/jiffies.h>
24
-#include <linux/sched.h>
25
-#include <linux/wait.h>
26
-#include <linux/rpmsg.h>
17
+#include <linux/kernel.h>
18
+#include <linux/module.h>
2719 #include <linux/mutex.h>
2820 #include <linux/of_device.h>
21
+#include <linux/rpmsg.h>
22
+#include <linux/scatterlist.h>
23
+#include <linux/slab.h>
24
+#include <linux/sched.h>
25
+#include <linux/virtio.h>
26
+#include <linux/virtio_byteorder.h>
27
+#include <linux/virtio_ids.h>
28
+#include <linux/virtio_config.h>
29
+#include <linux/wait.h>
2930
3031 #include "rpmsg_internal.h"
3132
....@@ -84,12 +85,12 @@
8485 * Every message sent(/received) on the rpmsg bus begins with this header.
8586 */
8687 struct rpmsg_hdr {
87
- u32 src;
88
- u32 dst;
89
- u32 reserved;
90
- u16 len;
91
- u16 flags;
92
- u8 data[0];
88
+ __virtio32 src;
89
+ __virtio32 dst;
90
+ __virtio32 reserved;
91
+ __virtio16 len;
92
+ __virtio16 flags;
93
+ u8 data[];
9394 } __packed;
9495
9596 /**
....@@ -106,8 +107,8 @@
106107 */
107108 struct rpmsg_ns_msg {
108109 char name[RPMSG_NAME_SIZE];
109
- u32 addr;
110
- u32 flags;
110
+ __virtio32 addr;
111
+ __virtio32 flags;
111112 } __packed;
112113
113114 /**
....@@ -122,7 +123,12 @@
122123 };
123124
124125 /**
125
- * @vrp: the remote processor this channel belongs to
126
+ * struct virtio_rpmsg_channel - rpmsg channel descriptor
127
+ * @rpdev: the rpmsg channel device
128
+ * @vrp: the virtio remote processor device this channel belongs to
129
+ *
130
+ * This structure stores the channel that links the rpmsg device to the virtio
131
+ * remote processor device.
126132 */
127133 struct virtio_rpmsg_channel {
128134 struct rpmsg_device rpdev;
....@@ -175,6 +181,7 @@
175181 int len, u32 dst);
176182 static int virtio_rpmsg_trysend_offchannel(struct rpmsg_endpoint *ept, u32 src,
177183 u32 dst, void *data, int len);
184
+static ssize_t virtio_rpmsg_get_mtu(struct rpmsg_endpoint *ept);
178185
179186 static const struct rpmsg_endpoint_ops virtio_endpoint_ops = {
180187 .destroy_ept = virtio_rpmsg_destroy_ept,
....@@ -184,6 +191,7 @@
184191 .trysend = virtio_rpmsg_trysend,
185192 .trysendto = virtio_rpmsg_trysendto,
186193 .trysend_offchannel = virtio_rpmsg_trysend_offchannel,
194
+ .get_mtu = virtio_rpmsg_get_mtu,
187195 };
188196
189197 /**
....@@ -335,8 +343,8 @@
335343 struct rpmsg_ns_msg nsm;
336344
337345 strncpy(nsm.name, rpdev->id.name, RPMSG_NAME_SIZE);
338
- nsm.addr = rpdev->ept->addr;
339
- nsm.flags = RPMSG_NS_CREATE;
346
+ nsm.addr = cpu_to_virtio32(vrp->vdev, rpdev->ept->addr);
347
+ nsm.flags = cpu_to_virtio32(vrp->vdev, RPMSG_NS_CREATE);
340348
341349 err = rpmsg_sendto(rpdev->ept, &nsm, sizeof(nsm), RPMSG_NS_ADDR);
342350 if (err)
....@@ -359,8 +367,8 @@
359367 struct rpmsg_ns_msg nsm;
360368
361369 strncpy(nsm.name, rpdev->id.name, RPMSG_NAME_SIZE);
362
- nsm.addr = rpdev->ept->addr;
363
- nsm.flags = RPMSG_NS_DESTROY;
370
+ nsm.addr = cpu_to_virtio32(vrp->vdev, rpdev->ept->addr);
371
+ nsm.flags = cpu_to_virtio32(vrp->vdev, RPMSG_NS_DESTROY);
364372
365373 err = rpmsg_sendto(rpdev->ept, &nsm, sizeof(nsm), RPMSG_NS_ADDR);
366374 if (err)
....@@ -612,18 +620,18 @@
612620 }
613621 }
614622
615
- msg->len = len;
623
+ msg->len = cpu_to_virtio16(vrp->vdev, len);
616624 msg->flags = 0;
617
- msg->src = src;
618
- msg->dst = dst;
625
+ msg->src = cpu_to_virtio32(vrp->vdev, src);
626
+ msg->dst = cpu_to_virtio32(vrp->vdev, dst);
619627 msg->reserved = 0;
620628 memcpy(msg->data, data, len);
621629
622630 dev_dbg(dev, "TX From 0x%x, To 0x%x, Len %d, Flags %d, Reserved %d\n",
623
- msg->src, msg->dst, msg->len, msg->flags, msg->reserved);
631
+ src, dst, len, msg->flags, msg->reserved);
624632 #if defined(CONFIG_DYNAMIC_DEBUG)
625633 dynamic_hex_dump("rpmsg_virtio TX: ", DUMP_PREFIX_NONE, 16, 1,
626
- msg, sizeof(*msg) + msg->len, true);
634
+ msg, sizeof(*msg) + len, true);
627635 #endif
628636
629637 rpmsg_sg_init(&sg, msg, sizeof(*msg) + len);
....@@ -699,18 +707,30 @@
699707 return rpmsg_send_offchannel_raw(rpdev, src, dst, data, len, false);
700708 }
701709
710
+static ssize_t virtio_rpmsg_get_mtu(struct rpmsg_endpoint *ept)
711
+{
712
+ struct rpmsg_device *rpdev = ept->rpdev;
713
+ struct virtio_rpmsg_channel *vch = to_virtio_rpmsg_channel(rpdev);
714
+
715
+ return vch->vrp->buf_size - sizeof(struct rpmsg_hdr);
716
+}
717
+
702718 static int rpmsg_recv_single(struct virtproc_info *vrp, struct device *dev,
703719 struct rpmsg_hdr *msg, unsigned int len)
704720 {
705721 struct rpmsg_endpoint *ept;
706722 struct scatterlist sg;
723
+ unsigned int msg_len = virtio16_to_cpu(vrp->vdev, msg->len);
707724 int err;
708725
709726 dev_dbg(dev, "From: 0x%x, To: 0x%x, Len: %d, Flags: %d, Reserved: %d\n",
710
- msg->src, msg->dst, msg->len, msg->flags, msg->reserved);
727
+ virtio32_to_cpu(vrp->vdev, msg->src),
728
+ virtio32_to_cpu(vrp->vdev, msg->dst), msg_len,
729
+ virtio16_to_cpu(vrp->vdev, msg->flags),
730
+ virtio32_to_cpu(vrp->vdev, msg->reserved));
711731 #if defined(CONFIG_DYNAMIC_DEBUG)
712732 dynamic_hex_dump("rpmsg_virtio RX: ", DUMP_PREFIX_NONE, 16, 1,
713
- msg, sizeof(*msg) + msg->len, true);
733
+ msg, sizeof(*msg) + msg_len, true);
714734 #endif
715735
716736 /*
....@@ -718,15 +738,15 @@
718738 * the reported payload length.
719739 */
720740 if (len > vrp->buf_size ||
721
- msg->len > (len - sizeof(struct rpmsg_hdr))) {
722
- dev_warn(dev, "inbound msg too big: (%d, %d)\n", len, msg->len);
741
+ msg_len > (len - sizeof(struct rpmsg_hdr))) {
742
+ dev_warn(dev, "inbound msg too big: (%d, %d)\n", len, msg_len);
723743 return -EINVAL;
724744 }
725745
726746 /* use the dst addr to fetch the callback of the appropriate user */
727747 mutex_lock(&vrp->endpoints_lock);
728748
729
- ept = idr_find(&vrp->endpoints, msg->dst);
749
+ ept = idr_find(&vrp->endpoints, virtio32_to_cpu(vrp->vdev, msg->dst));
730750
731751 /* let's make sure no one deallocates ept while we use it */
732752 if (ept)
....@@ -739,8 +759,8 @@
739759 mutex_lock(&ept->cb_lock);
740760
741761 if (ept->cb)
742
- ept->cb(ept->rpdev, msg->data, msg->len, ept->priv,
743
- msg->src);
762
+ ept->cb(ept->rpdev, msg->data, msg_len, ept->priv,
763
+ virtio32_to_cpu(vrp->vdev, msg->src));
744764
745765 mutex_unlock(&ept->cb_lock);
746766
....@@ -846,15 +866,15 @@
846866 /* don't trust the remote processor for null terminating the name */
847867 msg->name[RPMSG_NAME_SIZE - 1] = '\0';
848868
849
- dev_info(dev, "%sing channel %s addr 0x%x\n",
850
- msg->flags & RPMSG_NS_DESTROY ? "destroy" : "creat",
851
- msg->name, msg->addr);
852
-
853869 strncpy(chinfo.name, msg->name, sizeof(chinfo.name));
854870 chinfo.src = RPMSG_ADDR_ANY;
855
- chinfo.dst = msg->addr;
871
+ chinfo.dst = virtio32_to_cpu(vrp->vdev, msg->addr);
856872
857
- if (msg->flags & RPMSG_NS_DESTROY) {
873
+ dev_info(dev, "%sing channel %s addr 0x%x\n",
874
+ virtio32_to_cpu(vrp->vdev, msg->flags) & RPMSG_NS_DESTROY ?
875
+ "destroy" : "creat", msg->name, chinfo.dst);
876
+
877
+ if (virtio32_to_cpu(vrp->vdev, msg->flags) & RPMSG_NS_DESTROY) {
858878 ret = rpmsg_unregister_device(&vrp->vdev->dev, &chinfo);
859879 if (ret)
860880 dev_err(dev, "rpmsg_destroy_channel failed: %d\n", ret);
....@@ -920,7 +940,7 @@
920940 goto vqs_del;
921941 }
922942
923
- dev_dbg(&vdev->dev, "buffers: va %p, dma %pad\n",
943
+ dev_dbg(&vdev->dev, "buffers: va %pK, dma %pad\n",
924944 bufs_va, &vrp->bufs_dma);
925945
926946 /* half of the buffers is dedicated for RX */