aboutsummaryrefslogtreecommitdiffstats
path: root/net/9p/trans_virtio.c
diff options
context:
space:
mode:
Diffstat (limited to 'net/9p/trans_virtio.c')
-rw-r--r--net/9p/trans_virtio.c175
1 files changed, 155 insertions, 20 deletions
diff --git a/net/9p/trans_virtio.c b/net/9p/trans_virtio.c
index de7a9f532edc..42adc052b149 100644
--- a/net/9p/trans_virtio.c
+++ b/net/9p/trans_virtio.c
@@ -49,29 +49,75 @@
49#define VIRTQUEUE_NUM 128 49#define VIRTQUEUE_NUM 128
50 50
51/* a single mutex to manage channel initialization and attachment */ 51/* a single mutex to manage channel initialization and attachment */
52static DECLARE_MUTEX(virtio_9p_lock); 52static DEFINE_MUTEX(virtio_9p_lock);
53/* global which tracks highest initialized channel */ 53/* global which tracks highest initialized channel */
54static int chan_index; 54static int chan_index;
55 55
56#define P9_INIT_MAXTAG 16 56#define P9_INIT_MAXTAG 16
57 57
58#define REQ_STATUS_IDLE 0 58
59#define REQ_STATUS_SENT 1 59/**
60#define REQ_STATUS_RCVD 2 60 * enum p9_req_status_t - virtio request status
61#define REQ_STATUS_FLSH 3 61 * @REQ_STATUS_IDLE: request slot unused
62 * @REQ_STATUS_SENT: request sent to server
63 * @REQ_STATUS_RCVD: response received from server
64 * @REQ_STATUS_FLSH: request has been flushed
65 *
66 * The @REQ_STATUS_IDLE state is used to mark a request slot as unused
67 * but use is actually tracked by the idpool structure which handles tag
68 * id allocation.
69 *
70 */
71
72enum p9_req_status_t {
73 REQ_STATUS_IDLE,
74 REQ_STATUS_SENT,
75 REQ_STATUS_RCVD,
76 REQ_STATUS_FLSH,
77};
78
79/**
80 * struct p9_req_t - virtio request slots
81 * @status: status of this request slot
82 * @wq: wait_queue for the client to block on for this request
83 *
84 * The virtio transport uses an array to track outstanding requests
85 * instead of a list. While this may incurr overhead during initial
86 * allocation or expansion, it makes request lookup much easier as the
87 * tag id is a index into an array. (We use tag+1 so that we can accomodate
88 * the -1 tag for the T_VERSION request).
89 * This also has the nice effect of only having to allocate wait_queues
90 * once, instead of constantly allocating and freeing them. Its possible
91 * other resources could benefit from this scheme as well.
92 *
93 */
62 94
63struct p9_req_t { 95struct p9_req_t {
64 int status; 96 int status;
65 wait_queue_head_t *wq; 97 wait_queue_head_t *wq;
66}; 98};
67 99
68/* We keep all per-channel information in a structure. 100/**
101 * struct virtio_chan - per-instance transport information
102 * @initialized: whether the channel is initialized
103 * @inuse: whether the channel is in use
104 * @lock: protects multiple elements within this structure
105 * @vdev: virtio dev associated with this channel
106 * @vq: virtio queue associated with this channel
107 * @tagpool: accounting for tag ids (and request slots)
108 * @reqs: array of request slots
109 * @max_tag: current number of request_slots allocated
110 * @sg: scatter gather list which is used to pack a request (protected?)
111 *
112 * We keep all per-channel information in a structure.
69 * This structure is allocated within the devices dev->mem space. 113 * This structure is allocated within the devices dev->mem space.
70 * A pointer to the structure will get put in the transport private. 114 * A pointer to the structure will get put in the transport private.
115 *
71 */ 116 */
117
72static struct virtio_chan { 118static struct virtio_chan {
73 bool initialized; /* channel is initialized */ 119 bool initialized;
74 bool inuse; /* channel is in use */ 120 bool inuse;
75 121
76 spinlock_t lock; 122 spinlock_t lock;
77 123
@@ -86,7 +132,19 @@ static struct virtio_chan {
86 struct scatterlist sg[VIRTQUEUE_NUM]; 132 struct scatterlist sg[VIRTQUEUE_NUM];
87} channels[MAX_9P_CHAN]; 133} channels[MAX_9P_CHAN];
88 134
89/* Lookup requests by tag */ 135/**
136 * p9_lookup_tag - Lookup requests by tag
137 * @c: virtio channel to lookup tag within
138 * @tag: numeric id for transaction
139 *
140 * this is a simple array lookup, but will grow the
141 * request_slots as necessary to accomodate transaction
142 * ids which did not previously have a slot.
143 *
144 * Bugs: there is currently no upper limit on request slots set
145 * here, but that should be constrained by the id accounting.
146 */
147
90static struct p9_req_t *p9_lookup_tag(struct virtio_chan *c, u16 tag) 148static struct p9_req_t *p9_lookup_tag(struct virtio_chan *c, u16 tag)
91{ 149{
92 /* This looks up the original request by tag so we know which 150 /* This looks up the original request by tag so we know which
@@ -130,11 +188,20 @@ static unsigned int rest_of_page(void *data)
130 return PAGE_SIZE - ((unsigned long)data % PAGE_SIZE); 188 return PAGE_SIZE - ((unsigned long)data % PAGE_SIZE);
131} 189}
132 190
191/**
192 * p9_virtio_close - reclaim resources of a channel
193 * @trans: transport state
194 *
195 * This reclaims a channel by freeing its resources and
196 * reseting its inuse flag.
197 *
198 */
199
133static void p9_virtio_close(struct p9_trans *trans) 200static void p9_virtio_close(struct p9_trans *trans)
134{ 201{
135 struct virtio_chan *chan = trans->priv; 202 struct virtio_chan *chan = trans->priv;
136 int count; 203 int count;
137 unsigned int flags; 204 unsigned long flags;
138 205
139 spin_lock_irqsave(&chan->lock, flags); 206 spin_lock_irqsave(&chan->lock, flags);
140 p9_idpool_destroy(chan->tagpool); 207 p9_idpool_destroy(chan->tagpool);
@@ -144,13 +211,26 @@ static void p9_virtio_close(struct p9_trans *trans)
144 chan->max_tag = 0; 211 chan->max_tag = 0;
145 spin_unlock_irqrestore(&chan->lock, flags); 212 spin_unlock_irqrestore(&chan->lock, flags);
146 213
147 down(&virtio_9p_lock); 214 mutex_lock(&virtio_9p_lock);
148 chan->inuse = false; 215 chan->inuse = false;
149 up(&virtio_9p_lock); 216 mutex_unlock(&virtio_9p_lock);
150 217
151 kfree(trans); 218 kfree(trans);
152} 219}
153 220
221/**
222 * req_done - callback which signals activity from the server
223 * @vq: virtio queue activity was received on
224 *
225 * This notifies us that the server has triggered some activity
226 * on the virtio channel - most likely a response to request we
227 * sent. Figure out which requests now have responses and wake up
228 * those threads.
229 *
230 * Bugs: could do with some additional sanity checking, but appears to work.
231 *
232 */
233
154static void req_done(struct virtqueue *vq) 234static void req_done(struct virtqueue *vq)
155{ 235{
156 struct virtio_chan *chan = vq->vdev->priv; 236 struct virtio_chan *chan = vq->vdev->priv;
@@ -169,6 +249,20 @@ static void req_done(struct virtqueue *vq)
169 spin_unlock_irqrestore(&chan->lock, flags); 249 spin_unlock_irqrestore(&chan->lock, flags);
170} 250}
171 251
252/**
253 * pack_sg_list - pack a scatter gather list from a linear buffer
254 * @sg: scatter/gather list to pack into
255 * @start: which segment of the sg_list to start at
256 * @limit: maximum segment to pack data to
257 * @data: data to pack into scatter/gather list
258 * @count: amount of data to pack into the scatter/gather list
259 *
260 * sg_lists have multiple segments of various sizes. This will pack
261 * arbitrary data into an existing scatter gather list, segmenting the
262 * data as necessary within constraints.
263 *
264 */
265
172static int 266static int
173pack_sg_list(struct scatterlist *sg, int start, int limit, char *data, 267pack_sg_list(struct scatterlist *sg, int start, int limit, char *data,
174 int count) 268 int count)
@@ -189,6 +283,14 @@ pack_sg_list(struct scatterlist *sg, int start, int limit, char *data,
189 return index-start; 283 return index-start;
190} 284}
191 285
286/**
287 * p9_virtio_rpc - issue a request and wait for a response
288 * @t: transport state
289 * @tc: &p9_fcall request to transmit
290 * @rc: &p9_fcall to put reponse into
291 *
292 */
293
192static int 294static int
193p9_virtio_rpc(struct p9_trans *t, struct p9_fcall *tc, struct p9_fcall **rc) 295p9_virtio_rpc(struct p9_trans *t, struct p9_fcall *tc, struct p9_fcall **rc)
194{ 296{
@@ -263,16 +365,26 @@ p9_virtio_rpc(struct p9_trans *t, struct p9_fcall *tc, struct p9_fcall **rc)
263 return 0; 365 return 0;
264} 366}
265 367
368/**
369 * p9_virtio_probe - probe for existence of 9P virtio channels
370 * @vdev: virtio device to probe
371 *
372 * This probes for existing virtio channels. At present only
373 * a single channel is in use, so in the future more work may need
374 * to be done here.
375 *
376 */
377
266static int p9_virtio_probe(struct virtio_device *vdev) 378static int p9_virtio_probe(struct virtio_device *vdev)
267{ 379{
268 int err; 380 int err;
269 struct virtio_chan *chan; 381 struct virtio_chan *chan;
270 int index; 382 int index;
271 383
272 down(&virtio_9p_lock); 384 mutex_lock(&virtio_9p_lock);
273 index = chan_index++; 385 index = chan_index++;
274 chan = &channels[index]; 386 chan = &channels[index];
275 up(&virtio_9p_lock); 387 mutex_unlock(&virtio_9p_lock);
276 388
277 if (chan_index > MAX_9P_CHAN) { 389 if (chan_index > MAX_9P_CHAN) {
278 printk(KERN_ERR "9p: virtio: Maximum channels exceeded\n"); 390 printk(KERN_ERR "9p: virtio: Maximum channels exceeded\n");
@@ -301,17 +413,34 @@ static int p9_virtio_probe(struct virtio_device *vdev)
301out_free_vq: 413out_free_vq:
302 vdev->config->del_vq(chan->vq); 414 vdev->config->del_vq(chan->vq);
303fail: 415fail:
304 down(&virtio_9p_lock); 416 mutex_lock(&virtio_9p_lock);
305 chan_index--; 417 chan_index--;
306 up(&virtio_9p_lock); 418 mutex_unlock(&virtio_9p_lock);
307 return err; 419 return err;
308} 420}
309 421
310/* This sets up a transport channel for 9p communication. Right now 422
423/**
424 * p9_virtio_create - allocate a new virtio channel
425 * @devname: string identifying the channel to connect to (unused)
426 * @args: args passed from sys_mount() for per-transport options (unused)
427 * @msize: requested maximum packet size
428 * @extended: 9p2000.u enabled flag
429 *
430 * This sets up a transport channel for 9p communication. Right now
311 * we only match the first available channel, but eventually we couldlook up 431 * we only match the first available channel, but eventually we couldlook up
312 * alternate channels by matching devname versus a virtio_config entry. 432 * alternate channels by matching devname versus a virtio_config entry.
313 * We use a simple reference count mechanism to ensure that only a single 433 * We use a simple reference count mechanism to ensure that only a single
314 * mount has a channel open at a time. */ 434 * mount has a channel open at a time.
435 *
436 * Bugs: doesn't allow identification of a specific channel
437 * to allocate, channels are allocated sequentially. This was
438 * a pragmatic decision to get things rolling, but ideally some
439 * way of identifying the channel to attach to would be nice
440 * if we are going to support multiple channels.
441 *
442 */
443
315static struct p9_trans * 444static struct p9_trans *
316p9_virtio_create(const char *devname, char *args, int msize, 445p9_virtio_create(const char *devname, char *args, int msize,
317 unsigned char extended) 446 unsigned char extended)
@@ -320,7 +449,7 @@ p9_virtio_create(const char *devname, char *args, int msize,
320 struct virtio_chan *chan = channels; 449 struct virtio_chan *chan = channels;
321 int index = 0; 450 int index = 0;
322 451
323 down(&virtio_9p_lock); 452 mutex_lock(&virtio_9p_lock);
324 while (index < MAX_9P_CHAN) { 453 while (index < MAX_9P_CHAN) {
325 if (chan->initialized && !chan->inuse) { 454 if (chan->initialized && !chan->inuse) {
326 chan->inuse = true; 455 chan->inuse = true;
@@ -330,7 +459,7 @@ p9_virtio_create(const char *devname, char *args, int msize,
330 chan = &channels[index]; 459 chan = &channels[index];
331 } 460 }
332 } 461 }
333 up(&virtio_9p_lock); 462 mutex_unlock(&virtio_9p_lock);
334 463
335 if (index >= MAX_9P_CHAN) { 464 if (index >= MAX_9P_CHAN) {
336 printk(KERN_ERR "9p: no channels available\n"); 465 printk(KERN_ERR "9p: no channels available\n");
@@ -360,6 +489,12 @@ p9_virtio_create(const char *devname, char *args, int msize,
360 return trans; 489 return trans;
361} 490}
362 491
492/**
493 * p9_virtio_remove - clean up resources associated with a virtio device
494 * @vdev: virtio device to remove
495 *
496 */
497
363static void p9_virtio_remove(struct virtio_device *vdev) 498static void p9_virtio_remove(struct virtio_device *vdev)
364{ 499{
365 struct virtio_chan *chan = vdev->priv; 500 struct virtio_chan *chan = vdev->priv;