ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / drivers / net / net_init.c
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/types.h>
40 #include <linux/fs.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>
54
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
61     read & write
62    but this is probably better handled by a raw packet socket.
63
64    Given that almost all of these functions are handled in the current
65    socket-based scheme, putting ethercard devices in /dev/ seems pointless.
66    
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
69         and a space waste]
70 */
71
72
73 struct net_device *alloc_netdev(int sizeof_priv, const char *mask,
74                                        void (*setup)(struct net_device *))
75 {
76         void *p;
77         struct net_device *dev;
78         int alloc_size;
79
80         /* ensure 32-byte alignment of both the device and private area */
81
82         alloc_size = (sizeof(struct net_device) + NETDEV_ALIGN_CONST)
83                         & ~NETDEV_ALIGN_CONST;
84         alloc_size += sizeof_priv + NETDEV_ALIGN_CONST;
85
86         p = kmalloc (alloc_size, GFP_KERNEL);
87         if (!p) {
88                 printk(KERN_ERR "alloc_dev: Unable to allocate device.\n");
89                 return NULL;
90         }
91
92         memset(p, 0, alloc_size);
93
94         dev = (struct net_device *)(((long)p + NETDEV_ALIGN_CONST)
95                                 & ~NETDEV_ALIGN_CONST);
96         dev->padded = (char *)dev - (char *)p;
97
98         if (sizeof_priv)
99                 dev->priv = netdev_priv(dev);
100
101         setup(dev);
102         strcpy(dev->name, mask);
103
104         return dev;
105 }
106 EXPORT_SYMBOL(alloc_netdev);
107
108 /**
109  * alloc_etherdev - Allocates and sets up an ethernet device
110  * @sizeof_priv: Size of additional driver-private structure to be allocated
111  *      for this ethernet device
112  *
113  * Fill in the fields of the device structure with ethernet-generic
114  * values. Basically does everything except registering the device.
115  *
116  * Constructs a new net device, complete with a private data area of
117  * size @sizeof_priv.  A 32-byte (not bit) alignment is enforced for
118  * this private data area.
119  */
120
121 struct net_device *alloc_etherdev(int sizeof_priv)
122 {
123         return alloc_netdev(sizeof_priv, "eth%d", ether_setup);
124 }
125
126 EXPORT_SYMBOL(alloc_etherdev);
127
128 static int eth_mac_addr(struct net_device *dev, void *p)
129 {
130         struct sockaddr *addr=p;
131         if (netif_running(dev))
132                 return -EBUSY;
133         memcpy(dev->dev_addr, addr->sa_data,dev->addr_len);
134         return 0;
135 }
136
137 static int eth_change_mtu(struct net_device *dev, int new_mtu)
138 {
139         if ((new_mtu < 68) || (new_mtu > 1500))
140                 return -EINVAL;
141         dev->mtu = new_mtu;
142         return 0;
143 }
144
145 #ifdef CONFIG_FDDI
146
147 /**
148  * alloc_fddidev - Register FDDI device
149  * @sizeof_priv: Size of additional driver-private structure to be allocated
150  *      for this FDDI device
151  *
152  * Fill in the fields of the device structure with FDDI-generic values.
153  *
154  * Constructs a new net device, complete with a private data area of
155  * size @sizeof_priv.  A 32-byte (not bit) alignment is enforced for
156  * this private data area.
157  */
158
159 struct net_device *alloc_fddidev(int sizeof_priv)
160 {
161         return alloc_netdev(sizeof_priv, "fddi%d", fddi_setup);
162 }
163
164 EXPORT_SYMBOL(alloc_fddidev);
165
166 static int fddi_change_mtu(struct net_device *dev, int new_mtu)
167 {
168         if ((new_mtu < FDDI_K_SNAP_HLEN) || (new_mtu > FDDI_K_SNAP_DLEN))
169                 return(-EINVAL);
170         dev->mtu = new_mtu;
171         return(0);
172 }
173
174 #endif /* CONFIG_FDDI */
175
176 #ifdef CONFIG_HIPPI
177
178 static int hippi_change_mtu(struct net_device *dev, int new_mtu)
179 {
180         /*
181          * HIPPI's got these nice large MTUs.
182          */
183         if ((new_mtu < 68) || (new_mtu > 65280))
184                 return -EINVAL;
185         dev->mtu = new_mtu;
186         return(0);
187 }
188
189
190 /*
191  * For HIPPI we will actually use the lower 4 bytes of the hardware
192  * address as the I-FIELD rather than the actual hardware address.
193  */
194 static int hippi_mac_addr(struct net_device *dev, void *p)
195 {
196         struct sockaddr *addr = p;
197         if (netif_running(dev))
198                 return -EBUSY;
199         memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
200         return 0;
201 }
202
203 static int hippi_neigh_setup_dev(struct net_device *dev, struct neigh_parms *p)
204 {
205         /* Never send broadcast/multicast ARP messages */
206         p->mcast_probes = 0;
207  
208         /* In IPv6 unicast probes are valid even on NBMA,
209         * because they are encapsulated in normal IPv6 protocol.
210         * Should be a generic flag. 
211         */
212         if (p->tbl->family != AF_INET6)
213                 p->ucast_probes = 0;
214         return 0;
215 }
216
217 static void hippi_setup(struct net_device *dev)
218 {
219         dev->set_multicast_list = NULL;
220         dev->change_mtu                 = hippi_change_mtu;
221         dev->hard_header                = hippi_header;
222         dev->rebuild_header             = hippi_rebuild_header;
223         dev->set_mac_address            = hippi_mac_addr;
224         dev->hard_header_parse          = NULL;
225         dev->hard_header_cache          = NULL;
226         dev->header_cache_update        = NULL;
227         dev->neigh_setup                = hippi_neigh_setup_dev; 
228
229         /*
230          * We don't support HIPPI `ARP' for the time being, and probably
231          * never will unless someone else implements it. However we
232          * still need a fake ARPHRD to make ifconfig and friends play ball.
233          */
234         dev->type               = ARPHRD_HIPPI;
235         dev->hard_header_len    = HIPPI_HLEN;
236         dev->mtu                = 65280;
237         dev->addr_len           = HIPPI_ALEN;
238         dev->tx_queue_len       = 25 /* 5 */;
239         memset(dev->broadcast, 0xFF, HIPPI_ALEN);
240
241
242         /*
243          * HIPPI doesn't support broadcast+multicast and we only use
244          * static ARP tables. ARP is disabled by hippi_neigh_setup_dev. 
245          */
246         dev->flags = 0; 
247 }
248
249 /**
250  * alloc_hippi_dev - Register HIPPI device
251  * @sizeof_priv: Size of additional driver-private structure to be allocated
252  *      for this HIPPI device
253  *
254  * Fill in the fields of the device structure with HIPPI-generic values.
255  *
256  * Constructs a new net device, complete with a private data area of
257  * size @sizeof_priv.  A 32-byte (not bit) alignment is enforced for
258  * this private data area.
259  */
260
261 struct net_device *alloc_hippi_dev(int sizeof_priv)
262 {
263         return alloc_netdev(sizeof_priv, "hip%d", hippi_setup);
264 }
265
266 EXPORT_SYMBOL(alloc_hippi_dev);
267
268 #endif /* CONFIG_HIPPI */
269
270 void ether_setup(struct net_device *dev)
271 {
272         /* Fill in the fields of the device structure with ethernet-generic values.
273            This should be in a common file instead of per-driver.  */
274         
275         dev->change_mtu         = eth_change_mtu;
276         dev->hard_header        = eth_header;
277         dev->rebuild_header     = eth_rebuild_header;
278         dev->set_mac_address    = eth_mac_addr;
279         dev->hard_header_cache  = eth_header_cache;
280         dev->header_cache_update= eth_header_cache_update;
281         dev->hard_header_parse  = eth_header_parse;
282
283         dev->type               = ARPHRD_ETHER;
284         dev->hard_header_len    = ETH_HLEN;
285         dev->mtu                = 1500; /* eth_mtu */
286         dev->addr_len           = ETH_ALEN;
287         dev->tx_queue_len       = 1000; /* Ethernet wants good queues */        
288         
289         memset(dev->broadcast,0xFF, ETH_ALEN);
290
291         /* New-style flags. */
292         dev->flags              = IFF_BROADCAST|IFF_MULTICAST;
293 }
294 EXPORT_SYMBOL(ether_setup);
295
296 #ifdef CONFIG_FDDI
297
298 void fddi_setup(struct net_device *dev)
299 {
300         /*
301          * Fill in the fields of the device structure with FDDI-generic values.
302          * This should be in a common file instead of per-driver.
303          */
304         
305         dev->change_mtu                 = fddi_change_mtu;
306         dev->hard_header                = fddi_header;
307         dev->rebuild_header             = fddi_rebuild_header;
308
309         dev->type                               = ARPHRD_FDDI;
310         dev->hard_header_len    = FDDI_K_SNAP_HLEN+3;   /* Assume 802.2 SNAP hdr len + 3 pad bytes */
311         dev->mtu                                = FDDI_K_SNAP_DLEN;             /* Assume max payload of 802.2 SNAP frame */
312         dev->addr_len                   = FDDI_K_ALEN;
313         dev->tx_queue_len               = 100;  /* Long queues on FDDI */
314         
315         memset(dev->broadcast, 0xFF, FDDI_K_ALEN);
316
317         /* New-style flags */
318         dev->flags              = IFF_BROADCAST | IFF_MULTICAST;
319 }
320 EXPORT_SYMBOL(fddi_setup);
321
322 #endif /* CONFIG_FDDI */
323
324 #if defined(CONFIG_ATALK) || defined(CONFIG_ATALK_MODULE)
325
326 static int ltalk_change_mtu(struct net_device *dev, int mtu)
327 {
328         return -EINVAL;
329 }
330
331 static int ltalk_mac_addr(struct net_device *dev, void *addr)
332 {       
333         return -EINVAL;
334 }
335
336
337 void ltalk_setup(struct net_device *dev)
338 {
339         /* Fill in the fields of the device structure with localtalk-generic values. */
340         
341         dev->change_mtu         = ltalk_change_mtu;
342         dev->hard_header        = NULL;
343         dev->rebuild_header     = NULL;
344         dev->set_mac_address    = ltalk_mac_addr;
345         dev->hard_header_cache  = NULL;
346         dev->header_cache_update= NULL;
347
348         dev->type               = ARPHRD_LOCALTLK;
349         dev->hard_header_len    = LTALK_HLEN;
350         dev->mtu                = LTALK_MTU;
351         dev->addr_len           = LTALK_ALEN;
352         dev->tx_queue_len       = 10;   
353         
354         dev->broadcast[0]       = 0xFF;
355
356         dev->flags              = IFF_BROADCAST|IFF_MULTICAST|IFF_NOARP;
357 }
358 EXPORT_SYMBOL(ltalk_setup);
359
360 #endif /* CONFIG_ATALK || CONFIG_ATALK_MODULE */
361
362 int register_netdev(struct net_device *dev)
363 {
364         int err;
365
366         rtnl_lock();
367
368         /*
369          *      If the name is a format string the caller wants us to
370          *      do a name allocation
371          */
372          
373         if (strchr(dev->name, '%'))
374         {
375                 err = dev_alloc_name(dev, dev->name);
376                 if (err < 0)
377                         goto out;
378         }
379         
380         /*
381          *      Back compatibility hook. Kill this one in 2.5
382          */
383         
384         if (dev->name[0]==0 || dev->name[0]==' ')
385         {
386                 err = dev_alloc_name(dev, "eth%d");
387                 if (err < 0)
388                         goto out;
389         }
390
391         err = register_netdevice(dev);
392
393 out:
394         rtnl_unlock();
395         return err;
396 }
397
398 void unregister_netdev(struct net_device *dev)
399 {
400         rtnl_lock();
401         unregister_netdevice(dev);
402         rtnl_unlock();
403 }
404
405 EXPORT_SYMBOL(register_netdev);
406 EXPORT_SYMBOL(unregister_netdev);
407
408 #ifdef CONFIG_TR
409
410 void tr_setup(struct net_device *dev)
411 {
412         /*
413          *      Configure and register
414          */
415         
416         dev->hard_header        = tr_header;
417         dev->rebuild_header     = tr_rebuild_header;
418
419         dev->type               = ARPHRD_IEEE802_TR;
420         dev->hard_header_len    = TR_HLEN;
421         dev->mtu                = 2000;
422         dev->addr_len           = TR_ALEN;
423         dev->tx_queue_len       = 100;  /* Long queues on tr */
424         
425         memset(dev->broadcast,0xFF, TR_ALEN);
426
427         /* New-style flags. */
428         dev->flags              = IFF_BROADCAST | IFF_MULTICAST ;
429 }
430
431 /**
432  * alloc_trdev - Register token ring device
433  * @sizeof_priv: Size of additional driver-private structure to be allocated
434  *      for this token ring device
435  *
436  * Fill in the fields of the device structure with token ring-generic values.
437  *
438  * Constructs a new net device, complete with a private data area of
439  * size @sizeof_priv.  A 32-byte (not bit) alignment is enforced for
440  * this private data area.
441  */
442
443 struct net_device *alloc_trdev(int sizeof_priv)
444 {
445         return alloc_netdev(sizeof_priv, "tr%d", tr_setup);
446 }
447
448 EXPORT_SYMBOL(tr_setup);
449 EXPORT_SYMBOL(alloc_trdev);
450
451 #endif /* CONFIG_TR */
452
453 #ifdef CONFIG_NET_FC
454
455 void fc_setup(struct net_device *dev)
456 {
457         dev->hard_header        =        fc_header;
458         dev->rebuild_header     =        fc_rebuild_header;
459                 
460         dev->type               =        ARPHRD_IEEE802;
461         dev->hard_header_len    =        FC_HLEN;
462         dev->mtu                =        2024;
463         dev->addr_len           =        FC_ALEN;
464         dev->tx_queue_len       =        100; /* Long queues on fc */
465
466         memset(dev->broadcast,0xFF, FC_ALEN);
467
468         /* New-style flags. */
469         dev->flags              =        IFF_BROADCAST;
470 }
471
472 /**
473  * alloc_fcdev - Register fibre channel device
474  * @sizeof_priv: Size of additional driver-private structure to be allocated
475  *      for this fibre channel device
476  *
477  * Fill in the fields of the device structure with fibre channel-generic values.
478  *
479  * Constructs a new net device, complete with a private data area of
480  * size @sizeof_priv.  A 32-byte (not bit) alignment is enforced for
481  * this private data area.
482  */
483
484 struct net_device *alloc_fcdev(int sizeof_priv)
485 {
486         return alloc_netdev(sizeof_priv, "fc%d", fc_setup);
487 }
488
489 EXPORT_SYMBOL(fc_setup);
490 EXPORT_SYMBOL(alloc_fcdev);
491
492 #endif /* CONFIG_NET_FC */
493