1 // SPDX-License-Identifier: GPL-2.0
3 * Simulated Ethernet Driver
5 * Copyright (C) 1999-2001, 2003 Hewlett-Packard Co
6 * Stephane Eranian <eranian@hpl.hp.com>
8 #include <linux/kernel.h>
9 #include <linux/sched.h>
10 #include <linux/types.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>
25 #include <asm/hpsim.h>
27 #include "hpsim_ssc.h"
29 #define SIMETH_RECV_MAX 10
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 !!!
38 #define SIMETH_FRAME_SIZE ETH_FRAME_LEN
41 #define NETWORK_INTR 8
44 struct net_device_stats stats
;
45 int simfd
; /* descriptor in the simulator */
48 static int simeth_probe1(void);
49 static int simeth_open(struct net_device
*dev
);
50 static int simeth_close(struct net_device
*dev
);
51 static int simeth_tx(struct sk_buff
*skb
, struct net_device
*dev
);
52 static int simeth_rx(struct net_device
*dev
);
53 static struct net_device_stats
*simeth_get_stats(struct net_device
*dev
);
54 static irqreturn_t
simeth_interrupt(int irq
, void *dev_id
);
55 static void set_multicast_list(struct net_device
*dev
);
56 static int simeth_device_event(struct notifier_block
*this,unsigned long event
, void *ptr
);
58 static char *simeth_version
="0.3";
61 * This variable is used to establish a mapping between the Linux/ia64 kernel
62 * and the host linux kernel.
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
69 * In the future, we some string operations, we could easily support up
72 * The default mapping can be changed on the kernel command line by
73 * specifying simeth=ethX (or whatever string you want).
75 static char *simeth_device
="eth0"; /* default host interface to use */
79 static volatile unsigned int card_count
; /* how many cards "found" so far */
80 static int simeth_debug
; /* set to 1 to get debug information */
83 * Used to catch IFF_UP & IFF_DOWN events
85 static struct notifier_block simeth_dev_notifier
= {
92 * Function used when using a kernel command line option.
94 * Format: simeth=interface_name (like eth0)
97 simeth_setup(char *str
)
103 __setup("simeth=", simeth_setup
);
106 * Function used to probe for simeth devices when not installed
107 * as a loadable module
115 printk(KERN_INFO
"simeth: v%s\n", simeth_version
);
119 if (r
== 0) register_netdevice_notifier(&simeth_dev_notifier
);
125 netdev_probe(char *name
, unsigned char *ether
)
127 return ia64_ssc(__pa(name
), __pa(ether
), 0,0, SSC_NETDEV_PROBE
);
132 netdev_attach(int fd
, int irq
, unsigned int ipaddr
)
134 /* this puts the host interface in the right mode (start interrupting) */
135 return ia64_ssc(fd
, ipaddr
, 0,0, SSC_NETDEV_ATTACH
);
140 netdev_detach(int fd
)
143 * inactivate the host interface (don't interrupt anymore) */
144 return ia64_ssc(fd
, 0,0,0, SSC_NETDEV_DETACH
);
148 netdev_send(int fd
, unsigned char *buf
, unsigned int len
)
150 return ia64_ssc(fd
, __pa(buf
), len
, 0, SSC_NETDEV_SEND
);
154 netdev_read(int fd
, unsigned char *buf
, unsigned int len
)
156 return ia64_ssc(fd
, __pa(buf
), len
, 0, SSC_NETDEV_RECV
);
159 static 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
,
164 .ndo_set_rx_mode
= set_multicast_list
, /* not yet used */
169 * Function shared with module code, so cannot be in init section
171 * So far this function "detects" only one card (test_&_set) but could
172 * be extended easily.
175 * - -ENODEV is no device found
176 * - -ENOMEM is no more memory
182 unsigned char mac_addr
[ETH_ALEN
];
183 struct simeth_local
*local
;
184 struct net_device
*dev
;
189 * let's support just one card for now
191 if (test_and_set_bit(0, &card_count
))
195 * check with the simulator for the device
197 fd
= netdev_probe(simeth_device
, mac_addr
);
201 dev
= alloc_etherdev(sizeof(struct simeth_local
));
205 memcpy(dev
->dev_addr
, mac_addr
, sizeof(mac_addr
));
207 local
= netdev_priv(dev
);
208 local
->simfd
= fd
; /* keep track of underlying file descriptor */
210 dev
->netdev_ops
= &simeth_netdev_ops
;
212 err
= register_netdev(dev
);
219 * attach the interrupt in the simulator, this does enable interrupts
220 * until a netdev_attach() is called
222 if ((rc
= hpsim_get_irq(NETWORK_INTR
)) < 0)
223 panic("%s: out of interrupt vectors!\n", __func__
);
226 printk(KERN_INFO
"%s: hosteth=%s simfd=%d, HwAddr=%pm, IRQ %d\n",
227 dev
->name
, simeth_device
, local
->simfd
, dev
->dev_addr
, dev
->irq
);
233 * actually binds the device to an interrupt vector
236 simeth_open(struct net_device
*dev
)
238 if (request_irq(dev
->irq
, simeth_interrupt
, 0, "simeth", dev
)) {
239 printk(KERN_WARNING
"simeth: unable to get IRQ %d.\n", dev
->irq
);
243 netif_start_queue(dev
);
248 /* copied from lapbether.c */
249 static __inline__
int dev_is_ethdev(struct net_device
*dev
)
251 return ( dev
->type
== ARPHRD_ETHER
&& strncmp(dev
->name
, "dummy", 5));
256 * Handler for IFF_UP or IFF_DOWN
258 * The reason for that is that we don't want to be interrupted when the
259 * interface is down. There is no way to unconnect in the simualtor. Instead
260 * we use this function to shutdown packet processing in the frame filter
261 * in the simulator. Thus no interrupts are generated
264 * That's also the place where we pass the IP address of this device to the
265 * simulator so that that we can start filtering packets for it
267 * There may be a better way of doing this, but I don't know which yet.
270 simeth_device_event(struct notifier_block
*this,unsigned long event
, void *ptr
)
272 struct net_device
*dev
= netdev_notifier_info_to_dev(ptr
);
273 struct simeth_local
*local
;
274 struct in_device
*in_dev
;
275 struct in_ifaddr
**ifap
= NULL
;
276 struct in_ifaddr
*ifa
= NULL
;
281 printk(KERN_WARNING
"simeth_device_event dev=0\n");
285 if (dev_net(dev
) != &init_net
)
288 if ( event
!= NETDEV_UP
&& event
!= NETDEV_DOWN
) return NOTIFY_DONE
;
291 * Check whether or not it's for an ethernet device
293 * XXX Fixme: This works only as long as we support one
294 * type of ethernet device.
296 if ( !dev_is_ethdev(dev
) ) return NOTIFY_DONE
;
298 if ((in_dev
=dev
->ip_ptr
) != NULL
) {
299 for (ifap
=&in_dev
->ifa_list
; (ifa
=*ifap
) != NULL
; ifap
=&ifa
->ifa_next
)
300 if (strcmp(dev
->name
, ifa
->ifa_label
) == 0) break;
303 printk(KERN_ERR
"simeth_open: can't find device %s's ifa\n", dev
->name
);
307 printk(KERN_INFO
"simeth_device_event: %s ipaddr=0x%x\n",
308 dev
->name
, ntohl(ifa
->ifa_local
));
312 * if the device was up, and we're simply reconfiguring it, not sure
313 * we get DOWN then UP.
316 local
= netdev_priv(dev
);
317 /* now do it for real */
318 r
= event
== NETDEV_UP
?
319 netdev_attach(local
->simfd
, dev
->irq
, ntohl(ifa
->ifa_local
)):
320 netdev_detach(local
->simfd
);
322 printk(KERN_INFO
"simeth: netdev_attach/detach: event=%s ->%d\n",
323 event
== NETDEV_UP
? "attach":"detach", r
);
329 simeth_close(struct net_device
*dev
)
331 netif_stop_queue(dev
);
333 free_irq(dev
->irq
, dev
);
339 * Only used for debug
342 frame_print(unsigned char *from
, unsigned char *frame
, int len
)
346 printk("%s: (%d) %02x", from
, len
, frame
[0] & 0xff);
347 for(i
=1; i
< 6; i
++ ) {
348 printk(":%02x", frame
[i
] &0xff);
350 printk(" %2x", frame
[6] &0xff);
351 for(i
=7; i
< 12; i
++ ) {
352 printk(":%02x", frame
[i
] &0xff);
354 printk(" [%02x%02x]\n", frame
[12], frame
[13]);
356 for(i
=14; i
< len
; i
++ ) {
357 printk("%02x ", frame
[i
] &0xff);
358 if ( (i
%10)==0) printk("\n");
365 * Function used to transmit of frame, very last one on the path before
366 * going to the simulator.
369 simeth_tx(struct sk_buff
*skb
, struct net_device
*dev
)
371 struct simeth_local
*local
= netdev_priv(dev
);
374 /* ensure we have at least ETH_ZLEN bytes (min frame size) */
375 unsigned int length
= ETH_ZLEN
< skb
->len
? skb
->len
: ETH_ZLEN
;
376 /* Where do the extra padding bytes comes from inthe skbuff ? */
378 /* the real driver in the host system is going to take care of that
379 * or maybe it's the NIC itself.
381 unsigned int length
= skb
->len
;
384 local
->stats
.tx_bytes
+= skb
->len
;
385 local
->stats
.tx_packets
++;
388 if (simeth_debug
> 5) frame_print("simeth_tx", skb
->data
, length
);
390 netdev_send(local
->simfd
, skb
->data
, length
);
393 * we are synchronous on write, so we don't simulate a
394 * trasnmit complete interrupt, thus we don't need to arm a tx
401 static inline struct sk_buff
*
402 make_new_skb(struct net_device
*dev
)
404 struct sk_buff
*nskb
;
407 * The +2 is used to make sure that the IP header is nicely
408 * aligned (on 4byte boundary I assume 14+2=16)
410 nskb
= dev_alloc_skb(SIMETH_FRAME_SIZE
+ 2);
411 if ( nskb
== NULL
) {
412 printk(KERN_NOTICE
"%s: memory squeeze. dropping packet.\n", dev
->name
);
416 skb_reserve(nskb
, 2); /* Align IP on 16 byte boundaries */
418 skb_put(nskb
,SIMETH_FRAME_SIZE
);
424 * called from interrupt handler to process a received frame
427 simeth_rx(struct net_device
*dev
)
429 struct simeth_local
*local
;
432 int rcv_count
= SIMETH_RECV_MAX
;
434 local
= netdev_priv(dev
);
436 * the loop concept has been borrowed from other drivers
437 * looks to me like it's a throttling thing to avoid pushing to many
438 * packets at one time into the stack. Making sure we can process them
439 * upstream and make forward progress overall
442 if ( (skb
=make_new_skb(dev
)) == NULL
) {
443 printk(KERN_NOTICE
"%s: memory squeeze. dropping packet.\n", dev
->name
);
444 local
->stats
.rx_dropped
++;
448 * Read only one frame at a time
450 len
= netdev_read(local
->simfd
, skb
->data
, SIMETH_FRAME_SIZE
);
452 if ( simeth_debug
> 0 ) printk(KERN_WARNING
"%s: count=%d netdev_read=0\n",
453 dev
->name
, SIMETH_RECV_MAX
-rcv_count
);
459 * Should really do a csum+copy here
461 skb_copy_to_linear_data(skb
, frame
, len
);
463 skb
->protocol
= eth_type_trans(skb
, dev
);
465 if ( simeth_debug
> 6 ) frame_print("simeth_rx", skb
->data
, len
);
468 * push the packet up & trigger software interrupt
472 local
->stats
.rx_packets
++;
473 local
->stats
.rx_bytes
+= len
;
475 } while ( --rcv_count
);
477 return len
; /* 0 = nothing left to read, otherwise, we can try again */
481 * Interrupt handler (Yes, we can do it too !!!)
484 simeth_interrupt(int irq
, void *dev_id
)
486 struct net_device
*dev
= dev_id
;
489 * very simple loop because we get interrupts only when receiving
491 while (simeth_rx(dev
));
495 static struct net_device_stats
*
496 simeth_get_stats(struct net_device
*dev
)
498 struct simeth_local
*local
= netdev_priv(dev
);
500 return &local
->stats
;
503 /* fake multicast ability */
505 set_multicast_list(struct net_device
*dev
)
507 printk(KERN_WARNING
"%s: set_multicast_list called\n", dev
->name
);
510 __initcall(simeth_probe
);