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