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