xref: /openbmc/qemu/pc-bios/s390-ccw/main.c (revision bb185de4)
1 /*
2  * S390 virtio-ccw loading program
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 <stdlib.h>
12 #include <string.h>
13 #include <stdio.h>
14 #include "helper.h"
15 #include "s390-arch.h"
16 #include "s390-ccw.h"
17 #include "cio.h"
18 #include "virtio.h"
19 #include "virtio-scsi.h"
20 #include "dasd-ipl.h"
21 
22 static SubChannelId blk_schid = { .one = 1 };
23 static char loadparm_str[LOADPARM_LEN + 1];
24 QemuIplParameters qipl;
25 IplParameterBlock iplb __attribute__((__aligned__(PAGE_SIZE)));
26 static bool have_iplb;
27 static uint16_t cutype;
28 LowCore *lowcore; /* Yes, this *is* a pointer to address 0 */
29 
30 #define LOADPARM_PROMPT "PROMPT  "
31 #define LOADPARM_EMPTY  "        "
32 #define BOOT_MENU_FLAG_MASK (QIPL_FLAG_BM_OPTS_CMD | QIPL_FLAG_BM_OPTS_ZIPL)
33 
34 /*
35  * Principles of Operations (SA22-7832-09) chapter 17 requires that
36  * a subsystem-identification is at 184-187 and bytes 188-191 are zero
37  * after list-directed-IPL and ccw-IPL.
38  */
39 void write_subsystem_identification(void)
40 {
41     if (cutype == CU_TYPE_VIRTIO && virtio_get_device_type() == VIRTIO_ID_NET) {
42         lowcore->subchannel_id = net_schid.sch_id;
43         lowcore->subchannel_nr = net_schid.sch_no;
44     } else {
45         lowcore->subchannel_id = blk_schid.sch_id;
46         lowcore->subchannel_nr = blk_schid.sch_no;
47     }
48     lowcore->io_int_parm = 0;
49 }
50 
51 void write_iplb_location(void)
52 {
53     if (cutype == CU_TYPE_VIRTIO && virtio_get_device_type() != VIRTIO_ID_NET) {
54         lowcore->ptr_iplb = ptr2u32(&iplb);
55     }
56 }
57 
58 unsigned int get_loadparm_index(void)
59 {
60     return atoi(loadparm_str);
61 }
62 
63 static int is_dev_possibly_bootable(int dev_no, int sch_no)
64 {
65     bool is_virtio;
66     Schib schib;
67     int r;
68 
69     blk_schid.sch_no = sch_no;
70     r = stsch_err(blk_schid, &schib);
71     if (r == 3 || r == -EIO) {
72         return -ENODEV;
73     }
74     if (!schib.pmcw.dnv) {
75         return false;
76     }
77 
78     enable_subchannel(blk_schid);
79     cutype = cu_type(blk_schid);
80     if (cutype == CU_TYPE_UNKNOWN) {
81         return -EIO;
82     }
83 
84     /*
85      * Note: we always have to run virtio_is_supported() here to make
86      * sure that the vdev.senseid data gets pre-initialized correctly
87      */
88     is_virtio = virtio_is_supported(blk_schid);
89 
90     /* No specific devno given, just return whether the device is possibly bootable */
91     if (dev_no < 0) {
92         switch (cutype) {
93         case CU_TYPE_VIRTIO:
94             if (is_virtio) {
95                 /*
96                  * Skip net devices since no IPLB is created and therefore
97                  * no network bootloader has been loaded
98                  */
99                 if (virtio_get_device_type() != VIRTIO_ID_NET) {
100                     return true;
101                 }
102             }
103             return false;
104         case CU_TYPE_DASD_3990:
105         case CU_TYPE_DASD_2107:
106             return true;
107         default:
108             return false;
109         }
110     }
111 
112     /* Caller asked for a specific devno */
113     if (schib.pmcw.dev == dev_no) {
114         return true;
115     }
116 
117     return false;
118 }
119 
120 /*
121  * Find the subchannel connected to the given device (dev_no) and fill in the
122  * subchannel information block (schib) with the connected subchannel's info.
123  * NOTE: The global variable blk_schid is updated to contain the subchannel
124  * information.
125  *
126  * If the caller gives dev_no=-1 then the user did not specify a boot device.
127  * In this case we'll just use the first potentially bootable device we find.
128  */
129 static bool find_subch(int dev_no)
130 {
131     int i, r;
132 
133     for (i = 0; i < 0x10000; i++) {
134         r = is_dev_possibly_bootable(dev_no, i);
135         if (r < 0) {
136             break;
137         }
138         if (r == true) {
139             return true;
140         }
141     }
142 
143     return false;
144 }
145 
146 static void menu_setup(void)
147 {
148     if (memcmp(loadparm_str, LOADPARM_PROMPT, LOADPARM_LEN) == 0) {
149         menu_set_parms(QIPL_FLAG_BM_OPTS_CMD, 0);
150         return;
151     }
152 
153     /* If loadparm was set to any other value, then do not enable menu */
154     if (memcmp(loadparm_str, LOADPARM_EMPTY, LOADPARM_LEN) != 0) {
155         return;
156     }
157 
158     switch (iplb.pbt) {
159     case S390_IPL_TYPE_CCW:
160     case S390_IPL_TYPE_QEMU_SCSI:
161         menu_set_parms(qipl.qipl_flags & BOOT_MENU_FLAG_MASK,
162                        qipl.boot_menu_timeout);
163         return;
164     }
165 }
166 
167 /*
168  * Initialize the channel I/O subsystem so we can talk to our ipl/boot device.
169  */
170 static void css_setup(void)
171 {
172     /*
173      * Unconditionally enable mss support. In every sane configuration this
174      * will succeed; and even if it doesn't, stsch_err() can handle it.
175      */
176     enable_mss_facility();
177 }
178 
179 /*
180  * Collect various pieces of information from the hypervisor/hardware that
181  * we'll use to determine exactly how we'll boot.
182  */
183 static void boot_setup(void)
184 {
185     char lpmsg[] = "LOADPARM=[________]\n";
186     have_iplb = store_iplb(&iplb);
187 
188     if (memcmp(iplb.loadparm, NO_LOADPARM, LOADPARM_LEN) != 0) {
189         ebcdic_to_ascii((char *) iplb.loadparm, loadparm_str, LOADPARM_LEN);
190     } else {
191         sclp_get_loadparm_ascii(loadparm_str);
192     }
193 
194     memcpy(lpmsg + 10, loadparm_str, 8);
195     puts(lpmsg);
196 
197     /*
198      * Clear out any potential S390EP magic (see jump_to_low_kernel()),
199      * so we don't taint our decision-making process during a reboot.
200      */
201     memset((char *)S390EP, 0, 6);
202 }
203 
204 static bool find_boot_device(void)
205 {
206     VDev *vdev = virtio_get_device();
207     bool found = false;
208 
209     switch (iplb.pbt) {
210     case S390_IPL_TYPE_CCW:
211         debug_print_int("device no. ", iplb.ccw.devno);
212         blk_schid.ssid = iplb.ccw.ssid & 0x3;
213         debug_print_int("ssid ", blk_schid.ssid);
214         found = find_subch(iplb.ccw.devno);
215         break;
216     case S390_IPL_TYPE_QEMU_SCSI:
217         vdev->scsi_device_selected = true;
218         vdev->selected_scsi_device.channel = iplb.scsi.channel;
219         vdev->selected_scsi_device.target = iplb.scsi.target;
220         vdev->selected_scsi_device.lun = iplb.scsi.lun;
221         blk_schid.ssid = iplb.scsi.ssid & 0x3;
222         found = find_subch(iplb.scsi.devno);
223         break;
224     default:
225         puts("Unsupported IPLB");
226     }
227 
228     return found;
229 }
230 
231 static int virtio_setup(void)
232 {
233     VDev *vdev = virtio_get_device();
234     QemuIplParameters *early_qipl = (QemuIplParameters *)QIPL_ADDRESS;
235     int ret;
236 
237     memcpy(&qipl, early_qipl, sizeof(QemuIplParameters));
238 
239     if (have_iplb) {
240         menu_setup();
241     }
242 
243     switch (vdev->senseid.cu_model) {
244     case VIRTIO_ID_NET:
245         puts("Network boot device detected");
246         return 0;
247     case VIRTIO_ID_BLOCK:
248         ret = virtio_blk_setup_device(blk_schid);
249         break;
250     case VIRTIO_ID_SCSI:
251         ret = virtio_scsi_setup_device(blk_schid);
252         break;
253     default:
254         puts("\n! No IPL device available !\n");
255         return -1;
256     }
257 
258     if (!ret && !virtio_ipl_disk_is_valid()) {
259         puts("No valid IPL device detected");
260         return -ENODEV;
261     }
262 
263     return ret;
264 }
265 
266 static void ipl_boot_device(void)
267 {
268     switch (cutype) {
269     case CU_TYPE_DASD_3990:
270     case CU_TYPE_DASD_2107:
271         dasd_ipl(blk_schid, cutype);
272         break;
273     case CU_TYPE_VIRTIO:
274         if (virtio_setup()) {
275             return;    /* Only returns in case of errors */
276         }
277         zipl_load();
278         break;
279     default:
280         printf("Attempting to boot from unexpected device type 0x%X\n", cutype);
281     }
282 }
283 
284 /*
285  * No boot device has been specified, so we have to scan through the
286  * channels to find one.
287  */
288 static void probe_boot_device(void)
289 {
290     int ssid, sch_no, ret;
291 
292     for (ssid = 0; ssid < 0x3; ssid++) {
293         blk_schid.ssid = ssid;
294         for (sch_no = 0; sch_no < 0x10000; sch_no++) {
295             ret = is_dev_possibly_bootable(-1, sch_no);
296             if (ret < 0) {
297                 break;
298             }
299             if (ret == true) {
300                 ipl_boot_device();      /* Only returns if unsuccessful */
301             }
302         }
303     }
304 
305     puts("Could not find a suitable boot device (none specified)");
306 }
307 
308 void main(void)
309 {
310     sclp_setup();
311     css_setup();
312     boot_setup();
313     if (have_iplb && find_boot_device()) {
314         ipl_boot_device();
315     } else {
316         probe_boot_device();
317     }
318 
319     panic("Failed to IPL. Halting...");
320 }
321