1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * (c) 2017 Stefano Stabellini <stefano@aporeto.com>
6 #include <linux/inet.h>
7 #include <linux/kthread.h>
8 #include <linux/list.h>
9 #include <linux/radix-tree.h>
10 #include <linux/module.h>
11 #include <linux/semaphore.h>
12 #include <linux/wait.h>
14 #include <net/inet_common.h>
15 #include <net/inet_connection_sock.h>
16 #include <net/request_sock.h>
18 #include <xen/events.h>
19 #include <xen/grant_table.h>
21 #include <xen/xenbus.h>
22 #include <xen/interface/io/pvcalls.h>
24 #define PVCALLS_VERSIONS "1"
25 #define MAX_RING_ORDER XENBUS_MAX_RING_GRANT_ORDER
27 static struct pvcalls_back_global {
28 struct list_head frontends;
29 struct semaphore frontends_lock;
30 } pvcalls_back_global;
33 * Per-frontend data structure. It contains pointers to the command
34 * ring, its event channel, a list of active sockets and a tree of
37 struct pvcalls_fedata {
38 struct list_head list;
39 struct xenbus_device *dev;
40 struct xen_pvcalls_sring *sring;
41 struct xen_pvcalls_back_ring ring;
43 struct list_head socket_mappings;
44 struct radix_tree_root socketpass_mappings;
45 struct semaphore socket_lock;
48 struct pvcalls_ioworker {
49 struct work_struct register_work;
50 struct workqueue_struct *wq;
54 struct list_head list;
55 struct pvcalls_fedata *fedata;
56 struct sockpass_mapping *sockpass;
60 struct pvcalls_data_intf *ring;
62 struct pvcalls_data data;
70 void (*saved_data_ready)(struct sock *sk);
71 struct pvcalls_ioworker ioworker;
74 struct sockpass_mapping {
75 struct list_head list;
76 struct pvcalls_fedata *fedata;
79 struct xen_pvcalls_request reqcopy;
81 struct workqueue_struct *wq;
82 struct work_struct register_work;
83 void (*saved_data_ready)(struct sock *sk);
86 static irqreturn_t pvcalls_back_conn_event(int irq, void *sock_map);
87 static int pvcalls_back_release_active(struct xenbus_device *dev,
88 struct pvcalls_fedata *fedata,
89 struct sock_mapping *map);
91 static bool pvcalls_conn_back_read(void *opaque)
93 struct sock_mapping *map = (struct sock_mapping *)opaque;
96 RING_IDX cons, prod, size, wanted, array_size, masked_prod, masked_cons;
98 struct pvcalls_data_intf *intf = map->ring;
99 struct pvcalls_data *data = &map->data;
103 array_size = XEN_FLEX_RING_SIZE(map->ring_order);
104 cons = intf->in_cons;
105 prod = intf->in_prod;
106 error = intf->in_error;
107 /* read the indexes first, then deal with the data */
113 size = pvcalls_queued(prod, cons, array_size);
114 if (size >= array_size)
116 spin_lock_irqsave(&map->sock->sk->sk_receive_queue.lock, flags);
117 if (skb_queue_empty(&map->sock->sk->sk_receive_queue)) {
118 atomic_set(&map->read, 0);
119 spin_unlock_irqrestore(&map->sock->sk->sk_receive_queue.lock,
123 spin_unlock_irqrestore(&map->sock->sk->sk_receive_queue.lock, flags);
124 wanted = array_size - size;
125 masked_prod = pvcalls_mask(prod, array_size);
126 masked_cons = pvcalls_mask(cons, array_size);
128 memset(&msg, 0, sizeof(msg));
129 if (masked_prod < masked_cons) {
130 vec[0].iov_base = data->in + masked_prod;
131 vec[0].iov_len = wanted;
132 iov_iter_kvec(&msg.msg_iter, WRITE, vec, 1, wanted);
134 vec[0].iov_base = data->in + masked_prod;
135 vec[0].iov_len = array_size - masked_prod;
136 vec[1].iov_base = data->in;
137 vec[1].iov_len = wanted - vec[0].iov_len;
138 iov_iter_kvec(&msg.msg_iter, WRITE, vec, 2, wanted);
141 atomic_set(&map->read, 0);
142 ret = inet_recvmsg(map->sock, &msg, wanted, MSG_DONTWAIT);
143 WARN_ON(ret > wanted);
144 if (ret == -EAGAIN) /* shouldn't happen */
148 spin_lock_irqsave(&map->sock->sk->sk_receive_queue.lock, flags);
149 if (ret > 0 && !skb_queue_empty(&map->sock->sk->sk_receive_queue))
150 atomic_inc(&map->read);
151 spin_unlock_irqrestore(&map->sock->sk->sk_receive_queue.lock, flags);
153 /* write the data, then modify the indexes */
156 atomic_set(&map->read, 0);
157 intf->in_error = ret;
159 intf->in_prod = prod + ret;
160 /* update the indexes, then notify the other end */
162 notify_remote_via_irq(map->irq);
167 static bool pvcalls_conn_back_write(struct sock_mapping *map)
169 struct pvcalls_data_intf *intf = map->ring;
170 struct pvcalls_data *data = &map->data;
173 RING_IDX cons, prod, size, array_size;
176 cons = intf->out_cons;
177 prod = intf->out_prod;
178 /* read the indexes before dealing with the data */
181 array_size = XEN_FLEX_RING_SIZE(map->ring_order);
182 size = pvcalls_queued(prod, cons, array_size);
186 memset(&msg, 0, sizeof(msg));
187 msg.msg_flags |= MSG_DONTWAIT;
188 if (pvcalls_mask(prod, array_size) > pvcalls_mask(cons, array_size)) {
189 vec[0].iov_base = data->out + pvcalls_mask(cons, array_size);
190 vec[0].iov_len = size;
191 iov_iter_kvec(&msg.msg_iter, READ, vec, 1, size);
193 vec[0].iov_base = data->out + pvcalls_mask(cons, array_size);
194 vec[0].iov_len = array_size - pvcalls_mask(cons, array_size);
195 vec[1].iov_base = data->out;
196 vec[1].iov_len = size - vec[0].iov_len;
197 iov_iter_kvec(&msg.msg_iter, READ, vec, 2, size);
200 atomic_set(&map->write, 0);
201 ret = inet_sendmsg(map->sock, &msg, size);
202 if (ret == -EAGAIN) {
203 atomic_inc(&map->write);
204 atomic_inc(&map->io);
208 /* write the data, then update the indexes */
211 intf->out_error = ret;
214 intf->out_cons = cons + ret;
215 prod = intf->out_prod;
217 /* update the indexes, then notify the other end */
219 if (prod != cons + ret) {
220 atomic_inc(&map->write);
221 atomic_inc(&map->io);
223 notify_remote_via_irq(map->irq);
228 static void pvcalls_back_ioworker(struct work_struct *work)
230 struct pvcalls_ioworker *ioworker = container_of(work,
231 struct pvcalls_ioworker, register_work);
232 struct sock_mapping *map = container_of(ioworker, struct sock_mapping,
234 unsigned int eoi_flags = XEN_EOI_FLAG_SPURIOUS;
236 while (atomic_read(&map->io) > 0) {
237 if (atomic_read(&map->release) > 0) {
238 atomic_set(&map->release, 0);
242 if (atomic_read(&map->read) > 0 &&
243 pvcalls_conn_back_read(map))
245 if (atomic_read(&map->write) > 0 &&
246 pvcalls_conn_back_write(map))
249 if (atomic_read(&map->eoi) > 0 && !atomic_read(&map->write)) {
250 atomic_set(&map->eoi, 0);
251 xen_irq_lateeoi(map->irq, eoi_flags);
252 eoi_flags = XEN_EOI_FLAG_SPURIOUS;
255 atomic_dec(&map->io);
259 static int pvcalls_back_socket(struct xenbus_device *dev,
260 struct xen_pvcalls_request *req)
262 struct pvcalls_fedata *fedata;
264 struct xen_pvcalls_response *rsp;
266 fedata = dev_get_drvdata(&dev->dev);
268 if (req->u.socket.domain != AF_INET ||
269 req->u.socket.type != SOCK_STREAM ||
270 (req->u.socket.protocol != IPPROTO_IP &&
271 req->u.socket.protocol != AF_INET))
276 /* leave the actual socket allocation for later */
278 rsp = RING_GET_RESPONSE(&fedata->ring, fedata->ring.rsp_prod_pvt++);
279 rsp->req_id = req->req_id;
281 rsp->u.socket.id = req->u.socket.id;
287 static void pvcalls_sk_state_change(struct sock *sock)
289 struct sock_mapping *map = sock->sk_user_data;
294 atomic_inc(&map->read);
295 notify_remote_via_irq(map->irq);
298 static void pvcalls_sk_data_ready(struct sock *sock)
300 struct sock_mapping *map = sock->sk_user_data;
301 struct pvcalls_ioworker *iow;
306 iow = &map->ioworker;
307 atomic_inc(&map->read);
308 atomic_inc(&map->io);
309 queue_work(iow->wq, &iow->register_work);
312 static struct sock_mapping *pvcalls_new_active_socket(
313 struct pvcalls_fedata *fedata,
316 evtchn_port_t evtchn,
320 struct sock_mapping *map;
323 map = kzalloc(sizeof(*map), GFP_KERNEL);
327 map->fedata = fedata;
332 ret = xenbus_map_ring_valloc(fedata->dev, &ref, 1, &page);
336 map->ring_order = map->ring->ring_order;
337 /* first read the order, then map the data ring */
339 if (map->ring_order > MAX_RING_ORDER) {
340 pr_warn("%s frontend requested ring_order %u, which is > MAX (%u)\n",
341 __func__, map->ring_order, MAX_RING_ORDER);
344 ret = xenbus_map_ring_valloc(fedata->dev, map->ring->ref,
345 (1 << map->ring_order), &page);
350 ret = bind_interdomain_evtchn_to_irqhandler_lateeoi(
352 pvcalls_back_conn_event, 0, "pvcalls-backend", map);
357 map->data.in = map->bytes;
358 map->data.out = map->bytes + XEN_FLEX_RING_SIZE(map->ring_order);
360 map->ioworker.wq = alloc_workqueue("pvcalls_io", WQ_UNBOUND, 1);
361 if (!map->ioworker.wq)
363 atomic_set(&map->io, 1);
364 INIT_WORK(&map->ioworker.register_work, pvcalls_back_ioworker);
366 down(&fedata->socket_lock);
367 list_add_tail(&map->list, &fedata->socket_mappings);
368 up(&fedata->socket_lock);
370 write_lock_bh(&map->sock->sk->sk_callback_lock);
371 map->saved_data_ready = map->sock->sk->sk_data_ready;
372 map->sock->sk->sk_user_data = map;
373 map->sock->sk->sk_data_ready = pvcalls_sk_data_ready;
374 map->sock->sk->sk_state_change = pvcalls_sk_state_change;
375 write_unlock_bh(&map->sock->sk->sk_callback_lock);
379 down(&fedata->socket_lock);
380 list_del(&map->list);
381 pvcalls_back_release_active(fedata->dev, fedata, map);
382 up(&fedata->socket_lock);
386 static int pvcalls_back_connect(struct xenbus_device *dev,
387 struct xen_pvcalls_request *req)
389 struct pvcalls_fedata *fedata;
392 struct sock_mapping *map;
393 struct xen_pvcalls_response *rsp;
394 struct sockaddr *sa = (struct sockaddr *)&req->u.connect.addr;
396 fedata = dev_get_drvdata(&dev->dev);
398 if (req->u.connect.len < sizeof(sa->sa_family) ||
399 req->u.connect.len > sizeof(req->u.connect.addr) ||
400 sa->sa_family != AF_INET)
403 ret = sock_create(AF_INET, SOCK_STREAM, 0, &sock);
406 ret = inet_stream_connect(sock, sa, req->u.connect.len, 0);
412 map = pvcalls_new_active_socket(fedata,
415 req->u.connect.evtchn,
423 rsp = RING_GET_RESPONSE(&fedata->ring, fedata->ring.rsp_prod_pvt++);
424 rsp->req_id = req->req_id;
426 rsp->u.connect.id = req->u.connect.id;
432 static int pvcalls_back_release_active(struct xenbus_device *dev,
433 struct pvcalls_fedata *fedata,
434 struct sock_mapping *map)
436 disable_irq(map->irq);
437 if (map->sock->sk != NULL) {
438 write_lock_bh(&map->sock->sk->sk_callback_lock);
439 map->sock->sk->sk_user_data = NULL;
440 map->sock->sk->sk_data_ready = map->saved_data_ready;
441 write_unlock_bh(&map->sock->sk->sk_callback_lock);
444 atomic_set(&map->release, 1);
445 flush_work(&map->ioworker.register_work);
447 xenbus_unmap_ring_vfree(dev, map->bytes);
448 xenbus_unmap_ring_vfree(dev, (void *)map->ring);
449 unbind_from_irqhandler(map->irq, map);
451 sock_release(map->sock);
457 static int pvcalls_back_release_passive(struct xenbus_device *dev,
458 struct pvcalls_fedata *fedata,
459 struct sockpass_mapping *mappass)
461 if (mappass->sock->sk != NULL) {
462 write_lock_bh(&mappass->sock->sk->sk_callback_lock);
463 mappass->sock->sk->sk_user_data = NULL;
464 mappass->sock->sk->sk_data_ready = mappass->saved_data_ready;
465 write_unlock_bh(&mappass->sock->sk->sk_callback_lock);
467 sock_release(mappass->sock);
468 flush_workqueue(mappass->wq);
469 destroy_workqueue(mappass->wq);
475 static int pvcalls_back_release(struct xenbus_device *dev,
476 struct xen_pvcalls_request *req)
478 struct pvcalls_fedata *fedata;
479 struct sock_mapping *map, *n;
480 struct sockpass_mapping *mappass;
482 struct xen_pvcalls_response *rsp;
484 fedata = dev_get_drvdata(&dev->dev);
486 down(&fedata->socket_lock);
487 list_for_each_entry_safe(map, n, &fedata->socket_mappings, list) {
488 if (map->id == req->u.release.id) {
489 list_del(&map->list);
490 up(&fedata->socket_lock);
491 ret = pvcalls_back_release_active(dev, fedata, map);
495 mappass = radix_tree_lookup(&fedata->socketpass_mappings,
497 if (mappass != NULL) {
498 radix_tree_delete(&fedata->socketpass_mappings, mappass->id);
499 up(&fedata->socket_lock);
500 ret = pvcalls_back_release_passive(dev, fedata, mappass);
502 up(&fedata->socket_lock);
505 rsp = RING_GET_RESPONSE(&fedata->ring, fedata->ring.rsp_prod_pvt++);
506 rsp->req_id = req->req_id;
507 rsp->u.release.id = req->u.release.id;
513 static void __pvcalls_back_accept(struct work_struct *work)
515 struct sockpass_mapping *mappass = container_of(
516 work, struct sockpass_mapping, register_work);
517 struct sock_mapping *map;
518 struct pvcalls_ioworker *iow;
519 struct pvcalls_fedata *fedata;
521 struct xen_pvcalls_response *rsp;
522 struct xen_pvcalls_request *req;
527 fedata = mappass->fedata;
529 * __pvcalls_back_accept can race against pvcalls_back_accept.
530 * We only need to check the value of "cmd" on read. It could be
531 * done atomically, but to simplify the code on the write side, we
534 spin_lock_irqsave(&mappass->copy_lock, flags);
535 req = &mappass->reqcopy;
536 if (req->cmd != PVCALLS_ACCEPT) {
537 spin_unlock_irqrestore(&mappass->copy_lock, flags);
540 spin_unlock_irqrestore(&mappass->copy_lock, flags);
545 sock->type = mappass->sock->type;
546 sock->ops = mappass->sock->ops;
548 ret = inet_accept(mappass->sock, sock, O_NONBLOCK, true);
549 if (ret == -EAGAIN) {
554 map = pvcalls_new_active_socket(fedata,
555 req->u.accept.id_new,
557 req->u.accept.evtchn,
565 map->sockpass = mappass;
566 iow = &map->ioworker;
567 atomic_inc(&map->read);
568 atomic_inc(&map->io);
569 queue_work(iow->wq, &iow->register_work);
572 rsp = RING_GET_RESPONSE(&fedata->ring, fedata->ring.rsp_prod_pvt++);
573 rsp->req_id = req->req_id;
575 rsp->u.accept.id = req->u.accept.id;
577 RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&fedata->ring, notify);
579 notify_remote_via_irq(fedata->irq);
581 mappass->reqcopy.cmd = 0;
584 static void pvcalls_pass_sk_data_ready(struct sock *sock)
586 struct sockpass_mapping *mappass = sock->sk_user_data;
587 struct pvcalls_fedata *fedata;
588 struct xen_pvcalls_response *rsp;
595 fedata = mappass->fedata;
596 spin_lock_irqsave(&mappass->copy_lock, flags);
597 if (mappass->reqcopy.cmd == PVCALLS_POLL) {
598 rsp = RING_GET_RESPONSE(&fedata->ring,
599 fedata->ring.rsp_prod_pvt++);
600 rsp->req_id = mappass->reqcopy.req_id;
601 rsp->u.poll.id = mappass->reqcopy.u.poll.id;
602 rsp->cmd = mappass->reqcopy.cmd;
605 mappass->reqcopy.cmd = 0;
606 spin_unlock_irqrestore(&mappass->copy_lock, flags);
608 RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&fedata->ring, notify);
610 notify_remote_via_irq(mappass->fedata->irq);
612 spin_unlock_irqrestore(&mappass->copy_lock, flags);
613 queue_work(mappass->wq, &mappass->register_work);
617 static int pvcalls_back_bind(struct xenbus_device *dev,
618 struct xen_pvcalls_request *req)
620 struct pvcalls_fedata *fedata;
622 struct sockpass_mapping *map;
623 struct xen_pvcalls_response *rsp;
625 fedata = dev_get_drvdata(&dev->dev);
627 map = kzalloc(sizeof(*map), GFP_KERNEL);
633 INIT_WORK(&map->register_work, __pvcalls_back_accept);
634 spin_lock_init(&map->copy_lock);
635 map->wq = alloc_workqueue("pvcalls_wq", WQ_UNBOUND, 1);
641 ret = sock_create(AF_INET, SOCK_STREAM, 0, &map->sock);
645 ret = inet_bind(map->sock, (struct sockaddr *)&req->u.bind.addr,
650 map->fedata = fedata;
651 map->id = req->u.bind.id;
653 down(&fedata->socket_lock);
654 ret = radix_tree_insert(&fedata->socketpass_mappings, map->id,
656 up(&fedata->socket_lock);
660 write_lock_bh(&map->sock->sk->sk_callback_lock);
661 map->saved_data_ready = map->sock->sk->sk_data_ready;
662 map->sock->sk->sk_user_data = map;
663 map->sock->sk->sk_data_ready = pvcalls_pass_sk_data_ready;
664 write_unlock_bh(&map->sock->sk->sk_callback_lock);
668 if (map && map->sock)
669 sock_release(map->sock);
671 destroy_workqueue(map->wq);
674 rsp = RING_GET_RESPONSE(&fedata->ring, fedata->ring.rsp_prod_pvt++);
675 rsp->req_id = req->req_id;
677 rsp->u.bind.id = req->u.bind.id;
682 static int pvcalls_back_listen(struct xenbus_device *dev,
683 struct xen_pvcalls_request *req)
685 struct pvcalls_fedata *fedata;
687 struct sockpass_mapping *map;
688 struct xen_pvcalls_response *rsp;
690 fedata = dev_get_drvdata(&dev->dev);
692 down(&fedata->socket_lock);
693 map = radix_tree_lookup(&fedata->socketpass_mappings, req->u.listen.id);
694 up(&fedata->socket_lock);
698 ret = inet_listen(map->sock, req->u.listen.backlog);
701 rsp = RING_GET_RESPONSE(&fedata->ring, fedata->ring.rsp_prod_pvt++);
702 rsp->req_id = req->req_id;
704 rsp->u.listen.id = req->u.listen.id;
709 static int pvcalls_back_accept(struct xenbus_device *dev,
710 struct xen_pvcalls_request *req)
712 struct pvcalls_fedata *fedata;
713 struct sockpass_mapping *mappass;
715 struct xen_pvcalls_response *rsp;
718 fedata = dev_get_drvdata(&dev->dev);
720 down(&fedata->socket_lock);
721 mappass = radix_tree_lookup(&fedata->socketpass_mappings,
723 up(&fedata->socket_lock);
728 * Limitation of the current implementation: only support one
729 * concurrent accept or poll call on one socket.
731 spin_lock_irqsave(&mappass->copy_lock, flags);
732 if (mappass->reqcopy.cmd != 0) {
733 spin_unlock_irqrestore(&mappass->copy_lock, flags);
738 mappass->reqcopy = *req;
739 spin_unlock_irqrestore(&mappass->copy_lock, flags);
740 queue_work(mappass->wq, &mappass->register_work);
742 /* Tell the caller we don't need to send back a notification yet */
746 rsp = RING_GET_RESPONSE(&fedata->ring, fedata->ring.rsp_prod_pvt++);
747 rsp->req_id = req->req_id;
749 rsp->u.accept.id = req->u.accept.id;
754 static int pvcalls_back_poll(struct xenbus_device *dev,
755 struct xen_pvcalls_request *req)
757 struct pvcalls_fedata *fedata;
758 struct sockpass_mapping *mappass;
759 struct xen_pvcalls_response *rsp;
760 struct inet_connection_sock *icsk;
761 struct request_sock_queue *queue;
766 fedata = dev_get_drvdata(&dev->dev);
768 down(&fedata->socket_lock);
769 mappass = radix_tree_lookup(&fedata->socketpass_mappings,
771 up(&fedata->socket_lock);
776 * Limitation of the current implementation: only support one
777 * concurrent accept or poll call on one socket.
779 spin_lock_irqsave(&mappass->copy_lock, flags);
780 if (mappass->reqcopy.cmd != 0) {
785 mappass->reqcopy = *req;
786 icsk = inet_csk(mappass->sock->sk);
787 queue = &icsk->icsk_accept_queue;
788 data = READ_ONCE(queue->rskq_accept_head) != NULL;
790 mappass->reqcopy.cmd = 0;
794 spin_unlock_irqrestore(&mappass->copy_lock, flags);
796 /* Tell the caller we don't need to send back a notification yet */
800 spin_unlock_irqrestore(&mappass->copy_lock, flags);
802 rsp = RING_GET_RESPONSE(&fedata->ring, fedata->ring.rsp_prod_pvt++);
803 rsp->req_id = req->req_id;
805 rsp->u.poll.id = req->u.poll.id;
810 static int pvcalls_back_handle_cmd(struct xenbus_device *dev,
811 struct xen_pvcalls_request *req)
817 ret = pvcalls_back_socket(dev, req);
819 case PVCALLS_CONNECT:
820 ret = pvcalls_back_connect(dev, req);
822 case PVCALLS_RELEASE:
823 ret = pvcalls_back_release(dev, req);
826 ret = pvcalls_back_bind(dev, req);
829 ret = pvcalls_back_listen(dev, req);
832 ret = pvcalls_back_accept(dev, req);
835 ret = pvcalls_back_poll(dev, req);
839 struct pvcalls_fedata *fedata;
840 struct xen_pvcalls_response *rsp;
842 fedata = dev_get_drvdata(&dev->dev);
843 rsp = RING_GET_RESPONSE(
844 &fedata->ring, fedata->ring.rsp_prod_pvt++);
845 rsp->req_id = req->req_id;
847 rsp->ret = -ENOTSUPP;
854 static void pvcalls_back_work(struct pvcalls_fedata *fedata)
856 int notify, notify_all = 0, more = 1;
857 struct xen_pvcalls_request req;
858 struct xenbus_device *dev = fedata->dev;
861 while (RING_HAS_UNCONSUMED_REQUESTS(&fedata->ring)) {
862 RING_COPY_REQUEST(&fedata->ring,
863 fedata->ring.req_cons++,
866 if (!pvcalls_back_handle_cmd(dev, &req)) {
867 RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(
868 &fedata->ring, notify);
869 notify_all += notify;
874 notify_remote_via_irq(fedata->irq);
878 RING_FINAL_CHECK_FOR_REQUESTS(&fedata->ring, more);
882 static irqreturn_t pvcalls_back_event(int irq, void *dev_id)
884 struct xenbus_device *dev = dev_id;
885 struct pvcalls_fedata *fedata = NULL;
886 unsigned int eoi_flags = XEN_EOI_FLAG_SPURIOUS;
889 fedata = dev_get_drvdata(&dev->dev);
891 pvcalls_back_work(fedata);
896 xen_irq_lateeoi(irq, eoi_flags);
901 static irqreturn_t pvcalls_back_conn_event(int irq, void *sock_map)
903 struct sock_mapping *map = sock_map;
904 struct pvcalls_ioworker *iow;
906 if (map == NULL || map->sock == NULL || map->sock->sk == NULL ||
907 map->sock->sk->sk_user_data != map) {
908 xen_irq_lateeoi(irq, 0);
912 iow = &map->ioworker;
914 atomic_inc(&map->write);
915 atomic_inc(&map->eoi);
916 atomic_inc(&map->io);
917 queue_work(iow->wq, &iow->register_work);
922 static int backend_connect(struct xenbus_device *dev)
925 evtchn_port_t evtchn;
926 grant_ref_t ring_ref;
927 struct pvcalls_fedata *fedata = NULL;
929 fedata = kzalloc(sizeof(struct pvcalls_fedata), GFP_KERNEL);
934 err = xenbus_scanf(XBT_NIL, dev->otherend, "port", "%u",
938 xenbus_dev_fatal(dev, err, "reading %s/event-channel",
943 err = xenbus_scanf(XBT_NIL, dev->otherend, "ring-ref", "%u", &ring_ref);
946 xenbus_dev_fatal(dev, err, "reading %s/ring-ref",
951 err = bind_interdomain_evtchn_to_irq_lateeoi(dev, evtchn);
956 err = request_threaded_irq(fedata->irq, NULL, pvcalls_back_event,
957 IRQF_ONESHOT, "pvcalls-back", dev);
961 err = xenbus_map_ring_valloc(dev, &ring_ref, 1,
962 (void **)&fedata->sring);
966 BACK_RING_INIT(&fedata->ring, fedata->sring, XEN_PAGE_SIZE * 1);
969 INIT_LIST_HEAD(&fedata->socket_mappings);
970 INIT_RADIX_TREE(&fedata->socketpass_mappings, GFP_KERNEL);
971 sema_init(&fedata->socket_lock, 1);
972 dev_set_drvdata(&dev->dev, fedata);
974 down(&pvcalls_back_global.frontends_lock);
975 list_add_tail(&fedata->list, &pvcalls_back_global.frontends);
976 up(&pvcalls_back_global.frontends_lock);
981 if (fedata->irq >= 0)
982 unbind_from_irqhandler(fedata->irq, dev);
983 if (fedata->sring != NULL)
984 xenbus_unmap_ring_vfree(dev, fedata->sring);
989 static int backend_disconnect(struct xenbus_device *dev)
991 struct pvcalls_fedata *fedata;
992 struct sock_mapping *map, *n;
993 struct sockpass_mapping *mappass;
994 struct radix_tree_iter iter;
998 fedata = dev_get_drvdata(&dev->dev);
1000 down(&fedata->socket_lock);
1001 list_for_each_entry_safe(map, n, &fedata->socket_mappings, list) {
1002 list_del(&map->list);
1003 pvcalls_back_release_active(dev, fedata, map);
1006 radix_tree_for_each_slot(slot, &fedata->socketpass_mappings, &iter, 0) {
1007 mappass = radix_tree_deref_slot(slot);
1010 if (radix_tree_exception(mappass)) {
1011 if (radix_tree_deref_retry(mappass))
1012 slot = radix_tree_iter_retry(&iter);
1014 radix_tree_delete(&fedata->socketpass_mappings,
1016 pvcalls_back_release_passive(dev, fedata, mappass);
1019 up(&fedata->socket_lock);
1021 unbind_from_irqhandler(fedata->irq, dev);
1022 xenbus_unmap_ring_vfree(dev, fedata->sring);
1024 list_del(&fedata->list);
1026 dev_set_drvdata(&dev->dev, NULL);
1031 static int pvcalls_back_probe(struct xenbus_device *dev,
1032 const struct xenbus_device_id *id)
1035 struct xenbus_transaction xbt;
1040 err = xenbus_transaction_start(&xbt);
1042 pr_warn("%s cannot create xenstore transaction\n", __func__);
1046 err = xenbus_printf(xbt, dev->nodename, "versions", "%s",
1049 pr_warn("%s write out 'versions' failed\n", __func__);
1053 err = xenbus_printf(xbt, dev->nodename, "max-page-order", "%u",
1056 pr_warn("%s write out 'max-page-order' failed\n", __func__);
1060 err = xenbus_printf(xbt, dev->nodename, "function-calls",
1061 XENBUS_FUNCTIONS_CALLS);
1063 pr_warn("%s write out 'function-calls' failed\n", __func__);
1069 err = xenbus_transaction_end(xbt, abort);
1071 if (err == -EAGAIN && !abort)
1073 pr_warn("%s cannot complete xenstore transaction\n", __func__);
1080 xenbus_switch_state(dev, XenbusStateInitWait);
1085 static void set_backend_state(struct xenbus_device *dev,
1086 enum xenbus_state state)
1088 while (dev->state != state) {
1089 switch (dev->state) {
1090 case XenbusStateClosed:
1092 case XenbusStateInitWait:
1093 case XenbusStateConnected:
1094 xenbus_switch_state(dev, XenbusStateInitWait);
1096 case XenbusStateClosing:
1097 xenbus_switch_state(dev, XenbusStateClosing);
1103 case XenbusStateInitWait:
1104 case XenbusStateInitialised:
1106 case XenbusStateConnected:
1107 if (backend_connect(dev))
1109 xenbus_switch_state(dev, XenbusStateConnected);
1111 case XenbusStateClosing:
1112 case XenbusStateClosed:
1113 xenbus_switch_state(dev, XenbusStateClosing);
1119 case XenbusStateConnected:
1121 case XenbusStateInitWait:
1122 case XenbusStateClosing:
1123 case XenbusStateClosed:
1124 down(&pvcalls_back_global.frontends_lock);
1125 backend_disconnect(dev);
1126 up(&pvcalls_back_global.frontends_lock);
1127 xenbus_switch_state(dev, XenbusStateClosing);
1133 case XenbusStateClosing:
1135 case XenbusStateInitWait:
1136 case XenbusStateConnected:
1137 case XenbusStateClosed:
1138 xenbus_switch_state(dev, XenbusStateClosed);
1150 static void pvcalls_back_changed(struct xenbus_device *dev,
1151 enum xenbus_state frontend_state)
1153 switch (frontend_state) {
1154 case XenbusStateInitialising:
1155 set_backend_state(dev, XenbusStateInitWait);
1158 case XenbusStateInitialised:
1159 case XenbusStateConnected:
1160 set_backend_state(dev, XenbusStateConnected);
1163 case XenbusStateClosing:
1164 set_backend_state(dev, XenbusStateClosing);
1167 case XenbusStateClosed:
1168 set_backend_state(dev, XenbusStateClosed);
1169 if (xenbus_dev_is_online(dev))
1171 device_unregister(&dev->dev);
1173 case XenbusStateUnknown:
1174 set_backend_state(dev, XenbusStateClosed);
1175 device_unregister(&dev->dev);
1179 xenbus_dev_fatal(dev, -EINVAL, "saw state %d at frontend",
1185 static int pvcalls_back_remove(struct xenbus_device *dev)
1190 static int pvcalls_back_uevent(struct xenbus_device *xdev,
1191 struct kobj_uevent_env *env)
1196 static const struct xenbus_device_id pvcalls_back_ids[] = {
1201 static struct xenbus_driver pvcalls_back_driver = {
1202 .ids = pvcalls_back_ids,
1203 .probe = pvcalls_back_probe,
1204 .remove = pvcalls_back_remove,
1205 .uevent = pvcalls_back_uevent,
1206 .otherend_changed = pvcalls_back_changed,
1209 static int __init pvcalls_back_init(void)
1216 ret = xenbus_register_backend(&pvcalls_back_driver);
1220 sema_init(&pvcalls_back_global.frontends_lock, 1);
1221 INIT_LIST_HEAD(&pvcalls_back_global.frontends);
1224 module_init(pvcalls_back_init);
1226 static void __exit pvcalls_back_fin(void)
1228 struct pvcalls_fedata *fedata, *nfedata;
1230 down(&pvcalls_back_global.frontends_lock);
1231 list_for_each_entry_safe(fedata, nfedata,
1232 &pvcalls_back_global.frontends, list) {
1233 backend_disconnect(fedata->dev);
1235 up(&pvcalls_back_global.frontends_lock);
1237 xenbus_unregister_driver(&pvcalls_back_driver);
1240 module_exit(pvcalls_back_fin);
1242 MODULE_DESCRIPTION("Xen PV Calls backend driver");
1243 MODULE_AUTHOR("Stefano Stabellini <sstabellini@kernel.org>");
1244 MODULE_LICENSE("GPL");