qemu-iotests: Clean up all extents for vmdk
[qemu/cris-port.git] / pc-bios / s390-ccw / virtio.c
blob4d6e48fcbe0668790772d9468c3e752e6c0e5053
1 /*
2 * Virtio driver bits
4 * Copyright (c) 2013 Alexander Graf <agraf@suse.de>
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 */
11 #include "s390-ccw.h"
12 #include "virtio.h"
14 struct vring block;
16 static char chsc_page[PAGE_SIZE] __attribute__((__aligned__(PAGE_SIZE)));
18 static long kvm_hypercall(unsigned long nr, unsigned long param1,
19 unsigned long param2)
21 register ulong r_nr asm("1") = nr;
22 register ulong r_param1 asm("2") = param1;
23 register ulong r_param2 asm("3") = param2;
24 register long retval asm("2");
26 asm volatile ("diag 2,4,0x500"
27 : "=d" (retval)
28 : "d" (r_nr), "0" (r_param1), "r"(r_param2)
29 : "memory", "cc");
31 return retval;
34 static void virtio_notify(struct subchannel_id schid)
36 kvm_hypercall(KVM_S390_VIRTIO_CCW_NOTIFY, *(u32*)&schid, 0);
39 /***********************************************
40 * Virtio functions *
41 ***********************************************/
43 static int drain_irqs(struct subchannel_id schid)
45 struct irb irb = {};
46 int r = 0;
48 while (1) {
49 /* FIXME: make use of TPI, for that enable subchannel and isc */
50 if (tsch(schid, &irb)) {
51 /* Might want to differentiate error codes later on. */
52 if (irb.scsw.cstat) {
53 r = -EIO;
54 } else if (irb.scsw.dstat != 0xc) {
55 r = -EIO;
57 return r;
62 static int run_ccw(struct subchannel_id schid, int cmd, void *ptr, int len)
64 struct ccw1 ccw = {};
65 struct cmd_orb orb = {};
66 struct schib schib;
67 int r;
69 /* start command processing */
70 stsch_err(schid, &schib);
71 schib.scsw.ctrl = SCSW_FCTL_START_FUNC;
72 msch(schid, &schib);
74 /* start subchannel command */
75 orb.fmt = 1;
76 orb.cpa = (u32)(long)&ccw;
77 orb.lpm = 0x80;
79 ccw.cmd_code = cmd;
80 ccw.cda = (long)ptr;
81 ccw.count = len;
83 r = ssch(schid, &orb);
85 * XXX Wait until device is done processing the CCW. For now we can
86 * assume that a simple tsch will have finished the CCW processing,
87 * but the architecture allows for asynchronous operation
89 if (!r) {
90 r = drain_irqs(schid);
92 return r;
95 static void virtio_set_status(struct subchannel_id schid,
96 unsigned long dev_addr)
98 unsigned char status = dev_addr;
99 if (run_ccw(schid, CCW_CMD_WRITE_STATUS, &status, sizeof(status))) {
100 virtio_panic("Could not write status to host!\n");
104 static void virtio_reset(struct subchannel_id schid)
106 run_ccw(schid, CCW_CMD_VDEV_RESET, NULL, 0);
109 static void vring_init(struct vring *vr, unsigned int num, void *p,
110 unsigned long align)
112 debug_print_addr("init p", p);
113 vr->num = num;
114 vr->desc = p;
115 vr->avail = p + num*sizeof(struct vring_desc);
116 vr->used = (void *)(((unsigned long)&vr->avail->ring[num] + align-1)
117 & ~(align - 1));
119 /* Zero out all relevant field */
120 vr->avail->flags = 0;
121 vr->avail->idx = 0;
123 /* We're running with interrupts off anyways, so don't bother */
124 vr->used->flags = VRING_USED_F_NO_NOTIFY;
125 vr->used->idx = 0;
126 vr->used_idx = 0;
128 debug_print_addr("init vr", vr);
131 static void vring_notify(struct subchannel_id schid)
133 virtio_notify(schid);
136 static void vring_send_buf(struct vring *vr, void *p, int len, int flags)
138 /* For follow-up chains we need to keep the first entry point */
139 if (!(flags & VRING_HIDDEN_IS_CHAIN)) {
140 vr->avail->ring[vr->avail->idx % vr->num] = vr->next_idx;
143 vr->desc[vr->next_idx].addr = (ulong)p;
144 vr->desc[vr->next_idx].len = len;
145 vr->desc[vr->next_idx].flags = flags & ~VRING_HIDDEN_IS_CHAIN;
146 vr->desc[vr->next_idx].next = vr->next_idx;
147 vr->desc[vr->next_idx].next++;
148 vr->next_idx++;
150 /* Chains only have a single ID */
151 if (!(flags & VRING_DESC_F_NEXT)) {
152 vr->avail->idx++;
156 static u64 get_clock(void)
158 u64 r;
160 asm volatile("stck %0" : "=Q" (r) : : "cc");
161 return r;
164 static ulong get_second(void)
166 return (get_clock() >> 12) / 1000000;
170 * Wait for the host to reply.
172 * timeout is in seconds if > 0.
174 * Returns 0 on success, 1 on timeout.
176 static int vring_wait_reply(struct vring *vr, int timeout)
178 ulong target_second = get_second() + timeout;
179 struct subchannel_id schid = vr->schid;
180 int r = 0;
182 /* Wait until the used index has moved. */
183 while (vr->used->idx == vr->used_idx) {
184 vring_notify(schid);
185 if (timeout && (get_second() >= target_second)) {
186 r = 1;
187 break;
189 yield();
192 vr->used_idx = vr->used->idx;
193 vr->next_idx = 0;
194 vr->desc[0].len = 0;
195 vr->desc[0].flags = 0;
197 return r;
200 /***********************************************
201 * Virtio block *
202 ***********************************************/
204 static int virtio_read_many(ulong sector, void *load_addr, int sec_num)
206 struct virtio_blk_outhdr out_hdr;
207 u8 status;
208 int r;
210 /* Tell the host we want to read */
211 out_hdr.type = VIRTIO_BLK_T_IN;
212 out_hdr.ioprio = 99;
213 out_hdr.sector = sector;
215 vring_send_buf(&block, &out_hdr, sizeof(out_hdr), VRING_DESC_F_NEXT);
217 /* This is where we want to receive data */
218 vring_send_buf(&block, load_addr, SECTOR_SIZE * sec_num,
219 VRING_DESC_F_WRITE | VRING_HIDDEN_IS_CHAIN |
220 VRING_DESC_F_NEXT);
222 /* status field */
223 vring_send_buf(&block, &status, sizeof(u8), VRING_DESC_F_WRITE |
224 VRING_HIDDEN_IS_CHAIN);
226 /* Now we can tell the host to read */
227 vring_wait_reply(&block, 0);
229 r = drain_irqs(block.schid);
230 if (r) {
231 /* Well, whatever status is supposed to contain... */
232 status = 1;
234 return status;
237 unsigned long virtio_load_direct(ulong rec_list1, ulong rec_list2,
238 ulong subchan_id, void *load_addr)
240 u8 status;
241 int sec = rec_list1;
242 int sec_num = (((rec_list2 >> 32)+ 1) & 0xffff);
243 int sec_len = rec_list2 >> 48;
244 ulong addr = (ulong)load_addr;
246 if (sec_len != SECTOR_SIZE) {
247 return -1;
250 sclp_print(".");
251 status = virtio_read_many(sec, (void*)addr, sec_num);
252 if (status) {
253 virtio_panic("I/O Error");
255 addr += sec_num * SECTOR_SIZE;
257 return addr;
260 int virtio_read(ulong sector, void *load_addr)
262 return virtio_read_many(sector, load_addr, 1);
265 void virtio_setup_block(struct subchannel_id schid)
267 struct vq_info_block info;
268 struct vq_config_block config = {};
270 virtio_reset(schid);
272 config.index = 0;
273 if (run_ccw(schid, CCW_CMD_READ_VQ_CONF, &config, sizeof(config))) {
274 virtio_panic("Could not get block device configuration\n");
276 vring_init(&block, config.num, (void*)(100 * 1024 * 1024),
277 KVM_S390_VIRTIO_RING_ALIGN);
279 info.queue = (100ULL * 1024ULL* 1024ULL);
280 info.align = KVM_S390_VIRTIO_RING_ALIGN;
281 info.index = 0;
282 info.num = config.num;
283 block.schid = schid;
285 if (!run_ccw(schid, CCW_CMD_SET_VQ, &info, sizeof(info))) {
286 virtio_set_status(schid, VIRTIO_CONFIG_S_DRIVER_OK);
290 bool virtio_is_blk(struct subchannel_id schid)
292 int r;
293 struct senseid senseid = {};
295 /* run sense id command */
296 r = run_ccw(schid, CCW_CMD_SENSE_ID, &senseid, sizeof(senseid));
297 if (r) {
298 return false;
300 if ((senseid.cu_type != 0x3832) || (senseid.cu_model != VIRTIO_ID_BLOCK)) {
301 return false;
304 return true;
307 int enable_mss_facility(void)
309 int ret;
310 struct chsc_area_sda *sda_area = (struct chsc_area_sda *) chsc_page;
312 memset(sda_area, 0, PAGE_SIZE);
313 sda_area->request.length = 0x0400;
314 sda_area->request.code = 0x0031;
315 sda_area->operation_code = 0x2;
317 ret = chsc(sda_area);
318 if ((ret == 0) && (sda_area->response.code == 0x0001)) {
319 return 0;
321 return -EIO;