pvcalls-front.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258
  1. /*
  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. #include <linux/module.h>
  15. #include <linux/net.h>
  16. #include <linux/socket.h>
  17. #include <net/sock.h>
  18. #include <xen/events.h>
  19. #include <xen/grant_table.h>
  20. #include <xen/xen.h>
  21. #include <xen/xenbus.h>
  22. #include <xen/interface/io/pvcalls.h>
  23. #include "pvcalls-front.h"
  24. #define PVCALLS_INVALID_ID UINT_MAX
  25. #define PVCALLS_RING_ORDER XENBUS_MAX_RING_GRANT_ORDER
  26. #define PVCALLS_NR_RSP_PER_RING __CONST_RING_SIZE(xen_pvcalls, XEN_PAGE_SIZE)
  27. #define PVCALLS_FRONT_MAX_SPIN 5000
  28. struct pvcalls_bedata {
  29. struct xen_pvcalls_front_ring ring;
  30. grant_ref_t ref;
  31. int irq;
  32. struct list_head socket_mappings;
  33. spinlock_t socket_lock;
  34. wait_queue_head_t inflight_req;
  35. struct xen_pvcalls_response rsp[PVCALLS_NR_RSP_PER_RING];
  36. };
  37. /* Only one front/back connection supported. */
  38. static struct xenbus_device *pvcalls_front_dev;
  39. static atomic_t pvcalls_refcount;
  40. /* first increment refcount, then proceed */
  41. #define pvcalls_enter() { \
  42. atomic_inc(&pvcalls_refcount); \
  43. }
  44. /* first complete other operations, then decrement refcount */
  45. #define pvcalls_exit() { \
  46. atomic_dec(&pvcalls_refcount); \
  47. }
  48. struct sock_mapping {
  49. bool active_socket;
  50. struct list_head list;
  51. struct socket *sock;
  52. atomic_t refcount;
  53. union {
  54. struct {
  55. int irq;
  56. grant_ref_t ref;
  57. struct pvcalls_data_intf *ring;
  58. struct pvcalls_data data;
  59. struct mutex in_mutex;
  60. struct mutex out_mutex;
  61. wait_queue_head_t inflight_conn_req;
  62. } active;
  63. struct {
  64. /*
  65. * Socket status, needs to be 64-bit aligned due to the
  66. * test_and_* functions which have this requirement on arm64.
  67. */
  68. #define PVCALLS_STATUS_UNINITALIZED 0
  69. #define PVCALLS_STATUS_BIND 1
  70. #define PVCALLS_STATUS_LISTEN 2
  71. uint8_t status __attribute__((aligned(8)));
  72. /*
  73. * Internal state-machine flags.
  74. * Only one accept operation can be inflight for a socket.
  75. * Only one poll operation can be inflight for a given socket.
  76. * flags needs to be 64-bit aligned due to the test_and_*
  77. * functions which have this requirement on arm64.
  78. */
  79. #define PVCALLS_FLAG_ACCEPT_INFLIGHT 0
  80. #define PVCALLS_FLAG_POLL_INFLIGHT 1
  81. #define PVCALLS_FLAG_POLL_RET 2
  82. uint8_t flags __attribute__((aligned(8)));
  83. uint32_t inflight_req_id;
  84. struct sock_mapping *accept_map;
  85. wait_queue_head_t inflight_accept_req;
  86. } passive;
  87. };
  88. };
  89. static inline struct sock_mapping *pvcalls_enter_sock(struct socket *sock)
  90. {
  91. struct sock_mapping *map;
  92. if (!pvcalls_front_dev ||
  93. dev_get_drvdata(&pvcalls_front_dev->dev) == NULL)
  94. return ERR_PTR(-ENOTCONN);
  95. map = (struct sock_mapping *)sock->sk->sk_send_head;
  96. if (map == NULL)
  97. return ERR_PTR(-ENOTSOCK);
  98. pvcalls_enter();
  99. atomic_inc(&map->refcount);
  100. return map;
  101. }
  102. static inline void pvcalls_exit_sock(struct socket *sock)
  103. {
  104. struct sock_mapping *map;
  105. map = (struct sock_mapping *)sock->sk->sk_send_head;
  106. atomic_dec(&map->refcount);
  107. pvcalls_exit();
  108. }
  109. static inline int get_request(struct pvcalls_bedata *bedata, int *req_id)
  110. {
  111. *req_id = bedata->ring.req_prod_pvt & (RING_SIZE(&bedata->ring) - 1);
  112. if (RING_FULL(&bedata->ring) ||
  113. bedata->rsp[*req_id].req_id != PVCALLS_INVALID_ID)
  114. return -EAGAIN;
  115. return 0;
  116. }
  117. static bool pvcalls_front_write_todo(struct sock_mapping *map)
  118. {
  119. struct pvcalls_data_intf *intf = map->active.ring;
  120. RING_IDX cons, prod, size = XEN_FLEX_RING_SIZE(PVCALLS_RING_ORDER);
  121. int32_t error;
  122. error = intf->out_error;
  123. if (error == -ENOTCONN)
  124. return false;
  125. if (error != 0)
  126. return true;
  127. cons = intf->out_cons;
  128. prod = intf->out_prod;
  129. return !!(size - pvcalls_queued(prod, cons, size));
  130. }
  131. static bool pvcalls_front_read_todo(struct sock_mapping *map)
  132. {
  133. struct pvcalls_data_intf *intf = map->active.ring;
  134. RING_IDX cons, prod;
  135. int32_t error;
  136. cons = intf->in_cons;
  137. prod = intf->in_prod;
  138. error = intf->in_error;
  139. return (error != 0 ||
  140. pvcalls_queued(prod, cons,
  141. XEN_FLEX_RING_SIZE(PVCALLS_RING_ORDER)) != 0);
  142. }
  143. static irqreturn_t pvcalls_front_event_handler(int irq, void *dev_id)
  144. {
  145. struct xenbus_device *dev = dev_id;
  146. struct pvcalls_bedata *bedata;
  147. struct xen_pvcalls_response *rsp;
  148. uint8_t *src, *dst;
  149. int req_id = 0, more = 0, done = 0;
  150. if (dev == NULL)
  151. return IRQ_HANDLED;
  152. pvcalls_enter();
  153. bedata = dev_get_drvdata(&dev->dev);
  154. if (bedata == NULL) {
  155. pvcalls_exit();
  156. return IRQ_HANDLED;
  157. }
  158. again:
  159. while (RING_HAS_UNCONSUMED_RESPONSES(&bedata->ring)) {
  160. rsp = RING_GET_RESPONSE(&bedata->ring, bedata->ring.rsp_cons);
  161. req_id = rsp->req_id;
  162. if (rsp->cmd == PVCALLS_POLL) {
  163. struct sock_mapping *map = (struct sock_mapping *)(uintptr_t)
  164. rsp->u.poll.id;
  165. clear_bit(PVCALLS_FLAG_POLL_INFLIGHT,
  166. (void *)&map->passive.flags);
  167. /*
  168. * clear INFLIGHT, then set RET. It pairs with
  169. * the checks at the beginning of
  170. * pvcalls_front_poll_passive.
  171. */
  172. smp_wmb();
  173. set_bit(PVCALLS_FLAG_POLL_RET,
  174. (void *)&map->passive.flags);
  175. } else {
  176. dst = (uint8_t *)&bedata->rsp[req_id] +
  177. sizeof(rsp->req_id);
  178. src = (uint8_t *)rsp + sizeof(rsp->req_id);
  179. memcpy(dst, src, sizeof(*rsp) - sizeof(rsp->req_id));
  180. /*
  181. * First copy the rest of the data, then req_id. It is
  182. * paired with the barrier when accessing bedata->rsp.
  183. */
  184. smp_wmb();
  185. bedata->rsp[req_id].req_id = req_id;
  186. }
  187. done = 1;
  188. bedata->ring.rsp_cons++;
  189. }
  190. RING_FINAL_CHECK_FOR_RESPONSES(&bedata->ring, more);
  191. if (more)
  192. goto again;
  193. if (done)
  194. wake_up(&bedata->inflight_req);
  195. pvcalls_exit();
  196. return IRQ_HANDLED;
  197. }
  198. static void pvcalls_front_free_map(struct pvcalls_bedata *bedata,
  199. struct sock_mapping *map)
  200. {
  201. int i;
  202. unbind_from_irqhandler(map->active.irq, map);
  203. spin_lock(&bedata->socket_lock);
  204. if (!list_empty(&map->list))
  205. list_del_init(&map->list);
  206. spin_unlock(&bedata->socket_lock);
  207. for (i = 0; i < (1 << PVCALLS_RING_ORDER); i++)
  208. gnttab_end_foreign_access(map->active.ring->ref[i], 0, 0);
  209. gnttab_end_foreign_access(map->active.ref, 0, 0);
  210. free_page((unsigned long)map->active.ring);
  211. kfree(map);
  212. }
  213. static irqreturn_t pvcalls_front_conn_handler(int irq, void *sock_map)
  214. {
  215. struct sock_mapping *map = sock_map;
  216. if (map == NULL)
  217. return IRQ_HANDLED;
  218. wake_up_interruptible(&map->active.inflight_conn_req);
  219. return IRQ_HANDLED;
  220. }
  221. int pvcalls_front_socket(struct socket *sock)
  222. {
  223. struct pvcalls_bedata *bedata;
  224. struct sock_mapping *map = NULL;
  225. struct xen_pvcalls_request *req;
  226. int notify, req_id, ret;
  227. /*
  228. * PVCalls only supports domain AF_INET,
  229. * type SOCK_STREAM and protocol 0 sockets for now.
  230. *
  231. * Check socket type here, AF_INET and protocol checks are done
  232. * by the caller.
  233. */
  234. if (sock->type != SOCK_STREAM)
  235. return -EOPNOTSUPP;
  236. pvcalls_enter();
  237. if (!pvcalls_front_dev) {
  238. pvcalls_exit();
  239. return -EACCES;
  240. }
  241. bedata = dev_get_drvdata(&pvcalls_front_dev->dev);
  242. map = kzalloc(sizeof(*map), GFP_KERNEL);
  243. if (map == NULL) {
  244. pvcalls_exit();
  245. return -ENOMEM;
  246. }
  247. spin_lock(&bedata->socket_lock);
  248. ret = get_request(bedata, &req_id);
  249. if (ret < 0) {
  250. kfree(map);
  251. spin_unlock(&bedata->socket_lock);
  252. pvcalls_exit();
  253. return ret;
  254. }
  255. /*
  256. * sock->sk->sk_send_head is not used for ip sockets: reuse the
  257. * field to store a pointer to the struct sock_mapping
  258. * corresponding to the socket. This way, we can easily get the
  259. * struct sock_mapping from the struct socket.
  260. */
  261. sock->sk->sk_send_head = (void *)map;
  262. list_add_tail(&map->list, &bedata->socket_mappings);
  263. req = RING_GET_REQUEST(&bedata->ring, req_id);
  264. req->req_id = req_id;
  265. req->cmd = PVCALLS_SOCKET;
  266. req->u.socket.id = (uintptr_t) map;
  267. req->u.socket.domain = AF_INET;
  268. req->u.socket.type = SOCK_STREAM;
  269. req->u.socket.protocol = IPPROTO_IP;
  270. bedata->ring.req_prod_pvt++;
  271. RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&bedata->ring, notify);
  272. spin_unlock(&bedata->socket_lock);
  273. if (notify)
  274. notify_remote_via_irq(bedata->irq);
  275. wait_event(bedata->inflight_req,
  276. READ_ONCE(bedata->rsp[req_id].req_id) == req_id);
  277. /* read req_id, then the content */
  278. smp_rmb();
  279. ret = bedata->rsp[req_id].ret;
  280. bedata->rsp[req_id].req_id = PVCALLS_INVALID_ID;
  281. pvcalls_exit();
  282. return ret;
  283. }
  284. static int create_active(struct sock_mapping *map, int *evtchn)
  285. {
  286. void *bytes;
  287. int ret = -ENOMEM, irq = -1, i;
  288. *evtchn = -1;
  289. init_waitqueue_head(&map->active.inflight_conn_req);
  290. map->active.ring = (struct pvcalls_data_intf *)
  291. __get_free_page(GFP_KERNEL | __GFP_ZERO);
  292. if (map->active.ring == NULL)
  293. goto out_error;
  294. map->active.ring->ring_order = PVCALLS_RING_ORDER;
  295. bytes = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
  296. PVCALLS_RING_ORDER);
  297. if (bytes == NULL)
  298. goto out_error;
  299. for (i = 0; i < (1 << PVCALLS_RING_ORDER); i++)
  300. map->active.ring->ref[i] = gnttab_grant_foreign_access(
  301. pvcalls_front_dev->otherend_id,
  302. pfn_to_gfn(virt_to_pfn(bytes) + i), 0);
  303. map->active.ref = gnttab_grant_foreign_access(
  304. pvcalls_front_dev->otherend_id,
  305. pfn_to_gfn(virt_to_pfn((void *)map->active.ring)), 0);
  306. map->active.data.in = bytes;
  307. map->active.data.out = bytes +
  308. XEN_FLEX_RING_SIZE(PVCALLS_RING_ORDER);
  309. ret = xenbus_alloc_evtchn(pvcalls_front_dev, evtchn);
  310. if (ret)
  311. goto out_error;
  312. irq = bind_evtchn_to_irqhandler(*evtchn, pvcalls_front_conn_handler,
  313. 0, "pvcalls-frontend", map);
  314. if (irq < 0) {
  315. ret = irq;
  316. goto out_error;
  317. }
  318. map->active.irq = irq;
  319. map->active_socket = true;
  320. mutex_init(&map->active.in_mutex);
  321. mutex_init(&map->active.out_mutex);
  322. return 0;
  323. out_error:
  324. if (*evtchn >= 0)
  325. xenbus_free_evtchn(pvcalls_front_dev, *evtchn);
  326. free_pages((unsigned long)map->active.data.in, PVCALLS_RING_ORDER);
  327. free_page((unsigned long)map->active.ring);
  328. return ret;
  329. }
  330. int pvcalls_front_connect(struct socket *sock, struct sockaddr *addr,
  331. int addr_len, int flags)
  332. {
  333. struct pvcalls_bedata *bedata;
  334. struct sock_mapping *map = NULL;
  335. struct xen_pvcalls_request *req;
  336. int notify, req_id, ret, evtchn;
  337. if (addr->sa_family != AF_INET || sock->type != SOCK_STREAM)
  338. return -EOPNOTSUPP;
  339. map = pvcalls_enter_sock(sock);
  340. if (IS_ERR(map))
  341. return PTR_ERR(map);
  342. bedata = dev_get_drvdata(&pvcalls_front_dev->dev);
  343. spin_lock(&bedata->socket_lock);
  344. ret = get_request(bedata, &req_id);
  345. if (ret < 0) {
  346. spin_unlock(&bedata->socket_lock);
  347. pvcalls_exit_sock(sock);
  348. return ret;
  349. }
  350. ret = create_active(map, &evtchn);
  351. if (ret < 0) {
  352. spin_unlock(&bedata->socket_lock);
  353. pvcalls_exit_sock(sock);
  354. return ret;
  355. }
  356. req = RING_GET_REQUEST(&bedata->ring, req_id);
  357. req->req_id = req_id;
  358. req->cmd = PVCALLS_CONNECT;
  359. req->u.connect.id = (uintptr_t)map;
  360. req->u.connect.len = addr_len;
  361. req->u.connect.flags = flags;
  362. req->u.connect.ref = map->active.ref;
  363. req->u.connect.evtchn = evtchn;
  364. memcpy(req->u.connect.addr, addr, sizeof(*addr));
  365. map->sock = sock;
  366. bedata->ring.req_prod_pvt++;
  367. RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&bedata->ring, notify);
  368. spin_unlock(&bedata->socket_lock);
  369. if (notify)
  370. notify_remote_via_irq(bedata->irq);
  371. wait_event(bedata->inflight_req,
  372. READ_ONCE(bedata->rsp[req_id].req_id) == req_id);
  373. /* read req_id, then the content */
  374. smp_rmb();
  375. ret = bedata->rsp[req_id].ret;
  376. bedata->rsp[req_id].req_id = PVCALLS_INVALID_ID;
  377. pvcalls_exit_sock(sock);
  378. return ret;
  379. }
  380. static int __write_ring(struct pvcalls_data_intf *intf,
  381. struct pvcalls_data *data,
  382. struct iov_iter *msg_iter,
  383. int len)
  384. {
  385. RING_IDX cons, prod, size, masked_prod, masked_cons;
  386. RING_IDX array_size = XEN_FLEX_RING_SIZE(PVCALLS_RING_ORDER);
  387. int32_t error;
  388. error = intf->out_error;
  389. if (error < 0)
  390. return error;
  391. cons = intf->out_cons;
  392. prod = intf->out_prod;
  393. /* read indexes before continuing */
  394. virt_mb();
  395. size = pvcalls_queued(prod, cons, array_size);
  396. if (size >= array_size)
  397. return -EINVAL;
  398. if (len > array_size - size)
  399. len = array_size - size;
  400. masked_prod = pvcalls_mask(prod, array_size);
  401. masked_cons = pvcalls_mask(cons, array_size);
  402. if (masked_prod < masked_cons) {
  403. len = copy_from_iter(data->out + masked_prod, len, msg_iter);
  404. } else {
  405. if (len > array_size - masked_prod) {
  406. int ret = copy_from_iter(data->out + masked_prod,
  407. array_size - masked_prod, msg_iter);
  408. if (ret != array_size - masked_prod) {
  409. len = ret;
  410. goto out;
  411. }
  412. len = ret + copy_from_iter(data->out, len - ret, msg_iter);
  413. } else {
  414. len = copy_from_iter(data->out + masked_prod, len, msg_iter);
  415. }
  416. }
  417. out:
  418. /* write to ring before updating pointer */
  419. virt_wmb();
  420. intf->out_prod += len;
  421. return len;
  422. }
  423. int pvcalls_front_sendmsg(struct socket *sock, struct msghdr *msg,
  424. size_t len)
  425. {
  426. struct pvcalls_bedata *bedata;
  427. struct sock_mapping *map;
  428. int sent, tot_sent = 0;
  429. int count = 0, flags;
  430. flags = msg->msg_flags;
  431. if (flags & (MSG_CONFIRM|MSG_DONTROUTE|MSG_EOR|MSG_OOB))
  432. return -EOPNOTSUPP;
  433. map = pvcalls_enter_sock(sock);
  434. if (IS_ERR(map))
  435. return PTR_ERR(map);
  436. bedata = dev_get_drvdata(&pvcalls_front_dev->dev);
  437. mutex_lock(&map->active.out_mutex);
  438. if ((flags & MSG_DONTWAIT) && !pvcalls_front_write_todo(map)) {
  439. mutex_unlock(&map->active.out_mutex);
  440. pvcalls_exit_sock(sock);
  441. return -EAGAIN;
  442. }
  443. if (len > INT_MAX)
  444. len = INT_MAX;
  445. again:
  446. count++;
  447. sent = __write_ring(map->active.ring,
  448. &map->active.data, &msg->msg_iter,
  449. len);
  450. if (sent > 0) {
  451. len -= sent;
  452. tot_sent += sent;
  453. notify_remote_via_irq(map->active.irq);
  454. }
  455. if (sent >= 0 && len > 0 && count < PVCALLS_FRONT_MAX_SPIN)
  456. goto again;
  457. if (sent < 0)
  458. tot_sent = sent;
  459. mutex_unlock(&map->active.out_mutex);
  460. pvcalls_exit_sock(sock);
  461. return tot_sent;
  462. }
  463. static int __read_ring(struct pvcalls_data_intf *intf,
  464. struct pvcalls_data *data,
  465. struct iov_iter *msg_iter,
  466. size_t len, int flags)
  467. {
  468. RING_IDX cons, prod, size, masked_prod, masked_cons;
  469. RING_IDX array_size = XEN_FLEX_RING_SIZE(PVCALLS_RING_ORDER);
  470. int32_t error;
  471. cons = intf->in_cons;
  472. prod = intf->in_prod;
  473. error = intf->in_error;
  474. /* get pointers before reading from the ring */
  475. virt_rmb();
  476. if (error < 0)
  477. return error;
  478. size = pvcalls_queued(prod, cons, array_size);
  479. masked_prod = pvcalls_mask(prod, array_size);
  480. masked_cons = pvcalls_mask(cons, array_size);
  481. if (size == 0)
  482. return 0;
  483. if (len > size)
  484. len = size;
  485. if (masked_prod > masked_cons) {
  486. len = copy_to_iter(data->in + masked_cons, len, msg_iter);
  487. } else {
  488. if (len > (array_size - masked_cons)) {
  489. int ret = copy_to_iter(data->in + masked_cons,
  490. array_size - masked_cons, msg_iter);
  491. if (ret != array_size - masked_cons) {
  492. len = ret;
  493. goto out;
  494. }
  495. len = ret + copy_to_iter(data->in, len - ret, msg_iter);
  496. } else {
  497. len = copy_to_iter(data->in + masked_cons, len, msg_iter);
  498. }
  499. }
  500. out:
  501. /* read data from the ring before increasing the index */
  502. virt_mb();
  503. if (!(flags & MSG_PEEK))
  504. intf->in_cons += len;
  505. return len;
  506. }
  507. int pvcalls_front_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
  508. int flags)
  509. {
  510. struct pvcalls_bedata *bedata;
  511. int ret;
  512. struct sock_mapping *map;
  513. if (flags & (MSG_CMSG_CLOEXEC|MSG_ERRQUEUE|MSG_OOB|MSG_TRUNC))
  514. return -EOPNOTSUPP;
  515. map = pvcalls_enter_sock(sock);
  516. if (IS_ERR(map))
  517. return PTR_ERR(map);
  518. bedata = dev_get_drvdata(&pvcalls_front_dev->dev);
  519. mutex_lock(&map->active.in_mutex);
  520. if (len > XEN_FLEX_RING_SIZE(PVCALLS_RING_ORDER))
  521. len = XEN_FLEX_RING_SIZE(PVCALLS_RING_ORDER);
  522. while (!(flags & MSG_DONTWAIT) && !pvcalls_front_read_todo(map)) {
  523. wait_event_interruptible(map->active.inflight_conn_req,
  524. pvcalls_front_read_todo(map));
  525. }
  526. ret = __read_ring(map->active.ring, &map->active.data,
  527. &msg->msg_iter, len, flags);
  528. if (ret > 0)
  529. notify_remote_via_irq(map->active.irq);
  530. if (ret == 0)
  531. ret = (flags & MSG_DONTWAIT) ? -EAGAIN : 0;
  532. if (ret == -ENOTCONN)
  533. ret = 0;
  534. mutex_unlock(&map->active.in_mutex);
  535. pvcalls_exit_sock(sock);
  536. return ret;
  537. }
  538. int pvcalls_front_bind(struct socket *sock, struct sockaddr *addr, int addr_len)
  539. {
  540. struct pvcalls_bedata *bedata;
  541. struct sock_mapping *map = NULL;
  542. struct xen_pvcalls_request *req;
  543. int notify, req_id, ret;
  544. if (addr->sa_family != AF_INET || sock->type != SOCK_STREAM)
  545. return -EOPNOTSUPP;
  546. map = pvcalls_enter_sock(sock);
  547. if (IS_ERR(map))
  548. return PTR_ERR(map);
  549. bedata = dev_get_drvdata(&pvcalls_front_dev->dev);
  550. spin_lock(&bedata->socket_lock);
  551. ret = get_request(bedata, &req_id);
  552. if (ret < 0) {
  553. spin_unlock(&bedata->socket_lock);
  554. pvcalls_exit_sock(sock);
  555. return ret;
  556. }
  557. req = RING_GET_REQUEST(&bedata->ring, req_id);
  558. req->req_id = req_id;
  559. map->sock = sock;
  560. req->cmd = PVCALLS_BIND;
  561. req->u.bind.id = (uintptr_t)map;
  562. memcpy(req->u.bind.addr, addr, sizeof(*addr));
  563. req->u.bind.len = addr_len;
  564. init_waitqueue_head(&map->passive.inflight_accept_req);
  565. map->active_socket = false;
  566. bedata->ring.req_prod_pvt++;
  567. RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&bedata->ring, notify);
  568. spin_unlock(&bedata->socket_lock);
  569. if (notify)
  570. notify_remote_via_irq(bedata->irq);
  571. wait_event(bedata->inflight_req,
  572. READ_ONCE(bedata->rsp[req_id].req_id) == req_id);
  573. /* read req_id, then the content */
  574. smp_rmb();
  575. ret = bedata->rsp[req_id].ret;
  576. bedata->rsp[req_id].req_id = PVCALLS_INVALID_ID;
  577. map->passive.status = PVCALLS_STATUS_BIND;
  578. pvcalls_exit_sock(sock);
  579. return 0;
  580. }
  581. int pvcalls_front_listen(struct socket *sock, int backlog)
  582. {
  583. struct pvcalls_bedata *bedata;
  584. struct sock_mapping *map;
  585. struct xen_pvcalls_request *req;
  586. int notify, req_id, ret;
  587. map = pvcalls_enter_sock(sock);
  588. if (IS_ERR(map))
  589. return PTR_ERR(map);
  590. bedata = dev_get_drvdata(&pvcalls_front_dev->dev);
  591. if (map->passive.status != PVCALLS_STATUS_BIND) {
  592. pvcalls_exit_sock(sock);
  593. return -EOPNOTSUPP;
  594. }
  595. spin_lock(&bedata->socket_lock);
  596. ret = get_request(bedata, &req_id);
  597. if (ret < 0) {
  598. spin_unlock(&bedata->socket_lock);
  599. pvcalls_exit_sock(sock);
  600. return ret;
  601. }
  602. req = RING_GET_REQUEST(&bedata->ring, req_id);
  603. req->req_id = req_id;
  604. req->cmd = PVCALLS_LISTEN;
  605. req->u.listen.id = (uintptr_t) map;
  606. req->u.listen.backlog = backlog;
  607. bedata->ring.req_prod_pvt++;
  608. RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&bedata->ring, notify);
  609. spin_unlock(&bedata->socket_lock);
  610. if (notify)
  611. notify_remote_via_irq(bedata->irq);
  612. wait_event(bedata->inflight_req,
  613. READ_ONCE(bedata->rsp[req_id].req_id) == req_id);
  614. /* read req_id, then the content */
  615. smp_rmb();
  616. ret = bedata->rsp[req_id].ret;
  617. bedata->rsp[req_id].req_id = PVCALLS_INVALID_ID;
  618. map->passive.status = PVCALLS_STATUS_LISTEN;
  619. pvcalls_exit_sock(sock);
  620. return ret;
  621. }
  622. int pvcalls_front_accept(struct socket *sock, struct socket *newsock, int flags)
  623. {
  624. struct pvcalls_bedata *bedata;
  625. struct sock_mapping *map;
  626. struct sock_mapping *map2 = NULL;
  627. struct xen_pvcalls_request *req;
  628. int notify, req_id, ret, evtchn, nonblock;
  629. map = pvcalls_enter_sock(sock);
  630. if (IS_ERR(map))
  631. return PTR_ERR(map);
  632. bedata = dev_get_drvdata(&pvcalls_front_dev->dev);
  633. if (map->passive.status != PVCALLS_STATUS_LISTEN) {
  634. pvcalls_exit_sock(sock);
  635. return -EINVAL;
  636. }
  637. nonblock = flags & SOCK_NONBLOCK;
  638. /*
  639. * Backend only supports 1 inflight accept request, will return
  640. * errors for the others
  641. */
  642. if (test_and_set_bit(PVCALLS_FLAG_ACCEPT_INFLIGHT,
  643. (void *)&map->passive.flags)) {
  644. req_id = READ_ONCE(map->passive.inflight_req_id);
  645. if (req_id != PVCALLS_INVALID_ID &&
  646. READ_ONCE(bedata->rsp[req_id].req_id) == req_id) {
  647. map2 = map->passive.accept_map;
  648. goto received;
  649. }
  650. if (nonblock) {
  651. pvcalls_exit_sock(sock);
  652. return -EAGAIN;
  653. }
  654. if (wait_event_interruptible(map->passive.inflight_accept_req,
  655. !test_and_set_bit(PVCALLS_FLAG_ACCEPT_INFLIGHT,
  656. (void *)&map->passive.flags))) {
  657. pvcalls_exit_sock(sock);
  658. return -EINTR;
  659. }
  660. }
  661. spin_lock(&bedata->socket_lock);
  662. ret = get_request(bedata, &req_id);
  663. if (ret < 0) {
  664. clear_bit(PVCALLS_FLAG_ACCEPT_INFLIGHT,
  665. (void *)&map->passive.flags);
  666. spin_unlock(&bedata->socket_lock);
  667. pvcalls_exit_sock(sock);
  668. return ret;
  669. }
  670. map2 = kzalloc(sizeof(*map2), GFP_ATOMIC);
  671. if (map2 == NULL) {
  672. clear_bit(PVCALLS_FLAG_ACCEPT_INFLIGHT,
  673. (void *)&map->passive.flags);
  674. spin_unlock(&bedata->socket_lock);
  675. pvcalls_exit_sock(sock);
  676. return -ENOMEM;
  677. }
  678. ret = create_active(map2, &evtchn);
  679. if (ret < 0) {
  680. kfree(map2);
  681. clear_bit(PVCALLS_FLAG_ACCEPT_INFLIGHT,
  682. (void *)&map->passive.flags);
  683. spin_unlock(&bedata->socket_lock);
  684. pvcalls_exit_sock(sock);
  685. return ret;
  686. }
  687. list_add_tail(&map2->list, &bedata->socket_mappings);
  688. req = RING_GET_REQUEST(&bedata->ring, req_id);
  689. req->req_id = req_id;
  690. req->cmd = PVCALLS_ACCEPT;
  691. req->u.accept.id = (uintptr_t) map;
  692. req->u.accept.ref = map2->active.ref;
  693. req->u.accept.id_new = (uintptr_t) map2;
  694. req->u.accept.evtchn = evtchn;
  695. map->passive.accept_map = map2;
  696. bedata->ring.req_prod_pvt++;
  697. RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&bedata->ring, notify);
  698. spin_unlock(&bedata->socket_lock);
  699. if (notify)
  700. notify_remote_via_irq(bedata->irq);
  701. /* We could check if we have received a response before returning. */
  702. if (nonblock) {
  703. WRITE_ONCE(map->passive.inflight_req_id, req_id);
  704. pvcalls_exit_sock(sock);
  705. return -EAGAIN;
  706. }
  707. if (wait_event_interruptible(bedata->inflight_req,
  708. READ_ONCE(bedata->rsp[req_id].req_id) == req_id)) {
  709. pvcalls_exit_sock(sock);
  710. return -EINTR;
  711. }
  712. /* read req_id, then the content */
  713. smp_rmb();
  714. received:
  715. map2->sock = newsock;
  716. newsock->sk = kzalloc(sizeof(*newsock->sk), GFP_KERNEL);
  717. if (!newsock->sk) {
  718. bedata->rsp[req_id].req_id = PVCALLS_INVALID_ID;
  719. map->passive.inflight_req_id = PVCALLS_INVALID_ID;
  720. clear_bit(PVCALLS_FLAG_ACCEPT_INFLIGHT,
  721. (void *)&map->passive.flags);
  722. pvcalls_front_free_map(bedata, map2);
  723. pvcalls_exit_sock(sock);
  724. return -ENOMEM;
  725. }
  726. newsock->sk->sk_send_head = (void *)map2;
  727. ret = bedata->rsp[req_id].ret;
  728. bedata->rsp[req_id].req_id = PVCALLS_INVALID_ID;
  729. map->passive.inflight_req_id = PVCALLS_INVALID_ID;
  730. clear_bit(PVCALLS_FLAG_ACCEPT_INFLIGHT, (void *)&map->passive.flags);
  731. wake_up(&map->passive.inflight_accept_req);
  732. pvcalls_exit_sock(sock);
  733. return ret;
  734. }
  735. static __poll_t pvcalls_front_poll_passive(struct file *file,
  736. struct pvcalls_bedata *bedata,
  737. struct sock_mapping *map,
  738. poll_table *wait)
  739. {
  740. int notify, req_id, ret;
  741. struct xen_pvcalls_request *req;
  742. if (test_bit(PVCALLS_FLAG_ACCEPT_INFLIGHT,
  743. (void *)&map->passive.flags)) {
  744. uint32_t req_id = READ_ONCE(map->passive.inflight_req_id);
  745. if (req_id != PVCALLS_INVALID_ID &&
  746. READ_ONCE(bedata->rsp[req_id].req_id) == req_id)
  747. return EPOLLIN | EPOLLRDNORM;
  748. poll_wait(file, &map->passive.inflight_accept_req, wait);
  749. return 0;
  750. }
  751. if (test_and_clear_bit(PVCALLS_FLAG_POLL_RET,
  752. (void *)&map->passive.flags))
  753. return EPOLLIN | EPOLLRDNORM;
  754. /*
  755. * First check RET, then INFLIGHT. No barriers necessary to
  756. * ensure execution ordering because of the conditional
  757. * instructions creating control dependencies.
  758. */
  759. if (test_and_set_bit(PVCALLS_FLAG_POLL_INFLIGHT,
  760. (void *)&map->passive.flags)) {
  761. poll_wait(file, &bedata->inflight_req, wait);
  762. return 0;
  763. }
  764. spin_lock(&bedata->socket_lock);
  765. ret = get_request(bedata, &req_id);
  766. if (ret < 0) {
  767. spin_unlock(&bedata->socket_lock);
  768. return ret;
  769. }
  770. req = RING_GET_REQUEST(&bedata->ring, req_id);
  771. req->req_id = req_id;
  772. req->cmd = PVCALLS_POLL;
  773. req->u.poll.id = (uintptr_t) map;
  774. bedata->ring.req_prod_pvt++;
  775. RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&bedata->ring, notify);
  776. spin_unlock(&bedata->socket_lock);
  777. if (notify)
  778. notify_remote_via_irq(bedata->irq);
  779. poll_wait(file, &bedata->inflight_req, wait);
  780. return 0;
  781. }
  782. static __poll_t pvcalls_front_poll_active(struct file *file,
  783. struct pvcalls_bedata *bedata,
  784. struct sock_mapping *map,
  785. poll_table *wait)
  786. {
  787. __poll_t mask = 0;
  788. int32_t in_error, out_error;
  789. struct pvcalls_data_intf *intf = map->active.ring;
  790. out_error = intf->out_error;
  791. in_error = intf->in_error;
  792. poll_wait(file, &map->active.inflight_conn_req, wait);
  793. if (pvcalls_front_write_todo(map))
  794. mask |= EPOLLOUT | EPOLLWRNORM;
  795. if (pvcalls_front_read_todo(map))
  796. mask |= EPOLLIN | EPOLLRDNORM;
  797. if (in_error != 0 || out_error != 0)
  798. mask |= EPOLLERR;
  799. return mask;
  800. }
  801. __poll_t pvcalls_front_poll(struct file *file, struct socket *sock,
  802. poll_table *wait)
  803. {
  804. struct pvcalls_bedata *bedata;
  805. struct sock_mapping *map;
  806. __poll_t ret;
  807. map = pvcalls_enter_sock(sock);
  808. if (IS_ERR(map))
  809. return EPOLLNVAL;
  810. bedata = dev_get_drvdata(&pvcalls_front_dev->dev);
  811. if (map->active_socket)
  812. ret = pvcalls_front_poll_active(file, bedata, map, wait);
  813. else
  814. ret = pvcalls_front_poll_passive(file, bedata, map, wait);
  815. pvcalls_exit_sock(sock);
  816. return ret;
  817. }
  818. int pvcalls_front_release(struct socket *sock)
  819. {
  820. struct pvcalls_bedata *bedata;
  821. struct sock_mapping *map;
  822. int req_id, notify, ret;
  823. struct xen_pvcalls_request *req;
  824. if (sock->sk == NULL)
  825. return 0;
  826. map = pvcalls_enter_sock(sock);
  827. if (IS_ERR(map)) {
  828. if (PTR_ERR(map) == -ENOTCONN)
  829. return -EIO;
  830. else
  831. return 0;
  832. }
  833. bedata = dev_get_drvdata(&pvcalls_front_dev->dev);
  834. spin_lock(&bedata->socket_lock);
  835. ret = get_request(bedata, &req_id);
  836. if (ret < 0) {
  837. spin_unlock(&bedata->socket_lock);
  838. pvcalls_exit_sock(sock);
  839. return ret;
  840. }
  841. sock->sk->sk_send_head = NULL;
  842. req = RING_GET_REQUEST(&bedata->ring, req_id);
  843. req->req_id = req_id;
  844. req->cmd = PVCALLS_RELEASE;
  845. req->u.release.id = (uintptr_t)map;
  846. bedata->ring.req_prod_pvt++;
  847. RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&bedata->ring, notify);
  848. spin_unlock(&bedata->socket_lock);
  849. if (notify)
  850. notify_remote_via_irq(bedata->irq);
  851. wait_event(bedata->inflight_req,
  852. READ_ONCE(bedata->rsp[req_id].req_id) == req_id);
  853. if (map->active_socket) {
  854. /*
  855. * Set in_error and wake up inflight_conn_req to force
  856. * recvmsg waiters to exit.
  857. */
  858. map->active.ring->in_error = -EBADF;
  859. wake_up_interruptible(&map->active.inflight_conn_req);
  860. /*
  861. * We need to make sure that sendmsg/recvmsg on this socket have
  862. * not started before we've cleared sk_send_head here. The
  863. * easiest way to guarantee this is to see that no pvcalls
  864. * (other than us) is in progress on this socket.
  865. */
  866. while (atomic_read(&map->refcount) > 1)
  867. cpu_relax();
  868. pvcalls_front_free_map(bedata, map);
  869. } else {
  870. wake_up(&bedata->inflight_req);
  871. wake_up(&map->passive.inflight_accept_req);
  872. while (atomic_read(&map->refcount) > 1)
  873. cpu_relax();
  874. spin_lock(&bedata->socket_lock);
  875. list_del(&map->list);
  876. spin_unlock(&bedata->socket_lock);
  877. if (READ_ONCE(map->passive.inflight_req_id) !=
  878. PVCALLS_INVALID_ID) {
  879. pvcalls_front_free_map(bedata,
  880. map->passive.accept_map);
  881. }
  882. kfree(map);
  883. }
  884. WRITE_ONCE(bedata->rsp[req_id].req_id, PVCALLS_INVALID_ID);
  885. pvcalls_exit();
  886. return 0;
  887. }
  888. static const struct xenbus_device_id pvcalls_front_ids[] = {
  889. { "pvcalls" },
  890. { "" }
  891. };
  892. static int pvcalls_front_remove(struct xenbus_device *dev)
  893. {
  894. struct pvcalls_bedata *bedata;
  895. struct sock_mapping *map = NULL, *n;
  896. bedata = dev_get_drvdata(&pvcalls_front_dev->dev);
  897. dev_set_drvdata(&dev->dev, NULL);
  898. pvcalls_front_dev = NULL;
  899. if (bedata->irq >= 0)
  900. unbind_from_irqhandler(bedata->irq, dev);
  901. list_for_each_entry_safe(map, n, &bedata->socket_mappings, list) {
  902. map->sock->sk->sk_send_head = NULL;
  903. if (map->active_socket) {
  904. map->active.ring->in_error = -EBADF;
  905. wake_up_interruptible(&map->active.inflight_conn_req);
  906. }
  907. }
  908. smp_mb();
  909. while (atomic_read(&pvcalls_refcount) > 0)
  910. cpu_relax();
  911. list_for_each_entry_safe(map, n, &bedata->socket_mappings, list) {
  912. if (map->active_socket) {
  913. /* No need to lock, refcount is 0 */
  914. pvcalls_front_free_map(bedata, map);
  915. } else {
  916. list_del(&map->list);
  917. kfree(map);
  918. }
  919. }
  920. if (bedata->ref != -1)
  921. gnttab_end_foreign_access(bedata->ref, 0, 0);
  922. kfree(bedata->ring.sring);
  923. kfree(bedata);
  924. xenbus_switch_state(dev, XenbusStateClosed);
  925. return 0;
  926. }
  927. static int pvcalls_front_probe(struct xenbus_device *dev,
  928. const struct xenbus_device_id *id)
  929. {
  930. int ret = -ENOMEM, evtchn, i;
  931. unsigned int max_page_order, function_calls, len;
  932. char *versions;
  933. grant_ref_t gref_head = 0;
  934. struct xenbus_transaction xbt;
  935. struct pvcalls_bedata *bedata = NULL;
  936. struct xen_pvcalls_sring *sring;
  937. if (pvcalls_front_dev != NULL) {
  938. dev_err(&dev->dev, "only one PV Calls connection supported\n");
  939. return -EINVAL;
  940. }
  941. versions = xenbus_read(XBT_NIL, dev->otherend, "versions", &len);
  942. if (IS_ERR(versions))
  943. return PTR_ERR(versions);
  944. if (!len)
  945. return -EINVAL;
  946. if (strcmp(versions, "1")) {
  947. kfree(versions);
  948. return -EINVAL;
  949. }
  950. kfree(versions);
  951. max_page_order = xenbus_read_unsigned(dev->otherend,
  952. "max-page-order", 0);
  953. if (max_page_order < PVCALLS_RING_ORDER)
  954. return -ENODEV;
  955. function_calls = xenbus_read_unsigned(dev->otherend,
  956. "function-calls", 0);
  957. /* See XENBUS_FUNCTIONS_CALLS in pvcalls.h */
  958. if (function_calls != 1)
  959. return -ENODEV;
  960. pr_info("%s max-page-order is %u\n", __func__, max_page_order);
  961. bedata = kzalloc(sizeof(struct pvcalls_bedata), GFP_KERNEL);
  962. if (!bedata)
  963. return -ENOMEM;
  964. dev_set_drvdata(&dev->dev, bedata);
  965. pvcalls_front_dev = dev;
  966. init_waitqueue_head(&bedata->inflight_req);
  967. INIT_LIST_HEAD(&bedata->socket_mappings);
  968. spin_lock_init(&bedata->socket_lock);
  969. bedata->irq = -1;
  970. bedata->ref = -1;
  971. for (i = 0; i < PVCALLS_NR_RSP_PER_RING; i++)
  972. bedata->rsp[i].req_id = PVCALLS_INVALID_ID;
  973. sring = (struct xen_pvcalls_sring *) __get_free_page(GFP_KERNEL |
  974. __GFP_ZERO);
  975. if (!sring)
  976. goto error;
  977. SHARED_RING_INIT(sring);
  978. FRONT_RING_INIT(&bedata->ring, sring, XEN_PAGE_SIZE);
  979. ret = xenbus_alloc_evtchn(dev, &evtchn);
  980. if (ret)
  981. goto error;
  982. bedata->irq = bind_evtchn_to_irqhandler(evtchn,
  983. pvcalls_front_event_handler,
  984. 0, "pvcalls-frontend", dev);
  985. if (bedata->irq < 0) {
  986. ret = bedata->irq;
  987. goto error;
  988. }
  989. ret = gnttab_alloc_grant_references(1, &gref_head);
  990. if (ret < 0)
  991. goto error;
  992. ret = gnttab_claim_grant_reference(&gref_head);
  993. if (ret < 0)
  994. goto error;
  995. bedata->ref = ret;
  996. gnttab_grant_foreign_access_ref(bedata->ref, dev->otherend_id,
  997. virt_to_gfn((void *)sring), 0);
  998. again:
  999. ret = xenbus_transaction_start(&xbt);
  1000. if (ret) {
  1001. xenbus_dev_fatal(dev, ret, "starting transaction");
  1002. goto error;
  1003. }
  1004. ret = xenbus_printf(xbt, dev->nodename, "version", "%u", 1);
  1005. if (ret)
  1006. goto error_xenbus;
  1007. ret = xenbus_printf(xbt, dev->nodename, "ring-ref", "%d", bedata->ref);
  1008. if (ret)
  1009. goto error_xenbus;
  1010. ret = xenbus_printf(xbt, dev->nodename, "port", "%u",
  1011. evtchn);
  1012. if (ret)
  1013. goto error_xenbus;
  1014. ret = xenbus_transaction_end(xbt, 0);
  1015. if (ret) {
  1016. if (ret == -EAGAIN)
  1017. goto again;
  1018. xenbus_dev_fatal(dev, ret, "completing transaction");
  1019. goto error;
  1020. }
  1021. xenbus_switch_state(dev, XenbusStateInitialised);
  1022. return 0;
  1023. error_xenbus:
  1024. xenbus_transaction_end(xbt, 1);
  1025. xenbus_dev_fatal(dev, ret, "writing xenstore");
  1026. error:
  1027. pvcalls_front_remove(dev);
  1028. return ret;
  1029. }
  1030. static void pvcalls_front_changed(struct xenbus_device *dev,
  1031. enum xenbus_state backend_state)
  1032. {
  1033. switch (backend_state) {
  1034. case XenbusStateReconfiguring:
  1035. case XenbusStateReconfigured:
  1036. case XenbusStateInitialising:
  1037. case XenbusStateInitialised:
  1038. case XenbusStateUnknown:
  1039. break;
  1040. case XenbusStateInitWait:
  1041. break;
  1042. case XenbusStateConnected:
  1043. xenbus_switch_state(dev, XenbusStateConnected);
  1044. break;
  1045. case XenbusStateClosed:
  1046. if (dev->state == XenbusStateClosed)
  1047. break;
  1048. /* Missed the backend's CLOSING state */
  1049. /* fall through */
  1050. case XenbusStateClosing:
  1051. xenbus_frontend_closed(dev);
  1052. break;
  1053. }
  1054. }
  1055. static struct xenbus_driver pvcalls_front_driver = {
  1056. .ids = pvcalls_front_ids,
  1057. .probe = pvcalls_front_probe,
  1058. .remove = pvcalls_front_remove,
  1059. .otherend_changed = pvcalls_front_changed,
  1060. };
  1061. static int __init pvcalls_frontend_init(void)
  1062. {
  1063. if (!xen_domain())
  1064. return -ENODEV;
  1065. pr_info("Initialising Xen pvcalls frontend driver\n");
  1066. return xenbus_register_frontend(&pvcalls_front_driver);
  1067. }
  1068. module_init(pvcalls_frontend_init);
  1069. MODULE_DESCRIPTION("Xen PV Calls frontend driver");
  1070. MODULE_AUTHOR("Stefano Stabellini <sstabellini@kernel.org>");
  1071. MODULE_LICENSE("GPL");