1/* net_init.c: Initialization for network devices. */
2/*
3	Written 1993,1994,1995 by Donald Becker.
4
5	The author may be reached as becker@scyld.com, or C/O
6	Scyld Computing Corporation
7	410 Severn Ave., Suite 210
8	Annapolis MD 21403
9
10	This file contains the initialization for the "pl14+" style ethernet
11	drivers.  It should eventually replace most of drivers/net/Space.c.
12	It's primary advantage is that it's able to allocate low-memory buffers.
13	A secondary advantage is that the dangerous NE*000 netcards can reserve
14	their I/O port region before the SCSI probes start.
15
16	Modifications/additions by Bjorn Ekwall <bj0rn@blox.se>:
17		ethdev_index[MAX_ETH_CARDS]
18		register_netdev() / unregister_netdev()
19
20	Modifications by Wolfgang Walter
21		Use dev_close cleanly so we always shut things down tidily.
22
23	Changed 29/10/95, Alan Cox to pass sockaddr's around for mac addresses.
24
25	14/06/96 - Paul Gortmaker:	Add generic eth_change_mtu() function.
26	24/09/96 - Paul Norton: Add token-ring variants of the netdev functions.
27
28	08/11/99 - Alan Cox: Got fed up of the mess in this file and cleaned it
29			up. We now share common code and have regularised name
30			allocation setups. Abolished the 16 card limits.
31	03/19/2000 - jgarzik and Urban Widmark: init_etherdev 32-byte align
32	03/21/2001 - jgarzik: alloc_etherdev and friends
33
34*/
35
36#include <linux/config.h>
37#include <linux/module.h>
38#include <linux/kernel.h>
39#include <linux/sched.h>
40#include <linux/types.h>
41#include <linux/fs.h>
42#include <linux/slab.h>
43#include <linux/if_ether.h>
44#include <linux/string.h>
45#include <linux/netdevice.h>
46#include <linux/etherdevice.h>
47#include <linux/fddidevice.h>
48#include <linux/hippidevice.h>
49#include <linux/trdevice.h>
50#include <linux/fcdevice.h>
51#include <linux/if_arp.h>
52#include <linux/if_ltalk.h>
53#include <linux/rtnetlink.h>
54#include <net/neighbour.h>
55
56/* The network devices currently exist only in the socket namespace, so these
57   entries are unused.  The only ones that make sense are
58    open	start the ethercard
59    close	stop  the ethercard
60    ioctl	To get statistics, perhaps set the interface port (AUI, BNC, etc.)
61   One can also imagine getting raw packets using
62    read & write
63   but this is probably better handled by a raw packet socket.
64
65   Given that almost all of these functions are handled in the current
66   socket-based scheme, putting ethercard devices in /dev/ seems pointless.
67
68   [Removed all support for /dev network devices. When someone adds
69    streams then by magic we get them, but otherwise they are un-needed
70	and a space waste]
71*/
72
73
74static struct net_device *alloc_netdev(int sizeof_priv, const char *mask,
75				       void (*setup)(struct net_device *))
76{
77	struct net_device *dev;
78	int alloc_size;
79
80	/* ensure 32-byte alignment of the private area */
81	alloc_size = sizeof (*dev) + sizeof_priv + 31;
82
83	dev = (struct net_device *) kmalloc (alloc_size, GFP_KERNEL);
84	if (dev == NULL)
85	{
86		printk(KERN_ERR "alloc_dev: Unable to allocate device memory.\n");
87		return NULL;
88	}
89
90	memset(dev, 0, alloc_size);
91
92	if (sizeof_priv)
93		dev->priv = (void *) (((long)(dev + 1) + 31) & ~31);
94
95	setup(dev);
96	strcpy(dev->name, mask);
97
98	return dev;
99}
100
101static struct net_device *init_alloc_dev(int sizeof_priv)
102{
103	struct net_device *dev;
104	int alloc_size;
105
106	/* ensure 32-byte alignment of the private area */
107	alloc_size = sizeof (*dev) + sizeof_priv + 31;
108
109	dev = (struct net_device *) kmalloc (alloc_size, GFP_KERNEL);
110	if (dev == NULL)
111	{
112		printk(KERN_ERR "alloc_dev: Unable to allocate device memory.\n");
113		return NULL;
114	}
115
116	memset(dev, 0, alloc_size);
117
118	if (sizeof_priv)
119		dev->priv = (void *) (((long)(dev + 1) + 31) & ~31);
120
121	return dev;
122}
123
124/*
125 *	Create and name a device from a prototype, then perform any needed
126 *	setup.
127 */
128
129static struct net_device *init_netdev(struct net_device *dev, int sizeof_priv,
130				      char *mask, void (*setup)(struct net_device *))
131{
132	int new_device = 0;
133
134	/*
135	 *	Allocate a device if one is not provided.
136	 */
137
138	if (dev == NULL) {
139		dev=init_alloc_dev(sizeof_priv);
140		if(dev==NULL)
141			return NULL;
142		new_device = 1;
143	}
144
145	/*
146	 *	Allocate a name
147	 */
148
149	if (dev->name[0] == '\0' || dev->name[0] == ' ') {
150		strcpy(dev->name, mask);
151		if (dev_alloc_name(dev, mask)<0) {
152			if (new_device)
153				kfree(dev);
154			return NULL;
155		}
156	}
157
158	netdev_boot_setup_check(dev);
159
160	/*
161	 *	Configure via the caller provided setup function then
162	 *	register if needed.
163	 */
164
165	setup(dev);
166
167	if (new_device) {
168		int err;
169
170		rtnl_lock();
171		err = register_netdevice(dev);
172		rtnl_unlock();
173
174		if (err < 0) {
175			kfree(dev);
176			dev = NULL;
177		}
178	}
179	return dev;
180}
181
182#if defined(CONFIG_HIPPI) || defined(CONFIG_TR) || defined(CONFIG_NET_FC)
183static int __register_netdev(struct net_device *dev)
184{
185	if (dev->init && dev->init(dev) != 0) {
186		unregister_netdev(dev);
187		return -EIO;
188	}
189	return 0;
190}
191#endif
192
193/**
194 * init_etherdev - Register ethernet device
195 * @dev: An ethernet device structure to be filled in, or %NULL if a new
196 *	struct should be allocated.
197 * @sizeof_priv: Size of additional driver-private structure to be allocated
198 *	for this ethernet device
199 *
200 * Fill in the fields of the device structure with ethernet-generic values.
201 *
202 * If no device structure is passed, a new one is constructed, complete with
203 * a private data area of size @sizeof_priv.  A 32-byte (not bit)
204 * alignment is enforced for this private data area.
205 *
206 * If an empty string area is passed as dev->name, or a new structure is made,
207 * a new name string is constructed.
208 */
209
210struct net_device *init_etherdev(struct net_device *dev, int sizeof_priv)
211{
212	return init_netdev(dev, sizeof_priv, "eth%d", ether_setup);
213}
214
215/**
216 * alloc_etherdev - Allocates and sets up an ethernet device
217 * @sizeof_priv: Size of additional driver-private structure to be allocated
218 *	for this ethernet device
219 *
220 * Fill in the fields of the device structure with ethernet-generic
221 * values. Basically does everything except registering the device.
222 *
223 * Constructs a new net device, complete with a private data area of
224 * size @sizeof_priv.  A 32-byte (not bit) alignment is enforced for
225 * this private data area.
226 */
227
228struct net_device *alloc_etherdev(int sizeof_priv)
229{
230	return alloc_netdev(sizeof_priv, "eth%d", ether_setup);
231}
232
233EXPORT_SYMBOL(init_etherdev);
234EXPORT_SYMBOL(alloc_etherdev);
235
236static int eth_mac_addr(struct net_device *dev, void *p)
237{
238	struct sockaddr *addr=p;
239	if (netif_running(dev))
240		return -EBUSY;
241	memcpy(dev->dev_addr, addr->sa_data,dev->addr_len);
242	return 0;
243}
244
245static int eth_change_mtu(struct net_device *dev, int new_mtu)
246{
247	if ((new_mtu < 68) || (new_mtu > 1500))
248		return -EINVAL;
249	dev->mtu = new_mtu;
250	return 0;
251}
252
253#ifdef CONFIG_FDDI
254
255/**
256 * init_fddidev - Register FDDI device
257 * @dev: A FDDI device structure to be filled in, or %NULL if a new
258 *	struct should be allocated.
259 * @sizeof_priv: Size of additional driver-private structure to be allocated
260 *	for this ethernet device
261 *
262 * Fill in the fields of the device structure with FDDI-generic values.
263 *
264 * If no device structure is passed, a new one is constructed, complete with
265 * a private data area of size @sizeof_priv.  A 32-byte (not bit)
266 * alignment is enforced for this private data area.
267 *
268 * If an empty string area is passed as dev->name, or a new structure is made,
269 * a new name string is constructed.
270 */
271
272struct net_device *init_fddidev(struct net_device *dev, int sizeof_priv)
273{
274	return init_netdev(dev, sizeof_priv, "fddi%d", fddi_setup);
275}
276
277/**
278 * alloc_fddidev - Register FDDI device
279 * @sizeof_priv: Size of additional driver-private structure to be allocated
280 *	for this FDDI device
281 *
282 * Fill in the fields of the device structure with FDDI-generic values.
283 *
284 * Constructs a new net device, complete with a private data area of
285 * size @sizeof_priv.  A 32-byte (not bit) alignment is enforced for
286 * this private data area.
287 */
288
289struct net_device *alloc_fddidev(int sizeof_priv)
290{
291	return alloc_netdev(sizeof_priv, "fddi%d", fddi_setup);
292}
293
294EXPORT_SYMBOL(init_fddidev);
295EXPORT_SYMBOL(alloc_fddidev);
296
297static int fddi_change_mtu(struct net_device *dev, int new_mtu)
298{
299	if ((new_mtu < FDDI_K_SNAP_HLEN) || (new_mtu > FDDI_K_SNAP_DLEN))
300		return(-EINVAL);
301	dev->mtu = new_mtu;
302	return(0);
303}
304
305#endif /* CONFIG_FDDI */
306
307#ifdef CONFIG_HIPPI
308
309static int hippi_change_mtu(struct net_device *dev, int new_mtu)
310{
311	/*
312	 * HIPPI's got these nice large MTUs.
313	 */
314	if ((new_mtu < 68) || (new_mtu > 65280))
315		return -EINVAL;
316	dev->mtu = new_mtu;
317	return(0);
318}
319
320
321/*
322 * For HIPPI we will actually use the lower 4 bytes of the hardware
323 * address as the I-FIELD rather than the actual hardware address.
324 */
325static int hippi_mac_addr(struct net_device *dev, void *p)
326{
327	struct sockaddr *addr = p;
328	if (netif_running(dev))
329		return -EBUSY;
330	memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
331	return 0;
332}
333
334
335/**
336 * init_hippi_dev - Register HIPPI device
337 * @dev: A HIPPI device structure to be filled in, or %NULL if a new
338 *	struct should be allocated.
339 * @sizeof_priv: Size of additional driver-private structure to be allocated
340 *	for this ethernet device
341 *
342 * Fill in the fields of the device structure with HIPPI-generic values.
343 *
344 * If no device structure is passed, a new one is constructed, complete with
345 * a private data area of size @sizeof_priv.  A 32-byte (not bit)
346 * alignment is enforced for this private data area.
347 *
348 * If an empty string area is passed as dev->name, or a new structure is made,
349 * a new name string is constructed.
350 */
351
352struct net_device *init_hippi_dev(struct net_device *dev, int sizeof_priv)
353{
354	return init_netdev(dev, sizeof_priv, "hip%d", hippi_setup);
355}
356
357/**
358 * alloc_hippi_dev - Register HIPPI device
359 * @sizeof_priv: Size of additional driver-private structure to be allocated
360 *	for this HIPPI device
361 *
362 * Fill in the fields of the device structure with HIPPI-generic values.
363 *
364 * Constructs a new net device, complete with a private data area of
365 * size @sizeof_priv.  A 32-byte (not bit) alignment is enforced for
366 * this private data area.
367 */
368
369struct net_device *alloc_hippi_dev(int sizeof_priv)
370{
371	return alloc_netdev(sizeof_priv, "hip%d", hippi_setup);
372}
373
374int register_hipdev(struct net_device *dev)
375{
376	return __register_netdev(dev);
377}
378
379void unregister_hipdev(struct net_device *dev)
380{
381	unregister_netdev(dev);
382}
383
384EXPORT_SYMBOL(init_hippi_dev);
385EXPORT_SYMBOL(alloc_hippi_dev);
386EXPORT_SYMBOL(register_hipdev);
387EXPORT_SYMBOL(unregister_hipdev);
388
389static int hippi_neigh_setup_dev(struct net_device *dev, struct neigh_parms *p)
390{
391	/* Never send broadcast/multicast ARP messages */
392	p->mcast_probes = 0;
393
394	/* In IPv6 unicast probes are valid even on NBMA,
395	* because they are encapsulated in normal IPv6 protocol.
396	* Should be a generic flag.
397	*/
398	if (p->tbl->family != AF_INET6)
399		p->ucast_probes = 0;
400	return 0;
401}
402
403#endif /* CONFIG_HIPPI */
404
405void ether_setup(struct net_device *dev)
406{
407	/* Fill in the fields of the device structure with ethernet-generic values.
408	   This should be in a common file instead of per-driver.  */
409
410	dev->change_mtu		= eth_change_mtu;
411	dev->hard_header	= eth_header;
412	dev->rebuild_header 	= eth_rebuild_header;
413	dev->set_mac_address 	= eth_mac_addr;
414	dev->hard_header_cache	= eth_header_cache;
415	dev->header_cache_update= eth_header_cache_update;
416	dev->hard_header_parse	= eth_header_parse;
417
418	dev->type		= ARPHRD_ETHER;
419	dev->hard_header_len 	= ETH_HLEN;
420	dev->mtu		= 1500; /* eth_mtu */
421	dev->addr_len		= ETH_ALEN;
422	dev->tx_queue_len	= 100;	/* Ethernet wants good queues */
423
424	memset(dev->broadcast,0xFF, ETH_ALEN);
425
426	/* New-style flags. */
427	dev->flags		= IFF_BROADCAST|IFF_MULTICAST;
428}
429EXPORT_SYMBOL(ether_setup);
430
431#ifdef CONFIG_FDDI
432
433void fddi_setup(struct net_device *dev)
434{
435	/*
436	 * Fill in the fields of the device structure with FDDI-generic values.
437	 * This should be in a common file instead of per-driver.
438	 */
439
440	dev->change_mtu			= fddi_change_mtu;
441	dev->hard_header		= fddi_header;
442	dev->rebuild_header		= fddi_rebuild_header;
443
444	dev->type				= ARPHRD_FDDI;
445	dev->hard_header_len	= FDDI_K_SNAP_HLEN+3;	/* Assume 802.2 SNAP hdr len + 3 pad bytes */
446	dev->mtu				= FDDI_K_SNAP_DLEN;		/* Assume max payload of 802.2 SNAP frame */
447	dev->addr_len			= FDDI_K_ALEN;
448	dev->tx_queue_len		= 100;	/* Long queues on FDDI */
449
450	memset(dev->broadcast, 0xFF, FDDI_K_ALEN);
451
452	/* New-style flags */
453	dev->flags		= IFF_BROADCAST | IFF_MULTICAST;
454}
455EXPORT_SYMBOL(fddi_setup);
456
457#endif /* CONFIG_FDDI */
458
459#ifdef CONFIG_HIPPI
460void hippi_setup(struct net_device *dev)
461{
462	dev->set_multicast_list	= NULL;
463	dev->change_mtu			= hippi_change_mtu;
464	dev->hard_header		= hippi_header;
465	dev->rebuild_header 		= hippi_rebuild_header;
466	dev->set_mac_address 		= hippi_mac_addr;
467	dev->hard_header_parse		= NULL;
468	dev->hard_header_cache		= NULL;
469	dev->header_cache_update	= NULL;
470	dev->neigh_setup 		= hippi_neigh_setup_dev;
471
472	/*
473	 * We don't support HIPPI `ARP' for the time being, and probably
474	 * never will unless someone else implements it. However we
475	 * still need a fake ARPHRD to make ifconfig and friends play ball.
476	 */
477	dev->type		= ARPHRD_HIPPI;
478	dev->hard_header_len 	= HIPPI_HLEN;
479	dev->mtu		= 65280;
480	dev->addr_len		= HIPPI_ALEN;
481	dev->tx_queue_len	= 25 /* 5 */;
482	memset(dev->broadcast, 0xFF, HIPPI_ALEN);
483
484
485	/*
486	 * HIPPI doesn't support broadcast+multicast and we only use
487	 * static ARP tables. ARP is disabled by hippi_neigh_setup_dev.
488	 */
489	dev->flags = 0;
490}
491EXPORT_SYMBOL(hippi_setup);
492#endif /* CONFIG_HIPPI */
493
494#if defined(CONFIG_ATALK) || defined(CONFIG_ATALK_MODULE)
495
496static int ltalk_change_mtu(struct net_device *dev, int mtu)
497{
498	return -EINVAL;
499}
500
501static int ltalk_mac_addr(struct net_device *dev, void *addr)
502{
503	return -EINVAL;
504}
505
506
507void ltalk_setup(struct net_device *dev)
508{
509	/* Fill in the fields of the device structure with localtalk-generic values. */
510
511	dev->change_mtu		= ltalk_change_mtu;
512	dev->hard_header	= NULL;
513	dev->rebuild_header 	= NULL;
514	dev->set_mac_address 	= ltalk_mac_addr;
515	dev->hard_header_cache	= NULL;
516	dev->header_cache_update= NULL;
517
518	dev->type		= ARPHRD_LOCALTLK;
519	dev->hard_header_len 	= LTALK_HLEN;
520	dev->mtu		= LTALK_MTU;
521	dev->addr_len		= LTALK_ALEN;
522	dev->tx_queue_len	= 10;
523
524	dev->broadcast[0]	= 0xFF;
525
526	dev->flags		= IFF_BROADCAST|IFF_MULTICAST|IFF_NOARP;
527}
528EXPORT_SYMBOL(ltalk_setup);
529
530#endif /* CONFIG_ATALK || CONFIG_ATALK_MODULE */
531
532int register_netdev(struct net_device *dev)
533{
534	int err;
535
536	rtnl_lock();
537
538	/*
539	 *	If the name is a format string the caller wants us to
540	 *	do a name allocation
541	 */
542
543	if (strchr(dev->name, '%'))
544	{
545		err = dev_alloc_name(dev, dev->name);
546		if (err < 0)
547			goto out;
548	}
549
550	/*
551	 *	Back compatibility hook. Kill this one in 2.5
552	 */
553
554	if (dev->name[0]==0 || dev->name[0]==' ')
555	{
556		err = dev_alloc_name(dev, "eth%d");
557		if (err < 0)
558			goto out;
559	}
560
561	err = register_netdevice(dev);
562
563out:
564	rtnl_unlock();
565	return err;
566}
567
568void unregister_netdev(struct net_device *dev)
569{
570	rtnl_lock();
571	unregister_netdevice(dev);
572	rtnl_unlock();
573}
574
575EXPORT_SYMBOL(register_netdev);
576EXPORT_SYMBOL(unregister_netdev);
577
578#ifdef CONFIG_TR
579
580void tr_setup(struct net_device *dev)
581{
582	/*
583	 *	Configure and register
584	 */
585
586	dev->hard_header	= tr_header;
587	dev->rebuild_header	= tr_rebuild_header;
588
589	dev->type		= ARPHRD_IEEE802_TR;
590	dev->hard_header_len	= TR_HLEN;
591	dev->mtu		= 2000;
592	dev->addr_len		= TR_ALEN;
593	dev->tx_queue_len	= 100;	/* Long queues on tr */
594
595	memset(dev->broadcast,0xFF, TR_ALEN);
596
597	/* New-style flags. */
598	dev->flags		= IFF_BROADCAST | IFF_MULTICAST ;
599}
600
601/**
602 * init_trdev - Register token ring device
603 * @dev: A token ring device structure to be filled in, or %NULL if a new
604 *	struct should be allocated.
605 * @sizeof_priv: Size of additional driver-private structure to be allocated
606 *	for this ethernet device
607 *
608 * Fill in the fields of the device structure with token ring-generic values.
609 *
610 * If no device structure is passed, a new one is constructed, complete with
611 * a private data area of size @sizeof_priv.  A 32-byte (not bit)
612 * alignment is enforced for this private data area.
613 *
614 * If an empty string area is passed as dev->name, or a new structure is made,
615 * a new name string is constructed.
616 */
617
618struct net_device *init_trdev(struct net_device *dev, int sizeof_priv)
619{
620	return init_netdev(dev, sizeof_priv, "tr%d", tr_setup);
621}
622
623/**
624 * alloc_trdev - Register token ring device
625 * @sizeof_priv: Size of additional driver-private structure to be allocated
626 *	for this token ring device
627 *
628 * Fill in the fields of the device structure with token ring-generic values.
629 *
630 * Constructs a new net device, complete with a private data area of
631 * size @sizeof_priv.  A 32-byte (not bit) alignment is enforced for
632 * this private data area.
633 */
634
635struct net_device *alloc_trdev(int sizeof_priv)
636{
637	return alloc_netdev(sizeof_priv, "tr%d", tr_setup);
638}
639
640int register_trdev(struct net_device *dev)
641{
642	return __register_netdev(dev);
643}
644
645void unregister_trdev(struct net_device *dev)
646{
647	unregister_netdev(dev);
648}
649
650EXPORT_SYMBOL(tr_setup);
651EXPORT_SYMBOL(init_trdev);
652EXPORT_SYMBOL(alloc_trdev);
653EXPORT_SYMBOL(register_trdev);
654EXPORT_SYMBOL(unregister_trdev);
655
656#endif /* CONFIG_TR */
657
658
659#ifdef CONFIG_NET_FC
660
661void fc_setup(struct net_device *dev)
662{
663	dev->hard_header        =        fc_header;
664        dev->rebuild_header  	=        fc_rebuild_header;
665
666        dev->type               =        ARPHRD_IEEE802;
667	dev->hard_header_len    =        FC_HLEN;
668        dev->mtu                =        2024;
669        dev->addr_len           =        FC_ALEN;
670        dev->tx_queue_len       =        100; /* Long queues on fc */
671
672        memset(dev->broadcast,0xFF, FC_ALEN);
673
674        /* New-style flags. */
675        dev->flags              =        IFF_BROADCAST;
676}
677
678/**
679 * init_fcdev - Register fibre channel device
680 * @dev: A fibre channel device structure to be filled in, or %NULL if a new
681 *	struct should be allocated.
682 * @sizeof_priv: Size of additional driver-private structure to be allocated
683 *	for this ethernet device
684 *
685 * Fill in the fields of the device structure with fibre channel-generic values.
686 *
687 * If no device structure is passed, a new one is constructed, complete with
688 * a private data area of size @sizeof_priv.  A 32-byte (not bit)
689 * alignment is enforced for this private data area.
690 *
691 * If an empty string area is passed as dev->name, or a new structure is made,
692 * a new name string is constructed.
693 */
694
695struct net_device *init_fcdev(struct net_device *dev, int sizeof_priv)
696{
697	return init_netdev(dev, sizeof_priv, "fc%d", fc_setup);
698}
699
700/**
701 * alloc_fcdev - Register fibre channel device
702 * @sizeof_priv: Size of additional driver-private structure to be allocated
703 *	for this fibre channel device
704 *
705 * Fill in the fields of the device structure with fibre channel-generic values.
706 *
707 * Constructs a new net device, complete with a private data area of
708 * size @sizeof_priv.  A 32-byte (not bit) alignment is enforced for
709 * this private data area.
710 */
711
712struct net_device *alloc_fcdev(int sizeof_priv)
713{
714	return alloc_netdev(sizeof_priv, "fc%d", fc_setup);
715}
716
717int register_fcdev(struct net_device *dev)
718{
719	return __register_netdev(dev);
720}
721
722void unregister_fcdev(struct net_device *dev)
723{
724	unregister_netdev(dev);
725}
726
727EXPORT_SYMBOL(fc_setup);
728EXPORT_SYMBOL(init_fcdev);
729EXPORT_SYMBOL(alloc_fcdev);
730EXPORT_SYMBOL(register_fcdev);
731EXPORT_SYMBOL(unregister_fcdev);
732
733#endif /* CONFIG_NET_FC */
734
735