ppc/kvm: drop kvmppc_has_cap_htab_fd()
[qemu/ar7.git] / pc-bios / s390-ccw / netmain.c
blobd86d46b03f17038d3b016a9286f515573cd46b7f
1 /*
2 * S390 virtio-ccw network boot loading program
4 * Copyright 2017 Thomas Huth, Red Hat Inc.
6 * Based on the S390 virtio-ccw loading program (main.c)
7 * Copyright (c) 2013 Alexander Graf <agraf@suse.de>
9 * And based on the network loading code from SLOF (netload.c)
10 * Copyright (c) 2004, 2008 IBM Corporation
12 * This code is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2 of the License, or (at your
15 * option) any later version.
18 #include <stdint.h>
19 #include <stdbool.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <unistd.h>
25 #include <tftp.h>
26 #include <ethernet.h>
27 #include <dhcp.h>
28 #include <dhcpv6.h>
29 #include <ipv4.h>
30 #include <ipv6.h>
31 #include <dns.h>
32 #include <time.h>
34 #include "s390-ccw.h"
35 #include "virtio.h"
37 #define DEFAULT_BOOT_RETRIES 10
38 #define DEFAULT_TFTP_RETRIES 20
40 extern char _start[];
42 char stack[PAGE_SIZE * 8] __attribute__((aligned(PAGE_SIZE)));
43 IplParameterBlock iplb __attribute__((aligned(PAGE_SIZE)));
45 static SubChannelId net_schid = { .one = 1 };
46 static int ip_version = 4;
47 static uint64_t dest_timer;
49 static uint64_t get_timer_ms(void)
51 uint64_t clk;
53 asm volatile(" stck %0 " : : "Q"(clk) : "memory");
55 /* Bit 51 is incremented each microsecond */
56 return (clk >> (63 - 51)) / 1000;
59 void set_timer(int val)
61 dest_timer = get_timer_ms() + val;
64 int get_timer(void)
66 return dest_timer - get_timer_ms();
69 int get_sec_ticks(void)
71 return 1000; /* number of ticks in 1 second */
74 /**
75 * Obtain IP and configuration info from DHCP server (either IPv4 or IPv6).
76 * @param fn_ip contains the following configuration information:
77 * client MAC, client IP, TFTP-server MAC, TFTP-server IP,
78 * boot file name
79 * @param retries Number of DHCP attempts
80 * @return 0 : IP and configuration info obtained;
81 * non-0 : error condition occurred.
83 static int dhcp(struct filename_ip *fn_ip, int retries)
85 int i = retries + 1;
86 int rc = -1;
88 printf(" Requesting information via DHCP: ");
90 dhcpv4_generate_transaction_id();
91 dhcpv6_generate_transaction_id();
93 do {
94 printf("\b\b\b%03d", i - 1);
95 if (!--i) {
96 printf("\nGiving up after %d DHCP requests\n", retries);
97 return -1;
99 ip_version = 4;
100 rc = dhcpv4(NULL, fn_ip);
101 if (rc == -1) {
102 ip_version = 6;
103 set_ipv6_address(fn_ip->fd, 0);
104 rc = dhcpv6(NULL, fn_ip);
105 if (rc == 0) {
106 memcpy(&fn_ip->own_ip6, get_ipv6_address(), 16);
107 break;
110 if (rc != -1) { /* either success or non-dhcp failure */
111 break;
113 } while (1);
114 printf("\b\b\b\bdone\n");
116 return rc;
120 * Seed the random number generator with our mac and current timestamp
122 static void seed_rng(uint8_t mac[])
124 uint64_t seed;
126 asm volatile(" stck %0 " : : "Q"(seed) : "memory");
127 seed ^= (mac[2] << 24) | (mac[3] << 16) | (mac[4] << 8) | mac[5];
128 srand(seed);
131 static int tftp_load(filename_ip_t *fnip, void *buffer, int len,
132 unsigned int retries, int ip_vers)
134 tftp_err_t tftp_err;
135 int rc;
137 rc = tftp(fnip, buffer, len, retries, &tftp_err, 1, 1428, ip_vers);
139 if (rc > 0) {
140 printf(" TFTP: Received %s (%d KBytes)\n", fnip->filename,
141 rc / 1024);
142 } else if (rc == -1) {
143 puts("unknown TFTP error");
144 } else if (rc == -2) {
145 printf("TFTP buffer of %d bytes is too small for %s\n",
146 len, fnip->filename);
147 } else if (rc == -3) {
148 printf("file not found: %s\n", fnip->filename);
149 } else if (rc == -4) {
150 puts("TFTP access violation");
151 } else if (rc == -5) {
152 puts("illegal TFTP operation");
153 } else if (rc == -6) {
154 puts("unknown TFTP transfer ID");
155 } else if (rc == -7) {
156 puts("no such TFTP user");
157 } else if (rc == -8) {
158 puts("TFTP blocksize negotiation failed");
159 } else if (rc == -9) {
160 puts("file exceeds maximum TFTP transfer size");
161 } else if (rc <= -10 && rc >= -15) {
162 const char *icmp_err_str;
163 switch (rc) {
164 case -ICMP_NET_UNREACHABLE - 10:
165 icmp_err_str = "net unreachable";
166 break;
167 case -ICMP_HOST_UNREACHABLE - 10:
168 icmp_err_str = "host unreachable";
169 break;
170 case -ICMP_PROTOCOL_UNREACHABLE - 10:
171 icmp_err_str = "protocol unreachable";
172 break;
173 case -ICMP_PORT_UNREACHABLE - 10:
174 icmp_err_str = "port unreachable";
175 break;
176 case -ICMP_FRAGMENTATION_NEEDED - 10:
177 icmp_err_str = "fragmentation needed and DF set";
178 break;
179 case -ICMP_SOURCE_ROUTE_FAILED - 10:
180 icmp_err_str = "source route failed";
181 break;
182 default:
183 icmp_err_str = " UNKNOWN";
184 break;
186 printf("ICMP ERROR \"%s\"\n", icmp_err_str);
187 } else if (rc == -40) {
188 printf("TFTP error occurred after %d bad packets received",
189 tftp_err.bad_tftp_packets);
190 } else if (rc == -41) {
191 printf("TFTP error occurred after missing %d responses",
192 tftp_err.no_packets);
193 } else if (rc == -42) {
194 printf("TFTP error missing block %d, expected block was %d",
195 tftp_err.blocks_missed,
196 tftp_err.blocks_received);
199 return rc;
202 static int net_load(char *buffer, int len)
204 filename_ip_t fn_ip;
205 uint8_t mac[6];
206 int rc;
208 memset(&fn_ip, 0, sizeof(filename_ip_t));
210 rc = virtio_net_init(mac);
211 if (rc < 0) {
212 puts("Could not initialize network device");
213 return -101;
215 fn_ip.fd = rc;
217 printf(" Using MAC address: %02x:%02x:%02x:%02x:%02x:%02x\n",
218 mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
220 set_mac_address(mac); /* init ethernet layer */
221 seed_rng(mac);
223 rc = dhcp(&fn_ip, DEFAULT_BOOT_RETRIES);
224 if (rc >= 0) {
225 if (ip_version == 4) {
226 set_ipv4_address(fn_ip.own_ip);
228 } else {
229 puts("Could not get IP address");
230 return -101;
233 if (ip_version == 4) {
234 printf(" Using IPv4 address: %d.%d.%d.%d\n",
235 (fn_ip.own_ip >> 24) & 0xFF, (fn_ip.own_ip >> 16) & 0xFF,
236 (fn_ip.own_ip >> 8) & 0xFF, fn_ip.own_ip & 0xFF);
237 } else if (ip_version == 6) {
238 char ip6_str[40];
239 ipv6_to_str(fn_ip.own_ip6.addr, ip6_str);
240 printf(" Using IPv6 address: %s\n", ip6_str);
243 if (rc == -2) {
244 printf("ARP request to TFTP server (%d.%d.%d.%d) failed\n",
245 (fn_ip.server_ip >> 24) & 0xFF, (fn_ip.server_ip >> 16) & 0xFF,
246 (fn_ip.server_ip >> 8) & 0xFF, fn_ip.server_ip & 0xFF);
247 return -102;
249 if (rc == -4 || rc == -3) {
250 puts("Can't obtain TFTP server IP address");
251 return -107;
254 if (ip_version == 4) {
255 printf(" Requesting file \"%s\" via TFTP from %d.%d.%d.%d\n",
256 fn_ip.filename,
257 (fn_ip.server_ip >> 24) & 0xFF, (fn_ip.server_ip >> 16) & 0xFF,
258 (fn_ip.server_ip >> 8) & 0xFF, fn_ip.server_ip & 0xFF);
259 } else if (ip_version == 6) {
260 char ip6_str[40];
261 printf(" Requesting file \"%s\" via TFTP from ", fn_ip.filename);
262 ipv6_to_str(fn_ip.server_ip6.addr, ip6_str);
263 printf("%s\n", ip6_str);
266 /* Do the TFTP load and print error message if necessary */
267 rc = tftp_load(&fn_ip, buffer, len, DEFAULT_TFTP_RETRIES, ip_version);
269 if (ip_version == 4) {
270 dhcp_send_release(fn_ip.fd);
273 return rc;
276 void panic(const char *string)
278 sclp_print(string);
279 for (;;) {
280 disabled_wait();
284 static bool find_net_dev(Schib *schib, int dev_no)
286 int i, r;
288 for (i = 0; i < 0x10000; i++) {
289 net_schid.sch_no = i;
290 r = stsch_err(net_schid, schib);
291 if (r == 3 || r == -EIO) {
292 break;
294 if (!schib->pmcw.dnv) {
295 continue;
297 if (!virtio_is_supported(net_schid)) {
298 continue;
300 if (virtio_get_device_type() != VIRTIO_ID_NET) {
301 continue;
303 if (dev_no < 0 || schib->pmcw.dev == dev_no) {
304 return true;
308 return false;
311 static void virtio_setup(void)
313 Schib schib;
314 int ssid;
315 bool found = false;
316 uint16_t dev_no;
319 * We unconditionally enable mss support. In every sane configuration,
320 * this will succeed; and even if it doesn't, stsch_err() can deal
321 * with the consequences.
323 enable_mss_facility();
325 if (store_iplb(&iplb)) {
326 IPL_assert(iplb.pbt == S390_IPL_TYPE_CCW, "IPL_TYPE_CCW expected");
327 dev_no = iplb.ccw.devno;
328 debug_print_int("device no. ", dev_no);
329 net_schid.ssid = iplb.ccw.ssid & 0x3;
330 debug_print_int("ssid ", net_schid.ssid);
331 found = find_net_dev(&schib, dev_no);
332 } else {
333 for (ssid = 0; ssid < 0x3; ssid++) {
334 net_schid.ssid = ssid;
335 found = find_net_dev(&schib, -1);
336 if (found) {
337 break;
342 IPL_assert(found, "No virtio net device found");
345 void main(void)
347 int rc;
349 sclp_setup();
350 sclp_print("Network boot starting...\n");
352 virtio_setup();
354 rc = net_load(NULL, (long)_start);
355 if (rc > 0) {
356 sclp_print("Network loading done, starting kernel...\n");
357 asm volatile (" lpsw 0(%0) " : : "r"(0) : "memory");
360 panic("Failed to load OS from network\n");