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 { 469bfc04f9SJanosch Frank lowcore->ptr_iplb = ptr2u32(&iplb); 479bfc04f9SJanosch Frank } 489bfc04f9SJanosch Frank 4995fa1af8SFarhan Ali unsigned int get_loadparm_index(void) 5095fa1af8SFarhan Ali { 51074afe60SCollin Walling return atoui(loadparm_str); 5295fa1af8SFarhan Ali } 5395fa1af8SFarhan Ali 54d2cf4af1SThomas Huth static int is_dev_possibly_bootable(int dev_no, int sch_no) 5592f2ca38SAlexander Graf { 562880469cSJason J. Herne bool is_virtio; 57d2cf4af1SThomas Huth Schib schib; 58d2cf4af1SThomas Huth int r; 59ff151f4eSDominik Dingel 60d2cf4af1SThomas Huth blk_schid.sch_no = sch_no; 617b361db3SJason J. Herne r = stsch_err(blk_schid, &schib); 62d2cf4af1SThomas Huth if (r == 3 || r == -EIO) { 63d2cf4af1SThomas Huth return -ENODEV; 6422d67ab5SCornelia Huck } 657b361db3SJason J. Herne if (!schib.pmcw.dnv) { 66d2cf4af1SThomas Huth return false; 6792f2ca38SAlexander Graf } 68930072d2SJason J. Herne 693668cb7cSJason J. Herne enable_subchannel(blk_schid); 702880469cSJason J. Herne cutype = cu_type(blk_schid); 712880469cSJason J. Herne 722880469cSJason J. Herne /* 732880469cSJason J. Herne * Note: we always have to run virtio_is_supported() here to make 742880469cSJason J. Herne * sure that the vdev.senseid data gets pre-initialized correctly 752880469cSJason J. Herne */ 762880469cSJason J. Herne is_virtio = virtio_is_supported(blk_schid); 772880469cSJason J. Herne 78d2cf4af1SThomas Huth /* No specific devno given, just return whether the device is possibly bootable */ 792880469cSJason J. Herne if (dev_no < 0) { 802880469cSJason J. Herne switch (cutype) { 812880469cSJason J. Herne case CU_TYPE_VIRTIO: 822880469cSJason J. Herne if (is_virtio) { 832880469cSJason J. Herne /* 842880469cSJason J. Herne * Skip net devices since no IPLB is created and therefore 852880469cSJason J. Herne * no network bootloader has been loaded 862880469cSJason J. Herne */ 872880469cSJason J. Herne if (virtio_get_device_type() != VIRTIO_ID_NET) { 882880469cSJason J. Herne return true; 892880469cSJason J. Herne } 902880469cSJason J. Herne } 91d2cf4af1SThomas Huth return false; 922880469cSJason J. Herne case CU_TYPE_DASD_3990: 932880469cSJason J. Herne case CU_TYPE_DASD_2107: 942880469cSJason J. Herne return true; 952880469cSJason J. Herne default: 96d2cf4af1SThomas Huth return false; 9799b72e0fSFarhan Ali } 982880469cSJason J. Herne } 99930072d2SJason J. Herne 1002880469cSJason J. Herne /* Caller asked for a specific devno */ 1012880469cSJason J. Herne if (schib.pmcw.dev == dev_no) { 1020f79b89bSAlexander Yarygin return true; 1030f79b89bSAlexander Yarygin } 104d2cf4af1SThomas Huth 105d2cf4af1SThomas Huth return false; 106d2cf4af1SThomas Huth } 107d2cf4af1SThomas Huth 108d2cf4af1SThomas Huth /* 109d2cf4af1SThomas Huth * Find the subchannel connected to the given device (dev_no) and fill in the 110d2cf4af1SThomas Huth * subchannel information block (schib) with the connected subchannel's info. 111d2cf4af1SThomas Huth * NOTE: The global variable blk_schid is updated to contain the subchannel 112d2cf4af1SThomas Huth * information. 113d2cf4af1SThomas Huth * 114d2cf4af1SThomas Huth * If the caller gives dev_no=-1 then the user did not specify a boot device. 115d2cf4af1SThomas Huth * In this case we'll just use the first potentially bootable device we find. 116d2cf4af1SThomas Huth */ 117d2cf4af1SThomas Huth static bool find_subch(int dev_no) 118d2cf4af1SThomas Huth { 119d2cf4af1SThomas Huth int i, r; 120d2cf4af1SThomas Huth 121d2cf4af1SThomas Huth for (i = 0; i < 0x10000; i++) { 122d2cf4af1SThomas Huth r = is_dev_possibly_bootable(dev_no, i); 123d2cf4af1SThomas Huth if (r < 0) { 124d2cf4af1SThomas Huth break; 125d2cf4af1SThomas Huth } 126d2cf4af1SThomas Huth if (r == true) { 127d2cf4af1SThomas Huth return true; 128d2cf4af1SThomas Huth } 1290f79b89bSAlexander Yarygin } 1300f79b89bSAlexander Yarygin 1310f79b89bSAlexander Yarygin return false; 1320f79b89bSAlexander Yarygin } 1330f79b89bSAlexander Yarygin 1349eaa654aSCollin L. Walling static void menu_setup(void) 1359eaa654aSCollin L. Walling { 136a0e11b61SCollin Walling if (memcmp(loadparm_str, LOADPARM_PROMPT, LOADPARM_LEN) == 0) { 1379eaa654aSCollin L. Walling menu_set_parms(QIPL_FLAG_BM_OPTS_CMD, 0); 1389eaa654aSCollin L. Walling return; 1399eaa654aSCollin L. Walling } 1409eaa654aSCollin L. Walling 1419eaa654aSCollin L. Walling /* If loadparm was set to any other value, then do not enable menu */ 142a0e11b61SCollin Walling if (memcmp(loadparm_str, LOADPARM_EMPTY, LOADPARM_LEN) != 0) { 1439eaa654aSCollin L. Walling return; 1449eaa654aSCollin L. Walling } 1459eaa654aSCollin L. Walling 1469eaa654aSCollin L. Walling switch (iplb.pbt) { 1479eaa654aSCollin L. Walling case S390_IPL_TYPE_CCW: 148ffb4a1c8SCollin L. Walling case S390_IPL_TYPE_QEMU_SCSI: 14953b310ceSCollin L. Walling menu_set_parms(qipl.qipl_flags & BOOT_MENU_FLAG_MASK, 1509eaa654aSCollin L. Walling qipl.boot_menu_timeout); 1519eaa654aSCollin L. Walling return; 1529eaa654aSCollin L. Walling } 1539eaa654aSCollin L. Walling } 1549eaa654aSCollin L. Walling 15587f910c1SJason J. Herne /* 15687f910c1SJason J. Herne * Initialize the channel I/O subsystem so we can talk to our ipl/boot device. 15787f910c1SJason J. Herne */ 15887f910c1SJason J. Herne static void css_setup(void) 15987f910c1SJason J. Herne { 16087f910c1SJason J. Herne /* 16187f910c1SJason J. Herne * Unconditionally enable mss support. In every sane configuration this 16287f910c1SJason J. Herne * will succeed; and even if it doesn't, stsch_err() can handle it. 16387f910c1SJason J. Herne */ 16487f910c1SJason J. Herne enable_mss_facility(); 16587f910c1SJason J. Herne } 16687f910c1SJason J. Herne 167a5f6e097SJason J. Herne /* 168a5f6e097SJason J. Herne * Collect various pieces of information from the hypervisor/hardware that 169a5f6e097SJason J. Herne * we'll use to determine exactly how we'll boot. 170a5f6e097SJason J. Herne */ 171a5f6e097SJason J. Herne static void boot_setup(void) 172a5f6e097SJason J. Herne { 173a5f6e097SJason J. Herne char lpmsg[] = "LOADPARM=[________]\n"; 174a5f6e097SJason J. Herne 175a5f6e097SJason J. Herne sclp_get_loadparm_ascii(loadparm_str); 176a5f6e097SJason J. Herne memcpy(lpmsg + 10, loadparm_str, 8); 177a5f6e097SJason J. Herne sclp_print(lpmsg); 178a5f6e097SJason J. Herne 179a5f6e097SJason J. Herne have_iplb = store_iplb(&iplb); 180a5f6e097SJason J. Herne } 181a5f6e097SJason J. Herne 1827b361db3SJason J. Herne static void find_boot_device(void) 1830f79b89bSAlexander Yarygin { 18499b72e0fSFarhan Ali VDev *vdev = virtio_get_device(); 1857b361db3SJason J. Herne bool found; 1860f79b89bSAlexander Yarygin 187d046c51dSAlexander Yarygin switch (iplb.pbt) { 188d046c51dSAlexander Yarygin case S390_IPL_TYPE_CCW: 1897b361db3SJason J. Herne debug_print_int("device no. ", iplb.ccw.devno); 190d046c51dSAlexander Yarygin blk_schid.ssid = iplb.ccw.ssid & 0x3; 1910f79b89bSAlexander Yarygin debug_print_int("ssid ", blk_schid.ssid); 1927b361db3SJason J. Herne found = find_subch(iplb.ccw.devno); 193d046c51dSAlexander Yarygin break; 194b39b7718SEugene (jno) Dvurechenski case S390_IPL_TYPE_QEMU_SCSI: 195b39b7718SEugene (jno) Dvurechenski vdev->scsi_device_selected = true; 196b39b7718SEugene (jno) Dvurechenski vdev->selected_scsi_device.channel = iplb.scsi.channel; 197b39b7718SEugene (jno) Dvurechenski vdev->selected_scsi_device.target = iplb.scsi.target; 198b39b7718SEugene (jno) Dvurechenski vdev->selected_scsi_device.lun = iplb.scsi.lun; 199b39b7718SEugene (jno) Dvurechenski blk_schid.ssid = iplb.scsi.ssid & 0x3; 2007b361db3SJason J. Herne found = find_subch(iplb.scsi.devno); 201b39b7718SEugene (jno) Dvurechenski break; 202d046c51dSAlexander Yarygin default: 203d046c51dSAlexander Yarygin panic("List-directed IPL not supported yet!\n"); 204d046c51dSAlexander Yarygin } 2057b361db3SJason J. Herne 2067b361db3SJason J. Herne IPL_assert(found, "Boot device not found\n"); 207ff151f4eSDominik Dingel } 20892f2ca38SAlexander Graf 209605751b5SThomas Huth static int virtio_setup(void) 2107b361db3SJason J. Herne { 2117b361db3SJason J. Herne VDev *vdev = virtio_get_device(); 2127b361db3SJason J. Herne QemuIplParameters *early_qipl = (QemuIplParameters *)QIPL_ADDRESS; 2137b361db3SJason J. Herne 2147b361db3SJason J. Herne memcpy(&qipl, early_qipl, sizeof(QemuIplParameters)); 2157b361db3SJason J. Herne 2167b361db3SJason J. Herne if (have_iplb) { 2177b361db3SJason J. Herne menu_setup(); 2187b361db3SJason J. Herne } 21992f2ca38SAlexander Graf 22099b72e0fSFarhan Ali if (virtio_get_device_type() == VIRTIO_ID_NET) { 22199b72e0fSFarhan Ali sclp_print("Network boot device detected\n"); 222118ee80fSCollin L. Walling vdev->netboot_start_addr = qipl.netboot_start_addr; 22399b72e0fSFarhan Ali } else { 224605751b5SThomas Huth int ret = virtio_blk_setup_device(blk_schid); 225605751b5SThomas Huth if (ret) { 226605751b5SThomas Huth return ret; 227605751b5SThomas Huth } 22880ba3e24SEugene (jno) Dvurechenski IPL_assert(virtio_ipl_disk_is_valid(), "No valid IPL device detected"); 22992f2ca38SAlexander Graf } 230605751b5SThomas Huth 231605751b5SThomas Huth return 0; 23299b72e0fSFarhan Ali } 23392f2ca38SAlexander Graf 234d1f060a8SThomas Huth static void ipl_boot_device(void) 23592f2ca38SAlexander Graf { 2363668cb7cSJason J. Herne switch (cutype) { 237efa47d36SJason J. Herne case CU_TYPE_DASD_3990: 238efa47d36SJason J. Herne case CU_TYPE_DASD_2107: 239efa47d36SJason J. Herne dasd_ipl(blk_schid, cutype); /* no return */ 240efa47d36SJason J. Herne break; 2413668cb7cSJason J. Herne case CU_TYPE_VIRTIO: 242605751b5SThomas Huth if (virtio_setup() == 0) { 243*5dc739f3SThomas Huth zipl_load(); /* Only returns in case of errors */ 244605751b5SThomas Huth } 2453668cb7cSJason J. Herne break; 2463668cb7cSJason J. Herne default: 2473668cb7cSJason J. Herne print_int("Attempting to boot from unexpected device type", cutype); 248d1f060a8SThomas Huth panic("\nBoot failed.\n"); 2493668cb7cSJason J. Herne } 250d1f060a8SThomas Huth } 251d1f060a8SThomas Huth 252869d0e2fSThomas Huth /* 253869d0e2fSThomas Huth * No boot device has been specified, so we have to scan through the 254869d0e2fSThomas Huth * channels to find one. 255869d0e2fSThomas Huth */ 256869d0e2fSThomas Huth static void probe_boot_device(void) 257869d0e2fSThomas Huth { 258869d0e2fSThomas Huth int ssid, sch_no, ret; 259869d0e2fSThomas Huth 260869d0e2fSThomas Huth for (ssid = 0; ssid < 0x3; ssid++) { 261869d0e2fSThomas Huth blk_schid.ssid = ssid; 262869d0e2fSThomas Huth for (sch_no = 0; sch_no < 0x10000; sch_no++) { 263869d0e2fSThomas Huth ret = is_dev_possibly_bootable(-1, sch_no); 264869d0e2fSThomas Huth if (ret < 0) { 265869d0e2fSThomas Huth break; 266869d0e2fSThomas Huth } 267869d0e2fSThomas Huth if (ret == true) { 268869d0e2fSThomas Huth ipl_boot_device(); /* Only returns if unsuccessful */ 269869d0e2fSThomas Huth } 270869d0e2fSThomas Huth } 271869d0e2fSThomas Huth } 272869d0e2fSThomas Huth 273869d0e2fSThomas Huth sclp_print("Could not find a suitable boot device (none specified)\n"); 274869d0e2fSThomas Huth } 275869d0e2fSThomas Huth 276d1f060a8SThomas Huth int main(void) 277d1f060a8SThomas Huth { 278d1f060a8SThomas Huth sclp_setup(); 279d1f060a8SThomas Huth css_setup(); 280d1f060a8SThomas Huth boot_setup(); 281869d0e2fSThomas Huth if (have_iplb) { 282d1f060a8SThomas Huth find_boot_device(); 283d1f060a8SThomas Huth enable_subchannel(blk_schid); 284d1f060a8SThomas Huth ipl_boot_device(); 285869d0e2fSThomas Huth } else { 286869d0e2fSThomas Huth probe_boot_device(); 287869d0e2fSThomas Huth } 28860612d5cSEugene (jno) Dvurechenski 289c9262e8aSEugene (jno) Dvurechenski panic("Failed to load OS from hard disk\n"); 29060612d5cSEugene (jno) Dvurechenski return 0; /* make compiler happy */ 29192f2ca38SAlexander Graf } 292