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