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" 17efa47d36SJason J. Herne #include "dasd-ipl.h" 1892f2ca38SAlexander Graf 1992f2ca38SAlexander Graf char stack[PAGE_SIZE * 8] __attribute__((__aligned__(PAGE_SIZE))); 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 */ 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 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 5195fa1af8SFarhan Ali unsigned int get_loadparm_index(void) 5295fa1af8SFarhan Ali { 53074afe60SCollin Walling return atoui(loadparm_str); 5495fa1af8SFarhan Ali } 5595fa1af8SFarhan Ali 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 */ 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 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 */ 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 */ 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 181*3d651996SEric Farman /* 182*3d651996SEric Farman * Clear out any potential S390EP magic (see jump_to_low_kernel()), 183*3d651996SEric Farman * so we don't taint our decision-making process during a reboot. 184*3d651996SEric Farman */ 185*3d651996SEric Farman memset((char *)S390EP, 0, 6); 186*3d651996SEric Farman 187a5f6e097SJason J. Herne have_iplb = store_iplb(&iplb); 188a5f6e097SJason J. Herne } 189a5f6e097SJason J. Herne 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 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; 2217b361db3SJason J. Herne 2227b361db3SJason J. Herne memcpy(&qipl, early_qipl, sizeof(QemuIplParameters)); 2237b361db3SJason J. Herne 2247b361db3SJason J. Herne if (have_iplb) { 2257b361db3SJason J. Herne menu_setup(); 2267b361db3SJason J. Herne } 22792f2ca38SAlexander Graf 22899b72e0fSFarhan Ali if (virtio_get_device_type() == VIRTIO_ID_NET) { 22999b72e0fSFarhan Ali sclp_print("Network boot device detected\n"); 230118ee80fSCollin L. Walling vdev->netboot_start_addr = qipl.netboot_start_addr; 23199b72e0fSFarhan Ali } else { 232605751b5SThomas Huth int ret = virtio_blk_setup_device(blk_schid); 233605751b5SThomas Huth if (ret) { 234605751b5SThomas Huth return ret; 235605751b5SThomas Huth } 23680ba3e24SEugene (jno) Dvurechenski IPL_assert(virtio_ipl_disk_is_valid(), "No valid IPL device detected"); 23792f2ca38SAlexander Graf } 238605751b5SThomas Huth 239605751b5SThomas Huth return 0; 24099b72e0fSFarhan Ali } 24192f2ca38SAlexander Graf 242d1f060a8SThomas Huth static void ipl_boot_device(void) 24392f2ca38SAlexander Graf { 2443668cb7cSJason J. Herne switch (cutype) { 245efa47d36SJason J. Herne case CU_TYPE_DASD_3990: 246efa47d36SJason J. Herne case CU_TYPE_DASD_2107: 247efa47d36SJason J. Herne dasd_ipl(blk_schid, cutype); /* no return */ 248efa47d36SJason J. Herne break; 2493668cb7cSJason J. Herne case CU_TYPE_VIRTIO: 250605751b5SThomas Huth if (virtio_setup() == 0) { 2515dc739f3SThomas Huth zipl_load(); /* Only returns in case of errors */ 252605751b5SThomas Huth } 2533668cb7cSJason J. Herne break; 2543668cb7cSJason J. Herne default: 2553668cb7cSJason J. Herne print_int("Attempting to boot from unexpected device type", cutype); 256d1f060a8SThomas Huth panic("\nBoot failed.\n"); 2573668cb7cSJason J. Herne } 258d1f060a8SThomas Huth } 259d1f060a8SThomas Huth 260869d0e2fSThomas Huth /* 261869d0e2fSThomas Huth * No boot device has been specified, so we have to scan through the 262869d0e2fSThomas Huth * channels to find one. 263869d0e2fSThomas Huth */ 264869d0e2fSThomas Huth static void probe_boot_device(void) 265869d0e2fSThomas Huth { 266869d0e2fSThomas Huth int ssid, sch_no, ret; 267869d0e2fSThomas Huth 268869d0e2fSThomas Huth for (ssid = 0; ssid < 0x3; ssid++) { 269869d0e2fSThomas Huth blk_schid.ssid = ssid; 270869d0e2fSThomas Huth for (sch_no = 0; sch_no < 0x10000; sch_no++) { 271869d0e2fSThomas Huth ret = is_dev_possibly_bootable(-1, sch_no); 272869d0e2fSThomas Huth if (ret < 0) { 273869d0e2fSThomas Huth break; 274869d0e2fSThomas Huth } 275869d0e2fSThomas Huth if (ret == true) { 276869d0e2fSThomas Huth ipl_boot_device(); /* Only returns if unsuccessful */ 277869d0e2fSThomas Huth } 278869d0e2fSThomas Huth } 279869d0e2fSThomas Huth } 280869d0e2fSThomas Huth 281869d0e2fSThomas Huth sclp_print("Could not find a suitable boot device (none specified)\n"); 282869d0e2fSThomas Huth } 283869d0e2fSThomas Huth 284d1f060a8SThomas Huth int main(void) 285d1f060a8SThomas Huth { 286d1f060a8SThomas Huth sclp_setup(); 287d1f060a8SThomas Huth css_setup(); 288d1f060a8SThomas Huth boot_setup(); 289869d0e2fSThomas Huth if (have_iplb) { 290d1f060a8SThomas Huth find_boot_device(); 291d1f060a8SThomas Huth ipl_boot_device(); 292869d0e2fSThomas Huth } else { 293869d0e2fSThomas Huth probe_boot_device(); 294869d0e2fSThomas Huth } 29560612d5cSEugene (jno) Dvurechenski 296c9262e8aSEugene (jno) Dvurechenski panic("Failed to load OS from hard disk\n"); 29760612d5cSEugene (jno) Dvurechenski return 0; /* make compiler happy */ 29892f2ca38SAlexander Graf } 299