]> nv-tegra.nvidia Code Review - linux-3.10.git/blob - arch/ia64/hp/sim/simeth.c
ae84a1018a89a3d24f400fe4943d6bc7ffc28e88
[linux-3.10.git] / arch / ia64 / hp / sim / simeth.c
1 /*
2  * Simulated Ethernet Driver
3  *
4  * Copyright (C) 1999-2001, 2003 Hewlett-Packard Co
5  *      Stephane Eranian <eranian@hpl.hp.com>
6  */
7 #include <linux/config.h>
8 #include <linux/kernel.h>
9 #include <linux/sched.h>
10 #include <linux/types.h>
11 #include <linux/in.h>
12 #include <linux/string.h>
13 #include <linux/init.h>
14 #include <linux/errno.h>
15 #include <linux/interrupt.h>
16 #include <linux/netdevice.h>
17 #include <linux/etherdevice.h>
18 #include <linux/inetdevice.h>
19 #include <linux/if_ether.h>
20 #include <linux/if_arp.h>
21 #include <linux/skbuff.h>
22 #include <linux/notifier.h>
23 #include <linux/bitops.h>
24 #include <asm/system.h>
25 #include <asm/irq.h>
26
27 #define SIMETH_RECV_MAX 10
28
29 /*
30  * Maximum possible received frame for Ethernet.
31  * We preallocate an sk_buff of that size to avoid costly
32  * memcpy for temporary buffer into sk_buff. We do basically
33  * what's done in other drivers, like eepro with a ring.
34  * The difference is, of course, that we don't have real DMA !!!
35  */
36 #define SIMETH_FRAME_SIZE       ETH_FRAME_LEN
37
38
39 #define SSC_NETDEV_PROBE                100
40 #define SSC_NETDEV_SEND                 101
41 #define SSC_NETDEV_RECV                 102
42 #define SSC_NETDEV_ATTACH               103
43 #define SSC_NETDEV_DETACH               104
44
45 #define NETWORK_INTR                    8
46
47 struct simeth_local {
48         struct net_device_stats stats;
49         int                     simfd;   /* descriptor in the simulator */
50 };
51
52 static int simeth_probe1(void);
53 static int simeth_open(struct net_device *dev);
54 static int simeth_close(struct net_device *dev);
55 static int simeth_tx(struct sk_buff *skb, struct net_device *dev);
56 static int simeth_rx(struct net_device *dev);
57 static struct net_device_stats *simeth_get_stats(struct net_device *dev);
58 static irqreturn_t simeth_interrupt(int irq, void *dev_id, struct pt_regs * regs);
59 static void set_multicast_list(struct net_device *dev);
60 static int simeth_device_event(struct notifier_block *this,unsigned long event, void *ptr);
61
62 static char *simeth_version="0.3";
63
64 /*
65  * This variable is used to establish a mapping between the Linux/ia64 kernel
66  * and the host linux kernel.
67  *
68  * As of today, we support only one card, even though most of the code
69  * is ready for many more. The mapping is then:
70  *      linux/ia64 -> linux/x86
71  *         eth0    -> eth1
72  *
73  * In the future, we some string operations, we could easily support up
74  * to 10 cards (0-9).
75  *
76  * The default mapping can be changed on the kernel command line by
77  * specifying simeth=ethX (or whatever string you want).
78  */
79 static char *simeth_device="eth0";       /* default host interface to use */
80
81
82
83 static volatile unsigned int card_count; /* how many cards "found" so far */
84 static int simeth_debug;                /* set to 1 to get debug information */
85
86 /*
87  * Used to catch IFF_UP & IFF_DOWN events
88  */
89 static struct notifier_block simeth_dev_notifier = {
90         simeth_device_event,
91         0
92 };
93
94
95 /*
96  * Function used when using a kernel command line option.
97  *
98  * Format: simeth=interface_name (like eth0)
99  */
100 static int __init
101 simeth_setup(char *str)
102 {
103         simeth_device = str;
104         return 1;
105 }
106
107 __setup("simeth=", simeth_setup);
108
109 /*
110  * Function used to probe for simeth devices when not installed
111  * as a loadable module
112  */
113
114 int __init
115 simeth_probe (void)
116 {
117         int r;
118
119         printk(KERN_INFO "simeth: v%s\n", simeth_version);
120
121         r = simeth_probe1();
122
123         if (r == 0) register_netdevice_notifier(&simeth_dev_notifier);
124
125         return r;
126 }
127
128 extern long ia64_ssc (long, long, long, long, int);
129 extern void ia64_ssc_connect_irq (long intr, long irq);
130
131 static inline int
132 netdev_probe(char *name, unsigned char *ether)
133 {
134         return ia64_ssc(__pa(name), __pa(ether), 0,0, SSC_NETDEV_PROBE);
135 }
136
137
138 static inline int
139 netdev_connect(int irq)
140 {
141         /* XXX Fix me
142          * this does not support multiple cards
143          * also no return value
144          */
145         ia64_ssc_connect_irq(NETWORK_INTR, irq);
146         return 0;
147 }
148
149 static inline int
150 netdev_attach(int fd, int irq, unsigned int ipaddr)
151 {
152         /* this puts the host interface in the right mode (start interrupting) */
153         return ia64_ssc(fd, ipaddr, 0,0, SSC_NETDEV_ATTACH);
154 }
155
156
157 static inline int
158 netdev_detach(int fd)
159 {
160         /*
161          * inactivate the host interface (don't interrupt anymore) */
162         return ia64_ssc(fd, 0,0,0, SSC_NETDEV_DETACH);
163 }
164
165 static inline int
166 netdev_send(int fd, unsigned char *buf, unsigned int len)
167 {
168         return ia64_ssc(fd, __pa(buf), len, 0, SSC_NETDEV_SEND);
169 }
170
171 static inline int
172 netdev_read(int fd, unsigned char *buf, unsigned int len)
173 {
174         return ia64_ssc(fd, __pa(buf), len, 0, SSC_NETDEV_RECV);
175 }
176
177 /*
178  * Function shared with module code, so cannot be in init section
179  *
180  * So far this function "detects" only one card (test_&_set) but could
181  * be extended easily.
182  *
183  * Return:
184  *      - -ENODEV is no device found
185  *      - -ENOMEM is no more memory
186  *      - 0 otherwise
187  */
188 static int
189 simeth_probe1(void)
190 {
191         unsigned char mac_addr[ETH_ALEN];
192         struct simeth_local *local;
193         struct net_device *dev;
194         int fd, i, err;
195
196         /*
197          * XXX Fix me
198          * let's support just one card for now
199          */
200         if (test_and_set_bit(0, &card_count))
201                 return -ENODEV;
202
203         /*
204          * check with the simulator for the device
205          */
206         fd = netdev_probe(simeth_device, mac_addr);
207         if (fd == -1)
208                 return -ENODEV;
209
210         dev = alloc_etherdev(sizeof(struct simeth_local));
211         if (!dev)
212                 return -ENOMEM;
213
214         memcpy(dev->dev_addr, mac_addr, sizeof(mac_addr));
215
216         local = dev->priv;
217         local->simfd = fd; /* keep track of underlying file descriptor */
218
219         dev->open               = simeth_open;
220         dev->stop               = simeth_close;
221         dev->hard_start_xmit    = simeth_tx;
222         dev->get_stats          = simeth_get_stats;
223         dev->set_multicast_list = set_multicast_list; /* no yet used */
224
225         err = register_netdev(dev);
226         if (err) {
227                 free_netdev(dev);
228                 return err;
229         }
230
231         dev->irq = assign_irq_vector(AUTO_ASSIGN);
232
233         /*
234          * attach the interrupt in the simulator, this does enable interrupts
235          * until a netdev_attach() is called
236          */
237         netdev_connect(dev->irq);
238
239         printk(KERN_INFO "%s: hosteth=%s simfd=%d, HwAddr",
240                dev->name, simeth_device, local->simfd);
241         for(i = 0; i < ETH_ALEN; i++) {
242                 printk(" %2.2x", dev->dev_addr[i]);
243         }
244         printk(", IRQ %d\n", dev->irq);
245
246         return 0;
247 }
248
249 /*
250  * actually binds the device to an interrupt vector
251  */
252 static int
253 simeth_open(struct net_device *dev)
254 {
255         if (request_irq(dev->irq, simeth_interrupt, 0, "simeth", dev)) {
256                 printk(KERN_WARNING "simeth: unable to get IRQ %d.\n", dev->irq);
257                 return -EAGAIN;
258         }
259
260         netif_start_queue(dev);
261
262         return 0;
263 }
264
265 /* copied from lapbether.c */
266 static __inline__ int dev_is_ethdev(struct net_device *dev)
267 {
268        return ( dev->type == ARPHRD_ETHER && strncmp(dev->name, "dummy", 5));
269 }
270
271
272 /*
273  * Handler for IFF_UP or IFF_DOWN
274  *
275  * The reason for that is that we don't want to be interrupted when the
276  * interface is down. There is no way to unconnect in the simualtor. Instead
277  * we use this function to shutdown packet processing in the frame filter
278  * in the simulator. Thus no interrupts are generated
279  *
280  *
281  * That's also the place where we pass the IP address of this device to the
282  * simulator so that that we can start filtering packets for it
283  *
284  * There may be a better way of doing this, but I don't know which yet.
285  */
286 static int
287 simeth_device_event(struct notifier_block *this,unsigned long event, void *ptr)
288 {
289         struct net_device *dev = ptr;
290         struct simeth_local *local;
291         struct in_device *in_dev;
292         struct in_ifaddr **ifap = NULL;
293         struct in_ifaddr *ifa = NULL;
294         int r;
295
296
297         if ( ! dev ) {
298                 printk(KERN_WARNING "simeth_device_event dev=0\n");
299                 return NOTIFY_DONE;
300         }
301
302         if ( event != NETDEV_UP && event != NETDEV_DOWN ) return NOTIFY_DONE;
303
304         /*
305          * Check whether or not it's for an ethernet device
306          *
307          * XXX Fixme: This works only as long as we support one
308          * type of ethernet device.
309          */
310         if ( !dev_is_ethdev(dev) ) return NOTIFY_DONE;
311
312         if ((in_dev=dev->ip_ptr) != NULL) {
313                 for (ifap=&in_dev->ifa_list; (ifa=*ifap) != NULL; ifap=&ifa->ifa_next)
314                         if (strcmp(dev->name, ifa->ifa_label) == 0) break;
315         }
316         if ( ifa == NULL ) {
317                 printk(KERN_ERR "simeth_open: can't find device %s's ifa\n", dev->name);
318                 return NOTIFY_DONE;
319         }
320
321         printk(KERN_INFO "simeth_device_event: %s ipaddr=0x%x\n",
322                dev->name, htonl(ifa->ifa_local));
323
324         /*
325          * XXX Fix me
326          * if the device was up, and we're simply reconfiguring it, not sure
327          * we get DOWN then UP.
328          */
329
330         local = dev->priv;
331         /* now do it for real */
332         r = event == NETDEV_UP ?
333                 netdev_attach(local->simfd, dev->irq, htonl(ifa->ifa_local)):
334                 netdev_detach(local->simfd);
335
336         printk(KERN_INFO "simeth: netdev_attach/detach: event=%s ->%d\n",
337                event == NETDEV_UP ? "attach":"detach", r);
338
339         return NOTIFY_DONE;
340 }
341
342 static int
343 simeth_close(struct net_device *dev)
344 {
345         netif_stop_queue(dev);
346
347         free_irq(dev->irq, dev);
348
349         return 0;
350 }
351
352 /*
353  * Only used for debug
354  */
355 static void
356 frame_print(unsigned char *from, unsigned char *frame, int len)
357 {
358         int i;
359
360         printk("%s: (%d) %02x", from, len, frame[0] & 0xff);
361         for(i=1; i < 6; i++ ) {
362                 printk(":%02x", frame[i] &0xff);
363         }
364         printk(" %2x", frame[6] &0xff);
365         for(i=7; i < 12; i++ ) {
366                 printk(":%02x", frame[i] &0xff);
367         }
368         printk(" [%02x%02x]\n", frame[12], frame[13]);
369
370         for(i=14; i < len; i++ ) {
371                 printk("%02x ", frame[i] &0xff);
372                 if ( (i%10)==0) printk("\n");
373         }
374         printk("\n");
375 }
376
377
378 /*
379  * Function used to transmit of frame, very last one on the path before
380  * going to the simulator.
381  */
382 static int
383 simeth_tx(struct sk_buff *skb, struct net_device *dev)
384 {
385         struct simeth_local *local = dev->priv;
386
387 #if 0
388         /* ensure we have at least ETH_ZLEN bytes (min frame size) */
389         unsigned int length = ETH_ZLEN < skb->len ? skb->len : ETH_ZLEN;
390         /* Where do the extra padding bytes comes from inthe skbuff ? */
391 #else
392         /* the real driver in the host system is going to take care of that
393          * or maybe it's the NIC itself.
394          */
395         unsigned int length = skb->len;
396 #endif
397
398         local->stats.tx_bytes += skb->len;
399         local->stats.tx_packets++;
400
401
402         if (simeth_debug > 5) frame_print("simeth_tx", skb->data, length);
403
404         netdev_send(local->simfd, skb->data, length);
405
406         /*
407          * we are synchronous on write, so we don't simulate a
408          * trasnmit complete interrupt, thus we don't need to arm a tx
409          */
410
411         dev_kfree_skb(skb);
412         return 0;
413 }
414
415 static inline struct sk_buff *
416 make_new_skb(struct net_device *dev)
417 {
418         struct sk_buff *nskb;
419
420         /*
421          * The +2 is used to make sure that the IP header is nicely
422          * aligned (on 4byte boundary I assume 14+2=16)
423          */
424         nskb = dev_alloc_skb(SIMETH_FRAME_SIZE + 2);
425         if ( nskb == NULL ) {
426                 printk(KERN_NOTICE "%s: memory squeeze. dropping packet.\n", dev->name);
427                 return NULL;
428         }
429         nskb->dev = dev;
430
431         skb_reserve(nskb, 2);   /* Align IP on 16 byte boundaries */
432
433         skb_put(nskb,SIMETH_FRAME_SIZE);
434
435         return nskb;
436 }
437
438 /*
439  * called from interrupt handler to process a received frame
440  */
441 static int
442 simeth_rx(struct net_device *dev)
443 {
444         struct simeth_local     *local;
445         struct sk_buff          *skb;
446         int                     len;
447         int                     rcv_count = SIMETH_RECV_MAX;
448
449         local = dev->priv;
450         /*
451          * the loop concept has been borrowed from other drivers
452          * looks to me like it's a throttling thing to avoid pushing to many
453          * packets at one time into the stack. Making sure we can process them
454          * upstream and make forward progress overall
455          */
456         do {
457                 if ( (skb=make_new_skb(dev)) == NULL ) {
458                         printk(KERN_NOTICE "%s: memory squeeze. dropping packet.\n", dev->name);
459                         local->stats.rx_dropped++;
460                         return 0;
461                 }
462                 /*
463                  * Read only one frame at a time
464                  */
465                 len = netdev_read(local->simfd, skb->data, SIMETH_FRAME_SIZE);
466                 if ( len == 0 ) {
467                         if ( simeth_debug > 0 ) printk(KERN_WARNING "%s: count=%d netdev_read=0\n",
468                                                        dev->name, SIMETH_RECV_MAX-rcv_count);
469                         break;
470                 }
471 #if 0
472                 /*
473                  * XXX Fix me
474                  * Should really do a csum+copy here
475                  */
476                 memcpy(skb->data, frame, len);
477 #endif
478                 skb->protocol = eth_type_trans(skb, dev);
479
480                 if ( simeth_debug > 6 ) frame_print("simeth_rx", skb->data, len);
481
482                 /*
483                  * push the packet up & trigger software interrupt
484                  */
485                 netif_rx(skb);
486
487                 local->stats.rx_packets++;
488                 local->stats.rx_bytes += len;
489
490         } while ( --rcv_count );
491
492         return len; /* 0 = nothing left to read, otherwise, we can try again */
493 }
494
495 /*
496  * Interrupt handler (Yes, we can do it too !!!)
497  */
498 static irqreturn_t
499 simeth_interrupt(int irq, void *dev_id, struct pt_regs * regs)
500 {
501         struct net_device *dev = dev_id;
502
503         if ( dev == NULL ) {
504                 printk(KERN_WARNING "simeth: irq %d for unknown device\n", irq);
505                 return IRQ_NONE;
506         }
507
508         /*
509          * very simple loop because we get interrupts only when receiving
510          */
511         while (simeth_rx(dev));
512         return IRQ_HANDLED;
513 }
514
515 static struct net_device_stats *
516 simeth_get_stats(struct net_device *dev)
517 {
518         struct simeth_local *local = dev->priv;
519
520         return &local->stats;
521 }
522
523 /* fake multicast ability */
524 static void
525 set_multicast_list(struct net_device *dev)
526 {
527         printk(KERN_WARNING "%s: set_multicast_list called\n", dev->name);
528 }
529
530 __initcall(simeth_probe);