/** * Initialize virtio-block device. * @param dev pointer to virtio device information */ int virtioblk_init(struct virtio_device *dev) { struct vring_avail *vq_avail; /* Reset device */ // XXX That will clear the virtq base. We need to move // initializing it to here anyway // // virtio_reset_device(dev); /* Acknowledge device. */ virtio_set_status(dev, VIRTIO_STAT_ACKNOWLEDGE); /* Tell HV that we know how to drive the device. */ virtio_set_status(dev, VIRTIO_STAT_ACKNOWLEDGE|VIRTIO_STAT_DRIVER); /* Device specific setup - we do not support special features right now */ virtio_set_guest_features(dev, 0); vq_avail = virtio_get_vring_avail(dev, 0); vq_avail->flags = VRING_AVAIL_F_NO_INTERRUPT; vq_avail->idx = 0; /* Tell HV that setup succeeded */ virtio_set_status(dev, VIRTIO_STAT_ACKNOWLEDGE|VIRTIO_STAT_DRIVER |VIRTIO_STAT_DRIVER_OK); return 0; }
/** * virtio_9p_init * * Establish the VIRTIO connection for use with the 9P server. Setup queues * and negotiate capabilities. Setup the 9P (Client) library. * * @param reg[in] Pointer to device tree node for VIRTIO/9P interface. * @param tx_buf[in] TX buffer for use by 9P Client lib - 8K in size. * @param rx_buf[in] TX buffer for use by 9P Client lib - 8K in size. * @param buf_size Somewhat redundant, buffer size expected to be 8k. * @return 0 = success, -ve = error. */ int virtio_9p_init(struct virtio_device *dev, void *tx_buf, void *rx_buf, int buf_size) { struct vring_avail *vq_avail; int status = VIRTIO_STAT_ACKNOWLEDGE; /* Check for double open */ if (__buf_size) return -1; __buf_size = buf_size; dprintf("%s : device at %p\n", __func__, dev->base); dprintf("%s : type is %04x\n", __func__, dev->type); /* Keep it disabled until the driver is 1.0 capable */ dev->is_modern = false; virtio_reset_device(dev); /* Acknowledge device. */ virtio_set_status(dev, status); /* Tell HV that we know how to drive the device. */ status |= VIRTIO_STAT_DRIVER; virtio_set_status(dev, status); /* Device specific setup - we do not support special features */ virtio_set_guest_features(dev, 0); if (virtio_queue_init_vq(dev, &vq, 0)) goto dev_error; vq_avail = virtio_get_vring_avail(dev, 0); vq_avail->flags = VRING_AVAIL_F_NO_INTERRUPT; vq_avail->idx = 0; /* Tell HV that setup succeeded */ status |= VIRTIO_STAT_DRIVER_OK; virtio_set_status(dev, status); /* Setup 9P library. */ p9_reg_transport(virtio_9p_transact, dev,(uint8_t *)tx_buf, (uint8_t *)rx_buf); dprintf("%s : complete\n", __func__); return 0; dev_error: printf("%s: failed\n", __func__); status |= VIRTIO_STAT_FAILED; virtio_set_status(dev, status); return -1; }
/** * virtio_9p_init * * Establish the VIRTIO connection for use with the 9P server. Setup queues * and negotiate capabilities. Setup the 9P (Client) library. * * @param reg[in] Pointer to device tree node for VIRTIO/9P interface. * @param tx_buf[in] TX buffer for use by 9P Client lib - 8K in size. * @param rx_buf[in] TX buffer for use by 9P Client lib - 8K in size. * @param buf_size Somewhat redundant, buffer size expected to be 8k. * @return 0 = success, -ve = error. */ int virtio_9p_init(struct virtio_device *dev, void *tx_buf, void *rx_buf, int buf_size) { struct vring_avail *vq_avail; /* Check for double open */ if (__buf_size) return -1; __buf_size = buf_size; dprintf("%s : device at %p\n", __func__, dev->base); dprintf("%s : type is %04x\n", __func__, dev->type); /* Reset device */ // XXX That will clear the virtq base. We need to move // initializing it to here anyway // // virtio_reset_device(dev); /* Acknowledge device. */ virtio_set_status(dev, VIRTIO_STAT_ACKNOWLEDGE); /* Tell HV that we know how to drive the device. */ virtio_set_status(dev, VIRTIO_STAT_ACKNOWLEDGE | VIRTIO_STAT_DRIVER); /* Device specific setup - we do not support special features */ virtio_set_guest_features(dev, 0); vq_avail = virtio_get_vring_avail(dev, 0); vq_avail->flags = VRING_AVAIL_F_NO_INTERRUPT; vq_avail->idx = 0; /* Tell HV that setup succeeded */ virtio_set_status(dev, VIRTIO_STAT_ACKNOWLEDGE | VIRTIO_STAT_DRIVER |VIRTIO_STAT_DRIVER_OK); /* Setup 9P library. */ p9_reg_transport(virtio_9p_transact, dev,(uint8_t *)tx_buf, (uint8_t *)rx_buf); dprintf("%s : complete\n", __func__); return 0; }
/** * virtio_9p_transact * * Perform a 9P transaction over the VIRTIO queue interface. This function is * registered with the p9.c library via p9_reg_transport() to provide * connectivity to the 9P server. * * @param tx[in] Data to send, mapped to first queue item. * @param tx_size[in] Size of data to send. * @param rx[out] Data to receive, mappend to second queue item. * @param rx_size[out] Size of data received. * @return 0 = success, -ve = error. */ static int virtio_9p_transact(void *opaque, uint8_t *tx, int tx_size, uint8_t *rx, int *rx_size) { struct virtio_device *dev = opaque; struct vring_desc *desc; int id, i; uint32_t vq_size; struct vring_desc *vq_desc; struct vring_avail *vq_avail; struct vring_used *vq_used; volatile uint16_t *current_used_idx; uint16_t last_used_idx; /* Virt IO queues. */ vq_size = virtio_get_qsize(dev, 0); vq_desc = virtio_get_vring_desc(dev, 0); vq_avail = virtio_get_vring_avail(dev, 0); vq_used = virtio_get_vring_used(dev, 0); last_used_idx = vq_used->idx; current_used_idx = &vq_used->idx; /* Determine descriptor index */ id = (vq_avail->idx * 3) % vq_size; /* TX in first queue item. */ dprint_buffer("TX", tx, tx_size); desc = &vq_desc[id]; desc->addr = (uint64_t)tx; desc->len = tx_size; desc->flags = VRING_DESC_F_NEXT; desc->next = (id + 1) % vq_size; /* RX in the second queue item. */ desc = &vq_desc[(id + 1) % vq_size]; desc->addr = (uint64_t)rx; desc->len = *rx_size; desc->flags = VRING_DESC_F_WRITE; desc->next = 0; /* Tell HV that the queue is ready */ vq_avail->ring[vq_avail->idx % vq_size] = id; mb(); vq_avail->idx += 1; virtio_queue_notify(dev, 0); /* Receive the response. */ i = 10000000; while (*current_used_idx == last_used_idx && i-- > 0) { // do something better mb(); } if (i == 0) { return -1; } *rx_size = MIN(*rx_size, le32_to_cpu(*(uint32_t*)(&rx[0]))); dprint_buffer("RX", rx, *rx_size); return 0; }
/** * Read blocks * @param reg pointer to "reg" property * @param buf pointer to destination buffer * @param blocknum block number of the first block that should be read * @param cnt amount of blocks that should be read * @return number of blocks that have been read successfully */ int virtioblk_read(struct virtio_device *dev, char *buf, long blocknum, long cnt) { struct vring_desc *desc; int id; static struct virtio_blk_req blkhdr; //struct virtio_blk_config *blkconf; uint64_t capacity; uint32_t vq_size, time; struct vring_desc *vq_desc; /* Descriptor vring */ struct vring_avail *vq_avail; /* "Available" vring */ struct vring_used *vq_used; /* "Used" vring */ volatile uint8_t status = -1; volatile uint16_t *current_used_idx; uint16_t last_used_idx; //printf("virtioblk_read: dev=%p buf=%p blocknum=%li count=%li\n", // dev, buf, blocknum, cnt); /* Check whether request is within disk capacity */ capacity = virtio_get_config(dev, 0, sizeof(capacity)); if (blocknum + cnt - 1 > capacity) { puts("virtioblk_read: Access beyond end of device!"); return 0; } vq_size = virtio_get_qsize(dev, 0); vq_desc = virtio_get_vring_desc(dev, 0); vq_avail = virtio_get_vring_avail(dev, 0); vq_used = virtio_get_vring_used(dev, 0); last_used_idx = vq_used->idx; current_used_idx = &vq_used->idx; /* Set up header */ blkhdr.type = VIRTIO_BLK_T_IN | VIRTIO_BLK_T_BARRIER; blkhdr.ioprio = 1; blkhdr.sector = blocknum; /* Determine descriptor index */ id = (vq_avail->idx * 3) % vq_size; /* Set up virtqueue descriptor for header */ desc = &vq_desc[id]; desc->addr = (uint64_t)&blkhdr; desc->len = sizeof(struct virtio_blk_req); desc->flags = VRING_DESC_F_NEXT; desc->next = (id + 1) % vq_size; /* Set up virtqueue descriptor for data */ desc = &vq_desc[(id + 1) % vq_size]; desc->addr = (uint64_t)buf; desc->len = cnt * 512; desc->flags = VRING_DESC_F_NEXT | VRING_DESC_F_WRITE; desc->next = (id + 2) % vq_size; /* Set up virtqueue descriptor for status */ desc = &vq_desc[(id + 2) % vq_size]; desc->addr = (uint64_t)&status; desc->len = 1; desc->flags = VRING_DESC_F_WRITE; desc->next = 0; vq_avail->ring[vq_avail->idx % vq_size] = id; mb(); vq_avail->idx += 1; /* Tell HV that the queue is ready */ virtio_queue_notify(dev, 0); /* Wait for host to consume the descriptor */ time = SLOF_GetTimer() + VIRTIO_TIMEOUT; while (*current_used_idx == last_used_idx) { // do something better mb(); if (time < SLOF_GetTimer()) break; } if (status == 0) return cnt; printf("virtioblk_read failed! status = %i\n", status); return 0; }
/** * Get address of "used" vring * @param dev pointer to virtio device information * @param queue virtio queue number * @return pointer to the "used" ring */ struct vring_used *virtio_get_vring_used(struct virtio_device *dev, int queue) { return (void*)VQ_ALIGN((uint64_t)virtio_get_vring_avail(dev, queue) + virtio_get_qsize(dev, queue) * sizeof(struct vring_avail)); }