]> git.proxmox.com Git - qemu.git/blob - pc-bios/s390-ccw/virtio.c
1968fc6619ef6769f49949e831ee9a5256326c5c
[qemu.git] / pc-bios / s390-ccw / virtio.c
1 /*
2 * Virtio driver bits
3 *
4 * Copyright (c) 2013 Alexander Graf <agraf@suse.de>
5 *
6 * This work is licensed under the terms of the GNU GPL, version 2 or (at
7 * your option) any later version. See the COPYING file in the top-level
8 * directory.
9 */
10
11 #include "s390-ccw.h"
12 #include "virtio.h"
13
14 struct vring block;
15
16 static long kvm_hypercall(unsigned long nr, unsigned long param1,
17 unsigned long param2)
18 {
19 register ulong r_nr asm("1") = nr;
20 register ulong r_param1 asm("2") = param1;
21 register ulong r_param2 asm("3") = param2;
22 register long retval asm("2");
23
24 asm volatile ("diag 2,4,0x500"
25 : "=d" (retval)
26 : "d" (r_nr), "0" (r_param1), "r"(r_param2)
27 : "memory", "cc");
28
29 return retval;
30 }
31
32 static void virtio_notify(struct subchannel_id schid)
33 {
34 kvm_hypercall(KVM_S390_VIRTIO_CCW_NOTIFY, *(u32*)&schid, 0);
35 }
36
37 /***********************************************
38 * Virtio functions *
39 ***********************************************/
40
41 static void drain_irqs(struct subchannel_id schid)
42 {
43 struct irb irb = {};
44 while (1) {
45 if (tsch(schid, &irb)) {
46 return;
47 }
48 }
49 }
50
51 static int run_ccw(struct subchannel_id schid, int cmd, void *ptr, int len)
52 {
53 struct ccw1 ccw = {};
54 struct cmd_orb orb = {};
55 struct schib schib;
56 int r;
57
58 /* start command processing */
59 stsch_err(schid, &schib);
60 schib.scsw.ctrl = SCSW_FCTL_START_FUNC;
61 msch(schid, &schib);
62
63 /* start subchannel command */
64 orb.fmt = 1;
65 orb.cpa = (u32)(long)&ccw;
66 orb.lpm = 0x80;
67
68 ccw.cmd_code = cmd;
69 ccw.cda = (long)ptr;
70 ccw.count = len;
71
72 r = ssch(schid, &orb);
73 /*
74 * XXX Wait until device is done processing the CCW. For now we can
75 * assume that a simple tsch will have finished the CCW processing,
76 * but the architecture allows for asynchronous operation
77 */
78 drain_irqs(schid);
79 return r;
80 }
81
82 static void virtio_set_status(struct subchannel_id schid,
83 unsigned long dev_addr)
84 {
85 unsigned char status = dev_addr;
86 run_ccw(schid, CCW_CMD_WRITE_STATUS, &status, sizeof(status));
87 }
88
89 static void virtio_reset(struct subchannel_id schid)
90 {
91 run_ccw(schid, CCW_CMD_VDEV_RESET, NULL, 0);
92 }
93
94 static void vring_init(struct vring *vr, unsigned int num, void *p,
95 unsigned long align)
96 {
97 debug_print_addr("init p", p);
98 vr->num = num;
99 vr->desc = p;
100 vr->avail = p + num*sizeof(struct vring_desc);
101 vr->used = (void *)(((unsigned long)&vr->avail->ring[num] + align-1)
102 & ~(align - 1));
103
104 /* We're running with interrupts off anyways, so don't bother */
105 vr->used->flags = VRING_USED_F_NO_NOTIFY;
106
107 debug_print_addr("init vr", vr);
108 }
109
110 static void vring_notify(struct subchannel_id schid)
111 {
112 virtio_notify(schid);
113 }
114
115 static void vring_send_buf(struct vring *vr, void *p, int len, int flags)
116 {
117 /* For follow-up chains we need to keep the first entry point */
118 if (!(flags & VRING_HIDDEN_IS_CHAIN)) {
119 vr->avail->ring[vr->avail->idx % vr->num] = vr->next_idx;
120 }
121
122 vr->desc[vr->next_idx].addr = (ulong)p;
123 vr->desc[vr->next_idx].len = len;
124 vr->desc[vr->next_idx].flags = flags & ~VRING_HIDDEN_IS_CHAIN;
125 vr->desc[vr->next_idx].next = vr->next_idx;
126 vr->desc[vr->next_idx].next++;
127 vr->next_idx++;
128
129 /* Chains only have a single ID */
130 if (!(flags & VRING_DESC_F_NEXT)) {
131 vr->avail->idx++;
132 }
133
134 vr->used->idx = vr->next_idx;
135 }
136
137 static u64 get_clock(void)
138 {
139 u64 r;
140
141 asm volatile("stck %0" : "=Q" (r) : : "cc");
142 return r;
143 }
144
145 static ulong get_second(void)
146 {
147 return (get_clock() >> 12) / 1000000;
148 }
149
150 /*
151 * Wait for the host to reply.
152 *
153 * timeout is in seconds if > 0.
154 *
155 * Returns 0 on success, 1 on timeout.
156 */
157 static int vring_wait_reply(struct vring *vr, int timeout)
158 {
159 ulong target_second = get_second() + timeout;
160 struct subchannel_id schid = vr->schid;
161 int r = 0;
162
163 while (vr->used->idx == vr->next_idx) {
164 vring_notify(schid);
165 if (timeout && (get_second() >= target_second)) {
166 r = 1;
167 break;
168 }
169 yield();
170 }
171
172 vr->next_idx = 0;
173 vr->desc[0].len = 0;
174 vr->desc[0].flags = 0;
175
176 return r;
177 }
178
179 /***********************************************
180 * Virtio block *
181 ***********************************************/
182
183 static int virtio_read_many(ulong sector, void *load_addr, int sec_num)
184 {
185 struct virtio_blk_outhdr out_hdr;
186 u8 status;
187
188 /* Tell the host we want to read */
189 out_hdr.type = VIRTIO_BLK_T_IN;
190 out_hdr.ioprio = 99;
191 out_hdr.sector = sector;
192
193 vring_send_buf(&block, &out_hdr, sizeof(out_hdr), VRING_DESC_F_NEXT);
194
195 /* This is where we want to receive data */
196 vring_send_buf(&block, load_addr, SECTOR_SIZE * sec_num,
197 VRING_DESC_F_WRITE | VRING_HIDDEN_IS_CHAIN |
198 VRING_DESC_F_NEXT);
199
200 /* status field */
201 vring_send_buf(&block, &status, sizeof(u8), VRING_DESC_F_WRITE |
202 VRING_HIDDEN_IS_CHAIN);
203
204 /* Now we can tell the host to read */
205 vring_wait_reply(&block, 0);
206
207 drain_irqs(block.schid);
208
209 return status;
210 }
211
212 unsigned long virtio_load_direct(ulong rec_list1, ulong rec_list2,
213 ulong subchan_id, void *load_addr)
214 {
215 u8 status;
216 int sec = rec_list1;
217 int sec_num = (((rec_list2 >> 32)+ 1) & 0xffff);
218 int sec_len = rec_list2 >> 48;
219 ulong addr = (ulong)load_addr;
220
221 if (sec_len != SECTOR_SIZE) {
222 return -1;
223 }
224
225 sclp_print(".");
226 status = virtio_read_many(sec, (void*)addr, sec_num);
227 if (status) {
228 virtio_panic("I/O Error");
229 }
230 addr += sec_num * SECTOR_SIZE;
231
232 return addr;
233 }
234
235 int virtio_read(ulong sector, void *load_addr)
236 {
237 return virtio_read_many(sector, load_addr, 1);
238 }
239
240 void virtio_setup_block(struct subchannel_id schid)
241 {
242 struct vq_info_block info;
243
244 virtio_reset(schid);
245
246 /* XXX need to fetch the 128 from host */
247 vring_init(&block, 128, (void*)(100 * 1024 * 1024),
248 KVM_S390_VIRTIO_RING_ALIGN);
249
250 info.queue = (100ULL * 1024ULL* 1024ULL);
251 info.align = KVM_S390_VIRTIO_RING_ALIGN;
252 info.index = 0;
253 info.num = 128;
254 block.schid = schid;
255
256 run_ccw(schid, CCW_CMD_SET_VQ, &info, sizeof(info));
257 virtio_set_status(schid, VIRTIO_CONFIG_S_DRIVER_OK);
258 }
259
260 bool virtio_is_blk(struct subchannel_id schid)
261 {
262 int r;
263 struct senseid senseid = {};
264
265 /* run sense id command */
266 r = run_ccw(schid, CCW_CMD_SENSE_ID, &senseid, sizeof(senseid));
267 if (r) {
268 return false;
269 }
270 if ((senseid.cu_type != 0x3832) || (senseid.cu_model != VIRTIO_ID_BLOCK)) {
271 return false;
272 }
273
274 return true;
275 }
276