1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20#define pr_fmt(fmt) "%s: " fmt, __func__
21
22#include <linux/kernel.h>
23#include <linux/module.h>
24#include <linux/virtio.h>
25#include <linux/virtio_ids.h>
26#include <linux/virtio_config.h>
27#include <linux/scatterlist.h>
28#include <linux/dma-mapping.h>
29#include <linux/slab.h>
30#include <linux/idr.h>
31#include <linux/jiffies.h>
32#include <linux/sched.h>
33#include <linux/wait.h>
34#include <linux/rpmsg.h>
35#include <linux/mutex.h>
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59struct virtproc_info {
60 struct virtio_device *vdev;
61 struct virtqueue *rvq, *svq;
62 void *rbufs, *sbufs;
63 int last_sbuf;
64 dma_addr_t bufs_dma;
65 struct mutex tx_lock;
66 struct idr endpoints;
67 struct mutex endpoints_lock;
68 wait_queue_head_t sendq;
69 atomic_t sleepers;
70 struct rpmsg_endpoint *ns_ept;
71};
72
73
74
75
76
77
78
79struct rpmsg_channel_info {
80 char name[RPMSG_NAME_SIZE];
81 u32 src;
82 u32 dst;
83};
84
85#define to_rpmsg_channel(d) container_of(d, struct rpmsg_channel, dev)
86#define to_rpmsg_driver(d) container_of(d, struct rpmsg_driver, drv)
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105#define RPMSG_NUM_BUFS (512)
106#define RPMSG_BUF_SIZE (512)
107#define RPMSG_TOTAL_BUF_SPACE (RPMSG_NUM_BUFS * RPMSG_BUF_SIZE)
108
109
110
111
112
113
114#define RPMSG_RESERVED_ADDRESSES (1024)
115
116
117#define RPMSG_NS_ADDR (53)
118
119
120#define rpmsg_show_attr(field, path, format_string) \
121static ssize_t \
122field##_show(struct device *dev, \
123 struct device_attribute *attr, char *buf) \
124{ \
125 struct rpmsg_channel *rpdev = to_rpmsg_channel(dev); \
126 \
127 return sprintf(buf, format_string, rpdev->path); \
128}
129
130
131rpmsg_show_attr(name, id.name, "%s\n");
132rpmsg_show_attr(src, src, "0x%x\n");
133rpmsg_show_attr(dst, dst, "0x%x\n");
134rpmsg_show_attr(announce, announce ? "true" : "false", "%s\n");
135
136
137
138
139
140
141
142static unsigned int rpmsg_dev_index;
143
144static ssize_t modalias_show(struct device *dev,
145 struct device_attribute *attr, char *buf)
146{
147 struct rpmsg_channel *rpdev = to_rpmsg_channel(dev);
148
149 return sprintf(buf, RPMSG_DEVICE_MODALIAS_FMT "\n", rpdev->id.name);
150}
151
152static struct device_attribute rpmsg_dev_attrs[] = {
153 __ATTR_RO(name),
154 __ATTR_RO(modalias),
155 __ATTR_RO(dst),
156 __ATTR_RO(src),
157 __ATTR_RO(announce),
158 __ATTR_NULL
159};
160
161
162static inline int rpmsg_id_match(const struct rpmsg_channel *rpdev,
163 const struct rpmsg_device_id *id)
164{
165 return strncmp(id->name, rpdev->id.name, RPMSG_NAME_SIZE) == 0;
166}
167
168
169static int rpmsg_dev_match(struct device *dev, struct device_driver *drv)
170{
171 struct rpmsg_channel *rpdev = to_rpmsg_channel(dev);
172 struct rpmsg_driver *rpdrv = to_rpmsg_driver(drv);
173 const struct rpmsg_device_id *ids = rpdrv->id_table;
174 unsigned int i;
175
176 for (i = 0; ids[i].name[0]; i++)
177 if (rpmsg_id_match(rpdev, &ids[i]))
178 return 1;
179
180 return 0;
181}
182
183static int rpmsg_uevent(struct device *dev, struct kobj_uevent_env *env)
184{
185 struct rpmsg_channel *rpdev = to_rpmsg_channel(dev);
186
187 return add_uevent_var(env, "MODALIAS=" RPMSG_DEVICE_MODALIAS_FMT,
188 rpdev->id.name);
189}
190
191
192
193
194
195
196
197
198
199
200static void __ept_release(struct kref *kref)
201{
202 struct rpmsg_endpoint *ept = container_of(kref, struct rpmsg_endpoint,
203 refcount);
204
205
206
207
208 kfree(ept);
209}
210
211
212static struct rpmsg_endpoint *__rpmsg_create_ept(struct virtproc_info *vrp,
213 struct rpmsg_channel *rpdev, rpmsg_rx_cb_t cb,
214 void *priv, u32 addr)
215{
216 int err, tmpaddr, request;
217 struct rpmsg_endpoint *ept;
218 struct device *dev = rpdev ? &rpdev->dev : &vrp->vdev->dev;
219
220 if (!idr_pre_get(&vrp->endpoints, GFP_KERNEL))
221 return NULL;
222
223 ept = kzalloc(sizeof(*ept), GFP_KERNEL);
224 if (!ept) {
225 dev_err(dev, "failed to kzalloc a new ept\n");
226 return NULL;
227 }
228
229 kref_init(&ept->refcount);
230 mutex_init(&ept->cb_lock);
231
232 ept->rpdev = rpdev;
233 ept->cb = cb;
234 ept->priv = priv;
235
236
237 request = addr == RPMSG_ADDR_ANY ? RPMSG_RESERVED_ADDRESSES : addr;
238
239 mutex_lock(&vrp->endpoints_lock);
240
241
242 err = idr_get_new_above(&vrp->endpoints, ept, request, &tmpaddr);
243 if (err) {
244 dev_err(dev, "idr_get_new_above failed: %d\n", err);
245 goto free_ept;
246 }
247
248
249 if (addr != RPMSG_ADDR_ANY && tmpaddr != addr) {
250 dev_err(dev, "address 0x%x already in use\n", addr);
251 goto rem_idr;
252 }
253
254 ept->addr = tmpaddr;
255
256 mutex_unlock(&vrp->endpoints_lock);
257
258 return ept;
259
260rem_idr:
261 idr_remove(&vrp->endpoints, request);
262free_ept:
263 mutex_unlock(&vrp->endpoints_lock);
264 kref_put(&ept->refcount, __ept_release);
265 return NULL;
266}
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308struct rpmsg_endpoint *rpmsg_create_ept(struct rpmsg_channel *rpdev,
309 rpmsg_rx_cb_t cb, void *priv, u32 addr)
310{
311 return __rpmsg_create_ept(rpdev->vrp, rpdev, cb, priv, addr);
312}
313EXPORT_SYMBOL(rpmsg_create_ept);
314
315
316
317
318
319
320
321
322
323
324
325static void
326__rpmsg_destroy_ept(struct virtproc_info *vrp, struct rpmsg_endpoint *ept)
327{
328
329 mutex_lock(&vrp->endpoints_lock);
330 idr_remove(&vrp->endpoints, ept->addr);
331 mutex_unlock(&vrp->endpoints_lock);
332
333
334 mutex_lock(&ept->cb_lock);
335 ept->cb = NULL;
336 mutex_unlock(&ept->cb_lock);
337
338 kref_put(&ept->refcount, __ept_release);
339}
340
341
342
343
344
345
346
347
348void rpmsg_destroy_ept(struct rpmsg_endpoint *ept)
349{
350 __rpmsg_destroy_ept(ept->rpdev->vrp, ept);
351}
352EXPORT_SYMBOL(rpmsg_destroy_ept);
353
354
355
356
357
358
359
360
361
362static int rpmsg_dev_probe(struct device *dev)
363{
364 struct rpmsg_channel *rpdev = to_rpmsg_channel(dev);
365 struct rpmsg_driver *rpdrv = to_rpmsg_driver(rpdev->dev.driver);
366 struct virtproc_info *vrp = rpdev->vrp;
367 struct rpmsg_endpoint *ept;
368 int err;
369
370 ept = rpmsg_create_ept(rpdev, rpdrv->callback, NULL, rpdev->src);
371 if (!ept) {
372 dev_err(dev, "failed to create endpoint\n");
373 err = -ENOMEM;
374 goto out;
375 }
376
377 rpdev->ept = ept;
378 rpdev->src = ept->addr;
379
380 err = rpdrv->probe(rpdev);
381 if (err) {
382 dev_err(dev, "%s: failed: %d\n", __func__, err);
383 rpmsg_destroy_ept(ept);
384 goto out;
385 }
386
387
388 if (rpdev->announce &&
389 virtio_has_feature(vrp->vdev, VIRTIO_RPMSG_F_NS)) {
390 struct rpmsg_ns_msg nsm;
391
392 strncpy(nsm.name, rpdev->id.name, RPMSG_NAME_SIZE);
393 nsm.addr = rpdev->src;
394 nsm.flags = RPMSG_NS_CREATE;
395
396 err = rpmsg_sendto(rpdev, &nsm, sizeof(nsm), RPMSG_NS_ADDR);
397 if (err)
398 dev_err(dev, "failed to announce service %d\n", err);
399 }
400
401out:
402 return err;
403}
404
405static int rpmsg_dev_remove(struct device *dev)
406{
407 struct rpmsg_channel *rpdev = to_rpmsg_channel(dev);
408 struct rpmsg_driver *rpdrv = to_rpmsg_driver(rpdev->dev.driver);
409 struct virtproc_info *vrp = rpdev->vrp;
410 int err = 0;
411
412
413 if (rpdev->announce &&
414 virtio_has_feature(vrp->vdev, VIRTIO_RPMSG_F_NS)) {
415 struct rpmsg_ns_msg nsm;
416
417 strncpy(nsm.name, rpdev->id.name, RPMSG_NAME_SIZE);
418 nsm.addr = rpdev->src;
419 nsm.flags = RPMSG_NS_DESTROY;
420
421 err = rpmsg_sendto(rpdev, &nsm, sizeof(nsm), RPMSG_NS_ADDR);
422 if (err)
423 dev_err(dev, "failed to announce service %d\n", err);
424 }
425
426 rpdrv->remove(rpdev);
427
428 rpmsg_destroy_ept(rpdev->ept);
429
430 return err;
431}
432
433static struct bus_type rpmsg_bus = {
434 .name = "rpmsg",
435 .match = rpmsg_dev_match,
436 .dev_attrs = rpmsg_dev_attrs,
437 .uevent = rpmsg_uevent,
438 .probe = rpmsg_dev_probe,
439 .remove = rpmsg_dev_remove,
440};
441
442
443
444
445
446
447
448int register_rpmsg_driver(struct rpmsg_driver *rpdrv)
449{
450 rpdrv->drv.bus = &rpmsg_bus;
451 return driver_register(&rpdrv->drv);
452}
453EXPORT_SYMBOL(register_rpmsg_driver);
454
455
456
457
458
459
460
461void unregister_rpmsg_driver(struct rpmsg_driver *rpdrv)
462{
463 driver_unregister(&rpdrv->drv);
464}
465EXPORT_SYMBOL(unregister_rpmsg_driver);
466
467static void rpmsg_release_device(struct device *dev)
468{
469 struct rpmsg_channel *rpdev = to_rpmsg_channel(dev);
470
471 kfree(rpdev);
472}
473
474
475
476
477
478
479static int rpmsg_channel_match(struct device *dev, void *data)
480{
481 struct rpmsg_channel_info *chinfo = data;
482 struct rpmsg_channel *rpdev = to_rpmsg_channel(dev);
483
484 if (chinfo->src != RPMSG_ADDR_ANY && chinfo->src != rpdev->src)
485 return 0;
486
487 if (chinfo->dst != RPMSG_ADDR_ANY && chinfo->dst != rpdev->dst)
488 return 0;
489
490 if (strncmp(chinfo->name, rpdev->id.name, RPMSG_NAME_SIZE))
491 return 0;
492
493
494 return 1;
495}
496
497
498
499
500
501
502static struct rpmsg_channel *rpmsg_create_channel(struct virtproc_info *vrp,
503 struct rpmsg_channel_info *chinfo)
504{
505 struct rpmsg_channel *rpdev;
506 struct device *tmp, *dev = &vrp->vdev->dev;
507 int ret;
508
509
510 tmp = device_find_child(dev, chinfo, rpmsg_channel_match);
511 if (tmp) {
512
513 put_device(tmp);
514 dev_err(dev, "channel %s:%x:%x already exist\n",
515 chinfo->name, chinfo->src, chinfo->dst);
516 return NULL;
517 }
518
519 rpdev = kzalloc(sizeof(struct rpmsg_channel), GFP_KERNEL);
520 if (!rpdev) {
521 pr_err("kzalloc failed\n");
522 return NULL;
523 }
524
525 rpdev->vrp = vrp;
526 rpdev->src = chinfo->src;
527 rpdev->dst = chinfo->dst;
528
529
530
531
532
533 rpdev->announce = rpdev->src != RPMSG_ADDR_ANY ? true : false;
534
535 strncpy(rpdev->id.name, chinfo->name, RPMSG_NAME_SIZE);
536
537
538 dev_set_name(&rpdev->dev, "rpmsg%d", rpmsg_dev_index++);
539
540 rpdev->dev.parent = &vrp->vdev->dev;
541 rpdev->dev.bus = &rpmsg_bus;
542 rpdev->dev.release = rpmsg_release_device;
543
544 ret = device_register(&rpdev->dev);
545 if (ret) {
546 dev_err(dev, "device_register failed: %d\n", ret);
547 put_device(&rpdev->dev);
548 return NULL;
549 }
550
551 return rpdev;
552}
553
554
555
556
557
558static int rpmsg_destroy_channel(struct virtproc_info *vrp,
559 struct rpmsg_channel_info *chinfo)
560{
561 struct virtio_device *vdev = vrp->vdev;
562 struct device *dev;
563
564 dev = device_find_child(&vdev->dev, chinfo, rpmsg_channel_match);
565 if (!dev)
566 return -EINVAL;
567
568 device_unregister(dev);
569
570 put_device(dev);
571
572 return 0;
573}
574
575
576static void *get_a_tx_buf(struct virtproc_info *vrp)
577{
578 unsigned int len;
579 void *ret;
580
581
582 mutex_lock(&vrp->tx_lock);
583
584
585
586
587
588 if (vrp->last_sbuf < RPMSG_NUM_BUFS / 2)
589 ret = vrp->sbufs + RPMSG_BUF_SIZE * vrp->last_sbuf++;
590
591 else
592 ret = virtqueue_get_buf(vrp->svq, &len);
593
594 mutex_unlock(&vrp->tx_lock);
595
596 return ret;
597}
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615static void rpmsg_upref_sleepers(struct virtproc_info *vrp)
616{
617
618 mutex_lock(&vrp->tx_lock);
619
620
621 if (atomic_inc_return(&vrp->sleepers) == 1)
622
623 virtqueue_enable_cb(vrp->svq);
624
625 mutex_unlock(&vrp->tx_lock);
626}
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642static void rpmsg_downref_sleepers(struct virtproc_info *vrp)
643{
644
645 mutex_lock(&vrp->tx_lock);
646
647
648 if (atomic_dec_and_test(&vrp->sleepers))
649
650 virtqueue_disable_cb(vrp->svq);
651
652 mutex_unlock(&vrp->tx_lock);
653}
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689int rpmsg_send_offchannel_raw(struct rpmsg_channel *rpdev, u32 src, u32 dst,
690 void *data, int len, bool wait)
691{
692 struct virtproc_info *vrp = rpdev->vrp;
693 struct device *dev = &rpdev->dev;
694 struct scatterlist sg;
695 struct rpmsg_hdr *msg;
696 int err;
697
698
699 if (src == RPMSG_ADDR_ANY || dst == RPMSG_ADDR_ANY) {
700 dev_err(dev, "invalid addr (src 0x%x, dst 0x%x)\n", src, dst);
701 return -EINVAL;
702 }
703
704
705
706
707
708
709
710
711
712
713 if (len > RPMSG_BUF_SIZE - sizeof(struct rpmsg_hdr)) {
714 dev_err(dev, "message is too big (%d)\n", len);
715 return -EMSGSIZE;
716 }
717
718
719 msg = get_a_tx_buf(vrp);
720 if (!msg && !wait)
721 return -ENOMEM;
722
723
724 while (!msg) {
725
726 rpmsg_upref_sleepers(vrp);
727
728
729
730
731
732
733
734 err = wait_event_interruptible_timeout(vrp->sendq,
735 (msg = get_a_tx_buf(vrp)),
736 msecs_to_jiffies(15000));
737
738
739 rpmsg_downref_sleepers(vrp);
740
741
742 if (!err) {
743 dev_err(dev, "timeout waiting for a tx buffer\n");
744 return -ERESTARTSYS;
745 }
746 }
747
748 msg->len = len;
749 msg->flags = 0;
750 msg->src = src;
751 msg->dst = dst;
752 msg->reserved = 0;
753 memcpy(msg->data, data, len);
754
755 dev_dbg(dev, "TX From 0x%x, To 0x%x, Len %d, Flags %d, Reserved %d\n",
756 msg->src, msg->dst, msg->len,
757 msg->flags, msg->reserved);
758 print_hex_dump(KERN_DEBUG, "rpmsg_virtio TX: ", DUMP_PREFIX_NONE, 16, 1,
759 msg, sizeof(*msg) + msg->len, true);
760
761 sg_init_one(&sg, msg, sizeof(*msg) + len);
762
763 mutex_lock(&vrp->tx_lock);
764
765
766 err = virtqueue_add_buf(vrp->svq, &sg, 1, 0, msg, GFP_KERNEL);
767 if (err < 0) {
768
769
770
771
772
773 dev_err(dev, "virtqueue_add_buf failed: %d\n", err);
774 goto out;
775 }
776
777
778 virtqueue_kick(vrp->svq);
779
780 err = 0;
781out:
782 mutex_unlock(&vrp->tx_lock);
783 return err;
784}
785EXPORT_SYMBOL(rpmsg_send_offchannel_raw);
786
787
788static void rpmsg_recv_done(struct virtqueue *rvq)
789{
790 struct rpmsg_hdr *msg;
791 unsigned int len;
792 struct rpmsg_endpoint *ept;
793 struct scatterlist sg;
794 struct virtproc_info *vrp = rvq->vdev->priv;
795 struct device *dev = &rvq->vdev->dev;
796 int err;
797
798 msg = virtqueue_get_buf(rvq, &len);
799 if (!msg) {
800 dev_err(dev, "uhm, incoming signal, but no used buffer ?\n");
801 return;
802 }
803
804 dev_dbg(dev, "From: 0x%x, To: 0x%x, Len: %d, Flags: %d, Reserved: %d\n",
805 msg->src, msg->dst, msg->len,
806 msg->flags, msg->reserved);
807 print_hex_dump(KERN_DEBUG, "rpmsg_virtio RX: ", DUMP_PREFIX_NONE, 16, 1,
808 msg, sizeof(*msg) + msg->len, true);
809
810
811
812
813
814 if (len > RPMSG_BUF_SIZE ||
815 msg->len > (len - sizeof(struct rpmsg_hdr))) {
816 dev_warn(dev, "inbound msg too big: (%d, %d)\n", len, msg->len);
817 return;
818 }
819
820
821 mutex_lock(&vrp->endpoints_lock);
822
823 ept = idr_find(&vrp->endpoints, msg->dst);
824
825
826 if (ept)
827 kref_get(&ept->refcount);
828
829 mutex_unlock(&vrp->endpoints_lock);
830
831 if (ept) {
832
833 mutex_lock(&ept->cb_lock);
834
835 if (ept->cb)
836 ept->cb(ept->rpdev, msg->data, msg->len, ept->priv,
837 msg->src);
838
839 mutex_unlock(&ept->cb_lock);
840
841
842 kref_put(&ept->refcount, __ept_release);
843 } else
844 dev_warn(dev, "msg received with no recepient\n");
845
846
847 sg_init_one(&sg, msg, RPMSG_BUF_SIZE);
848
849
850 err = virtqueue_add_buf(vrp->rvq, &sg, 0, 1, msg, GFP_KERNEL);
851 if (err < 0) {
852 dev_err(dev, "failed to add a virtqueue buffer: %d\n", err);
853 return;
854 }
855
856
857 virtqueue_kick(vrp->rvq);
858}
859
860
861
862
863
864
865
866
867static void rpmsg_xmit_done(struct virtqueue *svq)
868{
869 struct virtproc_info *vrp = svq->vdev->priv;
870
871 dev_dbg(&svq->vdev->dev, "%s\n", __func__);
872
873
874 wake_up_interruptible(&vrp->sendq);
875}
876
877
878static void rpmsg_ns_cb(struct rpmsg_channel *rpdev, void *data, int len,
879 void *priv, u32 src)
880{
881 struct rpmsg_ns_msg *msg = data;
882 struct rpmsg_channel *newch;
883 struct rpmsg_channel_info chinfo;
884 struct virtproc_info *vrp = priv;
885 struct device *dev = &vrp->vdev->dev;
886 int ret;
887
888 print_hex_dump(KERN_DEBUG, "NS announcement: ",
889 DUMP_PREFIX_NONE, 16, 1,
890 data, len, true);
891
892 if (len != sizeof(*msg)) {
893 dev_err(dev, "malformed ns msg (%d)\n", len);
894 return;
895 }
896
897
898
899
900
901
902
903 if (rpdev) {
904 dev_err(dev, "anomaly: ns ept has an rpdev handle\n");
905 return;
906 }
907
908
909 msg->name[RPMSG_NAME_SIZE - 1] = '\0';
910
911 dev_info(dev, "%sing channel %s addr 0x%x\n",
912 msg->flags & RPMSG_NS_DESTROY ? "destroy" : "creat",
913 msg->name, msg->addr);
914
915 strncpy(chinfo.name, msg->name, sizeof(chinfo.name));
916 chinfo.src = RPMSG_ADDR_ANY;
917 chinfo.dst = msg->addr;
918
919 if (msg->flags & RPMSG_NS_DESTROY) {
920 ret = rpmsg_destroy_channel(vrp, &chinfo);
921 if (ret)
922 dev_err(dev, "rpmsg_destroy_channel failed: %d\n", ret);
923 } else {
924 newch = rpmsg_create_channel(vrp, &chinfo);
925 if (!newch)
926 dev_err(dev, "rpmsg_create_channel failed\n");
927 }
928}
929
930static int rpmsg_probe(struct virtio_device *vdev)
931{
932 vq_callback_t *vq_cbs[] = { rpmsg_recv_done, rpmsg_xmit_done };
933 const char *names[] = { "input", "output" };
934 struct virtqueue *vqs[2];
935 struct virtproc_info *vrp;
936 void *bufs_va;
937 int err = 0, i;
938
939 vrp = kzalloc(sizeof(*vrp), GFP_KERNEL);
940 if (!vrp)
941 return -ENOMEM;
942
943 vrp->vdev = vdev;
944
945 idr_init(&vrp->endpoints);
946 mutex_init(&vrp->endpoints_lock);
947 mutex_init(&vrp->tx_lock);
948 init_waitqueue_head(&vrp->sendq);
949
950
951 err = vdev->config->find_vqs(vdev, 2, vqs, vq_cbs, names);
952 if (err)
953 goto free_vrp;
954
955 vrp->rvq = vqs[0];
956 vrp->svq = vqs[1];
957
958
959 bufs_va = dma_alloc_coherent(vdev->dev.parent->parent,
960 RPMSG_TOTAL_BUF_SPACE,
961 &vrp->bufs_dma, GFP_KERNEL);
962 if (!bufs_va)
963 goto vqs_del;
964
965 dev_dbg(&vdev->dev, "buffers: va %p, dma 0x%llx\n", bufs_va,
966 (unsigned long long)vrp->bufs_dma);
967
968
969 vrp->rbufs = bufs_va;
970
971
972 vrp->sbufs = bufs_va + RPMSG_TOTAL_BUF_SPACE / 2;
973
974
975 for (i = 0; i < RPMSG_NUM_BUFS / 2; i++) {
976 struct scatterlist sg;
977 void *cpu_addr = vrp->rbufs + i * RPMSG_BUF_SIZE;
978
979 sg_init_one(&sg, cpu_addr, RPMSG_BUF_SIZE);
980
981 err = virtqueue_add_buf(vrp->rvq, &sg, 0, 1, cpu_addr,
982 GFP_KERNEL);
983 WARN_ON(err < 0);
984 }
985
986
987 virtqueue_disable_cb(vrp->svq);
988
989 vdev->priv = vrp;
990
991
992 if (virtio_has_feature(vdev, VIRTIO_RPMSG_F_NS)) {
993
994 vrp->ns_ept = __rpmsg_create_ept(vrp, NULL, rpmsg_ns_cb,
995 vrp, RPMSG_NS_ADDR);
996 if (!vrp->ns_ept) {
997 dev_err(&vdev->dev, "failed to create the ns ept\n");
998 err = -ENOMEM;
999 goto free_coherent;
1000 }
1001 }
1002
1003
1004 virtqueue_kick(vrp->rvq);
1005
1006 dev_info(&vdev->dev, "rpmsg host is online\n");
1007
1008 return 0;
1009
1010free_coherent:
1011 dma_free_coherent(vdev->dev.parent->parent, RPMSG_TOTAL_BUF_SPACE,
1012 bufs_va, vrp->bufs_dma);
1013vqs_del:
1014 vdev->config->del_vqs(vrp->vdev);
1015free_vrp:
1016 kfree(vrp);
1017 return err;
1018}
1019
1020static int rpmsg_remove_device(struct device *dev, void *data)
1021{
1022 device_unregister(dev);
1023
1024 return 0;
1025}
1026
1027static void __devexit rpmsg_remove(struct virtio_device *vdev)
1028{
1029 struct virtproc_info *vrp = vdev->priv;
1030 int ret;
1031
1032 vdev->config->reset(vdev);
1033
1034 ret = device_for_each_child(&vdev->dev, NULL, rpmsg_remove_device);
1035 if (ret)
1036 dev_warn(&vdev->dev, "can't remove rpmsg device: %d\n", ret);
1037
1038 if (vrp->ns_ept)
1039 __rpmsg_destroy_ept(vrp, vrp->ns_ept);
1040
1041 idr_remove_all(&vrp->endpoints);
1042 idr_destroy(&vrp->endpoints);
1043
1044 vdev->config->del_vqs(vrp->vdev);
1045
1046 dma_free_coherent(vdev->dev.parent->parent, RPMSG_TOTAL_BUF_SPACE,
1047 vrp->rbufs, vrp->bufs_dma);
1048
1049 kfree(vrp);
1050}
1051
1052static struct virtio_device_id id_table[] = {
1053 { VIRTIO_ID_RPMSG, VIRTIO_DEV_ANY_ID },
1054 { 0 },
1055};
1056
1057static unsigned int features[] = {
1058 VIRTIO_RPMSG_F_NS,
1059};
1060
1061static struct virtio_driver virtio_ipc_driver = {
1062 .feature_table = features,
1063 .feature_table_size = ARRAY_SIZE(features),
1064 .driver.name = KBUILD_MODNAME,
1065 .driver.owner = THIS_MODULE,
1066 .id_table = id_table,
1067 .probe = rpmsg_probe,
1068 .remove = __devexit_p(rpmsg_remove),
1069};
1070
1071static int __init rpmsg_init(void)
1072{
1073 int ret;
1074
1075 ret = bus_register(&rpmsg_bus);
1076 if (ret) {
1077 pr_err("failed to register rpmsg bus: %d\n", ret);
1078 return ret;
1079 }
1080
1081 ret = register_virtio_driver(&virtio_ipc_driver);
1082 if (ret) {
1083 pr_err("failed to register virtio driver: %d\n", ret);
1084 bus_unregister(&rpmsg_bus);
1085 }
1086
1087 return ret;
1088}
1089subsys_initcall(rpmsg_init);
1090
1091static void __exit rpmsg_fini(void)
1092{
1093 unregister_virtio_driver(&virtio_ipc_driver);
1094 bus_unregister(&rpmsg_bus);
1095}
1096module_exit(rpmsg_fini);
1097
1098MODULE_DEVICE_TABLE(virtio, id_table);
1099MODULE_DESCRIPTION("Virtio-based remote processor messaging bus");
1100MODULE_LICENSE("GPL v2");
1101