debuggers.hg

view xen/drivers/net/net_init.c @ 618:4480b471191c

bitkeeper revision 1.259.2.7 (3f0c428fGYxQAV_56B2hOOjYs1PF0A)

Port a bunch of network drivers for low-quality NICS (which will incur extra copying overheads within Xen). But will allow us to work on a wider range of systems at least.
author kaf24@scramble.cl.cam.ac.uk
date Wed Jul 09 16:27:59 2003 +0000 (2003-07-09)
parents 170eb7974e43
children 125f43340354
line source
1 /* net_init.c: Initialization for network devices. */
2 /*
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
8 Annapolis MD 21403
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
34 */
36 #include <linux/config.h>
37 #include <linux/module.h>
38 #include <linux/errno.h>
39 //#include <linux/kernel.h>
40 #include <linux/sched.h>
41 #include <linux/types.h>
42 //#include <linux/fs.h>
43 #include <linux/slab.h>
44 #include <linux/if_ether.h>
45 #include <linux/lib.h>
46 #include <linux/netdevice.h>
47 #include <linux/etherdevice.h>
48 //#include <linux/fddidevice.h>
49 //#include <linux/hippidevice.h>
50 //#include <linux/trdevice.h>
51 //#include <linux/fcdevice.h>
52 //#include <linux/if_arp.h>
53 //#include <linux/if_ltalk.h>
54 //#include <linux/rtnetlink.h>
55 //#include <net/neighbour.h>
57 #define rtnl_lock() ((void)0)
58 #define rtnl_unlock() ((void)0)
60 /* The network devices currently exist only in the socket namespace, so these
61 entries are unused. The only ones that make sense are
62 open start the ethercard
63 close stop the ethercard
64 ioctl To get statistics, perhaps set the interface port (AUI, BNC, etc.)
65 One can also imagine getting raw packets using
66 read & write
67 but this is probably better handled by a raw packet socket.
69 Given that almost all of these functions are handled in the current
70 socket-based scheme, putting ethercard devices in /dev/ seems pointless.
72 [Removed all support for /dev network devices. When someone adds
73 streams then by magic we get them, but otherwise they are un-needed
74 and a space waste]
75 */
78 static struct net_device *alloc_netdev(int sizeof_priv, const char *mask,
79 void (*setup)(struct net_device *))
80 {
81 struct net_device *dev;
82 int alloc_size;
84 /* ensure 32-byte alignment of the private area */
85 alloc_size = sizeof (*dev) + sizeof_priv + 31;
87 dev = (struct net_device *) kmalloc (alloc_size, GFP_KERNEL);
88 if (dev == NULL)
89 {
90 printk(KERN_ERR "alloc_dev: Unable to allocate device memory.\n");
91 return NULL;
92 }
94 memset(dev, 0, alloc_size);
96 if (sizeof_priv)
97 dev->priv = (void *) (((long)(dev + 1) + 31) & ~31);
99 setup(dev);
100 strcpy(dev->name, mask);
102 return dev;
103 }
105 static struct net_device *init_alloc_dev(int sizeof_priv)
106 {
107 struct net_device *dev;
108 int alloc_size;
110 /* ensure 32-byte alignment of the private area */
111 alloc_size = sizeof (*dev) + sizeof_priv + 31;
113 dev = (struct net_device *) kmalloc (alloc_size, GFP_KERNEL);
114 if (dev == NULL)
115 {
116 printk(KERN_ERR "alloc_dev: Unable to allocate device memory.\n");
117 return NULL;
118 }
120 memset(dev, 0, alloc_size);
122 if (sizeof_priv)
123 dev->priv = (void *) (((long)(dev + 1) + 31) & ~31);
125 return dev;
126 }
128 /*
129 * Create and name a device from a prototype, then perform any needed
130 * setup.
131 */
133 static struct net_device *init_netdev(struct net_device *dev, int sizeof_priv,
134 char *mask, void (*setup)(struct net_device *))
135 {
136 int new_device = 0;
138 /*
139 * Allocate a device if one is not provided.
140 */
142 if (dev == NULL) {
143 dev=init_alloc_dev(sizeof_priv);
144 if(dev==NULL)
145 return NULL;
146 new_device = 1;
147 }
149 /*
150 * Allocate a name
151 */
153 if (dev->name[0] == '\0' || dev->name[0] == ' ') {
154 strcpy(dev->name, mask);
155 if (dev_alloc_name(dev, mask)<0) {
156 if (new_device)
157 kfree(dev);
158 return NULL;
159 }
160 }
162 //netdev_boot_setup_check(dev);
164 /*
165 * Configure via the caller provided setup function then
166 * register if needed.
167 */
169 setup(dev);
171 if (new_device) {
172 int err;
174 rtnl_lock();
175 err = register_netdevice(dev);
176 rtnl_unlock();
178 if (err < 0) {
179 kfree(dev);
180 dev = NULL;
181 }
182 }
183 return dev;
184 }
186 #if defined(CONFIG_HIPPI) || defined(CONFIG_TR) || defined(CONFIG_NET_FC)
187 static int __register_netdev(struct net_device *dev)
188 {
189 if (dev->init && dev->init(dev) != 0) {
190 unregister_netdev(dev);
191 return -EIO;
192 }
193 return 0;
194 }
195 #endif
197 /**
198 * init_etherdev - Register ethernet device
199 * @dev: An ethernet device structure to be filled in, or %NULL if a new
200 * struct should be allocated.
201 * @sizeof_priv: Size of additional driver-private structure to be allocated
202 * for this ethernet device
203 *
204 * Fill in the fields of the device structure with ethernet-generic values.
205 *
206 * If no device structure is passed, a new one is constructed, complete with
207 * a private data area of size @sizeof_priv. A 32-byte (not bit)
208 * alignment is enforced for this private data area.
209 *
210 * If an empty string area is passed as dev->name, or a new structure is made,
211 * a new name string is constructed.
212 */
214 struct net_device *init_etherdev(struct net_device *dev, int sizeof_priv)
215 {
216 return init_netdev(dev, sizeof_priv, "eth%d", ether_setup);
217 }
219 /**
220 * alloc_etherdev - Allocates and sets up an ethernet device
221 * @sizeof_priv: Size of additional driver-private structure to be allocated
222 * for this ethernet device
223 *
224 * Fill in the fields of the device structure with ethernet-generic
225 * values. Basically does everything except registering the device.
226 *
227 * Constructs a new net device, complete with a private data area of
228 * size @sizeof_priv. A 32-byte (not bit) alignment is enforced for
229 * this private data area.
230 */
232 struct net_device *alloc_etherdev(int sizeof_priv)
233 {
234 return alloc_netdev(sizeof_priv, "eth%d", ether_setup);
235 }
237 EXPORT_SYMBOL(init_etherdev);
238 EXPORT_SYMBOL(alloc_etherdev);
240 static int eth_mac_addr(struct net_device *dev, void *p)
241 {
242 struct sockaddr *addr=p;
243 if (netif_running(dev))
244 return -EBUSY;
245 memcpy(dev->dev_addr, addr->sa_data,dev->addr_len);
246 return 0;
247 }
249 static int eth_change_mtu(struct net_device *dev, int new_mtu)
250 {
251 if ((new_mtu < 68) || (new_mtu > 1500))
252 return -EINVAL;
253 dev->mtu = new_mtu;
254 return 0;
255 }
257 #ifdef CONFIG_FDDI
259 /**
260 * init_fddidev - Register FDDI device
261 * @dev: A FDDI device structure to be filled in, or %NULL if a new
262 * struct should be allocated.
263 * @sizeof_priv: Size of additional driver-private structure to be allocated
264 * for this ethernet device
265 *
266 * Fill in the fields of the device structure with FDDI-generic values.
267 *
268 * If no device structure is passed, a new one is constructed, complete with
269 * a private data area of size @sizeof_priv. A 32-byte (not bit)
270 * alignment is enforced for this private data area.
271 *
272 * If an empty string area is passed as dev->name, or a new structure is made,
273 * a new name string is constructed.
274 */
276 struct net_device *init_fddidev(struct net_device *dev, int sizeof_priv)
277 {
278 return init_netdev(dev, sizeof_priv, "fddi%d", fddi_setup);
279 }
281 /**
282 * alloc_fddidev - Register FDDI device
283 * @sizeof_priv: Size of additional driver-private structure to be allocated
284 * for this FDDI device
285 *
286 * Fill in the fields of the device structure with FDDI-generic values.
287 *
288 * Constructs a new net device, complete with a private data area of
289 * size @sizeof_priv. A 32-byte (not bit) alignment is enforced for
290 * this private data area.
291 */
293 struct net_device *alloc_fddidev(int sizeof_priv)
294 {
295 return alloc_netdev(sizeof_priv, "fddi%d", fddi_setup);
296 }
298 EXPORT_SYMBOL(init_fddidev);
299 EXPORT_SYMBOL(alloc_fddidev);
301 static int fddi_change_mtu(struct net_device *dev, int new_mtu)
302 {
303 if ((new_mtu < FDDI_K_SNAP_HLEN) || (new_mtu > FDDI_K_SNAP_DLEN))
304 return(-EINVAL);
305 dev->mtu = new_mtu;
306 return(0);
307 }
309 #endif /* CONFIG_FDDI */
311 #ifdef CONFIG_HIPPI
313 static int hippi_change_mtu(struct net_device *dev, int new_mtu)
314 {
315 /*
316 * HIPPI's got these nice large MTUs.
317 */
318 if ((new_mtu < 68) || (new_mtu > 65280))
319 return -EINVAL;
320 dev->mtu = new_mtu;
321 return(0);
322 }
325 /*
326 * For HIPPI we will actually use the lower 4 bytes of the hardware
327 * address as the I-FIELD rather than the actual hardware address.
328 */
329 static int hippi_mac_addr(struct net_device *dev, void *p)
330 {
331 struct sockaddr *addr = p;
332 if (netif_running(dev))
333 return -EBUSY;
334 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
335 return 0;
336 }
339 /**
340 * init_hippi_dev - Register HIPPI device
341 * @dev: A HIPPI device structure to be filled in, or %NULL if a new
342 * struct should be allocated.
343 * @sizeof_priv: Size of additional driver-private structure to be allocated
344 * for this ethernet device
345 *
346 * Fill in the fields of the device structure with HIPPI-generic values.
347 *
348 * If no device structure is passed, a new one is constructed, complete with
349 * a private data area of size @sizeof_priv. A 32-byte (not bit)
350 * alignment is enforced for this private data area.
351 *
352 * If an empty string area is passed as dev->name, or a new structure is made,
353 * a new name string is constructed.
354 */
356 struct net_device *init_hippi_dev(struct net_device *dev, int sizeof_priv)
357 {
358 return init_netdev(dev, sizeof_priv, "hip%d", hippi_setup);
359 }
361 /**
362 * alloc_hippi_dev - Register HIPPI device
363 * @sizeof_priv: Size of additional driver-private structure to be allocated
364 * for this HIPPI device
365 *
366 * Fill in the fields of the device structure with HIPPI-generic values.
367 *
368 * Constructs a new net device, complete with a private data area of
369 * size @sizeof_priv. A 32-byte (not bit) alignment is enforced for
370 * this private data area.
371 */
373 struct net_device *alloc_hippi_dev(int sizeof_priv)
374 {
375 return alloc_netdev(sizeof_priv, "hip%d", hippi_setup);
376 }
378 int register_hipdev(struct net_device *dev)
379 {
380 return __register_netdev(dev);
381 }
383 void unregister_hipdev(struct net_device *dev)
384 {
385 unregister_netdev(dev);
386 }
388 EXPORT_SYMBOL(init_hippi_dev);
389 EXPORT_SYMBOL(alloc_hippi_dev);
390 EXPORT_SYMBOL(register_hipdev);
391 EXPORT_SYMBOL(unregister_hipdev);
393 static int hippi_neigh_setup_dev(struct net_device *dev, struct neigh_parms *p)
394 {
395 /* Never send broadcast/multicast ARP messages */
396 p->mcast_probes = 0;
398 /* In IPv6 unicast probes are valid even on NBMA,
399 * because they are encapsulated in normal IPv6 protocol.
400 * Should be a generic flag.
401 */
402 if (p->tbl->family != AF_INET6)
403 p->ucast_probes = 0;
404 return 0;
405 }
407 #endif /* CONFIG_HIPPI */
409 void ether_setup(struct net_device *dev)
410 {
411 /* Fill in the fields of the device structure with ethernet-generic values.
412 This should be in a common file instead of per-driver. */
414 dev->change_mtu = eth_change_mtu;
415 dev->hard_header = eth_header;
416 dev->rebuild_header = eth_rebuild_header;
417 dev->set_mac_address = eth_mac_addr;
418 dev->hard_header_cache = eth_header_cache;
419 dev->header_cache_update= eth_header_cache_update;
420 dev->hard_header_parse = eth_header_parse;
422 dev->type = 0; //ARPHRD_ETHER;
423 dev->hard_header_len = ETH_HLEN;
424 dev->mtu = 1500; /* eth_mtu */
425 dev->addr_len = ETH_ALEN;
427 memset(dev->broadcast,0xFF, ETH_ALEN);
429 /* New-style flags. */
430 dev->flags = IFF_BROADCAST|IFF_MULTICAST;
431 }
432 EXPORT_SYMBOL(ether_setup);
434 #ifdef CONFIG_FDDI
436 void fddi_setup(struct net_device *dev)
437 {
438 /*
439 * Fill in the fields of the device structure with FDDI-generic values.
440 * This should be in a common file instead of per-driver.
441 */
443 dev->change_mtu = fddi_change_mtu;
444 dev->hard_header = fddi_header;
445 dev->rebuild_header = fddi_rebuild_header;
447 dev->type = ARPHRD_FDDI;
448 dev->hard_header_len = FDDI_K_SNAP_HLEN+3; /* Assume 802.2 SNAP hdr len + 3 pad bytes */
449 dev->mtu = FDDI_K_SNAP_DLEN; /* Assume max payload of 802.2 SNAP frame */
450 dev->addr_len = FDDI_K_ALEN;
452 memset(dev->broadcast, 0xFF, FDDI_K_ALEN);
454 /* New-style flags */
455 dev->flags = IFF_BROADCAST | IFF_MULTICAST;
456 }
457 EXPORT_SYMBOL(fddi_setup);
459 #endif /* CONFIG_FDDI */
461 #ifdef CONFIG_HIPPI
462 void hippi_setup(struct net_device *dev)
463 {
464 dev->set_multicast_list = NULL;
465 dev->change_mtu = hippi_change_mtu;
466 dev->hard_header = hippi_header;
467 dev->rebuild_header = hippi_rebuild_header;
468 dev->set_mac_address = hippi_mac_addr;
469 dev->hard_header_parse = NULL;
470 dev->hard_header_cache = NULL;
471 dev->header_cache_update = NULL;
472 dev->neigh_setup = hippi_neigh_setup_dev;
474 /*
475 * We don't support HIPPI `ARP' for the time being, and probably
476 * never will unless someone else implements it. However we
477 * still need a fake ARPHRD to make ifconfig and friends play ball.
478 */
479 dev->type = ARPHRD_HIPPI;
480 dev->hard_header_len = HIPPI_HLEN;
481 dev->mtu = 65280;
482 dev->addr_len = HIPPI_ALEN;
484 memset(dev->broadcast, 0xFF, HIPPI_ALEN);
486 /*
487 * HIPPI doesn't support broadcast+multicast and we only use
488 * static ARP tables. ARP is disabled by hippi_neigh_setup_dev.
489 */
490 dev->flags = 0;
491 }
492 EXPORT_SYMBOL(hippi_setup);
493 #endif /* CONFIG_HIPPI */
495 #if defined(CONFIG_ATALK) || defined(CONFIG_ATALK_MODULE)
497 static int ltalk_change_mtu(struct net_device *dev, int mtu)
498 {
499 return -EINVAL;
500 }
502 static int ltalk_mac_addr(struct net_device *dev, void *addr)
503 {
504 return -EINVAL;
505 }
508 void ltalk_setup(struct net_device *dev)
509 {
510 /* Fill in the fields of the device structure with localtalk-generic values. */
512 dev->change_mtu = ltalk_change_mtu;
513 dev->hard_header = NULL;
514 dev->rebuild_header = NULL;
515 dev->set_mac_address = ltalk_mac_addr;
516 dev->hard_header_cache = NULL;
517 dev->header_cache_update= NULL;
519 dev->type = ARPHRD_LOCALTLK;
520 dev->hard_header_len = LTALK_HLEN;
521 dev->mtu = LTALK_MTU;
522 dev->addr_len = LTALK_ALEN;
524 dev->broadcast[0] = 0xFF;
526 dev->flags = IFF_BROADCAST|IFF_MULTICAST|IFF_NOARP;
527 }
528 EXPORT_SYMBOL(ltalk_setup);
530 #endif /* CONFIG_ATALK || CONFIG_ATALK_MODULE */
532 int register_netdev(struct net_device *dev)
533 {
534 int err;
536 rtnl_lock();
538 /*
539 * If the name is a format string the caller wants us to
540 * do a name allocation
541 */
543 if (strchr(dev->name, '%'))
544 {
545 err = dev_alloc_name(dev, dev->name);
546 if (err < 0)
547 goto out;
548 }
550 /*
551 * Back compatibility hook. Kill this one in 2.5
552 */
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 }
561 err = register_netdevice(dev);
563 out:
564 rtnl_unlock();
565 return err;
566 }
568 void unregister_netdev(struct net_device *dev)
569 {
570 rtnl_lock();
571 unregister_netdevice(dev);
572 rtnl_unlock();
573 }
575 EXPORT_SYMBOL(register_netdev);
576 EXPORT_SYMBOL(unregister_netdev);
579 void alert_slow_netdevice(struct net_device *dev, char *nictype)
580 {
581 printk("***************************\n");
582 printk("* WARNING FOR NET DEVICE %s (NIC type '%s'):\n",
583 dev->name, nictype);
584 printk("* This NIC cannot support fully efficient networking in Xen.\n");
585 printk("* In particular, extra packet copies will be incurred!\n");
586 printk("* See documentation for a list of recommended NIC types\n");
587 printk("***************************\n");
588 }
591 #ifdef CONFIG_TR
593 void tr_setup(struct net_device *dev)
594 {
595 /*
596 * Configure and register
597 */
599 dev->hard_header = tr_header;
600 dev->rebuild_header = tr_rebuild_header;
602 dev->type = ARPHRD_IEEE802_TR;
603 dev->hard_header_len = TR_HLEN;
604 dev->mtu = 2000;
605 dev->addr_len = TR_ALEN;
607 memset(dev->broadcast,0xFF, TR_ALEN);
609 /* New-style flags. */
610 dev->flags = IFF_BROADCAST | IFF_MULTICAST ;
611 }
613 /**
614 * init_trdev - Register token ring device
615 * @dev: A token ring device structure to be filled in, or %NULL if a new
616 * struct should be allocated.
617 * @sizeof_priv: Size of additional driver-private structure to be allocated
618 * for this ethernet device
619 *
620 * Fill in the fields of the device structure with token ring-generic values.
621 *
622 * If no device structure is passed, a new one is constructed, complete with
623 * a private data area of size @sizeof_priv. A 32-byte (not bit)
624 * alignment is enforced for this private data area.
625 *
626 * If an empty string area is passed as dev->name, or a new structure is made,
627 * a new name string is constructed.
628 */
630 struct net_device *init_trdev(struct net_device *dev, int sizeof_priv)
631 {
632 return init_netdev(dev, sizeof_priv, "tr%d", tr_setup);
633 }
635 /**
636 * alloc_trdev - Register token ring device
637 * @sizeof_priv: Size of additional driver-private structure to be allocated
638 * for this token ring device
639 *
640 * Fill in the fields of the device structure with token ring-generic values.
641 *
642 * Constructs a new net device, complete with a private data area of
643 * size @sizeof_priv. A 32-byte (not bit) alignment is enforced for
644 * this private data area.
645 */
647 struct net_device *alloc_trdev(int sizeof_priv)
648 {
649 return alloc_netdev(sizeof_priv, "tr%d", tr_setup);
650 }
652 int register_trdev(struct net_device *dev)
653 {
654 return __register_netdev(dev);
655 }
657 void unregister_trdev(struct net_device *dev)
658 {
659 unregister_netdev(dev);
660 }
662 EXPORT_SYMBOL(tr_setup);
663 EXPORT_SYMBOL(init_trdev);
664 EXPORT_SYMBOL(alloc_trdev);
665 EXPORT_SYMBOL(register_trdev);
666 EXPORT_SYMBOL(unregister_trdev);
668 #endif /* CONFIG_TR */
671 #ifdef CONFIG_NET_FC
673 void fc_setup(struct net_device *dev)
674 {
675 dev->hard_header = fc_header;
676 dev->rebuild_header = fc_rebuild_header;
678 dev->type = ARPHRD_IEEE802;
679 dev->hard_header_len = FC_HLEN;
680 dev->mtu = 2024;
681 dev->addr_len = FC_ALEN;
683 memset(dev->broadcast,0xFF, FC_ALEN);
685 /* New-style flags. */
686 dev->flags = IFF_BROADCAST;
687 }
689 /**
690 * init_fcdev - Register fibre channel device
691 * @dev: A fibre channel device structure to be filled in, or %NULL if a new
692 * struct should be allocated.
693 * @sizeof_priv: Size of additional driver-private structure to be allocated
694 * for this ethernet device
695 *
696 * Fill in the fields of the device structure with fibre channel-generic values.
697 *
698 * If no device structure is passed, a new one is constructed, complete with
699 * a private data area of size @sizeof_priv. A 32-byte (not bit)
700 * alignment is enforced for this private data area.
701 *
702 * If an empty string area is passed as dev->name, or a new structure is made,
703 * a new name string is constructed.
704 */
706 struct net_device *init_fcdev(struct net_device *dev, int sizeof_priv)
707 {
708 return init_netdev(dev, sizeof_priv, "fc%d", fc_setup);
709 }
711 /**
712 * alloc_fcdev - Register fibre channel device
713 * @sizeof_priv: Size of additional driver-private structure to be allocated
714 * for this fibre channel device
715 *
716 * Fill in the fields of the device structure with fibre channel-generic values.
717 *
718 * Constructs a new net device, complete with a private data area of
719 * size @sizeof_priv. A 32-byte (not bit) alignment is enforced for
720 * this private data area.
721 */
723 struct net_device *alloc_fcdev(int sizeof_priv)
724 {
725 return alloc_netdev(sizeof_priv, "fc%d", fc_setup);
726 }
728 int register_fcdev(struct net_device *dev)
729 {
730 return __register_netdev(dev);
731 }
733 void unregister_fcdev(struct net_device *dev)
734 {
735 unregister_netdev(dev);
736 }
738 EXPORT_SYMBOL(fc_setup);
739 EXPORT_SYMBOL(init_fcdev);
740 EXPORT_SYMBOL(alloc_fcdev);
741 EXPORT_SYMBOL(register_fcdev);
742 EXPORT_SYMBOL(unregister_fcdev);
744 #endif /* CONFIG_NET_FC */