MAINTAINERS: Make section QOM cover hw/core/*bus.c as well
[qemu/armbru.git] / pc-bios / s390-ccw / main.c
blob4e65b411e1d890ba7f8536d7b99f1962782eca15
1 /*
2 * S390 virtio-ccw loading program
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 "helper.h"
13 #include "s390-arch.h"
14 #include "s390-ccw.h"
15 #include "cio.h"
16 #include "virtio.h"
17 #include "dasd-ipl.h"
19 char stack[PAGE_SIZE * 8] __attribute__((__aligned__(PAGE_SIZE)));
20 static SubChannelId blk_schid = { .one = 1 };
21 static char loadparm_str[LOADPARM_LEN + 1];
22 QemuIplParameters qipl;
23 IplParameterBlock iplb __attribute__((__aligned__(PAGE_SIZE)));
24 static bool have_iplb;
25 static uint16_t cutype;
26 LowCore *lowcore; /* Yes, this *is* a pointer to address 0 */
28 #define LOADPARM_PROMPT "PROMPT "
29 #define LOADPARM_EMPTY " "
30 #define BOOT_MENU_FLAG_MASK (QIPL_FLAG_BM_OPTS_CMD | QIPL_FLAG_BM_OPTS_ZIPL)
33 * Principles of Operations (SA22-7832-09) chapter 17 requires that
34 * a subsystem-identification is at 184-187 and bytes 188-191 are zero
35 * after list-directed-IPL and ccw-IPL.
37 void write_subsystem_identification(void)
39 SubChannelId *schid = (SubChannelId *) 184;
40 uint32_t *zeroes = (uint32_t *) 188;
42 *schid = blk_schid;
43 *zeroes = 0;
46 void write_iplb_location(void)
48 lowcore->ptr_iplb = ptr2u32(&iplb);
51 void panic(const char *string)
53 sclp_print(string);
54 disabled_wait();
55 while (1) { }
58 unsigned int get_loadparm_index(void)
60 return atoui(loadparm_str);
64 * Find the subchannel connected to the given device (dev_no) and fill in the
65 * subchannel information block (schib) with the connected subchannel's info.
66 * NOTE: The global variable blk_schid is updated to contain the subchannel
67 * information.
69 * If the caller gives dev_no=-1 then the user did not specify a boot device.
70 * In this case we'll just use the first potentially bootable device we find.
72 static bool find_subch(int dev_no)
74 Schib schib;
75 int i, r;
76 bool is_virtio;
78 for (i = 0; i < 0x10000; i++) {
79 blk_schid.sch_no = i;
80 r = stsch_err(blk_schid, &schib);
81 if ((r == 3) || (r == -EIO)) {
82 break;
84 if (!schib.pmcw.dnv) {
85 continue;
88 enable_subchannel(blk_schid);
89 cutype = cu_type(blk_schid);
92 * Note: we always have to run virtio_is_supported() here to make
93 * sure that the vdev.senseid data gets pre-initialized correctly
95 is_virtio = virtio_is_supported(blk_schid);
97 /* No specific devno given, just return 1st possibly bootable device */
98 if (dev_no < 0) {
99 switch (cutype) {
100 case CU_TYPE_VIRTIO:
101 if (is_virtio) {
103 * Skip net devices since no IPLB is created and therefore
104 * no network bootloader has been loaded
106 if (virtio_get_device_type() != VIRTIO_ID_NET) {
107 return true;
110 continue;
111 case CU_TYPE_DASD_3990:
112 case CU_TYPE_DASD_2107:
113 return true;
114 default:
115 continue;
119 /* Caller asked for a specific devno */
120 if (schib.pmcw.dev == dev_no) {
121 return true;
125 return false;
128 static void menu_setup(void)
130 if (memcmp(loadparm_str, LOADPARM_PROMPT, LOADPARM_LEN) == 0) {
131 menu_set_parms(QIPL_FLAG_BM_OPTS_CMD, 0);
132 return;
135 /* If loadparm was set to any other value, then do not enable menu */
136 if (memcmp(loadparm_str, LOADPARM_EMPTY, LOADPARM_LEN) != 0) {
137 return;
140 switch (iplb.pbt) {
141 case S390_IPL_TYPE_CCW:
142 case S390_IPL_TYPE_QEMU_SCSI:
143 menu_set_parms(qipl.qipl_flags & BOOT_MENU_FLAG_MASK,
144 qipl.boot_menu_timeout);
145 return;
150 * Initialize the channel I/O subsystem so we can talk to our ipl/boot device.
152 static void css_setup(void)
155 * Unconditionally enable mss support. In every sane configuration this
156 * will succeed; and even if it doesn't, stsch_err() can handle it.
158 enable_mss_facility();
162 * Collect various pieces of information from the hypervisor/hardware that
163 * we'll use to determine exactly how we'll boot.
165 static void boot_setup(void)
167 char lpmsg[] = "LOADPARM=[________]\n";
169 sclp_get_loadparm_ascii(loadparm_str);
170 memcpy(lpmsg + 10, loadparm_str, 8);
171 sclp_print(lpmsg);
173 have_iplb = store_iplb(&iplb);
176 static void find_boot_device(void)
178 VDev *vdev = virtio_get_device();
179 int ssid;
180 bool found;
182 if (!have_iplb) {
183 for (ssid = 0; ssid < 0x3; ssid++) {
184 blk_schid.ssid = ssid;
185 found = find_subch(-1);
186 if (found) {
187 return;
190 panic("Could not find a suitable boot device (none specified)\n");
193 switch (iplb.pbt) {
194 case S390_IPL_TYPE_CCW:
195 debug_print_int("device no. ", iplb.ccw.devno);
196 blk_schid.ssid = iplb.ccw.ssid & 0x3;
197 debug_print_int("ssid ", blk_schid.ssid);
198 found = find_subch(iplb.ccw.devno);
199 break;
200 case S390_IPL_TYPE_QEMU_SCSI:
201 vdev->scsi_device_selected = true;
202 vdev->selected_scsi_device.channel = iplb.scsi.channel;
203 vdev->selected_scsi_device.target = iplb.scsi.target;
204 vdev->selected_scsi_device.lun = iplb.scsi.lun;
205 blk_schid.ssid = iplb.scsi.ssid & 0x3;
206 found = find_subch(iplb.scsi.devno);
207 break;
208 default:
209 panic("List-directed IPL not supported yet!\n");
212 IPL_assert(found, "Boot device not found\n");
215 static void virtio_setup(void)
217 VDev *vdev = virtio_get_device();
218 QemuIplParameters *early_qipl = (QemuIplParameters *)QIPL_ADDRESS;
220 memcpy(&qipl, early_qipl, sizeof(QemuIplParameters));
222 if (have_iplb) {
223 menu_setup();
226 if (virtio_get_device_type() == VIRTIO_ID_NET) {
227 sclp_print("Network boot device detected\n");
228 vdev->netboot_start_addr = qipl.netboot_start_addr;
229 } else {
230 virtio_blk_setup_device(blk_schid);
231 IPL_assert(virtio_ipl_disk_is_valid(), "No valid IPL device detected");
235 int main(void)
237 sclp_setup();
238 css_setup();
239 boot_setup();
240 find_boot_device();
241 enable_subchannel(blk_schid);
243 switch (cutype) {
244 case CU_TYPE_DASD_3990:
245 case CU_TYPE_DASD_2107:
246 dasd_ipl(blk_schid, cutype); /* no return */
247 break;
248 case CU_TYPE_VIRTIO:
249 virtio_setup();
250 zipl_load(); /* no return */
251 break;
252 default:
253 print_int("Attempting to boot from unexpected device type", cutype);
254 panic("");
257 panic("Failed to load OS from hard disk\n");
258 return 0; /* make compiler happy */