1b530cc79SEric Van Hensbergen /* 2fea511a6SEric Van Hensbergen * The Virtio 9p transport driver 3b530cc79SEric Van Hensbergen * 4e2735b77SEric Van Hensbergen * This is a block based transport driver based on the lguest block driver 5e2735b77SEric Van Hensbergen * code. 6b530cc79SEric Van Hensbergen * 7fea511a6SEric Van Hensbergen * Copyright (C) 2007, 2008 Eric Van Hensbergen, IBM Corporation 8b530cc79SEric Van Hensbergen * 9b530cc79SEric Van Hensbergen * Based on virtio console driver 10b530cc79SEric Van Hensbergen * Copyright (C) 2006, 2007 Rusty Russell, IBM Corporation 11b530cc79SEric Van Hensbergen * 12b530cc79SEric Van Hensbergen * This program is free software; you can redistribute it and/or modify 13b530cc79SEric Van Hensbergen * it under the terms of the GNU General Public License version 2 14b530cc79SEric Van Hensbergen * as published by the Free Software Foundation. 15b530cc79SEric Van Hensbergen * 16b530cc79SEric Van Hensbergen * This program is distributed in the hope that it will be useful, 17b530cc79SEric Van Hensbergen * but WITHOUT ANY WARRANTY; without even the implied warranty of 18b530cc79SEric Van Hensbergen * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 19b530cc79SEric Van Hensbergen * GNU General Public License for more details. 20b530cc79SEric Van Hensbergen * 21b530cc79SEric Van Hensbergen * You should have received a copy of the GNU General Public License 22b530cc79SEric Van Hensbergen * along with this program; if not, write to: 23b530cc79SEric Van Hensbergen * Free Software Foundation 24b530cc79SEric Van Hensbergen * 51 Franklin Street, Fifth Floor 25b530cc79SEric Van Hensbergen * Boston, MA 02111-1301 USA 26b530cc79SEric Van Hensbergen * 27b530cc79SEric Van Hensbergen */ 28b530cc79SEric Van Hensbergen 29b530cc79SEric Van Hensbergen #include <linux/in.h> 30b530cc79SEric Van Hensbergen #include <linux/module.h> 31b530cc79SEric Van Hensbergen #include <linux/net.h> 32b530cc79SEric Van Hensbergen #include <linux/ipv6.h> 33b530cc79SEric Van Hensbergen #include <linux/errno.h> 34b530cc79SEric Van Hensbergen #include <linux/kernel.h> 35b530cc79SEric Van Hensbergen #include <linux/un.h> 36b530cc79SEric Van Hensbergen #include <linux/uaccess.h> 37b530cc79SEric Van Hensbergen #include <linux/inet.h> 38b530cc79SEric Van Hensbergen #include <linux/idr.h> 39b530cc79SEric Van Hensbergen #include <linux/file.h> 405a0e3ad6STejun Heo #include <linux/slab.h> 41b530cc79SEric Van Hensbergen #include <net/9p/9p.h> 42b530cc79SEric Van Hensbergen #include <linux/parser.h> 438b81ef58SEric Van Hensbergen #include <net/9p/client.h> 44b530cc79SEric Van Hensbergen #include <net/9p/transport.h> 45b530cc79SEric Van Hensbergen #include <linux/scatterlist.h> 46b530cc79SEric Van Hensbergen #include <linux/virtio.h> 47b530cc79SEric Van Hensbergen #include <linux/virtio_9p.h> 484038866dSVenkateswararao Jujjuri (JV) #include "trans_common.h" 49b530cc79SEric Van Hensbergen 50e2735b77SEric Van Hensbergen #define VIRTQUEUE_NUM 128 51e2735b77SEric Van Hensbergen 52b530cc79SEric Van Hensbergen /* a single mutex to manage channel initialization and attachment */ 53c1549497SJosef 'Jeff' Sipek static DEFINE_MUTEX(virtio_9p_lock); 54b530cc79SEric Van Hensbergen 55ee443996SEric Van Hensbergen /** 56ee443996SEric Van Hensbergen * struct virtio_chan - per-instance transport information 57ee443996SEric Van Hensbergen * @initialized: whether the channel is initialized 58ee443996SEric Van Hensbergen * @inuse: whether the channel is in use 59ee443996SEric Van Hensbergen * @lock: protects multiple elements within this structure 600e15597eSAbhishek Kulkarni * @client: client instance 61ee443996SEric Van Hensbergen * @vdev: virtio dev associated with this channel 62ee443996SEric Van Hensbergen * @vq: virtio queue associated with this channel 63ee443996SEric Van Hensbergen * @sg: scatter gather list which is used to pack a request (protected?) 64ee443996SEric Van Hensbergen * 65ee443996SEric Van Hensbergen * We keep all per-channel information in a structure. 66b530cc79SEric Van Hensbergen * This structure is allocated within the devices dev->mem space. 67b530cc79SEric Van Hensbergen * A pointer to the structure will get put in the transport private. 68ee443996SEric Van Hensbergen * 69b530cc79SEric Van Hensbergen */ 70ee443996SEric Van Hensbergen 7137c1209dSAneesh Kumar K.V struct virtio_chan { 72ee443996SEric Van Hensbergen bool inuse; 73b530cc79SEric Van Hensbergen 74e2735b77SEric Van Hensbergen spinlock_t lock; 75e2735b77SEric Van Hensbergen 76fea511a6SEric Van Hensbergen struct p9_client *client; 77b530cc79SEric Van Hensbergen struct virtio_device *vdev; 78e2735b77SEric Van Hensbergen struct virtqueue *vq; 7952f44e0dSVenkateswararao Jujjuri (JV) int ring_bufs_avail; 8052f44e0dSVenkateswararao Jujjuri (JV) wait_queue_head_t *vc_wq; 81b530cc79SEric Van Hensbergen 82e2735b77SEric Van Hensbergen /* Scatterlist: can be too big for stack. */ 83e2735b77SEric Van Hensbergen struct scatterlist sg[VIRTQUEUE_NUM]; 8437c1209dSAneesh Kumar K.V 8597ee9b02SAneesh Kumar K.V int tag_len; 8697ee9b02SAneesh Kumar K.V /* 8797ee9b02SAneesh Kumar K.V * tag name to identify a mount Non-null terminated 8897ee9b02SAneesh Kumar K.V */ 8997ee9b02SAneesh Kumar K.V char *tag; 9097ee9b02SAneesh Kumar K.V 9137c1209dSAneesh Kumar K.V struct list_head chan_list; 9237c1209dSAneesh Kumar K.V }; 9337c1209dSAneesh Kumar K.V 9437c1209dSAneesh Kumar K.V static struct list_head virtio_chan_list; 95b530cc79SEric Van Hensbergen 96b530cc79SEric Van Hensbergen /* How many bytes left in this page. */ 97b530cc79SEric Van Hensbergen static unsigned int rest_of_page(void *data) 98b530cc79SEric Van Hensbergen { 99b530cc79SEric Van Hensbergen return PAGE_SIZE - ((unsigned long)data % PAGE_SIZE); 100b530cc79SEric Van Hensbergen } 101b530cc79SEric Van Hensbergen 102ee443996SEric Van Hensbergen /** 103ee443996SEric Van Hensbergen * p9_virtio_close - reclaim resources of a channel 1040e15597eSAbhishek Kulkarni * @client: client instance 105ee443996SEric Van Hensbergen * 106ee443996SEric Van Hensbergen * This reclaims a channel by freeing its resources and 107ee443996SEric Van Hensbergen * reseting its inuse flag. 108ee443996SEric Van Hensbergen * 109ee443996SEric Van Hensbergen */ 110ee443996SEric Van Hensbergen 1118b81ef58SEric Van Hensbergen static void p9_virtio_close(struct p9_client *client) 112b530cc79SEric Van Hensbergen { 1138b81ef58SEric Van Hensbergen struct virtio_chan *chan = client->trans; 114b530cc79SEric Van Hensbergen 115c1549497SJosef 'Jeff' Sipek mutex_lock(&virtio_9p_lock); 116fb786100SAneesh Kumar K.V if (chan) 117b530cc79SEric Van Hensbergen chan->inuse = false; 118c1549497SJosef 'Jeff' Sipek mutex_unlock(&virtio_9p_lock); 119b530cc79SEric Van Hensbergen } 120b530cc79SEric Van Hensbergen 121ee443996SEric Van Hensbergen /** 122ee443996SEric Van Hensbergen * req_done - callback which signals activity from the server 123ee443996SEric Van Hensbergen * @vq: virtio queue activity was received on 124ee443996SEric Van Hensbergen * 125ee443996SEric Van Hensbergen * This notifies us that the server has triggered some activity 126ee443996SEric Van Hensbergen * on the virtio channel - most likely a response to request we 127ee443996SEric Van Hensbergen * sent. Figure out which requests now have responses and wake up 128ee443996SEric Van Hensbergen * those threads. 129ee443996SEric Van Hensbergen * 130ee443996SEric Van Hensbergen * Bugs: could do with some additional sanity checking, but appears to work. 131ee443996SEric Van Hensbergen * 132ee443996SEric Van Hensbergen */ 133ee443996SEric Van Hensbergen 134e2735b77SEric Van Hensbergen static void req_done(struct virtqueue *vq) 135b530cc79SEric Van Hensbergen { 136e2735b77SEric Van Hensbergen struct virtio_chan *chan = vq->vdev->priv; 137e2735b77SEric Van Hensbergen struct p9_fcall *rc; 138e2735b77SEric Van Hensbergen unsigned int len; 139e2735b77SEric Van Hensbergen struct p9_req_t *req; 140419b3956SVenkateswararao Jujjuri (JV) unsigned long flags; 141b530cc79SEric Van Hensbergen 14291b8534fSEric Van Hensbergen P9_DPRINTK(P9_DEBUG_TRANS, ": request done\n"); 14391b8534fSEric Van Hensbergen 144a01a9840SVenkateswararao Jujjuri (JV) while (1) { 145419b3956SVenkateswararao Jujjuri (JV) spin_lock_irqsave(&chan->lock, flags); 146419b3956SVenkateswararao Jujjuri (JV) rc = virtqueue_get_buf(chan->vq, &len); 147419b3956SVenkateswararao Jujjuri (JV) 148a01a9840SVenkateswararao Jujjuri (JV) if (rc == NULL) { 149a01a9840SVenkateswararao Jujjuri (JV) spin_unlock_irqrestore(&chan->lock, flags); 150a01a9840SVenkateswararao Jujjuri (JV) break; 151a01a9840SVenkateswararao Jujjuri (JV) } 152a01a9840SVenkateswararao Jujjuri (JV) 15352f44e0dSVenkateswararao Jujjuri (JV) chan->ring_bufs_avail = 1; 15452f44e0dSVenkateswararao Jujjuri (JV) spin_unlock_irqrestore(&chan->lock, flags); 15553bda3e5SVenkateswararao Jujjuri (JV) /* Wakeup if anyone waiting for VirtIO ring space. */ 15653bda3e5SVenkateswararao Jujjuri (JV) wake_up(chan->vc_wq); 15791b8534fSEric Van Hensbergen P9_DPRINTK(P9_DEBUG_TRANS, ": rc %p\n", rc); 158a01a9840SVenkateswararao Jujjuri (JV) P9_DPRINTK(P9_DEBUG_TRANS, ": lookup tag %d\n", rc->tag); 159fea511a6SEric Van Hensbergen req = p9_tag_lookup(chan->client, rc->tag); 1604038866dSVenkateswararao Jujjuri (JV) if (req->tc->private) { 1614038866dSVenkateswararao Jujjuri (JV) struct trans_rpage_info *rp = req->tc->private; 1624038866dSVenkateswararao Jujjuri (JV) /*Release pages */ 1634038866dSVenkateswararao Jujjuri (JV) p9_release_req_pages(rp); 1644038866dSVenkateswararao Jujjuri (JV) if (rp->rp_alloc) 1654038866dSVenkateswararao Jujjuri (JV) kfree(rp); 1664038866dSVenkateswararao Jujjuri (JV) req->tc->private = NULL; 1674038866dSVenkateswararao Jujjuri (JV) } 168a01a9840SVenkateswararao Jujjuri (JV) req->status = REQ_STATUS_RCVD; 16991b8534fSEric Van Hensbergen p9_client_cb(chan->client, req); 170e2735b77SEric Van Hensbergen } 171b530cc79SEric Van Hensbergen } 172b530cc79SEric Van Hensbergen 173ee443996SEric Van Hensbergen /** 174ee443996SEric Van Hensbergen * pack_sg_list - pack a scatter gather list from a linear buffer 175ee443996SEric Van Hensbergen * @sg: scatter/gather list to pack into 176ee443996SEric Van Hensbergen * @start: which segment of the sg_list to start at 177ee443996SEric Van Hensbergen * @limit: maximum segment to pack data to 178ee443996SEric Van Hensbergen * @data: data to pack into scatter/gather list 179ee443996SEric Van Hensbergen * @count: amount of data to pack into the scatter/gather list 180ee443996SEric Van Hensbergen * 181ee443996SEric Van Hensbergen * sg_lists have multiple segments of various sizes. This will pack 182ee443996SEric Van Hensbergen * arbitrary data into an existing scatter gather list, segmenting the 183ee443996SEric Van Hensbergen * data as necessary within constraints. 184ee443996SEric Van Hensbergen * 185ee443996SEric Van Hensbergen */ 186ee443996SEric Van Hensbergen 187e2735b77SEric Van Hensbergen static int 188e2735b77SEric Van Hensbergen pack_sg_list(struct scatterlist *sg, int start, int limit, char *data, 189e2735b77SEric Van Hensbergen int count) 190e2735b77SEric Van Hensbergen { 191e2735b77SEric Van Hensbergen int s; 192e2735b77SEric Van Hensbergen int index = start; 193e2735b77SEric Van Hensbergen 194e2735b77SEric Van Hensbergen while (count) { 195e2735b77SEric Van Hensbergen s = rest_of_page(data); 196e2735b77SEric Van Hensbergen if (s > count) 197e2735b77SEric Van Hensbergen s = count; 198e2735b77SEric Van Hensbergen sg_set_buf(&sg[index++], data, s); 199e2735b77SEric Van Hensbergen count -= s; 200e2735b77SEric Van Hensbergen data += s; 201d6584f3aSJulia Lawall BUG_ON(index > limit); 202e2735b77SEric Van Hensbergen } 203e2735b77SEric Van Hensbergen 204e2735b77SEric Van Hensbergen return index-start; 205e2735b77SEric Van Hensbergen } 206e2735b77SEric Van Hensbergen 20791b8534fSEric Van Hensbergen /* We don't currently allow canceling of virtio requests */ 20891b8534fSEric Van Hensbergen static int p9_virtio_cancel(struct p9_client *client, struct p9_req_t *req) 20991b8534fSEric Van Hensbergen { 21091b8534fSEric Van Hensbergen return 1; 21191b8534fSEric Van Hensbergen } 21291b8534fSEric Van Hensbergen 213ee443996SEric Van Hensbergen /** 2144038866dSVenkateswararao Jujjuri (JV) * pack_sg_list_p - Just like pack_sg_list. Instead of taking a buffer, 2154038866dSVenkateswararao Jujjuri (JV) * this takes a list of pages. 2164038866dSVenkateswararao Jujjuri (JV) * @sg: scatter/gather list to pack into 2174038866dSVenkateswararao Jujjuri (JV) * @start: which segment of the sg_list to start at 2184038866dSVenkateswararao Jujjuri (JV) * @pdata_off: Offset into the first page 2194038866dSVenkateswararao Jujjuri (JV) * @**pdata: a list of pages to add into sg. 2204038866dSVenkateswararao Jujjuri (JV) * @count: amount of data to pack into the scatter/gather list 2214038866dSVenkateswararao Jujjuri (JV) */ 2224038866dSVenkateswararao Jujjuri (JV) static int 2234038866dSVenkateswararao Jujjuri (JV) pack_sg_list_p(struct scatterlist *sg, int start, int limit, size_t pdata_off, 2244038866dSVenkateswararao Jujjuri (JV) struct page **pdata, int count) 2254038866dSVenkateswararao Jujjuri (JV) { 2264038866dSVenkateswararao Jujjuri (JV) int s; 2274038866dSVenkateswararao Jujjuri (JV) int i = 0; 2284038866dSVenkateswararao Jujjuri (JV) int index = start; 2294038866dSVenkateswararao Jujjuri (JV) 2304038866dSVenkateswararao Jujjuri (JV) if (pdata_off) { 2314038866dSVenkateswararao Jujjuri (JV) s = min((int)(PAGE_SIZE - pdata_off), count); 2324038866dSVenkateswararao Jujjuri (JV) sg_set_page(&sg[index++], pdata[i++], s, pdata_off); 2334038866dSVenkateswararao Jujjuri (JV) count -= s; 2344038866dSVenkateswararao Jujjuri (JV) } 2354038866dSVenkateswararao Jujjuri (JV) 2364038866dSVenkateswararao Jujjuri (JV) while (count) { 2374038866dSVenkateswararao Jujjuri (JV) BUG_ON(index > limit); 2384038866dSVenkateswararao Jujjuri (JV) s = min((int)PAGE_SIZE, count); 2394038866dSVenkateswararao Jujjuri (JV) sg_set_page(&sg[index++], pdata[i++], s, 0); 2404038866dSVenkateswararao Jujjuri (JV) count -= s; 2414038866dSVenkateswararao Jujjuri (JV) } 2424038866dSVenkateswararao Jujjuri (JV) return index-start; 2434038866dSVenkateswararao Jujjuri (JV) } 2444038866dSVenkateswararao Jujjuri (JV) 2454038866dSVenkateswararao Jujjuri (JV) /** 24691b8534fSEric Van Hensbergen * p9_virtio_request - issue a request 2470e15597eSAbhishek Kulkarni * @client: client instance issuing the request 2480e15597eSAbhishek Kulkarni * @req: request to be issued 249ee443996SEric Van Hensbergen * 250ee443996SEric Van Hensbergen */ 251ee443996SEric Van Hensbergen 252e2735b77SEric Van Hensbergen static int 25391b8534fSEric Van Hensbergen p9_virtio_request(struct p9_client *client, struct p9_req_t *req) 254e2735b77SEric Van Hensbergen { 2554038866dSVenkateswararao Jujjuri (JV) int in, out, inp, outp; 25691b8534fSEric Van Hensbergen struct virtio_chan *chan = client->trans; 25791b8534fSEric Van Hensbergen char *rdata = (char *)req->rc+sizeof(struct p9_fcall); 258419b3956SVenkateswararao Jujjuri (JV) unsigned long flags; 2594038866dSVenkateswararao Jujjuri (JV) size_t pdata_off = 0; 2604038866dSVenkateswararao Jujjuri (JV) struct trans_rpage_info *rpinfo = NULL; 2614038866dSVenkateswararao Jujjuri (JV) int err, pdata_len = 0; 262e2735b77SEric Van Hensbergen 26391b8534fSEric Van Hensbergen P9_DPRINTK(P9_DEBUG_TRANS, "9p debug: virtio request\n"); 264e2735b77SEric Van Hensbergen 265419b3956SVenkateswararao Jujjuri (JV) req->status = REQ_STATUS_SENT; 266419b3956SVenkateswararao Jujjuri (JV) 2674038866dSVenkateswararao Jujjuri (JV) if (req->tc->pbuf_size && (req->tc->pubuf && P9_IS_USER_CONTEXT)) { 2684038866dSVenkateswararao Jujjuri (JV) int nr_pages = p9_nr_pages(req); 2694038866dSVenkateswararao Jujjuri (JV) int rpinfo_size = sizeof(struct trans_rpage_info) + 2704038866dSVenkateswararao Jujjuri (JV) sizeof(struct page *) * nr_pages; 2714038866dSVenkateswararao Jujjuri (JV) 2724038866dSVenkateswararao Jujjuri (JV) if (rpinfo_size <= (req->tc->capacity - req->tc->size)) { 2734038866dSVenkateswararao Jujjuri (JV) /* We can use sdata */ 2744038866dSVenkateswararao Jujjuri (JV) req->tc->private = req->tc->sdata + req->tc->size; 2754038866dSVenkateswararao Jujjuri (JV) rpinfo = (struct trans_rpage_info *)req->tc->private; 2764038866dSVenkateswararao Jujjuri (JV) rpinfo->rp_alloc = 0; 2774038866dSVenkateswararao Jujjuri (JV) } else { 2784038866dSVenkateswararao Jujjuri (JV) req->tc->private = kmalloc(rpinfo_size, GFP_NOFS); 2794038866dSVenkateswararao Jujjuri (JV) if (!req->tc->private) { 2804038866dSVenkateswararao Jujjuri (JV) P9_DPRINTK(P9_DEBUG_TRANS, "9p debug: " 2814038866dSVenkateswararao Jujjuri (JV) "private kmalloc returned NULL"); 2824038866dSVenkateswararao Jujjuri (JV) return -ENOMEM; 2834038866dSVenkateswararao Jujjuri (JV) } 2844038866dSVenkateswararao Jujjuri (JV) rpinfo = (struct trans_rpage_info *)req->tc->private; 2854038866dSVenkateswararao Jujjuri (JV) rpinfo->rp_alloc = 1; 2864038866dSVenkateswararao Jujjuri (JV) } 2874038866dSVenkateswararao Jujjuri (JV) 2884038866dSVenkateswararao Jujjuri (JV) err = p9_payload_gup(req, &pdata_off, &pdata_len, nr_pages, 2894038866dSVenkateswararao Jujjuri (JV) req->tc->id == P9_TREAD ? 1 : 0); 2904038866dSVenkateswararao Jujjuri (JV) if (err < 0) { 2914038866dSVenkateswararao Jujjuri (JV) if (rpinfo->rp_alloc) 2924038866dSVenkateswararao Jujjuri (JV) kfree(rpinfo); 2934038866dSVenkateswararao Jujjuri (JV) return err; 2944038866dSVenkateswararao Jujjuri (JV) } 2954038866dSVenkateswararao Jujjuri (JV) } 2964038866dSVenkateswararao Jujjuri (JV) 297*316ad550SVenkateswararao Jujjuri (JV) req_retry_pinned: 298419b3956SVenkateswararao Jujjuri (JV) spin_lock_irqsave(&chan->lock, flags); 2994038866dSVenkateswararao Jujjuri (JV) 3004038866dSVenkateswararao Jujjuri (JV) /* Handle out VirtIO ring buffers */ 30191b8534fSEric Van Hensbergen out = pack_sg_list(chan->sg, 0, VIRTQUEUE_NUM, req->tc->sdata, 30291b8534fSEric Van Hensbergen req->tc->size); 3034038866dSVenkateswararao Jujjuri (JV) 3044038866dSVenkateswararao Jujjuri (JV) if (req->tc->pbuf_size && (req->tc->id == P9_TWRITE)) { 3054038866dSVenkateswararao Jujjuri (JV) /* We have additional write payload buffer to take care */ 3064038866dSVenkateswararao Jujjuri (JV) if (req->tc->pubuf && P9_IS_USER_CONTEXT) { 3074038866dSVenkateswararao Jujjuri (JV) outp = pack_sg_list_p(chan->sg, out, VIRTQUEUE_NUM, 3084038866dSVenkateswararao Jujjuri (JV) pdata_off, rpinfo->rp_data, pdata_len); 3094038866dSVenkateswararao Jujjuri (JV) } else { 3104038866dSVenkateswararao Jujjuri (JV) char *pbuf = req->tc->pubuf ? req->tc->pubuf : 3114038866dSVenkateswararao Jujjuri (JV) req->tc->pkbuf; 3124038866dSVenkateswararao Jujjuri (JV) outp = pack_sg_list(chan->sg, out, VIRTQUEUE_NUM, pbuf, 3134038866dSVenkateswararao Jujjuri (JV) req->tc->pbuf_size); 3144038866dSVenkateswararao Jujjuri (JV) } 3154038866dSVenkateswararao Jujjuri (JV) out += outp; 3164038866dSVenkateswararao Jujjuri (JV) } 3174038866dSVenkateswararao Jujjuri (JV) 3184038866dSVenkateswararao Jujjuri (JV) /* Handle in VirtIO ring buffers */ 3194038866dSVenkateswararao Jujjuri (JV) if (req->tc->pbuf_size && 3204038866dSVenkateswararao Jujjuri (JV) ((req->tc->id == P9_TREAD) || (req->tc->id == P9_TREADDIR))) { 3214038866dSVenkateswararao Jujjuri (JV) /* 3224038866dSVenkateswararao Jujjuri (JV) * Take care of additional Read payload. 3234038866dSVenkateswararao Jujjuri (JV) * 11 is the read/write header = PDU Header(7) + IO Size (4). 3244038866dSVenkateswararao Jujjuri (JV) * Arrange in such a way that server places header in the 3254038866dSVenkateswararao Jujjuri (JV) * alloced memory and payload onto the user buffer. 3264038866dSVenkateswararao Jujjuri (JV) */ 3274038866dSVenkateswararao Jujjuri (JV) inp = pack_sg_list(chan->sg, out, VIRTQUEUE_NUM, rdata, 11); 3284038866dSVenkateswararao Jujjuri (JV) /* 3294038866dSVenkateswararao Jujjuri (JV) * Running executables in the filesystem may result in 3304038866dSVenkateswararao Jujjuri (JV) * a read request with kernel buffer as opposed to user buffer. 3314038866dSVenkateswararao Jujjuri (JV) */ 3324038866dSVenkateswararao Jujjuri (JV) if (req->tc->pubuf && P9_IS_USER_CONTEXT) { 3334038866dSVenkateswararao Jujjuri (JV) in = pack_sg_list_p(chan->sg, out+inp, VIRTQUEUE_NUM, 3344038866dSVenkateswararao Jujjuri (JV) pdata_off, rpinfo->rp_data, pdata_len); 3354038866dSVenkateswararao Jujjuri (JV) } else { 3364038866dSVenkateswararao Jujjuri (JV) char *pbuf = req->tc->pubuf ? req->tc->pubuf : 3374038866dSVenkateswararao Jujjuri (JV) req->tc->pkbuf; 3384038866dSVenkateswararao Jujjuri (JV) in = pack_sg_list(chan->sg, out+inp, VIRTQUEUE_NUM, 3394038866dSVenkateswararao Jujjuri (JV) pbuf, req->tc->pbuf_size); 3404038866dSVenkateswararao Jujjuri (JV) } 3414038866dSVenkateswararao Jujjuri (JV) in += inp; 3424038866dSVenkateswararao Jujjuri (JV) } else { 3434038866dSVenkateswararao Jujjuri (JV) in = pack_sg_list(chan->sg, out, VIRTQUEUE_NUM, rdata, 34491b8534fSEric Van Hensbergen client->msize); 3454038866dSVenkateswararao Jujjuri (JV) } 346e2735b77SEric Van Hensbergen 347419b3956SVenkateswararao Jujjuri (JV) err = virtqueue_add_buf(chan->vq, chan->sg, out, in, req->tc); 348419b3956SVenkateswararao Jujjuri (JV) if (err < 0) { 34952f44e0dSVenkateswararao Jujjuri (JV) if (err == -ENOSPC) { 35052f44e0dSVenkateswararao Jujjuri (JV) chan->ring_bufs_avail = 0; 35152f44e0dSVenkateswararao Jujjuri (JV) spin_unlock_irqrestore(&chan->lock, flags); 35252f44e0dSVenkateswararao Jujjuri (JV) err = wait_event_interruptible(*chan->vc_wq, 35352f44e0dSVenkateswararao Jujjuri (JV) chan->ring_bufs_avail); 35452f44e0dSVenkateswararao Jujjuri (JV) if (err == -ERESTARTSYS) 35552f44e0dSVenkateswararao Jujjuri (JV) return err; 35652f44e0dSVenkateswararao Jujjuri (JV) 35752f44e0dSVenkateswararao Jujjuri (JV) P9_DPRINTK(P9_DEBUG_TRANS, "9p:Retry virtio request\n"); 358*316ad550SVenkateswararao Jujjuri (JV) goto req_retry_pinned; 35952f44e0dSVenkateswararao Jujjuri (JV) } else { 360419b3956SVenkateswararao Jujjuri (JV) spin_unlock_irqrestore(&chan->lock, flags); 361e2735b77SEric Van Hensbergen P9_DPRINTK(P9_DEBUG_TRANS, 36252f44e0dSVenkateswararao Jujjuri (JV) "9p debug: " 36352f44e0dSVenkateswararao Jujjuri (JV) "virtio rpc add_buf returned failure"); 3644038866dSVenkateswararao Jujjuri (JV) if (rpinfo && rpinfo->rp_alloc) 3654038866dSVenkateswararao Jujjuri (JV) kfree(rpinfo); 366e2735b77SEric Van Hensbergen return -EIO; 367e2735b77SEric Van Hensbergen } 36852f44e0dSVenkateswararao Jujjuri (JV) } 369e2735b77SEric Van Hensbergen 370dc3f5e68SMichael S. Tsirkin virtqueue_kick(chan->vq); 371419b3956SVenkateswararao Jujjuri (JV) spin_unlock_irqrestore(&chan->lock, flags); 372e2735b77SEric Van Hensbergen 37391b8534fSEric Van Hensbergen P9_DPRINTK(P9_DEBUG_TRANS, "9p debug: virtio request kicked\n"); 374e2735b77SEric Van Hensbergen return 0; 375e2735b77SEric Van Hensbergen } 376e2735b77SEric Van Hensbergen 37786c84373SAneesh Kumar K.V static ssize_t p9_mount_tag_show(struct device *dev, 37886c84373SAneesh Kumar K.V struct device_attribute *attr, char *buf) 37986c84373SAneesh Kumar K.V { 38086c84373SAneesh Kumar K.V struct virtio_chan *chan; 38186c84373SAneesh Kumar K.V struct virtio_device *vdev; 38286c84373SAneesh Kumar K.V 38386c84373SAneesh Kumar K.V vdev = dev_to_virtio(dev); 38486c84373SAneesh Kumar K.V chan = vdev->priv; 38586c84373SAneesh Kumar K.V 38686c84373SAneesh Kumar K.V return snprintf(buf, chan->tag_len + 1, "%s", chan->tag); 38786c84373SAneesh Kumar K.V } 38886c84373SAneesh Kumar K.V 38986c84373SAneesh Kumar K.V static DEVICE_ATTR(mount_tag, 0444, p9_mount_tag_show, NULL); 39086c84373SAneesh Kumar K.V 391ee443996SEric Van Hensbergen /** 392ee443996SEric Van Hensbergen * p9_virtio_probe - probe for existence of 9P virtio channels 393ee443996SEric Van Hensbergen * @vdev: virtio device to probe 394ee443996SEric Van Hensbergen * 39537c1209dSAneesh Kumar K.V * This probes for existing virtio channels. 396ee443996SEric Van Hensbergen * 397ee443996SEric Van Hensbergen */ 398ee443996SEric Van Hensbergen 399e2735b77SEric Van Hensbergen static int p9_virtio_probe(struct virtio_device *vdev) 400b530cc79SEric Van Hensbergen { 40197ee9b02SAneesh Kumar K.V __u16 tag_len; 40297ee9b02SAneesh Kumar K.V char *tag; 403b530cc79SEric Van Hensbergen int err; 404b530cc79SEric Van Hensbergen struct virtio_chan *chan; 405b530cc79SEric Van Hensbergen 40637c1209dSAneesh Kumar K.V chan = kmalloc(sizeof(struct virtio_chan), GFP_KERNEL); 40737c1209dSAneesh Kumar K.V if (!chan) { 40837c1209dSAneesh Kumar K.V printk(KERN_ERR "9p: Failed to allocate virtio 9P channel\n"); 409b530cc79SEric Van Hensbergen err = -ENOMEM; 410b530cc79SEric Van Hensbergen goto fail; 411b530cc79SEric Van Hensbergen } 412b530cc79SEric Van Hensbergen 413e2735b77SEric Van Hensbergen chan->vdev = vdev; 414e2735b77SEric Van Hensbergen 415e2735b77SEric Van Hensbergen /* We expect one virtqueue, for requests. */ 416d2a7dddaSMichael S. Tsirkin chan->vq = virtio_find_single_vq(vdev, req_done, "requests"); 417e2735b77SEric Van Hensbergen if (IS_ERR(chan->vq)) { 418e2735b77SEric Van Hensbergen err = PTR_ERR(chan->vq); 419e2735b77SEric Van Hensbergen goto out_free_vq; 420b530cc79SEric Van Hensbergen } 421e2735b77SEric Van Hensbergen chan->vq->vdev->priv = chan; 422e2735b77SEric Van Hensbergen spin_lock_init(&chan->lock); 423b530cc79SEric Van Hensbergen 424e2735b77SEric Van Hensbergen sg_init_table(chan->sg, VIRTQUEUE_NUM); 425b530cc79SEric Van Hensbergen 426b530cc79SEric Van Hensbergen chan->inuse = false; 42797ee9b02SAneesh Kumar K.V if (virtio_has_feature(vdev, VIRTIO_9P_MOUNT_TAG)) { 42897ee9b02SAneesh Kumar K.V vdev->config->get(vdev, 42997ee9b02SAneesh Kumar K.V offsetof(struct virtio_9p_config, tag_len), 43097ee9b02SAneesh Kumar K.V &tag_len, sizeof(tag_len)); 43197ee9b02SAneesh Kumar K.V } else { 43297ee9b02SAneesh Kumar K.V err = -EINVAL; 43397ee9b02SAneesh Kumar K.V goto out_free_vq; 43497ee9b02SAneesh Kumar K.V } 43597ee9b02SAneesh Kumar K.V tag = kmalloc(tag_len, GFP_KERNEL); 43697ee9b02SAneesh Kumar K.V if (!tag) { 43797ee9b02SAneesh Kumar K.V err = -ENOMEM; 43897ee9b02SAneesh Kumar K.V goto out_free_vq; 43997ee9b02SAneesh Kumar K.V } 44097ee9b02SAneesh Kumar K.V vdev->config->get(vdev, offsetof(struct virtio_9p_config, tag), 44197ee9b02SAneesh Kumar K.V tag, tag_len); 44297ee9b02SAneesh Kumar K.V chan->tag = tag; 44397ee9b02SAneesh Kumar K.V chan->tag_len = tag_len; 44486c84373SAneesh Kumar K.V err = sysfs_create_file(&(vdev->dev.kobj), &dev_attr_mount_tag.attr); 44586c84373SAneesh Kumar K.V if (err) { 44652f44e0dSVenkateswararao Jujjuri (JV) goto out_free_tag; 44786c84373SAneesh Kumar K.V } 44852f44e0dSVenkateswararao Jujjuri (JV) chan->vc_wq = kmalloc(sizeof(wait_queue_head_t), GFP_KERNEL); 44952f44e0dSVenkateswararao Jujjuri (JV) if (!chan->vc_wq) { 45052f44e0dSVenkateswararao Jujjuri (JV) err = -ENOMEM; 45152f44e0dSVenkateswararao Jujjuri (JV) goto out_free_tag; 45252f44e0dSVenkateswararao Jujjuri (JV) } 45352f44e0dSVenkateswararao Jujjuri (JV) init_waitqueue_head(chan->vc_wq); 45452f44e0dSVenkateswararao Jujjuri (JV) chan->ring_bufs_avail = 1; 45552f44e0dSVenkateswararao Jujjuri (JV) 45637c1209dSAneesh Kumar K.V mutex_lock(&virtio_9p_lock); 45737c1209dSAneesh Kumar K.V list_add_tail(&chan->chan_list, &virtio_chan_list); 45837c1209dSAneesh Kumar K.V mutex_unlock(&virtio_9p_lock); 459b530cc79SEric Van Hensbergen return 0; 460b530cc79SEric Van Hensbergen 46152f44e0dSVenkateswararao Jujjuri (JV) out_free_tag: 46252f44e0dSVenkateswararao Jujjuri (JV) kfree(tag); 463e2735b77SEric Van Hensbergen out_free_vq: 464d2a7dddaSMichael S. Tsirkin vdev->config->del_vqs(vdev); 46537c1209dSAneesh Kumar K.V kfree(chan); 466b530cc79SEric Van Hensbergen fail: 467b530cc79SEric Van Hensbergen return err; 468b530cc79SEric Van Hensbergen } 469b530cc79SEric Van Hensbergen 470ee443996SEric Van Hensbergen 471ee443996SEric Van Hensbergen /** 472ee443996SEric Van Hensbergen * p9_virtio_create - allocate a new virtio channel 4738b81ef58SEric Van Hensbergen * @client: client instance invoking this transport 474ee443996SEric Van Hensbergen * @devname: string identifying the channel to connect to (unused) 475ee443996SEric Van Hensbergen * @args: args passed from sys_mount() for per-transport options (unused) 476ee443996SEric Van Hensbergen * 477ee443996SEric Van Hensbergen * This sets up a transport channel for 9p communication. Right now 478b530cc79SEric Van Hensbergen * we only match the first available channel, but eventually we couldlook up 479b530cc79SEric Van Hensbergen * alternate channels by matching devname versus a virtio_config entry. 480b530cc79SEric Van Hensbergen * We use a simple reference count mechanism to ensure that only a single 481ee443996SEric Van Hensbergen * mount has a channel open at a time. 482ee443996SEric Van Hensbergen * 483ee443996SEric Van Hensbergen */ 484ee443996SEric Van Hensbergen 4858b81ef58SEric Van Hensbergen static int 4868b81ef58SEric Van Hensbergen p9_virtio_create(struct p9_client *client, const char *devname, char *args) 487b530cc79SEric Van Hensbergen { 48837c1209dSAneesh Kumar K.V struct virtio_chan *chan; 489c1a7c226SAneesh Kumar K.V int ret = -ENOENT; 49037c1209dSAneesh Kumar K.V int found = 0; 491b530cc79SEric Van Hensbergen 492c1549497SJosef 'Jeff' Sipek mutex_lock(&virtio_9p_lock); 49337c1209dSAneesh Kumar K.V list_for_each_entry(chan, &virtio_chan_list, chan_list) { 4940b20406cSSven Eckelmann if (!strncmp(devname, chan->tag, chan->tag_len) && 4950b20406cSSven Eckelmann strlen(devname) == chan->tag_len) { 496f75580c4SAneesh Kumar K.V if (!chan->inuse) { 497b530cc79SEric Van Hensbergen chan->inuse = true; 49837c1209dSAneesh Kumar K.V found = 1; 499b530cc79SEric Van Hensbergen break; 500f75580c4SAneesh Kumar K.V } 501c1a7c226SAneesh Kumar K.V ret = -EBUSY; 502f75580c4SAneesh Kumar K.V } 503b530cc79SEric Van Hensbergen } 504c1549497SJosef 'Jeff' Sipek mutex_unlock(&virtio_9p_lock); 505b530cc79SEric Van Hensbergen 50637c1209dSAneesh Kumar K.V if (!found) { 507e2735b77SEric Van Hensbergen printk(KERN_ERR "9p: no channels available\n"); 508c1a7c226SAneesh Kumar K.V return ret; 509b530cc79SEric Van Hensbergen } 510b530cc79SEric Van Hensbergen 5118b81ef58SEric Van Hensbergen client->trans = (void *)chan; 512562ada61SEric Van Hensbergen client->status = Connected; 513fea511a6SEric Van Hensbergen chan->client = client; 514b530cc79SEric Van Hensbergen 5158b81ef58SEric Van Hensbergen return 0; 516b530cc79SEric Van Hensbergen } 517b530cc79SEric Van Hensbergen 518ee443996SEric Van Hensbergen /** 519ee443996SEric Van Hensbergen * p9_virtio_remove - clean up resources associated with a virtio device 520ee443996SEric Van Hensbergen * @vdev: virtio device to remove 521ee443996SEric Van Hensbergen * 522ee443996SEric Van Hensbergen */ 523ee443996SEric Van Hensbergen 524f3933545SEric Van Hensbergen static void p9_virtio_remove(struct virtio_device *vdev) 525f3933545SEric Van Hensbergen { 526f3933545SEric Van Hensbergen struct virtio_chan *chan = vdev->priv; 527f3933545SEric Van Hensbergen 528f3933545SEric Van Hensbergen BUG_ON(chan->inuse); 529d2a7dddaSMichael S. Tsirkin vdev->config->del_vqs(vdev); 53037c1209dSAneesh Kumar K.V 53137c1209dSAneesh Kumar K.V mutex_lock(&virtio_9p_lock); 53237c1209dSAneesh Kumar K.V list_del(&chan->chan_list); 53337c1209dSAneesh Kumar K.V mutex_unlock(&virtio_9p_lock); 53486c84373SAneesh Kumar K.V sysfs_remove_file(&(vdev->dev.kobj), &dev_attr_mount_tag.attr); 53597ee9b02SAneesh Kumar K.V kfree(chan->tag); 53652f44e0dSVenkateswararao Jujjuri (JV) kfree(chan->vc_wq); 53737c1209dSAneesh Kumar K.V kfree(chan); 53837c1209dSAneesh Kumar K.V 539f3933545SEric Van Hensbergen } 540f3933545SEric Van Hensbergen 541b530cc79SEric Van Hensbergen static struct virtio_device_id id_table[] = { 542b530cc79SEric Van Hensbergen { VIRTIO_ID_9P, VIRTIO_DEV_ANY_ID }, 543b530cc79SEric Van Hensbergen { 0 }, 544b530cc79SEric Van Hensbergen }; 545b530cc79SEric Van Hensbergen 54697ee9b02SAneesh Kumar K.V static unsigned int features[] = { 54797ee9b02SAneesh Kumar K.V VIRTIO_9P_MOUNT_TAG, 54897ee9b02SAneesh Kumar K.V }; 54997ee9b02SAneesh Kumar K.V 550b530cc79SEric Van Hensbergen /* The standard "struct lguest_driver": */ 551b530cc79SEric Van Hensbergen static struct virtio_driver p9_virtio_drv = { 55297ee9b02SAneesh Kumar K.V .feature_table = features, 55397ee9b02SAneesh Kumar K.V .feature_table_size = ARRAY_SIZE(features), 554b530cc79SEric Van Hensbergen .driver.name = KBUILD_MODNAME, 555b530cc79SEric Van Hensbergen .driver.owner = THIS_MODULE, 556b530cc79SEric Van Hensbergen .id_table = id_table, 557b530cc79SEric Van Hensbergen .probe = p9_virtio_probe, 558f3933545SEric Van Hensbergen .remove = p9_virtio_remove, 559b530cc79SEric Van Hensbergen }; 560b530cc79SEric Van Hensbergen 561b530cc79SEric Van Hensbergen static struct p9_trans_module p9_virtio_trans = { 562b530cc79SEric Van Hensbergen .name = "virtio", 563b530cc79SEric Van Hensbergen .create = p9_virtio_create, 5648b81ef58SEric Van Hensbergen .close = p9_virtio_close, 56591b8534fSEric Van Hensbergen .request = p9_virtio_request, 56691b8534fSEric Van Hensbergen .cancel = p9_virtio_cancel, 567e2735b77SEric Van Hensbergen .maxsize = PAGE_SIZE*16, 5686f69c395SVenkateswararao Jujjuri (JV) .pref = P9_TRANS_PREF_PAYLOAD_SEP, 569b530cc79SEric Van Hensbergen .def = 0, 57072029fe8STejun Heo .owner = THIS_MODULE, 571b530cc79SEric Van Hensbergen }; 572b530cc79SEric Van Hensbergen 573b530cc79SEric Van Hensbergen /* The standard init function */ 574b530cc79SEric Van Hensbergen static int __init p9_virtio_init(void) 575b530cc79SEric Van Hensbergen { 57637c1209dSAneesh Kumar K.V INIT_LIST_HEAD(&virtio_chan_list); 577b530cc79SEric Van Hensbergen 578b530cc79SEric Van Hensbergen v9fs_register_trans(&p9_virtio_trans); 579b530cc79SEric Van Hensbergen return register_virtio_driver(&p9_virtio_drv); 580b530cc79SEric Van Hensbergen } 581b530cc79SEric Van Hensbergen 582f3933545SEric Van Hensbergen static void __exit p9_virtio_cleanup(void) 583f3933545SEric Van Hensbergen { 584f3933545SEric Van Hensbergen unregister_virtio_driver(&p9_virtio_drv); 58572029fe8STejun Heo v9fs_unregister_trans(&p9_virtio_trans); 586f3933545SEric Van Hensbergen } 587f3933545SEric Van Hensbergen 588b530cc79SEric Van Hensbergen module_init(p9_virtio_init); 589f3933545SEric Van Hensbergen module_exit(p9_virtio_cleanup); 590b530cc79SEric Van Hensbergen 591b530cc79SEric Van Hensbergen MODULE_DEVICE_TABLE(virtio, id_table); 592b530cc79SEric Van Hensbergen MODULE_AUTHOR("Eric Van Hensbergen <ericvh@gmail.com>"); 593b530cc79SEric Van Hensbergen MODULE_DESCRIPTION("Virtio 9p Transport"); 594b530cc79SEric Van Hensbergen MODULE_LICENSE("GPL"); 595