xref: /openbmc/linux/drivers/net/Space.c (revision fb3ceec1)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * INET		An implementation of the TCP/IP protocol suite for the LINUX
4  *		operating system.  INET is implemented using the  BSD Socket
5  *		interface as the means of communication with the user level.
6  *
7  *		Holds initial configuration information for devices.
8  *
9  * Version:	@(#)Space.c	1.0.7	08/12/93
10  *
11  * Authors:	Ross Biro
12  *		Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
13  *		Donald J. Becker, <becker@scyld.com>
14  *
15  * Changelog:
16  *		Stephen Hemminger (09/2003)
17  *		- get rid of pre-linked dev list, dynamic device allocation
18  *		Paul Gortmaker (03/2002)
19  *		- struct init cleanup, enable multiple ISA autoprobes.
20  *		Arnaldo Carvalho de Melo <acme@conectiva.com.br> - 09/1999
21  *		- fix sbni: s/device/net_device/
22  *		Paul Gortmaker (06/98):
23  *		 - sort probes in a sane way, make sure all (safe) probes
24  *		   get run once & failed autoprobes don't autoprobe again.
25  */
26 #include <linux/netdevice.h>
27 #include <linux/etherdevice.h>
28 #include <linux/errno.h>
29 #include <linux/init.h>
30 #include <linux/netlink.h>
31 #include <net/Space.h>
32 
33 /*
34  * This structure holds boot-time configured netdevice settings. They
35  * are then used in the device probing.
36  */
37 struct netdev_boot_setup {
38 	char name[IFNAMSIZ];
39 	struct ifmap map;
40 };
41 #define NETDEV_BOOT_SETUP_MAX 8
42 
43 
44 /******************************************************************************
45  *
46  *		      Device Boot-time Settings Routines
47  *
48  ******************************************************************************/
49 
50 /* Boot time configuration table */
51 static struct netdev_boot_setup dev_boot_setup[NETDEV_BOOT_SETUP_MAX];
52 
53 /**
54  *	netdev_boot_setup_add	- add new setup entry
55  *	@name: name of the device
56  *	@map: configured settings for the device
57  *
58  *	Adds new setup entry to the dev_boot_setup list.  The function
59  *	returns 0 on error and 1 on success.  This is a generic routine to
60  *	all netdevices.
61  */
netdev_boot_setup_add(char * name,struct ifmap * map)62 static int netdev_boot_setup_add(char *name, struct ifmap *map)
63 {
64 	struct netdev_boot_setup *s;
65 	int i;
66 
67 	s = dev_boot_setup;
68 	for (i = 0; i < NETDEV_BOOT_SETUP_MAX; i++) {
69 		if (s[i].name[0] == '\0' || s[i].name[0] == ' ') {
70 			memset(s[i].name, 0, sizeof(s[i].name));
71 			strscpy(s[i].name, name, IFNAMSIZ);
72 			memcpy(&s[i].map, map, sizeof(s[i].map));
73 			break;
74 		}
75 	}
76 
77 	return i >= NETDEV_BOOT_SETUP_MAX ? 0 : 1;
78 }
79 
80 /**
81  * netdev_boot_setup_check	- check boot time settings
82  * @dev: the netdevice
83  *
84  * Check boot time settings for the device.
85  * The found settings are set for the device to be used
86  * later in the device probing.
87  * Returns 0 if no settings found, 1 if they are.
88  */
netdev_boot_setup_check(struct net_device * dev)89 int netdev_boot_setup_check(struct net_device *dev)
90 {
91 	struct netdev_boot_setup *s = dev_boot_setup;
92 	int i;
93 
94 	for (i = 0; i < NETDEV_BOOT_SETUP_MAX; i++) {
95 		if (s[i].name[0] != '\0' && s[i].name[0] != ' ' &&
96 		    !strcmp(dev->name, s[i].name)) {
97 			dev->irq = s[i].map.irq;
98 			dev->base_addr = s[i].map.base_addr;
99 			dev->mem_start = s[i].map.mem_start;
100 			dev->mem_end = s[i].map.mem_end;
101 			return 1;
102 		}
103 	}
104 	return 0;
105 }
106 EXPORT_SYMBOL(netdev_boot_setup_check);
107 
108 /**
109  * netdev_boot_base	- get address from boot time settings
110  * @prefix: prefix for network device
111  * @unit: id for network device
112  *
113  * Check boot time settings for the base address of device.
114  * The found settings are set for the device to be used
115  * later in the device probing.
116  * Returns 0 if no settings found.
117  */
netdev_boot_base(const char * prefix,int unit)118 static unsigned long netdev_boot_base(const char *prefix, int unit)
119 {
120 	const struct netdev_boot_setup *s = dev_boot_setup;
121 	char name[IFNAMSIZ];
122 	int i;
123 
124 	sprintf(name, "%s%d", prefix, unit);
125 
126 	/*
127 	 * If device already registered then return base of 1
128 	 * to indicate not to probe for this interface
129 	 */
130 	if (__dev_get_by_name(&init_net, name))
131 		return 1;
132 
133 	for (i = 0; i < NETDEV_BOOT_SETUP_MAX; i++)
134 		if (!strcmp(name, s[i].name))
135 			return s[i].map.base_addr;
136 	return 0;
137 }
138 
139 /*
140  * Saves at boot time configured settings for any netdevice.
141  */
netdev_boot_setup(char * str)142 static int __init netdev_boot_setup(char *str)
143 {
144 	int ints[5];
145 	struct ifmap map;
146 
147 	str = get_options(str, ARRAY_SIZE(ints), ints);
148 	if (!str || !*str)
149 		return 0;
150 
151 	/* Save settings */
152 	memset(&map, 0, sizeof(map));
153 	if (ints[0] > 0)
154 		map.irq = ints[1];
155 	if (ints[0] > 1)
156 		map.base_addr = ints[2];
157 	if (ints[0] > 2)
158 		map.mem_start = ints[3];
159 	if (ints[0] > 3)
160 		map.mem_end = ints[4];
161 
162 	/* Add new entry to the list */
163 	return netdev_boot_setup_add(str, &map);
164 }
165 
166 __setup("netdev=", netdev_boot_setup);
167 
ether_boot_setup(char * str)168 static int __init ether_boot_setup(char *str)
169 {
170 	return netdev_boot_setup(str);
171 }
172 __setup("ether=", ether_boot_setup);
173 
174 
175 /* A unified ethernet device probe.  This is the easiest way to have every
176  * ethernet adaptor have the name "eth[0123...]".
177  */
178 
179 struct devprobe2 {
180 	struct net_device *(*probe)(int unit);
181 	int status;	/* non-zero if autoprobe has failed */
182 };
183 
probe_list2(int unit,struct devprobe2 * p,int autoprobe)184 static int __init probe_list2(int unit, struct devprobe2 *p, int autoprobe)
185 {
186 	struct net_device *dev;
187 
188 	for (; p->probe; p++) {
189 		if (autoprobe && p->status)
190 			continue;
191 		dev = p->probe(unit);
192 		if (!IS_ERR(dev))
193 			return 0;
194 		if (autoprobe)
195 			p->status = PTR_ERR(dev);
196 	}
197 	return -ENODEV;
198 }
199 
200 /* ISA probes that touch addresses < 0x400 (including those that also
201  * look for EISA/PCI cards in addition to ISA cards).
202  */
203 static struct devprobe2 isa_probes[] __initdata = {
204 #ifdef CONFIG_3C515
205 	{tc515_probe, 0},
206 #endif
207 #ifdef CONFIG_ULTRA
208 	{ultra_probe, 0},
209 #endif
210 #ifdef CONFIG_WD80x3
211 	{wd_probe, 0},
212 #endif
213 #if defined(CONFIG_NE2000) /* ISA (use ne2k-pci for PCI cards) */
214 	{ne_probe, 0},
215 #endif
216 #ifdef CONFIG_LANCE		/* ISA/VLB (use pcnet32 for PCI cards) */
217 	{lance_probe, 0},
218 #endif
219 #ifdef CONFIG_SMC9194
220 	{smc_init, 0},
221 #endif
222 #ifdef CONFIG_CS89x0_ISA
223 	{cs89x0_probe, 0},
224 #endif
225 	{NULL, 0},
226 };
227 
228 /* Unified ethernet device probe, segmented per architecture and
229  * per bus interface. This drives the legacy devices only for now.
230  */
231 
ethif_probe2(int unit)232 static void __init ethif_probe2(int unit)
233 {
234 	unsigned long base_addr = netdev_boot_base("eth", unit);
235 
236 	if (base_addr == 1)
237 		return;
238 
239 	probe_list2(unit, isa_probes, base_addr == 0);
240 }
241 
242 /*  Statically configured drivers -- order matters here. */
net_olddevs_init(void)243 static int __init net_olddevs_init(void)
244 {
245 	int num;
246 
247 	for (num = 0; num < 8; ++num)
248 		ethif_probe2(num);
249 
250 #ifdef CONFIG_COPS
251 	cops_probe(0);
252 	cops_probe(1);
253 	cops_probe(2);
254 #endif
255 
256 	return 0;
257 }
258 
259 device_initcall(net_olddevs_init);
260