xref: /netbsd-src/sys/arch/alpha/alpha/autoconf.c (revision 296121ed992a66fda2c79e843285616269b1d6d1)
1 /* $NetBSD: autoconf.c,v 1.59 2024/03/31 19:06:30 thorpej Exp $ */
2 
3 /*
4  * Copyright (c) 1992, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * This software was developed by the Computer Systems Engineering group
8  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
9  * contributed to Berkeley.
10  *
11  * All advertising materials mentioning features or use of this software
12  * must display the following acknowledgement:
13  *	This product includes software developed by the University of
14  *	California, Lawrence Berkeley Laboratory.
15  *
16  * Redistribution and use in source and binary forms, with or without
17  * modification, are permitted provided that the following conditions
18  * are met:
19  * 1. Redistributions of source code must retain the above copyright
20  *    notice, this list of conditions and the following disclaimer.
21  * 2. Redistributions in binary form must reproduce the above copyright
22  *    notice, this list of conditions and the following disclaimer in the
23  *    documentation and/or other materials provided with the distribution.
24  * 3. Neither the name of the University nor the names of its contributors
25  *    may be used to endorse or promote products derived from this software
26  *    without specific prior written permission.
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38  * SUCH DAMAGE.
39  *
40  *	@(#)autoconf.c	8.4 (Berkeley) 10/1/93
41  */
42 
43 #include <sys/cdefs.h>			/* RCS ID & Copyright macro defns */
44 
45 __KERNEL_RCSID(0, "$NetBSD: autoconf.c,v 1.59 2024/03/31 19:06:30 thorpej Exp $");
46 
47 #include "pci.h"
48 
49 #include <sys/param.h>
50 #include <sys/systm.h>
51 #include <sys/buf.h>
52 #include <sys/disklabel.h>
53 #include <sys/reboot.h>
54 #include <sys/device.h>
55 #include <sys/conf.h>
56 #include <dev/cons.h>
57 
58 #include <dev/pci/pcivar.h>
59 
60 #include <net/if.h>
61 #include <net/if_ether.h>
62 
63 #include <machine/autoconf.h>
64 #include <machine/alpha.h>
65 #include <machine/cpu.h>
66 #include <machine/prom.h>
67 #include <machine/cpuconf.h>
68 #include <machine/intr.h>
69 
70 struct bootdev_data	*bootdev_data;
71 
72 static void	parse_prom_bootdev(void);
73 
74 /*
75  * cpu_configure:
76  * called at boot time, configure all devices on system
77  */
78 void
cpu_configure(void)79 cpu_configure(void)
80 {
81 
82 	parse_prom_bootdev();
83 
84 	/*
85 	 * Disable interrupts during autoconfiguration.  splhigh() won't
86 	 * work, because it simply _raises_ the IPL, so if machine checks
87 	 * are disabled, they'll stay disabled.  Machine checks are needed
88 	 * during autoconfig.
89 	 */
90 	(void)alpha_pal_swpipl(ALPHA_PSL_IPL_HIGH);
91 	if (config_rootfound("mainbus", NULL) == NULL)
92 		panic("no mainbus found");
93 	(void)spl0();
94 
95 	/*
96 	 * Note that bootstrapping is finished, and set the HWRPB up
97 	 * to do restarts.
98 	 */
99 	hwrpb_restart_setup();
100 }
101 
102 static void
qemu_find_rootdev(void)103 qemu_find_rootdev(void)
104 {
105 	char buf[32] = { 0 };
106 
107 	/*
108 	 * Check for "rootdev=wd0".
109 	 */
110 	if (! prom_qemu_getenv("rootdev", buf, sizeof(buf))) {
111 		/*
112 		 * Check "root=/dev/wd0a", "root=/dev/hda1", etc.
113 		 */
114 		if (! prom_qemu_getenv("root", buf, sizeof(buf))) {
115 			printf("WARNING: no rootdev= or root= arguments "
116 			       "provided by Qemu\n");
117 			return;
118 		}
119 	}
120 
121 	const size_t devlen = strlen("/dev/");
122 	const char *cp = buf;
123 	char *ecp = &buf[sizeof(buf) - 1];
124 
125 	/* Find the start of the device xname. */
126 	if (strlen(cp) > devlen && strncmp(cp, "/dev/", devlen) == 0) {
127 		cp += devlen;
128 	}
129 
130 	/* Now strip any partition letter off the end. */
131 	while (ecp != cp) {
132 		if (*ecp >= '0' && *ecp <= '9') {
133 			break;
134 		}
135 		*ecp-- = '\0';
136 	}
137 
138 	snprintf(bootinfo.booted_dev, sizeof(bootinfo.booted_dev),
139 	    "root=%s", cp);
140 	booted_device = device_find_by_xname(cp);
141 }
142 
143 static bool
parse_dec_macaddr(const char * str,uint8_t enaddr[ETHER_ADDR_LEN])144 parse_dec_macaddr(const char *str, uint8_t enaddr[ETHER_ADDR_LEN])
145 {
146 	char *cp;
147 	long long l;
148 	int i;
149 
150 	/*
151 	 * DEC Ethernet address strings are formatted like so:
152 	 *
153 	 *	XX-XX-XX-XX-XX-XX
154 	 */
155 
156 	for (i = 0; i < ETHER_ADDR_LEN; i++) {
157 		l = strtoll(str, &cp, 16);
158 		if (l < 0 || l > 0xff) {
159 			/* Not a valid MAC address. */
160 			return false;
161 		}
162 		if (*cp == '-') {
163 			/* Octet separator. */
164 			enaddr[i] = (uint8_t)l;
165 			str = cp + 1;
166 			continue;
167 		}
168 		if (*cp == ' ' || *cp == '\0') {
169 			/* End of the string. */
170 			enaddr[i] = (uint8_t)l;
171 			return i == ETHER_ADDR_LEN - 1;
172 		}
173 		/* Bogus character. */
174 		break;
175 	}
176 
177 	/* Encountered bogus character or didn't reach end of string. */
178 	return false;
179 }
180 
181 static void
netboot_find_rootdev_planb(void)182 netboot_find_rootdev_planb(void)
183 {
184 	struct psref psref;
185 	uint8_t enaddr[ETHER_ADDR_LEN];
186 	char ifname[IFNAMSIZ];
187 	int i;
188 
189 	if (strncasecmp(bootinfo.booted_dev, "BOOTP ", 6) != 0 &&
190 	    strncasecmp(bootinfo.booted_dev, "MOP ", 4) != 0) {
191 		/* We weren't netbooted. */
192 		return;
193 	}
194 
195 	for (i = 2; bootinfo.booted_dev[i] != '\0'; i++) {
196 		if (bootinfo.booted_dev[i] == '-') {
197 			if (parse_dec_macaddr(&bootinfo.booted_dev[i - 2],
198 					      enaddr)) {
199 				/* Found it! */
200 				break;
201 			}
202 		}
203 	}
204 	if (bootinfo.booted_dev[i] == '\0') {
205 		/* No MAC address in string. */
206 		return;
207 	}
208 
209 	/* Now try to look up the interface by the link address. */
210 	struct ifnet *ifp = if_get_bylla(enaddr, ETHER_ADDR_LEN, &psref);
211 	if (ifp == NULL) {
212 		/* No interface attached with that MAC address. */
213 		return;
214 	}
215 
216 	strlcpy(ifname, if_name(ifp), sizeof(ifname));
217 	if_put(ifp, &psref);
218 
219 	/* Ok! Now look up the device_t by name! */
220 	booted_device = device_find_by_xname(ifname);
221 }
222 
223 void
cpu_rootconf(void)224 cpu_rootconf(void)
225 {
226 
227 	if (booted_device == NULL && alpha_is_qemu) {
228 		qemu_find_rootdev();
229 	}
230 
231 	if (booted_device == NULL) {
232 		/*
233 		 * It's possible that we netbooted from an Ethernet
234 		 * interface that can't be matched via the usual
235 		 * logic in device_register() (a DE204 in an ISA slot,
236 		 * for example).  In these cases, the console may have
237 		 * provided us with a MAC address that we can use to
238 		 * try and find the interface, * e.g.:
239 		 *
240 		 *	BOOTP 1 1 0 0 0 5 0 08-00-2B-xx-xx-xx 1
241 		 */
242 		netboot_find_rootdev_planb();
243 	}
244 
245 	if (booted_device == NULL) {
246 		printf("WARNING: can't figure what device matches \"%s\"\n",
247 		    bootinfo.booted_dev);
248 	}
249 	rootconf();
250 }
251 
252 static inline int
atoi(const char * s)253 atoi(const char *s)
254 {
255 	return (int)strtoll(s, NULL, 10);
256 }
257 
258 static void
parse_prom_bootdev(void)259 parse_prom_bootdev(void)
260 {
261 	static char hacked_boot_dev[128];
262 	static struct bootdev_data bd;
263 	char *cp, *scp, *boot_fields[8];
264 	int i, done;
265 
266 	booted_device = NULL;
267 	booted_partition = 0;
268 	bootdev_data = NULL;
269 
270 	memcpy(hacked_boot_dev, bootinfo.booted_dev,
271 	    uimin(sizeof bootinfo.booted_dev, sizeof hacked_boot_dev));
272 #if 0
273 	printf("parse_prom_bootdev: boot dev = \"%s\"\n", hacked_boot_dev);
274 #endif
275 
276 	i = 0;
277 	scp = cp = hacked_boot_dev;
278 	for (done = 0; !done; cp++) {
279 		if (*cp != ' ' && *cp != '\0')
280 			continue;
281 		if (*cp == '\0')
282 			done = 1;
283 
284 		*cp = '\0';
285 		boot_fields[i++] = scp;
286 		scp = cp + 1;
287 		if (i == 8)
288 			done = 1;
289 	}
290 	if (i != 8)
291 		return;		/* doesn't look like anything we know! */
292 
293 #if 0
294 	printf("i = %d, done = %d\n", i, done);
295 	for (i--; i >= 0; i--)
296 		printf("%d = %s\n", i, boot_fields[i]);
297 #endif
298 
299 	bd.protocol = boot_fields[0];
300 	bd.bus = atoi(boot_fields[1]);
301 	bd.slot = atoi(boot_fields[2]);
302 	bd.channel = atoi(boot_fields[3]);
303 	bd.remote_address = boot_fields[4];
304 	bd.unit = atoi(boot_fields[5]);
305 	bd.boot_dev_type = atoi(boot_fields[6]);
306 	bd.ctrl_dev_type = boot_fields[7];
307 
308 #if 0
309 	printf("parsed: proto = %s, bus = %d, slot = %d, channel = %d,\n",
310 	    bd.protocol, bd.bus, bd.slot, bd.channel);
311 	printf("\tremote = %s, unit = %d, dev_type = %d, ctrl_type = %s\n",
312 	    bd.remote_address, bd.unit, bd.boot_dev_type, bd.ctrl_dev_type);
313 #endif
314 
315 	bootdev_data = &bd;
316 }
317 
318 void
device_register(device_t dev,void * aux)319 device_register(device_t dev, void *aux)
320 {
321 #if NPCI > 0
322 	device_t parent = device_parent(dev);
323 
324 	if (parent != NULL && device_is_a(parent, "pci"))
325 		device_pci_register(dev, aux);
326 #endif
327 	if (platform.device_register)
328 		(*platform.device_register)(dev, aux);
329 }
330