1 /* net_init.c: Initialization for network devices. */
3 Written 1993,1994,1995 by Donald Becker.
5 The author may be reached as becker@scyld.com, or C/O
6 Scyld Computing Corporation
7 410 Severn Ave., Suite 210
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.
16 Modifications/additions by Bjorn Ekwall <bj0rn@blox.se>:
17 ethdev_index[MAX_ETH_CARDS]
18 register_netdev() / unregister_netdev()
20 Modifications by Wolfgang Walter
21 Use dev_close cleanly so we always shut things down tidily.
23 Changed 29/10/95, Alan Cox to pass sockaddr's around for mac addresses.
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.
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
36 #include <linux/config.h>
37 #include <linux/module.h>
38 #include <linux/kernel.h>
39 #include <linux/types.h>
41 #include <linux/slab.h>
42 #include <linux/if_ether.h>
43 #include <linux/string.h>
44 #include <linux/netdevice.h>
45 #include <linux/etherdevice.h>
46 #include <linux/fddidevice.h>
47 #include <linux/hippidevice.h>
48 #include <linux/trdevice.h>
49 #include <linux/fcdevice.h>
50 #include <linux/if_arp.h>
51 #include <linux/if_ltalk.h>
52 #include <linux/rtnetlink.h>
53 #include <net/neighbour.h>
55 /* The network devices currently exist only in the socket namespace, so these
56 entries are unused. The only ones that make sense are
57 open start the ethercard
58 close stop the ethercard
59 ioctl To get statistics, perhaps set the interface port (AUI, BNC, etc.)
60 One can also imagine getting raw packets using
62 but this is probably better handled by a raw packet socket.
64 Given that almost all of these functions are handled in the current
65 socket-based scheme, putting ethercard devices in /dev/ seems pointless.
67 [Removed all support for /dev network devices. When someone adds
68 streams then by magic we get them, but otherwise they are un-needed
73 struct net_device
*alloc_netdev(int sizeof_priv
, const char *mask
,
74 void (*setup
)(struct net_device
*))
76 struct net_device
*dev
;
79 /* ensure 32-byte alignment of the private area */
80 alloc_size
= sizeof (*dev
) + sizeof_priv
+ 31;
82 dev
= (struct net_device
*) kmalloc (alloc_size
, GFP_KERNEL
);
85 printk(KERN_ERR
"alloc_dev: Unable to allocate device memory.\n");
89 memset(dev
, 0, alloc_size
);
92 dev
->priv
= (void *) (((long)(dev
+ 1) + 31) & ~31);
95 strcpy(dev
->name
, mask
);
99 EXPORT_SYMBOL(alloc_netdev
);
101 static struct net_device
*init_alloc_dev(int sizeof_priv
)
103 struct net_device
*dev
;
106 /* ensure 32-byte alignment of the private area */
107 alloc_size
= sizeof (*dev
) + sizeof_priv
+ 31;
109 dev
= (struct net_device
*) kmalloc (alloc_size
, GFP_KERNEL
);
112 printk(KERN_ERR
"alloc_dev: Unable to allocate device memory.\n");
116 memset(dev
, 0, alloc_size
);
119 dev
->priv
= (void *) (((long)(dev
+ 1) + 31) & ~31);
125 * Create and name a device from a prototype, then perform any needed
129 static struct net_device
*init_netdev(struct net_device
*dev
, int sizeof_priv
,
130 char *mask
, void (*setup
)(struct net_device
*))
135 * Allocate a device if one is not provided.
139 dev
=init_alloc_dev(sizeof_priv
);
149 if (dev
->name
[0] == '\0' || dev
->name
[0] == ' ') {
150 strcpy(dev
->name
, mask
);
151 if (dev_alloc_name(dev
, mask
)<0) {
158 netdev_boot_setup_check(dev
);
161 * Configure via the caller provided setup function then
162 * register if needed.
171 err
= register_netdevice(dev
);
183 * init_etherdev - Register ethernet device
184 * @dev: An ethernet device structure to be filled in, or %NULL if a new
185 * struct should be allocated.
186 * @sizeof_priv: Size of additional driver-private structure to be allocated
187 * for this ethernet device
189 * Fill in the fields of the device structure with ethernet-generic values.
191 * If no device structure is passed, a new one is constructed, complete with
192 * a private data area of size @sizeof_priv. A 32-byte (not bit)
193 * alignment is enforced for this private data area.
195 * If an empty string area is passed as dev->name, or a new structure is made,
196 * a new name string is constructed.
199 struct net_device
*init_etherdev(struct net_device
*dev
, int sizeof_priv
)
201 return init_netdev(dev
, sizeof_priv
, "eth%d", ether_setup
);
205 * alloc_etherdev - Allocates and sets up an ethernet device
206 * @sizeof_priv: Size of additional driver-private structure to be allocated
207 * for this ethernet device
209 * Fill in the fields of the device structure with ethernet-generic
210 * values. Basically does everything except registering the device.
212 * Constructs a new net device, complete with a private data area of
213 * size @sizeof_priv. A 32-byte (not bit) alignment is enforced for
214 * this private data area.
217 struct net_device
*alloc_etherdev(int sizeof_priv
)
219 return alloc_netdev(sizeof_priv
, "eth%d", ether_setup
);
222 EXPORT_SYMBOL(init_etherdev
);
223 EXPORT_SYMBOL(alloc_etherdev
);
225 static int eth_mac_addr(struct net_device
*dev
, void *p
)
227 struct sockaddr
*addr
=p
;
228 if (netif_running(dev
))
230 memcpy(dev
->dev_addr
, addr
->sa_data
,dev
->addr_len
);
234 static int eth_change_mtu(struct net_device
*dev
, int new_mtu
)
236 if ((new_mtu
< 68) || (new_mtu
> 1500))
245 * alloc_fddidev - Register FDDI device
246 * @sizeof_priv: Size of additional driver-private structure to be allocated
247 * for this FDDI device
249 * Fill in the fields of the device structure with FDDI-generic values.
251 * Constructs a new net device, complete with a private data area of
252 * size @sizeof_priv. A 32-byte (not bit) alignment is enforced for
253 * this private data area.
256 struct net_device
*alloc_fddidev(int sizeof_priv
)
258 return alloc_netdev(sizeof_priv
, "fddi%d", fddi_setup
);
261 EXPORT_SYMBOL(alloc_fddidev
);
263 static int fddi_change_mtu(struct net_device
*dev
, int new_mtu
)
265 if ((new_mtu
< FDDI_K_SNAP_HLEN
) || (new_mtu
> FDDI_K_SNAP_DLEN
))
271 #endif /* CONFIG_FDDI */
275 static int hippi_change_mtu(struct net_device
*dev
, int new_mtu
)
278 * HIPPI's got these nice large MTUs.
280 if ((new_mtu
< 68) || (new_mtu
> 65280))
288 * For HIPPI we will actually use the lower 4 bytes of the hardware
289 * address as the I-FIELD rather than the actual hardware address.
291 static int hippi_mac_addr(struct net_device
*dev
, void *p
)
293 struct sockaddr
*addr
= p
;
294 if (netif_running(dev
))
296 memcpy(dev
->dev_addr
, addr
->sa_data
, dev
->addr_len
);
300 static int hippi_neigh_setup_dev(struct net_device
*dev
, struct neigh_parms
*p
)
302 /* Never send broadcast/multicast ARP messages */
305 /* In IPv6 unicast probes are valid even on NBMA,
306 * because they are encapsulated in normal IPv6 protocol.
307 * Should be a generic flag.
309 if (p
->tbl
->family
!= AF_INET6
)
314 static void hippi_setup(struct net_device
*dev
)
316 dev
->set_multicast_list
= NULL
;
317 dev
->change_mtu
= hippi_change_mtu
;
318 dev
->hard_header
= hippi_header
;
319 dev
->rebuild_header
= hippi_rebuild_header
;
320 dev
->set_mac_address
= hippi_mac_addr
;
321 dev
->hard_header_parse
= NULL
;
322 dev
->hard_header_cache
= NULL
;
323 dev
->header_cache_update
= NULL
;
324 dev
->neigh_setup
= hippi_neigh_setup_dev
;
327 * We don't support HIPPI `ARP' for the time being, and probably
328 * never will unless someone else implements it. However we
329 * still need a fake ARPHRD to make ifconfig and friends play ball.
331 dev
->type
= ARPHRD_HIPPI
;
332 dev
->hard_header_len
= HIPPI_HLEN
;
334 dev
->addr_len
= HIPPI_ALEN
;
335 dev
->tx_queue_len
= 25 /* 5 */;
336 memset(dev
->broadcast
, 0xFF, HIPPI_ALEN
);
340 * HIPPI doesn't support broadcast+multicast and we only use
341 * static ARP tables. ARP is disabled by hippi_neigh_setup_dev.
347 * alloc_hippi_dev - Register HIPPI device
348 * @sizeof_priv: Size of additional driver-private structure to be allocated
349 * for this HIPPI device
351 * Fill in the fields of the device structure with HIPPI-generic values.
353 * Constructs a new net device, complete with a private data area of
354 * size @sizeof_priv. A 32-byte (not bit) alignment is enforced for
355 * this private data area.
358 struct net_device
*alloc_hippi_dev(int sizeof_priv
)
360 return alloc_netdev(sizeof_priv
, "hip%d", hippi_setup
);
363 EXPORT_SYMBOL(alloc_hippi_dev
);
365 #endif /* CONFIG_HIPPI */
367 void ether_setup(struct net_device
*dev
)
369 /* Fill in the fields of the device structure with ethernet-generic values.
370 This should be in a common file instead of per-driver. */
372 dev
->change_mtu
= eth_change_mtu
;
373 dev
->hard_header
= eth_header
;
374 dev
->rebuild_header
= eth_rebuild_header
;
375 dev
->set_mac_address
= eth_mac_addr
;
376 dev
->hard_header_cache
= eth_header_cache
;
377 dev
->header_cache_update
= eth_header_cache_update
;
378 dev
->hard_header_parse
= eth_header_parse
;
380 dev
->type
= ARPHRD_ETHER
;
381 dev
->hard_header_len
= ETH_HLEN
;
382 dev
->mtu
= 1500; /* eth_mtu */
383 dev
->addr_len
= ETH_ALEN
;
384 dev
->tx_queue_len
= 100; /* Ethernet wants good queues */
386 memset(dev
->broadcast
,0xFF, ETH_ALEN
);
388 /* New-style flags. */
389 dev
->flags
= IFF_BROADCAST
|IFF_MULTICAST
;
391 EXPORT_SYMBOL(ether_setup
);
395 void fddi_setup(struct net_device
*dev
)
398 * Fill in the fields of the device structure with FDDI-generic values.
399 * This should be in a common file instead of per-driver.
402 dev
->change_mtu
= fddi_change_mtu
;
403 dev
->hard_header
= fddi_header
;
404 dev
->rebuild_header
= fddi_rebuild_header
;
406 dev
->type
= ARPHRD_FDDI
;
407 dev
->hard_header_len
= FDDI_K_SNAP_HLEN
+3; /* Assume 802.2 SNAP hdr len + 3 pad bytes */
408 dev
->mtu
= FDDI_K_SNAP_DLEN
; /* Assume max payload of 802.2 SNAP frame */
409 dev
->addr_len
= FDDI_K_ALEN
;
410 dev
->tx_queue_len
= 100; /* Long queues on FDDI */
412 memset(dev
->broadcast
, 0xFF, FDDI_K_ALEN
);
414 /* New-style flags */
415 dev
->flags
= IFF_BROADCAST
| IFF_MULTICAST
;
417 EXPORT_SYMBOL(fddi_setup
);
419 #endif /* CONFIG_FDDI */
421 #if defined(CONFIG_ATALK) || defined(CONFIG_ATALK_MODULE)
423 static int ltalk_change_mtu(struct net_device
*dev
, int mtu
)
428 static int ltalk_mac_addr(struct net_device
*dev
, void *addr
)
434 void ltalk_setup(struct net_device
*dev
)
436 /* Fill in the fields of the device structure with localtalk-generic values. */
438 dev
->change_mtu
= ltalk_change_mtu
;
439 dev
->hard_header
= NULL
;
440 dev
->rebuild_header
= NULL
;
441 dev
->set_mac_address
= ltalk_mac_addr
;
442 dev
->hard_header_cache
= NULL
;
443 dev
->header_cache_update
= NULL
;
445 dev
->type
= ARPHRD_LOCALTLK
;
446 dev
->hard_header_len
= LTALK_HLEN
;
447 dev
->mtu
= LTALK_MTU
;
448 dev
->addr_len
= LTALK_ALEN
;
449 dev
->tx_queue_len
= 10;
451 dev
->broadcast
[0] = 0xFF;
453 dev
->flags
= IFF_BROADCAST
|IFF_MULTICAST
|IFF_NOARP
;
455 EXPORT_SYMBOL(ltalk_setup
);
457 #endif /* CONFIG_ATALK || CONFIG_ATALK_MODULE */
459 int register_netdev(struct net_device
*dev
)
466 * If the name is a format string the caller wants us to
467 * do a name allocation
470 if (strchr(dev
->name
, '%'))
472 err
= dev_alloc_name(dev
, dev
->name
);
478 * Back compatibility hook. Kill this one in 2.5
481 if (dev
->name
[0]==0 || dev
->name
[0]==' ')
483 err
= dev_alloc_name(dev
, "eth%d");
488 err
= register_netdevice(dev
);
495 void unregister_netdev(struct net_device
*dev
)
498 unregister_netdevice(dev
);
502 EXPORT_SYMBOL(register_netdev
);
503 EXPORT_SYMBOL(unregister_netdev
);
507 void tr_setup(struct net_device
*dev
)
510 * Configure and register
513 dev
->hard_header
= tr_header
;
514 dev
->rebuild_header
= tr_rebuild_header
;
516 dev
->type
= ARPHRD_IEEE802_TR
;
517 dev
->hard_header_len
= TR_HLEN
;
519 dev
->addr_len
= TR_ALEN
;
520 dev
->tx_queue_len
= 100; /* Long queues on tr */
522 memset(dev
->broadcast
,0xFF, TR_ALEN
);
524 /* New-style flags. */
525 dev
->flags
= IFF_BROADCAST
| IFF_MULTICAST
;
529 * alloc_trdev - Register token ring device
530 * @sizeof_priv: Size of additional driver-private structure to be allocated
531 * for this token ring device
533 * Fill in the fields of the device structure with token ring-generic values.
535 * Constructs a new net device, complete with a private data area of
536 * size @sizeof_priv. A 32-byte (not bit) alignment is enforced for
537 * this private data area.
540 struct net_device
*alloc_trdev(int sizeof_priv
)
542 return alloc_netdev(sizeof_priv
, "tr%d", tr_setup
);
545 EXPORT_SYMBOL(tr_setup
);
546 EXPORT_SYMBOL(alloc_trdev
);
548 #endif /* CONFIG_TR */
552 void fc_setup(struct net_device
*dev
)
554 dev
->hard_header
= fc_header
;
555 dev
->rebuild_header
= fc_rebuild_header
;
557 dev
->type
= ARPHRD_IEEE802
;
558 dev
->hard_header_len
= FC_HLEN
;
560 dev
->addr_len
= FC_ALEN
;
561 dev
->tx_queue_len
= 100; /* Long queues on fc */
563 memset(dev
->broadcast
,0xFF, FC_ALEN
);
565 /* New-style flags. */
566 dev
->flags
= IFF_BROADCAST
;
570 * alloc_fcdev - Register fibre channel device
571 * @sizeof_priv: Size of additional driver-private structure to be allocated
572 * for this fibre channel device
574 * Fill in the fields of the device structure with fibre channel-generic values.
576 * Constructs a new net device, complete with a private data area of
577 * size @sizeof_priv. A 32-byte (not bit) alignment is enforced for
578 * this private data area.
581 struct net_device
*alloc_fcdev(int sizeof_priv
)
583 return alloc_netdev(sizeof_priv
, "fc%d", fc_setup
);
586 EXPORT_SYMBOL(fc_setup
);
587 EXPORT_SYMBOL(alloc_fcdev
);
589 #endif /* CONFIG_NET_FC */