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>
17 #include <trace/events/sock.h>
19 #include <xen/events.h>
20 #include <xen/grant_table.h>
22 #include <xen/xenbus.h>
23 #include <xen/interface/io/pvcalls.h>
25 #define PVCALLS_VERSIONS "1"
26 #define MAX_RING_ORDER XENBUS_MAX_RING_GRANT_ORDER
28 static struct pvcalls_back_global {
29 struct list_head frontends;
30 struct semaphore frontends_lock;
31 } pvcalls_back_global;
34 * Per-frontend data structure. It contains pointers to the command
35 * ring, its event channel, a list of active sockets and a tree of
38 struct pvcalls_fedata {
39 struct list_head list;
40 struct xenbus_device *dev;
41 struct xen_pvcalls_sring *sring;
42 struct xen_pvcalls_back_ring ring;
44 struct list_head socket_mappings;
45 struct radix_tree_root socketpass_mappings;
46 struct semaphore socket_lock;
49 struct pvcalls_ioworker {
50 struct work_struct register_work;
51 struct workqueue_struct *wq;
55 struct list_head list;
56 struct pvcalls_fedata *fedata;
57 struct sockpass_mapping *sockpass;
61 struct pvcalls_data_intf *ring;
63 struct pvcalls_data data;
71 void (*saved_data_ready)(struct sock *sk);
72 struct pvcalls_ioworker ioworker;
75 struct sockpass_mapping {
76 struct list_head list;
77 struct pvcalls_fedata *fedata;
80 struct xen_pvcalls_request reqcopy;
82 struct workqueue_struct *wq;
83 struct work_struct register_work;
84 void (*saved_data_ready)(struct sock *sk);
87 static irqreturn_t pvcalls_back_conn_event(int irq, void *sock_map);
88 static int pvcalls_back_release_active(struct xenbus_device *dev,
89 struct pvcalls_fedata *fedata,
90 struct sock_mapping *map);
92 static bool pvcalls_conn_back_read(void *opaque)
94 struct sock_mapping *map = (struct sock_mapping *)opaque;
97 RING_IDX cons, prod, size, wanted, array_size, masked_prod, masked_cons;
99 struct pvcalls_data_intf *intf = map->ring;
100 struct pvcalls_data *data = &map->data;
104 array_size = XEN_FLEX_RING_SIZE(map->ring_order);
105 cons = intf->in_cons;
106 prod = intf->in_prod;
107 error = intf->in_error;
108 /* read the indexes first, then deal with the data */
114 size = pvcalls_queued(prod, cons, array_size);
115 if (size >= array_size)
117 spin_lock_irqsave(&map->sock->sk->sk_receive_queue.lock, flags);
118 if (skb_queue_empty(&map->sock->sk->sk_receive_queue)) {
119 atomic_set(&map->read, 0);
120 spin_unlock_irqrestore(&map->sock->sk->sk_receive_queue.lock,
124 spin_unlock_irqrestore(&map->sock->sk->sk_receive_queue.lock, flags);
125 wanted = array_size - size;
126 masked_prod = pvcalls_mask(prod, array_size);
127 masked_cons = pvcalls_mask(cons, array_size);
129 memset(&msg, 0, sizeof(msg));
130 if (masked_prod < masked_cons) {
131 vec[0].iov_base = data->in + masked_prod;
132 vec[0].iov_len = wanted;
133 iov_iter_kvec(&msg.msg_iter, ITER_DEST, vec, 1, wanted);
135 vec[0].iov_base = data->in + masked_prod;
136 vec[0].iov_len = array_size - masked_prod;
137 vec[1].iov_base = data->in;
138 vec[1].iov_len = wanted - vec[0].iov_len;
139 iov_iter_kvec(&msg.msg_iter, ITER_DEST, vec, 2, wanted);
142 atomic_set(&map->read, 0);
143 ret = inet_recvmsg(map->sock, &msg, wanted, MSG_DONTWAIT);
144 WARN_ON(ret > wanted);
145 if (ret == -EAGAIN) /* shouldn't happen */
149 spin_lock_irqsave(&map->sock->sk->sk_receive_queue.lock, flags);
150 if (ret > 0 && !skb_queue_empty(&map->sock->sk->sk_receive_queue))
151 atomic_inc(&map->read);
152 spin_unlock_irqrestore(&map->sock->sk->sk_receive_queue.lock, flags);
154 /* write the data, then modify the indexes */
157 atomic_set(&map->read, 0);
158 intf->in_error = ret;
160 intf->in_prod = prod + ret;
161 /* update the indexes, then notify the other end */
163 notify_remote_via_irq(map->irq);
168 static bool pvcalls_conn_back_write(struct sock_mapping *map)
170 struct pvcalls_data_intf *intf = map->ring;
171 struct pvcalls_data *data = &map->data;
174 RING_IDX cons, prod, size, array_size;
177 atomic_set(&map->write, 0);
179 cons = intf->out_cons;
180 prod = intf->out_prod;
181 /* read the indexes before dealing with the data */
184 array_size = XEN_FLEX_RING_SIZE(map->ring_order);
185 size = pvcalls_queued(prod, cons, array_size);
189 memset(&msg, 0, sizeof(msg));
190 msg.msg_flags |= MSG_DONTWAIT;
191 if (pvcalls_mask(prod, array_size) > pvcalls_mask(cons, array_size)) {
192 vec[0].iov_base = data->out + pvcalls_mask(cons, array_size);
193 vec[0].iov_len = size;
194 iov_iter_kvec(&msg.msg_iter, ITER_SOURCE, vec, 1, size);
196 vec[0].iov_base = data->out + pvcalls_mask(cons, array_size);
197 vec[0].iov_len = array_size - pvcalls_mask(cons, array_size);
198 vec[1].iov_base = data->out;
199 vec[1].iov_len = size - vec[0].iov_len;
200 iov_iter_kvec(&msg.msg_iter, ITER_SOURCE, vec, 2, size);
203 ret = inet_sendmsg(map->sock, &msg, size);
204 if (ret == -EAGAIN) {
205 atomic_inc(&map->write);
206 atomic_inc(&map->io);
210 /* write the data, then update the indexes */
213 intf->out_error = ret;
216 intf->out_cons = cons + ret;
217 prod = intf->out_prod;
219 /* update the indexes, then notify the other end */
221 if (prod != cons + ret) {
222 atomic_inc(&map->write);
223 atomic_inc(&map->io);
225 notify_remote_via_irq(map->irq);
230 static void pvcalls_back_ioworker(struct work_struct *work)
232 struct pvcalls_ioworker *ioworker = container_of(work,
233 struct pvcalls_ioworker, register_work);
234 struct sock_mapping *map = container_of(ioworker, struct sock_mapping,
236 unsigned int eoi_flags = XEN_EOI_FLAG_SPURIOUS;
238 while (atomic_read(&map->io) > 0) {
239 if (atomic_read(&map->release) > 0) {
240 atomic_set(&map->release, 0);
244 if (atomic_read(&map->read) > 0 &&
245 pvcalls_conn_back_read(map))
247 if (atomic_read(&map->write) > 0 &&
248 pvcalls_conn_back_write(map))
251 if (atomic_read(&map->eoi) > 0 && !atomic_read(&map->write)) {
252 atomic_set(&map->eoi, 0);
253 xen_irq_lateeoi(map->irq, eoi_flags);
254 eoi_flags = XEN_EOI_FLAG_SPURIOUS;
257 atomic_dec(&map->io);
261 static int pvcalls_back_socket(struct xenbus_device *dev,
262 struct xen_pvcalls_request *req)
264 struct pvcalls_fedata *fedata;
266 struct xen_pvcalls_response *rsp;
268 fedata = dev_get_drvdata(&dev->dev);
270 if (req->u.socket.domain != AF_INET ||
271 req->u.socket.type != SOCK_STREAM ||
272 (req->u.socket.protocol != IPPROTO_IP &&
273 req->u.socket.protocol != AF_INET))
278 /* leave the actual socket allocation for later */
280 rsp = RING_GET_RESPONSE(&fedata->ring, fedata->ring.rsp_prod_pvt++);
281 rsp->req_id = req->req_id;
283 rsp->u.socket.id = req->u.socket.id;
289 static void pvcalls_sk_state_change(struct sock *sock)
291 struct sock_mapping *map = sock->sk_user_data;
296 atomic_inc(&map->read);
297 notify_remote_via_irq(map->irq);
300 static void pvcalls_sk_data_ready(struct sock *sock)
302 struct sock_mapping *map = sock->sk_user_data;
303 struct pvcalls_ioworker *iow;
305 trace_sk_data_ready(sock);
310 iow = &map->ioworker;
311 atomic_inc(&map->read);
312 atomic_inc(&map->io);
313 queue_work(iow->wq, &iow->register_work);
316 static struct sock_mapping *pvcalls_new_active_socket(
317 struct pvcalls_fedata *fedata,
320 evtchn_port_t evtchn,
324 struct sock_mapping *map;
327 map = kzalloc(sizeof(*map), GFP_KERNEL);
333 map->fedata = fedata;
338 ret = xenbus_map_ring_valloc(fedata->dev, &ref, 1, &page);
342 map->ring_order = map->ring->ring_order;
343 /* first read the order, then map the data ring */
345 if (map->ring_order > MAX_RING_ORDER) {
346 pr_warn("%s frontend requested ring_order %u, which is > MAX (%u)\n",
347 __func__, map->ring_order, MAX_RING_ORDER);
350 ret = xenbus_map_ring_valloc(fedata->dev, map->ring->ref,
351 (1 << map->ring_order), &page);
356 ret = bind_interdomain_evtchn_to_irqhandler_lateeoi(
358 pvcalls_back_conn_event, 0, "pvcalls-backend", map);
363 map->data.in = map->bytes;
364 map->data.out = map->bytes + XEN_FLEX_RING_SIZE(map->ring_order);
366 map->ioworker.wq = alloc_ordered_workqueue("pvcalls_io", 0);
367 if (!map->ioworker.wq)
369 atomic_set(&map->io, 1);
370 INIT_WORK(&map->ioworker.register_work, pvcalls_back_ioworker);
372 down(&fedata->socket_lock);
373 list_add_tail(&map->list, &fedata->socket_mappings);
374 up(&fedata->socket_lock);
376 write_lock_bh(&map->sock->sk->sk_callback_lock);
377 map->saved_data_ready = map->sock->sk->sk_data_ready;
378 map->sock->sk->sk_user_data = map;
379 map->sock->sk->sk_data_ready = pvcalls_sk_data_ready;
380 map->sock->sk->sk_state_change = pvcalls_sk_state_change;
381 write_unlock_bh(&map->sock->sk->sk_callback_lock);
385 down(&fedata->socket_lock);
386 list_del(&map->list);
387 pvcalls_back_release_active(fedata->dev, fedata, map);
388 up(&fedata->socket_lock);
392 static int pvcalls_back_connect(struct xenbus_device *dev,
393 struct xen_pvcalls_request *req)
395 struct pvcalls_fedata *fedata;
398 struct sock_mapping *map;
399 struct xen_pvcalls_response *rsp;
400 struct sockaddr *sa = (struct sockaddr *)&req->u.connect.addr;
402 fedata = dev_get_drvdata(&dev->dev);
404 if (req->u.connect.len < sizeof(sa->sa_family) ||
405 req->u.connect.len > sizeof(req->u.connect.addr) ||
406 sa->sa_family != AF_INET)
409 ret = sock_create(AF_INET, SOCK_STREAM, 0, &sock);
412 ret = inet_stream_connect(sock, sa, req->u.connect.len, 0);
418 map = pvcalls_new_active_socket(fedata,
421 req->u.connect.evtchn,
427 rsp = RING_GET_RESPONSE(&fedata->ring, fedata->ring.rsp_prod_pvt++);
428 rsp->req_id = req->req_id;
430 rsp->u.connect.id = req->u.connect.id;
436 static int pvcalls_back_release_active(struct xenbus_device *dev,
437 struct pvcalls_fedata *fedata,
438 struct sock_mapping *map)
440 disable_irq(map->irq);
441 if (map->sock->sk != NULL) {
442 write_lock_bh(&map->sock->sk->sk_callback_lock);
443 map->sock->sk->sk_user_data = NULL;
444 map->sock->sk->sk_data_ready = map->saved_data_ready;
445 write_unlock_bh(&map->sock->sk->sk_callback_lock);
448 atomic_set(&map->release, 1);
449 flush_work(&map->ioworker.register_work);
451 xenbus_unmap_ring_vfree(dev, map->bytes);
452 xenbus_unmap_ring_vfree(dev, (void *)map->ring);
453 unbind_from_irqhandler(map->irq, map);
455 sock_release(map->sock);
461 static int pvcalls_back_release_passive(struct xenbus_device *dev,
462 struct pvcalls_fedata *fedata,
463 struct sockpass_mapping *mappass)
465 if (mappass->sock->sk != NULL) {
466 write_lock_bh(&mappass->sock->sk->sk_callback_lock);
467 mappass->sock->sk->sk_user_data = NULL;
468 mappass->sock->sk->sk_data_ready = mappass->saved_data_ready;
469 write_unlock_bh(&mappass->sock->sk->sk_callback_lock);
471 sock_release(mappass->sock);
472 destroy_workqueue(mappass->wq);
478 static int pvcalls_back_release(struct xenbus_device *dev,
479 struct xen_pvcalls_request *req)
481 struct pvcalls_fedata *fedata;
482 struct sock_mapping *map, *n;
483 struct sockpass_mapping *mappass;
485 struct xen_pvcalls_response *rsp;
487 fedata = dev_get_drvdata(&dev->dev);
489 down(&fedata->socket_lock);
490 list_for_each_entry_safe(map, n, &fedata->socket_mappings, list) {
491 if (map->id == req->u.release.id) {
492 list_del(&map->list);
493 up(&fedata->socket_lock);
494 ret = pvcalls_back_release_active(dev, fedata, map);
498 mappass = radix_tree_lookup(&fedata->socketpass_mappings,
500 if (mappass != NULL) {
501 radix_tree_delete(&fedata->socketpass_mappings, mappass->id);
502 up(&fedata->socket_lock);
503 ret = pvcalls_back_release_passive(dev, fedata, mappass);
505 up(&fedata->socket_lock);
508 rsp = RING_GET_RESPONSE(&fedata->ring, fedata->ring.rsp_prod_pvt++);
509 rsp->req_id = req->req_id;
510 rsp->u.release.id = req->u.release.id;
516 static void __pvcalls_back_accept(struct work_struct *work)
518 struct sockpass_mapping *mappass = container_of(
519 work, struct sockpass_mapping, register_work);
520 struct sock_mapping *map;
521 struct pvcalls_ioworker *iow;
522 struct pvcalls_fedata *fedata;
524 struct xen_pvcalls_response *rsp;
525 struct xen_pvcalls_request *req;
530 fedata = mappass->fedata;
532 * __pvcalls_back_accept can race against pvcalls_back_accept.
533 * We only need to check the value of "cmd" on read. It could be
534 * done atomically, but to simplify the code on the write side, we
537 spin_lock_irqsave(&mappass->copy_lock, flags);
538 req = &mappass->reqcopy;
539 if (req->cmd != PVCALLS_ACCEPT) {
540 spin_unlock_irqrestore(&mappass->copy_lock, flags);
543 spin_unlock_irqrestore(&mappass->copy_lock, flags);
548 sock->type = mappass->sock->type;
549 sock->ops = mappass->sock->ops;
551 ret = inet_accept(mappass->sock, sock, O_NONBLOCK, true);
552 if (ret == -EAGAIN) {
557 map = pvcalls_new_active_socket(fedata,
558 req->u.accept.id_new,
560 req->u.accept.evtchn,
567 map->sockpass = mappass;
568 iow = &map->ioworker;
569 atomic_inc(&map->read);
570 atomic_inc(&map->io);
571 queue_work(iow->wq, &iow->register_work);
574 rsp = RING_GET_RESPONSE(&fedata->ring, fedata->ring.rsp_prod_pvt++);
575 rsp->req_id = req->req_id;
577 rsp->u.accept.id = req->u.accept.id;
579 RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&fedata->ring, notify);
581 notify_remote_via_irq(fedata->irq);
583 mappass->reqcopy.cmd = 0;
586 static void pvcalls_pass_sk_data_ready(struct sock *sock)
588 struct sockpass_mapping *mappass = sock->sk_user_data;
589 struct pvcalls_fedata *fedata;
590 struct xen_pvcalls_response *rsp;
594 trace_sk_data_ready(sock);
599 fedata = mappass->fedata;
600 spin_lock_irqsave(&mappass->copy_lock, flags);
601 if (mappass->reqcopy.cmd == PVCALLS_POLL) {
602 rsp = RING_GET_RESPONSE(&fedata->ring,
603 fedata->ring.rsp_prod_pvt++);
604 rsp->req_id = mappass->reqcopy.req_id;
605 rsp->u.poll.id = mappass->reqcopy.u.poll.id;
606 rsp->cmd = mappass->reqcopy.cmd;
609 mappass->reqcopy.cmd = 0;
610 spin_unlock_irqrestore(&mappass->copy_lock, flags);
612 RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&fedata->ring, notify);
614 notify_remote_via_irq(mappass->fedata->irq);
616 spin_unlock_irqrestore(&mappass->copy_lock, flags);
617 queue_work(mappass->wq, &mappass->register_work);
621 static int pvcalls_back_bind(struct xenbus_device *dev,
622 struct xen_pvcalls_request *req)
624 struct pvcalls_fedata *fedata;
626 struct sockpass_mapping *map;
627 struct xen_pvcalls_response *rsp;
629 fedata = dev_get_drvdata(&dev->dev);
631 map = kzalloc(sizeof(*map), GFP_KERNEL);
637 INIT_WORK(&map->register_work, __pvcalls_back_accept);
638 spin_lock_init(&map->copy_lock);
639 map->wq = alloc_ordered_workqueue("pvcalls_wq", 0);
645 ret = sock_create(AF_INET, SOCK_STREAM, 0, &map->sock);
649 ret = inet_bind(map->sock, (struct sockaddr *)&req->u.bind.addr,
654 map->fedata = fedata;
655 map->id = req->u.bind.id;
657 down(&fedata->socket_lock);
658 ret = radix_tree_insert(&fedata->socketpass_mappings, map->id,
660 up(&fedata->socket_lock);
664 write_lock_bh(&map->sock->sk->sk_callback_lock);
665 map->saved_data_ready = map->sock->sk->sk_data_ready;
666 map->sock->sk->sk_user_data = map;
667 map->sock->sk->sk_data_ready = pvcalls_pass_sk_data_ready;
668 write_unlock_bh(&map->sock->sk->sk_callback_lock);
672 if (map && map->sock)
673 sock_release(map->sock);
675 destroy_workqueue(map->wq);
678 rsp = RING_GET_RESPONSE(&fedata->ring, fedata->ring.rsp_prod_pvt++);
679 rsp->req_id = req->req_id;
681 rsp->u.bind.id = req->u.bind.id;
686 static int pvcalls_back_listen(struct xenbus_device *dev,
687 struct xen_pvcalls_request *req)
689 struct pvcalls_fedata *fedata;
691 struct sockpass_mapping *map;
692 struct xen_pvcalls_response *rsp;
694 fedata = dev_get_drvdata(&dev->dev);
696 down(&fedata->socket_lock);
697 map = radix_tree_lookup(&fedata->socketpass_mappings, req->u.listen.id);
698 up(&fedata->socket_lock);
702 ret = inet_listen(map->sock, req->u.listen.backlog);
705 rsp = RING_GET_RESPONSE(&fedata->ring, fedata->ring.rsp_prod_pvt++);
706 rsp->req_id = req->req_id;
708 rsp->u.listen.id = req->u.listen.id;
713 static int pvcalls_back_accept(struct xenbus_device *dev,
714 struct xen_pvcalls_request *req)
716 struct pvcalls_fedata *fedata;
717 struct sockpass_mapping *mappass;
719 struct xen_pvcalls_response *rsp;
722 fedata = dev_get_drvdata(&dev->dev);
724 down(&fedata->socket_lock);
725 mappass = radix_tree_lookup(&fedata->socketpass_mappings,
727 up(&fedata->socket_lock);
732 * Limitation of the current implementation: only support one
733 * concurrent accept or poll call on one socket.
735 spin_lock_irqsave(&mappass->copy_lock, flags);
736 if (mappass->reqcopy.cmd != 0) {
737 spin_unlock_irqrestore(&mappass->copy_lock, flags);
742 mappass->reqcopy = *req;
743 spin_unlock_irqrestore(&mappass->copy_lock, flags);
744 queue_work(mappass->wq, &mappass->register_work);
746 /* Tell the caller we don't need to send back a notification yet */
750 rsp = RING_GET_RESPONSE(&fedata->ring, fedata->ring.rsp_prod_pvt++);
751 rsp->req_id = req->req_id;
753 rsp->u.accept.id = req->u.accept.id;
758 static int pvcalls_back_poll(struct xenbus_device *dev,
759 struct xen_pvcalls_request *req)
761 struct pvcalls_fedata *fedata;
762 struct sockpass_mapping *mappass;
763 struct xen_pvcalls_response *rsp;
764 struct inet_connection_sock *icsk;
765 struct request_sock_queue *queue;
770 fedata = dev_get_drvdata(&dev->dev);
772 down(&fedata->socket_lock);
773 mappass = radix_tree_lookup(&fedata->socketpass_mappings,
775 up(&fedata->socket_lock);
780 * Limitation of the current implementation: only support one
781 * concurrent accept or poll call on one socket.
783 spin_lock_irqsave(&mappass->copy_lock, flags);
784 if (mappass->reqcopy.cmd != 0) {
789 mappass->reqcopy = *req;
790 icsk = inet_csk(mappass->sock->sk);
791 queue = &icsk->icsk_accept_queue;
792 data = READ_ONCE(queue->rskq_accept_head) != NULL;
794 mappass->reqcopy.cmd = 0;
798 spin_unlock_irqrestore(&mappass->copy_lock, flags);
800 /* Tell the caller we don't need to send back a notification yet */
804 spin_unlock_irqrestore(&mappass->copy_lock, flags);
806 rsp = RING_GET_RESPONSE(&fedata->ring, fedata->ring.rsp_prod_pvt++);
807 rsp->req_id = req->req_id;
809 rsp->u.poll.id = req->u.poll.id;
814 static int pvcalls_back_handle_cmd(struct xenbus_device *dev,
815 struct xen_pvcalls_request *req)
821 ret = pvcalls_back_socket(dev, req);
823 case PVCALLS_CONNECT:
824 ret = pvcalls_back_connect(dev, req);
826 case PVCALLS_RELEASE:
827 ret = pvcalls_back_release(dev, req);
830 ret = pvcalls_back_bind(dev, req);
833 ret = pvcalls_back_listen(dev, req);
836 ret = pvcalls_back_accept(dev, req);
839 ret = pvcalls_back_poll(dev, req);
843 struct pvcalls_fedata *fedata;
844 struct xen_pvcalls_response *rsp;
846 fedata = dev_get_drvdata(&dev->dev);
847 rsp = RING_GET_RESPONSE(
848 &fedata->ring, fedata->ring.rsp_prod_pvt++);
849 rsp->req_id = req->req_id;
851 rsp->ret = -ENOTSUPP;
858 static void pvcalls_back_work(struct pvcalls_fedata *fedata)
860 int notify, notify_all = 0, more = 1;
861 struct xen_pvcalls_request req;
862 struct xenbus_device *dev = fedata->dev;
865 while (RING_HAS_UNCONSUMED_REQUESTS(&fedata->ring)) {
866 RING_COPY_REQUEST(&fedata->ring,
867 fedata->ring.req_cons++,
870 if (!pvcalls_back_handle_cmd(dev, &req)) {
871 RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(
872 &fedata->ring, notify);
873 notify_all += notify;
878 notify_remote_via_irq(fedata->irq);
882 RING_FINAL_CHECK_FOR_REQUESTS(&fedata->ring, more);
886 static irqreturn_t pvcalls_back_event(int irq, void *dev_id)
888 struct xenbus_device *dev = dev_id;
889 struct pvcalls_fedata *fedata = NULL;
890 unsigned int eoi_flags = XEN_EOI_FLAG_SPURIOUS;
893 fedata = dev_get_drvdata(&dev->dev);
895 pvcalls_back_work(fedata);
900 xen_irq_lateeoi(irq, eoi_flags);
905 static irqreturn_t pvcalls_back_conn_event(int irq, void *sock_map)
907 struct sock_mapping *map = sock_map;
908 struct pvcalls_ioworker *iow;
910 if (map == NULL || map->sock == NULL || map->sock->sk == NULL ||
911 map->sock->sk->sk_user_data != map) {
912 xen_irq_lateeoi(irq, 0);
916 iow = &map->ioworker;
918 atomic_inc(&map->write);
919 atomic_inc(&map->eoi);
920 atomic_inc(&map->io);
921 queue_work(iow->wq, &iow->register_work);
926 static int backend_connect(struct xenbus_device *dev)
929 evtchn_port_t evtchn;
930 grant_ref_t ring_ref;
931 struct pvcalls_fedata *fedata = NULL;
933 fedata = kzalloc(sizeof(struct pvcalls_fedata), GFP_KERNEL);
938 err = xenbus_scanf(XBT_NIL, dev->otherend, "port", "%u",
942 xenbus_dev_fatal(dev, err, "reading %s/event-channel",
947 err = xenbus_scanf(XBT_NIL, dev->otherend, "ring-ref", "%u", &ring_ref);
950 xenbus_dev_fatal(dev, err, "reading %s/ring-ref",
955 err = bind_interdomain_evtchn_to_irq_lateeoi(dev, evtchn);
960 err = request_threaded_irq(fedata->irq, NULL, pvcalls_back_event,
961 IRQF_ONESHOT, "pvcalls-back", dev);
965 err = xenbus_map_ring_valloc(dev, &ring_ref, 1,
966 (void **)&fedata->sring);
970 BACK_RING_INIT(&fedata->ring, fedata->sring, XEN_PAGE_SIZE * 1);
973 INIT_LIST_HEAD(&fedata->socket_mappings);
974 INIT_RADIX_TREE(&fedata->socketpass_mappings, GFP_KERNEL);
975 sema_init(&fedata->socket_lock, 1);
976 dev_set_drvdata(&dev->dev, fedata);
978 down(&pvcalls_back_global.frontends_lock);
979 list_add_tail(&fedata->list, &pvcalls_back_global.frontends);
980 up(&pvcalls_back_global.frontends_lock);
985 if (fedata->irq >= 0)
986 unbind_from_irqhandler(fedata->irq, dev);
987 if (fedata->sring != NULL)
988 xenbus_unmap_ring_vfree(dev, fedata->sring);
993 static int backend_disconnect(struct xenbus_device *dev)
995 struct pvcalls_fedata *fedata;
996 struct sock_mapping *map, *n;
997 struct sockpass_mapping *mappass;
998 struct radix_tree_iter iter;
1002 fedata = dev_get_drvdata(&dev->dev);
1004 down(&fedata->socket_lock);
1005 list_for_each_entry_safe(map, n, &fedata->socket_mappings, list) {
1006 list_del(&map->list);
1007 pvcalls_back_release_active(dev, fedata, map);
1010 radix_tree_for_each_slot(slot, &fedata->socketpass_mappings, &iter, 0) {
1011 mappass = radix_tree_deref_slot(slot);
1014 if (radix_tree_exception(mappass)) {
1015 if (radix_tree_deref_retry(mappass))
1016 slot = radix_tree_iter_retry(&iter);
1018 radix_tree_delete(&fedata->socketpass_mappings,
1020 pvcalls_back_release_passive(dev, fedata, mappass);
1023 up(&fedata->socket_lock);
1025 unbind_from_irqhandler(fedata->irq, dev);
1026 xenbus_unmap_ring_vfree(dev, fedata->sring);
1028 list_del(&fedata->list);
1030 dev_set_drvdata(&dev->dev, NULL);
1035 static int pvcalls_back_probe(struct xenbus_device *dev,
1036 const struct xenbus_device_id *id)
1039 struct xenbus_transaction xbt;
1044 err = xenbus_transaction_start(&xbt);
1046 pr_warn("%s cannot create xenstore transaction\n", __func__);
1050 err = xenbus_printf(xbt, dev->nodename, "versions", "%s",
1053 pr_warn("%s write out 'versions' failed\n", __func__);
1057 err = xenbus_printf(xbt, dev->nodename, "max-page-order", "%u",
1060 pr_warn("%s write out 'max-page-order' failed\n", __func__);
1064 err = xenbus_printf(xbt, dev->nodename, "function-calls",
1065 XENBUS_FUNCTIONS_CALLS);
1067 pr_warn("%s write out 'function-calls' failed\n", __func__);
1073 err = xenbus_transaction_end(xbt, abort);
1075 if (err == -EAGAIN && !abort)
1077 pr_warn("%s cannot complete xenstore transaction\n", __func__);
1084 xenbus_switch_state(dev, XenbusStateInitWait);
1089 static void set_backend_state(struct xenbus_device *dev,
1090 enum xenbus_state state)
1092 while (dev->state != state) {
1093 switch (dev->state) {
1094 case XenbusStateClosed:
1096 case XenbusStateInitWait:
1097 case XenbusStateConnected:
1098 xenbus_switch_state(dev, XenbusStateInitWait);
1100 case XenbusStateClosing:
1101 xenbus_switch_state(dev, XenbusStateClosing);
1107 case XenbusStateInitWait:
1108 case XenbusStateInitialised:
1110 case XenbusStateConnected:
1111 if (backend_connect(dev))
1113 xenbus_switch_state(dev, XenbusStateConnected);
1115 case XenbusStateClosing:
1116 case XenbusStateClosed:
1117 xenbus_switch_state(dev, XenbusStateClosing);
1123 case XenbusStateConnected:
1125 case XenbusStateInitWait:
1126 case XenbusStateClosing:
1127 case XenbusStateClosed:
1128 down(&pvcalls_back_global.frontends_lock);
1129 backend_disconnect(dev);
1130 up(&pvcalls_back_global.frontends_lock);
1131 xenbus_switch_state(dev, XenbusStateClosing);
1137 case XenbusStateClosing:
1139 case XenbusStateInitWait:
1140 case XenbusStateConnected:
1141 case XenbusStateClosed:
1142 xenbus_switch_state(dev, XenbusStateClosed);
1154 static void pvcalls_back_changed(struct xenbus_device *dev,
1155 enum xenbus_state frontend_state)
1157 switch (frontend_state) {
1158 case XenbusStateInitialising:
1159 set_backend_state(dev, XenbusStateInitWait);
1162 case XenbusStateInitialised:
1163 case XenbusStateConnected:
1164 set_backend_state(dev, XenbusStateConnected);
1167 case XenbusStateClosing:
1168 set_backend_state(dev, XenbusStateClosing);
1171 case XenbusStateClosed:
1172 set_backend_state(dev, XenbusStateClosed);
1173 if (xenbus_dev_is_online(dev))
1175 device_unregister(&dev->dev);
1177 case XenbusStateUnknown:
1178 set_backend_state(dev, XenbusStateClosed);
1179 device_unregister(&dev->dev);
1183 xenbus_dev_fatal(dev, -EINVAL, "saw state %d at frontend",
1189 static void pvcalls_back_remove(struct xenbus_device *dev)
1193 static int pvcalls_back_uevent(const struct xenbus_device *xdev,
1194 struct kobj_uevent_env *env)
1199 static const struct xenbus_device_id pvcalls_back_ids[] = {
1204 static struct xenbus_driver pvcalls_back_driver = {
1205 .ids = pvcalls_back_ids,
1206 .probe = pvcalls_back_probe,
1207 .remove = pvcalls_back_remove,
1208 .uevent = pvcalls_back_uevent,
1209 .otherend_changed = pvcalls_back_changed,
1212 static int __init pvcalls_back_init(void)
1219 ret = xenbus_register_backend(&pvcalls_back_driver);
1223 sema_init(&pvcalls_back_global.frontends_lock, 1);
1224 INIT_LIST_HEAD(&pvcalls_back_global.frontends);
1227 module_init(pvcalls_back_init);
1229 static void __exit pvcalls_back_fin(void)
1231 struct pvcalls_fedata *fedata, *nfedata;
1233 down(&pvcalls_back_global.frontends_lock);
1234 list_for_each_entry_safe(fedata, nfedata,
1235 &pvcalls_back_global.frontends, list) {
1236 backend_disconnect(fedata->dev);
1238 up(&pvcalls_back_global.frontends_lock);
1240 xenbus_unregister_driver(&pvcalls_back_driver);
1243 module_exit(pvcalls_back_fin);
1245 MODULE_DESCRIPTION("Xen PV Calls backend driver");
1246 MODULE_AUTHOR("Stefano Stabellini <sstabellini@kernel.org>");
1247 MODULE_LICENSE("GPL");