blob: 1618502c1c661741415de0301b898a526c918197 [file] [log] [blame]
Stefano Stabellini416efba2017-10-30 15:40:51 -07001/*
2 * (c) 2017 Stefano Stabellini <stefano@aporeto.com>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 */
14
15#include <linux/module.h>
16
17#include <xen/events.h>
18#include <xen/grant_table.h>
19#include <xen/xen.h>
20#include <xen/xenbus.h>
21#include <xen/interface/io/pvcalls.h>
22
Stefano Stabelliniaa7ba372017-10-30 15:40:52 -070023#define PVCALLS_INVALID_ID UINT_MAX
24#define PVCALLS_RING_ORDER XENBUS_MAX_RING_GRANT_ORDER
25#define PVCALLS_NR_RSP_PER_RING __CONST_RING_SIZE(xen_pvcalls, XEN_PAGE_SIZE)
26
27struct pvcalls_bedata {
28 struct xen_pvcalls_front_ring ring;
29 grant_ref_t ref;
30 int irq;
31
32 struct list_head socket_mappings;
33 spinlock_t socket_lock;
34
35 wait_queue_head_t inflight_req;
36 struct xen_pvcalls_response rsp[PVCALLS_NR_RSP_PER_RING];
37};
38/* Only one front/back connection supported. */
39static struct xenbus_device *pvcalls_front_dev;
40static atomic_t pvcalls_refcount;
41
42/* first increment refcount, then proceed */
43#define pvcalls_enter() { \
44 atomic_inc(&pvcalls_refcount); \
45}
46
47/* first complete other operations, then decrement refcount */
48#define pvcalls_exit() { \
49 atomic_dec(&pvcalls_refcount); \
50}
51
52struct sock_mapping {
53 bool active_socket;
54 struct list_head list;
55 struct socket *sock;
56};
57
58static irqreturn_t pvcalls_front_event_handler(int irq, void *dev_id)
59{
60 return IRQ_HANDLED;
61}
62
63static void pvcalls_front_free_map(struct pvcalls_bedata *bedata,
64 struct sock_mapping *map)
65{
66}
67
Stefano Stabellini416efba2017-10-30 15:40:51 -070068static const struct xenbus_device_id pvcalls_front_ids[] = {
69 { "pvcalls" },
70 { "" }
71};
72
73static int pvcalls_front_remove(struct xenbus_device *dev)
74{
Stefano Stabelliniaa7ba372017-10-30 15:40:52 -070075 struct pvcalls_bedata *bedata;
76 struct sock_mapping *map = NULL, *n;
77
78 bedata = dev_get_drvdata(&pvcalls_front_dev->dev);
79 dev_set_drvdata(&dev->dev, NULL);
80 pvcalls_front_dev = NULL;
81 if (bedata->irq >= 0)
82 unbind_from_irqhandler(bedata->irq, dev);
83
84 smp_mb();
85 while (atomic_read(&pvcalls_refcount) > 0)
86 cpu_relax();
87 list_for_each_entry_safe(map, n, &bedata->socket_mappings, list) {
88 if (map->active_socket) {
89 /* No need to lock, refcount is 0 */
90 pvcalls_front_free_map(bedata, map);
91 } else {
92 list_del(&map->list);
93 kfree(map);
94 }
95 }
96 if (bedata->ref >= 0)
97 gnttab_end_foreign_access(bedata->ref, 0, 0);
98 kfree(bedata->ring.sring);
99 kfree(bedata);
100 xenbus_switch_state(dev, XenbusStateClosed);
Stefano Stabellini416efba2017-10-30 15:40:51 -0700101 return 0;
102}
103
104static int pvcalls_front_probe(struct xenbus_device *dev,
105 const struct xenbus_device_id *id)
106{
Stefano Stabellini21968192017-10-30 15:40:53 -0700107 int ret = -ENOMEM, evtchn, i;
108 unsigned int max_page_order, function_calls, len;
109 char *versions;
110 grant_ref_t gref_head = 0;
111 struct xenbus_transaction xbt;
112 struct pvcalls_bedata *bedata = NULL;
113 struct xen_pvcalls_sring *sring;
114
115 if (pvcalls_front_dev != NULL) {
116 dev_err(&dev->dev, "only one PV Calls connection supported\n");
117 return -EINVAL;
118 }
119
120 versions = xenbus_read(XBT_NIL, dev->otherend, "versions", &len);
121 if (!len)
122 return -EINVAL;
123 if (strcmp(versions, "1")) {
124 kfree(versions);
125 return -EINVAL;
126 }
127 kfree(versions);
128 max_page_order = xenbus_read_unsigned(dev->otherend,
129 "max-page-order", 0);
130 if (max_page_order < PVCALLS_RING_ORDER)
131 return -ENODEV;
132 function_calls = xenbus_read_unsigned(dev->otherend,
133 "function-calls", 0);
134 /* See XENBUS_FUNCTIONS_CALLS in pvcalls.h */
135 if (function_calls != 1)
136 return -ENODEV;
137 pr_info("%s max-page-order is %u\n", __func__, max_page_order);
138
139 bedata = kzalloc(sizeof(struct pvcalls_bedata), GFP_KERNEL);
140 if (!bedata)
141 return -ENOMEM;
142
143 dev_set_drvdata(&dev->dev, bedata);
144 pvcalls_front_dev = dev;
145 init_waitqueue_head(&bedata->inflight_req);
146 INIT_LIST_HEAD(&bedata->socket_mappings);
147 spin_lock_init(&bedata->socket_lock);
148 bedata->irq = -1;
149 bedata->ref = -1;
150
151 for (i = 0; i < PVCALLS_NR_RSP_PER_RING; i++)
152 bedata->rsp[i].req_id = PVCALLS_INVALID_ID;
153
154 sring = (struct xen_pvcalls_sring *) __get_free_page(GFP_KERNEL |
155 __GFP_ZERO);
156 if (!sring)
157 goto error;
158 SHARED_RING_INIT(sring);
159 FRONT_RING_INIT(&bedata->ring, sring, XEN_PAGE_SIZE);
160
161 ret = xenbus_alloc_evtchn(dev, &evtchn);
162 if (ret)
163 goto error;
164
165 bedata->irq = bind_evtchn_to_irqhandler(evtchn,
166 pvcalls_front_event_handler,
167 0, "pvcalls-frontend", dev);
168 if (bedata->irq < 0) {
169 ret = bedata->irq;
170 goto error;
171 }
172
173 ret = gnttab_alloc_grant_references(1, &gref_head);
174 if (ret < 0)
175 goto error;
176 bedata->ref = gnttab_claim_grant_reference(&gref_head);
177 if (bedata->ref < 0) {
178 ret = bedata->ref;
179 goto error;
180 }
181 gnttab_grant_foreign_access_ref(bedata->ref, dev->otherend_id,
182 virt_to_gfn((void *)sring), 0);
183
184 again:
185 ret = xenbus_transaction_start(&xbt);
186 if (ret) {
187 xenbus_dev_fatal(dev, ret, "starting transaction");
188 goto error;
189 }
190 ret = xenbus_printf(xbt, dev->nodename, "version", "%u", 1);
191 if (ret)
192 goto error_xenbus;
193 ret = xenbus_printf(xbt, dev->nodename, "ring-ref", "%d", bedata->ref);
194 if (ret)
195 goto error_xenbus;
196 ret = xenbus_printf(xbt, dev->nodename, "port", "%u",
197 evtchn);
198 if (ret)
199 goto error_xenbus;
200 ret = xenbus_transaction_end(xbt, 0);
201 if (ret) {
202 if (ret == -EAGAIN)
203 goto again;
204 xenbus_dev_fatal(dev, ret, "completing transaction");
205 goto error;
206 }
207 xenbus_switch_state(dev, XenbusStateInitialised);
208
Stefano Stabellini416efba2017-10-30 15:40:51 -0700209 return 0;
Stefano Stabellini21968192017-10-30 15:40:53 -0700210
211 error_xenbus:
212 xenbus_transaction_end(xbt, 1);
213 xenbus_dev_fatal(dev, ret, "writing xenstore");
214 error:
215 pvcalls_front_remove(dev);
216 return ret;
Stefano Stabellini416efba2017-10-30 15:40:51 -0700217}
218
219static void pvcalls_front_changed(struct xenbus_device *dev,
220 enum xenbus_state backend_state)
221{
Stefano Stabellini21968192017-10-30 15:40:53 -0700222 switch (backend_state) {
223 case XenbusStateReconfiguring:
224 case XenbusStateReconfigured:
225 case XenbusStateInitialising:
226 case XenbusStateInitialised:
227 case XenbusStateUnknown:
228 break;
229
230 case XenbusStateInitWait:
231 break;
232
233 case XenbusStateConnected:
234 xenbus_switch_state(dev, XenbusStateConnected);
235 break;
236
237 case XenbusStateClosed:
238 if (dev->state == XenbusStateClosed)
239 break;
240 /* Missed the backend's CLOSING state -- fallthrough */
241 case XenbusStateClosing:
242 xenbus_frontend_closed(dev);
243 break;
244 }
Stefano Stabellini416efba2017-10-30 15:40:51 -0700245}
246
247static struct xenbus_driver pvcalls_front_driver = {
248 .ids = pvcalls_front_ids,
249 .probe = pvcalls_front_probe,
250 .remove = pvcalls_front_remove,
251 .otherend_changed = pvcalls_front_changed,
252};
253
254static int __init pvcalls_frontend_init(void)
255{
256 if (!xen_domain())
257 return -ENODEV;
258
259 pr_info("Initialising Xen pvcalls frontend driver\n");
260
261 return xenbus_register_frontend(&pvcalls_front_driver);
262}
263
264module_init(pvcalls_frontend_init);