These changes are the raw update to qemu-2.6.
[kvmfornfv.git] / qemu / roms / SLOF / lib / libvirtio / virtio-9p.c
1 /******************************************************************************
2  * Copyright (c) 2011 IBM Corporation
3  * All rights reserved.
4  * This program and the accompanying materials
5  * are made available under the terms of the BSD License
6  * which accompanies this distribution, and is available at
7  * http://www.opensource.org/licenses/bsd-license.php
8  *
9  * Contributors:
10  *     IBM Corporation - initial implementation
11  *****************************************************************************/
12
13 #include <stdio.h>
14 #include <string.h>
15 #include <stdint.h>
16 #include <byteorder.h>
17 #include <cpu.h>
18
19 #include "virtio-9p.h"
20 #include "p9.h"
21
22 static struct vqs vq;
23
24 /**
25  * Notes for 9P Server config:
26  *
27  * make distclean; cm make qemu
28  * sudo cp boot_rom.bin /opt/qemu/share/qemu/slof.bin
29  * /opt/qemu/bin/qemu-system-ppc64 -M pseries -m 512 -boot d -nographic -fsdev
30  *    local,id=trule,path=/home/trule/virtfs,security_model=none -device
31  *    virtio-9p-spapr,fsdev=trule,mount_tag=trule
32  * load virtfs:\some\file
33  */
34
35 /* We support only one instance due to the (ab)use of globals. We
36  * use the buffer size as an open marker as well.
37  */
38 static int __buf_size;
39
40
41 #define ROOT_FID        1
42 #define FILE_FID        2
43 #define TAG_SIZE        128
44 #define MIN(a,b)        ((a)>(b)?(b):(a))
45
46
47 #undef DEBUG
48 //#define DEBUG
49 #ifdef DEBUG
50 #define dprintf(_x ...) do { printf(_x); } while(0)
51 #else
52 #define dprintf(_x ...)
53 #endif
54
55 #ifdef DEBUG
56 static void dprint_buffer(const char *name, uint8_t *buffer, int length)
57 {
58         int i;
59
60         printf("*** %s ***", name);
61
62         for (i = 0; i < length; i++) {
63                 if (i % 16 == 0) {
64                         printf("\n %04x:", i);
65                 }
66
67                 printf(" %02x", buffer[i]);
68         }
69
70         printf("\n");
71 }
72 #else
73 #define dprint_buffer(n, b, l)
74 #endif
75
76 /**
77  * virtio_9p_transact
78  *
79  * Perform a 9P transaction over the VIRTIO queue interface. This function is
80  * registered with the p9.c library via p9_reg_transport() to provide
81  * connectivity to the 9P server.
82  *
83  * @param tx[in]        Data to send, mapped to first queue item.
84  * @param tx_size[in]   Size of data to send.
85  * @param rx[out]       Data to receive, mappend to second queue item.
86  * @param rx_size[out]  Size of data received.
87  * @return      0 = success, -ve = error.
88  */
89 static int virtio_9p_transact(void *opaque, uint8_t *tx, int tx_size, uint8_t *rx,
90                               uint32_t *rx_size)
91 {
92         struct virtio_device *dev = opaque;
93         struct vring_desc *desc;
94         int id, i;
95         uint32_t vq_size;
96         struct vring_desc *vq_desc;
97         struct vring_avail *vq_avail;
98         struct vring_used *vq_used;
99         volatile uint16_t *current_used_idx;
100         uint16_t last_used_idx;
101
102
103         /* Virt IO queues. */
104         vq_size = virtio_get_qsize(dev, 0);
105         vq_desc = virtio_get_vring_desc(dev, 0);
106         vq_avail = virtio_get_vring_avail(dev, 0);
107         vq_used = virtio_get_vring_used(dev, 0);
108
109         last_used_idx = vq_used->idx;
110         current_used_idx = &vq_used->idx;
111
112         /* Determine descriptor index */
113         id = (vq_avail->idx * 3) % vq_size;
114
115         /* TX in first queue item. */
116         dprint_buffer("TX", tx, tx_size);
117
118         desc = &vq_desc[id];
119         desc->addr = (uint64_t)tx;
120         desc->len = tx_size;
121         desc->flags = VRING_DESC_F_NEXT;
122         desc->next = (id + 1) % vq_size;
123
124         /* RX in the second queue item. */
125         desc = &vq_desc[(id + 1) % vq_size];
126         desc->addr = (uint64_t)rx;
127         desc->len = *rx_size;
128         desc->flags = VRING_DESC_F_WRITE;
129         desc->next = 0;
130
131         /* Tell HV that the queue is ready */
132         vq_avail->ring[vq_avail->idx % vq_size] = id;
133         mb();
134         vq_avail->idx += 1;
135         virtio_queue_notify(dev, 0);
136
137         /* Receive the response. */
138         i = 10000000;
139         while (*current_used_idx == last_used_idx && i-- > 0) {
140                 // do something better
141                 mb();
142         }
143         if (i == 0) {
144                 return -1;
145         }
146
147         *rx_size = MIN(*rx_size, le32_to_cpu(*(uint32_t*)(&rx[0])));
148         dprint_buffer("RX", rx, *rx_size);
149
150         return 0;
151 }
152
153 /**
154  * virtio_9p_init
155  *
156  * Establish the VIRTIO connection for use with the 9P server. Setup queues
157  * and negotiate capabilities. Setup the 9P (Client) library.
158  *
159  * @param reg[in]       Pointer to device tree node for VIRTIO/9P interface.
160  * @param tx_buf[in]    TX buffer for use by 9P Client lib - 8K in size.
161  * @param rx_buf[in]    TX buffer for use by 9P Client lib - 8K in size.
162  * @param buf_size      Somewhat redundant, buffer size expected to be 8k.
163  * @return      0 = success, -ve = error.
164  */
165 int virtio_9p_init(struct virtio_device *dev, void *tx_buf, void *rx_buf,
166                    int buf_size)
167 {
168         struct vring_avail *vq_avail;
169         int status = VIRTIO_STAT_ACKNOWLEDGE;
170
171         /* Check for double open */
172         if (__buf_size)
173                 return -1;
174         __buf_size = buf_size;
175
176         dprintf("%s : device at %p\n", __func__, dev->base);
177         dprintf("%s : type is %04x\n", __func__, dev->type);
178
179         /* Keep it disabled until the driver is 1.0 capable */
180         dev->is_modern = false;
181
182         virtio_reset_device(dev);
183
184         /* Acknowledge device. */
185         virtio_set_status(dev, status);
186
187         /* Tell HV that we know how to drive the device. */
188         status |= VIRTIO_STAT_DRIVER;
189         virtio_set_status(dev, status);
190
191         /* Device specific setup - we do not support special features */
192         virtio_set_guest_features(dev,  0);
193
194         if (virtio_queue_init_vq(dev, &vq, 0))
195                 goto dev_error;
196
197         vq_avail = virtio_get_vring_avail(dev, 0);
198         vq_avail->flags = VRING_AVAIL_F_NO_INTERRUPT;
199         vq_avail->idx = 0;
200
201         /* Tell HV that setup succeeded */
202         status |= VIRTIO_STAT_DRIVER_OK;
203         virtio_set_status(dev, status);
204
205         /* Setup 9P library. */
206         p9_reg_transport(virtio_9p_transact, dev,(uint8_t *)tx_buf,
207                         (uint8_t *)rx_buf);
208
209         dprintf("%s : complete\n", __func__);
210         return 0;
211
212 dev_error:
213         printf("%s: failed\n", __func__);
214         status |= VIRTIO_STAT_FAILED;
215         virtio_set_status(dev, status);
216         return -1;
217 }
218
219 /**
220  * virtio_9p_shutdown
221  */
222 void virtio_9p_shutdown(struct virtio_device *dev)
223 {
224         /* Quiesce device */
225         virtio_set_status(dev, VIRTIO_STAT_FAILED);
226
227         /* Reset device */
228         virtio_reset_device(dev);
229
230         __buf_size = 0;
231 }
232
233 /**
234  * virtio_9p_load
235  *
236  * Read a file from the 9P Server on the VIRTIO interface.
237  *
238  * @param file_name[in] File to read, use Linux style paths.
239  * @param buffer[out]   Where to read the file to.
240  * @return      +ve = amount of data read, -ve = error.
241  */
242 long virtio_9p_load(struct virtio_device *dev, const char *file_name, uint8_t *buffer)
243 {
244         int rc;
245         uint16_t tag_len;
246         char tag_name[TAG_SIZE];
247         uint64_t offset = 0;
248         uint8_t *pos = (uint8_t *)file_name;
249         int start_fid = ROOT_FID;
250         p9_connection_t connection = {
251                 .message_size = __buf_size,
252                 .fid = ROOT_FID,
253                 .uname = "slof"
254         };
255         p9_file_t file = {
256                 .connection = &connection,
257                 .fid = FILE_FID,
258         };
259
260
261         /* Get the share name from 9P config space. */
262         tag_len = virtio_get_config(dev, 0, sizeof(tag_len));
263         if (tag_len >= TAG_SIZE)
264                 tag_len = TAG_SIZE - 1;
265         __virtio_read_config(dev, tag_name, 2, tag_len);
266         connection.aname = tag_name;
267
268         /* Connect to the 9P server. */
269         dprintf("%s : connecting, tag = %s, user = %s, msgsize = %d\n",
270                         __func__, connection.aname, connection.uname,
271                         connection.message_size);
272         rc = p9_version(&connection);
273         if (rc != 0) {
274                 printf("Version check failed, rc = %d\n", rc);
275                 goto cleanup_connection;
276         }
277         rc = p9_attach(&connection);
278         if (rc != 0) {
279                 printf("Attach failed, rc = %d\n", rc);
280                 goto cleanup_connection;
281         }
282         dprintf("%s : connected, msgsize = %d\n", __func__,
283                         connection.message_size);
284
285         /* Walk to the file. */
286         do {
287                 dprintf("%s : walk path %s\n", __func__, pos);
288                 rc = p9_walk(&connection, start_fid, FILE_FID, &pos);
289
290                 if (rc < 0) {   /* Some error. */
291                         printf("Walk failed, rc = %d\n", rc);
292                         goto cleanup_connection;
293                 }
294
295                 /*
296                  * If partial walk (*pos != 0) then continue the walk from
297                  * mid point with start_fid updated to current position
298                  * FILE_FID. FILE_FID will then be reused for the result of
299                  * the next call to walk.
300                  */
301                 start_fid = FILE_FID;
302         } while (*pos != 0);
303
304         /* Open the file. */
305         dprintf("%s : stat and open\n", __func__);
306         rc = p9_stat(&file);
307         if (rc != 0) {
308                 printf("Stat failed, rc = %d\n", rc);
309                 goto cleanup_file;
310         }
311         rc = p9_open(&file, 0x00); /* TODO find include for "read mode" */
312         if (rc != 0) {
313                 printf("Open failed, rc = %d\n", rc);
314                 goto cleanup_file;
315         }
316         dprintf("%s : file opened, size %lld\n", __func__, file.length);
317
318         /* Read the file contents to buffer. */
319         while (offset < file.length) {
320                 dprintf("%s : read from offset %llu\n", __func__, offset);
321                 rc = p9_read(&file, buffer + offset,
322                                 file.length - offset, offset);
323                 dprintf("%s : read done, length was %d\n", __func__, rc);
324                 if (rc < 0) {
325                         printf("Read failed, rc = %d\n", rc);
326                         goto cleanup_file;
327                 }
328                 if (rc == 0) {
329                         break;
330                 }
331                 offset += rc;
332                 rc = 0;
333         }
334
335         /* Cleanup and disconnect. */
336 cleanup_file:
337         dprintf("%s : clunking file\n", __func__);
338         p9_clunk(&connection, file.fid);
339
340 cleanup_connection:
341         dprintf("%s : clunking connection\n", __func__);
342         p9_clunk(&connection, connection.fid);
343
344
345         dprintf("%s : complete, read %llu bytes\n", __func__, offset);
346         return rc == 0 ? (long)offset : rc;
347 }