xref: /qemu/pc-bios/s390-ccw/netmain.c (revision 9a848adf)
13e4415a7SThomas Huth /*
23e4415a7SThomas Huth  * S390 virtio-ccw network boot loading program
33e4415a7SThomas Huth  *
43e4415a7SThomas Huth  * Copyright 2017 Thomas Huth, Red Hat Inc.
53e4415a7SThomas Huth  *
63e4415a7SThomas Huth  * Based on the S390 virtio-ccw loading program (main.c)
73e4415a7SThomas Huth  * Copyright (c) 2013 Alexander Graf <agraf@suse.de>
83e4415a7SThomas Huth  *
929d12216SThomas Huth  * And based on the network loading code from SLOF (netload.c)
1029d12216SThomas Huth  * Copyright (c) 2004, 2008 IBM Corporation
1129d12216SThomas Huth  *
123e4415a7SThomas Huth  * This code is free software; you can redistribute it and/or modify it
133e4415a7SThomas Huth  * under the terms of the GNU General Public License as published by the
143e4415a7SThomas Huth  * Free Software Foundation; either version 2 of the License, or (at your
153e4415a7SThomas Huth  * option) any later version.
163e4415a7SThomas Huth  */
173e4415a7SThomas Huth 
183e4415a7SThomas Huth #include <stdint.h>
193e4415a7SThomas Huth #include <stdbool.h>
203e4415a7SThomas Huth #include <stdio.h>
213e4415a7SThomas Huth #include <stdlib.h>
223e4415a7SThomas Huth #include <string.h>
233e4415a7SThomas Huth #include <unistd.h>
2429d12216SThomas Huth 
2529d12216SThomas Huth #include <tftp.h>
2629d12216SThomas Huth #include <ethernet.h>
2729d12216SThomas Huth #include <dhcp.h>
2829d12216SThomas Huth #include <dhcpv6.h>
2929d12216SThomas Huth #include <ipv4.h>
3029d12216SThomas Huth #include <ipv6.h>
3129d12216SThomas Huth #include <dns.h>
323e4415a7SThomas Huth #include <time.h>
333e4415a7SThomas Huth 
343e4415a7SThomas Huth #include "s390-ccw.h"
353e4415a7SThomas Huth #include "virtio.h"
363e4415a7SThomas Huth 
3729d12216SThomas Huth #define DEFAULT_BOOT_RETRIES 10
3829d12216SThomas Huth #define DEFAULT_TFTP_RETRIES 20
3929d12216SThomas Huth 
403e4415a7SThomas Huth extern char _start[];
413e4415a7SThomas Huth 
423e4415a7SThomas Huth char stack[PAGE_SIZE * 8] __attribute__((aligned(PAGE_SIZE)));
433e4415a7SThomas Huth IplParameterBlock iplb __attribute__((aligned(PAGE_SIZE)));
443e4415a7SThomas Huth 
453e4415a7SThomas Huth static SubChannelId net_schid = { .one = 1 };
4629d12216SThomas Huth static int ip_version = 4;
473e4415a7SThomas Huth static uint64_t dest_timer;
483e4415a7SThomas Huth 
493e4415a7SThomas Huth static uint64_t get_timer_ms(void)
503e4415a7SThomas Huth {
513e4415a7SThomas Huth     uint64_t clk;
523e4415a7SThomas Huth 
533e4415a7SThomas Huth     asm volatile(" stck %0 " : : "Q"(clk) : "memory");
543e4415a7SThomas Huth 
553e4415a7SThomas Huth     /* Bit 51 is incremented each microsecond */
563e4415a7SThomas Huth     return (clk >> (63 - 51)) / 1000;
573e4415a7SThomas Huth }
583e4415a7SThomas Huth 
593e4415a7SThomas Huth void set_timer(int val)
603e4415a7SThomas Huth {
613e4415a7SThomas Huth     dest_timer = get_timer_ms() + val;
623e4415a7SThomas Huth }
633e4415a7SThomas Huth 
643e4415a7SThomas Huth int get_timer(void)
653e4415a7SThomas Huth {
663e4415a7SThomas Huth     return dest_timer - get_timer_ms();
673e4415a7SThomas Huth }
683e4415a7SThomas Huth 
693e4415a7SThomas Huth int get_sec_ticks(void)
703e4415a7SThomas Huth {
713e4415a7SThomas Huth     return 1000;    /* number of ticks in 1 second */
723e4415a7SThomas Huth }
733e4415a7SThomas Huth 
7429d12216SThomas Huth /**
7529d12216SThomas Huth  * Obtain IP and configuration info from DHCP server (either IPv4 or IPv6).
7629d12216SThomas Huth  * @param  fn_ip     contains the following configuration information:
7729d12216SThomas Huth  *                   client MAC, client IP, TFTP-server MAC, TFTP-server IP,
7829d12216SThomas Huth  *                   boot file name
7929d12216SThomas Huth  * @param  retries   Number of DHCP attempts
8029d12216SThomas Huth  * @return           0 : IP and configuration info obtained;
8129d12216SThomas Huth  *                   non-0 : error condition occurred.
8229d12216SThomas Huth  */
8329d12216SThomas Huth static int dhcp(struct filename_ip *fn_ip, int retries)
8429d12216SThomas Huth {
8529d12216SThomas Huth     int i = retries + 1;
8629d12216SThomas Huth     int rc = -1;
8729d12216SThomas Huth 
8829d12216SThomas Huth     printf("  Requesting information via DHCP:     ");
8929d12216SThomas Huth 
9029d12216SThomas Huth     dhcpv4_generate_transaction_id();
9129d12216SThomas Huth     dhcpv6_generate_transaction_id();
9229d12216SThomas Huth 
9329d12216SThomas Huth     do {
9429d12216SThomas Huth         printf("\b\b\b%03d", i - 1);
9529d12216SThomas Huth         if (!--i) {
9629d12216SThomas Huth             printf("\nGiving up after %d DHCP requests\n", retries);
9729d12216SThomas Huth             return -1;
9829d12216SThomas Huth         }
9929d12216SThomas Huth         ip_version = 4;
10029d12216SThomas Huth         rc = dhcpv4(NULL, fn_ip);
10129d12216SThomas Huth         if (rc == -1) {
10229d12216SThomas Huth             ip_version = 6;
10329d12216SThomas Huth             set_ipv6_address(fn_ip->fd, 0);
10429d12216SThomas Huth             rc = dhcpv6(NULL, fn_ip);
10529d12216SThomas Huth             if (rc == 0) {
10629d12216SThomas Huth                 memcpy(&fn_ip->own_ip6, get_ipv6_address(), 16);
10729d12216SThomas Huth                 break;
10829d12216SThomas Huth             }
10929d12216SThomas Huth         }
11029d12216SThomas Huth         if (rc != -1) {    /* either success or non-dhcp failure */
11129d12216SThomas Huth             break;
11229d12216SThomas Huth         }
11329d12216SThomas Huth     } while (1);
11429d12216SThomas Huth     printf("\b\b\b\bdone\n");
11529d12216SThomas Huth 
11629d12216SThomas Huth     return rc;
11729d12216SThomas Huth }
11829d12216SThomas Huth 
11929d12216SThomas Huth /**
12029d12216SThomas Huth  * Seed the random number generator with our mac and current timestamp
12129d12216SThomas Huth  */
12229d12216SThomas Huth static void seed_rng(uint8_t mac[])
12329d12216SThomas Huth {
12429d12216SThomas Huth     uint64_t seed;
12529d12216SThomas Huth 
12629d12216SThomas Huth     asm volatile(" stck %0 " : : "Q"(seed) : "memory");
12729d12216SThomas Huth     seed ^= (mac[2] << 24) | (mac[3] << 16) | (mac[4] << 8) | mac[5];
12829d12216SThomas Huth     srand(seed);
12929d12216SThomas Huth }
13029d12216SThomas Huth 
1310c188229SThomas Huth static int tftp_load(filename_ip_t *fnip, void *buffer, int len)
13229d12216SThomas Huth {
13329d12216SThomas Huth     tftp_err_t tftp_err;
13429d12216SThomas Huth     int rc;
13529d12216SThomas Huth 
1360c188229SThomas Huth     rc = tftp(fnip, buffer, len, DEFAULT_TFTP_RETRIES, &tftp_err, 1, 1428,
1370c188229SThomas Huth               ip_version);
13829d12216SThomas Huth 
13929d12216SThomas Huth     if (rc > 0) {
14029d12216SThomas Huth         printf("  TFTP: Received %s (%d KBytes)\n", fnip->filename,
14129d12216SThomas Huth                rc / 1024);
14229d12216SThomas Huth     } else if (rc == -1) {
14329d12216SThomas Huth         puts("unknown TFTP error");
14429d12216SThomas Huth     } else if (rc == -2) {
14529d12216SThomas Huth         printf("TFTP buffer of %d bytes is too small for %s\n",
14629d12216SThomas Huth             len, fnip->filename);
14729d12216SThomas Huth     } else if (rc == -3) {
14829d12216SThomas Huth         printf("file not found: %s\n", fnip->filename);
14929d12216SThomas Huth     } else if (rc == -4) {
15029d12216SThomas Huth         puts("TFTP access violation");
15129d12216SThomas Huth     } else if (rc == -5) {
15229d12216SThomas Huth         puts("illegal TFTP operation");
15329d12216SThomas Huth     } else if (rc == -6) {
15429d12216SThomas Huth         puts("unknown TFTP transfer ID");
15529d12216SThomas Huth     } else if (rc == -7) {
15629d12216SThomas Huth         puts("no such TFTP user");
15729d12216SThomas Huth     } else if (rc == -8) {
15829d12216SThomas Huth         puts("TFTP blocksize negotiation failed");
15929d12216SThomas Huth     } else if (rc == -9) {
16029d12216SThomas Huth         puts("file exceeds maximum TFTP transfer size");
16129d12216SThomas Huth     } else if (rc <= -10 && rc >= -15) {
16229d12216SThomas Huth         const char *icmp_err_str;
16329d12216SThomas Huth         switch (rc) {
16429d12216SThomas Huth         case -ICMP_NET_UNREACHABLE - 10:
16529d12216SThomas Huth             icmp_err_str = "net unreachable";
16629d12216SThomas Huth             break;
16729d12216SThomas Huth         case -ICMP_HOST_UNREACHABLE - 10:
16829d12216SThomas Huth             icmp_err_str = "host unreachable";
16929d12216SThomas Huth             break;
17029d12216SThomas Huth         case -ICMP_PROTOCOL_UNREACHABLE - 10:
17129d12216SThomas Huth             icmp_err_str = "protocol unreachable";
17229d12216SThomas Huth             break;
17329d12216SThomas Huth         case -ICMP_PORT_UNREACHABLE - 10:
17429d12216SThomas Huth             icmp_err_str = "port unreachable";
17529d12216SThomas Huth             break;
17629d12216SThomas Huth         case -ICMP_FRAGMENTATION_NEEDED - 10:
17729d12216SThomas Huth             icmp_err_str = "fragmentation needed and DF set";
17829d12216SThomas Huth             break;
17929d12216SThomas Huth         case -ICMP_SOURCE_ROUTE_FAILED - 10:
18029d12216SThomas Huth             icmp_err_str = "source route failed";
18129d12216SThomas Huth             break;
18229d12216SThomas Huth         default:
18329d12216SThomas Huth             icmp_err_str = " UNKNOWN";
18429d12216SThomas Huth             break;
18529d12216SThomas Huth         }
18629d12216SThomas Huth         printf("ICMP ERROR \"%s\"\n", icmp_err_str);
18729d12216SThomas Huth     } else if (rc == -40) {
18829d12216SThomas Huth         printf("TFTP error occurred after %d bad packets received",
18929d12216SThomas Huth             tftp_err.bad_tftp_packets);
19029d12216SThomas Huth     } else if (rc == -41) {
19129d12216SThomas Huth         printf("TFTP error occurred after missing %d responses",
19229d12216SThomas Huth             tftp_err.no_packets);
19329d12216SThomas Huth     } else if (rc == -42) {
19429d12216SThomas Huth         printf("TFTP error missing block %d, expected block was %d",
19529d12216SThomas Huth             tftp_err.blocks_missed,
19629d12216SThomas Huth             tftp_err.blocks_received);
19729d12216SThomas Huth     }
19829d12216SThomas Huth 
19929d12216SThomas Huth     return rc;
20029d12216SThomas Huth }
20129d12216SThomas Huth 
2020c188229SThomas Huth static int net_init(filename_ip_t *fn_ip)
20329d12216SThomas Huth {
20429d12216SThomas Huth     uint8_t mac[6];
20529d12216SThomas Huth     int rc;
20629d12216SThomas Huth 
2070c188229SThomas Huth     memset(fn_ip, 0, sizeof(filename_ip_t));
20829d12216SThomas Huth 
20929d12216SThomas Huth     rc = virtio_net_init(mac);
21029d12216SThomas Huth     if (rc < 0) {
21129d12216SThomas Huth         puts("Could not initialize network device");
21229d12216SThomas Huth         return -101;
21329d12216SThomas Huth     }
2140c188229SThomas Huth     fn_ip->fd = rc;
21529d12216SThomas Huth 
21629d12216SThomas Huth     printf("  Using MAC address: %02x:%02x:%02x:%02x:%02x:%02x\n",
21729d12216SThomas Huth            mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
21829d12216SThomas Huth 
21929d12216SThomas Huth     set_mac_address(mac);    /* init ethernet layer */
22029d12216SThomas Huth     seed_rng(mac);
22129d12216SThomas Huth 
2220c188229SThomas Huth     rc = dhcp(fn_ip, DEFAULT_BOOT_RETRIES);
22329d12216SThomas Huth     if (rc >= 0) {
22429d12216SThomas Huth         if (ip_version == 4) {
2250c188229SThomas Huth             set_ipv4_address(fn_ip->own_ip);
22629d12216SThomas Huth         }
22729d12216SThomas Huth     } else {
22829d12216SThomas Huth         puts("Could not get IP address");
22929d12216SThomas Huth         return -101;
23029d12216SThomas Huth     }
23129d12216SThomas Huth 
23229d12216SThomas Huth     if (ip_version == 4) {
23329d12216SThomas Huth         printf("  Using IPv4 address: %d.%d.%d.%d\n",
2340c188229SThomas Huth               (fn_ip->own_ip >> 24) & 0xFF, (fn_ip->own_ip >> 16) & 0xFF,
2350c188229SThomas Huth               (fn_ip->own_ip >>  8) & 0xFF, fn_ip->own_ip & 0xFF);
23629d12216SThomas Huth     } else if (ip_version == 6) {
23729d12216SThomas Huth         char ip6_str[40];
2380c188229SThomas Huth         ipv6_to_str(fn_ip->own_ip6.addr, ip6_str);
23929d12216SThomas Huth         printf("  Using IPv6 address: %s\n", ip6_str);
24029d12216SThomas Huth     }
24129d12216SThomas Huth 
24229d12216SThomas Huth     if (rc == -2) {
24329d12216SThomas Huth         printf("ARP request to TFTP server (%d.%d.%d.%d) failed\n",
2440c188229SThomas Huth                (fn_ip->server_ip >> 24) & 0xFF, (fn_ip->server_ip >> 16) & 0xFF,
2450c188229SThomas Huth                (fn_ip->server_ip >>  8) & 0xFF, fn_ip->server_ip & 0xFF);
24629d12216SThomas Huth         return -102;
24729d12216SThomas Huth     }
24829d12216SThomas Huth     if (rc == -4 || rc == -3) {
24929d12216SThomas Huth         puts("Can't obtain TFTP server IP address");
25029d12216SThomas Huth         return -107;
25129d12216SThomas Huth     }
25229d12216SThomas Huth 
2530c188229SThomas Huth     printf("  Using TFTP server: ");
25429d12216SThomas Huth     if (ip_version == 4) {
2550c188229SThomas Huth         printf("%d.%d.%d.%d\n",
2560c188229SThomas Huth                (fn_ip->server_ip >> 24) & 0xFF, (fn_ip->server_ip >> 16) & 0xFF,
2570c188229SThomas Huth                (fn_ip->server_ip >>  8) & 0xFF, fn_ip->server_ip & 0xFF);
25829d12216SThomas Huth     } else if (ip_version == 6) {
25929d12216SThomas Huth         char ip6_str[40];
2600c188229SThomas Huth         ipv6_to_str(fn_ip->server_ip6.addr, ip6_str);
26129d12216SThomas Huth         printf("%s\n", ip6_str);
26229d12216SThomas Huth     }
26329d12216SThomas Huth 
2640c188229SThomas Huth     if (strlen((char *)fn_ip->filename) > 0) {
2650c188229SThomas Huth         printf("  Bootfile name: '%s'\n", fn_ip->filename);
26629d12216SThomas Huth     }
26729d12216SThomas Huth 
26829d12216SThomas Huth     return rc;
26929d12216SThomas Huth }
27029d12216SThomas Huth 
2710c188229SThomas Huth static void net_release(filename_ip_t *fn_ip)
2720c188229SThomas Huth {
2730c188229SThomas Huth     if (ip_version == 4) {
2740c188229SThomas Huth         dhcp_send_release(fn_ip->fd);
2750c188229SThomas Huth     }
2760c188229SThomas Huth }
2770c188229SThomas Huth 
2783e4415a7SThomas Huth void panic(const char *string)
2793e4415a7SThomas Huth {
2803e4415a7SThomas Huth     sclp_print(string);
2813e4415a7SThomas Huth     for (;;) {
2823e4415a7SThomas Huth         disabled_wait();
2833e4415a7SThomas Huth     }
2843e4415a7SThomas Huth }
2853e4415a7SThomas Huth 
286*9a848adfSThomas Huth void write_subsystem_identification(void)
287*9a848adfSThomas Huth {
288*9a848adfSThomas Huth     SubChannelId *schid = (SubChannelId *) 184;
289*9a848adfSThomas Huth     uint32_t *zeroes = (uint32_t *) 188;
290*9a848adfSThomas Huth 
291*9a848adfSThomas Huth     *schid = net_schid;
292*9a848adfSThomas Huth     *zeroes = 0;
293*9a848adfSThomas Huth }
294*9a848adfSThomas Huth 
2953e4415a7SThomas Huth static bool find_net_dev(Schib *schib, int dev_no)
2963e4415a7SThomas Huth {
2973e4415a7SThomas Huth     int i, r;
2983e4415a7SThomas Huth 
2993e4415a7SThomas Huth     for (i = 0; i < 0x10000; i++) {
3003e4415a7SThomas Huth         net_schid.sch_no = i;
3013e4415a7SThomas Huth         r = stsch_err(net_schid, schib);
3023e4415a7SThomas Huth         if (r == 3 || r == -EIO) {
3033e4415a7SThomas Huth             break;
3043e4415a7SThomas Huth         }
3053e4415a7SThomas Huth         if (!schib->pmcw.dnv) {
3063e4415a7SThomas Huth             continue;
3073e4415a7SThomas Huth         }
3083e4415a7SThomas Huth         if (!virtio_is_supported(net_schid)) {
3093e4415a7SThomas Huth             continue;
3103e4415a7SThomas Huth         }
3113e4415a7SThomas Huth         if (virtio_get_device_type() != VIRTIO_ID_NET) {
3123e4415a7SThomas Huth             continue;
3133e4415a7SThomas Huth         }
3143e4415a7SThomas Huth         if (dev_no < 0 || schib->pmcw.dev == dev_no) {
3153e4415a7SThomas Huth             return true;
3163e4415a7SThomas Huth         }
3173e4415a7SThomas Huth     }
3183e4415a7SThomas Huth 
3193e4415a7SThomas Huth     return false;
3203e4415a7SThomas Huth }
3213e4415a7SThomas Huth 
3223e4415a7SThomas Huth static void virtio_setup(void)
3233e4415a7SThomas Huth {
3243e4415a7SThomas Huth     Schib schib;
3253e4415a7SThomas Huth     int ssid;
3263e4415a7SThomas Huth     bool found = false;
3273e4415a7SThomas Huth     uint16_t dev_no;
3283e4415a7SThomas Huth 
3293e4415a7SThomas Huth     /*
3303e4415a7SThomas Huth      * We unconditionally enable mss support. In every sane configuration,
3313e4415a7SThomas Huth      * this will succeed; and even if it doesn't, stsch_err() can deal
3323e4415a7SThomas Huth      * with the consequences.
3333e4415a7SThomas Huth      */
3343e4415a7SThomas Huth     enable_mss_facility();
3353e4415a7SThomas Huth 
3363e4415a7SThomas Huth     if (store_iplb(&iplb)) {
3373e4415a7SThomas Huth         IPL_assert(iplb.pbt == S390_IPL_TYPE_CCW, "IPL_TYPE_CCW expected");
3383e4415a7SThomas Huth         dev_no = iplb.ccw.devno;
3393e4415a7SThomas Huth         debug_print_int("device no. ", dev_no);
3403e4415a7SThomas Huth         net_schid.ssid = iplb.ccw.ssid & 0x3;
3413e4415a7SThomas Huth         debug_print_int("ssid ", net_schid.ssid);
3423e4415a7SThomas Huth         found = find_net_dev(&schib, dev_no);
3433e4415a7SThomas Huth     } else {
3443e4415a7SThomas Huth         for (ssid = 0; ssid < 0x3; ssid++) {
3453e4415a7SThomas Huth             net_schid.ssid = ssid;
3463e4415a7SThomas Huth             found = find_net_dev(&schib, -1);
3473e4415a7SThomas Huth             if (found) {
3483e4415a7SThomas Huth                 break;
3493e4415a7SThomas Huth             }
3503e4415a7SThomas Huth         }
3513e4415a7SThomas Huth     }
3523e4415a7SThomas Huth 
3533e4415a7SThomas Huth     IPL_assert(found, "No virtio net device found");
3543e4415a7SThomas Huth }
3553e4415a7SThomas Huth 
3563e4415a7SThomas Huth void main(void)
3573e4415a7SThomas Huth {
3580c188229SThomas Huth     filename_ip_t fn_ip;
35929d12216SThomas Huth     int rc;
36029d12216SThomas Huth 
3613e4415a7SThomas Huth     sclp_setup();
3623e4415a7SThomas Huth     sclp_print("Network boot starting...\n");
3633e4415a7SThomas Huth 
3643e4415a7SThomas Huth     virtio_setup();
3653e4415a7SThomas Huth 
3660c188229SThomas Huth     rc = net_init(&fn_ip);
3670c188229SThomas Huth     if (rc) {
3680c188229SThomas Huth         panic("Network initialization failed. Halting.\n");
3690c188229SThomas Huth     }
3700c188229SThomas Huth 
3710c188229SThomas Huth     rc = tftp_load(&fn_ip, NULL, (long)_start);
3720c188229SThomas Huth 
3730c188229SThomas Huth     net_release(&fn_ip);
3740c188229SThomas Huth 
37529d12216SThomas Huth     if (rc > 0) {
37629d12216SThomas Huth         sclp_print("Network loading done, starting kernel...\n");
377*9a848adfSThomas Huth         jump_to_low_kernel();
37829d12216SThomas Huth     }
37929d12216SThomas Huth 
3803e4415a7SThomas Huth     panic("Failed to load OS from network\n");
3813e4415a7SThomas Huth }
382