xref: /qemu/pc-bios/s390-ccw/main.c (revision e31f08dc)
192f2ca38SAlexander Graf /*
292f2ca38SAlexander Graf  * S390 virtio-ccw loading program
392f2ca38SAlexander Graf  *
492f2ca38SAlexander Graf  * Copyright (c) 2013 Alexander Graf <agraf@suse.de>
592f2ca38SAlexander Graf  *
692f2ca38SAlexander Graf  * This work is licensed under the terms of the GNU GPL, version 2 or (at
792f2ca38SAlexander Graf  * your option) any later version. See the COPYING file in the top-level
892f2ca38SAlexander Graf  * directory.
992f2ca38SAlexander Graf  */
1092f2ca38SAlexander Graf 
1190806fecSThomas Huth #include "libc.h"
129bfc04f9SJanosch Frank #include "helper.h"
13c95df3d1SJason J. Herne #include "s390-arch.h"
1492f2ca38SAlexander Graf #include "s390-ccw.h"
15120d0410SJason J. Herne #include "cio.h"
1660612d5cSEugene (jno) Dvurechenski #include "virtio.h"
17cf30b7c4SThomas Huth #include "virtio-scsi.h"
18efa47d36SJason J. Herne #include "dasd-ipl.h"
1992f2ca38SAlexander Graf 
20b88d7fa5SEugene (jno) Dvurechenski static SubChannelId blk_schid = { .one = 1 };
216673ded7SThomas Huth static char loadparm_str[LOADPARM_LEN + 1];
22118ee80fSCollin L. Walling QemuIplParameters qipl;
23a5f6e097SJason J. Herne IplParameterBlock iplb __attribute__((__aligned__(PAGE_SIZE)));
24a5f6e097SJason J. Herne static bool have_iplb;
252880469cSJason J. Herne static uint16_t cutype;
269bfc04f9SJanosch Frank LowCore *lowcore; /* Yes, this *is* a pointer to address 0 */
27f2879a5cSChristian Borntraeger 
289eaa654aSCollin L. Walling #define LOADPARM_PROMPT "PROMPT  "
29074afe60SCollin Walling #define LOADPARM_EMPTY  "        "
3053b310ceSCollin L. Walling #define BOOT_MENU_FLAG_MASK (QIPL_FLAG_BM_OPTS_CMD | QIPL_FLAG_BM_OPTS_ZIPL)
319eaa654aSCollin L. Walling 
32f2879a5cSChristian Borntraeger /*
33a5f6e097SJason J. Herne  * Principles of Operations (SA22-7832-09) chapter 17 requires that
34f2879a5cSChristian Borntraeger  * a subsystem-identification is at 184-187 and bytes 188-191 are zero
35f2879a5cSChristian Borntraeger  * after list-directed-IPL and ccw-IPL.
36f2879a5cSChristian Borntraeger  */
write_subsystem_identification(void)37f2879a5cSChristian Borntraeger void write_subsystem_identification(void)
38f2879a5cSChristian Borntraeger {
39e6d393d0SJanosch Frank     lowcore->subchannel_id = blk_schid.sch_id;
40e6d393d0SJanosch Frank     lowcore->subchannel_nr = blk_schid.sch_no;
41e6d393d0SJanosch Frank     lowcore->io_int_parm = 0;
42f2879a5cSChristian Borntraeger }
43f2879a5cSChristian Borntraeger 
write_iplb_location(void)449bfc04f9SJanosch Frank void write_iplb_location(void)
459bfc04f9SJanosch Frank {
46872882e7SJason J. Herne     if (cutype == CU_TYPE_VIRTIO && virtio_get_device_type() != VIRTIO_ID_NET) {
479bfc04f9SJanosch Frank         lowcore->ptr_iplb = ptr2u32(&iplb);
489bfc04f9SJanosch Frank     }
49872882e7SJason J. Herne }
509bfc04f9SJanosch Frank 
get_loadparm_index(void)5195fa1af8SFarhan Ali unsigned int get_loadparm_index(void)
5295fa1af8SFarhan Ali {
53074afe60SCollin Walling     return atoui(loadparm_str);
5495fa1af8SFarhan Ali }
5595fa1af8SFarhan Ali 
is_dev_possibly_bootable(int dev_no,int sch_no)56d2cf4af1SThomas Huth static int is_dev_possibly_bootable(int dev_no, int sch_no)
5792f2ca38SAlexander Graf {
582880469cSJason J. Herne     bool is_virtio;
59d2cf4af1SThomas Huth     Schib schib;
60d2cf4af1SThomas Huth     int r;
61ff151f4eSDominik Dingel 
62d2cf4af1SThomas Huth     blk_schid.sch_no = sch_no;
637b361db3SJason J. Herne     r = stsch_err(blk_schid, &schib);
64d2cf4af1SThomas Huth     if (r == 3 || r == -EIO) {
65d2cf4af1SThomas Huth         return -ENODEV;
6622d67ab5SCornelia Huck     }
677b361db3SJason J. Herne     if (!schib.pmcw.dnv) {
68d2cf4af1SThomas Huth         return false;
6992f2ca38SAlexander Graf     }
70930072d2SJason J. Herne 
713668cb7cSJason J. Herne     enable_subchannel(blk_schid);
722880469cSJason J. Herne     cutype = cu_type(blk_schid);
732880469cSJason J. Herne 
742880469cSJason J. Herne     /*
752880469cSJason J. Herne      * Note: we always have to run virtio_is_supported() here to make
762880469cSJason J. Herne      * sure that the vdev.senseid data gets pre-initialized correctly
772880469cSJason J. Herne      */
782880469cSJason J. Herne     is_virtio = virtio_is_supported(blk_schid);
792880469cSJason J. Herne 
80d2cf4af1SThomas Huth     /* No specific devno given, just return whether the device is possibly bootable */
812880469cSJason J. Herne     if (dev_no < 0) {
822880469cSJason J. Herne         switch (cutype) {
832880469cSJason J. Herne         case CU_TYPE_VIRTIO:
842880469cSJason J. Herne             if (is_virtio) {
852880469cSJason J. Herne                 /*
862880469cSJason J. Herne                  * Skip net devices since no IPLB is created and therefore
872880469cSJason J. Herne                  * no network bootloader has been loaded
882880469cSJason J. Herne                  */
892880469cSJason J. Herne                 if (virtio_get_device_type() != VIRTIO_ID_NET) {
902880469cSJason J. Herne                     return true;
912880469cSJason J. Herne                 }
922880469cSJason J. Herne             }
93d2cf4af1SThomas Huth             return false;
942880469cSJason J. Herne         case CU_TYPE_DASD_3990:
952880469cSJason J. Herne         case CU_TYPE_DASD_2107:
962880469cSJason J. Herne             return true;
972880469cSJason J. Herne         default:
98d2cf4af1SThomas Huth             return false;
9999b72e0fSFarhan Ali         }
1002880469cSJason J. Herne     }
101930072d2SJason J. Herne 
1022880469cSJason J. Herne     /* Caller asked for a specific devno */
1032880469cSJason J. Herne     if (schib.pmcw.dev == dev_no) {
1040f79b89bSAlexander Yarygin         return true;
1050f79b89bSAlexander Yarygin     }
106d2cf4af1SThomas Huth 
107d2cf4af1SThomas Huth     return false;
108d2cf4af1SThomas Huth }
109d2cf4af1SThomas Huth 
110d2cf4af1SThomas Huth /*
111d2cf4af1SThomas Huth  * Find the subchannel connected to the given device (dev_no) and fill in the
112d2cf4af1SThomas Huth  * subchannel information block (schib) with the connected subchannel's info.
113d2cf4af1SThomas Huth  * NOTE: The global variable blk_schid is updated to contain the subchannel
114d2cf4af1SThomas Huth  * information.
115d2cf4af1SThomas Huth  *
116d2cf4af1SThomas Huth  * If the caller gives dev_no=-1 then the user did not specify a boot device.
117d2cf4af1SThomas Huth  * In this case we'll just use the first potentially bootable device we find.
118d2cf4af1SThomas Huth  */
find_subch(int dev_no)119d2cf4af1SThomas Huth static bool find_subch(int dev_no)
120d2cf4af1SThomas Huth {
121d2cf4af1SThomas Huth     int i, r;
122d2cf4af1SThomas Huth 
123d2cf4af1SThomas Huth     for (i = 0; i < 0x10000; i++) {
124d2cf4af1SThomas Huth         r = is_dev_possibly_bootable(dev_no, i);
125d2cf4af1SThomas Huth         if (r < 0) {
126d2cf4af1SThomas Huth             break;
127d2cf4af1SThomas Huth         }
128d2cf4af1SThomas Huth         if (r == true) {
129d2cf4af1SThomas Huth             return true;
130d2cf4af1SThomas Huth         }
1310f79b89bSAlexander Yarygin     }
1320f79b89bSAlexander Yarygin 
1330f79b89bSAlexander Yarygin     return false;
1340f79b89bSAlexander Yarygin }
1350f79b89bSAlexander Yarygin 
menu_setup(void)1369eaa654aSCollin L. Walling static void menu_setup(void)
1379eaa654aSCollin L. Walling {
138a0e11b61SCollin Walling     if (memcmp(loadparm_str, LOADPARM_PROMPT, LOADPARM_LEN) == 0) {
1399eaa654aSCollin L. Walling         menu_set_parms(QIPL_FLAG_BM_OPTS_CMD, 0);
1409eaa654aSCollin L. Walling         return;
1419eaa654aSCollin L. Walling     }
1429eaa654aSCollin L. Walling 
1439eaa654aSCollin L. Walling     /* If loadparm was set to any other value, then do not enable menu */
144a0e11b61SCollin Walling     if (memcmp(loadparm_str, LOADPARM_EMPTY, LOADPARM_LEN) != 0) {
1459eaa654aSCollin L. Walling         return;
1469eaa654aSCollin L. Walling     }
1479eaa654aSCollin L. Walling 
1489eaa654aSCollin L. Walling     switch (iplb.pbt) {
1499eaa654aSCollin L. Walling     case S390_IPL_TYPE_CCW:
150ffb4a1c8SCollin L. Walling     case S390_IPL_TYPE_QEMU_SCSI:
15153b310ceSCollin L. Walling         menu_set_parms(qipl.qipl_flags & BOOT_MENU_FLAG_MASK,
1529eaa654aSCollin L. Walling                        qipl.boot_menu_timeout);
1539eaa654aSCollin L. Walling         return;
1549eaa654aSCollin L. Walling     }
1559eaa654aSCollin L. Walling }
1569eaa654aSCollin L. Walling 
15787f910c1SJason J. Herne /*
15887f910c1SJason J. Herne  * Initialize the channel I/O subsystem so we can talk to our ipl/boot device.
15987f910c1SJason J. Herne  */
css_setup(void)16087f910c1SJason J. Herne static void css_setup(void)
16187f910c1SJason J. Herne {
16287f910c1SJason J. Herne     /*
16387f910c1SJason J. Herne      * Unconditionally enable mss support. In every sane configuration this
16487f910c1SJason J. Herne      * will succeed; and even if it doesn't, stsch_err() can handle it.
16587f910c1SJason J. Herne      */
16687f910c1SJason J. Herne     enable_mss_facility();
16787f910c1SJason J. Herne }
16887f910c1SJason J. Herne 
169a5f6e097SJason J. Herne /*
170a5f6e097SJason J. Herne  * Collect various pieces of information from the hypervisor/hardware that
171a5f6e097SJason J. Herne  * we'll use to determine exactly how we'll boot.
172a5f6e097SJason J. Herne  */
boot_setup(void)173a5f6e097SJason J. Herne static void boot_setup(void)
174a5f6e097SJason J. Herne {
175a5f6e097SJason J. Herne     char lpmsg[] = "LOADPARM=[________]\n";
176a5f6e097SJason J. Herne 
177a5f6e097SJason J. Herne     sclp_get_loadparm_ascii(loadparm_str);
178a5f6e097SJason J. Herne     memcpy(lpmsg + 10, loadparm_str, 8);
179a5f6e097SJason J. Herne     sclp_print(lpmsg);
180a5f6e097SJason J. Herne 
1813d651996SEric Farman     /*
1823d651996SEric Farman      * Clear out any potential S390EP magic (see jump_to_low_kernel()),
1833d651996SEric Farman      * so we don't taint our decision-making process during a reboot.
1843d651996SEric Farman      */
1853d651996SEric Farman     memset((char *)S390EP, 0, 6);
1863d651996SEric Farman 
187a5f6e097SJason J. Herne     have_iplb = store_iplb(&iplb);
188a5f6e097SJason J. Herne }
189a5f6e097SJason J. Herne 
find_boot_device(void)1907b361db3SJason J. Herne static void find_boot_device(void)
1910f79b89bSAlexander Yarygin {
19299b72e0fSFarhan Ali     VDev *vdev = virtio_get_device();
1937b361db3SJason J. Herne     bool found;
1940f79b89bSAlexander Yarygin 
195d046c51dSAlexander Yarygin     switch (iplb.pbt) {
196d046c51dSAlexander Yarygin     case S390_IPL_TYPE_CCW:
1977b361db3SJason J. Herne         debug_print_int("device no. ", iplb.ccw.devno);
198d046c51dSAlexander Yarygin         blk_schid.ssid = iplb.ccw.ssid & 0x3;
1990f79b89bSAlexander Yarygin         debug_print_int("ssid ", blk_schid.ssid);
2007b361db3SJason J. Herne         found = find_subch(iplb.ccw.devno);
201d046c51dSAlexander Yarygin         break;
202b39b7718SEugene (jno) Dvurechenski     case S390_IPL_TYPE_QEMU_SCSI:
203b39b7718SEugene (jno) Dvurechenski         vdev->scsi_device_selected = true;
204b39b7718SEugene (jno) Dvurechenski         vdev->selected_scsi_device.channel = iplb.scsi.channel;
205b39b7718SEugene (jno) Dvurechenski         vdev->selected_scsi_device.target = iplb.scsi.target;
206b39b7718SEugene (jno) Dvurechenski         vdev->selected_scsi_device.lun = iplb.scsi.lun;
207b39b7718SEugene (jno) Dvurechenski         blk_schid.ssid = iplb.scsi.ssid & 0x3;
2087b361db3SJason J. Herne         found = find_subch(iplb.scsi.devno);
209b39b7718SEugene (jno) Dvurechenski         break;
210d046c51dSAlexander Yarygin     default:
211d046c51dSAlexander Yarygin         panic("List-directed IPL not supported yet!\n");
212d046c51dSAlexander Yarygin     }
2137b361db3SJason J. Herne 
2147b361db3SJason J. Herne     IPL_assert(found, "Boot device not found\n");
215ff151f4eSDominik Dingel }
21692f2ca38SAlexander Graf 
virtio_setup(void)217605751b5SThomas Huth static int virtio_setup(void)
2187b361db3SJason J. Herne {
2197b361db3SJason J. Herne     VDev *vdev = virtio_get_device();
2207b361db3SJason J. Herne     QemuIplParameters *early_qipl = (QemuIplParameters *)QIPL_ADDRESS;
221cf30b7c4SThomas Huth     int ret;
2227b361db3SJason J. Herne 
2237b361db3SJason J. Herne     memcpy(&qipl, early_qipl, sizeof(QemuIplParameters));
2247b361db3SJason J. Herne 
2257b361db3SJason J. Herne     if (have_iplb) {
2267b361db3SJason J. Herne         menu_setup();
2277b361db3SJason J. Herne     }
22892f2ca38SAlexander Graf 
229cf30b7c4SThomas Huth     switch (vdev->senseid.cu_model) {
230cf30b7c4SThomas Huth     case VIRTIO_ID_NET:
23199b72e0fSFarhan Ali         sclp_print("Network boot device detected\n");
232118ee80fSCollin L. Walling         vdev->netboot_start_addr = qipl.netboot_start_addr;
233cf30b7c4SThomas Huth         return 0;
234cf30b7c4SThomas Huth     case VIRTIO_ID_BLOCK:
235cf30b7c4SThomas Huth         ret = virtio_blk_setup_device(blk_schid);
236cf30b7c4SThomas Huth         break;
237cf30b7c4SThomas Huth     case VIRTIO_ID_SCSI:
238cf30b7c4SThomas Huth         ret = virtio_scsi_setup_device(blk_schid);
239cf30b7c4SThomas Huth         break;
240cf30b7c4SThomas Huth     default:
241cf30b7c4SThomas Huth         panic("\n! No IPL device available !\n");
242605751b5SThomas Huth     }
243cf30b7c4SThomas Huth 
244cf30b7c4SThomas Huth     if (!ret) {
24580ba3e24SEugene (jno) Dvurechenski         IPL_assert(virtio_ipl_disk_is_valid(), "No valid IPL device detected");
24692f2ca38SAlexander Graf     }
247605751b5SThomas Huth 
248cf30b7c4SThomas Huth     return ret;
24999b72e0fSFarhan Ali }
25092f2ca38SAlexander Graf 
ipl_boot_device(void)251d1f060a8SThomas Huth static void ipl_boot_device(void)
25292f2ca38SAlexander Graf {
2533668cb7cSJason J. Herne     switch (cutype) {
254efa47d36SJason J. Herne     case CU_TYPE_DASD_3990:
255efa47d36SJason J. Herne     case CU_TYPE_DASD_2107:
256efa47d36SJason J. Herne         dasd_ipl(blk_schid, cutype); /* no return */
257efa47d36SJason J. Herne         break;
2583668cb7cSJason J. Herne     case CU_TYPE_VIRTIO:
259605751b5SThomas Huth         if (virtio_setup() == 0) {
2605dc739f3SThomas Huth             zipl_load();             /* Only returns in case of errors */
261605751b5SThomas Huth         }
2623668cb7cSJason J. Herne         break;
2633668cb7cSJason J. Herne     default:
2643668cb7cSJason J. Herne         print_int("Attempting to boot from unexpected device type", cutype);
265d1f060a8SThomas Huth         panic("\nBoot failed.\n");
2663668cb7cSJason J. Herne     }
267d1f060a8SThomas Huth }
268d1f060a8SThomas Huth 
269869d0e2fSThomas Huth /*
270869d0e2fSThomas Huth  * No boot device has been specified, so we have to scan through the
271869d0e2fSThomas Huth  * channels to find one.
272869d0e2fSThomas Huth  */
probe_boot_device(void)273869d0e2fSThomas Huth static void probe_boot_device(void)
274869d0e2fSThomas Huth {
275869d0e2fSThomas Huth     int ssid, sch_no, ret;
276869d0e2fSThomas Huth 
277869d0e2fSThomas Huth     for (ssid = 0; ssid < 0x3; ssid++) {
278869d0e2fSThomas Huth         blk_schid.ssid = ssid;
279869d0e2fSThomas Huth         for (sch_no = 0; sch_no < 0x10000; sch_no++) {
280869d0e2fSThomas Huth             ret = is_dev_possibly_bootable(-1, sch_no);
281869d0e2fSThomas Huth             if (ret < 0) {
282869d0e2fSThomas Huth                 break;
283869d0e2fSThomas Huth             }
284869d0e2fSThomas Huth             if (ret == true) {
285869d0e2fSThomas Huth                 ipl_boot_device();      /* Only returns if unsuccessful */
286869d0e2fSThomas Huth             }
287869d0e2fSThomas Huth         }
288869d0e2fSThomas Huth     }
289869d0e2fSThomas Huth 
290869d0e2fSThomas Huth     sclp_print("Could not find a suitable boot device (none specified)\n");
291869d0e2fSThomas Huth }
292869d0e2fSThomas Huth 
main(void)2932ba3cc47SThomas Huth void main(void)
294d1f060a8SThomas Huth {
295d1f060a8SThomas Huth     sclp_setup();
296d1f060a8SThomas Huth     css_setup();
297d1f060a8SThomas Huth     boot_setup();
298869d0e2fSThomas Huth     if (have_iplb) {
299d1f060a8SThomas Huth         find_boot_device();
300d1f060a8SThomas Huth         ipl_boot_device();
301869d0e2fSThomas Huth     } else {
302869d0e2fSThomas Huth         probe_boot_device();
303869d0e2fSThomas Huth     }
30460612d5cSEugene (jno) Dvurechenski 
305c9262e8aSEugene (jno) Dvurechenski     panic("Failed to load OS from hard disk\n");
30692f2ca38SAlexander Graf }
307