1 /******************************************************************************
2 * Copyright (c) 2011 IBM Corporation
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
10 * IBM Corporation - initial implementation
11 *****************************************************************************/
16 #include <byteorder.h>
19 #include "virtio-9p.h"
24 * Notes for 9P Server config:
26 * make distclean; cm make qemu
27 * sudo cp boot_rom.bin /opt/qemu/share/qemu/slof.bin
28 * /opt/qemu/bin/qemu-system-ppc64 -M pseries -m 512 -boot d -nographic -fsdev
29 * local,id=trule,path=/home/trule/virtfs,security_model=none -device
30 * virtio-9p-spapr,fsdev=trule,mount_tag=trule
31 * load virtfs:\some\file
34 /* We support only one instance due to the (ab)use of globals. We
35 * use the buffer size as an open marker as well.
37 static int __buf_size;
43 #define MIN(a,b) ((a)>(b)?(b):(a))
49 #define dprintf(_x ...) do { printf(_x); } while(0)
51 #define dprintf(_x ...)
55 static void dprint_buffer(const char *name, uint8_t *buffer, int length)
59 printf("*** %s ***", name);
61 for (i = 0; i < length; i++) {
63 printf("\n %04x:", i);
66 printf(" %02x", buffer[i]);
72 #define dprint_buffer(n, b, l)
78 * Perform a 9P transaction over the VIRTIO queue interface. This function is
79 * registered with the p9.c library via p9_reg_transport() to provide
80 * connectivity to the 9P server.
82 * @param tx[in] Data to send, mapped to first queue item.
83 * @param tx_size[in] Size of data to send.
84 * @param rx[out] Data to receive, mappend to second queue item.
85 * @param rx_size[out] Size of data received.
86 * @return 0 = success, -ve = error.
88 static int virtio_9p_transact(void *opaque, uint8_t *tx, int tx_size, uint8_t *rx,
91 struct virtio_device *dev = opaque;
92 struct vring_desc *desc;
95 struct vring_desc *vq_desc;
96 struct vring_avail *vq_avail;
97 struct vring_used *vq_used;
98 volatile uint16_t *current_used_idx;
99 uint16_t last_used_idx;
102 /* Virt IO queues. */
103 vq_size = virtio_get_qsize(dev, 0);
104 vq_desc = virtio_get_vring_desc(dev, 0);
105 vq_avail = virtio_get_vring_avail(dev, 0);
106 vq_used = virtio_get_vring_used(dev, 0);
108 last_used_idx = vq_used->idx;
109 current_used_idx = &vq_used->idx;
111 /* Determine descriptor index */
112 id = (vq_avail->idx * 3) % vq_size;
114 /* TX in first queue item. */
115 dprint_buffer("TX", tx, tx_size);
118 desc->addr = (uint64_t)tx;
120 desc->flags = VRING_DESC_F_NEXT;
121 desc->next = (id + 1) % vq_size;
123 /* RX in the second queue item. */
124 desc = &vq_desc[(id + 1) % vq_size];
125 desc->addr = (uint64_t)rx;
126 desc->len = *rx_size;
127 desc->flags = VRING_DESC_F_WRITE;
130 /* Tell HV that the queue is ready */
131 vq_avail->ring[vq_avail->idx % vq_size] = id;
134 virtio_queue_notify(dev, 0);
136 /* Receive the response. */
138 while (*current_used_idx == last_used_idx && i-- > 0) {
139 // do something better
146 *rx_size = MIN(*rx_size, le32_to_cpu(*(uint32_t*)(&rx[0])));
147 dprint_buffer("RX", rx, *rx_size);
155 * Establish the VIRTIO connection for use with the 9P server. Setup queues
156 * and negotiate capabilities. Setup the 9P (Client) library.
158 * @param reg[in] Pointer to device tree node for VIRTIO/9P interface.
159 * @param tx_buf[in] TX buffer for use by 9P Client lib - 8K in size.
160 * @param rx_buf[in] TX buffer for use by 9P Client lib - 8K in size.
161 * @param buf_size Somewhat redundant, buffer size expected to be 8k.
162 * @return 0 = success, -ve = error.
164 int virtio_9p_init(struct virtio_device *dev, void *tx_buf, void *rx_buf,
167 struct vring_avail *vq_avail;
169 /* Check for double open */
172 __buf_size = buf_size;
174 dprintf("%s : device at %p\n", __func__, dev->base);
175 dprintf("%s : type is %04x\n", __func__, dev->type);
178 // XXX That will clear the virtq base. We need to move
179 // initializing it to here anyway
181 // virtio_reset_device(dev);
183 /* Acknowledge device. */
184 virtio_set_status(dev, VIRTIO_STAT_ACKNOWLEDGE);
186 /* Tell HV that we know how to drive the device. */
187 virtio_set_status(dev, VIRTIO_STAT_ACKNOWLEDGE | VIRTIO_STAT_DRIVER);
189 /* Device specific setup - we do not support special features */
190 virtio_set_guest_features(dev, 0);
192 vq_avail = virtio_get_vring_avail(dev, 0);
193 vq_avail->flags = VRING_AVAIL_F_NO_INTERRUPT;
196 /* Tell HV that setup succeeded */
197 virtio_set_status(dev, VIRTIO_STAT_ACKNOWLEDGE | VIRTIO_STAT_DRIVER
198 |VIRTIO_STAT_DRIVER_OK);
200 /* Setup 9P library. */
201 p9_reg_transport(virtio_9p_transact, dev,(uint8_t *)tx_buf,
204 dprintf("%s : complete\n", __func__);
211 void virtio_9p_shutdown(struct virtio_device *dev)
214 virtio_set_status(dev, VIRTIO_STAT_FAILED);
217 virtio_reset_device(dev);
225 * Read a file from the 9P Server on the VIRTIO interface.
227 * @param file_name[in] File to read, use Linux style paths.
228 * @param buffer[out] Where to read the file to.
229 * @return +ve = amount of data read, -ve = error.
231 int virtio_9p_load(struct virtio_device *dev, const char *file_name, uint8_t *buffer)
235 char tag_name[TAG_SIZE];
237 uint8_t *pos = (uint8_t *)file_name;
238 int start_fid = ROOT_FID;
239 p9_connection_t connection = {
240 .message_size = __buf_size,
245 .connection = &connection,
250 /* Get the share name from 9P config space. */
251 tag_len = virtio_get_config(dev, 0, sizeof(tag_len));
252 if (tag_len >= TAG_SIZE)
253 tag_len = TAG_SIZE - 1;
254 __virtio_read_config(dev, tag_name, 2, tag_len);
255 connection.aname = tag_name;
257 /* Connect to the 9P server. */
258 dprintf("%s : connecting, tag = %s, user = %s, msgsize = %d\n",
259 __func__, connection.aname, connection.uname,
260 connection.message_size);
261 rc = p9_version(&connection);
263 printf("Version check failed, rc = %d\n", rc);
264 goto cleanup_connection;
266 rc = p9_attach(&connection);
268 printf("Attach failed, rc = %d\n", rc);
269 goto cleanup_connection;
271 dprintf("%s : connected, msgsize = %d\n", __func__,
272 connection.message_size);
274 /* Walk to the file. */
276 dprintf("%s : walk path %s\n", __func__, pos);
277 rc = p9_walk(&connection, start_fid, FILE_FID, &pos);
279 if (rc < 0) { /* Some error. */
280 printf("Walk failed, rc = %d\n", rc);
281 goto cleanup_connection;
285 * If partial walk (*pos != 0) then continue the walk from
286 * mid point with start_fid updated to current position
287 * FILE_FID. FILE_FID will then be reused for the result of
288 * the next call to walk.
290 start_fid = FILE_FID;
294 dprintf("%s : stat and open\n", __func__);
297 printf("Stat failed, rc = %d\n", rc);
300 rc = p9_open(&file, 0x00); /* TODO find include for "read mode" */
302 printf("Open failed, rc = %d\n", rc);
305 dprintf("%s : file opened, size %lld\n", __func__, file.length);
307 /* Read the file contents to buffer. */
308 while (offset < file.length) {
309 dprintf("%s : read from offset %llu\n", __func__, offset);
310 rc = p9_read(&file, buffer + offset,
311 file.length - offset, offset);
312 dprintf("%s : read done, length was %d\n", __func__, rc);
314 printf("Read failed, rc = %d\n", rc);
324 /* Cleanup and disconnect. */
326 dprintf("%s : clunking file\n", __func__);
327 p9_clunk(&connection, file.fid);
330 dprintf("%s : clunking connection\n", __func__);
331 p9_clunk(&connection, connection.fid);
334 dprintf("%s : complete, read %llu bytes\n", __func__, offset);
335 return rc == 0 ? offset : rc;