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