Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
awilliam
GitHub Repository: awilliam/linux-vfio
Path: blob/master/arch/ia64/hp/sim/simeth.c
15126 views
1
/*
2
* Simulated Ethernet Driver
3
*
4
* Copyright (C) 1999-2001, 2003 Hewlett-Packard Co
5
* Stephane Eranian <[email protected]>
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
#include <asm/hpsim.h>
26
27
#include "hpsim_ssc.h"
28
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
41
#define NETWORK_INTR 8
42
43
struct simeth_local {
44
struct net_device_stats stats;
45
int simfd; /* descriptor in the simulator */
46
};
47
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);
57
58
static 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
*/
75
static char *simeth_device="eth0"; /* default host interface to use */
76
77
78
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 */
81
82
/*
83
* Used to catch IFF_UP & IFF_DOWN events
84
*/
85
static struct notifier_block simeth_dev_notifier = {
86
simeth_device_event,
87
NULL
88
};
89
90
91
/*
92
* Function used when using a kernel command line option.
93
*
94
* Format: simeth=interface_name (like eth0)
95
*/
96
static int __init
97
simeth_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
110
int __init
111
simeth_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
124
static inline int
125
netdev_probe(char *name, unsigned char *ether)
126
{
127
return ia64_ssc(__pa(name), __pa(ether), 0,0, SSC_NETDEV_PROBE);
128
}
129
130
131
static inline int
132
netdev_connect(int irq)
133
{
134
/* XXX Fix me
135
* this does not support multiple cards
136
* also no return value
137
*/
138
ia64_ssc_connect_irq(NETWORK_INTR, irq);
139
return 0;
140
}
141
142
static inline int
143
netdev_attach(int fd, int irq, unsigned int ipaddr)
144
{
145
/* this puts the host interface in the right mode (start interrupting) */
146
return ia64_ssc(fd, ipaddr, 0,0, SSC_NETDEV_ATTACH);
147
}
148
149
150
static inline int
151
netdev_detach(int fd)
152
{
153
/*
154
* inactivate the host interface (don't interrupt anymore) */
155
return ia64_ssc(fd, 0,0,0, SSC_NETDEV_DETACH);
156
}
157
158
static inline int
159
netdev_send(int fd, unsigned char *buf, unsigned int len)
160
{
161
return ia64_ssc(fd, __pa(buf), len, 0, SSC_NETDEV_SEND);
162
}
163
164
static inline int
165
netdev_read(int fd, unsigned char *buf, unsigned int len)
166
{
167
return ia64_ssc(fd, __pa(buf), len, 0, SSC_NETDEV_RECV);
168
}
169
170
static const struct net_device_ops simeth_netdev_ops = {
171
.ndo_open = simeth_open,
172
.ndo_stop = simeth_close,
173
.ndo_start_xmit = simeth_tx,
174
.ndo_get_stats = simeth_get_stats,
175
.ndo_set_multicast_list = set_multicast_list, /* not yet used */
176
177
};
178
179
/*
180
* Function shared with module code, so cannot be in init section
181
*
182
* So far this function "detects" only one card (test_&_set) but could
183
* be extended easily.
184
*
185
* Return:
186
* - -ENODEV is no device found
187
* - -ENOMEM is no more memory
188
* - 0 otherwise
189
*/
190
static int
191
simeth_probe1(void)
192
{
193
unsigned char mac_addr[ETH_ALEN];
194
struct simeth_local *local;
195
struct net_device *dev;
196
int fd, i, err, rc;
197
198
/*
199
* XXX Fix me
200
* let's support just one card for now
201
*/
202
if (test_and_set_bit(0, &card_count))
203
return -ENODEV;
204
205
/*
206
* check with the simulator for the device
207
*/
208
fd = netdev_probe(simeth_device, mac_addr);
209
if (fd == -1)
210
return -ENODEV;
211
212
dev = alloc_etherdev(sizeof(struct simeth_local));
213
if (!dev)
214
return -ENOMEM;
215
216
memcpy(dev->dev_addr, mac_addr, sizeof(mac_addr));
217
218
local = netdev_priv(dev);
219
local->simfd = fd; /* keep track of underlying file descriptor */
220
221
dev->netdev_ops = &simeth_netdev_ops;
222
223
err = register_netdev(dev);
224
if (err) {
225
free_netdev(dev);
226
return err;
227
}
228
229
if ((rc = assign_irq_vector(AUTO_ASSIGN)) < 0)
230
panic("%s: out of interrupt vectors!\n", __func__);
231
dev->irq = rc;
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 (dev_net(dev) != &init_net)
303
return NOTIFY_DONE;
304
305
if ( event != NETDEV_UP && event != NETDEV_DOWN ) return NOTIFY_DONE;
306
307
/*
308
* Check whether or not it's for an ethernet device
309
*
310
* XXX Fixme: This works only as long as we support one
311
* type of ethernet device.
312
*/
313
if ( !dev_is_ethdev(dev) ) return NOTIFY_DONE;
314
315
if ((in_dev=dev->ip_ptr) != NULL) {
316
for (ifap=&in_dev->ifa_list; (ifa=*ifap) != NULL; ifap=&ifa->ifa_next)
317
if (strcmp(dev->name, ifa->ifa_label) == 0) break;
318
}
319
if ( ifa == NULL ) {
320
printk(KERN_ERR "simeth_open: can't find device %s's ifa\n", dev->name);
321
return NOTIFY_DONE;
322
}
323
324
printk(KERN_INFO "simeth_device_event: %s ipaddr=0x%x\n",
325
dev->name, ntohl(ifa->ifa_local));
326
327
/*
328
* XXX Fix me
329
* if the device was up, and we're simply reconfiguring it, not sure
330
* we get DOWN then UP.
331
*/
332
333
local = netdev_priv(dev);
334
/* now do it for real */
335
r = event == NETDEV_UP ?
336
netdev_attach(local->simfd, dev->irq, ntohl(ifa->ifa_local)):
337
netdev_detach(local->simfd);
338
339
printk(KERN_INFO "simeth: netdev_attach/detach: event=%s ->%d\n",
340
event == NETDEV_UP ? "attach":"detach", r);
341
342
return NOTIFY_DONE;
343
}
344
345
static int
346
simeth_close(struct net_device *dev)
347
{
348
netif_stop_queue(dev);
349
350
free_irq(dev->irq, dev);
351
352
return 0;
353
}
354
355
/*
356
* Only used for debug
357
*/
358
static void
359
frame_print(unsigned char *from, unsigned char *frame, int len)
360
{
361
int i;
362
363
printk("%s: (%d) %02x", from, len, frame[0] & 0xff);
364
for(i=1; i < 6; i++ ) {
365
printk(":%02x", frame[i] &0xff);
366
}
367
printk(" %2x", frame[6] &0xff);
368
for(i=7; i < 12; i++ ) {
369
printk(":%02x", frame[i] &0xff);
370
}
371
printk(" [%02x%02x]\n", frame[12], frame[13]);
372
373
for(i=14; i < len; i++ ) {
374
printk("%02x ", frame[i] &0xff);
375
if ( (i%10)==0) printk("\n");
376
}
377
printk("\n");
378
}
379
380
381
/*
382
* Function used to transmit of frame, very last one on the path before
383
* going to the simulator.
384
*/
385
static int
386
simeth_tx(struct sk_buff *skb, struct net_device *dev)
387
{
388
struct simeth_local *local = netdev_priv(dev);
389
390
#if 0
391
/* ensure we have at least ETH_ZLEN bytes (min frame size) */
392
unsigned int length = ETH_ZLEN < skb->len ? skb->len : ETH_ZLEN;
393
/* Where do the extra padding bytes comes from inthe skbuff ? */
394
#else
395
/* the real driver in the host system is going to take care of that
396
* or maybe it's the NIC itself.
397
*/
398
unsigned int length = skb->len;
399
#endif
400
401
local->stats.tx_bytes += skb->len;
402
local->stats.tx_packets++;
403
404
405
if (simeth_debug > 5) frame_print("simeth_tx", skb->data, length);
406
407
netdev_send(local->simfd, skb->data, length);
408
409
/*
410
* we are synchronous on write, so we don't simulate a
411
* trasnmit complete interrupt, thus we don't need to arm a tx
412
*/
413
414
dev_kfree_skb(skb);
415
return NETDEV_TX_OK;
416
}
417
418
static inline struct sk_buff *
419
make_new_skb(struct net_device *dev)
420
{
421
struct sk_buff *nskb;
422
423
/*
424
* The +2 is used to make sure that the IP header is nicely
425
* aligned (on 4byte boundary I assume 14+2=16)
426
*/
427
nskb = dev_alloc_skb(SIMETH_FRAME_SIZE + 2);
428
if ( nskb == NULL ) {
429
printk(KERN_NOTICE "%s: memory squeeze. dropping packet.\n", dev->name);
430
return NULL;
431
}
432
433
skb_reserve(nskb, 2); /* Align IP on 16 byte boundaries */
434
435
skb_put(nskb,SIMETH_FRAME_SIZE);
436
437
return nskb;
438
}
439
440
/*
441
* called from interrupt handler to process a received frame
442
*/
443
static int
444
simeth_rx(struct net_device *dev)
445
{
446
struct simeth_local *local;
447
struct sk_buff *skb;
448
int len;
449
int rcv_count = SIMETH_RECV_MAX;
450
451
local = netdev_priv(dev);
452
/*
453
* the loop concept has been borrowed from other drivers
454
* looks to me like it's a throttling thing to avoid pushing to many
455
* packets at one time into the stack. Making sure we can process them
456
* upstream and make forward progress overall
457
*/
458
do {
459
if ( (skb=make_new_skb(dev)) == NULL ) {
460
printk(KERN_NOTICE "%s: memory squeeze. dropping packet.\n", dev->name);
461
local->stats.rx_dropped++;
462
return 0;
463
}
464
/*
465
* Read only one frame at a time
466
*/
467
len = netdev_read(local->simfd, skb->data, SIMETH_FRAME_SIZE);
468
if ( len == 0 ) {
469
if ( simeth_debug > 0 ) printk(KERN_WARNING "%s: count=%d netdev_read=0\n",
470
dev->name, SIMETH_RECV_MAX-rcv_count);
471
break;
472
}
473
#if 0
474
/*
475
* XXX Fix me
476
* Should really do a csum+copy here
477
*/
478
skb_copy_to_linear_data(skb, frame, len);
479
#endif
480
skb->protocol = eth_type_trans(skb, dev);
481
482
if ( simeth_debug > 6 ) frame_print("simeth_rx", skb->data, len);
483
484
/*
485
* push the packet up & trigger software interrupt
486
*/
487
netif_rx(skb);
488
489
local->stats.rx_packets++;
490
local->stats.rx_bytes += len;
491
492
} while ( --rcv_count );
493
494
return len; /* 0 = nothing left to read, otherwise, we can try again */
495
}
496
497
/*
498
* Interrupt handler (Yes, we can do it too !!!)
499
*/
500
static irqreturn_t
501
simeth_interrupt(int irq, void *dev_id)
502
{
503
struct net_device *dev = dev_id;
504
505
/*
506
* very simple loop because we get interrupts only when receiving
507
*/
508
while (simeth_rx(dev));
509
return IRQ_HANDLED;
510
}
511
512
static struct net_device_stats *
513
simeth_get_stats(struct net_device *dev)
514
{
515
struct simeth_local *local = netdev_priv(dev);
516
517
return &local->stats;
518
}
519
520
/* fake multicast ability */
521
static void
522
set_multicast_list(struct net_device *dev)
523
{
524
printk(KERN_WARNING "%s: set_multicast_list called\n", dev->name);
525
}
526
527
__initcall(simeth_probe);
528
529