xref: /openbmc/qemu/pc-bios/s390-ccw/main.c (revision ba3658ad)
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 
187     sclp_get_loadparm_ascii(loadparm_str);
188     memcpy(lpmsg + 10, loadparm_str, 8);
189     puts(lpmsg);
190 
191     /*
192      * Clear out any potential S390EP magic (see jump_to_low_kernel()),
193      * so we don't taint our decision-making process during a reboot.
194      */
195     memset((char *)S390EP, 0, 6);
196 
197     have_iplb = store_iplb(&iplb);
198 }
199 
200 static bool find_boot_device(void)
201 {
202     VDev *vdev = virtio_get_device();
203     bool found = false;
204 
205     switch (iplb.pbt) {
206     case S390_IPL_TYPE_CCW:
207         debug_print_int("device no. ", iplb.ccw.devno);
208         blk_schid.ssid = iplb.ccw.ssid & 0x3;
209         debug_print_int("ssid ", blk_schid.ssid);
210         found = find_subch(iplb.ccw.devno);
211         break;
212     case S390_IPL_TYPE_QEMU_SCSI:
213         vdev->scsi_device_selected = true;
214         vdev->selected_scsi_device.channel = iplb.scsi.channel;
215         vdev->selected_scsi_device.target = iplb.scsi.target;
216         vdev->selected_scsi_device.lun = iplb.scsi.lun;
217         blk_schid.ssid = iplb.scsi.ssid & 0x3;
218         found = find_subch(iplb.scsi.devno);
219         break;
220     default:
221         puts("Unsupported IPLB");
222     }
223 
224     return found;
225 }
226 
227 static int virtio_setup(void)
228 {
229     VDev *vdev = virtio_get_device();
230     QemuIplParameters *early_qipl = (QemuIplParameters *)QIPL_ADDRESS;
231     int ret;
232 
233     memcpy(&qipl, early_qipl, sizeof(QemuIplParameters));
234 
235     if (have_iplb) {
236         menu_setup();
237     }
238 
239     switch (vdev->senseid.cu_model) {
240     case VIRTIO_ID_NET:
241         puts("Network boot device detected");
242         return 0;
243     case VIRTIO_ID_BLOCK:
244         ret = virtio_blk_setup_device(blk_schid);
245         break;
246     case VIRTIO_ID_SCSI:
247         ret = virtio_scsi_setup_device(blk_schid);
248         break;
249     default:
250         puts("\n! No IPL device available !\n");
251         return -1;
252     }
253 
254     if (!ret && !virtio_ipl_disk_is_valid()) {
255         puts("No valid IPL device detected");
256         return -ENODEV;
257     }
258 
259     return ret;
260 }
261 
262 static void ipl_boot_device(void)
263 {
264     switch (cutype) {
265     case CU_TYPE_DASD_3990:
266     case CU_TYPE_DASD_2107:
267         dasd_ipl(blk_schid, cutype);
268         break;
269     case CU_TYPE_VIRTIO:
270         if (virtio_setup()) {
271             return;    /* Only returns in case of errors */
272         }
273         zipl_load();
274         break;
275     default:
276         printf("Attempting to boot from unexpected device type 0x%X\n", cutype);
277     }
278 }
279 
280 /*
281  * No boot device has been specified, so we have to scan through the
282  * channels to find one.
283  */
284 static void probe_boot_device(void)
285 {
286     int ssid, sch_no, ret;
287 
288     for (ssid = 0; ssid < 0x3; ssid++) {
289         blk_schid.ssid = ssid;
290         for (sch_no = 0; sch_no < 0x10000; sch_no++) {
291             ret = is_dev_possibly_bootable(-1, sch_no);
292             if (ret < 0) {
293                 break;
294             }
295             if (ret == true) {
296                 ipl_boot_device();      /* Only returns if unsuccessful */
297             }
298         }
299     }
300 
301     puts("Could not find a suitable boot device (none specified)");
302 }
303 
304 void main(void)
305 {
306     sclp_setup();
307     css_setup();
308     boot_setup();
309     if (have_iplb && find_boot_device()) {
310         ipl_boot_device();
311     } else {
312         probe_boot_device();
313     }
314 
315     panic("Failed to IPL. Halting...");
316 }
317