xref: /qemu/pc-bios/s390-ccw/main.c (revision 92eecfff)
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 "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"
18 
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 */
27 
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)
31 
32 /*
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.
36  */
37 void write_subsystem_identification(void)
38 {
39     lowcore->subchannel_id = blk_schid.sch_id;
40     lowcore->subchannel_nr = blk_schid.sch_no;
41     lowcore->io_int_parm = 0;
42 }
43 
44 void write_iplb_location(void)
45 {
46     if (cutype == CU_TYPE_VIRTIO && virtio_get_device_type() != VIRTIO_ID_NET) {
47         lowcore->ptr_iplb = ptr2u32(&iplb);
48     }
49 }
50 
51 unsigned int get_loadparm_index(void)
52 {
53     return atoui(loadparm_str);
54 }
55 
56 static int is_dev_possibly_bootable(int dev_no, int sch_no)
57 {
58     bool is_virtio;
59     Schib schib;
60     int r;
61 
62     blk_schid.sch_no = sch_no;
63     r = stsch_err(blk_schid, &schib);
64     if (r == 3 || r == -EIO) {
65         return -ENODEV;
66     }
67     if (!schib.pmcw.dnv) {
68         return false;
69     }
70 
71     enable_subchannel(blk_schid);
72     cutype = cu_type(blk_schid);
73 
74     /*
75      * Note: we always have to run virtio_is_supported() here to make
76      * sure that the vdev.senseid data gets pre-initialized correctly
77      */
78     is_virtio = virtio_is_supported(blk_schid);
79 
80     /* No specific devno given, just return whether the device is possibly bootable */
81     if (dev_no < 0) {
82         switch (cutype) {
83         case CU_TYPE_VIRTIO:
84             if (is_virtio) {
85                 /*
86                  * Skip net devices since no IPLB is created and therefore
87                  * no network bootloader has been loaded
88                  */
89                 if (virtio_get_device_type() != VIRTIO_ID_NET) {
90                     return true;
91                 }
92             }
93             return false;
94         case CU_TYPE_DASD_3990:
95         case CU_TYPE_DASD_2107:
96             return true;
97         default:
98             return false;
99         }
100     }
101 
102     /* Caller asked for a specific devno */
103     if (schib.pmcw.dev == dev_no) {
104         return true;
105     }
106 
107     return false;
108 }
109 
110 /*
111  * Find the subchannel connected to the given device (dev_no) and fill in the
112  * subchannel information block (schib) with the connected subchannel's info.
113  * NOTE: The global variable blk_schid is updated to contain the subchannel
114  * information.
115  *
116  * If the caller gives dev_no=-1 then the user did not specify a boot device.
117  * In this case we'll just use the first potentially bootable device we find.
118  */
119 static bool find_subch(int dev_no)
120 {
121     int i, r;
122 
123     for (i = 0; i < 0x10000; i++) {
124         r = is_dev_possibly_bootable(dev_no, i);
125         if (r < 0) {
126             break;
127         }
128         if (r == true) {
129             return true;
130         }
131     }
132 
133     return false;
134 }
135 
136 static void menu_setup(void)
137 {
138     if (memcmp(loadparm_str, LOADPARM_PROMPT, LOADPARM_LEN) == 0) {
139         menu_set_parms(QIPL_FLAG_BM_OPTS_CMD, 0);
140         return;
141     }
142 
143     /* If loadparm was set to any other value, then do not enable menu */
144     if (memcmp(loadparm_str, LOADPARM_EMPTY, LOADPARM_LEN) != 0) {
145         return;
146     }
147 
148     switch (iplb.pbt) {
149     case S390_IPL_TYPE_CCW:
150     case S390_IPL_TYPE_QEMU_SCSI:
151         menu_set_parms(qipl.qipl_flags & BOOT_MENU_FLAG_MASK,
152                        qipl.boot_menu_timeout);
153         return;
154     }
155 }
156 
157 /*
158  * Initialize the channel I/O subsystem so we can talk to our ipl/boot device.
159  */
160 static void css_setup(void)
161 {
162     /*
163      * Unconditionally enable mss support. In every sane configuration this
164      * will succeed; and even if it doesn't, stsch_err() can handle it.
165      */
166     enable_mss_facility();
167 }
168 
169 /*
170  * Collect various pieces of information from the hypervisor/hardware that
171  * we'll use to determine exactly how we'll boot.
172  */
173 static void boot_setup(void)
174 {
175     char lpmsg[] = "LOADPARM=[________]\n";
176 
177     sclp_get_loadparm_ascii(loadparm_str);
178     memcpy(lpmsg + 10, loadparm_str, 8);
179     sclp_print(lpmsg);
180 
181     have_iplb = store_iplb(&iplb);
182 }
183 
184 static void find_boot_device(void)
185 {
186     VDev *vdev = virtio_get_device();
187     bool found;
188 
189     switch (iplb.pbt) {
190     case S390_IPL_TYPE_CCW:
191         debug_print_int("device no. ", iplb.ccw.devno);
192         blk_schid.ssid = iplb.ccw.ssid & 0x3;
193         debug_print_int("ssid ", blk_schid.ssid);
194         found = find_subch(iplb.ccw.devno);
195         break;
196     case S390_IPL_TYPE_QEMU_SCSI:
197         vdev->scsi_device_selected = true;
198         vdev->selected_scsi_device.channel = iplb.scsi.channel;
199         vdev->selected_scsi_device.target = iplb.scsi.target;
200         vdev->selected_scsi_device.lun = iplb.scsi.lun;
201         blk_schid.ssid = iplb.scsi.ssid & 0x3;
202         found = find_subch(iplb.scsi.devno);
203         break;
204     default:
205         panic("List-directed IPL not supported yet!\n");
206     }
207 
208     IPL_assert(found, "Boot device not found\n");
209 }
210 
211 static int virtio_setup(void)
212 {
213     VDev *vdev = virtio_get_device();
214     QemuIplParameters *early_qipl = (QemuIplParameters *)QIPL_ADDRESS;
215 
216     memcpy(&qipl, early_qipl, sizeof(QemuIplParameters));
217 
218     if (have_iplb) {
219         menu_setup();
220     }
221 
222     if (virtio_get_device_type() == VIRTIO_ID_NET) {
223         sclp_print("Network boot device detected\n");
224         vdev->netboot_start_addr = qipl.netboot_start_addr;
225     } else {
226         int ret = virtio_blk_setup_device(blk_schid);
227         if (ret) {
228             return ret;
229         }
230         IPL_assert(virtio_ipl_disk_is_valid(), "No valid IPL device detected");
231     }
232 
233     return 0;
234 }
235 
236 static void ipl_boot_device(void)
237 {
238     switch (cutype) {
239     case CU_TYPE_DASD_3990:
240     case CU_TYPE_DASD_2107:
241         dasd_ipl(blk_schid, cutype); /* no return */
242         break;
243     case CU_TYPE_VIRTIO:
244         if (virtio_setup() == 0) {
245             zipl_load();             /* Only returns in case of errors */
246         }
247         break;
248     default:
249         print_int("Attempting to boot from unexpected device type", cutype);
250         panic("\nBoot failed.\n");
251     }
252 }
253 
254 /*
255  * No boot device has been specified, so we have to scan through the
256  * channels to find one.
257  */
258 static void probe_boot_device(void)
259 {
260     int ssid, sch_no, ret;
261 
262     for (ssid = 0; ssid < 0x3; ssid++) {
263         blk_schid.ssid = ssid;
264         for (sch_no = 0; sch_no < 0x10000; sch_no++) {
265             ret = is_dev_possibly_bootable(-1, sch_no);
266             if (ret < 0) {
267                 break;
268             }
269             if (ret == true) {
270                 ipl_boot_device();      /* Only returns if unsuccessful */
271             }
272         }
273     }
274 
275     sclp_print("Could not find a suitable boot device (none specified)\n");
276 }
277 
278 int main(void)
279 {
280     sclp_setup();
281     css_setup();
282     boot_setup();
283     if (have_iplb) {
284         find_boot_device();
285         ipl_boot_device();
286     } else {
287         probe_boot_device();
288     }
289 
290     panic("Failed to load OS from hard disk\n");
291     return 0; /* make compiler happy */
292 }
293