migration/rdma: Plug memory leaks in qemu_rdma_registration_stop()
[qemu/armbru.git] / pc-bios / s390-ccw / virtio-blkdev.c
blob11c56261cadec4d4be7409b595ba82ea626782cb
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 "libc.h"
12 #include "s390-ccw.h"
13 #include "virtio.h"
14 #include "virtio-scsi.h"
16 static int virtio_blk_read_many(VDev *vdev, ulong sector, void *load_addr,
17 int sec_num)
19 VirtioBlkOuthdr out_hdr;
20 u8 status;
21 VRing *vr = &vdev->vrings[vdev->cmd_vr_idx];
23 /* Tell the host we want to read */
24 out_hdr.type = VIRTIO_BLK_T_IN;
25 out_hdr.ioprio = 99;
26 out_hdr.sector = virtio_sector_adjust(sector);
28 vring_send_buf(vr, &out_hdr, sizeof(out_hdr), VRING_DESC_F_NEXT);
30 /* This is where we want to receive data */
31 vring_send_buf(vr, load_addr, virtio_get_block_size() * sec_num,
32 VRING_DESC_F_WRITE | VRING_HIDDEN_IS_CHAIN |
33 VRING_DESC_F_NEXT);
35 /* status field */
36 vring_send_buf(vr, &status, sizeof(u8),
37 VRING_DESC_F_WRITE | VRING_HIDDEN_IS_CHAIN);
39 /* Now we can tell the host to read */
40 vring_wait_reply();
42 if (drain_irqs(vr->schid)) {
43 /* Well, whatever status is supposed to contain... */
44 status = 1;
46 return status;
49 int virtio_read_many(ulong sector, void *load_addr, int sec_num)
51 VDev *vdev = virtio_get_device();
53 switch (vdev->senseid.cu_model) {
54 case VIRTIO_ID_BLOCK:
55 return virtio_blk_read_many(vdev, sector, load_addr, sec_num);
56 case VIRTIO_ID_SCSI:
57 return virtio_scsi_read_many(vdev, sector, load_addr, sec_num);
59 panic("\n! No readable IPL device !\n");
60 return -1;
63 unsigned long virtio_load_direct(ulong rec_list1, ulong rec_list2,
64 ulong subchan_id, void *load_addr)
66 u8 status;
67 int sec = rec_list1;
68 int sec_num = ((rec_list2 >> 32) & 0xffff) + 1;
69 int sec_len = rec_list2 >> 48;
70 ulong addr = (ulong)load_addr;
72 if (sec_len != virtio_get_block_size()) {
73 return -1;
76 sclp_print(".");
77 status = virtio_read_many(sec, (void *)addr, sec_num);
78 if (status) {
79 panic("I/O Error");
81 addr += sec_num * virtio_get_block_size();
83 return addr;
86 int virtio_read(ulong sector, void *load_addr)
88 return virtio_read_many(sector, load_addr, 1);
92 * Other supported value pairs, if any, would need to be added here.
93 * Note: head count is always 15.
95 static inline u8 virtio_eckd_sectors_for_block_size(int size)
97 switch (size) {
98 case 512:
99 return 49;
100 case 1024:
101 return 33;
102 case 2048:
103 return 21;
104 case 4096:
105 return 12;
107 return 0;
110 VirtioGDN virtio_guessed_disk_nature(void)
112 return virtio_get_device()->guessed_disk_nature;
115 void virtio_assume_scsi(void)
117 VDev *vdev = virtio_get_device();
119 switch (vdev->senseid.cu_model) {
120 case VIRTIO_ID_BLOCK:
121 vdev->guessed_disk_nature = VIRTIO_GDN_SCSI;
122 vdev->config.blk.blk_size = VIRTIO_SCSI_BLOCK_SIZE;
123 vdev->config.blk.physical_block_exp = 0;
124 vdev->blk_factor = 1;
125 break;
126 case VIRTIO_ID_SCSI:
127 vdev->scsi_block_size = VIRTIO_SCSI_BLOCK_SIZE;
128 break;
132 void virtio_assume_iso9660(void)
134 VDev *vdev = virtio_get_device();
136 switch (vdev->senseid.cu_model) {
137 case VIRTIO_ID_BLOCK:
138 vdev->guessed_disk_nature = VIRTIO_GDN_SCSI;
139 vdev->config.blk.blk_size = VIRTIO_ISO_BLOCK_SIZE;
140 vdev->config.blk.physical_block_exp = 0;
141 vdev->blk_factor = VIRTIO_ISO_BLOCK_SIZE / VIRTIO_SECTOR_SIZE;
142 break;
143 case VIRTIO_ID_SCSI:
144 vdev->scsi_block_size = VIRTIO_ISO_BLOCK_SIZE;
145 break;
149 void virtio_assume_eckd(void)
151 VDev *vdev = virtio_get_device();
153 vdev->guessed_disk_nature = VIRTIO_GDN_DASD;
154 vdev->blk_factor = 1;
155 vdev->config.blk.physical_block_exp = 0;
156 switch (vdev->senseid.cu_model) {
157 case VIRTIO_ID_BLOCK:
158 vdev->config.blk.blk_size = 4096;
159 break;
160 case VIRTIO_ID_SCSI:
161 vdev->config.blk.blk_size = vdev->scsi_block_size;
162 break;
164 vdev->config.blk.geometry.heads = 15;
165 vdev->config.blk.geometry.sectors =
166 virtio_eckd_sectors_for_block_size(vdev->config.blk.blk_size);
169 bool virtio_disk_is_scsi(void)
171 VDev *vdev = virtio_get_device();
173 if (vdev->guessed_disk_nature == VIRTIO_GDN_SCSI) {
174 return true;
176 switch (vdev->senseid.cu_model) {
177 case VIRTIO_ID_BLOCK:
178 return (vdev->config.blk.geometry.heads == 255)
179 && (vdev->config.blk.geometry.sectors == 63)
180 && (virtio_get_block_size() == VIRTIO_SCSI_BLOCK_SIZE);
181 case VIRTIO_ID_SCSI:
182 return true;
184 return false;
187 bool virtio_disk_is_eckd(void)
189 VDev *vdev = virtio_get_device();
190 const int block_size = virtio_get_block_size();
192 if (vdev->guessed_disk_nature == VIRTIO_GDN_DASD) {
193 return true;
195 switch (vdev->senseid.cu_model) {
196 case VIRTIO_ID_BLOCK:
197 return (vdev->config.blk.geometry.heads == 15)
198 && (vdev->config.blk.geometry.sectors ==
199 virtio_eckd_sectors_for_block_size(block_size));
200 case VIRTIO_ID_SCSI:
201 return false;
203 return false;
206 bool virtio_ipl_disk_is_valid(void)
208 return virtio_disk_is_scsi() || virtio_disk_is_eckd();
211 int virtio_get_block_size(void)
213 VDev *vdev = virtio_get_device();
215 switch (vdev->senseid.cu_model) {
216 case VIRTIO_ID_BLOCK:
217 return vdev->config.blk.blk_size << vdev->config.blk.physical_block_exp;
218 case VIRTIO_ID_SCSI:
219 return vdev->scsi_block_size;
221 return 0;
224 uint8_t virtio_get_heads(void)
226 VDev *vdev = virtio_get_device();
228 switch (vdev->senseid.cu_model) {
229 case VIRTIO_ID_BLOCK:
230 return vdev->config.blk.geometry.heads;
231 case VIRTIO_ID_SCSI:
232 return vdev->guessed_disk_nature == VIRTIO_GDN_DASD
233 ? vdev->config.blk.geometry.heads : 255;
235 return 0;
238 uint8_t virtio_get_sectors(void)
240 VDev *vdev = virtio_get_device();
242 switch (vdev->senseid.cu_model) {
243 case VIRTIO_ID_BLOCK:
244 return vdev->config.blk.geometry.sectors;
245 case VIRTIO_ID_SCSI:
246 return vdev->guessed_disk_nature == VIRTIO_GDN_DASD
247 ? vdev->config.blk.geometry.sectors : 63;
249 return 0;
252 uint64_t virtio_get_blocks(void)
254 VDev *vdev = virtio_get_device();
255 const uint64_t factor = virtio_get_block_size() / VIRTIO_SECTOR_SIZE;
257 switch (vdev->senseid.cu_model) {
258 case VIRTIO_ID_BLOCK:
259 return vdev->config.blk.capacity / factor;
260 case VIRTIO_ID_SCSI:
261 return vdev->scsi_last_block / factor;
263 return 0;
266 void virtio_blk_setup_device(SubChannelId schid)
268 VDev *vdev = virtio_get_device();
270 vdev->schid = schid;
271 virtio_setup_ccw(vdev);
273 switch (vdev->senseid.cu_model) {
274 case VIRTIO_ID_BLOCK:
275 sclp_print("Using virtio-blk.\n");
276 if (!virtio_ipl_disk_is_valid()) {
277 /* make sure all getters but blocksize return 0 for
278 * invalid IPL disk
280 memset(&vdev->config.blk, 0, sizeof(vdev->config.blk));
281 virtio_assume_scsi();
283 break;
284 case VIRTIO_ID_SCSI:
285 IPL_assert(vdev->config.scsi.sense_size == VIRTIO_SCSI_SENSE_SIZE,
286 "Config: sense size mismatch");
287 IPL_assert(vdev->config.scsi.cdb_size == VIRTIO_SCSI_CDB_SIZE,
288 "Config: CDB size mismatch");
290 sclp_print("Using virtio-scsi.\n");
291 virtio_scsi_setup(vdev);
292 break;
293 default:
294 panic("\n! No IPL device available !\n");