vscsi: send the CHECK_CONDITION status down together with autosense data
[qemu.git] / hw / s390-virtio.c
blobacbf02604bf435a145862c736f758823fcfe194c
1 /*
2 * QEMU S390 virtio target
4 * Copyright (c) 2009 Alexander Graf <agraf@suse.de>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
20 #include "hw.h"
21 #include "block.h"
22 #include "blockdev.h"
23 #include "sysemu.h"
24 #include "net.h"
25 #include "boards.h"
26 #include "monitor.h"
27 #include "loader.h"
28 #include "elf.h"
29 #include "hw/virtio.h"
30 #include "hw/sysbus.h"
31 #include "kvm.h"
33 #include "hw/s390-virtio-bus.h"
35 //#define DEBUG_S390
37 #ifdef DEBUG_S390
38 #define dprintf(fmt, ...) \
39 do { fprintf(stderr, fmt, ## __VA_ARGS__); } while (0)
40 #else
41 #define dprintf(fmt, ...) \
42 do { } while (0)
43 #endif
45 #define KVM_S390_VIRTIO_NOTIFY 0
46 #define KVM_S390_VIRTIO_RESET 1
47 #define KVM_S390_VIRTIO_SET_STATUS 2
49 #define KERN_IMAGE_START 0x010000UL
50 #define KERN_PARM_AREA 0x010480UL
51 #define INITRD_START 0x800000UL
52 #define INITRD_PARM_START 0x010408UL
53 #define INITRD_PARM_SIZE 0x010410UL
54 #define PARMFILE_START 0x001000UL
56 #define ZIPL_START 0x009000UL
57 #define ZIPL_LOAD_ADDR 0x009000UL
58 #define ZIPL_FILENAME "s390-zipl.rom"
60 #define MAX_BLK_DEVS 10
62 static VirtIOS390Bus *s390_bus;
63 static CPUState **ipi_states;
65 void irq_info(Monitor *mon);
66 void pic_info(Monitor *mon);
68 void irq_info(Monitor *mon)
72 void pic_info(Monitor *mon)
76 CPUState *s390_cpu_addr2state(uint16_t cpu_addr)
78 if (cpu_addr >= smp_cpus) {
79 return NULL;
82 return ipi_states[cpu_addr];
85 int s390_virtio_hypercall(CPUState *env, uint64_t mem, uint64_t hypercall)
87 int r = 0, i;
89 dprintf("KVM hypercall: %ld\n", hypercall);
90 switch (hypercall) {
91 case KVM_S390_VIRTIO_NOTIFY:
92 if (mem > ram_size) {
93 VirtIOS390Device *dev = s390_virtio_bus_find_vring(s390_bus,
94 mem, &i);
95 if (dev) {
96 virtio_queue_notify(dev->vdev, i);
97 } else {
98 r = -EINVAL;
100 } else {
101 /* Early printk */
103 break;
104 case KVM_S390_VIRTIO_RESET:
106 VirtIOS390Device *dev;
108 dev = s390_virtio_bus_find_mem(s390_bus, mem);
109 virtio_reset(dev->vdev);
110 s390_virtio_device_sync(dev);
111 break;
113 case KVM_S390_VIRTIO_SET_STATUS:
115 VirtIOS390Device *dev;
117 dev = s390_virtio_bus_find_mem(s390_bus, mem);
118 if (dev) {
119 s390_virtio_device_update_status(dev);
120 } else {
121 r = -EINVAL;
123 break;
125 default:
126 r = -EINVAL;
127 break;
130 return r;
133 /* PC hardware initialisation */
134 static void s390_init(ram_addr_t my_ram_size,
135 const char *boot_device,
136 const char *kernel_filename,
137 const char *kernel_cmdline,
138 const char *initrd_filename,
139 const char *cpu_model)
141 CPUState *env = NULL;
142 ram_addr_t ram_addr;
143 ram_addr_t kernel_size = 0;
144 ram_addr_t initrd_offset;
145 ram_addr_t initrd_size = 0;
146 int shift = 0;
147 uint8_t *storage_keys;
148 int i;
150 /* s390x ram size detection needs a 16bit multiplier + an increment. So
151 guests > 64GB can be specified in 2MB steps etc. */
152 while ((my_ram_size >> (20 + shift)) > 65535) {
153 shift++;
155 my_ram_size = my_ram_size >> (20 + shift) << (20 + shift);
157 /* lets propagate the changed ram size into the global variable. */
158 ram_size = my_ram_size;
160 /* get a BUS */
161 s390_bus = s390_virtio_bus_init(&my_ram_size);
163 /* allocate RAM */
164 ram_addr = qemu_ram_alloc(NULL, "s390.ram", my_ram_size);
165 cpu_register_physical_memory(0, my_ram_size, ram_addr);
167 /* allocate storage keys */
168 storage_keys = g_malloc0(my_ram_size / TARGET_PAGE_SIZE);
170 /* init CPUs */
171 if (cpu_model == NULL) {
172 cpu_model = "host";
175 ipi_states = g_malloc(sizeof(CPUState *) * smp_cpus);
177 for (i = 0; i < smp_cpus; i++) {
178 CPUState *tmp_env;
180 tmp_env = cpu_init(cpu_model);
181 if (!env) {
182 env = tmp_env;
184 ipi_states[i] = tmp_env;
185 tmp_env->halted = 1;
186 tmp_env->exception_index = EXCP_HLT;
187 tmp_env->storage_keys = storage_keys;
190 env->halted = 0;
191 env->exception_index = 0;
193 if (kernel_filename) {
194 kernel_size = load_image(kernel_filename, qemu_get_ram_ptr(0));
196 if (lduw_be_phys(KERN_IMAGE_START) != 0x0dd0) {
197 fprintf(stderr, "Specified image is not an s390 boot image\n");
198 exit(1);
201 env->psw.addr = KERN_IMAGE_START;
202 env->psw.mask = 0x0000000180000000ULL;
203 } else {
204 ram_addr_t bios_size = 0;
205 char *bios_filename;
207 /* Load zipl bootloader */
208 if (bios_name == NULL) {
209 bios_name = ZIPL_FILENAME;
212 bios_filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name);
213 bios_size = load_image(bios_filename, qemu_get_ram_ptr(ZIPL_LOAD_ADDR));
214 g_free(bios_filename);
216 if ((long)bios_size < 0) {
217 hw_error("could not load bootloader '%s'\n", bios_name);
220 if (bios_size > 4096) {
221 hw_error("stage1 bootloader is > 4k\n");
224 env->psw.addr = ZIPL_START;
225 env->psw.mask = 0x0000000180000000ULL;
228 if (initrd_filename) {
229 initrd_offset = INITRD_START;
230 while (kernel_size + 0x100000 > initrd_offset) {
231 initrd_offset += 0x100000;
233 initrd_size = load_image(initrd_filename, qemu_get_ram_ptr(initrd_offset));
235 stq_be_phys(INITRD_PARM_START, initrd_offset);
236 stq_be_phys(INITRD_PARM_SIZE, initrd_size);
239 if (kernel_cmdline) {
240 cpu_physical_memory_write(KERN_PARM_AREA, kernel_cmdline,
241 strlen(kernel_cmdline));
244 /* Create VirtIO network adapters */
245 for(i = 0; i < nb_nics; i++) {
246 NICInfo *nd = &nd_table[i];
247 DeviceState *dev;
249 if (!nd->model) {
250 nd->model = g_strdup("virtio");
253 if (strcmp(nd->model, "virtio")) {
254 fprintf(stderr, "S390 only supports VirtIO nics\n");
255 exit(1);
258 dev = qdev_create((BusState *)s390_bus, "virtio-net-s390");
259 qdev_set_nic_properties(dev, nd);
260 qdev_init_nofail(dev);
263 /* Create VirtIO disk drives */
264 for(i = 0; i < MAX_BLK_DEVS; i++) {
265 DriveInfo *dinfo;
266 DeviceState *dev;
268 dinfo = drive_get(IF_IDE, 0, i);
269 if (!dinfo) {
270 continue;
273 dev = qdev_create((BusState *)s390_bus, "virtio-blk-s390");
274 qdev_prop_set_drive_nofail(dev, "drive", dinfo->bdrv);
275 qdev_init_nofail(dev);
279 static QEMUMachine s390_machine = {
280 .name = "s390-virtio",
281 .alias = "s390",
282 .desc = "VirtIO based S390 machine",
283 .init = s390_init,
284 .no_serial = 1,
285 .no_parallel = 1,
286 .use_virtcon = 1,
287 .no_vga = 1,
288 .max_cpus = 255,
289 .is_default = 1,
292 static void s390_machine_init(void)
294 qemu_register_machine(&s390_machine);
297 machine_init(s390_machine_init);