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 2092f2ca38SAlexander Graf char stack[PAGE_SIZE * 8] __attribute__((__aligned__(PAGE_SIZE))); 21b88d7fa5SEugene (jno) Dvurechenski static SubChannelId blk_schid = { .one = 1 }; 226673ded7SThomas Huth static char loadparm_str[LOADPARM_LEN + 1]; 23118ee80fSCollin L. Walling QemuIplParameters qipl; 24a5f6e097SJason J. Herne IplParameterBlock iplb __attribute__((__aligned__(PAGE_SIZE))); 25a5f6e097SJason J. Herne static bool have_iplb; 262880469cSJason J. Herne static uint16_t cutype; 279bfc04f9SJanosch Frank LowCore *lowcore; /* Yes, this *is* a pointer to address 0 */ 28f2879a5cSChristian Borntraeger 299eaa654aSCollin L. Walling #define LOADPARM_PROMPT "PROMPT " 30074afe60SCollin Walling #define LOADPARM_EMPTY " " 3153b310ceSCollin L. Walling #define BOOT_MENU_FLAG_MASK (QIPL_FLAG_BM_OPTS_CMD | QIPL_FLAG_BM_OPTS_ZIPL) 329eaa654aSCollin L. Walling 33f2879a5cSChristian Borntraeger /* 34a5f6e097SJason J. Herne * Principles of Operations (SA22-7832-09) chapter 17 requires that 35f2879a5cSChristian Borntraeger * a subsystem-identification is at 184-187 and bytes 188-191 are zero 36f2879a5cSChristian Borntraeger * after list-directed-IPL and ccw-IPL. 37f2879a5cSChristian Borntraeger */ 38f2879a5cSChristian Borntraeger void write_subsystem_identification(void) 39f2879a5cSChristian Borntraeger { 40e6d393d0SJanosch Frank lowcore->subchannel_id = blk_schid.sch_id; 41e6d393d0SJanosch Frank lowcore->subchannel_nr = blk_schid.sch_no; 42e6d393d0SJanosch Frank lowcore->io_int_parm = 0; 43f2879a5cSChristian Borntraeger } 44f2879a5cSChristian Borntraeger 459bfc04f9SJanosch Frank void write_iplb_location(void) 469bfc04f9SJanosch Frank { 47872882e7SJason J. Herne if (cutype == CU_TYPE_VIRTIO && virtio_get_device_type() != VIRTIO_ID_NET) { 489bfc04f9SJanosch Frank lowcore->ptr_iplb = ptr2u32(&iplb); 499bfc04f9SJanosch Frank } 50872882e7SJason J. Herne } 519bfc04f9SJanosch Frank 5295fa1af8SFarhan Ali unsigned int get_loadparm_index(void) 5395fa1af8SFarhan Ali { 54074afe60SCollin Walling return atoui(loadparm_str); 5595fa1af8SFarhan Ali } 5695fa1af8SFarhan Ali 57d2cf4af1SThomas Huth static int is_dev_possibly_bootable(int dev_no, int sch_no) 5892f2ca38SAlexander Graf { 592880469cSJason J. Herne bool is_virtio; 60d2cf4af1SThomas Huth Schib schib; 61d2cf4af1SThomas Huth int r; 62ff151f4eSDominik Dingel 63d2cf4af1SThomas Huth blk_schid.sch_no = sch_no; 647b361db3SJason J. Herne r = stsch_err(blk_schid, &schib); 65d2cf4af1SThomas Huth if (r == 3 || r == -EIO) { 66d2cf4af1SThomas Huth return -ENODEV; 6722d67ab5SCornelia Huck } 687b361db3SJason J. Herne if (!schib.pmcw.dnv) { 69d2cf4af1SThomas Huth return false; 7092f2ca38SAlexander Graf } 71930072d2SJason J. Herne 723668cb7cSJason J. Herne enable_subchannel(blk_schid); 732880469cSJason J. Herne cutype = cu_type(blk_schid); 742880469cSJason J. Herne 752880469cSJason J. Herne /* 762880469cSJason J. Herne * Note: we always have to run virtio_is_supported() here to make 772880469cSJason J. Herne * sure that the vdev.senseid data gets pre-initialized correctly 782880469cSJason J. Herne */ 792880469cSJason J. Herne is_virtio = virtio_is_supported(blk_schid); 802880469cSJason J. Herne 81d2cf4af1SThomas Huth /* No specific devno given, just return whether the device is possibly bootable */ 822880469cSJason J. Herne if (dev_no < 0) { 832880469cSJason J. Herne switch (cutype) { 842880469cSJason J. Herne case CU_TYPE_VIRTIO: 852880469cSJason J. Herne if (is_virtio) { 862880469cSJason J. Herne /* 872880469cSJason J. Herne * Skip net devices since no IPLB is created and therefore 882880469cSJason J. Herne * no network bootloader has been loaded 892880469cSJason J. Herne */ 902880469cSJason J. Herne if (virtio_get_device_type() != VIRTIO_ID_NET) { 912880469cSJason J. Herne return true; 922880469cSJason J. Herne } 932880469cSJason J. Herne } 94d2cf4af1SThomas Huth return false; 952880469cSJason J. Herne case CU_TYPE_DASD_3990: 962880469cSJason J. Herne case CU_TYPE_DASD_2107: 972880469cSJason J. Herne return true; 982880469cSJason J. Herne default: 99d2cf4af1SThomas Huth return false; 10099b72e0fSFarhan Ali } 1012880469cSJason J. Herne } 102930072d2SJason J. Herne 1032880469cSJason J. Herne /* Caller asked for a specific devno */ 1042880469cSJason J. Herne if (schib.pmcw.dev == dev_no) { 1050f79b89bSAlexander Yarygin return true; 1060f79b89bSAlexander Yarygin } 107d2cf4af1SThomas Huth 108d2cf4af1SThomas Huth return false; 109d2cf4af1SThomas Huth } 110d2cf4af1SThomas Huth 111d2cf4af1SThomas Huth /* 112d2cf4af1SThomas Huth * Find the subchannel connected to the given device (dev_no) and fill in the 113d2cf4af1SThomas Huth * subchannel information block (schib) with the connected subchannel's info. 114d2cf4af1SThomas Huth * NOTE: The global variable blk_schid is updated to contain the subchannel 115d2cf4af1SThomas Huth * information. 116d2cf4af1SThomas Huth * 117d2cf4af1SThomas Huth * If the caller gives dev_no=-1 then the user did not specify a boot device. 118d2cf4af1SThomas Huth * In this case we'll just use the first potentially bootable device we find. 119d2cf4af1SThomas Huth */ 120d2cf4af1SThomas Huth static bool find_subch(int dev_no) 121d2cf4af1SThomas Huth { 122d2cf4af1SThomas Huth int i, r; 123d2cf4af1SThomas Huth 124d2cf4af1SThomas Huth for (i = 0; i < 0x10000; i++) { 125d2cf4af1SThomas Huth r = is_dev_possibly_bootable(dev_no, i); 126d2cf4af1SThomas Huth if (r < 0) { 127d2cf4af1SThomas Huth break; 128d2cf4af1SThomas Huth } 129d2cf4af1SThomas Huth if (r == true) { 130d2cf4af1SThomas Huth return true; 131d2cf4af1SThomas Huth } 1320f79b89bSAlexander Yarygin } 1330f79b89bSAlexander Yarygin 1340f79b89bSAlexander Yarygin return false; 1350f79b89bSAlexander Yarygin } 1360f79b89bSAlexander Yarygin 1379eaa654aSCollin L. Walling static void menu_setup(void) 1389eaa654aSCollin L. Walling { 139a0e11b61SCollin Walling if (memcmp(loadparm_str, LOADPARM_PROMPT, LOADPARM_LEN) == 0) { 1409eaa654aSCollin L. Walling menu_set_parms(QIPL_FLAG_BM_OPTS_CMD, 0); 1419eaa654aSCollin L. Walling return; 1429eaa654aSCollin L. Walling } 1439eaa654aSCollin L. Walling 1449eaa654aSCollin L. Walling /* If loadparm was set to any other value, then do not enable menu */ 145a0e11b61SCollin Walling if (memcmp(loadparm_str, LOADPARM_EMPTY, LOADPARM_LEN) != 0) { 1469eaa654aSCollin L. Walling return; 1479eaa654aSCollin L. Walling } 1489eaa654aSCollin L. Walling 1499eaa654aSCollin L. Walling switch (iplb.pbt) { 1509eaa654aSCollin L. Walling case S390_IPL_TYPE_CCW: 151ffb4a1c8SCollin L. Walling case S390_IPL_TYPE_QEMU_SCSI: 15253b310ceSCollin L. Walling menu_set_parms(qipl.qipl_flags & BOOT_MENU_FLAG_MASK, 1539eaa654aSCollin L. Walling qipl.boot_menu_timeout); 1549eaa654aSCollin L. Walling return; 1559eaa654aSCollin L. Walling } 1569eaa654aSCollin L. Walling } 1579eaa654aSCollin L. Walling 15887f910c1SJason J. Herne /* 15987f910c1SJason J. Herne * Initialize the channel I/O subsystem so we can talk to our ipl/boot device. 16087f910c1SJason J. Herne */ 16187f910c1SJason J. Herne static void css_setup(void) 16287f910c1SJason J. Herne { 16387f910c1SJason J. Herne /* 16487f910c1SJason J. Herne * Unconditionally enable mss support. In every sane configuration this 16587f910c1SJason J. Herne * will succeed; and even if it doesn't, stsch_err() can handle it. 16687f910c1SJason J. Herne */ 16787f910c1SJason J. Herne enable_mss_facility(); 16887f910c1SJason J. Herne } 16987f910c1SJason J. Herne 170a5f6e097SJason J. Herne /* 171a5f6e097SJason J. Herne * Collect various pieces of information from the hypervisor/hardware that 172a5f6e097SJason J. Herne * we'll use to determine exactly how we'll boot. 173a5f6e097SJason J. Herne */ 174a5f6e097SJason J. Herne static void boot_setup(void) 175a5f6e097SJason J. Herne { 176a5f6e097SJason J. Herne char lpmsg[] = "LOADPARM=[________]\n"; 177a5f6e097SJason J. Herne 178a5f6e097SJason J. Herne sclp_get_loadparm_ascii(loadparm_str); 179a5f6e097SJason J. Herne memcpy(lpmsg + 10, loadparm_str, 8); 180a5f6e097SJason J. Herne sclp_print(lpmsg); 181a5f6e097SJason J. Herne 1823d651996SEric Farman /* 1833d651996SEric Farman * Clear out any potential S390EP magic (see jump_to_low_kernel()), 1843d651996SEric Farman * so we don't taint our decision-making process during a reboot. 1853d651996SEric Farman */ 1863d651996SEric Farman memset((char *)S390EP, 0, 6); 1873d651996SEric Farman 188a5f6e097SJason J. Herne have_iplb = store_iplb(&iplb); 189a5f6e097SJason J. Herne } 190a5f6e097SJason J. Herne 1917b361db3SJason J. Herne static void find_boot_device(void) 1920f79b89bSAlexander Yarygin { 19399b72e0fSFarhan Ali VDev *vdev = virtio_get_device(); 1947b361db3SJason J. Herne bool found; 1950f79b89bSAlexander Yarygin 196d046c51dSAlexander Yarygin switch (iplb.pbt) { 197d046c51dSAlexander Yarygin case S390_IPL_TYPE_CCW: 1987b361db3SJason J. Herne debug_print_int("device no. ", iplb.ccw.devno); 199d046c51dSAlexander Yarygin blk_schid.ssid = iplb.ccw.ssid & 0x3; 2000f79b89bSAlexander Yarygin debug_print_int("ssid ", blk_schid.ssid); 2017b361db3SJason J. Herne found = find_subch(iplb.ccw.devno); 202d046c51dSAlexander Yarygin break; 203b39b7718SEugene (jno) Dvurechenski case S390_IPL_TYPE_QEMU_SCSI: 204b39b7718SEugene (jno) Dvurechenski vdev->scsi_device_selected = true; 205b39b7718SEugene (jno) Dvurechenski vdev->selected_scsi_device.channel = iplb.scsi.channel; 206b39b7718SEugene (jno) Dvurechenski vdev->selected_scsi_device.target = iplb.scsi.target; 207b39b7718SEugene (jno) Dvurechenski vdev->selected_scsi_device.lun = iplb.scsi.lun; 208b39b7718SEugene (jno) Dvurechenski blk_schid.ssid = iplb.scsi.ssid & 0x3; 2097b361db3SJason J. Herne found = find_subch(iplb.scsi.devno); 210b39b7718SEugene (jno) Dvurechenski break; 211d046c51dSAlexander Yarygin default: 212d046c51dSAlexander Yarygin panic("List-directed IPL not supported yet!\n"); 213d046c51dSAlexander Yarygin } 2147b361db3SJason J. Herne 2157b361db3SJason J. Herne IPL_assert(found, "Boot device not found\n"); 216ff151f4eSDominik Dingel } 21792f2ca38SAlexander Graf 218605751b5SThomas Huth static int virtio_setup(void) 2197b361db3SJason J. Herne { 2207b361db3SJason J. Herne VDev *vdev = virtio_get_device(); 2217b361db3SJason J. Herne QemuIplParameters *early_qipl = (QemuIplParameters *)QIPL_ADDRESS; 222cf30b7c4SThomas Huth int ret; 2237b361db3SJason J. Herne 2247b361db3SJason J. Herne memcpy(&qipl, early_qipl, sizeof(QemuIplParameters)); 2257b361db3SJason J. Herne 2267b361db3SJason J. Herne if (have_iplb) { 2277b361db3SJason J. Herne menu_setup(); 2287b361db3SJason J. Herne } 22992f2ca38SAlexander Graf 230cf30b7c4SThomas Huth switch (vdev->senseid.cu_model) { 231cf30b7c4SThomas Huth case VIRTIO_ID_NET: 23299b72e0fSFarhan Ali sclp_print("Network boot device detected\n"); 233118ee80fSCollin L. Walling vdev->netboot_start_addr = qipl.netboot_start_addr; 234cf30b7c4SThomas Huth return 0; 235cf30b7c4SThomas Huth case VIRTIO_ID_BLOCK: 236cf30b7c4SThomas Huth ret = virtio_blk_setup_device(blk_schid); 237cf30b7c4SThomas Huth break; 238cf30b7c4SThomas Huth case VIRTIO_ID_SCSI: 239cf30b7c4SThomas Huth ret = virtio_scsi_setup_device(blk_schid); 240cf30b7c4SThomas Huth break; 241cf30b7c4SThomas Huth default: 242cf30b7c4SThomas Huth panic("\n! No IPL device available !\n"); 243605751b5SThomas Huth } 244cf30b7c4SThomas Huth 245cf30b7c4SThomas Huth if (!ret) { 24680ba3e24SEugene (jno) Dvurechenski IPL_assert(virtio_ipl_disk_is_valid(), "No valid IPL device detected"); 24792f2ca38SAlexander Graf } 248605751b5SThomas Huth 249cf30b7c4SThomas Huth return ret; 25099b72e0fSFarhan Ali } 25192f2ca38SAlexander Graf 252d1f060a8SThomas Huth static void ipl_boot_device(void) 25392f2ca38SAlexander Graf { 2543668cb7cSJason J. Herne switch (cutype) { 255efa47d36SJason J. Herne case CU_TYPE_DASD_3990: 256efa47d36SJason J. Herne case CU_TYPE_DASD_2107: 257efa47d36SJason J. Herne dasd_ipl(blk_schid, cutype); /* no return */ 258efa47d36SJason J. Herne break; 2593668cb7cSJason J. Herne case CU_TYPE_VIRTIO: 260605751b5SThomas Huth if (virtio_setup() == 0) { 2615dc739f3SThomas Huth zipl_load(); /* Only returns in case of errors */ 262605751b5SThomas Huth } 2633668cb7cSJason J. Herne break; 2643668cb7cSJason J. Herne default: 2653668cb7cSJason J. Herne print_int("Attempting to boot from unexpected device type", cutype); 266d1f060a8SThomas Huth panic("\nBoot failed.\n"); 2673668cb7cSJason J. Herne } 268d1f060a8SThomas Huth } 269d1f060a8SThomas Huth 270869d0e2fSThomas Huth /* 271869d0e2fSThomas Huth * No boot device has been specified, so we have to scan through the 272869d0e2fSThomas Huth * channels to find one. 273869d0e2fSThomas Huth */ 274869d0e2fSThomas Huth static void probe_boot_device(void) 275869d0e2fSThomas Huth { 276869d0e2fSThomas Huth int ssid, sch_no, ret; 277869d0e2fSThomas Huth 278869d0e2fSThomas Huth for (ssid = 0; ssid < 0x3; ssid++) { 279869d0e2fSThomas Huth blk_schid.ssid = ssid; 280869d0e2fSThomas Huth for (sch_no = 0; sch_no < 0x10000; sch_no++) { 281869d0e2fSThomas Huth ret = is_dev_possibly_bootable(-1, sch_no); 282869d0e2fSThomas Huth if (ret < 0) { 283869d0e2fSThomas Huth break; 284869d0e2fSThomas Huth } 285869d0e2fSThomas Huth if (ret == true) { 286869d0e2fSThomas Huth ipl_boot_device(); /* Only returns if unsuccessful */ 287869d0e2fSThomas Huth } 288869d0e2fSThomas Huth } 289869d0e2fSThomas Huth } 290869d0e2fSThomas Huth 291869d0e2fSThomas Huth sclp_print("Could not find a suitable boot device (none specified)\n"); 292869d0e2fSThomas Huth } 293869d0e2fSThomas Huth 2942ba3cc47SThomas Huth void main(void) 295d1f060a8SThomas Huth { 296d1f060a8SThomas Huth sclp_setup(); 297d1f060a8SThomas Huth css_setup(); 298d1f060a8SThomas Huth boot_setup(); 299869d0e2fSThomas Huth if (have_iplb) { 300d1f060a8SThomas Huth find_boot_device(); 301d1f060a8SThomas Huth ipl_boot_device(); 302869d0e2fSThomas Huth } else { 303869d0e2fSThomas Huth probe_boot_device(); 304869d0e2fSThomas Huth } 30560612d5cSEugene (jno) Dvurechenski 306c9262e8aSEugene (jno) Dvurechenski panic("Failed to load OS from hard disk\n"); 30792f2ca38SAlexander Graf } 308