1 /******************************************************************************
2  * Copyright (c) 2012 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 <cpu.h>
16 #include <helpers.h>
17 #include "virtio.h"
18 #include "virtio-internal.h"
19 #include "virtio-scsi.h"
20 
virtioscsi_send(struct virtio_device * dev,struct virtio_scsi_req_cmd * req,struct virtio_scsi_resp_cmd * resp,int is_read,void * buf,uint64_t buf_len)21 int virtioscsi_send(struct virtio_device *dev,
22 		    struct virtio_scsi_req_cmd *req,
23 		    struct virtio_scsi_resp_cmd *resp,
24 		    int is_read, void *buf, uint64_t buf_len)
25 {
26 
27 	volatile uint16_t *current_used_idx;
28 	uint16_t last_used_idx, avail_idx;
29 	int id;
30 	uint32_t time;
31 	struct vqs *vq = &dev->vq[VIRTIO_SCSI_REQUEST_VQ];
32 
33 	avail_idx = virtio_modern16_to_cpu(dev, vq->avail->idx);
34 
35 	last_used_idx = vq->used->idx;
36 	current_used_idx = &vq->used->idx;
37 
38 	/* Determine descriptor index */
39 	id = (avail_idx * 3) % vq->size;
40 	virtio_fill_desc(vq, id, dev->features, (uint64_t)req, sizeof(*req), VRING_DESC_F_NEXT,
41 			 id + 1);
42 
43 	if (buf == NULL || buf_len == 0) {
44 		/* Set up descriptor for response information */
45 		virtio_fill_desc(vq, id + 1, dev->features,
46 				 (uint64_t)resp, sizeof(*resp),
47 				 VRING_DESC_F_WRITE, 0);
48 	} else if (is_read) {
49 		/* Set up descriptor for response information */
50 		virtio_fill_desc(vq, id + 1, dev->features,
51 				 (uint64_t)resp, sizeof(*resp),
52 				 VRING_DESC_F_NEXT | VRING_DESC_F_WRITE,
53 				 id + 2);
54 		/* Set up virtqueue descriptor for data from device */
55 		virtio_fill_desc(vq, id + 2, dev->features,
56 				 (uint64_t)buf, buf_len, VRING_DESC_F_WRITE, 0);
57 	} else {
58 		/* Set up virtqueue descriptor for data to device */
59 		virtio_fill_desc(vq, id + 1, dev->features,
60 				 (uint64_t)buf, buf_len, VRING_DESC_F_NEXT,
61 				 id + 2);
62 		/* Set up descriptor for response information */
63 		virtio_fill_desc(vq, id + 2, dev->features,
64 				 (uint64_t)resp, sizeof(*resp),
65 				 VRING_DESC_F_WRITE, 0);
66 	}
67 
68 	vq->avail->ring[avail_idx % vq->size] = virtio_cpu_to_modern16(dev, id);
69 	mb();
70 	vq->avail->idx = virtio_cpu_to_modern16(dev, avail_idx + 1);
71 
72 	/* Tell HV that the vq is ready */
73 	virtio_queue_notify(dev, VIRTIO_SCSI_REQUEST_VQ);
74 
75 	/* Wait for host to consume the descriptor */
76 	time = SLOF_GetTimer() + VIRTIO_TIMEOUT;
77 	while (*current_used_idx == last_used_idx) {
78 		// do something better
79 		mb();
80 		if (time < SLOF_GetTimer())
81 			break;
82 	}
83 
84 	virtio_free_desc(vq, id, dev->features);
85 	virtio_free_desc(vq, id + 1, dev->features);
86 	if (!(buf == NULL || buf_len == 0))
87 		virtio_free_desc(vq, id + 2, dev->features);
88 
89 	return 0;
90 }
91 
92 /**
93  * Initialize virtio-block device.
94  * @param  dev  pointer to virtio device information
95  */
virtioscsi_init(struct virtio_device * dev)96 int virtioscsi_init(struct virtio_device *dev)
97 {
98 	struct vqs *vq_ctrl, *vq_event, *vq_request;
99 	int status = VIRTIO_STAT_ACKNOWLEDGE;
100 
101 	/* Reset device */
102 	// XXX That will clear the virtq base. We need to move
103 	//     initializing it to here anyway
104 	//
105 	//     virtio_reset_device(dev);
106 
107 	/* Acknowledge device. */
108 	virtio_set_status(dev, status);
109 
110 	/* Tell HV that we know how to drive the device. */
111 	status |= VIRTIO_STAT_DRIVER;
112 	virtio_set_status(dev, status);
113 
114 	/* Device specific setup - we do not support special features right now */
115 	if (dev->features & VIRTIO_F_VERSION_1) {
116 		if (virtio_negotiate_guest_features(dev, VIRTIO_F_VERSION_1))
117 			goto dev_error;
118 		virtio_get_status(dev, &status);
119 	} else {
120 		virtio_set_guest_features(dev, 0);
121 	}
122 
123 	vq_ctrl = virtio_queue_init_vq(dev, VIRTIO_SCSI_CONTROL_VQ);
124 	vq_event = virtio_queue_init_vq(dev, VIRTIO_SCSI_EVENT_VQ);
125 	vq_request = virtio_queue_init_vq(dev, VIRTIO_SCSI_REQUEST_VQ);
126 	if (!vq_ctrl || !vq_event || !vq_request)
127 		goto dev_error;
128 
129 	/* Tell HV that setup succeeded */
130 	status |= VIRTIO_STAT_DRIVER_OK;
131 	virtio_set_status(dev, status);
132 
133 	return 0;
134 dev_error:
135 	printf("%s: failed\n", __func__);
136 	status |= VIRTIO_STAT_FAILED;
137 	virtio_set_status(dev, status);
138 	return -1;
139 }
140 
141 /**
142  * Shutdown the virtio-block device.
143  * @param  dev  pointer to virtio device information
144  */
virtioscsi_shutdown(struct virtio_device * dev)145 void virtioscsi_shutdown(struct virtio_device *dev)
146 {
147 	/* Quiesce device */
148 	virtio_set_status(dev, VIRTIO_STAT_FAILED);
149 
150 	/* Reset device */
151 	virtio_reset_device(dev);
152 }
153