1416efba0SStefano Stabellini /* 2416efba0SStefano Stabellini * (c) 2017 Stefano Stabellini <stefano@aporeto.com> 3416efba0SStefano Stabellini * 4416efba0SStefano Stabellini * This program is free software; you can redistribute it and/or modify 5416efba0SStefano Stabellini * it under the terms of the GNU General Public License as published by 6416efba0SStefano Stabellini * the Free Software Foundation; either version 2 of the License, or 7416efba0SStefano Stabellini * (at your option) any later version. 8416efba0SStefano Stabellini * 9416efba0SStefano Stabellini * This program is distributed in the hope that it will be useful, 10416efba0SStefano Stabellini * but WITHOUT ANY WARRANTY; without even the implied warranty of 11416efba0SStefano Stabellini * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12416efba0SStefano Stabellini * GNU General Public License for more details. 13416efba0SStefano Stabellini */ 14416efba0SStefano Stabellini 15416efba0SStefano Stabellini #include <linux/module.h> 162195046bSStefano Stabellini #include <linux/net.h> 172195046bSStefano Stabellini #include <linux/socket.h> 182195046bSStefano Stabellini 192195046bSStefano Stabellini #include <net/sock.h> 20416efba0SStefano Stabellini 21416efba0SStefano Stabellini #include <xen/events.h> 22416efba0SStefano Stabellini #include <xen/grant_table.h> 23416efba0SStefano Stabellini #include <xen/xen.h> 24416efba0SStefano Stabellini #include <xen/xenbus.h> 25416efba0SStefano Stabellini #include <xen/interface/io/pvcalls.h> 26416efba0SStefano Stabellini 272195046bSStefano Stabellini #include "pvcalls-front.h" 282195046bSStefano Stabellini 29aa7ba376SStefano Stabellini #define PVCALLS_INVALID_ID UINT_MAX 30aa7ba376SStefano Stabellini #define PVCALLS_RING_ORDER XENBUS_MAX_RING_GRANT_ORDER 31aa7ba376SStefano Stabellini #define PVCALLS_NR_RSP_PER_RING __CONST_RING_SIZE(xen_pvcalls, XEN_PAGE_SIZE) 3245ddce21SStefano Stabellini #define PVCALLS_FRONT_MAX_SPIN 5000 33aa7ba376SStefano Stabellini 34aa7ba376SStefano Stabellini struct pvcalls_bedata { 35aa7ba376SStefano Stabellini struct xen_pvcalls_front_ring ring; 36aa7ba376SStefano Stabellini grant_ref_t ref; 37aa7ba376SStefano Stabellini int irq; 38aa7ba376SStefano Stabellini 39aa7ba376SStefano Stabellini struct list_head socket_mappings; 40aa7ba376SStefano Stabellini spinlock_t socket_lock; 41aa7ba376SStefano Stabellini 42aa7ba376SStefano Stabellini wait_queue_head_t inflight_req; 43aa7ba376SStefano Stabellini struct xen_pvcalls_response rsp[PVCALLS_NR_RSP_PER_RING]; 44aa7ba376SStefano Stabellini }; 45aa7ba376SStefano Stabellini /* Only one front/back connection supported. */ 46aa7ba376SStefano Stabellini static struct xenbus_device *pvcalls_front_dev; 47aa7ba376SStefano Stabellini static atomic_t pvcalls_refcount; 48aa7ba376SStefano Stabellini 49aa7ba376SStefano Stabellini /* first increment refcount, then proceed */ 50aa7ba376SStefano Stabellini #define pvcalls_enter() { \ 51aa7ba376SStefano Stabellini atomic_inc(&pvcalls_refcount); \ 52aa7ba376SStefano Stabellini } 53aa7ba376SStefano Stabellini 54aa7ba376SStefano Stabellini /* first complete other operations, then decrement refcount */ 55aa7ba376SStefano Stabellini #define pvcalls_exit() { \ 56aa7ba376SStefano Stabellini atomic_dec(&pvcalls_refcount); \ 57aa7ba376SStefano Stabellini } 58aa7ba376SStefano Stabellini 59aa7ba376SStefano Stabellini struct sock_mapping { 60aa7ba376SStefano Stabellini bool active_socket; 61aa7ba376SStefano Stabellini struct list_head list; 62aa7ba376SStefano Stabellini struct socket *sock; 63*64d68718SStefano Stabellini atomic_t refcount; 64cb1c7d9bSStefano Stabellini union { 65cb1c7d9bSStefano Stabellini struct { 66cb1c7d9bSStefano Stabellini int irq; 67cb1c7d9bSStefano Stabellini grant_ref_t ref; 68cb1c7d9bSStefano Stabellini struct pvcalls_data_intf *ring; 69cb1c7d9bSStefano Stabellini struct pvcalls_data data; 70cb1c7d9bSStefano Stabellini struct mutex in_mutex; 71cb1c7d9bSStefano Stabellini struct mutex out_mutex; 72cb1c7d9bSStefano Stabellini 73cb1c7d9bSStefano Stabellini wait_queue_head_t inflight_conn_req; 74cb1c7d9bSStefano Stabellini } active; 7567ea9893SStefano Stabellini struct { 7667ea9893SStefano Stabellini /* Socket status */ 7767ea9893SStefano Stabellini #define PVCALLS_STATUS_UNINITALIZED 0 7867ea9893SStefano Stabellini #define PVCALLS_STATUS_BIND 1 7967ea9893SStefano Stabellini #define PVCALLS_STATUS_LISTEN 2 8067ea9893SStefano Stabellini uint8_t status; 819774c6ccSStefano Stabellini /* 829774c6ccSStefano Stabellini * Internal state-machine flags. 839774c6ccSStefano Stabellini * Only one accept operation can be inflight for a socket. 849774c6ccSStefano Stabellini * Only one poll operation can be inflight for a given socket. 859774c6ccSStefano Stabellini */ 869774c6ccSStefano Stabellini #define PVCALLS_FLAG_ACCEPT_INFLIGHT 0 875842c835SStefano Stabellini #define PVCALLS_FLAG_POLL_INFLIGHT 1 885842c835SStefano Stabellini #define PVCALLS_FLAG_POLL_RET 2 899774c6ccSStefano Stabellini uint8_t flags; 909774c6ccSStefano Stabellini uint32_t inflight_req_id; 919774c6ccSStefano Stabellini struct sock_mapping *accept_map; 929774c6ccSStefano Stabellini wait_queue_head_t inflight_accept_req; 9367ea9893SStefano Stabellini } passive; 94cb1c7d9bSStefano Stabellini }; 95aa7ba376SStefano Stabellini }; 96aa7ba376SStefano Stabellini 97*64d68718SStefano Stabellini static inline struct sock_mapping *pvcalls_enter_sock(struct socket *sock) 98*64d68718SStefano Stabellini { 99*64d68718SStefano Stabellini struct sock_mapping *map; 100*64d68718SStefano Stabellini 101*64d68718SStefano Stabellini if (!pvcalls_front_dev || 102*64d68718SStefano Stabellini dev_get_drvdata(&pvcalls_front_dev->dev) == NULL) 103*64d68718SStefano Stabellini return ERR_PTR(-ENOTCONN); 104*64d68718SStefano Stabellini 105*64d68718SStefano Stabellini map = (struct sock_mapping *)sock->sk->sk_send_head; 106*64d68718SStefano Stabellini if (map == NULL) 107*64d68718SStefano Stabellini return ERR_PTR(-ENOTSOCK); 108*64d68718SStefano Stabellini 109*64d68718SStefano Stabellini pvcalls_enter(); 110*64d68718SStefano Stabellini atomic_inc(&map->refcount); 111*64d68718SStefano Stabellini return map; 112*64d68718SStefano Stabellini } 113*64d68718SStefano Stabellini 114*64d68718SStefano Stabellini static inline void pvcalls_exit_sock(struct socket *sock) 115*64d68718SStefano Stabellini { 116*64d68718SStefano Stabellini struct sock_mapping *map; 117*64d68718SStefano Stabellini 118*64d68718SStefano Stabellini map = (struct sock_mapping *)sock->sk->sk_send_head; 119*64d68718SStefano Stabellini atomic_dec(&map->refcount); 120*64d68718SStefano Stabellini pvcalls_exit(); 121*64d68718SStefano Stabellini } 122*64d68718SStefano Stabellini 1232195046bSStefano Stabellini static inline int get_request(struct pvcalls_bedata *bedata, int *req_id) 1242195046bSStefano Stabellini { 1252195046bSStefano Stabellini *req_id = bedata->ring.req_prod_pvt & (RING_SIZE(&bedata->ring) - 1); 1262195046bSStefano Stabellini if (RING_FULL(&bedata->ring) || 1272195046bSStefano Stabellini bedata->rsp[*req_id].req_id != PVCALLS_INVALID_ID) 1282195046bSStefano Stabellini return -EAGAIN; 1292195046bSStefano Stabellini return 0; 1302195046bSStefano Stabellini } 1312195046bSStefano Stabellini 13245ddce21SStefano Stabellini static bool pvcalls_front_write_todo(struct sock_mapping *map) 13345ddce21SStefano Stabellini { 13445ddce21SStefano Stabellini struct pvcalls_data_intf *intf = map->active.ring; 13545ddce21SStefano Stabellini RING_IDX cons, prod, size = XEN_FLEX_RING_SIZE(PVCALLS_RING_ORDER); 13645ddce21SStefano Stabellini int32_t error; 13745ddce21SStefano Stabellini 13845ddce21SStefano Stabellini error = intf->out_error; 13945ddce21SStefano Stabellini if (error == -ENOTCONN) 14045ddce21SStefano Stabellini return false; 14145ddce21SStefano Stabellini if (error != 0) 14245ddce21SStefano Stabellini return true; 14345ddce21SStefano Stabellini 14445ddce21SStefano Stabellini cons = intf->out_cons; 14545ddce21SStefano Stabellini prod = intf->out_prod; 14645ddce21SStefano Stabellini return !!(size - pvcalls_queued(prod, cons, size)); 14745ddce21SStefano Stabellini } 14845ddce21SStefano Stabellini 149ae0d0405SStefano Stabellini static bool pvcalls_front_read_todo(struct sock_mapping *map) 150ae0d0405SStefano Stabellini { 151ae0d0405SStefano Stabellini struct pvcalls_data_intf *intf = map->active.ring; 152ae0d0405SStefano Stabellini RING_IDX cons, prod; 153ae0d0405SStefano Stabellini int32_t error; 154ae0d0405SStefano Stabellini 155ae0d0405SStefano Stabellini cons = intf->in_cons; 156ae0d0405SStefano Stabellini prod = intf->in_prod; 157ae0d0405SStefano Stabellini error = intf->in_error; 158ae0d0405SStefano Stabellini return (error != 0 || 159ae0d0405SStefano Stabellini pvcalls_queued(prod, cons, 160ae0d0405SStefano Stabellini XEN_FLEX_RING_SIZE(PVCALLS_RING_ORDER)) != 0); 161ae0d0405SStefano Stabellini } 162ae0d0405SStefano Stabellini 163aa7ba376SStefano Stabellini static irqreturn_t pvcalls_front_event_handler(int irq, void *dev_id) 164aa7ba376SStefano Stabellini { 1652195046bSStefano Stabellini struct xenbus_device *dev = dev_id; 1662195046bSStefano Stabellini struct pvcalls_bedata *bedata; 1672195046bSStefano Stabellini struct xen_pvcalls_response *rsp; 1682195046bSStefano Stabellini uint8_t *src, *dst; 1692195046bSStefano Stabellini int req_id = 0, more = 0, done = 0; 1702195046bSStefano Stabellini 1712195046bSStefano Stabellini if (dev == NULL) 1722195046bSStefano Stabellini return IRQ_HANDLED; 1732195046bSStefano Stabellini 1742195046bSStefano Stabellini pvcalls_enter(); 1752195046bSStefano Stabellini bedata = dev_get_drvdata(&dev->dev); 1762195046bSStefano Stabellini if (bedata == NULL) { 1772195046bSStefano Stabellini pvcalls_exit(); 1782195046bSStefano Stabellini return IRQ_HANDLED; 1792195046bSStefano Stabellini } 1802195046bSStefano Stabellini 1812195046bSStefano Stabellini again: 1822195046bSStefano Stabellini while (RING_HAS_UNCONSUMED_RESPONSES(&bedata->ring)) { 1832195046bSStefano Stabellini rsp = RING_GET_RESPONSE(&bedata->ring, bedata->ring.rsp_cons); 1842195046bSStefano Stabellini 1852195046bSStefano Stabellini req_id = rsp->req_id; 1865842c835SStefano Stabellini if (rsp->cmd == PVCALLS_POLL) { 1875842c835SStefano Stabellini struct sock_mapping *map = (struct sock_mapping *)(uintptr_t) 1885842c835SStefano Stabellini rsp->u.poll.id; 1895842c835SStefano Stabellini 1905842c835SStefano Stabellini clear_bit(PVCALLS_FLAG_POLL_INFLIGHT, 1915842c835SStefano Stabellini (void *)&map->passive.flags); 1925842c835SStefano Stabellini /* 1935842c835SStefano Stabellini * clear INFLIGHT, then set RET. It pairs with 1945842c835SStefano Stabellini * the checks at the beginning of 1955842c835SStefano Stabellini * pvcalls_front_poll_passive. 1965842c835SStefano Stabellini */ 1975842c835SStefano Stabellini smp_wmb(); 1985842c835SStefano Stabellini set_bit(PVCALLS_FLAG_POLL_RET, 1995842c835SStefano Stabellini (void *)&map->passive.flags); 2005842c835SStefano Stabellini } else { 2015842c835SStefano Stabellini dst = (uint8_t *)&bedata->rsp[req_id] + 2025842c835SStefano Stabellini sizeof(rsp->req_id); 2032195046bSStefano Stabellini src = (uint8_t *)rsp + sizeof(rsp->req_id); 2042195046bSStefano Stabellini memcpy(dst, src, sizeof(*rsp) - sizeof(rsp->req_id)); 2052195046bSStefano Stabellini /* 2062195046bSStefano Stabellini * First copy the rest of the data, then req_id. It is 2072195046bSStefano Stabellini * paired with the barrier when accessing bedata->rsp. 2082195046bSStefano Stabellini */ 2092195046bSStefano Stabellini smp_wmb(); 2105842c835SStefano Stabellini bedata->rsp[req_id].req_id = req_id; 2115842c835SStefano Stabellini } 2122195046bSStefano Stabellini 2132195046bSStefano Stabellini done = 1; 2142195046bSStefano Stabellini bedata->ring.rsp_cons++; 2152195046bSStefano Stabellini } 2162195046bSStefano Stabellini 2172195046bSStefano Stabellini RING_FINAL_CHECK_FOR_RESPONSES(&bedata->ring, more); 2182195046bSStefano Stabellini if (more) 2192195046bSStefano Stabellini goto again; 2202195046bSStefano Stabellini if (done) 2212195046bSStefano Stabellini wake_up(&bedata->inflight_req); 2222195046bSStefano Stabellini pvcalls_exit(); 223aa7ba376SStefano Stabellini return IRQ_HANDLED; 224aa7ba376SStefano Stabellini } 225aa7ba376SStefano Stabellini 226aa7ba376SStefano Stabellini static void pvcalls_front_free_map(struct pvcalls_bedata *bedata, 227aa7ba376SStefano Stabellini struct sock_mapping *map) 228aa7ba376SStefano Stabellini { 229235a71c5SStefano Stabellini int i; 230235a71c5SStefano Stabellini 231235a71c5SStefano Stabellini unbind_from_irqhandler(map->active.irq, map); 232235a71c5SStefano Stabellini 233235a71c5SStefano Stabellini spin_lock(&bedata->socket_lock); 234235a71c5SStefano Stabellini if (!list_empty(&map->list)) 235235a71c5SStefano Stabellini list_del_init(&map->list); 236235a71c5SStefano Stabellini spin_unlock(&bedata->socket_lock); 237235a71c5SStefano Stabellini 238235a71c5SStefano Stabellini for (i = 0; i < (1 << PVCALLS_RING_ORDER); i++) 239235a71c5SStefano Stabellini gnttab_end_foreign_access(map->active.ring->ref[i], 0, 0); 240235a71c5SStefano Stabellini gnttab_end_foreign_access(map->active.ref, 0, 0); 241235a71c5SStefano Stabellini free_page((unsigned long)map->active.ring); 242235a71c5SStefano Stabellini 243235a71c5SStefano Stabellini kfree(map); 244aa7ba376SStefano Stabellini } 245aa7ba376SStefano Stabellini 246cb1c7d9bSStefano Stabellini static irqreturn_t pvcalls_front_conn_handler(int irq, void *sock_map) 247cb1c7d9bSStefano Stabellini { 248cb1c7d9bSStefano Stabellini struct sock_mapping *map = sock_map; 249cb1c7d9bSStefano Stabellini 250cb1c7d9bSStefano Stabellini if (map == NULL) 251cb1c7d9bSStefano Stabellini return IRQ_HANDLED; 252cb1c7d9bSStefano Stabellini 253cb1c7d9bSStefano Stabellini wake_up_interruptible(&map->active.inflight_conn_req); 254cb1c7d9bSStefano Stabellini 255cb1c7d9bSStefano Stabellini return IRQ_HANDLED; 256cb1c7d9bSStefano Stabellini } 257cb1c7d9bSStefano Stabellini 2582195046bSStefano Stabellini int pvcalls_front_socket(struct socket *sock) 2592195046bSStefano Stabellini { 2602195046bSStefano Stabellini struct pvcalls_bedata *bedata; 2612195046bSStefano Stabellini struct sock_mapping *map = NULL; 2622195046bSStefano Stabellini struct xen_pvcalls_request *req; 2632195046bSStefano Stabellini int notify, req_id, ret; 2642195046bSStefano Stabellini 2652195046bSStefano Stabellini /* 2662195046bSStefano Stabellini * PVCalls only supports domain AF_INET, 2672195046bSStefano Stabellini * type SOCK_STREAM and protocol 0 sockets for now. 2682195046bSStefano Stabellini * 2692195046bSStefano Stabellini * Check socket type here, AF_INET and protocol checks are done 2702195046bSStefano Stabellini * by the caller. 2712195046bSStefano Stabellini */ 2722195046bSStefano Stabellini if (sock->type != SOCK_STREAM) 2732195046bSStefano Stabellini return -EOPNOTSUPP; 2742195046bSStefano Stabellini 2752195046bSStefano Stabellini pvcalls_enter(); 2762195046bSStefano Stabellini if (!pvcalls_front_dev) { 2772195046bSStefano Stabellini pvcalls_exit(); 2782195046bSStefano Stabellini return -EACCES; 2792195046bSStefano Stabellini } 2802195046bSStefano Stabellini bedata = dev_get_drvdata(&pvcalls_front_dev->dev); 2812195046bSStefano Stabellini 2822195046bSStefano Stabellini map = kzalloc(sizeof(*map), GFP_KERNEL); 2832195046bSStefano Stabellini if (map == NULL) { 2842195046bSStefano Stabellini pvcalls_exit(); 2852195046bSStefano Stabellini return -ENOMEM; 2862195046bSStefano Stabellini } 2872195046bSStefano Stabellini 2882195046bSStefano Stabellini spin_lock(&bedata->socket_lock); 2892195046bSStefano Stabellini 2902195046bSStefano Stabellini ret = get_request(bedata, &req_id); 2912195046bSStefano Stabellini if (ret < 0) { 2922195046bSStefano Stabellini kfree(map); 2932195046bSStefano Stabellini spin_unlock(&bedata->socket_lock); 2942195046bSStefano Stabellini pvcalls_exit(); 2952195046bSStefano Stabellini return ret; 2962195046bSStefano Stabellini } 2972195046bSStefano Stabellini 2982195046bSStefano Stabellini /* 2992195046bSStefano Stabellini * sock->sk->sk_send_head is not used for ip sockets: reuse the 3002195046bSStefano Stabellini * field to store a pointer to the struct sock_mapping 3012195046bSStefano Stabellini * corresponding to the socket. This way, we can easily get the 3022195046bSStefano Stabellini * struct sock_mapping from the struct socket. 3032195046bSStefano Stabellini */ 3042195046bSStefano Stabellini sock->sk->sk_send_head = (void *)map; 3052195046bSStefano Stabellini list_add_tail(&map->list, &bedata->socket_mappings); 3062195046bSStefano Stabellini 3072195046bSStefano Stabellini req = RING_GET_REQUEST(&bedata->ring, req_id); 3082195046bSStefano Stabellini req->req_id = req_id; 3092195046bSStefano Stabellini req->cmd = PVCALLS_SOCKET; 3102195046bSStefano Stabellini req->u.socket.id = (uintptr_t) map; 3112195046bSStefano Stabellini req->u.socket.domain = AF_INET; 3122195046bSStefano Stabellini req->u.socket.type = SOCK_STREAM; 3132195046bSStefano Stabellini req->u.socket.protocol = IPPROTO_IP; 3142195046bSStefano Stabellini 3152195046bSStefano Stabellini bedata->ring.req_prod_pvt++; 3162195046bSStefano Stabellini RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&bedata->ring, notify); 3172195046bSStefano Stabellini spin_unlock(&bedata->socket_lock); 3182195046bSStefano Stabellini if (notify) 3192195046bSStefano Stabellini notify_remote_via_irq(bedata->irq); 3202195046bSStefano Stabellini 3212195046bSStefano Stabellini wait_event(bedata->inflight_req, 3222195046bSStefano Stabellini READ_ONCE(bedata->rsp[req_id].req_id) == req_id); 3232195046bSStefano Stabellini 3242195046bSStefano Stabellini /* read req_id, then the content */ 3252195046bSStefano Stabellini smp_rmb(); 3262195046bSStefano Stabellini ret = bedata->rsp[req_id].ret; 3272195046bSStefano Stabellini bedata->rsp[req_id].req_id = PVCALLS_INVALID_ID; 3282195046bSStefano Stabellini 3292195046bSStefano Stabellini pvcalls_exit(); 3302195046bSStefano Stabellini return ret; 3312195046bSStefano Stabellini } 3322195046bSStefano Stabellini 333cb1c7d9bSStefano Stabellini static int create_active(struct sock_mapping *map, int *evtchn) 334cb1c7d9bSStefano Stabellini { 335cb1c7d9bSStefano Stabellini void *bytes; 336cb1c7d9bSStefano Stabellini int ret = -ENOMEM, irq = -1, i; 337cb1c7d9bSStefano Stabellini 338cb1c7d9bSStefano Stabellini *evtchn = -1; 339cb1c7d9bSStefano Stabellini init_waitqueue_head(&map->active.inflight_conn_req); 340cb1c7d9bSStefano Stabellini 341cb1c7d9bSStefano Stabellini map->active.ring = (struct pvcalls_data_intf *) 342cb1c7d9bSStefano Stabellini __get_free_page(GFP_KERNEL | __GFP_ZERO); 343cb1c7d9bSStefano Stabellini if (map->active.ring == NULL) 344cb1c7d9bSStefano Stabellini goto out_error; 345cb1c7d9bSStefano Stabellini map->active.ring->ring_order = PVCALLS_RING_ORDER; 346cb1c7d9bSStefano Stabellini bytes = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, 347cb1c7d9bSStefano Stabellini PVCALLS_RING_ORDER); 348cb1c7d9bSStefano Stabellini if (bytes == NULL) 349cb1c7d9bSStefano Stabellini goto out_error; 350cb1c7d9bSStefano Stabellini for (i = 0; i < (1 << PVCALLS_RING_ORDER); i++) 351cb1c7d9bSStefano Stabellini map->active.ring->ref[i] = gnttab_grant_foreign_access( 352cb1c7d9bSStefano Stabellini pvcalls_front_dev->otherend_id, 353cb1c7d9bSStefano Stabellini pfn_to_gfn(virt_to_pfn(bytes) + i), 0); 354cb1c7d9bSStefano Stabellini 355cb1c7d9bSStefano Stabellini map->active.ref = gnttab_grant_foreign_access( 356cb1c7d9bSStefano Stabellini pvcalls_front_dev->otherend_id, 357cb1c7d9bSStefano Stabellini pfn_to_gfn(virt_to_pfn((void *)map->active.ring)), 0); 358cb1c7d9bSStefano Stabellini 359cb1c7d9bSStefano Stabellini map->active.data.in = bytes; 360cb1c7d9bSStefano Stabellini map->active.data.out = bytes + 361cb1c7d9bSStefano Stabellini XEN_FLEX_RING_SIZE(PVCALLS_RING_ORDER); 362cb1c7d9bSStefano Stabellini 363cb1c7d9bSStefano Stabellini ret = xenbus_alloc_evtchn(pvcalls_front_dev, evtchn); 364cb1c7d9bSStefano Stabellini if (ret) 365cb1c7d9bSStefano Stabellini goto out_error; 366cb1c7d9bSStefano Stabellini irq = bind_evtchn_to_irqhandler(*evtchn, pvcalls_front_conn_handler, 367cb1c7d9bSStefano Stabellini 0, "pvcalls-frontend", map); 368cb1c7d9bSStefano Stabellini if (irq < 0) { 369cb1c7d9bSStefano Stabellini ret = irq; 370cb1c7d9bSStefano Stabellini goto out_error; 371cb1c7d9bSStefano Stabellini } 372cb1c7d9bSStefano Stabellini 373cb1c7d9bSStefano Stabellini map->active.irq = irq; 374cb1c7d9bSStefano Stabellini map->active_socket = true; 375cb1c7d9bSStefano Stabellini mutex_init(&map->active.in_mutex); 376cb1c7d9bSStefano Stabellini mutex_init(&map->active.out_mutex); 377cb1c7d9bSStefano Stabellini 378cb1c7d9bSStefano Stabellini return 0; 379cb1c7d9bSStefano Stabellini 380cb1c7d9bSStefano Stabellini out_error: 381773aaadcSColin Ian King if (*evtchn >= 0) 382cb1c7d9bSStefano Stabellini xenbus_free_evtchn(pvcalls_front_dev, *evtchn); 383cb1c7d9bSStefano Stabellini kfree(map->active.data.in); 384cb1c7d9bSStefano Stabellini kfree(map->active.ring); 385cb1c7d9bSStefano Stabellini return ret; 386cb1c7d9bSStefano Stabellini } 387cb1c7d9bSStefano Stabellini 388cb1c7d9bSStefano Stabellini int pvcalls_front_connect(struct socket *sock, struct sockaddr *addr, 389cb1c7d9bSStefano Stabellini int addr_len, int flags) 390cb1c7d9bSStefano Stabellini { 391cb1c7d9bSStefano Stabellini struct pvcalls_bedata *bedata; 392cb1c7d9bSStefano Stabellini struct sock_mapping *map = NULL; 393cb1c7d9bSStefano Stabellini struct xen_pvcalls_request *req; 394cb1c7d9bSStefano Stabellini int notify, req_id, ret, evtchn; 395cb1c7d9bSStefano Stabellini 396cb1c7d9bSStefano Stabellini if (addr->sa_family != AF_INET || sock->type != SOCK_STREAM) 397cb1c7d9bSStefano Stabellini return -EOPNOTSUPP; 398cb1c7d9bSStefano Stabellini 399*64d68718SStefano Stabellini map = pvcalls_enter_sock(sock); 400*64d68718SStefano Stabellini if (IS_ERR(map)) 401*64d68718SStefano Stabellini return PTR_ERR(map); 402cb1c7d9bSStefano Stabellini 403cb1c7d9bSStefano Stabellini bedata = dev_get_drvdata(&pvcalls_front_dev->dev); 404cb1c7d9bSStefano Stabellini 405cb1c7d9bSStefano Stabellini spin_lock(&bedata->socket_lock); 406cb1c7d9bSStefano Stabellini ret = get_request(bedata, &req_id); 407cb1c7d9bSStefano Stabellini if (ret < 0) { 408cb1c7d9bSStefano Stabellini spin_unlock(&bedata->socket_lock); 409*64d68718SStefano Stabellini pvcalls_exit_sock(sock); 410cb1c7d9bSStefano Stabellini return ret; 411cb1c7d9bSStefano Stabellini } 412cb1c7d9bSStefano Stabellini ret = create_active(map, &evtchn); 413cb1c7d9bSStefano Stabellini if (ret < 0) { 414cb1c7d9bSStefano Stabellini spin_unlock(&bedata->socket_lock); 415*64d68718SStefano Stabellini pvcalls_exit_sock(sock); 416cb1c7d9bSStefano Stabellini return ret; 417cb1c7d9bSStefano Stabellini } 418cb1c7d9bSStefano Stabellini 419cb1c7d9bSStefano Stabellini req = RING_GET_REQUEST(&bedata->ring, req_id); 420cb1c7d9bSStefano Stabellini req->req_id = req_id; 421cb1c7d9bSStefano Stabellini req->cmd = PVCALLS_CONNECT; 422cb1c7d9bSStefano Stabellini req->u.connect.id = (uintptr_t)map; 423cb1c7d9bSStefano Stabellini req->u.connect.len = addr_len; 424cb1c7d9bSStefano Stabellini req->u.connect.flags = flags; 425cb1c7d9bSStefano Stabellini req->u.connect.ref = map->active.ref; 426cb1c7d9bSStefano Stabellini req->u.connect.evtchn = evtchn; 427cb1c7d9bSStefano Stabellini memcpy(req->u.connect.addr, addr, sizeof(*addr)); 428cb1c7d9bSStefano Stabellini 429cb1c7d9bSStefano Stabellini map->sock = sock; 430cb1c7d9bSStefano Stabellini 431cb1c7d9bSStefano Stabellini bedata->ring.req_prod_pvt++; 432cb1c7d9bSStefano Stabellini RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&bedata->ring, notify); 433cb1c7d9bSStefano Stabellini spin_unlock(&bedata->socket_lock); 434cb1c7d9bSStefano Stabellini 435cb1c7d9bSStefano Stabellini if (notify) 436cb1c7d9bSStefano Stabellini notify_remote_via_irq(bedata->irq); 437cb1c7d9bSStefano Stabellini 438cb1c7d9bSStefano Stabellini wait_event(bedata->inflight_req, 439cb1c7d9bSStefano Stabellini READ_ONCE(bedata->rsp[req_id].req_id) == req_id); 440cb1c7d9bSStefano Stabellini 441cb1c7d9bSStefano Stabellini /* read req_id, then the content */ 442cb1c7d9bSStefano Stabellini smp_rmb(); 443cb1c7d9bSStefano Stabellini ret = bedata->rsp[req_id].ret; 444cb1c7d9bSStefano Stabellini bedata->rsp[req_id].req_id = PVCALLS_INVALID_ID; 445*64d68718SStefano Stabellini pvcalls_exit_sock(sock); 446cb1c7d9bSStefano Stabellini return ret; 447cb1c7d9bSStefano Stabellini } 448cb1c7d9bSStefano Stabellini 44945ddce21SStefano Stabellini static int __write_ring(struct pvcalls_data_intf *intf, 45045ddce21SStefano Stabellini struct pvcalls_data *data, 45145ddce21SStefano Stabellini struct iov_iter *msg_iter, 45245ddce21SStefano Stabellini int len) 45345ddce21SStefano Stabellini { 45445ddce21SStefano Stabellini RING_IDX cons, prod, size, masked_prod, masked_cons; 45545ddce21SStefano Stabellini RING_IDX array_size = XEN_FLEX_RING_SIZE(PVCALLS_RING_ORDER); 45645ddce21SStefano Stabellini int32_t error; 45745ddce21SStefano Stabellini 45845ddce21SStefano Stabellini error = intf->out_error; 45945ddce21SStefano Stabellini if (error < 0) 46045ddce21SStefano Stabellini return error; 46145ddce21SStefano Stabellini cons = intf->out_cons; 46245ddce21SStefano Stabellini prod = intf->out_prod; 46345ddce21SStefano Stabellini /* read indexes before continuing */ 46445ddce21SStefano Stabellini virt_mb(); 46545ddce21SStefano Stabellini 46645ddce21SStefano Stabellini size = pvcalls_queued(prod, cons, array_size); 46745ddce21SStefano Stabellini if (size >= array_size) 46845ddce21SStefano Stabellini return -EINVAL; 46945ddce21SStefano Stabellini if (len > array_size - size) 47045ddce21SStefano Stabellini len = array_size - size; 47145ddce21SStefano Stabellini 47245ddce21SStefano Stabellini masked_prod = pvcalls_mask(prod, array_size); 47345ddce21SStefano Stabellini masked_cons = pvcalls_mask(cons, array_size); 47445ddce21SStefano Stabellini 47545ddce21SStefano Stabellini if (masked_prod < masked_cons) { 47645ddce21SStefano Stabellini len = copy_from_iter(data->out + masked_prod, len, msg_iter); 47745ddce21SStefano Stabellini } else { 47845ddce21SStefano Stabellini if (len > array_size - masked_prod) { 47945ddce21SStefano Stabellini int ret = copy_from_iter(data->out + masked_prod, 48045ddce21SStefano Stabellini array_size - masked_prod, msg_iter); 48145ddce21SStefano Stabellini if (ret != array_size - masked_prod) { 48245ddce21SStefano Stabellini len = ret; 48345ddce21SStefano Stabellini goto out; 48445ddce21SStefano Stabellini } 48545ddce21SStefano Stabellini len = ret + copy_from_iter(data->out, len - ret, msg_iter); 48645ddce21SStefano Stabellini } else { 48745ddce21SStefano Stabellini len = copy_from_iter(data->out + masked_prod, len, msg_iter); 48845ddce21SStefano Stabellini } 48945ddce21SStefano Stabellini } 49045ddce21SStefano Stabellini out: 49145ddce21SStefano Stabellini /* write to ring before updating pointer */ 49245ddce21SStefano Stabellini virt_wmb(); 49345ddce21SStefano Stabellini intf->out_prod += len; 49445ddce21SStefano Stabellini 49545ddce21SStefano Stabellini return len; 49645ddce21SStefano Stabellini } 49745ddce21SStefano Stabellini 49845ddce21SStefano Stabellini int pvcalls_front_sendmsg(struct socket *sock, struct msghdr *msg, 49945ddce21SStefano Stabellini size_t len) 50045ddce21SStefano Stabellini { 50145ddce21SStefano Stabellini struct pvcalls_bedata *bedata; 50245ddce21SStefano Stabellini struct sock_mapping *map; 50345ddce21SStefano Stabellini int sent, tot_sent = 0; 50445ddce21SStefano Stabellini int count = 0, flags; 50545ddce21SStefano Stabellini 50645ddce21SStefano Stabellini flags = msg->msg_flags; 50745ddce21SStefano Stabellini if (flags & (MSG_CONFIRM|MSG_DONTROUTE|MSG_EOR|MSG_OOB)) 50845ddce21SStefano Stabellini return -EOPNOTSUPP; 50945ddce21SStefano Stabellini 510*64d68718SStefano Stabellini map = pvcalls_enter_sock(sock); 511*64d68718SStefano Stabellini if (IS_ERR(map)) 512*64d68718SStefano Stabellini return PTR_ERR(map); 51345ddce21SStefano Stabellini bedata = dev_get_drvdata(&pvcalls_front_dev->dev); 51445ddce21SStefano Stabellini 51545ddce21SStefano Stabellini mutex_lock(&map->active.out_mutex); 51645ddce21SStefano Stabellini if ((flags & MSG_DONTWAIT) && !pvcalls_front_write_todo(map)) { 51745ddce21SStefano Stabellini mutex_unlock(&map->active.out_mutex); 518*64d68718SStefano Stabellini pvcalls_exit_sock(sock); 51945ddce21SStefano Stabellini return -EAGAIN; 52045ddce21SStefano Stabellini } 52145ddce21SStefano Stabellini if (len > INT_MAX) 52245ddce21SStefano Stabellini len = INT_MAX; 52345ddce21SStefano Stabellini 52445ddce21SStefano Stabellini again: 52545ddce21SStefano Stabellini count++; 52645ddce21SStefano Stabellini sent = __write_ring(map->active.ring, 52745ddce21SStefano Stabellini &map->active.data, &msg->msg_iter, 52845ddce21SStefano Stabellini len); 52945ddce21SStefano Stabellini if (sent > 0) { 53045ddce21SStefano Stabellini len -= sent; 53145ddce21SStefano Stabellini tot_sent += sent; 53245ddce21SStefano Stabellini notify_remote_via_irq(map->active.irq); 53345ddce21SStefano Stabellini } 53445ddce21SStefano Stabellini if (sent >= 0 && len > 0 && count < PVCALLS_FRONT_MAX_SPIN) 53545ddce21SStefano Stabellini goto again; 53645ddce21SStefano Stabellini if (sent < 0) 53745ddce21SStefano Stabellini tot_sent = sent; 53845ddce21SStefano Stabellini 53945ddce21SStefano Stabellini mutex_unlock(&map->active.out_mutex); 540*64d68718SStefano Stabellini pvcalls_exit_sock(sock); 54145ddce21SStefano Stabellini return tot_sent; 54245ddce21SStefano Stabellini } 54345ddce21SStefano Stabellini 544ae0d0405SStefano Stabellini static int __read_ring(struct pvcalls_data_intf *intf, 545ae0d0405SStefano Stabellini struct pvcalls_data *data, 546ae0d0405SStefano Stabellini struct iov_iter *msg_iter, 547ae0d0405SStefano Stabellini size_t len, int flags) 548ae0d0405SStefano Stabellini { 549ae0d0405SStefano Stabellini RING_IDX cons, prod, size, masked_prod, masked_cons; 550ae0d0405SStefano Stabellini RING_IDX array_size = XEN_FLEX_RING_SIZE(PVCALLS_RING_ORDER); 551ae0d0405SStefano Stabellini int32_t error; 552ae0d0405SStefano Stabellini 553ae0d0405SStefano Stabellini cons = intf->in_cons; 554ae0d0405SStefano Stabellini prod = intf->in_prod; 555ae0d0405SStefano Stabellini error = intf->in_error; 556ae0d0405SStefano Stabellini /* get pointers before reading from the ring */ 557ae0d0405SStefano Stabellini virt_rmb(); 558ae0d0405SStefano Stabellini if (error < 0) 559ae0d0405SStefano Stabellini return error; 560ae0d0405SStefano Stabellini 561ae0d0405SStefano Stabellini size = pvcalls_queued(prod, cons, array_size); 562ae0d0405SStefano Stabellini masked_prod = pvcalls_mask(prod, array_size); 563ae0d0405SStefano Stabellini masked_cons = pvcalls_mask(cons, array_size); 564ae0d0405SStefano Stabellini 565ae0d0405SStefano Stabellini if (size == 0) 566ae0d0405SStefano Stabellini return 0; 567ae0d0405SStefano Stabellini 568ae0d0405SStefano Stabellini if (len > size) 569ae0d0405SStefano Stabellini len = size; 570ae0d0405SStefano Stabellini 571ae0d0405SStefano Stabellini if (masked_prod > masked_cons) { 572ae0d0405SStefano Stabellini len = copy_to_iter(data->in + masked_cons, len, msg_iter); 573ae0d0405SStefano Stabellini } else { 574ae0d0405SStefano Stabellini if (len > (array_size - masked_cons)) { 575ae0d0405SStefano Stabellini int ret = copy_to_iter(data->in + masked_cons, 576ae0d0405SStefano Stabellini array_size - masked_cons, msg_iter); 577ae0d0405SStefano Stabellini if (ret != array_size - masked_cons) { 578ae0d0405SStefano Stabellini len = ret; 579ae0d0405SStefano Stabellini goto out; 580ae0d0405SStefano Stabellini } 581ae0d0405SStefano Stabellini len = ret + copy_to_iter(data->in, len - ret, msg_iter); 582ae0d0405SStefano Stabellini } else { 583ae0d0405SStefano Stabellini len = copy_to_iter(data->in + masked_cons, len, msg_iter); 584ae0d0405SStefano Stabellini } 585ae0d0405SStefano Stabellini } 586ae0d0405SStefano Stabellini out: 587ae0d0405SStefano Stabellini /* read data from the ring before increasing the index */ 588ae0d0405SStefano Stabellini virt_mb(); 589ae0d0405SStefano Stabellini if (!(flags & MSG_PEEK)) 590ae0d0405SStefano Stabellini intf->in_cons += len; 591ae0d0405SStefano Stabellini 592ae0d0405SStefano Stabellini return len; 593ae0d0405SStefano Stabellini } 594ae0d0405SStefano Stabellini 595ae0d0405SStefano Stabellini int pvcalls_front_recvmsg(struct socket *sock, struct msghdr *msg, size_t len, 596ae0d0405SStefano Stabellini int flags) 597ae0d0405SStefano Stabellini { 598ae0d0405SStefano Stabellini struct pvcalls_bedata *bedata; 599ae0d0405SStefano Stabellini int ret; 600ae0d0405SStefano Stabellini struct sock_mapping *map; 601ae0d0405SStefano Stabellini 602ae0d0405SStefano Stabellini if (flags & (MSG_CMSG_CLOEXEC|MSG_ERRQUEUE|MSG_OOB|MSG_TRUNC)) 603ae0d0405SStefano Stabellini return -EOPNOTSUPP; 604ae0d0405SStefano Stabellini 605*64d68718SStefano Stabellini map = pvcalls_enter_sock(sock); 606*64d68718SStefano Stabellini if (IS_ERR(map)) 607*64d68718SStefano Stabellini return PTR_ERR(map); 608ae0d0405SStefano Stabellini bedata = dev_get_drvdata(&pvcalls_front_dev->dev); 609ae0d0405SStefano Stabellini 610ae0d0405SStefano Stabellini mutex_lock(&map->active.in_mutex); 611ae0d0405SStefano Stabellini if (len > XEN_FLEX_RING_SIZE(PVCALLS_RING_ORDER)) 612ae0d0405SStefano Stabellini len = XEN_FLEX_RING_SIZE(PVCALLS_RING_ORDER); 613ae0d0405SStefano Stabellini 614ae0d0405SStefano Stabellini while (!(flags & MSG_DONTWAIT) && !pvcalls_front_read_todo(map)) { 615ae0d0405SStefano Stabellini wait_event_interruptible(map->active.inflight_conn_req, 616ae0d0405SStefano Stabellini pvcalls_front_read_todo(map)); 617ae0d0405SStefano Stabellini } 618ae0d0405SStefano Stabellini ret = __read_ring(map->active.ring, &map->active.data, 619ae0d0405SStefano Stabellini &msg->msg_iter, len, flags); 620ae0d0405SStefano Stabellini 621ae0d0405SStefano Stabellini if (ret > 0) 622ae0d0405SStefano Stabellini notify_remote_via_irq(map->active.irq); 623ae0d0405SStefano Stabellini if (ret == 0) 624ae0d0405SStefano Stabellini ret = (flags & MSG_DONTWAIT) ? -EAGAIN : 0; 625ae0d0405SStefano Stabellini if (ret == -ENOTCONN) 626ae0d0405SStefano Stabellini ret = 0; 627ae0d0405SStefano Stabellini 628ae0d0405SStefano Stabellini mutex_unlock(&map->active.in_mutex); 629*64d68718SStefano Stabellini pvcalls_exit_sock(sock); 630ae0d0405SStefano Stabellini return ret; 631ae0d0405SStefano Stabellini } 632ae0d0405SStefano Stabellini 63367ea9893SStefano Stabellini int pvcalls_front_bind(struct socket *sock, struct sockaddr *addr, int addr_len) 63467ea9893SStefano Stabellini { 63567ea9893SStefano Stabellini struct pvcalls_bedata *bedata; 63667ea9893SStefano Stabellini struct sock_mapping *map = NULL; 63767ea9893SStefano Stabellini struct xen_pvcalls_request *req; 63867ea9893SStefano Stabellini int notify, req_id, ret; 63967ea9893SStefano Stabellini 64067ea9893SStefano Stabellini if (addr->sa_family != AF_INET || sock->type != SOCK_STREAM) 64167ea9893SStefano Stabellini return -EOPNOTSUPP; 64267ea9893SStefano Stabellini 643*64d68718SStefano Stabellini map = pvcalls_enter_sock(sock); 644*64d68718SStefano Stabellini if (IS_ERR(map)) 645*64d68718SStefano Stabellini return PTR_ERR(map); 64667ea9893SStefano Stabellini bedata = dev_get_drvdata(&pvcalls_front_dev->dev); 64767ea9893SStefano Stabellini 64867ea9893SStefano Stabellini spin_lock(&bedata->socket_lock); 64967ea9893SStefano Stabellini ret = get_request(bedata, &req_id); 65067ea9893SStefano Stabellini if (ret < 0) { 65167ea9893SStefano Stabellini spin_unlock(&bedata->socket_lock); 652*64d68718SStefano Stabellini pvcalls_exit_sock(sock); 65367ea9893SStefano Stabellini return ret; 65467ea9893SStefano Stabellini } 65567ea9893SStefano Stabellini req = RING_GET_REQUEST(&bedata->ring, req_id); 65667ea9893SStefano Stabellini req->req_id = req_id; 65767ea9893SStefano Stabellini map->sock = sock; 65867ea9893SStefano Stabellini req->cmd = PVCALLS_BIND; 65967ea9893SStefano Stabellini req->u.bind.id = (uintptr_t)map; 66067ea9893SStefano Stabellini memcpy(req->u.bind.addr, addr, sizeof(*addr)); 66167ea9893SStefano Stabellini req->u.bind.len = addr_len; 66267ea9893SStefano Stabellini 6639774c6ccSStefano Stabellini init_waitqueue_head(&map->passive.inflight_accept_req); 6649774c6ccSStefano Stabellini 66567ea9893SStefano Stabellini map->active_socket = false; 66667ea9893SStefano Stabellini 66767ea9893SStefano Stabellini bedata->ring.req_prod_pvt++; 66867ea9893SStefano Stabellini RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&bedata->ring, notify); 66967ea9893SStefano Stabellini spin_unlock(&bedata->socket_lock); 67067ea9893SStefano Stabellini if (notify) 67167ea9893SStefano Stabellini notify_remote_via_irq(bedata->irq); 67267ea9893SStefano Stabellini 67367ea9893SStefano Stabellini wait_event(bedata->inflight_req, 67467ea9893SStefano Stabellini READ_ONCE(bedata->rsp[req_id].req_id) == req_id); 67567ea9893SStefano Stabellini 67667ea9893SStefano Stabellini /* read req_id, then the content */ 67767ea9893SStefano Stabellini smp_rmb(); 67867ea9893SStefano Stabellini ret = bedata->rsp[req_id].ret; 67967ea9893SStefano Stabellini bedata->rsp[req_id].req_id = PVCALLS_INVALID_ID; 68067ea9893SStefano Stabellini 68167ea9893SStefano Stabellini map->passive.status = PVCALLS_STATUS_BIND; 682*64d68718SStefano Stabellini pvcalls_exit_sock(sock); 68367ea9893SStefano Stabellini return 0; 68467ea9893SStefano Stabellini } 68567ea9893SStefano Stabellini 6861853f11dSStefano Stabellini int pvcalls_front_listen(struct socket *sock, int backlog) 6871853f11dSStefano Stabellini { 6881853f11dSStefano Stabellini struct pvcalls_bedata *bedata; 6891853f11dSStefano Stabellini struct sock_mapping *map; 6901853f11dSStefano Stabellini struct xen_pvcalls_request *req; 6911853f11dSStefano Stabellini int notify, req_id, ret; 6921853f11dSStefano Stabellini 693*64d68718SStefano Stabellini map = pvcalls_enter_sock(sock); 694*64d68718SStefano Stabellini if (IS_ERR(map)) 695*64d68718SStefano Stabellini return PTR_ERR(map); 6961853f11dSStefano Stabellini bedata = dev_get_drvdata(&pvcalls_front_dev->dev); 6971853f11dSStefano Stabellini 6981853f11dSStefano Stabellini if (map->passive.status != PVCALLS_STATUS_BIND) { 699*64d68718SStefano Stabellini pvcalls_exit_sock(sock); 7001853f11dSStefano Stabellini return -EOPNOTSUPP; 7011853f11dSStefano Stabellini } 7021853f11dSStefano Stabellini 7031853f11dSStefano Stabellini spin_lock(&bedata->socket_lock); 7041853f11dSStefano Stabellini ret = get_request(bedata, &req_id); 7051853f11dSStefano Stabellini if (ret < 0) { 7061853f11dSStefano Stabellini spin_unlock(&bedata->socket_lock); 707*64d68718SStefano Stabellini pvcalls_exit_sock(sock); 7081853f11dSStefano Stabellini return ret; 7091853f11dSStefano Stabellini } 7101853f11dSStefano Stabellini req = RING_GET_REQUEST(&bedata->ring, req_id); 7111853f11dSStefano Stabellini req->req_id = req_id; 7121853f11dSStefano Stabellini req->cmd = PVCALLS_LISTEN; 7131853f11dSStefano Stabellini req->u.listen.id = (uintptr_t) map; 7141853f11dSStefano Stabellini req->u.listen.backlog = backlog; 7151853f11dSStefano Stabellini 7161853f11dSStefano Stabellini bedata->ring.req_prod_pvt++; 7171853f11dSStefano Stabellini RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&bedata->ring, notify); 7181853f11dSStefano Stabellini spin_unlock(&bedata->socket_lock); 7191853f11dSStefano Stabellini if (notify) 7201853f11dSStefano Stabellini notify_remote_via_irq(bedata->irq); 7211853f11dSStefano Stabellini 7221853f11dSStefano Stabellini wait_event(bedata->inflight_req, 7231853f11dSStefano Stabellini READ_ONCE(bedata->rsp[req_id].req_id) == req_id); 7241853f11dSStefano Stabellini 7251853f11dSStefano Stabellini /* read req_id, then the content */ 7261853f11dSStefano Stabellini smp_rmb(); 7271853f11dSStefano Stabellini ret = bedata->rsp[req_id].ret; 7281853f11dSStefano Stabellini bedata->rsp[req_id].req_id = PVCALLS_INVALID_ID; 7291853f11dSStefano Stabellini 7301853f11dSStefano Stabellini map->passive.status = PVCALLS_STATUS_LISTEN; 731*64d68718SStefano Stabellini pvcalls_exit_sock(sock); 7321853f11dSStefano Stabellini return ret; 7331853f11dSStefano Stabellini } 7341853f11dSStefano Stabellini 7359774c6ccSStefano Stabellini int pvcalls_front_accept(struct socket *sock, struct socket *newsock, int flags) 7369774c6ccSStefano Stabellini { 7379774c6ccSStefano Stabellini struct pvcalls_bedata *bedata; 7389774c6ccSStefano Stabellini struct sock_mapping *map; 7399774c6ccSStefano Stabellini struct sock_mapping *map2 = NULL; 7409774c6ccSStefano Stabellini struct xen_pvcalls_request *req; 7419774c6ccSStefano Stabellini int notify, req_id, ret, evtchn, nonblock; 7429774c6ccSStefano Stabellini 743*64d68718SStefano Stabellini map = pvcalls_enter_sock(sock); 744*64d68718SStefano Stabellini if (IS_ERR(map)) 745*64d68718SStefano Stabellini return PTR_ERR(map); 7469774c6ccSStefano Stabellini bedata = dev_get_drvdata(&pvcalls_front_dev->dev); 7479774c6ccSStefano Stabellini 7489774c6ccSStefano Stabellini if (map->passive.status != PVCALLS_STATUS_LISTEN) { 749*64d68718SStefano Stabellini pvcalls_exit_sock(sock); 7509774c6ccSStefano Stabellini return -EINVAL; 7519774c6ccSStefano Stabellini } 7529774c6ccSStefano Stabellini 7539774c6ccSStefano Stabellini nonblock = flags & SOCK_NONBLOCK; 7549774c6ccSStefano Stabellini /* 7559774c6ccSStefano Stabellini * Backend only supports 1 inflight accept request, will return 7569774c6ccSStefano Stabellini * errors for the others 7579774c6ccSStefano Stabellini */ 7589774c6ccSStefano Stabellini if (test_and_set_bit(PVCALLS_FLAG_ACCEPT_INFLIGHT, 7599774c6ccSStefano Stabellini (void *)&map->passive.flags)) { 7609774c6ccSStefano Stabellini req_id = READ_ONCE(map->passive.inflight_req_id); 7619774c6ccSStefano Stabellini if (req_id != PVCALLS_INVALID_ID && 7629774c6ccSStefano Stabellini READ_ONCE(bedata->rsp[req_id].req_id) == req_id) { 7639774c6ccSStefano Stabellini map2 = map->passive.accept_map; 7649774c6ccSStefano Stabellini goto received; 7659774c6ccSStefano Stabellini } 7669774c6ccSStefano Stabellini if (nonblock) { 767*64d68718SStefano Stabellini pvcalls_exit_sock(sock); 7689774c6ccSStefano Stabellini return -EAGAIN; 7699774c6ccSStefano Stabellini } 7709774c6ccSStefano Stabellini if (wait_event_interruptible(map->passive.inflight_accept_req, 7719774c6ccSStefano Stabellini !test_and_set_bit(PVCALLS_FLAG_ACCEPT_INFLIGHT, 7729774c6ccSStefano Stabellini (void *)&map->passive.flags))) { 773*64d68718SStefano Stabellini pvcalls_exit_sock(sock); 7749774c6ccSStefano Stabellini return -EINTR; 7759774c6ccSStefano Stabellini } 7769774c6ccSStefano Stabellini } 7779774c6ccSStefano Stabellini 7789774c6ccSStefano Stabellini spin_lock(&bedata->socket_lock); 7799774c6ccSStefano Stabellini ret = get_request(bedata, &req_id); 7809774c6ccSStefano Stabellini if (ret < 0) { 7819774c6ccSStefano Stabellini clear_bit(PVCALLS_FLAG_ACCEPT_INFLIGHT, 7829774c6ccSStefano Stabellini (void *)&map->passive.flags); 7839774c6ccSStefano Stabellini spin_unlock(&bedata->socket_lock); 784*64d68718SStefano Stabellini pvcalls_exit_sock(sock); 7859774c6ccSStefano Stabellini return ret; 7869774c6ccSStefano Stabellini } 7874aac2cafSWei Yongjun map2 = kzalloc(sizeof(*map2), GFP_ATOMIC); 7889774c6ccSStefano Stabellini if (map2 == NULL) { 7899774c6ccSStefano Stabellini clear_bit(PVCALLS_FLAG_ACCEPT_INFLIGHT, 7909774c6ccSStefano Stabellini (void *)&map->passive.flags); 7919774c6ccSStefano Stabellini spin_unlock(&bedata->socket_lock); 792*64d68718SStefano Stabellini pvcalls_exit_sock(sock); 7939774c6ccSStefano Stabellini return -ENOMEM; 7949774c6ccSStefano Stabellini } 7959774c6ccSStefano Stabellini ret = create_active(map2, &evtchn); 7969774c6ccSStefano Stabellini if (ret < 0) { 7979774c6ccSStefano Stabellini kfree(map2); 7989774c6ccSStefano Stabellini clear_bit(PVCALLS_FLAG_ACCEPT_INFLIGHT, 7999774c6ccSStefano Stabellini (void *)&map->passive.flags); 8009774c6ccSStefano Stabellini spin_unlock(&bedata->socket_lock); 801*64d68718SStefano Stabellini pvcalls_exit_sock(sock); 8029774c6ccSStefano Stabellini return ret; 8039774c6ccSStefano Stabellini } 8049774c6ccSStefano Stabellini list_add_tail(&map2->list, &bedata->socket_mappings); 8059774c6ccSStefano Stabellini 8069774c6ccSStefano Stabellini req = RING_GET_REQUEST(&bedata->ring, req_id); 8079774c6ccSStefano Stabellini req->req_id = req_id; 8089774c6ccSStefano Stabellini req->cmd = PVCALLS_ACCEPT; 8099774c6ccSStefano Stabellini req->u.accept.id = (uintptr_t) map; 8109774c6ccSStefano Stabellini req->u.accept.ref = map2->active.ref; 8119774c6ccSStefano Stabellini req->u.accept.id_new = (uintptr_t) map2; 8129774c6ccSStefano Stabellini req->u.accept.evtchn = evtchn; 8139774c6ccSStefano Stabellini map->passive.accept_map = map2; 8149774c6ccSStefano Stabellini 8159774c6ccSStefano Stabellini bedata->ring.req_prod_pvt++; 8169774c6ccSStefano Stabellini RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&bedata->ring, notify); 8179774c6ccSStefano Stabellini spin_unlock(&bedata->socket_lock); 8189774c6ccSStefano Stabellini if (notify) 8199774c6ccSStefano Stabellini notify_remote_via_irq(bedata->irq); 8209774c6ccSStefano Stabellini /* We could check if we have received a response before returning. */ 8219774c6ccSStefano Stabellini if (nonblock) { 8229774c6ccSStefano Stabellini WRITE_ONCE(map->passive.inflight_req_id, req_id); 823*64d68718SStefano Stabellini pvcalls_exit_sock(sock); 8249774c6ccSStefano Stabellini return -EAGAIN; 8259774c6ccSStefano Stabellini } 8269774c6ccSStefano Stabellini 8279774c6ccSStefano Stabellini if (wait_event_interruptible(bedata->inflight_req, 8289774c6ccSStefano Stabellini READ_ONCE(bedata->rsp[req_id].req_id) == req_id)) { 829*64d68718SStefano Stabellini pvcalls_exit_sock(sock); 8309774c6ccSStefano Stabellini return -EINTR; 8319774c6ccSStefano Stabellini } 8329774c6ccSStefano Stabellini /* read req_id, then the content */ 8339774c6ccSStefano Stabellini smp_rmb(); 8349774c6ccSStefano Stabellini 8359774c6ccSStefano Stabellini received: 8369774c6ccSStefano Stabellini map2->sock = newsock; 8379774c6ccSStefano Stabellini newsock->sk = kzalloc(sizeof(*newsock->sk), GFP_KERNEL); 8389774c6ccSStefano Stabellini if (!newsock->sk) { 8399774c6ccSStefano Stabellini bedata->rsp[req_id].req_id = PVCALLS_INVALID_ID; 8409774c6ccSStefano Stabellini map->passive.inflight_req_id = PVCALLS_INVALID_ID; 8419774c6ccSStefano Stabellini clear_bit(PVCALLS_FLAG_ACCEPT_INFLIGHT, 8429774c6ccSStefano Stabellini (void *)&map->passive.flags); 8439774c6ccSStefano Stabellini pvcalls_front_free_map(bedata, map2); 844*64d68718SStefano Stabellini pvcalls_exit_sock(sock); 8459774c6ccSStefano Stabellini return -ENOMEM; 8469774c6ccSStefano Stabellini } 8479774c6ccSStefano Stabellini newsock->sk->sk_send_head = (void *)map2; 8489774c6ccSStefano Stabellini 8499774c6ccSStefano Stabellini ret = bedata->rsp[req_id].ret; 8509774c6ccSStefano Stabellini bedata->rsp[req_id].req_id = PVCALLS_INVALID_ID; 8519774c6ccSStefano Stabellini map->passive.inflight_req_id = PVCALLS_INVALID_ID; 8529774c6ccSStefano Stabellini 8539774c6ccSStefano Stabellini clear_bit(PVCALLS_FLAG_ACCEPT_INFLIGHT, (void *)&map->passive.flags); 8549774c6ccSStefano Stabellini wake_up(&map->passive.inflight_accept_req); 8559774c6ccSStefano Stabellini 856*64d68718SStefano Stabellini pvcalls_exit_sock(sock); 8579774c6ccSStefano Stabellini return ret; 8589774c6ccSStefano Stabellini } 8599774c6ccSStefano Stabellini 860afc9a42bSAl Viro static __poll_t pvcalls_front_poll_passive(struct file *file, 8615842c835SStefano Stabellini struct pvcalls_bedata *bedata, 8625842c835SStefano Stabellini struct sock_mapping *map, 8635842c835SStefano Stabellini poll_table *wait) 8645842c835SStefano Stabellini { 8655842c835SStefano Stabellini int notify, req_id, ret; 8665842c835SStefano Stabellini struct xen_pvcalls_request *req; 8675842c835SStefano Stabellini 8685842c835SStefano Stabellini if (test_bit(PVCALLS_FLAG_ACCEPT_INFLIGHT, 8695842c835SStefano Stabellini (void *)&map->passive.flags)) { 8705842c835SStefano Stabellini uint32_t req_id = READ_ONCE(map->passive.inflight_req_id); 8715842c835SStefano Stabellini 8725842c835SStefano Stabellini if (req_id != PVCALLS_INVALID_ID && 8735842c835SStefano Stabellini READ_ONCE(bedata->rsp[req_id].req_id) == req_id) 874a9a08845SLinus Torvalds return EPOLLIN | EPOLLRDNORM; 8755842c835SStefano Stabellini 8765842c835SStefano Stabellini poll_wait(file, &map->passive.inflight_accept_req, wait); 8775842c835SStefano Stabellini return 0; 8785842c835SStefano Stabellini } 8795842c835SStefano Stabellini 8805842c835SStefano Stabellini if (test_and_clear_bit(PVCALLS_FLAG_POLL_RET, 8815842c835SStefano Stabellini (void *)&map->passive.flags)) 882a9a08845SLinus Torvalds return EPOLLIN | EPOLLRDNORM; 8835842c835SStefano Stabellini 8845842c835SStefano Stabellini /* 8855842c835SStefano Stabellini * First check RET, then INFLIGHT. No barriers necessary to 8865842c835SStefano Stabellini * ensure execution ordering because of the conditional 8875842c835SStefano Stabellini * instructions creating control dependencies. 8885842c835SStefano Stabellini */ 8895842c835SStefano Stabellini 8905842c835SStefano Stabellini if (test_and_set_bit(PVCALLS_FLAG_POLL_INFLIGHT, 8915842c835SStefano Stabellini (void *)&map->passive.flags)) { 8925842c835SStefano Stabellini poll_wait(file, &bedata->inflight_req, wait); 8935842c835SStefano Stabellini return 0; 8945842c835SStefano Stabellini } 8955842c835SStefano Stabellini 8965842c835SStefano Stabellini spin_lock(&bedata->socket_lock); 8975842c835SStefano Stabellini ret = get_request(bedata, &req_id); 8985842c835SStefano Stabellini if (ret < 0) { 8995842c835SStefano Stabellini spin_unlock(&bedata->socket_lock); 9005842c835SStefano Stabellini return ret; 9015842c835SStefano Stabellini } 9025842c835SStefano Stabellini req = RING_GET_REQUEST(&bedata->ring, req_id); 9035842c835SStefano Stabellini req->req_id = req_id; 9045842c835SStefano Stabellini req->cmd = PVCALLS_POLL; 9055842c835SStefano Stabellini req->u.poll.id = (uintptr_t) map; 9065842c835SStefano Stabellini 9075842c835SStefano Stabellini bedata->ring.req_prod_pvt++; 9085842c835SStefano Stabellini RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&bedata->ring, notify); 9095842c835SStefano Stabellini spin_unlock(&bedata->socket_lock); 9105842c835SStefano Stabellini if (notify) 9115842c835SStefano Stabellini notify_remote_via_irq(bedata->irq); 9125842c835SStefano Stabellini 9135842c835SStefano Stabellini poll_wait(file, &bedata->inflight_req, wait); 9145842c835SStefano Stabellini return 0; 9155842c835SStefano Stabellini } 9165842c835SStefano Stabellini 917afc9a42bSAl Viro static __poll_t pvcalls_front_poll_active(struct file *file, 9185842c835SStefano Stabellini struct pvcalls_bedata *bedata, 9195842c835SStefano Stabellini struct sock_mapping *map, 9205842c835SStefano Stabellini poll_table *wait) 9215842c835SStefano Stabellini { 922afc9a42bSAl Viro __poll_t mask = 0; 9235842c835SStefano Stabellini int32_t in_error, out_error; 9245842c835SStefano Stabellini struct pvcalls_data_intf *intf = map->active.ring; 9255842c835SStefano Stabellini 9265842c835SStefano Stabellini out_error = intf->out_error; 9275842c835SStefano Stabellini in_error = intf->in_error; 9285842c835SStefano Stabellini 9295842c835SStefano Stabellini poll_wait(file, &map->active.inflight_conn_req, wait); 9305842c835SStefano Stabellini if (pvcalls_front_write_todo(map)) 931a9a08845SLinus Torvalds mask |= EPOLLOUT | EPOLLWRNORM; 9325842c835SStefano Stabellini if (pvcalls_front_read_todo(map)) 933a9a08845SLinus Torvalds mask |= EPOLLIN | EPOLLRDNORM; 9345842c835SStefano Stabellini if (in_error != 0 || out_error != 0) 935a9a08845SLinus Torvalds mask |= EPOLLERR; 9365842c835SStefano Stabellini 9375842c835SStefano Stabellini return mask; 9385842c835SStefano Stabellini } 9395842c835SStefano Stabellini 940afc9a42bSAl Viro __poll_t pvcalls_front_poll(struct file *file, struct socket *sock, 9415842c835SStefano Stabellini poll_table *wait) 9425842c835SStefano Stabellini { 9435842c835SStefano Stabellini struct pvcalls_bedata *bedata; 9445842c835SStefano Stabellini struct sock_mapping *map; 945afc9a42bSAl Viro __poll_t ret; 9465842c835SStefano Stabellini 947*64d68718SStefano Stabellini map = pvcalls_enter_sock(sock); 948*64d68718SStefano Stabellini if (IS_ERR(map)) 949a9a08845SLinus Torvalds return EPOLLNVAL; 9505842c835SStefano Stabellini bedata = dev_get_drvdata(&pvcalls_front_dev->dev); 9515842c835SStefano Stabellini 9525842c835SStefano Stabellini if (map->active_socket) 9535842c835SStefano Stabellini ret = pvcalls_front_poll_active(file, bedata, map, wait); 9545842c835SStefano Stabellini else 9555842c835SStefano Stabellini ret = pvcalls_front_poll_passive(file, bedata, map, wait); 956*64d68718SStefano Stabellini pvcalls_exit_sock(sock); 9575842c835SStefano Stabellini return ret; 9585842c835SStefano Stabellini } 9595842c835SStefano Stabellini 960235a71c5SStefano Stabellini int pvcalls_front_release(struct socket *sock) 961235a71c5SStefano Stabellini { 962235a71c5SStefano Stabellini struct pvcalls_bedata *bedata; 963235a71c5SStefano Stabellini struct sock_mapping *map; 964235a71c5SStefano Stabellini int req_id, notify, ret; 965235a71c5SStefano Stabellini struct xen_pvcalls_request *req; 966235a71c5SStefano Stabellini 967235a71c5SStefano Stabellini if (sock->sk == NULL) 968235a71c5SStefano Stabellini return 0; 969235a71c5SStefano Stabellini 970*64d68718SStefano Stabellini map = pvcalls_enter_sock(sock); 971*64d68718SStefano Stabellini if (IS_ERR(map)) { 972*64d68718SStefano Stabellini if (PTR_ERR(map) == -ENOTCONN) 973235a71c5SStefano Stabellini return -EIO; 974*64d68718SStefano Stabellini else 975235a71c5SStefano Stabellini return 0; 976235a71c5SStefano Stabellini } 977*64d68718SStefano Stabellini bedata = dev_get_drvdata(&pvcalls_front_dev->dev); 978235a71c5SStefano Stabellini 979235a71c5SStefano Stabellini spin_lock(&bedata->socket_lock); 980235a71c5SStefano Stabellini ret = get_request(bedata, &req_id); 981235a71c5SStefano Stabellini if (ret < 0) { 982235a71c5SStefano Stabellini spin_unlock(&bedata->socket_lock); 983*64d68718SStefano Stabellini pvcalls_exit_sock(sock); 984235a71c5SStefano Stabellini return ret; 985235a71c5SStefano Stabellini } 986235a71c5SStefano Stabellini sock->sk->sk_send_head = NULL; 987235a71c5SStefano Stabellini 988235a71c5SStefano Stabellini req = RING_GET_REQUEST(&bedata->ring, req_id); 989235a71c5SStefano Stabellini req->req_id = req_id; 990235a71c5SStefano Stabellini req->cmd = PVCALLS_RELEASE; 991235a71c5SStefano Stabellini req->u.release.id = (uintptr_t)map; 992235a71c5SStefano Stabellini 993235a71c5SStefano Stabellini bedata->ring.req_prod_pvt++; 994235a71c5SStefano Stabellini RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&bedata->ring, notify); 995235a71c5SStefano Stabellini spin_unlock(&bedata->socket_lock); 996235a71c5SStefano Stabellini if (notify) 997235a71c5SStefano Stabellini notify_remote_via_irq(bedata->irq); 998235a71c5SStefano Stabellini 999235a71c5SStefano Stabellini wait_event(bedata->inflight_req, 1000235a71c5SStefano Stabellini READ_ONCE(bedata->rsp[req_id].req_id) == req_id); 1001235a71c5SStefano Stabellini 1002235a71c5SStefano Stabellini if (map->active_socket) { 1003235a71c5SStefano Stabellini /* 1004235a71c5SStefano Stabellini * Set in_error and wake up inflight_conn_req to force 1005235a71c5SStefano Stabellini * recvmsg waiters to exit. 1006235a71c5SStefano Stabellini */ 1007235a71c5SStefano Stabellini map->active.ring->in_error = -EBADF; 1008235a71c5SStefano Stabellini wake_up_interruptible(&map->active.inflight_conn_req); 1009235a71c5SStefano Stabellini 1010235a71c5SStefano Stabellini /* 1011646d944cSStefano Stabellini * We need to make sure that sendmsg/recvmsg on this socket have 1012646d944cSStefano Stabellini * not started before we've cleared sk_send_head here. The 1013*64d68718SStefano Stabellini * easiest way to guarantee this is to see that no pvcalls 1014*64d68718SStefano Stabellini * (other than us) is in progress on this socket. 1015235a71c5SStefano Stabellini */ 1016*64d68718SStefano Stabellini while (atomic_read(&map->refcount) > 1) 1017235a71c5SStefano Stabellini cpu_relax(); 1018235a71c5SStefano Stabellini 1019235a71c5SStefano Stabellini pvcalls_front_free_map(bedata, map); 1020235a71c5SStefano Stabellini } else { 1021235a71c5SStefano Stabellini spin_lock(&bedata->socket_lock); 1022235a71c5SStefano Stabellini list_del(&map->list); 1023235a71c5SStefano Stabellini spin_unlock(&bedata->socket_lock); 1024235a71c5SStefano Stabellini if (READ_ONCE(map->passive.inflight_req_id) != 1025235a71c5SStefano Stabellini PVCALLS_INVALID_ID) { 1026235a71c5SStefano Stabellini pvcalls_front_free_map(bedata, 1027235a71c5SStefano Stabellini map->passive.accept_map); 1028235a71c5SStefano Stabellini } 1029235a71c5SStefano Stabellini kfree(map); 1030235a71c5SStefano Stabellini } 1031235a71c5SStefano Stabellini WRITE_ONCE(bedata->rsp[req_id].req_id, PVCALLS_INVALID_ID); 1032235a71c5SStefano Stabellini 1033235a71c5SStefano Stabellini pvcalls_exit(); 1034235a71c5SStefano Stabellini return 0; 1035235a71c5SStefano Stabellini } 1036235a71c5SStefano Stabellini 1037416efba0SStefano Stabellini static const struct xenbus_device_id pvcalls_front_ids[] = { 1038416efba0SStefano Stabellini { "pvcalls" }, 1039416efba0SStefano Stabellini { "" } 1040416efba0SStefano Stabellini }; 1041416efba0SStefano Stabellini 1042416efba0SStefano Stabellini static int pvcalls_front_remove(struct xenbus_device *dev) 1043416efba0SStefano Stabellini { 1044aa7ba376SStefano Stabellini struct pvcalls_bedata *bedata; 1045aa7ba376SStefano Stabellini struct sock_mapping *map = NULL, *n; 1046aa7ba376SStefano Stabellini 1047aa7ba376SStefano Stabellini bedata = dev_get_drvdata(&pvcalls_front_dev->dev); 1048aa7ba376SStefano Stabellini dev_set_drvdata(&dev->dev, NULL); 1049aa7ba376SStefano Stabellini pvcalls_front_dev = NULL; 1050aa7ba376SStefano Stabellini if (bedata->irq >= 0) 1051aa7ba376SStefano Stabellini unbind_from_irqhandler(bedata->irq, dev); 1052aa7ba376SStefano Stabellini 1053cb1c7d9bSStefano Stabellini list_for_each_entry_safe(map, n, &bedata->socket_mappings, list) { 1054cb1c7d9bSStefano Stabellini map->sock->sk->sk_send_head = NULL; 1055cb1c7d9bSStefano Stabellini if (map->active_socket) { 1056cb1c7d9bSStefano Stabellini map->active.ring->in_error = -EBADF; 1057cb1c7d9bSStefano Stabellini wake_up_interruptible(&map->active.inflight_conn_req); 1058cb1c7d9bSStefano Stabellini } 1059cb1c7d9bSStefano Stabellini } 1060cb1c7d9bSStefano Stabellini 1061aa7ba376SStefano Stabellini smp_mb(); 1062aa7ba376SStefano Stabellini while (atomic_read(&pvcalls_refcount) > 0) 1063aa7ba376SStefano Stabellini cpu_relax(); 1064aa7ba376SStefano Stabellini list_for_each_entry_safe(map, n, &bedata->socket_mappings, list) { 1065aa7ba376SStefano Stabellini if (map->active_socket) { 1066aa7ba376SStefano Stabellini /* No need to lock, refcount is 0 */ 1067aa7ba376SStefano Stabellini pvcalls_front_free_map(bedata, map); 1068aa7ba376SStefano Stabellini } else { 1069aa7ba376SStefano Stabellini list_del(&map->list); 1070aa7ba376SStefano Stabellini kfree(map); 1071aa7ba376SStefano Stabellini } 1072aa7ba376SStefano Stabellini } 10731ab134caSDan Carpenter if (bedata->ref != -1) 1074aa7ba376SStefano Stabellini gnttab_end_foreign_access(bedata->ref, 0, 0); 1075aa7ba376SStefano Stabellini kfree(bedata->ring.sring); 1076aa7ba376SStefano Stabellini kfree(bedata); 1077aa7ba376SStefano Stabellini xenbus_switch_state(dev, XenbusStateClosed); 1078416efba0SStefano Stabellini return 0; 1079416efba0SStefano Stabellini } 1080416efba0SStefano Stabellini 1081416efba0SStefano Stabellini static int pvcalls_front_probe(struct xenbus_device *dev, 1082416efba0SStefano Stabellini const struct xenbus_device_id *id) 1083416efba0SStefano Stabellini { 108421968190SStefano Stabellini int ret = -ENOMEM, evtchn, i; 108521968190SStefano Stabellini unsigned int max_page_order, function_calls, len; 108621968190SStefano Stabellini char *versions; 108721968190SStefano Stabellini grant_ref_t gref_head = 0; 108821968190SStefano Stabellini struct xenbus_transaction xbt; 108921968190SStefano Stabellini struct pvcalls_bedata *bedata = NULL; 109021968190SStefano Stabellini struct xen_pvcalls_sring *sring; 109121968190SStefano Stabellini 109221968190SStefano Stabellini if (pvcalls_front_dev != NULL) { 109321968190SStefano Stabellini dev_err(&dev->dev, "only one PV Calls connection supported\n"); 109421968190SStefano Stabellini return -EINVAL; 109521968190SStefano Stabellini } 109621968190SStefano Stabellini 109721968190SStefano Stabellini versions = xenbus_read(XBT_NIL, dev->otherend, "versions", &len); 10988c71fa88SDan Carpenter if (IS_ERR(versions)) 10998c71fa88SDan Carpenter return PTR_ERR(versions); 110021968190SStefano Stabellini if (!len) 110121968190SStefano Stabellini return -EINVAL; 110221968190SStefano Stabellini if (strcmp(versions, "1")) { 110321968190SStefano Stabellini kfree(versions); 110421968190SStefano Stabellini return -EINVAL; 110521968190SStefano Stabellini } 110621968190SStefano Stabellini kfree(versions); 110721968190SStefano Stabellini max_page_order = xenbus_read_unsigned(dev->otherend, 110821968190SStefano Stabellini "max-page-order", 0); 110921968190SStefano Stabellini if (max_page_order < PVCALLS_RING_ORDER) 111021968190SStefano Stabellini return -ENODEV; 111121968190SStefano Stabellini function_calls = xenbus_read_unsigned(dev->otherend, 111221968190SStefano Stabellini "function-calls", 0); 111321968190SStefano Stabellini /* See XENBUS_FUNCTIONS_CALLS in pvcalls.h */ 111421968190SStefano Stabellini if (function_calls != 1) 111521968190SStefano Stabellini return -ENODEV; 111621968190SStefano Stabellini pr_info("%s max-page-order is %u\n", __func__, max_page_order); 111721968190SStefano Stabellini 111821968190SStefano Stabellini bedata = kzalloc(sizeof(struct pvcalls_bedata), GFP_KERNEL); 111921968190SStefano Stabellini if (!bedata) 112021968190SStefano Stabellini return -ENOMEM; 112121968190SStefano Stabellini 112221968190SStefano Stabellini dev_set_drvdata(&dev->dev, bedata); 112321968190SStefano Stabellini pvcalls_front_dev = dev; 112421968190SStefano Stabellini init_waitqueue_head(&bedata->inflight_req); 112521968190SStefano Stabellini INIT_LIST_HEAD(&bedata->socket_mappings); 112621968190SStefano Stabellini spin_lock_init(&bedata->socket_lock); 112721968190SStefano Stabellini bedata->irq = -1; 112821968190SStefano Stabellini bedata->ref = -1; 112921968190SStefano Stabellini 113021968190SStefano Stabellini for (i = 0; i < PVCALLS_NR_RSP_PER_RING; i++) 113121968190SStefano Stabellini bedata->rsp[i].req_id = PVCALLS_INVALID_ID; 113221968190SStefano Stabellini 113321968190SStefano Stabellini sring = (struct xen_pvcalls_sring *) __get_free_page(GFP_KERNEL | 113421968190SStefano Stabellini __GFP_ZERO); 113521968190SStefano Stabellini if (!sring) 113621968190SStefano Stabellini goto error; 113721968190SStefano Stabellini SHARED_RING_INIT(sring); 113821968190SStefano Stabellini FRONT_RING_INIT(&bedata->ring, sring, XEN_PAGE_SIZE); 113921968190SStefano Stabellini 114021968190SStefano Stabellini ret = xenbus_alloc_evtchn(dev, &evtchn); 114121968190SStefano Stabellini if (ret) 114221968190SStefano Stabellini goto error; 114321968190SStefano Stabellini 114421968190SStefano Stabellini bedata->irq = bind_evtchn_to_irqhandler(evtchn, 114521968190SStefano Stabellini pvcalls_front_event_handler, 114621968190SStefano Stabellini 0, "pvcalls-frontend", dev); 114721968190SStefano Stabellini if (bedata->irq < 0) { 114821968190SStefano Stabellini ret = bedata->irq; 114921968190SStefano Stabellini goto error; 115021968190SStefano Stabellini } 115121968190SStefano Stabellini 115221968190SStefano Stabellini ret = gnttab_alloc_grant_references(1, &gref_head); 115321968190SStefano Stabellini if (ret < 0) 115421968190SStefano Stabellini goto error; 115595110ac8SColin Ian King ret = gnttab_claim_grant_reference(&gref_head); 115695110ac8SColin Ian King if (ret < 0) 115721968190SStefano Stabellini goto error; 115895110ac8SColin Ian King bedata->ref = ret; 115921968190SStefano Stabellini gnttab_grant_foreign_access_ref(bedata->ref, dev->otherend_id, 116021968190SStefano Stabellini virt_to_gfn((void *)sring), 0); 116121968190SStefano Stabellini 116221968190SStefano Stabellini again: 116321968190SStefano Stabellini ret = xenbus_transaction_start(&xbt); 116421968190SStefano Stabellini if (ret) { 116521968190SStefano Stabellini xenbus_dev_fatal(dev, ret, "starting transaction"); 116621968190SStefano Stabellini goto error; 116721968190SStefano Stabellini } 116821968190SStefano Stabellini ret = xenbus_printf(xbt, dev->nodename, "version", "%u", 1); 116921968190SStefano Stabellini if (ret) 117021968190SStefano Stabellini goto error_xenbus; 117121968190SStefano Stabellini ret = xenbus_printf(xbt, dev->nodename, "ring-ref", "%d", bedata->ref); 117221968190SStefano Stabellini if (ret) 117321968190SStefano Stabellini goto error_xenbus; 117421968190SStefano Stabellini ret = xenbus_printf(xbt, dev->nodename, "port", "%u", 117521968190SStefano Stabellini evtchn); 117621968190SStefano Stabellini if (ret) 117721968190SStefano Stabellini goto error_xenbus; 117821968190SStefano Stabellini ret = xenbus_transaction_end(xbt, 0); 117921968190SStefano Stabellini if (ret) { 118021968190SStefano Stabellini if (ret == -EAGAIN) 118121968190SStefano Stabellini goto again; 118221968190SStefano Stabellini xenbus_dev_fatal(dev, ret, "completing transaction"); 118321968190SStefano Stabellini goto error; 118421968190SStefano Stabellini } 118521968190SStefano Stabellini xenbus_switch_state(dev, XenbusStateInitialised); 118621968190SStefano Stabellini 1187416efba0SStefano Stabellini return 0; 118821968190SStefano Stabellini 118921968190SStefano Stabellini error_xenbus: 119021968190SStefano Stabellini xenbus_transaction_end(xbt, 1); 119121968190SStefano Stabellini xenbus_dev_fatal(dev, ret, "writing xenstore"); 119221968190SStefano Stabellini error: 119321968190SStefano Stabellini pvcalls_front_remove(dev); 119421968190SStefano Stabellini return ret; 1195416efba0SStefano Stabellini } 1196416efba0SStefano Stabellini 1197416efba0SStefano Stabellini static void pvcalls_front_changed(struct xenbus_device *dev, 1198416efba0SStefano Stabellini enum xenbus_state backend_state) 1199416efba0SStefano Stabellini { 120021968190SStefano Stabellini switch (backend_state) { 120121968190SStefano Stabellini case XenbusStateReconfiguring: 120221968190SStefano Stabellini case XenbusStateReconfigured: 120321968190SStefano Stabellini case XenbusStateInitialising: 120421968190SStefano Stabellini case XenbusStateInitialised: 120521968190SStefano Stabellini case XenbusStateUnknown: 120621968190SStefano Stabellini break; 120721968190SStefano Stabellini 120821968190SStefano Stabellini case XenbusStateInitWait: 120921968190SStefano Stabellini break; 121021968190SStefano Stabellini 121121968190SStefano Stabellini case XenbusStateConnected: 121221968190SStefano Stabellini xenbus_switch_state(dev, XenbusStateConnected); 121321968190SStefano Stabellini break; 121421968190SStefano Stabellini 121521968190SStefano Stabellini case XenbusStateClosed: 121621968190SStefano Stabellini if (dev->state == XenbusStateClosed) 121721968190SStefano Stabellini break; 12183d8765d4SGustavo A. R. Silva /* Missed the backend's CLOSING state */ 12193d8765d4SGustavo A. R. Silva /* fall through */ 122021968190SStefano Stabellini case XenbusStateClosing: 122121968190SStefano Stabellini xenbus_frontend_closed(dev); 122221968190SStefano Stabellini break; 122321968190SStefano Stabellini } 1224416efba0SStefano Stabellini } 1225416efba0SStefano Stabellini 1226416efba0SStefano Stabellini static struct xenbus_driver pvcalls_front_driver = { 1227416efba0SStefano Stabellini .ids = pvcalls_front_ids, 1228416efba0SStefano Stabellini .probe = pvcalls_front_probe, 1229416efba0SStefano Stabellini .remove = pvcalls_front_remove, 1230416efba0SStefano Stabellini .otherend_changed = pvcalls_front_changed, 1231416efba0SStefano Stabellini }; 1232416efba0SStefano Stabellini 1233416efba0SStefano Stabellini static int __init pvcalls_frontend_init(void) 1234416efba0SStefano Stabellini { 1235416efba0SStefano Stabellini if (!xen_domain()) 1236416efba0SStefano Stabellini return -ENODEV; 1237416efba0SStefano Stabellini 1238416efba0SStefano Stabellini pr_info("Initialising Xen pvcalls frontend driver\n"); 1239416efba0SStefano Stabellini 1240416efba0SStefano Stabellini return xenbus_register_frontend(&pvcalls_front_driver); 1241416efba0SStefano Stabellini } 1242416efba0SStefano Stabellini 1243416efba0SStefano Stabellini module_init(pvcalls_frontend_init); 124424e7f84dSBoris Ostrovsky 124524e7f84dSBoris Ostrovsky MODULE_DESCRIPTION("Xen PV Calls frontend driver"); 124624e7f84dSBoris Ostrovsky MODULE_AUTHOR("Stefano Stabellini <sstabellini@kernel.org>"); 124724e7f84dSBoris Ostrovsky MODULE_LICENSE("GPL"); 1248