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