12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds * INET An implementation of the TCP/IP protocol suite for the LINUX
41da177e4SLinus Torvalds * operating system. INET is implemented using the BSD Socket
51da177e4SLinus Torvalds * interface as the means of communication with the user level.
61da177e4SLinus Torvalds *
71da177e4SLinus Torvalds * Holds initial configuration information for devices.
81da177e4SLinus Torvalds *
91da177e4SLinus Torvalds * Version: @(#)Space.c 1.0.7 08/12/93
101da177e4SLinus Torvalds *
1102c30a84SJesper Juhl * Authors: Ross Biro
121da177e4SLinus Torvalds * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
131da177e4SLinus Torvalds * Donald J. Becker, <becker@scyld.com>
141da177e4SLinus Torvalds *
151da177e4SLinus Torvalds * Changelog:
161da177e4SLinus Torvalds * Stephen Hemminger (09/2003)
171da177e4SLinus Torvalds * - get rid of pre-linked dev list, dynamic device allocation
181da177e4SLinus Torvalds * Paul Gortmaker (03/2002)
191da177e4SLinus Torvalds * - struct init cleanup, enable multiple ISA autoprobes.
201da177e4SLinus Torvalds * Arnaldo Carvalho de Melo <acme@conectiva.com.br> - 09/1999
211da177e4SLinus Torvalds * - fix sbni: s/device/net_device/
221da177e4SLinus Torvalds * Paul Gortmaker (06/98):
231da177e4SLinus Torvalds * - sort probes in a sane way, make sure all (safe) probes
241da177e4SLinus Torvalds * get run once & failed autoprobes don't autoprobe again.
251da177e4SLinus Torvalds */
261da177e4SLinus Torvalds #include <linux/netdevice.h>
271da177e4SLinus Torvalds #include <linux/etherdevice.h>
281da177e4SLinus Torvalds #include <linux/errno.h>
291da177e4SLinus Torvalds #include <linux/init.h>
301da177e4SLinus Torvalds #include <linux/netlink.h>
31b6835f9cSRashika Kheria #include <net/Space.h>
321da177e4SLinus Torvalds
335ea2f5ffSArnd Bergmann /*
345ea2f5ffSArnd Bergmann * This structure holds boot-time configured netdevice settings. They
355ea2f5ffSArnd Bergmann * are then used in the device probing.
365ea2f5ffSArnd Bergmann */
375ea2f5ffSArnd Bergmann struct netdev_boot_setup {
385ea2f5ffSArnd Bergmann char name[IFNAMSIZ];
395ea2f5ffSArnd Bergmann struct ifmap map;
405ea2f5ffSArnd Bergmann };
415ea2f5ffSArnd Bergmann #define NETDEV_BOOT_SETUP_MAX 8
425ea2f5ffSArnd Bergmann
435ea2f5ffSArnd Bergmann
445ea2f5ffSArnd Bergmann /******************************************************************************
455ea2f5ffSArnd Bergmann *
465ea2f5ffSArnd Bergmann * Device Boot-time Settings Routines
475ea2f5ffSArnd Bergmann *
485ea2f5ffSArnd Bergmann ******************************************************************************/
495ea2f5ffSArnd Bergmann
505ea2f5ffSArnd Bergmann /* Boot time configuration table */
515ea2f5ffSArnd Bergmann static struct netdev_boot_setup dev_boot_setup[NETDEV_BOOT_SETUP_MAX];
525ea2f5ffSArnd Bergmann
535ea2f5ffSArnd Bergmann /**
545ea2f5ffSArnd Bergmann * netdev_boot_setup_add - add new setup entry
555ea2f5ffSArnd Bergmann * @name: name of the device
565ea2f5ffSArnd Bergmann * @map: configured settings for the device
575ea2f5ffSArnd Bergmann *
585ea2f5ffSArnd Bergmann * Adds new setup entry to the dev_boot_setup list. The function
595ea2f5ffSArnd Bergmann * returns 0 on error and 1 on success. This is a generic routine to
605ea2f5ffSArnd Bergmann * all netdevices.
615ea2f5ffSArnd Bergmann */
netdev_boot_setup_add(char * name,struct ifmap * map)625ea2f5ffSArnd Bergmann static int netdev_boot_setup_add(char *name, struct ifmap *map)
635ea2f5ffSArnd Bergmann {
645ea2f5ffSArnd Bergmann struct netdev_boot_setup *s;
655ea2f5ffSArnd Bergmann int i;
665ea2f5ffSArnd Bergmann
675ea2f5ffSArnd Bergmann s = dev_boot_setup;
685ea2f5ffSArnd Bergmann for (i = 0; i < NETDEV_BOOT_SETUP_MAX; i++) {
695ea2f5ffSArnd Bergmann if (s[i].name[0] == '\0' || s[i].name[0] == ' ') {
705ea2f5ffSArnd Bergmann memset(s[i].name, 0, sizeof(s[i].name));
71*fb3ceec1SWolfram Sang strscpy(s[i].name, name, IFNAMSIZ);
725ea2f5ffSArnd Bergmann memcpy(&s[i].map, map, sizeof(s[i].map));
735ea2f5ffSArnd Bergmann break;
745ea2f5ffSArnd Bergmann }
755ea2f5ffSArnd Bergmann }
765ea2f5ffSArnd Bergmann
775ea2f5ffSArnd Bergmann return i >= NETDEV_BOOT_SETUP_MAX ? 0 : 1;
785ea2f5ffSArnd Bergmann }
795ea2f5ffSArnd Bergmann
805ea2f5ffSArnd Bergmann /**
815ea2f5ffSArnd Bergmann * netdev_boot_setup_check - check boot time settings
825ea2f5ffSArnd Bergmann * @dev: the netdevice
835ea2f5ffSArnd Bergmann *
845ea2f5ffSArnd Bergmann * Check boot time settings for the device.
855ea2f5ffSArnd Bergmann * The found settings are set for the device to be used
865ea2f5ffSArnd Bergmann * later in the device probing.
875ea2f5ffSArnd Bergmann * Returns 0 if no settings found, 1 if they are.
885ea2f5ffSArnd Bergmann */
netdev_boot_setup_check(struct net_device * dev)895ea2f5ffSArnd Bergmann int netdev_boot_setup_check(struct net_device *dev)
905ea2f5ffSArnd Bergmann {
915ea2f5ffSArnd Bergmann struct netdev_boot_setup *s = dev_boot_setup;
925ea2f5ffSArnd Bergmann int i;
935ea2f5ffSArnd Bergmann
945ea2f5ffSArnd Bergmann for (i = 0; i < NETDEV_BOOT_SETUP_MAX; i++) {
955ea2f5ffSArnd Bergmann if (s[i].name[0] != '\0' && s[i].name[0] != ' ' &&
965ea2f5ffSArnd Bergmann !strcmp(dev->name, s[i].name)) {
975ea2f5ffSArnd Bergmann dev->irq = s[i].map.irq;
985ea2f5ffSArnd Bergmann dev->base_addr = s[i].map.base_addr;
995ea2f5ffSArnd Bergmann dev->mem_start = s[i].map.mem_start;
1005ea2f5ffSArnd Bergmann dev->mem_end = s[i].map.mem_end;
1015ea2f5ffSArnd Bergmann return 1;
1025ea2f5ffSArnd Bergmann }
1035ea2f5ffSArnd Bergmann }
1045ea2f5ffSArnd Bergmann return 0;
1055ea2f5ffSArnd Bergmann }
1065ea2f5ffSArnd Bergmann EXPORT_SYMBOL(netdev_boot_setup_check);
1075ea2f5ffSArnd Bergmann
1085ea2f5ffSArnd Bergmann /**
1095ea2f5ffSArnd Bergmann * netdev_boot_base - get address from boot time settings
1105ea2f5ffSArnd Bergmann * @prefix: prefix for network device
1115ea2f5ffSArnd Bergmann * @unit: id for network device
1125ea2f5ffSArnd Bergmann *
1135ea2f5ffSArnd Bergmann * Check boot time settings for the base address of device.
1145ea2f5ffSArnd Bergmann * The found settings are set for the device to be used
1155ea2f5ffSArnd Bergmann * later in the device probing.
1165ea2f5ffSArnd Bergmann * Returns 0 if no settings found.
1175ea2f5ffSArnd Bergmann */
netdev_boot_base(const char * prefix,int unit)1185ea2f5ffSArnd Bergmann static unsigned long netdev_boot_base(const char *prefix, int unit)
1195ea2f5ffSArnd Bergmann {
1205ea2f5ffSArnd Bergmann const struct netdev_boot_setup *s = dev_boot_setup;
1215ea2f5ffSArnd Bergmann char name[IFNAMSIZ];
1225ea2f5ffSArnd Bergmann int i;
1235ea2f5ffSArnd Bergmann
1245ea2f5ffSArnd Bergmann sprintf(name, "%s%d", prefix, unit);
1255ea2f5ffSArnd Bergmann
1265ea2f5ffSArnd Bergmann /*
1275ea2f5ffSArnd Bergmann * If device already registered then return base of 1
1285ea2f5ffSArnd Bergmann * to indicate not to probe for this interface
1295ea2f5ffSArnd Bergmann */
1305ea2f5ffSArnd Bergmann if (__dev_get_by_name(&init_net, name))
1315ea2f5ffSArnd Bergmann return 1;
1325ea2f5ffSArnd Bergmann
1335ea2f5ffSArnd Bergmann for (i = 0; i < NETDEV_BOOT_SETUP_MAX; i++)
1345ea2f5ffSArnd Bergmann if (!strcmp(name, s[i].name))
1355ea2f5ffSArnd Bergmann return s[i].map.base_addr;
1365ea2f5ffSArnd Bergmann return 0;
1375ea2f5ffSArnd Bergmann }
1385ea2f5ffSArnd Bergmann
1395ea2f5ffSArnd Bergmann /*
1405ea2f5ffSArnd Bergmann * Saves at boot time configured settings for any netdevice.
1415ea2f5ffSArnd Bergmann */
netdev_boot_setup(char * str)1425ea2f5ffSArnd Bergmann static int __init netdev_boot_setup(char *str)
1435ea2f5ffSArnd Bergmann {
1445ea2f5ffSArnd Bergmann int ints[5];
1455ea2f5ffSArnd Bergmann struct ifmap map;
1465ea2f5ffSArnd Bergmann
1475ea2f5ffSArnd Bergmann str = get_options(str, ARRAY_SIZE(ints), ints);
1485ea2f5ffSArnd Bergmann if (!str || !*str)
1495ea2f5ffSArnd Bergmann return 0;
1505ea2f5ffSArnd Bergmann
1515ea2f5ffSArnd Bergmann /* Save settings */
1525ea2f5ffSArnd Bergmann memset(&map, 0, sizeof(map));
1535ea2f5ffSArnd Bergmann if (ints[0] > 0)
1545ea2f5ffSArnd Bergmann map.irq = ints[1];
1555ea2f5ffSArnd Bergmann if (ints[0] > 1)
1565ea2f5ffSArnd Bergmann map.base_addr = ints[2];
1575ea2f5ffSArnd Bergmann if (ints[0] > 2)
1585ea2f5ffSArnd Bergmann map.mem_start = ints[3];
1595ea2f5ffSArnd Bergmann if (ints[0] > 3)
1605ea2f5ffSArnd Bergmann map.mem_end = ints[4];
1615ea2f5ffSArnd Bergmann
1625ea2f5ffSArnd Bergmann /* Add new entry to the list */
1635ea2f5ffSArnd Bergmann return netdev_boot_setup_add(str, &map);
1645ea2f5ffSArnd Bergmann }
1655ea2f5ffSArnd Bergmann
1665ea2f5ffSArnd Bergmann __setup("netdev=", netdev_boot_setup);
1675ea2f5ffSArnd Bergmann
ether_boot_setup(char * str)1685ea2f5ffSArnd Bergmann static int __init ether_boot_setup(char *str)
1695ea2f5ffSArnd Bergmann {
1705ea2f5ffSArnd Bergmann return netdev_boot_setup(str);
1715ea2f5ffSArnd Bergmann }
1725ea2f5ffSArnd Bergmann __setup("ether=", ether_boot_setup);
1735ea2f5ffSArnd Bergmann
1745ea2f5ffSArnd Bergmann
1751da177e4SLinus Torvalds /* A unified ethernet device probe. This is the easiest way to have every
1766104503cSAmit Ghadge * ethernet adaptor have the name "eth[0123...]".
1771da177e4SLinus Torvalds */
1781da177e4SLinus Torvalds
1791da177e4SLinus Torvalds struct devprobe2 {
1801da177e4SLinus Torvalds struct net_device *(*probe)(int unit);
1811da177e4SLinus Torvalds int status; /* non-zero if autoprobe has failed */
1821da177e4SLinus Torvalds };
1831da177e4SLinus Torvalds
probe_list2(int unit,struct devprobe2 * p,int autoprobe)1841da177e4SLinus Torvalds static int __init probe_list2(int unit, struct devprobe2 *p, int autoprobe)
1851da177e4SLinus Torvalds {
1861da177e4SLinus Torvalds struct net_device *dev;
1876104503cSAmit Ghadge
1881da177e4SLinus Torvalds for (; p->probe; p++) {
1891da177e4SLinus Torvalds if (autoprobe && p->status)
1901da177e4SLinus Torvalds continue;
1911da177e4SLinus Torvalds dev = p->probe(unit);
1921da177e4SLinus Torvalds if (!IS_ERR(dev))
1931da177e4SLinus Torvalds return 0;
1941da177e4SLinus Torvalds if (autoprobe)
1951da177e4SLinus Torvalds p->status = PTR_ERR(dev);
1961da177e4SLinus Torvalds }
1971da177e4SLinus Torvalds return -ENODEV;
1981da177e4SLinus Torvalds }
1991da177e4SLinus Torvalds
2006104503cSAmit Ghadge /* ISA probes that touch addresses < 0x400 (including those that also
201a5e371f6SPaul Gortmaker * look for EISA/PCI cards in addition to ISA cards).
2021da177e4SLinus Torvalds */
2031da177e4SLinus Torvalds static struct devprobe2 isa_probes[] __initdata = {
2041da177e4SLinus Torvalds #ifdef CONFIG_3C515
2051da177e4SLinus Torvalds {tc515_probe, 0},
2061da177e4SLinus Torvalds #endif
2071da177e4SLinus Torvalds #ifdef CONFIG_ULTRA
2081da177e4SLinus Torvalds {ultra_probe, 0},
2091da177e4SLinus Torvalds #endif
2101da177e4SLinus Torvalds #ifdef CONFIG_WD80x3
2111da177e4SLinus Torvalds {wd_probe, 0},
2121da177e4SLinus Torvalds #endif
213dd9589c7SGuenter Roeck #if defined(CONFIG_NE2000) /* ISA (use ne2k-pci for PCI cards) */
2141da177e4SLinus Torvalds {ne_probe, 0},
2151da177e4SLinus Torvalds #endif
2161da177e4SLinus Torvalds #ifdef CONFIG_LANCE /* ISA/VLB (use pcnet32 for PCI cards) */
2171da177e4SLinus Torvalds {lance_probe, 0},
2181da177e4SLinus Torvalds #endif
2191da177e4SLinus Torvalds #ifdef CONFIG_SMC9194
2201da177e4SLinus Torvalds {smc_init, 0},
2211da177e4SLinus Torvalds #endif
22247fd22f2SArnd Bergmann #ifdef CONFIG_CS89x0_ISA
2231da177e4SLinus Torvalds {cs89x0_probe, 0},
2241da177e4SLinus Torvalds #endif
2251da177e4SLinus Torvalds {NULL, 0},
2261da177e4SLinus Torvalds };
2271da177e4SLinus Torvalds
2286104503cSAmit Ghadge /* Unified ethernet device probe, segmented per architecture and
2291da177e4SLinus Torvalds * per bus interface. This drives the legacy devices only for now.
2301da177e4SLinus Torvalds */
2311da177e4SLinus Torvalds
ethif_probe2(int unit)2321da177e4SLinus Torvalds static void __init ethif_probe2(int unit)
2331da177e4SLinus Torvalds {
2341da177e4SLinus Torvalds unsigned long base_addr = netdev_boot_base("eth", unit);
2351da177e4SLinus Torvalds
2361da177e4SLinus Torvalds if (base_addr == 1)
2371da177e4SLinus Torvalds return;
2381da177e4SLinus Torvalds
239e179d78eSArnd Bergmann probe_list2(unit, isa_probes, base_addr == 0);
2401da177e4SLinus Torvalds }
2411da177e4SLinus Torvalds
2421da177e4SLinus Torvalds /* Statically configured drivers -- order matters here. */
net_olddevs_init(void)2431da177e4SLinus Torvalds static int __init net_olddevs_init(void)
2441da177e4SLinus Torvalds {
2451da177e4SLinus Torvalds int num;
2461da177e4SLinus Torvalds
2471da177e4SLinus Torvalds for (num = 0; num < 8; ++num)
2481da177e4SLinus Torvalds ethif_probe2(num);
2491da177e4SLinus Torvalds
2501da177e4SLinus Torvalds return 0;
2511da177e4SLinus Torvalds }
2521da177e4SLinus Torvalds
2531da177e4SLinus Torvalds device_initcall(net_olddevs_init);
254